search_test.dart 24.8 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
import 'package:flutter/foundation.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/services.dart';
8 9
import 'package:flutter_test/flutter_test.dart';

10 11
import '../widgets/semantics_tester.dart';

12 13
void main() {
  testWidgets('Can open and close search', (WidgetTester tester) async {
14
    final _TestSearchDelegate delegate = _TestSearchDelegate();
15 16
    final List<String> selectedResults = <String>[];

17
    await tester.pumpWidget(TestHomePage(
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
      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']);
  });

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

52
    final _TestSearchDelegate delegate = _TestSearchDelegate();
53 54
    final List<String> selectedResults = <String>[];

55
    await tester.pumpWidget(TestHomePage(
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      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'));
75
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    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);
  });

94 95 96 97 98 99 100 101 102
  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();

103 104 105
    final TextField textField = tester.widget<TextField>(find.byType(TextField));
    final Color hintColor = textField.decoration.hintStyle.color;
    expect(hintColor, delegate.hintTextColor);
106 107
  });

108
  testWidgets('Requests suggestions', (WidgetTester tester) async {
109
    final _TestSearchDelegate delegate = _TestSearchDelegate();
110

111
    await tester.pumpWidget(TestHomePage(
112 113 114 115 116 117
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
    await tester.pumpAndSettle();

    expect(delegate.query, '');
118 119
    expect(delegate.queriesForSuggestions.last, '');
    expect(delegate.queriesForResults, hasLength(0));
120 121

    // Type W o w into search field
122
    delegate.queriesForSuggestions.clear();
123 124 125 126 127 128 129 130 131 132
    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');

133 134
    expect(delegate.queriesForSuggestions, <String>['W', 'Wo', 'Wow']);
    expect(delegate.queriesForResults, hasLength(0));
135 136 137
  });

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

141
    await tester.pumpWidget(TestHomePage(
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      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);
160
    expect(delegate.queriesForResults, <String>['Wow']);
161 162 163 164 165 166 167 168 169 170 171 172

    // 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']);
  });

173
  testWidgets('Can switch between results and suggestions', (WidgetTester tester) async {
174
    final _TestSearchDelegate delegate = _TestSearchDelegate();
175

176
    await tester.pumpWidget(TestHomePage(
177 178 179
      delegate: delegate,
    ));
    await tester.tap(find.byTooltip('Search'));
180
    await tester.pumpAndSettle();
181 182 183 184 185 186

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

    // Typing query Wow
187
    delegate.queriesForSuggestions.clear();
188
    await tester.enterText(find.byType(TextField), 'Wow');
189
    await tester.pumpAndSettle();
190 191

    expect(delegate.query, 'Wow');
192 193
    expect(delegate.queriesForSuggestions, <String>['Wow']);
    expect(delegate.queriesForResults, hasLength(0));
194 195

    await tester.tap(find.text('Suggestions'));
196
    await tester.pumpAndSettle();
197 198 199 200 201 202

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

    expect(delegate.query, 'Wow');
203 204
    expect(delegate.queriesForSuggestions, <String>['Wow']);
    expect(delegate.queriesForResults, <String>['Wow']);
205 206 207 208

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

209
    // Tapping search field to go back to suggestions
210
    await tester.tap(find.byType(TextField));
211
    await tester.pumpAndSettle();
212 213 214 215 216 217

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

    expect(find.text('Suggestions'), findsOneWidget);
    expect(find.text('Results'), findsNothing);
218 219
    expect(delegate.queriesForSuggestions, <String>['Wow', 'Wow']);
    expect(delegate.queriesForResults, <String>['Wow']);
220 221

    await tester.enterText(find.byType(TextField), 'Foo');
222
    await tester.pumpAndSettle();
223 224

    expect(delegate.query, 'Foo');
225 226
    expect(delegate.queriesForSuggestions, <String>['Wow', 'Wow', 'Foo']);
    expect(delegate.queriesForResults, <String>['Wow']);
227 228 229

    // Go to results again
    await tester.tap(find.text('Suggestions'));
230
    await tester.pumpAndSettle();
231 232 233 234 235

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

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

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

243
  testWidgets('Fresh search allways starts with empty query', (WidgetTester tester) async {
244
    final _TestSearchDelegate delegate = _TestSearchDelegate();
245

246
    await tester.pumpWidget(TestHomePage(
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      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 {
264
    final _TestSearchDelegate delegate = _TestSearchDelegate();
265 266 267

    expect(delegate.query, '');

268
    await tester.pumpWidget(TestHomePage(
269 270 271 272 273 274 275 276 277 278 279
      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 {
280
    final _TestSearchDelegate delegate = _TestSearchDelegate();
281 282 283

    delegate.query = 'Foo';

284
    await tester.pumpWidget(TestHomePage(
285 286 287 288 289 290 291 292 293 294 295
      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 {
296
    final _TestSearchDelegate delegate = _TestSearchDelegate();
297

298
    await tester.pumpWidget(TestHomePage(
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      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 {
318
    final _TestSearchDelegate delegate = _TestSearchDelegate();
319

320
    await tester.pumpWidget(TestHomePage(
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
      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>[];
348
    final _TestSearchDelegate nestedSearchDelegate = _TestSearchDelegate(
349 350 351 352 353
      suggestions: 'Nested Suggestions',
      result: 'Nested Result',
    );

    final List<String> selectedResults = <String>[];
354
    final _TestSearchDelegate delegate = _TestSearchDelegate(
355
      actions: <Widget>[
356
        Builder(
357
          builder: (BuildContext context) {
358
            return IconButton(
359 360 361 362 363 364 365 366 367 368 369
              tooltip: 'Nested Search',
              icon: const Icon(Icons.search),
              onPressed: () async {
                final String result = await showSearch(
                  context: context,
                  delegate: nestedSearchDelegate,
                );
                nestedSearchResults.add(result);
              },
            );
          },
370
        ),
371 372 373
      ],
    );

374
    await tester.pumpWidget(TestHomePage(
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
      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>[];
413
    final _TestSearchDelegate nestedSearchDelegate = _TestSearchDelegate(
414 415 416
      suggestions: 'Nested Suggestions',
      result: 'Nested Result',
      actions: <Widget>[
417
        Builder(
418
          builder: (BuildContext context) {
419
            return IconButton(
420 421 422 423 424 425 426
              tooltip: 'Close Search',
              icon: const Icon(Icons.close),
              onPressed: () async {
                delegate.close(context, 'Result Foo');
              },
            );
          },
427
        ),
428 429 430 431
      ],
    );

    final List<String> selectedResults = <String>[];
432
    delegate = _TestSearchDelegate(
433
      actions: <Widget>[
434
        Builder(
435
          builder: (BuildContext context) {
436
            return IconButton(
437 438 439 440 441 442 443 444 445 446 447
              tooltip: 'Nested Search',
              icon: const Icon(Icons.search),
              onPressed: () async {
                final String result = await showSearch(
                  context: context,
                  delegate: nestedSearchDelegate,
                );
                nestedSearchResults.add(result);
              },
            );
          },
448
        ),
449 450 451
      ],
    );

452
    await tester.pumpWidget(TestHomePage(
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
      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);
478
    expect(nestedSearchResults, <String>[null]);
479 480
    expect(selectedResults, <String>['Result Foo']);
  });
481

482 483 484 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 510 511 512
  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);
  });

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

515
    await tester.pumpWidget(TestHomePage(
516 517 518 519 520 521 522 523 524
      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());
  });
525

526 527 528 529 530 531 532 533 534 535 536 537
  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());
  });

538
  group('contributes semantics', () {
539
    TestSemantics buildExpected({ String routeName }) {
540
      return TestSemantics.root(
541
        children: <TestSemantics>[
542
          TestSemantics(
543 544 545
            id: 1,
            textDirection: TextDirection.ltr,
            children: <TestSemantics>[
546
              TestSemantics(
547 548 549 550 551 552 553 554
                id: 7,
                flags: <SemanticsFlag>[
                  SemanticsFlag.scopesRoute,
                  SemanticsFlag.namesRoute,
                ],
                label: routeName,
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
555
                  TestSemantics(
556 557
                    id: 9,
                    children: <TestSemantics>[
558
                      TestSemantics(
559 560 561
                        id: 10,
                        flags: <SemanticsFlag>[
                          SemanticsFlag.hasEnabledState,
562
                          SemanticsFlag.isButton,
563
                          SemanticsFlag.isEnabled,
564
                          SemanticsFlag.isFocusable,
565 566 567 568 569
                        ],
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: 'Back',
                        textDirection: TextDirection.ltr,
                      ),
570
                      TestSemantics(
571 572 573 574 575
                        id: 11,
                        flags: <SemanticsFlag>[
                          SemanticsFlag.isTextField,
                          SemanticsFlag.isFocused,
                          SemanticsFlag.isHeader,
576
                          if (debugDefaultTargetPlatformOverride != TargetPlatform.iOS) SemanticsFlag.namesRoute,
577 578 579 580 581 582 583 584
                        ],
                        actions: <SemanticsAction>[
                          SemanticsAction.tap,
                          SemanticsAction.setSelection,
                          SemanticsAction.paste,
                        ],
                        label: 'Search',
                        textDirection: TextDirection.ltr,
585
                        textSelection: const TextSelection(baseOffset: 0, extentOffset: 0),
586 587 588
                      ),
                    ],
                  ),
589
                  TestSemantics(
590 591 592
                    id: 8,
                    flags: <SemanticsFlag>[
                      SemanticsFlag.hasEnabledState,
593
                      SemanticsFlag.isButton,
594
                      SemanticsFlag.isEnabled,
595
                      SemanticsFlag.isFocusable,
596 597 598 599 600
                    ],
                    actions: <SemanticsAction>[SemanticsAction.tap],
                    label: 'Suggestions',
                    textDirection: TextDirection.ltr,
                  ),
601 602 603 604 605 606 607 608 609
                ],
              ),
            ],
          ),
        ],
      );
    }

    testWidgets('includes routeName on Android', (WidgetTester tester) async {
610 611 612
      final SemanticsTester semantics = SemanticsTester(tester);
      final _TestSearchDelegate delegate = _TestSearchDelegate();
      await tester.pumpWidget(TestHomePage(
613 614 615 616 617 618 619 620 621 622 623 624 625 626
        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();
    });

    testWidgets('does not include routeName on iOS', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
627 628 629
      final SemanticsTester semantics = SemanticsTester(tester);
      final _TestSearchDelegate delegate = _TestSearchDelegate();
      await tester.pumpWidget(TestHomePage(
630 631 632 633 634 635 636 637 638 639 640 641 642
        delegate: delegate,
      ));

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

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

      debugDefaultTargetPlatformOverride = null;
      semantics.dispose();
    });
  });
643 644 645 646
}

class TestHomePage extends StatelessWidget {
  const TestHomePage({
647
    Key key,
648 649
    this.results,
    this.delegate,
650
    this.passInInitialQuery = false,
651
    this.initialQuery,
652
  }) : super(key: key);
653 654 655 656 657 658 659 660

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

  @override
  Widget build(BuildContext context) {
661
    return MaterialApp(
662
      home: Builder(builder: (BuildContext context) {
663 664
        return Scaffold(
          appBar: AppBar(
665 666
            title: const Text('HomeTitle'),
            actions: <Widget>[
667
              IconButton(
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
                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({
698 699 700
    this.suggestions = 'Suggestions',
    this.result = 'Result',
    this.actions = const <Widget>[],
701 702 703
    String searchHint,
    TextInputAction textInputAction = TextInputAction.search,
  }) : super(searchFieldLabel: searchHint, textInputAction: textInputAction);
704 705 706 707

  final String suggestions;
  final String result;
  final List<Widget> actions;
708 709 710 711 712 713 714 715 716
  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)),
    );
  }
717 718 719

  @override
  Widget buildLeading(BuildContext context) {
720
    return IconButton(
721 722 723 724 725 726 727 728
      tooltip: 'Back',
      icon: const Icon(Icons.arrow_back),
      onPressed: () {
        close(context, result);
      },
    );
  }

729 730
  final List<String> queriesForSuggestions = <String>[];
  final List<String> queriesForResults = <String>[];
731 732 733

  @override
  Widget buildSuggestions(BuildContext context) {
734
    queriesForSuggestions.add(query);
735
    return MaterialButton(
736 737 738
      onPressed: () {
        showResults(context);
      },
739
      child: Text(suggestions),
740 741 742 743 744
    );
  }

  @override
  Widget buildResults(BuildContext context) {
745
    queriesForResults.add(query);
746 747 748 749 750 751 752
    return const Text('Results');
  }

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