focus_scope_test.dart 51 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui';

7 8
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
9
import 'package:flutter/semantics.dart';
10

11 12
class TestFocus extends StatefulWidget {
  const TestFocus({
13
    Key key,
14
    this.debugLabel,
15 16 17 18
    this.name = 'a',
    this.autofocus = false,
  }) : super(key: key);

19
  final String debugLabel;
20 21 22 23
  final String name;
  final bool autofocus;

  @override
24
  TestFocusState createState() => TestFocusState();
25 26
}

27
class TestFocusState extends State<TestFocus> {
28 29
  FocusNode focusNode;
  String _label;
30
  bool built = false;
31 32 33

  @override
  void dispose() {
34 35
    focusNode.removeListener(_updateLabel);
    focusNode?.dispose();
36 37 38
    super.dispose();
  }

39 40
  String get label => focusNode.hasFocus ? '${widget.name.toUpperCase()} FOCUSED' : widget.name.toLowerCase();

41 42 43 44
  @override
  void initState() {
    super.initState();
    focusNode = FocusNode(debugLabel: widget.debugLabel);
45 46 47 48 49 50 51 52
    _label = label;
    focusNode.addListener(_updateLabel);
  }

  void _updateLabel() {
    setState(() {
      _label = label;
    });
53 54
  }

55 56
  @override
  Widget build(BuildContext context) {
57
    built = true;
58 59 60 61
    return GestureDetector(
      onTap: () {
        FocusScope.of(context).requestFocus(focusNode);
      },
62 63 64 65 66 67 68 69
      child: Focus(
        autofocus: widget.autofocus,
        focusNode: focusNode,
        debugLabel: widget.debugLabel,
        child: Text(
          _label,
          textDirection: TextDirection.ltr,
        ),
70 71 72 73 74 75
      ),
    );
  }
}

void main() {
76 77 78
  group(FocusScope, () {
    testWidgets('Can focus', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> key = GlobalKey();
79

80 81 82
      await tester.pumpWidget(
        TestFocus(key: key, name: 'a'),
      );
83

84
      expect(key.currentState.focusNode.hasFocus, isFalse);
85

86 87
      FocusScope.of(key.currentContext).requestFocus(key.currentState.focusNode);
      await tester.pumpAndSettle();
88

89 90 91
      expect(key.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
    });
92

93 94 95 96 97
    testWidgets('Can unfocus', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      await tester.pumpWidget(
        Column(
98
          children: <Widget>[
99 100
            TestFocus(key: keyA, name: 'a'),
            TestFocus(key: keyB, name: 'b'),
101
          ],
102
        ),
103
      );
104

105 106 107 108
      expect(keyA.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
109

110 111
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      await tester.pumpAndSettle();
112

113 114 115 116
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
117

118 119 120
      // Set focus to the "B" node to unfocus the "A" node.
      FocusScope.of(keyB.currentContext).requestFocus(keyB.currentState.focusNode);
      await tester.pumpAndSettle();
121

122 123 124 125 126
      expect(keyA.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
    });
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    testWidgets('Autofocus works', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            TestFocus(key: keyA, name: 'a'),
            TestFocus(key: keyB, name: 'b', autofocus: true),
          ],
        ),
      );

      await tester.pump();

      expect(keyA.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
    });

148 149 150 151 152 153
    testWidgets('Can have multiple focused children and they update accordingly', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();

      await tester.pumpWidget(
        Column(
154
          children: <Widget>[
155 156 157 158 159 160 161 162
            TestFocus(
              key: keyA,
              name: 'a',
              autofocus: true,
            ),
            TestFocus(
              key: keyB,
              name: 'b',
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      );

      // Autofocus is delayed one frame.
      await tester.pump();
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
      await tester.tap(find.text('A FOCUSED'));
      await tester.pump();
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
      await tester.tap(find.text('b'));
      await tester.pump();
      expect(keyA.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
      await tester.tap(find.text('a'));
      await tester.pump();
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
    });

    // This moves a focus node first into a focus scope that is added to its
    // parent, and then out of that focus scope again.
    testWidgets('Can move focus in and out of FocusScope', (WidgetTester tester) async {
      final FocusScopeNode parentFocusScope = FocusScopeNode(debugLabel: 'Parent Scope Node');
      final FocusScopeNode childFocusScope = FocusScopeNode(debugLabel: 'Child Scope Node');
      final GlobalKey<TestFocusState> key = GlobalKey();

      // Initially create the focus inside of the parent FocusScope.
      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          autofocus: true,
          child: Column(
            children: <Widget>[
              TestFocus(
                key: key,
                name: 'a',
                debugLabel: 'Child',
              ),
            ],
          ),
        ),
      );

      expect(key.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
      FocusScope.of(key.currentContext).requestFocus(key.currentState.focusNode);
      await tester.pumpAndSettle();

      expect(key.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);

      expect(parentFocusScope, hasAGoodToStringDeep);
      expect(
        parentFocusScope.toStringDeep(),
230
        equalsIgnoringHashCodes('FocusScopeNode#00000(Parent Scope Node [IN FOCUS PATH])\n'
231
            ' │ context: FocusScope\n'
232
            ' │ IN FOCUS PATH\n'
233
            ' │ focusedChildren: FocusNode#00000(Child [PRIMARY FOCUS])\n'
234
            ' │\n'
235
            ' └─Child 1: FocusNode#00000(Child [PRIMARY FOCUS])\n'
236
            '     context: Focus\n'
237
            '     PRIMARY FOCUS\n'),
238 239
      );

240
      expect(FocusManager.instance.rootScope, hasAGoodToStringDeep);
241
      expect(
242
        FocusManager.instance.rootScope.toStringDeep(minLevel: DiagnosticLevel.info),
243
        equalsIgnoringHashCodes('FocusScopeNode#00000(Root Focus Scope [IN FOCUS PATH])\n'
244
            ' │ IN FOCUS PATH\n'
245 246
            ' │ focusedChildren: FocusScopeNode#00000(Parent Scope Node [IN FOCUS\n'
            ' │   PATH])\n'
247
            ' │\n'
248
            ' └─Child 1: FocusScopeNode#00000(Parent Scope Node [IN FOCUS PATH])\n'
249
            '   │ context: FocusScope\n'
250
            '   │ IN FOCUS PATH\n'
251
            '   │ focusedChildren: FocusNode#00000(Child [PRIMARY FOCUS])\n'
252
            '   │\n'
253
            '   └─Child 1: FocusNode#00000(Child [PRIMARY FOCUS])\n'
254
            '       context: Focus\n'
255
            '       PRIMARY FOCUS\n'),
256 257 258
      );

      // Add the child focus scope to the focus tree.
259
      final FocusAttachment childAttachment = childFocusScope.attach(key.currentContext);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
      parentFocusScope.setFirstFocus(childFocusScope);
      await tester.pumpAndSettle();
      expect(childFocusScope.isFirstFocus, isTrue);

      // Now add the child focus scope with no child focusable in it to the tree.
      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          child: Column(
            children: <Widget>[
              TestFocus(
                key: key,
                debugLabel: 'Child',
              ),
              FocusScope(
                debugLabel: 'Child Scope',
                node: childFocusScope,
                child: Container(),
              ),
            ],
          ),
        ),
      );

      expect(key.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);

      // Now move the existing focus node into the child focus scope.
      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          child: Column(
            children: <Widget>[
              FocusScope(
                debugLabel: 'Child Scope',
                node: childFocusScope,
                child: TestFocus(
                  key: key,
                  debugLabel: 'Child',
                ),
              ),
            ],
          ),
        ),
      );

      await tester.pumpAndSettle();

      expect(key.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);

      // Now remove the child focus scope.
      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          child: Column(
            children: <Widget>[
              TestFocus(
                key: key,
                debugLabel: 'Child',
              ),
            ],
          ),
        ),
      );
