focus_test.dart 2.48 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';
Hixie's avatar
Hixie committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import 'package:test/test.dart';

class TestFocusable extends StatelessComponent {
  TestFocusable(this.no, this.yes, GlobalKey key) : super(key: key);
  final String no;
  final String yes;
  Widget build(BuildContext context) {
    bool focused = Focus.at(context, this);
    return new GestureDetector(
      onTap: () { Focus.moveTo(context, this); },
      child: new Text(focused ? yes : no)
    );
  }
}

void main() {
  test('Can have multiple focused children and they update accordingly', () {
    testWidgets((WidgetTester tester) {
      GlobalKey keyA = new GlobalKey();
      GlobalKey keyB = new GlobalKey();
      tester.pumpWidget(
        new Focus(
29
          child: new Column(<Widget>[
Hixie's avatar
Hixie committed
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 58 59 60 61 62 63 64 65 66
            // reverse these when you fix https://github.com/flutter/engine/issues/1495
            new TestFocusable('b', 'B FOCUSED', keyB),
            new TestFocusable('a', 'A FOCUSED', keyA),
          ])
        )
      );
      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);
      expect(tester.findText('b'),         isNotNull);
      expect(tester.findText('B FOCUSED'), isNull);
      tester.tap(tester.findText('A FOCUSED'));
      tester.pump();
      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);
      expect(tester.findText('b'),         isNotNull);
      expect(tester.findText('B FOCUSED'), isNull);
      tester.tap(tester.findText('A FOCUSED'));
      tester.pump();
      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);
      expect(tester.findText('b'),         isNotNull);
      expect(tester.findText('B FOCUSED'), isNull);
      tester.tap(tester.findText('b'));
      tester.pump();
      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);
      expect(tester.findText('b'),         isNull);
      expect(tester.findText('B FOCUSED'), isNotNull);
      tester.tap(tester.findText('a'));
      tester.pump();
      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);
      expect(tester.findText('b'),         isNotNull);
      expect(tester.findText('B FOCUSED'), isNull);
    });
  });
}