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 15 16

    void checkTree(BoxDecoration expectedDecoration) {
      SingleChildRenderObjectElement element = tester.element(
        find.byElementPredicate((Element element) => element is SingleChildRenderObjectElement)
17
      );
18 19 20 21 22
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
      RenderDecoratedBox renderObject = element.renderObject;
      expect(renderObject.decoration, equals(expectedDecoration));
    }
23

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

31
    checkTree(kBoxDecorationA);
32

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

40
    checkTree(kBoxDecorationB);
41

42
    flipStatefulWidget(tester);
43

44
    await tester.pump();
45

46
    checkTree(kBoxDecorationA);
47

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

    checkTree(kBoxDecorationB);
56 57
  });

58 59
  testWidgets('Don\'t rebuild subwidgets', (WidgetTester tester) async {
    await tester.pumpWidget(
60 61 62 63 64 65
      new FlipWidget(
        key: new Key('rebuild test'),
        left: new TestBuildCounter(),
        right: new DecoratedBox(decoration: kBoxDecorationB)
      )
    );
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
}