search_test.dart 26.7 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
// @dart = 2.8

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter/services.dart';
10 11
import 'package:flutter_test/flutter_test.dart';

12 13
import '../widgets/semantics_tester.dart';

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class MockClipboard {
  Object _clipboardData = <String, dynamic>{
    'text': null,
  };

  Future<dynamic> handleMethodCall(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'Clipboard.getData':
        return _clipboardData;
      case 'Clipboard.setData':
        _clipboardData = methodCall.arguments;
        break;
    }
  }
}

30
void main() {
31 32 33 34
  TestWidgetsFlutterBinding.ensureInitialized();
  final MockClipboard mockClipboard = MockClipboard();

  setUp(() async {
35
    // Fill the clipboard so that the Paste option is available in the text
36 37 38 39 40 41 42 43 44
    // selection menu.
    SystemChannels.platform.setMockMethodCallHandler(mockClipboard.handleMethodCall);
    await Clipboard.setData(const ClipboardData(text: 'Clipboard data'));
  });

  tearDown(() {
    SystemChannels.platform.setMockMethodCallHandler(null);
  });

45
  testWidgets('Can open and close search', (WidgetTester tester) async {
46
    final _TestSearchDelegate delegate = _TestSearchDelegate();
47 48
    final List<String> selectedResults = <String>[];

49
    await tester.pumpWidget(TestHomePage(
50 51 52 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
      delegate: delegate,
      results: selectedResults,
    ));

    // We are on the homepage
    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('HomeTitle'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);

    // Open search
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('HomeTitle'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);
    expect(selectedResults, hasLength(0));

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.focusNode.hasFocus, isTrue);

    // Close search
    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('HomeTitle'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);
    expect(selectedResults, <String>['Result']);
  });

81 82 83
  testWidgets('Can close search with system back button to return null', (WidgetTester tester) async {
    // regression test for https://github.com/flutter/flutter/issues/18145

84
    final _TestSearchDelegate delegate = _TestSearchDelegate();
85 86
    final List<String> selectedResults = <String>[];

87
    await tester.pumpWidget(TestHomePage(
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      delegate: delegate,
      results: selectedResults,
    ));

    // We are on the homepage
    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('HomeTitle'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);

    // Open search
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('HomeTitle'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);

    // Simulate system back button
    final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute'));
107
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    await tester.pumpAndSettle();

    expect(selectedResults, <void>[null]);

    // We are on the homepage again
    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('HomeTitle'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);

    // Open search again
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('HomeTitle'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);
  });

