stateful_component_test.dart 2.04 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 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.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 19
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
20
      final RenderDecoratedBox renderObject = element.renderObject;
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 59
  testWidgets('Don\'t rebuild subwidgets', (WidgetTester tester) async {
    await tester.pumpWidget(
60
      FlipWidget(
61
        key: const Key('rebuild test'),
62
        left: TestBuildCounter(),
63
        right: const 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
}