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

5 6
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8

9
import 'test_widgets.dart';
10

11
void main() {
12
  testWidgets('Stateful widget smoke test', (WidgetTester tester) async {
13 14

    void checkTree(BoxDecoration expectedDecoration) {
15
      final SingleChildRenderObjectElement element = tester.element(
16
        find.byElementPredicate((Element element) => element is SingleChildRenderObjectElement),
17
      );
18
      expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
19
      expect(element.renderObject, isA<RenderDecoratedBox>());
20
      final RenderDecoratedBox renderObject = element.renderObject as RenderDecoratedBox;
21 22
      expect(renderObject.decoration, equals(expectedDecoration));
    }
23

24
    await tester.pumpWidget(
25
      const FlipWidget(
26
        left: DecoratedBox(decoration: kBoxDecorationA),
27
        right: DecoratedBox(decoration: kBoxDecorationB),
28
      ),
29
    );
30

31
    checkTree(kBoxDecorationA);
32

33
    await tester.pumpWidget(
34
      const FlipWidget(
35
        left: DecoratedBox(decoration: kBoxDecorationB),
36
        right: DecoratedBox(decoration: kBoxDecorationA),
37
      ),
38
    );
39

40
    checkTree(kBoxDecorationB);
41

42
    flipStatefulWidget(tester);
43

44
    await tester.pump();
45

46
    checkTree(kBoxDecorationA);
47

48
    await tester.pumpWidget(
49
      const FlipWidget(
50
        left: DecoratedBox(decoration: kBoxDecorationA),
51
        right: DecoratedBox(decoration: kBoxDecorationB),
52
      ),
53 54 55
    );

    checkTree(kBoxDecorationB);
56 57
  });

58
  testWidgets("Don't rebuild subwidgets", (WidgetTester tester) async {
59
    await tester.pumpWidget(
60 61
      const FlipWidget(
        key: Key('rebuild test'),
62
        left: TestBuildCounter(),
63
        right: DecoratedBox(decoration: kBoxDecorationB),
64
      ),
65
    );
66

67
    expect(TestBuildCounter.buildCount, equals(1));
68

69
    flipStatefulWidget(tester);
70

71
    await tester.pump();
72

73
    expect(TestBuildCounter.buildCount, equals(1));
74
  });
75
}