328

329 330 331
      await tester.pumpAndSettle();
      expect(key.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);
332

333 334 335 336 337
      // Must detach the child because we had to attach it in order to call
      // setFirstFocus before adding to the widget.
      childAttachment.detach();
    });

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    testWidgets('Setting first focus requests focus for the scope properly.', (WidgetTester tester) async {
      final FocusScopeNode parentFocusScope = FocusScopeNode(debugLabel: 'Parent Scope Node');
      final FocusScopeNode childFocusScope1 = FocusScopeNode(debugLabel: 'Child Scope Node 1');
      final FocusScopeNode childFocusScope2 = FocusScopeNode(debugLabel: 'Child Scope Node 2');
      final GlobalKey<TestFocusState> keyA = GlobalKey(debugLabel: 'Key A');
      final GlobalKey<TestFocusState> keyB = GlobalKey(debugLabel: 'Key B');
      final GlobalKey<TestFocusState> keyC = GlobalKey(debugLabel: 'Key C');

      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          child: Column(
            children: <Widget>[
              FocusScope(
                debugLabel: 'Child Scope 1',
                node: childFocusScope1,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      key: keyA,
                      name: 'a',
                      autofocus: true,
                      debugLabel: 'Child A',
                    ),
                    TestFocus(
                      key: keyB,
                      name: 'b',
                      debugLabel: 'Child B',
                    ),
                  ],
                ),
              ),
              FocusScope(
                debugLabel: 'Child Scope 2',
                node: childFocusScope2,
                child: TestFocus(
                  key: keyC,
                  name: 'c',
                  debugLabel: 'Child C',
                ),
              ),
            ],
          ),
        ),
      );

      await tester.pumpAndSettle();

      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);

      parentFocusScope.setFirstFocus(childFocusScope2);
      await tester.pumpAndSettle();

      expect(keyA.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('a'), findsOneWidget);

      parentFocusScope.setFirstFocus(childFocusScope1);
      await tester.pumpAndSettle();

      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);

      keyB.currentState.focusNode.requestFocus();
      await tester.pumpAndSettle();

      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
      expect(parentFocusScope.isFirstFocus, isTrue);
      expect(childFocusScope1.isFirstFocus, isTrue);

      parentFocusScope.setFirstFocus(childFocusScope2);
      await tester.pumpAndSettle();

      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
      expect(parentFocusScope.isFirstFocus, isTrue);
      expect(childFocusScope1.isFirstFocus, isFalse);
      expect(childFocusScope2.isFirstFocus, isTrue);

      keyC.currentState.focusNode.requestFocus();
      await tester.pumpAndSettle();

      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
      expect(keyC.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('C FOCUSED'), findsOneWidget);
      expect(parentFocusScope.isFirstFocus, isTrue);
      expect(childFocusScope1.isFirstFocus, isFalse);
      expect(childFocusScope2.isFirstFocus, isTrue);

      childFocusScope1.requestFocus();
      await tester.pumpAndSettle();
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
      expect(keyC.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('c'), findsOneWidget);
      expect(parentFocusScope.isFirstFocus, isTrue);
      expect(childFocusScope1.isFirstFocus, isTrue);
      expect(childFocusScope2.isFirstFocus, isFalse);
    });

    testWidgets('Removing focused widget moves focus to next widget', (WidgetTester tester) async {
442 443 444 445 446
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();

      await tester.pumpWidget(
        Column(
447
          children: <Widget>[
448 449 450 451 452 453 454
            TestFocus(
              key: keyA,
              name: 'a',
            ),
            TestFocus(
              key: keyB,
              name: 'b',
455 456 457
            ),
          ],
        ),
458
      );
459

460
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
461

462
      await tester.pumpAndSettle();
463

464 465 466 467 468 469 470
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);

      await tester.pumpWidget(
        Column(
471
          children: <Widget>[
472 473 474 475
            TestFocus(
              key: keyB,
              name: 'b',
            ),
476 477
          ],
        ),
478
      );
479

480
      await tester.pump();
481

482 483 484
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
    });
