focus_test.dart 5.29 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
class TestFocusable extends StatelessWidget {
9 10 11 12 13 14 15
  TestFocusable({
    GlobalKey key,
    this.no,
    this.yes,
    this.autofocus: true
  }) : super(key: key);

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

20
  @override
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
      child: new Text(focused ? yes : no)
    );
  }
}

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

84
  testWidgets('Can blur', (WidgetTester tester) async {
85 86
    GlobalKey keyFocus = new GlobalKey();
    GlobalKey keyA = new GlobalKey();
87
    await tester.pumpWidget(
88 89 90 91 92 93 94
      new Focus(
        key: keyFocus,
        child: new TestFocusable(
          key: keyA,
          no: 'a',
          yes: 'A FOCUSED',
          autofocus: false
95
        )
96 97
      )
    );
98

99 100
    expect(find.text('a'), findsOneWidget);
    expect(find.text('A FOCUSED'), findsNothing);
101

102
    Focus.moveTo(keyA);
103
    await tester.pump();
104

105 106
    expect(find.text('a'), findsNothing);
    expect(find.text('A FOCUSED'), findsOneWidget);
107

108
    Focus.clear(keyA.currentContext);
109
    await tester.pump();
110

111 112
    expect(find.text('a'), findsOneWidget);
    expect(find.text('A FOCUSED'), findsNothing);
113
  });
114

115
  testWidgets('Can move focus to scope', (WidgetTester tester) async {
116 117 118
    GlobalKey keyParentFocus = new GlobalKey();
    GlobalKey keyChildFocus = new GlobalKey();
    GlobalKey keyA = new GlobalKey();
119
    await tester.pumpWidget(
120 121 122
      new Focus(
        key: keyParentFocus,
        child: new Row(
123
          children: <Widget>[
124 125 126 127 128 129 130
            new TestFocusable(
              key: keyA,
              no: 'a',
              yes: 'A FOCUSED',
              autofocus: false
            )
          ]
131
        )
132 133 134 135 136 137 138
      )
    );

    expect(find.text('a'), findsOneWidget);
    expect(find.text('A FOCUSED'), findsNothing);

    Focus.moveTo(keyA);
139
    await tester.pump();
140 141 142 143 144 145

    expect(find.text('a'), findsNothing);
    expect(find.text('A FOCUSED'), findsOneWidget);

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

146
    await tester.pumpWidget(
147 148 149
      new Focus(
        key: keyParentFocus,
        child: new Row(
150
          children: <Widget>[
151 152 153 154 155 156 157 158 159 160 161
            new TestFocusable(
              key: keyA,
              no: 'a',
              yes: 'A FOCUSED',
              autofocus: false
            ),
            new Focus(
              key: keyChildFocus,
              child: new Container(
                width: 50.0,
                height: 50.0
162
              )
163 164
            )
          ]
165
        )
166 167 168 169 170 171
      )
    );

    expect(find.text('a'), findsOneWidget);
    expect(find.text('A FOCUSED'), findsNothing);

172
    await tester.pumpWidget(
173 174 175
      new Focus(
        key: keyParentFocus,
        child: new Row(
176
          children: <Widget>[
177 178 179 180 181 182 183
            new TestFocusable(
              key: keyA,
              no: 'a',
              yes: 'A FOCUSED',
              autofocus: false
            )
          ]
184
        )
185 186
      )
    );
187

188 189 190
    // Focus has received the removal notification but we haven't rebuilt yet.
    expect(find.text('a'), findsOneWidget);
    expect(find.text('A FOCUSED'), findsNothing);
191

192
    await tester.pump();
193

194 195
    expect(find.text('a'), findsNothing);
    expect(find.text('A FOCUSED'), findsOneWidget);
196
  });
Hixie's avatar
Hixie committed
197
}