126 127 128 129 130 131 132 133 134
  testWidgets('Hint text color overridden', (WidgetTester tester) async {
    final _TestSearchDelegate delegate = _TestSearchDelegate();

    await tester.pumpWidget(TestHomePage(
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

135 136 137
    final TextField textField = tester.widget<TextField>(find.byType(TextField));
    final Color hintColor = textField.decoration.hintStyle.color;
    expect(hintColor, delegate.hintTextColor);
138 139
  });

140
  testWidgets('Requests suggestions', (WidgetTester tester) async {
141
    final _TestSearchDelegate delegate = _TestSearchDelegate();
142

143
    await tester.pumpWidget(TestHomePage(
144 145 146 147 148 149
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, '');
150 151
    expect(delegate.queriesForSuggestions.last, '');
    expect(delegate.queriesForResults, hasLength(0));
152 153

    // Type W o w into search field
154
    delegate.queriesForSuggestions.clear();
155 156 157 158 159 160 161 162 163 164
    await tester.enterText(find.byType(TextField), 'W');
    await tester.pumpAndSettle();
    expect(delegate.query, 'W');
    await tester.enterText(find.byType(TextField), 'Wo');
    await tester.pumpAndSettle();
    expect(delegate.query, 'Wo');
    await tester.enterText(find.byType(TextField), 'Wow');
    await tester.pumpAndSettle();
    expect(delegate.query, 'Wow');

165 166
    expect(delegate.queriesForSuggestions, <String>['W', 'Wo', 'Wow']);
    expect(delegate.queriesForResults, hasLength(0));
167 168 169
  });

  testWidgets('Shows Results and closes search', (WidgetTester tester) async {
170
    final _TestSearchDelegate delegate = _TestSearchDelegate();
171 172
    final List<String> selectedResults = <String>[];

173
    await tester.pumpWidget(TestHomePage(
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      delegate: delegate,
      results: selectedResults,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();
    await tester.enterText(find.byType(TextField), 'Wow');
    await tester.pumpAndSettle();
    await tester.tap(find.text('Suggestions'));
    await tester.pumpAndSettle();

    // We are on the results page for Wow
    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('HomeTitle'), findsNothing);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Results'), findsOneWidget);

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.focusNode.hasFocus, isFalse);
192
    expect(delegate.queriesForResults, <String>['Wow']);
193 194 195 196 197 198 199 200 201 202 203 204

    // Close search
    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('HomeTitle'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Results'), findsNothing);
    expect(selectedResults, <String>['Result']);
  });

205
  testWidgets('Can switch between results and suggestions', (WidgetTester tester) async {
206
    final _TestSearchDelegate delegate = _TestSearchDelegate();
207

208
    await tester.pumpWidget(TestHomePage(
209 210 211
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
212
    await tester.pumpAndSettle();
213 214 215 216 217 218

    // Showing suggestions
    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Results'), findsNothing);

    // Typing query Wow
219
    delegate.queriesForSuggestions.clear();
220
    await tester.enterText(find.byType(TextField), 'Wow');
221
    await tester.pumpAndSettle();
222 223

    expect(delegate.query, 'Wow');
224 225
    expect(delegate.queriesForSuggestions, <String>['Wow']);
    expect(delegate.queriesForResults, hasLength(0));
226 227

    await tester.tap(find.text('Suggestions'));
228
    await tester.pumpAndSettle();
229 230 231 232 233 234

    // Showing Results
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Results'), findsOneWidget);

    expect(delegate.query, 'Wow');
235 236
    expect(delegate.queriesForSuggestions, <String>['Wow']);
    expect(delegate.queriesForResults, <String>['Wow']);
237 238 239 240

    TextField textField = tester.widget(find.byType(TextField));
    expect(textField.focusNode.hasFocus, isFalse);

241
    // Tapping search field to go back to suggestions
242
    await tester.tap(find.byType(TextField));
243
    await tester.pumpAndSettle();
244 245 246 247 248 249

    textField = tester.widget(find.byType(TextField));
    expect(textField.focusNode.hasFocus, isTrue);

    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Results'), findsNothing);
250 251
    expect(delegate.queriesForSuggestions, <String>['Wow', 'Wow']);
    expect(delegate.queriesForResults, <String>['Wow']);
252 253

    await tester.enterText(find.byType(TextField), 'Foo');
254
    await tester.pumpAndSettle();
255 256

    expect(delegate.query, 'Foo');
257 258
    expect(delegate.queriesForSuggestions, <String>['Wow', 'Wow', 'Foo']);
    expect(delegate.queriesForResults, <String>['Wow']);
259 260 261

    // Go to results again
    await tester.tap(find.text('Suggestions'));
262
    await tester.pumpAndSettle();
263 264 265 266 267

    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Results'), findsOneWidget);

    expect(delegate.query, 'Foo');
268 269
    expect(delegate.queriesForSuggestions, <String>['Wow', 'Wow', 'Foo']);
    expect(delegate.queriesForResults, <String>['Wow', 'Foo']);
270 271 272 273 274

    textField = tester.widget(find.byType(TextField));
    expect(textField.focusNode.hasFocus, isFalse);
  });

275
  testWidgets('Fresh search always starts with empty query', (WidgetTester tester) async {
276
    final _TestSearchDelegate delegate = _TestSearchDelegate();
277

278
    await tester.pumpWidget(TestHomePage(
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, '');

    delegate.query = 'Foo';
    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, '');
  });

  testWidgets('Initial queries are honored', (WidgetTester tester) async {
296
    final _TestSearchDelegate delegate = _TestSearchDelegate();
297 298 299

    expect(delegate.query, '');

300
    await tester.pumpWidget(TestHomePage(
301 302 303 304 305 306 307 308 309 310 311
      delegate: delegate,
      passInInitialQuery: true,
      initialQuery: 'Foo',
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, 'Foo');
  });

  testWidgets('Initial query null re-used previous query', (WidgetTester tester) async {
312
    final _TestSearchDelegate delegate = _TestSearchDelegate();
313 314 315

    delegate.query = 'Foo';

316
    await tester.pumpWidget(TestHomePage(
317 318 319 320 321 322 323 324 325 326 327
      delegate: delegate,
      passInInitialQuery: true,
      initialQuery: null,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, 'Foo');
  });

  testWidgets('Changing query shows up in search field', (WidgetTester tester) async {
328
    final _TestSearchDelegate delegate = _TestSearchDelegate();
329

330
    await tester.pumpWidget(TestHomePage(
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
      delegate: delegate,
      passInInitialQuery: true,
      initialQuery: null,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    delegate.query = 'Foo';

    expect(find.text('Foo'), findsOneWidget);
    expect(find.text('Bar'), findsNothing);

    delegate.query = 'Bar';

    expect(find.text('Foo'), findsNothing);
    expect(find.text('Bar'), findsOneWidget);
  });

  testWidgets('transitionAnimation runs while search fades in/out', (WidgetTester tester) async {
350
    final _TestSearchDelegate delegate = _TestSearchDelegate();
351

352
    await tester.pumpWidget(TestHomePage(
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
      delegate: delegate,
      passInInitialQuery: true,
      initialQuery: null,
    ));

    // runs while search fades in
    expect(delegate.transitionAnimation.status, AnimationStatus.dismissed);
    await tester.tap(find.byTooltip('Search'));
    expect(delegate.transitionAnimation.status, AnimationStatus.forward);
    await tester.pumpAndSettle();
    expect(delegate.transitionAnimation.status, AnimationStatus.completed);

    // does not run while switching to results
    await tester.tap(find.text('Suggestions'));
    expect(delegate.transitionAnimation.status, AnimationStatus.completed);
    await tester.pumpAndSettle();
    expect(delegate.transitionAnimation.status, AnimationStatus.completed);

    // runs while search fades out
    await tester.tap(find.byTooltip('Back'));
    expect(delegate.transitionAnimation.status, AnimationStatus.reverse);
    await tester.pumpAndSettle();
    expect(delegate.transitionAnimation.status, AnimationStatus.dismissed);
  });

  testWidgets('Closing nested search returns to search', (WidgetTester tester) async {
    final List<String> nestedSearchResults = <String>[];
380
    final _TestSearchDelegate nestedSearchDelegate = _TestSearchDelegate(
381 382 383 384 385
      suggestions: 'Nested Suggestions',
      result: 'Nested Result',
    );

    final List<String> selectedResults = <String>[];
386
    final _TestSearchDelegate delegate = _TestSearchDelegate(
387
      actions: <Widget>[
388
        Builder(
389
          builder: (BuildContext context) {
390
            return IconButton(
391 392 393 394 395 396 397 398 399 400 401
              tooltip: 'Nested Search',
              icon: const Icon(Icons.search),
              onPressed: () async {
                final String result = await showSearch(
                  context: context,
                  delegate: nestedSearchDelegate,
                );
                nestedSearchResults.add(result);
              },
            );
          },
402
        ),
403 404 405
      ],
    );

406
    await tester.pumpWidget(TestHomePage(
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 442 443 444
      delegate: delegate,
      results: selectedResults,
    ));
    expect(find.text('HomeBody'), findsOneWidget);
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Nested Suggestions'), findsNothing);

    await tester.tap(find.byTooltip('Nested Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Nested Suggestions'), findsOneWidget);

    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();
    expect(nestedSearchResults, <String>['Nested Result']);

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Nested Suggestions'), findsNothing);

    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Nested Suggestions'), findsNothing);
    expect(selectedResults, <String>['Result']);
  });

  testWidgets('Closing search with nested search shown goes back to underlying route', (WidgetTester tester) async {
    _TestSearchDelegate delegate;
    final List<String> nestedSearchResults = <String>[];
445
    final _TestSearchDelegate nestedSearchDelegate = _TestSearchDelegate(
446 447 448
      suggestions: 'Nested Suggestions',
      result: 'Nested Result',
      actions: <Widget>[
449
        Builder(
450
          builder: (BuildContext context) {
451
            return IconButton(
452 453 454 455 456 457 458
              tooltip: 'Close Search',
              icon: const Icon(Icons.close),
              onPressed: () async {
                delegate.close(context, 'Result Foo');
              },
            );
          },
459
        ),
460 461 462 463
      ],
    );

    final List<String> selectedResults = <String>[];
464
    delegate = _TestSearchDelegate(
465
      actions: <Widget>[
466
        Builder(
467
          builder: (BuildContext context) {
468
            return IconButton(
469 470 471 472 473 474 475 476 477 478 479
              tooltip: 'Nested Search',
              icon: const Icon(Icons.search),
              onPressed: () async {
                final String result = await showSearch(
                  context: context,
                  delegate: nestedSearchDelegate,
                );
                nestedSearchResults.add(result);
              },
            );
          },
480
        ),
481 482 483
      ],
    );

484
    await tester.pumpWidget(TestHomePage(
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
      delegate: delegate,
      results: selectedResults,
    ));

    expect(find.text('HomeBody'), findsOneWidget);
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Nested Suggestions'), findsNothing);

    await tester.tap(find.byTooltip('Nested Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsNothing);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Nested Suggestions'), findsOneWidget);

    await tester.tap(find.byTooltip('Close Search'));
    await tester.pumpAndSettle();

    expect(find.text('HomeBody'), findsOneWidget);
    expect(find.text('Suggestions'), findsNothing);
    expect(find.text('Nested Suggestions'), findsNothing);
510
    expect(nestedSearchResults, <String>[null]);
511 512
    expect(selectedResults, <String>['Result Foo']);
  });
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  testWidgets('Custom searchFieldLabel value', (WidgetTester tester) async {
    const String searchHint = 'custom search hint';
    final String defaultSearchHint = const DefaultMaterialLocalizations().searchFieldLabel;

    final _TestSearchDelegate delegate = _TestSearchDelegate(searchHint: searchHint);

    await tester.pumpWidget(TestHomePage(
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text(searchHint), findsOneWidget);
    expect(find.text(defaultSearchHint), findsNothing);
  });

  testWidgets('Default searchFieldLabel is used when it is set to null', (WidgetTester tester) async {
    final String searchHint = const DefaultMaterialLocalizations().searchFieldLabel;

    final _TestSearchDelegate delegate = _TestSearchDelegate();

    await tester.pumpWidget(TestHomePage(
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(find.text(searchHint), findsOneWidget);
  });

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  testWidgets('Custom searchFieldStyle value', (WidgetTester tester) async {
    const TextStyle searchStyle = TextStyle(color: Colors.red, fontSize: 3);

    final _TestSearchDelegate delegate = _TestSearchDelegate(searchFieldStyle: searchStyle);

    await tester.pumpWidget(
      TestHomePage(
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    final TextField textField = tester.widget<TextField>(find.byType(TextField));
    final TextStyle hintStyle = textField.decoration.hintStyle;
    expect(hintStyle, delegate.searchFieldStyle);
  });

561
  testWidgets('keyboard show search button by default', (WidgetTester tester) async {
562
    final _TestSearchDelegate delegate = _TestSearchDelegate();
563

564
    await tester.pumpWidget(TestHomePage(
565 566 567 568 569 570 571 572 573
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    await tester.showKeyboard(find.byType(TextField));

    expect(tester.testTextInput.setClientArgs['inputAction'], TextInputAction.search.toString());
  });
574

575 576 577 578 579 580 581 582 583 584 585 586
  testWidgets('Custom textInputAction results in keyboard with corresponding button', (WidgetTester tester) async {
    final _TestSearchDelegate delegate = _TestSearchDelegate(textInputAction: TextInputAction.done);

    await tester.pumpWidget(TestHomePage(
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();
    await tester.showKeyboard(find.byType(TextField));
    expect(tester.testTextInput.setClientArgs['inputAction'], TextInputAction.done.toString());
  });

587
  group('contributes semantics', () {
588
    TestSemantics buildExpected({ String routeName }) {
589
      return TestSemantics.root(
590
        children: <TestSemantics>[
591
          TestSemantics(
592 593 594
            id: 1,
            textDirection: TextDirection.ltr,
            children: <TestSemantics>[
595
              TestSemantics(
596
                id: 2,
597
                children: <TestSemantics>[
598
                  TestSemantics(
599 600 601 602 603 604 605
                    id: 7,
                    flags: <SemanticsFlag>[
                      SemanticsFlag.scopesRoute,
                      SemanticsFlag.namesRoute,
                    ],
                    label: routeName,
                    textDirection: TextDirection.ltr,
606
                    children: <TestSemantics>[
607
                      TestSemantics(
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
                        id: 9,
                        children: <TestSemantics>[
                          TestSemantics(
                            id: 10,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                            label: 'Back',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            id: 11,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.isTextField,
                              SemanticsFlag.isFocused,
                              SemanticsFlag.isHeader,
                              if (debugDefaultTargetPlatformOverride != TargetPlatform.iOS &&
                                debugDefaultTargetPlatformOverride != TargetPlatform.macOS) SemanticsFlag.namesRoute,
                            ],
                            actions: <SemanticsAction>[
                              SemanticsAction.tap,
                              SemanticsAction.setSelection,
                              SemanticsAction.paste,
                            ],
                            label: 'Search',
                            textDirection: TextDirection.ltr,
                            textSelection: const TextSelection(baseOffset: 0, extentOffset: 0),
                          ),
                        ],
                      ),
                      TestSemantics(
                        id: 8,
644 645
                        flags: <SemanticsFlag>[
                          SemanticsFlag.hasEnabledState,
646
                          SemanticsFlag.isButton,
647
                          SemanticsFlag.isEnabled,
648
                          SemanticsFlag.isFocusable,
649 650
                        ],
                        actions: <SemanticsAction>[SemanticsAction.tap],
651
                        label: 'Suggestions',
652 653 654 655 656 657 658 659 660 661 662 663 664
                        textDirection: TextDirection.ltr,
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      );
    }

    testWidgets('includes routeName on Android', (WidgetTester tester) async {
665 666 667
      final SemanticsTester semantics = SemanticsTester(tester);
      final _TestSearchDelegate delegate = _TestSearchDelegate();
      await tester.pumpWidget(TestHomePage(
668 669 670 671 672 673 674 675 676 677 678 679
        delegate: delegate,
      ));

      await tester.tap(find.byTooltip('Search'));
      await tester.pumpAndSettle();

      expect(semantics, hasSemantics(buildExpected(routeName: 'Search'),
          ignoreId: true, ignoreRect: true, ignoreTransform: true));

      semantics.dispose();
    });

Dan Field's avatar
Dan Field committed
680
    testWidgets('does not include routeName', (WidgetTester tester) async {
681 682 683
      final SemanticsTester semantics = SemanticsTester(tester);
      final _TestSearchDelegate delegate = _TestSearchDelegate();
      await tester.pumpWidget(TestHomePage(
684 685 686 687 688 689 690 691 692 693
        delegate: delegate,
      ));

      await tester.tap(find.byTooltip('Search'));
      await tester.pumpAndSettle();

      expect(semantics, hasSemantics(buildExpected(routeName: ''),
          ignoreId: true, ignoreRect: true, ignoreTransform: true));

      semantics.dispose();
Dan Field's avatar
Dan Field committed
694
    }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
695
  });
696 697 698 699
}

class TestHomePage extends StatelessWidget {
  const TestHomePage({
700
    Key key,
701 702
    this.results,
    this.delegate,
703
    this.passInInitialQuery = false,
704
    this.initialQuery,
705
  }) : super(key: key);
706 707 708 709 710 711 712 713

  final List<String> results;
  final SearchDelegate<String> delegate;
  final bool passInInitialQuery;
  final String initialQuery;

  @override
  Widget build(BuildContext context) {
714
    return MaterialApp(
715
      home: Builder(builder: (BuildContext context) {
716 717
        return Scaffold(
          appBar: AppBar(
718 719
            title: const Text('HomeTitle'),
            actions: <Widget>[
720
              IconButton(
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
                tooltip: 'Search',
                icon: const Icon(Icons.search),
                onPressed: () async {
                  String selectedResult;
                  if (passInInitialQuery) {
                    selectedResult = await showSearch<String>(
                      context: context,
                      delegate: delegate,
                      query: initialQuery,
                    );
                  } else {
                    selectedResult = await showSearch<String>(
                      context: context,
                      delegate: delegate,
                    );
                  }
                  results?.add(selectedResult);
                },
              ),
            ],
          ),
          body: const Text('HomeBody'),
        );
      }),
    );
  }
}

class _TestSearchDelegate extends SearchDelegate<String> {
  _TestSearchDelegate({
751 752 753
    this.suggestions = 'Suggestions',
    this.result = 'Result',
    this.actions = const <Widget>[],
754
    TextStyle searchFieldStyle,
755 756
    String searchHint,
    TextInputAction textInputAction = TextInputAction.search,
757
  }) : super(searchFieldLabel: searchHint, textInputAction: textInputAction, searchFieldStyle: searchFieldStyle);
758 759 760 761

  final String suggestions;
  final String result;
  final List<Widget> actions;
762 763 764 765 766 767 768 769 770
  final Color hintTextColor = Colors.green;

  @override
  ThemeData appBarTheme(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return theme.copyWith(
      inputDecorationTheme: InputDecorationTheme(hintStyle: TextStyle(color: hintTextColor)),
    );
  }
771 772 773

  @override
  Widget buildLeading(BuildContext context) {
774
    return IconButton(
775 776 777 778 779 780 781 782
      tooltip: 'Back',
      icon: const Icon(Icons.arrow_back),
      onPressed: () {
        close(context, result);
      },
    );
  }

783 784
  final List<String> queriesForSuggestions = <String>[];
  final List<String> queriesForResults = <String>[];
785 786 787

  @override
  Widget buildSuggestions(BuildContext context) {
788
    queriesForSuggestions.add(query);
789
    return MaterialButton(
790 791 792
      onPressed: () {
        showResults(context);
      },
793
      child: Text(suggestions),
794 795 796 797 798
    );
  }

  @override
  Widget buildResults(BuildContext context) {
799
    queriesForResults.add(query);
800 801 802 803 804 805 806
    return const Text('Results');
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    return actions;
  }
807
}