485

486 487 488 489
    testWidgets('Adding a new FocusScope attaches the child it to its parent.', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final FocusScopeNode parentFocusScope = FocusScopeNode(debugLabel: 'Parent Scope Node');
      final FocusScopeNode childFocusScope = FocusScopeNode(debugLabel: 'Child Scope Node');
490

491 492 493 494 495
      await tester.pumpWidget(
        FocusScope(
          node: childFocusScope,
          child: TestFocus(
            debugLabel: 'Child',
496
            key: keyA,
497
          ),
498 499
        ),
      );
500

501 502
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      expect(FocusScope.of(keyA.currentContext), equals(childFocusScope));
503
      expect(Focus.of(keyA.currentContext, scopeOk: true), equals(childFocusScope));
504
      FocusManager.instance.rootScope.setFirstFocus(FocusScope.of(keyA.currentContext));
505

506
      await tester.pumpAndSettle();
507

508 509 510
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(childFocusScope.isFirstFocus, isTrue);
511

512 513 514 515 516 517 518 519 520
      await tester.pumpWidget(
        FocusScope(
          node: parentFocusScope,
          child: FocusScope(
            node: childFocusScope,
            child: TestFocus(
              debugLabel: 'Child',
              key: keyA,
            ),
521
          ),
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        ),
      );

      await tester.pump();
      expect(childFocusScope.isFirstFocus, isTrue);
      // Node keeps it's focus when moved to the new scope.
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
    });

    // Arguably, this isn't correct behavior, but it is what happens now.
    testWidgets("Removing focused widget doesn't move focus to next widget within FocusScope", (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      final FocusScopeNode parentFocusScope = FocusScopeNode(debugLabel: 'Parent Scope');

      await tester.pumpWidget(
        FocusScope(
          debugLabel: 'Parent Scope',
          node: parentFocusScope,
          autofocus: true,
          child: Column(
            children: <Widget>[
              TestFocus(
                debugLabel: 'Widget A',
                key: keyA,
                name: 'a',
              ),
              TestFocus(
                debugLabel: 'Widget B',
                key: keyB,
                name: 'b',
              ),
            ],
          ),
        ),
      );

      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode scope = FocusScope.of(keyA.currentContext);
562
      FocusManager.instance.rootScope.setFirstFocus(scope);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

      await tester.pumpAndSettle();

      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);

      await tester.pumpWidget(
        FocusScope(
          node: parentFocusScope,
          child: Column(
            children: <Widget>[
              TestFocus(
                key: keyB,
                name: 'b',
              ),
            ],
          ),
        ),
      );
584

585
      await tester.pump();
586

587 588 589
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
    });
590

591 592 593 594 595 596
    testWidgets('Removing a FocusScope removes its node from the tree', (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      final GlobalKey<TestFocusState> scopeKeyA = GlobalKey();
      final GlobalKey<TestFocusState> scopeKeyB = GlobalKey();
      final FocusScopeNode parentFocusScope = FocusScopeNode(debugLabel: 'Parent Scope');
597

598 599 600
      // This checks both FocusScopes that have their own nodes, as well as those
      // that use external nodes.
      await tester.pumpWidget(
601
        FocusTraversalGroup(
602 603 604 605 606 607 608 609 610 611 612 613 614 615
          child: Column(
            children: <Widget>[
              FocusScope(
                key: scopeKeyA,
                node: parentFocusScope,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child A',
                      key: keyA,
                      name: 'a',
                    ),
                  ],
                ),
616
              ),
617 618 619 620 621 622 623 624 625 626 627
              FocusScope(
                key: scopeKeyB,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child B',
                      key: keyB,
                      name: 'b',
                    ),
                  ],
                ),
628
              ),
629 630
            ],
          ),
631
        ),
632
      );
633

