stateful_component_test.dart 2.28 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 13
void main() {
  test('Stateful component smoke test', () {
14 15 16 17
    testWidgets((WidgetTester tester) {

      void checkTree(BoxDecoration expectedDecoration) {
        OneChildRenderObjectElement element =
18
            tester.findElement((Element element) => element is OneChildRenderObjectElement);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        expect(element, isNotNull);
        expect(element.renderObject is RenderDecoratedBox, isTrue);
        RenderDecoratedBox renderObject = element.renderObject;
        expect(renderObject.decoration, equals(expectedDecoration));
      }

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationA),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );

      checkTree(kBoxDecorationA);

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationB),
          right: new DecoratedBox(decoration: kBoxDecorationA)
        )
      );

      checkTree(kBoxDecorationB);

      flipStatefulComponent(tester);

      tester.pump();

      checkTree(kBoxDecorationA);

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationA),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );

      checkTree(kBoxDecorationB);
    });
58 59
  });

60
  test('Don\'t rebuild subcomponents', () {
61 62 63 64 65 66 67 68
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
        new FlipComponent(
          key: new Key('rebuild test'), // this is so we don't get the state from the TestComponentConfig in the last test, but instead instantiate a new element with a new state.
          left: new TestBuildCounter(),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );
69

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

72
      flipStatefulComponent(tester);
73

74
      tester.pump();
75

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