1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6import 'package:flutter_api_samples/material/autocomplete/autocomplete.3.dart' as example;
7import 'package:flutter_test/flutter_test.dart';
8
9void main() {
10 testWidgets('can search and find options after waiting for fake network delay and debounce delay', (WidgetTester tester) async {
11 await tester.pumpWidget(const example.AutocompleteExampleApp());
12
13 expect(find.text('aardvark'), findsNothing);
14 expect(find.text('bobcat'), findsNothing);
15 expect(find.text('chameleon'), findsNothing);
16
17 await tester.enterText(find.byType(TextFormField), 'a');
18 await tester.pump(example.fakeAPIDuration);
19
20 // No results yet, need to also wait for the debounce duration.
21 expect(find.text('aardvark'), findsNothing);
22 expect(find.text('bobcat'), findsNothing);
23 expect(find.text('chameleon'), findsNothing);
24
25 await tester.pump(example.debounceDuration);
26
27 expect(find.text('aardvark'), findsOneWidget);
28 expect(find.text('bobcat'), findsOneWidget);
29 expect(find.text('chameleon'), findsOneWidget);
30
31 await tester.enterText(find.byType(TextFormField), 'aa');
32 await tester.pump(example.debounceDuration + example.fakeAPIDuration);
33
34 expect(find.text('aardvark'), findsOneWidget);
35 expect(find.text('bobcat'), findsNothing);
36 expect(find.text('chameleon'), findsNothing);
37 });
38
39 testWidgets('debounce is reset each time a character is entered', (WidgetTester tester) async {
40 await tester.pumpWidget(const example.AutocompleteExampleApp());
41
42 await tester.enterText(find.byType(TextFormField), 'c');
43 await tester.pump(example.debounceDuration - const Duration(milliseconds: 100));
44
45 expect(find.text('aardvark'), findsNothing);
46 expect(find.text('bobcat'), findsNothing);
47 expect(find.text('chameleon'), findsNothing);
48
49 await tester.enterText(find.byType(TextFormField), 'ch');
50 await tester.pump(example.debounceDuration - const Duration(milliseconds: 100));
51
52 expect(find.text('aardvark'), findsNothing);
53 expect(find.text('bobcat'), findsNothing);
54 expect(find.text('chameleon'), findsNothing);
55
56 await tester.enterText(find.byType(TextFormField), 'cha');
57 await tester.pump(example.debounceDuration - const Duration(milliseconds: 100));
58
59 expect(find.text('aardvark'), findsNothing);
60 expect(find.text('bobcat'), findsNothing);
61 expect(find.text('chameleon'), findsNothing);
62
63 await tester.enterText(find.byType(TextFormField), 'cham');
64 await tester.pump(example.debounceDuration - const Duration(milliseconds: 100));
65
66 // Despite the total elapsed time being greater than debounceDuration +
67 // fakeAPIDuration, the search has not yet completed, because the debounce
68 // was reset each time text input happened.
69 expect(find.text('aardvark'), findsNothing);
70 expect(find.text('bobcat'), findsNothing);
71 expect(find.text('chameleon'), findsNothing);
72
73 await tester.enterText(find.byType(TextFormField), 'chame');
74 await tester.pump(example.debounceDuration + example.fakeAPIDuration);
75
76 expect(find.text('aardvark'), findsNothing);
77 expect(find.text('bobcat'), findsNothing);
78 expect(find.text('chameleon'), findsOneWidget);
79 });
80
81 testWidgets('multiple pending requests', (WidgetTester tester) async {
82 await tester.pumpWidget(const example.AutocompleteExampleApp());
83
84 await tester.enterText(find.byType(TextFormField), 'a');
85
86 // Wait until the debounce duration has expired, but the request is still
87 // pending.
88 await tester.pump(example.debounceDuration);
89
90 expect(find.text('aardvark'), findsNothing);
91 expect(find.text('bobcat'), findsNothing);
92 expect(find.text('chameleon'), findsNothing);
93
94 await tester.enterText(find.byType(TextFormField), 'aa');
95
96 // Wait until the first request has completed.
97 await tester.pump(example.fakeAPIDuration - example.debounceDuration);
98
99 // The results from the first request are thrown away since the query has
100 // changed.
101 expect(find.text('aardvark'), findsNothing);
102 expect(find.text('bobcat'), findsNothing);
103 expect(find.text('chameleon'), findsNothing);
104
105 // Wait until the second request has completed.
106 await tester.pump(example.fakeAPIDuration);
107
108 // The results of the second request are reflected.
109 expect(find.text('aardvark'), findsOneWidget);
110 expect(find.text('bobcat'), findsNothing);
111 expect(find.text('chameleon'), findsNothing);
112 });
113}
114

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com