build_scope_test.dart 3.69 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/scheduler.dart';
7
import 'package:flutter/widgets.dart';
8 9
import 'package:test/test.dart';

Adam Barth's avatar
Adam Barth committed
10
import 'test_widgets.dart';
11 12 13 14 15 16 17 18

class ProbeWidget extends StatefulComponent {
  ProbeWidgetState createState() => new ProbeWidgetState();
}

class ProbeWidgetState extends State<ProbeWidget> {
  static int buildCount = 0;

19 20
  void initState() {
    super.initState();
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    setState(() {});
  }

  void didUpdateConfig(ProbeWidget oldConfig) {
    setState(() {});
  }

  Widget build(BuildContext context) {
    setState(() {});
    buildCount++;
    return new Container();
  }
}

class BadWidget extends StatelessComponent {
  BadWidget(this.parentState);

  final State parentState;

  Widget build(BuildContext context) {
    parentState.setState(() {});
    return new Container();
  }
}

class BadWidgetParent extends StatefulComponent {
  BadWidgetParentState createState() => new BadWidgetParentState();
}

class BadWidgetParentState extends State<BadWidgetParent> {
  Widget build(BuildContext context) {
    return new BadWidget(this);
  }
}

class BadDisposeWidget extends StatefulComponent {
  BadDisposeWidgetState createState() => new BadDisposeWidgetState();
}

class BadDisposeWidgetState extends State<BadDisposeWidget> {
  Widget build(BuildContext context) {
    return new Container();
  }

  void dispose() {
    setState(() {});
    super.dispose();
  }
}

void main() {
  dynamic cachedException;

Hixie's avatar
Hixie committed
74 75 76 77
  // ** WARNING **
  // THIS TEST OVERRIDES THE NORMAL EXCEPTION HANDLING
  // AND DOES NOT REPORT EXCEPTIONS FROM THE FRAMEWORK

78 79 80 81 82
  setUp(() {
    assert(cachedException == null);
    debugWidgetsExceptionHandler = (String context, dynamic exception, StackTrace stack) {
      cachedException = exception;
    };
Hixie's avatar
Hixie committed
83
    debugSchedulerExceptionHandler = (dynamic exception, StackTrace stack) { throw exception; };
84 85 86 87 88 89
  });

  tearDown(() {
    assert(cachedException == null);
    cachedException = null;
    debugWidgetsExceptionHandler = null;
Hixie's avatar
Hixie committed
90
    debugSchedulerExceptionHandler = null;
91 92 93
  });

  test('Legal times for setState', () {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    testWidgets((WidgetTester tester) {
      GlobalKey flipKey = new GlobalKey();
      expect(ProbeWidgetState.buildCount, equals(0));
      tester.pumpWidget(new ProbeWidget());
      expect(ProbeWidgetState.buildCount, equals(1));
      tester.pumpWidget(new ProbeWidget());
      expect(ProbeWidgetState.buildCount, equals(2));
      tester.pumpWidget(new FlipComponent(
        key: flipKey,
        left: new Container(),
        right: new ProbeWidget()
      ));
      expect(ProbeWidgetState.buildCount, equals(2));
      (flipKey.currentState as FlipComponentState).flip();
      tester.pump();
      expect(ProbeWidgetState.buildCount, equals(3));
      (flipKey.currentState as FlipComponentState).flip();
      tester.pump();
      expect(ProbeWidgetState.buildCount, equals(3));
      tester.pumpWidget(new Container());
      expect(ProbeWidgetState.buildCount, equals(3));
    });
116 117 118
  });

  test('Setting parent state during build is forbidden', () {
119 120 121 122 123 124 125 126
    testWidgets((WidgetTester tester) {
      expect(cachedException, isNull);
      tester.pumpWidget(new BadWidgetParent());
      expect(cachedException, isNotNull);
      cachedException = null;
      tester.pumpWidget(new Container());
      expect(cachedException, isNull);
    });
127 128 129
  });

  test('Setting state during dispose is forbidden', () {
130 131 132 133 134 135
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new BadDisposeWidget());
      expect(() {
        tester.pumpWidget(new Container());
      }, throws);
    });
136 137
  });
}