test_widgets.dart 1.59 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
import 'package:flutter/widgets.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20
import 'package:test/test.dart';

final BoxDecoration kBoxDecorationA = new BoxDecoration(
  backgroundColor: const Color(0xFFFF0000)
);

final BoxDecoration kBoxDecorationB = new BoxDecoration(
  backgroundColor: const Color(0xFF00FF00)
);

final BoxDecoration kBoxDecorationC = new BoxDecoration(
  backgroundColor: const Color(0xFF0000FF)
);

21 22 23 24 25 26 27 28 29 30
class TestBuildCounter extends StatelessComponent {
  static int buildCount = 0;

  Widget build(BuildContext context) {
    ++buildCount;
    return new DecoratedBox(decoration: kBoxDecorationA);
  }
}


31 32 33 34 35 36
class FlipComponent extends StatefulComponent {
  FlipComponent({ Key key, this.left, this.right }) : super(key: key);

  final Widget left;
  final Widget right;

37
  FlipComponentState createState() => new FlipComponentState();
38 39
}

40
class FlipComponentState extends State<FlipComponent> {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  bool _showLeft = true;

  void flip() {
    setState(() {
      _showLeft = !_showLeft;
    });
  }

  Widget build(BuildContext context) {
    return _showLeft ? config.left : config.right;
  }
}

void flipStatefulComponent(WidgetTester tester) {
  StatefulComponentElement stateElement =
56
      tester.findElement((Element element) => element is StatefulComponentElement);
57 58 59 60 61
  expect(stateElement, isNotNull);
  expect(stateElement.state is FlipComponentState, isTrue);
  FlipComponentState state = stateElement.state;
  state.flip();
}