stateful_component_test.dart 2.06 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
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8 9
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
10

11
import 'test_widgets.dart';
12

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

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

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

33
    checkTree(kBoxDecorationA);
34

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

42
    checkTree(kBoxDecorationB);
43

44
    flipStatefulWidget(tester);
45

46
    await tester.pump();
47

48
    checkTree(kBoxDecorationA);
49

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

    checkTree(kBoxDecorationB);
58 59
  });

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

69
    expect(TestBuildCounter.buildCount, equals(1));
70

71
    flipStatefulWidget(tester);
72

73
    await tester.pump();
74

75
    expect(TestBuildCounter.buildCount, equals(1));
76
  });
77
}