634 635 636
      FocusScope.of(keyB.currentContext).requestFocus(keyB.currentState.focusNode);
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode aScope = FocusScope.of(keyA.currentContext);
637
      final FocusScopeNode bScope = FocusScope.of(keyB.currentContext);
638 639
      FocusManager.instance.rootScope.setFirstFocus(bScope);
      FocusManager.instance.rootScope.setFirstFocus(aScope);
640

641
      await tester.pumpAndSettle();
642

643 644 645 646 647
      expect(FocusScope.of(keyA.currentContext).isFirstFocus, isTrue);
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
648

649
      await tester.pumpWidget(Container());
650

651
      expect(FocusManager.instance.rootScope.children, isEmpty);
652
    });
653

654 655 656 657 658 659 660 661 662 663
    // By "pinned", it means kept in the tree by a GlobalKey.
    testWidgets("Removing pinned focused scope doesn't move focus to focused widget within next FocusScope", (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      final GlobalKey<TestFocusState> scopeKeyA = GlobalKey();
      final GlobalKey<TestFocusState> scopeKeyB = GlobalKey();
      final FocusScopeNode parentFocusScope1 = FocusScopeNode(debugLabel: 'Parent Scope 1');
      final FocusScopeNode parentFocusScope2 = FocusScopeNode(debugLabel: 'Parent Scope 2');

      await tester.pumpWidget(
664
        FocusTraversalGroup(
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
          child: Column(
            children: <Widget>[
              FocusScope(
                key: scopeKeyA,
                node: parentFocusScope1,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child A',
                      key: keyA,
                      name: 'a',
                    ),
                  ],
                ),
              ),
              FocusScope(
                key: scopeKeyB,
                node: parentFocusScope2,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child B',
                      key: keyB,
                      name: 'b',
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      );

      FocusScope.of(keyB.currentContext).requestFocus(keyB.currentState.focusNode);
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode bScope = FocusScope.of(keyB.currentContext);
      final FocusScopeNode aScope = FocusScope.of(keyA.currentContext);
702 703
      FocusManager.instance.rootScope.setFirstFocus(bScope);
      FocusManager.instance.rootScope.setFirstFocus(aScope);
704 705 706 707 708 709 710 711 712 713

      await tester.pumpAndSettle();

      expect(FocusScope.of(keyA.currentContext).isFirstFocus, isTrue);
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);

      await tester.pumpWidget(
714
        FocusTraversalGroup(
715 716 717 718 719 720 721 722
          child: Column(
            children: <Widget>[
              FocusScope(
                key: scopeKeyB,
                node: parentFocusScope2,
                child: Column(
                  children: <Widget>[
                    TestFocus(
723
                      debugLabel: 'Child B',
724 725 726 727 728 729 730 731 732 733 734 735 736 737
                      key: keyB,
                      name: 'b',
                      autofocus: true,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      );

      await tester.pump();

738 739
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
740 741
    });

742 743 744 745 746
    testWidgets("Removing unpinned focused scope doesn't move focus to focused widget within next FocusScope", (WidgetTester tester) async {
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
      final FocusScopeNode parentFocusScope1 = FocusScopeNode(debugLabel: 'Parent Scope 1');
      final FocusScopeNode parentFocusScope2 = FocusScopeNode(debugLabel: 'Parent Scope 2');
747

748
      await tester.pumpWidget(
749
        FocusTraversalGroup(
750 751 752 753 754 755 756 757 758 759 760 761 762
          child: Column(
            children: <Widget>[
              FocusScope(
                node: parentFocusScope1,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child A',
                      key: keyA,
                      name: 'a',
                    ),
                  ],
                ),
763
              ),
764 765 766 767 768 769 770 771 772 773 774
              FocusScope(
                node: parentFocusScope2,
                child: Column(
                  children: <Widget>[
                    TestFocus(
                      debugLabel: 'Child B',
                      key: keyB,
                      name: 'b',
                    ),
                  ],
                ),
775
              ),
776 777
            ],
          ),
778
        ),
779 780 781 782 783
      );

      FocusScope.of(keyB.currentContext).requestFocus(keyB.currentState.focusNode);
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode bScope = FocusScope.of(keyB.currentContext);
784
      final FocusScopeNode aScope = FocusScope.of(keyA.currentContext);
785 786
      FocusManager.instance.rootScope.setFirstFocus(bScope);
      FocusManager.instance.rootScope.setFirstFocus(aScope);
787 788 789 790 791 792 793 794 795 796

      await tester.pumpAndSettle();

      expect(FocusScope.of(keyA.currentContext).isFirstFocus, isTrue);
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);

      await tester.pumpWidget(
797
        FocusTraversalGroup(
798 799 800 801 802 803 804
          child: Column(
            children: <Widget>[
              FocusScope(
                node: parentFocusScope2,
                child: Column(
                  children: <Widget>[
                    TestFocus(
805
                      debugLabel: 'Child B',
806 807 808 809 810 811
                      key: keyB,
                      name: 'b',
                      autofocus: true,
                    ),
                  ],
                ),
812
              ),
813 814
            ],
          ),
815
        ),
816 817
      );
      await tester.pump();
818

819 820
      expect(keyB.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('B FOCUSED'), findsOneWidget);
821
    });
822

823 824 825 826 827
    testWidgets('Moving widget from one scope to another retains focus', (WidgetTester tester) async {
      final FocusScopeNode parentFocusScope1 = FocusScopeNode();
      final FocusScopeNode parentFocusScope2 = FocusScopeNode();
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
828

829 830 831 832 833 834 835 836 837 838 839 840 841
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            FocusScope(
              node: parentFocusScope1,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    key: keyA,
                    name: 'a',
                  ),
                ],
              ),
842
            ),
843 844 845 846 847 848 849 850 851 852
            FocusScope(
              node: parentFocusScope2,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    key: keyB,
                    name: 'b',
                  ),
                ],
              ),
