1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:spell_check/main.dart';
late DefaultSpellCheckService defaultSpellCheckService;
late Locale locale;
/// Waits to find [EditableText] that displays text with misspelled
/// words marked the same as the [TextSpan] provided and returns
/// true if it is found before timing out at 20 seconds.
Future<bool> findTextSpanTree(
WidgetTester tester,
TextSpan inlineSpan,
) async {
final RenderObject root = tester.renderObject(find.byType(EditableText));
expect(root, isNotNull);
RenderEditable? renderEditable;
void recursiveFinder(RenderObject child) {
if (child is RenderEditable && child.text == inlineSpan) {
renderEditable = child;
return;
}
child.visitChildren(recursiveFinder);
}
final DateTime endTime = tester.binding.clock.now().add(const Duration(seconds: 20));
do {
if (tester.binding.clock.now().isAfter(endTime)) {
return false;
}
await tester.pump(const Duration(seconds: 1));
root.visitChildren(recursiveFinder);
} while (renderEditable == null);
return true;
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
defaultSpellCheckService = DefaultSpellCheckService();
locale = const Locale('en', 'us');
});
test(
'fetchSpellCheckSuggestions returns null with no misspelled words',
() async {
const String text = 'Hello, world!';
final List<SuggestionSpan>? spellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
expect(spellCheckSuggestionSpans!.length, equals(0));
expect(
defaultSpellCheckService.lastSavedResults!.spellCheckedText,
equals(text)
);
expect(
defaultSpellCheckService.lastSavedResults!.suggestionSpans,
equals(spellCheckSuggestionSpans)
);
});
test(
'fetchSpellCheckSuggestions returns correct ranges with misspelled words',
() async {
const String text = 'Hlelo, world! Yuou are magnificente';
const List<TextRange> misspelledWordRanges = <TextRange>[
TextRange(start: 0, end: 5),
TextRange(start: 14, end: 18),
TextRange(start: 23, end: 35)
];
final List<SuggestionSpan>? spellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
expect(spellCheckSuggestionSpans, isNotNull);
expect(
spellCheckSuggestionSpans!.length,
equals(misspelledWordRanges.length)
);
for (int i = 0; i < misspelledWordRanges.length; i += 1) {
expect(
spellCheckSuggestionSpans[i].range,
equals(misspelledWordRanges[i])
);
}
expect(
defaultSpellCheckService.lastSavedResults!.spellCheckedText,
equals(text)
);
expect(
defaultSpellCheckService.lastSavedResults!.suggestionSpans,
equals(spellCheckSuggestionSpans)
);
});
test(
'fetchSpellCheckSuggestions does not correct results when Gboard not ignoring composing region',
() async {
const String text = 'Wwow, whaaett a beautiful day it is!';
final List<SuggestionSpan>? spellCheckSpansWithComposingRegion =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
expect(spellCheckSpansWithComposingRegion, isNotNull);
expect(spellCheckSpansWithComposingRegion!.length, equals(2));
final List<SuggestionSpan>? spellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
expect(
spellCheckSuggestionSpans,
equals(spellCheckSpansWithComposingRegion)
);
});
test(
'fetchSpellCheckSuggestions merges results when Gboard ignoring composing region',
() async {
const String text = 'Wooahha it is an amazzinng dayyebf!';
final List<SuggestionSpan>? modifiedSpellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
final List<SuggestionSpan> expectedSpellCheckSuggestionSpans =
List<SuggestionSpan>.from(modifiedSpellCheckSuggestionSpans!);
expect(modifiedSpellCheckSuggestionSpans, isNotNull);
expect(modifiedSpellCheckSuggestionSpans.length, equals(3));
// Remove one span to simulate Gboard attempting to un-ignore the composing region, after tapping away from "Yuou".
modifiedSpellCheckSuggestionSpans.removeAt(1);
defaultSpellCheckService.lastSavedResults =
SpellCheckResults(text, modifiedSpellCheckSuggestionSpans);
final List<SuggestionSpan>? spellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
expect(spellCheckSuggestionSpans, isNotNull);
expect(
spellCheckSuggestionSpans,
equals(expectedSpellCheckSuggestionSpans)
);
});
testWidgets('EditableText spell checks when text is entered and spell check enabled', (WidgetTester tester) async {
const TextStyle style = TextStyle();
const TextStyle misspelledTextStyle = TextField.materialMisspelledTextStyle;
await tester.pumpWidget(const MyApp());
await tester.enterText(find.byType(EditableText), 'Hey cfabiueq qocnakoef! Hey!');
const TextSpan expectedTextSpanTree = TextSpan(
style: style,
children: <TextSpan>[
TextSpan(style: style, text: 'Hey '),
TextSpan(style: misspelledTextStyle, text: 'cfabiueq'),
TextSpan(style: style, text: ' '),
TextSpan(style: misspelledTextStyle, text: 'qocnakoef'),
TextSpan(style: style, text: '! Hey!'),
]);
final bool expectedTextSpanTreeFound = await findTextSpanTree(tester, expectedTextSpanTree);
expect(expectedTextSpanTreeFound, isTrue);
});
test(
'fetchSpellCheckSuggestions returns null when there is a pending request',
() async {
final String text =
'neaf niofenaifn iofn iefnaoeifn ifneoa finoiafn inf ionfieaon ienf ifn ieonfaiofneionf oieafn oifnaioe nioenfio nefaion oifan' *
10;
defaultSpellCheckService.fetchSpellCheckSuggestions(locale, text);
final String modifiedText = text.substring(5);
final List<SuggestionSpan>? spellCheckSuggestionSpans =
await defaultSpellCheckService.fetchSpellCheckSuggestions(
locale, modifiedText);
expect(spellCheckSuggestionSpans, isNull);
// We expect it to be rare for the first request to complete before the
// second, so no text should be saved as of now.
expect(defaultSpellCheckService.lastSavedResults, null);
});
}