stateful_component_test.dart 2.15 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 'package:test/test.dart';

10
import 'test_widgets.dart';
11

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

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

      tester.pumpWidget(
27
        new FlipWidget(
28 29 30 31 32 33 34 35
          left: new DecoratedBox(decoration: kBoxDecorationA),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );

      checkTree(kBoxDecorationA);

      tester.pumpWidget(
36
        new FlipWidget(
37 38 39 40 41 42 43
          left: new DecoratedBox(decoration: kBoxDecorationB),
          right: new DecoratedBox(decoration: kBoxDecorationA)
        )
      );

      checkTree(kBoxDecorationB);

44
      flipStatefulWidget(tester);
45 46 47 48 49 50

      tester.pump();

      checkTree(kBoxDecorationA);

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

      checkTree(kBoxDecorationB);
    });
59 60
  });

61
  test('Don\'t rebuild subwidgets', () {
62 63
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
64 65
        new FlipWidget(
          key: new Key('rebuild test'),
66 67 68 69
          left: new TestBuildCounter(),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );
70

71
      expect(TestBuildCounter.buildCount, equals(1));
72

73
      flipStatefulWidget(tester);
74

75
      tester.pump();
76

77 78
      expect(TestBuildCounter.buildCount, equals(1));
    });
79
  });
80
}