853
            ),
854 855 856
          ],
        ),
      );
857

858 859
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode aScope = FocusScope.of(keyA.currentContext);
860
      FocusManager.instance.rootScope.setFirstFocus(aScope);
861

862
      await tester.pumpAndSettle();
863

864 865 866 867
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
868

869 870 871 872 873 874 875 876 877 878 879 880 881
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            FocusScope(
              node: parentFocusScope1,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    key: keyB,
                    name: 'b',
                  ),
                ],
              ),
882
            ),
883 884 885 886 887 888 889 890 891 892 893 894 895 896
            FocusScope(
              node: parentFocusScope2,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    key: keyA,
                    name: 'a',
                  ),
                ],
              ),
            ),
          ],
        ),
      );
897

898
      await tester.pump();
899

900 901 902 903 904
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
    });
905

906 907 908 909 910
    testWidgets('Moving FocusScopeNodes retains focus', (WidgetTester tester) async {
      final FocusScopeNode parentFocusScope1 = FocusScopeNode(debugLabel: 'Scope 1');
      final FocusScopeNode parentFocusScope2 = FocusScopeNode(debugLabel: 'Scope 2');
      final GlobalKey<TestFocusState> keyA = GlobalKey();
      final GlobalKey<TestFocusState> keyB = GlobalKey();
911

912 913 914 915 916 917 918 919 920 921 922 923 924 925
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            FocusScope(
              node: parentFocusScope1,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    debugLabel: 'Child A',
                    key: keyA,
                    name: 'a',
                  ),
                ],
              ),
926
            ),
927 928 929 930 931 932 933 934 935 936 937
            FocusScope(
              node: parentFocusScope2,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    debugLabel: 'Child B',
                    key: keyB,
                    name: 'b',
                  ),
                ],
              ),
938
            ),
939 940 941
          ],
        ),
      );
942

943 944
      FocusScope.of(keyA.currentContext).requestFocus(keyA.currentState.focusNode);
      final FocusScopeNode aScope = FocusScope.of(keyA.currentContext);
945
      FocusManager.instance.rootScope.setFirstFocus(aScope);
946

947
      await tester.pumpAndSettle();
948

949 950 951 952
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
      // This just swaps the FocusScopeNodes that the FocusScopes have in them.
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            FocusScope(
              node: parentFocusScope2,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    debugLabel: 'Child A',
                    key: keyA,
                    name: 'a',
                  ),
                ],
              ),
969
            ),
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
            FocusScope(
              node: parentFocusScope1,
              child: Column(
                children: <Widget>[
                  TestFocus(
                    debugLabel: 'Child B',
                    key: keyB,
                    name: 'b',
                  ),
                ],
              ),
            ),
          ],
        ),
      );
985

986
      await tester.pump();
987

988 989 990 991 992
      expect(keyA.currentState.focusNode.hasFocus, isTrue);
      expect(find.text('A FOCUSED'), findsOneWidget);
      expect(keyB.currentState.focusNode.hasFocus, isFalse);
      expect(find.text('b'), findsOneWidget);
    });
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    testWidgets('Can focus root node.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      await tester.pumpWidget(
        Focus(
          key: key1,
          child: Container(),
        ),
      );

      final Element firstElement = tester.element(find.byKey(key1));
      final FocusScopeNode rootNode = FocusScope.of(firstElement);
      rootNode.requestFocus();

      await tester.pump();

      expect(rootNode.hasFocus, isTrue);
      expect(rootNode, equals(firstElement.owner.focusManager.rootScope));
    });
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    testWidgets('Can autofocus a node.', (WidgetTester tester) async {
      final FocusNode focusNode = FocusNode(debugLabel: 'Test Node');
      await tester.pumpWidget(
        Focus(
          focusNode: focusNode,
          child: Container(),
        ),
      );

      await tester.pump();
      expect(focusNode.hasPrimaryFocus, isFalse);

      await tester.pumpWidget(
        Focus(
          autofocus: true,
          focusNode: focusNode,
          child: Container(),
        ),
      );

      await tester.pump();
      expect(focusNode.hasPrimaryFocus, isTrue);
    });
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    testWidgets("Won't autofocus a node if one is already focused.", (WidgetTester tester) async {
      final FocusNode focusNodeA = FocusNode(debugLabel: 'Test Node A');
      final FocusNode focusNodeB = FocusNode(debugLabel: 'Test Node B');
      await tester.pumpWidget(
        Column(
          children: <Widget>[
            Focus(
              focusNode: focusNodeA,
              autofocus: true,
              child: Container(),
            ),
          ],
        ),
      );

      await tester.pump();
      expect(focusNodeA.hasPrimaryFocus, isTrue);

      await tester.pumpWidget(
        Column(
          children: <Widget>[
            Focus(
              focusNode: focusNodeA,
              child: Container(),
            ),
            Focus(
              focusNode: focusNodeB,
              autofocus: true,
              child: Container(),
            ),
          ],
        ),
      );

      await tester.pump();
      expect(focusNodeB.hasPrimaryFocus, isFalse);
      expect(focusNodeA.hasPrimaryFocus, isTrue);
    });
