set_state_test.dart 1.65 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.

5
import 'package:flutter/widgets.dart';
6 7 8
import 'package:test/test.dart';

import '../engine/mock_events.dart';
Adam Barth's avatar
Adam Barth committed
9
import 'widget_tester.dart';
10 11

class Inside extends StatefulComponent {
12 13
  InsideState createState() => new InsideState();
}
14

15 16
class InsideState extends State<Inside> {
  Widget build(BuildContext context) {
17 18 19 20 21 22
    return new Listener(
      onPointerDown: _handlePointerDown,
      child: new Text('INSIDE')
    );
  }

Adam Barth's avatar
Adam Barth committed
23
  void _handlePointerDown(_) {
24 25 26 27 28 29 30
    setState(() { });
  }
}

class Middle extends StatefulComponent {
  Middle({ this.child });

31
  final Inside child;
32

33 34 35 36 37
  MiddleState createState() => new MiddleState();
}

class MiddleState extends State<Middle> {
  Widget build(BuildContext context) {
38 39
    return new Listener(
      onPointerDown: _handlePointerDown,
40
      child: config.child
41 42 43
    );
  }

Adam Barth's avatar
Adam Barth committed
44
  void _handlePointerDown(_) {
45 46
    setState(() { });
  }
47
}
48

49 50
class Outside extends StatefulComponent {
  OutsideState createState() => new OutsideState();
51 52
}

53 54
class OutsideState extends State<Outside> {
  Widget build(BuildContext context) {
55 56 57 58 59 60
    return new Middle(child: new Inside());
  }
}

void main() {
  test('setState() smoke test', () {
61 62 63 64 65 66 67 68 69
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new Outside());
      TestPointer pointer = new TestPointer(1);
      Point location = tester.getCenter(tester.findText('INSIDE'));
      tester.dispatchEvent(pointer.down(location), location);
      tester.pump();
      tester.dispatchEvent(pointer.up(), location);
      tester.pump();
    });
70
  });
Adam Barth's avatar
Adam Barth committed
71
}