stateful_component_test.dart 2.08 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
    void checkTree(BoxDecoration expectedDecoration) {
14
      final SingleChildRenderObjectElement element = tester.element(
15
        find.byElementPredicate((Element element) => element is SingleChildRenderObjectElement && element.renderObject is! RenderView),
16
      );
17
      expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
18
      expect(element.renderObject, isA<RenderDecoratedBox>());
19
      final RenderDecoratedBox renderObject = element.renderObject as RenderDecoratedBox;
20 21
      expect(renderObject.decoration, equals(expectedDecoration));
    }
22

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

30
    checkTree(kBoxDecorationA);
31

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

39
    checkTree(kBoxDecorationB);
40

41
    flipStatefulWidget(tester);
42

43
    await tester.pump();
44

45
    checkTree(kBoxDecorationA);
46

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

    checkTree(kBoxDecorationB);
55 56
  });

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

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

68
    flipStatefulWidget(tester);
69

70
    await tester.pump();
71

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