1072 1073
  });
  group(Focus, () {
1074
    testWidgets('Focus.of stops at the nearest Focus widget.', (WidgetTester tester) async {
1075 1076 1077 1078 1079 1080
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      final GlobalKey key2 = GlobalKey(debugLabel: '2');
      final GlobalKey key3 = GlobalKey(debugLabel: '3');
      final GlobalKey key4 = GlobalKey(debugLabel: '4');
      final GlobalKey key5 = GlobalKey(debugLabel: '5');
      final GlobalKey key6 = GlobalKey(debugLabel: '6');
1081
      final FocusScopeNode scopeNode = FocusScopeNode();
1082
      await tester.pumpWidget(
1083
        FocusScope(
1084
          key: key1,
1085
          node: scopeNode,
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
          debugLabel: 'Key 1',
          child: Container(
            key: key2,
            child: Focus(
              debugLabel: 'Key 3',
              key: key3,
              child: Container(
                key: key4,
                child: Focus(
                  debugLabel: 'Key 5',
                  key: key5,
                  child: Container(
                    key: key6,
                  ),
1100
                ),
1101
              ),
1102 1103
            ),
          ),
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
        ),
      );
      final Element element1 = tester.element(find.byKey(key1));
      final Element element2 = tester.element(find.byKey(key2));
      final Element element3 = tester.element(find.byKey(key3));
      final Element element4 = tester.element(find.byKey(key4));
      final Element element5 = tester.element(find.byKey(key5));
      final Element element6 = tester.element(find.byKey(key6));
      final FocusNode root = element1.owner.focusManager.rootScope;

1114 1115 1116
      expect(Focus.of(element1, nullOk: true), isNull);
      expect(Focus.of(element2, nullOk: true), isNull);
      expect(Focus.of(element3, nullOk: true), isNull);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
      expect(Focus.of(element4).parent.parent, equals(root));
      expect(Focus.of(element5).parent.parent, equals(root));
      expect(Focus.of(element6).parent.parent.parent, equals(root));
    });
    testWidgets('Can traverse Focus children.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      final GlobalKey key2 = GlobalKey(debugLabel: '2');
      final GlobalKey key3 = GlobalKey(debugLabel: '3');
      final GlobalKey key4 = GlobalKey(debugLabel: '4');
      final GlobalKey key5 = GlobalKey(debugLabel: '5');
      final GlobalKey key6 = GlobalKey(debugLabel: '6');
      final GlobalKey key7 = GlobalKey(debugLabel: '7');
      final GlobalKey key8 = GlobalKey(debugLabel: '8');
      await tester.pumpWidget(
        Focus(
          child: Column(
            key: key1,
            children: <Widget>[
              Focus(
                key: key2,
                child: Container(
                  child: Focus(
                    key: key3,
                    child: Container(),
                  ),
1142
                ),
1143 1144 1145 1146 1147 1148 1149 1150
              ),
              Focus(
                key: key4,
                child: Container(
                  child: Focus(
                    key: key5,
                    child: Container(),
                  ),
1151
                ),
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
              ),
              Focus(
                key: key6,
                child: Column(
                  children: <Widget>[
                    Focus(
                      key: key7,
                      child: Container(),
                    ),
                    Focus(
                      key: key8,
                      child: Container(),
                    ),
                  ],
1166
                ),
1167 1168
              ),
            ],
1169
          ),
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
        ),
      );

      final Element firstScope = tester.element(find.byKey(key1));
      final List<FocusNode> nodes = <FocusNode>[];
      final List<Key> keys = <Key>[];
      bool visitor(FocusNode node) {
        nodes.add(node);
        keys.add(node.context.widget.key);
        return true;
      }

      await tester.pump();

      Focus.of(firstScope).descendants.forEach(visitor);
      expect(nodes.length, equals(7));
      expect(keys.length, equals(7));
      // Depth first.
      expect(keys, equals(<Key>[key3, key2, key5, key4, key7, key8, key6]));

      // Just traverses a sub-tree.
      final Element secondScope = tester.element(find.byKey(key7));
      nodes.clear();
      keys.clear();
      Focus.of(secondScope).descendants.forEach(visitor);
      expect(nodes.length, equals(2));
      expect(keys, equals(<Key>[key7, key8]));
    });
    testWidgets('Can set focus.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      bool gotFocus;
      await tester.pumpWidget(
        Focus(
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Container(key: key1),
        ),
      );

      final Element firstNode = tester.element(find.byKey(key1));
      final FocusNode node = Focus.of(firstNode);
      node.requestFocus();

      await tester.pump();

      expect(gotFocus, isTrue);
      expect(node.hasFocus, isTrue);
    });
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    testWidgets('Focus is ignored when set to not focusable.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      bool gotFocus;
      await tester.pumpWidget(
        Focus(
          canRequestFocus: false,
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Container(key: key1),
        ),
      );

      final Element firstNode = tester.element(find.byKey(key1));
      final FocusNode node = Focus.of(firstNode);
      node.requestFocus();

      await tester.pump();

      expect(gotFocus, isNull);
      expect(node.hasFocus, isFalse);
    });
    testWidgets('Focus is lost when set to not focusable.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      bool gotFocus;
      await tester.pumpWidget(
        Focus(
          autofocus: true,
          canRequestFocus: true,
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Container(key: key1),
        ),
      );

      Element firstNode = tester.element(find.byKey(key1));
      FocusNode node = Focus.of(firstNode);
      node.requestFocus();

      await tester.pump();

      expect(gotFocus, isTrue);
      expect(node.hasFocus, isTrue);

      gotFocus = null;
      await tester.pumpWidget(
        Focus(
          canRequestFocus: false,
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Container(key: key1),
        ),
      );

      firstNode = tester.element(find.byKey(key1));
      node = Focus.of(firstNode);
      node.requestFocus();

      await tester.pump();

      expect(gotFocus, false);
      expect(node.hasFocus, isFalse);
    });
    testWidgets('Child of unfocusable Focus can get focus.', (WidgetTester tester) async {
      final GlobalKey key1 = GlobalKey(debugLabel: '1');
      final GlobalKey key2 = GlobalKey(debugLabel: '2');
      final FocusNode focusNode = FocusNode();
      bool gotFocus;
      await tester.pumpWidget(
        Focus(
          canRequestFocus: false,
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Focus(key: key1, focusNode: focusNode, child: Container(key: key2)),
        ),
      );

      final Element childWidget = tester.element(find.byKey(key1));
      final FocusNode unfocusableNode = Focus.of(childWidget);
      unfocusableNode.requestFocus();

      await tester.pump();

      expect(gotFocus, isNull);
      expect(unfocusableNode.hasFocus, isFalse);

      final Element containerWidget = tester.element(find.byKey(key2));
      final FocusNode focusableNode = Focus.of(containerWidget);
      focusableNode.requestFocus();

      await tester.pump();

      expect(gotFocus, isTrue);
      expect(unfocusableNode.hasFocus, isTrue);
    });
