focus_test.dart 5.7 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
import 'package:test/test.dart';

class TestFocusable extends StatelessComponent {
10 11 12 13 14 15 16
  TestFocusable({
    GlobalKey key,
    this.no,
    this.yes,
    this.autofocus: true
  }) : super(key: key);

Hixie's avatar
Hixie committed
17 18
  final String no;
  final String yes;
19 20
  final bool autofocus;

Hixie's avatar
Hixie committed
21
  Widget build(BuildContext context) {
22
    bool focused = Focus.at(context, autofocus: autofocus);
Hixie's avatar
Hixie committed
23
    return new GestureDetector(
24
      onTap: () { Focus.moveTo(key); },
Hixie's avatar
Hixie committed
25 26 27 28 29 30 31 32
      child: new Text(focused ? yes : no)
    );
  }
}

void main() {
  test('Can have multiple focused children and they update accordingly', () {
    testWidgets((WidgetTester tester) {
33
      GlobalKey keyFocus = new GlobalKey();
Hixie's avatar
Hixie committed
34 35 36 37
      GlobalKey keyA = new GlobalKey();
      GlobalKey keyB = new GlobalKey();
      tester.pumpWidget(
        new Focus(
38
          key: keyFocus,
39 40
          child: new Column(
            children: <Widget>[
41 42 43 44 45
              new TestFocusable(
                key: keyA,
                no: 'a',
                yes: 'A FOCUSED'
              ),
46 47 48 49 50
              new TestFocusable(
                key: keyB,
                no: 'b',
                yes: 'B FOCUSED'
              ),
51 52
            ]
          )
Hixie's avatar
Hixie committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        )
      );
      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);
    });
  });
85 86 87

  test('Can blur', () {
    testWidgets((WidgetTester tester) {
88
      GlobalKey keyFocus = new GlobalKey();
89 90 91
      GlobalKey keyA = new GlobalKey();
      tester.pumpWidget(
        new Focus(
92
          key: keyFocus,
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
          child: new TestFocusable(
            key: keyA,
            no: 'a',
            yes: 'A FOCUSED',
            autofocus: false
          )
        )
      );

      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);

      Focus.moveTo(keyA);
      tester.pump();

      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);

      Focus.clear(keyA.currentContext);
      tester.pump();

      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);
    });
  });
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  test('Can move focus to scope', () {
    testWidgets((WidgetTester tester) {
      GlobalKey keyParentFocus = new GlobalKey();
      GlobalKey keyChildFocus = new GlobalKey();
      GlobalKey keyA = new GlobalKey();
      tester.pumpWidget(
        new Focus(
          key: keyParentFocus,
          child: new Row(
            children: [
              new TestFocusable(
                key: keyA,
                no: 'a',
                yes: 'A FOCUSED',
                autofocus: false
              )
            ]
          )
        )
      );

      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);

      Focus.moveTo(keyA);
      tester.pump();

      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);

      Focus.moveScopeTo(keyChildFocus, context: keyA.currentContext);

      tester.pumpWidget(
        new Focus(
          key: keyParentFocus,
          child: new Row(
            children: [
              new TestFocusable(
                key: keyA,
                no: 'a',
                yes: 'A FOCUSED',
                autofocus: false
              ),
              new Focus(
                key: keyChildFocus,
                child: new Container(
                  width: 50.0,
                  height: 50.0
                )
              )
            ]
          )
        )
      );

      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);

      tester.pumpWidget(
        new Focus(
          key: keyParentFocus,
          child: new Row(
            children: [
              new TestFocusable(
                key: keyA,
                no: 'a',
                yes: 'A FOCUSED',
                autofocus: false
              )
            ]
          )
        )
      );

      // Focus has received the removal notification but we haven't rebuilt yet.
      expect(tester.findText('a'),         isNotNull);
      expect(tester.findText('A FOCUSED'), isNull);

      tester.pump();

      expect(tester.findText('a'),         isNull);
      expect(tester.findText('A FOCUSED'), isNotNull);
    });
  });
Hixie's avatar
Hixie committed
203
}