1307 1308 1309 1310
  });
  testWidgets('Nodes are removed when all Focuses are removed.', (WidgetTester tester) async {
    final GlobalKey key1 = GlobalKey(debugLabel: '1');
    bool gotFocus;
1311
    await tester.pumpWidget(
1312 1313 1314 1315 1316
      FocusScope(
        child: Focus(
          onFocusChange: (bool focused) => gotFocus = focused,
          child: Container(key: key1),
        ),
1317 1318 1319
      ),
    );

1320 1321 1322
    final Element firstNode = tester.element(find.byKey(key1));
    final FocusNode node = Focus.of(firstNode);
    node.requestFocus();
1323

1324
    await tester.pump();
1325

1326 1327
    expect(gotFocus, isTrue);
    expect(node.hasFocus, isTrue);
1328

1329
    await tester.pumpWidget(Container());
1330

1331
    expect(FocusManager.instance.rootScope.descendants, isEmpty);
1332
  });
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
  testWidgets('Focus widgets set Semantics information about focus', (WidgetTester tester) async {
    final GlobalKey<TestFocusState> key = GlobalKey();

    await tester.pumpWidget(
      TestFocus(key: key, name: 'a'),
    );

    final SemanticsNode semantics = tester.getSemantics(find.byKey(key));

    expect(key.currentState.focusNode.hasFocus, isFalse);
    expect(semantics.hasFlag(SemanticsFlag.isFocused), isFalse);
    expect(semantics.hasFlag(SemanticsFlag.isFocusable), isTrue);

    FocusScope.of(key.currentContext).requestFocus(key.currentState.focusNode);
    await tester.pumpAndSettle();

    expect(key.currentState.focusNode.hasFocus, isTrue);
    expect(semantics.hasFlag(SemanticsFlag.isFocused), isTrue);
    expect(semantics.hasFlag(SemanticsFlag.isFocusable), isTrue);

    key.currentState.focusNode.canRequestFocus = false;
    await tester.pumpAndSettle();

    expect(key.currentState.focusNode.hasFocus, isFalse);
    expect(key.currentState.focusNode.canRequestFocus, isFalse);
    expect(semantics.hasFlag(SemanticsFlag.isFocused), isFalse);
    expect(semantics.hasFlag(SemanticsFlag.isFocusable), isFalse);
  });
  testWidgets('Setting canRequestFocus on focus node causes update.', (WidgetTester tester) async {
    final GlobalKey<TestFocusState> key = GlobalKey();

    final TestFocus testFocus = TestFocus(key: key, name: 'a');
    await tester.pumpWidget(
      testFocus,
    );

    await tester.pumpAndSettle();
    key.currentState.built = false;
    key.currentState.focusNode.canRequestFocus = false;
    await tester.pumpAndSettle();
    key.currentState.built = true;

    expect(key.currentState.focusNode.canRequestFocus, isFalse);
  });
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430

  testWidgets('canRequestFocus causes descendants of scope to be skipped.', (WidgetTester tester) async {
    final GlobalKey scope1 = GlobalKey(debugLabel: 'scope1');
    final GlobalKey scope2 = GlobalKey(debugLabel: 'scope2');
    final GlobalKey focus1 = GlobalKey(debugLabel: 'focus1');
    final GlobalKey focus2 = GlobalKey(debugLabel: 'focus2');
    final GlobalKey container1 = GlobalKey(debugLabel: 'container');
    Future<void> pumpTest({
      bool allowScope1 = true,
      bool allowScope2 = true,
      bool allowFocus1 = true,
      bool allowFocus2 = true,
    }) async {
      await tester.pumpWidget(
        FocusScope(
          key: scope1,
          canRequestFocus: allowScope1,
          child: FocusScope(
            key: scope2,
            canRequestFocus: allowScope2,
            child: Focus(
              key: focus1,
              canRequestFocus: allowFocus1,
              child: Focus(
                key: focus2,
                canRequestFocus: allowFocus2,
                child: Container(
                  key: container1,
                ),
              ),
            ),
          ),
        ),
      );
      await tester.pump();
    }

    // Check childless node (focus2).
    await pumpTest();
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);
    await pumpTest(allowFocus2: false);
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    await pumpTest();
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);

    // Check FocusNode with child (focus1). Shouldn't affect children.
    await pumpTest(allowFocus1: false);
1431
    expect(Focus.of(container1.currentContext).hasFocus, isTrue); // focus2 has focus.
1432 1433
    Focus.of(focus2.currentContext).requestFocus(); // Try to focus focus1
    await tester.pump();
1434
    expect(Focus.of(container1.currentContext).hasFocus, isTrue); // focus2 still has focus.
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
    Focus.of(container1.currentContext).requestFocus(); // Now try to focus focus2
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);
    await pumpTest();
    // Try again, now that we've set focus1's canRequestFocus to true again.
    Focus.of(container1.currentContext).unfocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);

    // Check FocusScopeNode with only FocusNode children (scope2). Should affect children.
    await pumpTest(allowScope2: false);
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    FocusScope.of(focus1.currentContext).requestFocus(); // Try to focus scope2
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(focus2.currentContext).requestFocus(); // Try to focus focus1
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(container1.currentContext).requestFocus(); // Try to focus focus2
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    await pumpTest();
    // Try again, now that we've set scope2's canRequestFocus to true again.
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);

    // Check FocusScopeNode with both FocusNode children and FocusScope children (scope1). Should affect children.
    await pumpTest(allowScope1: false);
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    FocusScope.of(scope2.currentContext).requestFocus(); // Try to focus scope1
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    FocusScope.of(focus1.currentContext).requestFocus(); // Try to focus scope2
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(focus2.currentContext).requestFocus(); // Try to focus focus1
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    Focus.of(container1.currentContext).requestFocus(); // Try to focus focus2
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isFalse);
    await pumpTest();
    // Try again, now that we've set scope1's canRequestFocus to true again.
    Focus.of(container1.currentContext).requestFocus();
    await tester.pump();
    expect(Focus.of(container1.currentContext).hasFocus, isTrue);
  });

  testWidgets('skipTraversal works as expected.', (WidgetTester tester) async {
    final FocusScopeNode scope1 = FocusScopeNode(debugLabel: 'scope1');
    final FocusScopeNode scope2 = FocusScopeNode(debugLabel: 'scope2');
    final FocusNode focus1 = FocusNode(debugLabel: 'focus1');
    final FocusNode focus2 = FocusNode(debugLabel: 'focus2');

    Future<void> pumpTest({
      bool traverseScope1 = false,
      bool traverseScope2 = false,
      bool traverseFocus1 = false,
      bool traverseFocus2 = false,
    }) async {
      await tester.pumpWidget(
        FocusScope(
          node: scope1,
          skipTraversal: traverseScope1,
          child: FocusScope(
            node: scope2,
            skipTraversal: traverseScope2,
            child: Focus(
              focusNode: focus1,
              skipTraversal: traverseFocus1,
              child: Focus(
                focusNode: focus2,
                skipTraversal: traverseFocus2,
                child: Container(),
              ),
            ),
          ),
        ),
      );
      await tester.pump();
    }

    await pumpTest();
    expect(scope1.traversalDescendants, equals(<FocusNode>[focus2, focus1, scope2]));

    // Check childless node (focus2).
    await pumpTest(traverseFocus2: true);
    expect(scope1.traversalDescendants, equals(<FocusNode>[focus1, scope2]));

    // Check FocusNode with child (focus1). Shouldn't affect children.
    await pumpTest(traverseFocus1: true);
    expect(scope1.traversalDescendants, equals(<FocusNode>[focus2, scope2]));

    // Check FocusScopeNode with only FocusNode children (scope2). Should affect children.
    await pumpTest(traverseScope2: true);
    expect(scope1.traversalDescendants, equals(<FocusNode>[focus2, focus1]));

    // Check FocusScopeNode with both FocusNode children and FocusScope children (scope1). Should affect children.
    await pumpTest(traverseScope1: true);
    expect(scope1.traversalDescendants, equals(<FocusNode>[focus2, focus1, scope2]));
  });
1540
}