Flutter Engine
The Flutter Engine
SpellCheckPluginTest.java
Go to the documentation of this file.
1package io.flutter.plugin.editing;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.any;
5import static org.mockito.Mockito.eq;
6import static org.mockito.Mockito.isNull;
7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.never;
9import static org.mockito.Mockito.spy;
10import static org.mockito.Mockito.times;
11import static org.mockito.Mockito.verify;
12import static org.mockito.Mockito.when;
13
14import android.content.Context;
15import android.view.textservice.SentenceSuggestionsInfo;
16import android.view.textservice.SpellCheckerSession;
17import android.view.textservice.SuggestionsInfo;
18import android.view.textservice.TextInfo;
19import android.view.textservice.TextServicesManager;
20import androidx.test.ext.junit.runners.AndroidJUnit4;
21import io.flutter.embedding.engine.dart.DartExecutor;
22import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
23import io.flutter.plugin.common.BinaryMessenger;
24import io.flutter.plugin.common.MethodCall;
25import io.flutter.plugin.common.MethodChannel;
26import io.flutter.plugin.common.StandardMethodCodec;
27import java.nio.ByteBuffer;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.HashMap;
31import java.util.Locale;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.ArgumentCaptor;
35
36@RunWith(AndroidJUnit4.class)
38
39 private static void sendToBinaryMessageHandler(
40 BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) {
41 MethodCall methodCall = new MethodCall(method, args);
42 ByteBuffer encodedMethodCall = StandardMethodCodec.INSTANCE.encodeMethodCall(methodCall);
43 binaryMessageHandler.onMessage(
44 (ByteBuffer) encodedMethodCall.flip(), mock(BinaryMessenger.BinaryReply.class));
45 }
46
47 @SuppressWarnings("deprecation")
48 // setMessageHandler is deprecated.
49 @Test
51 ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
52 ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
53 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
54 SpellCheckChannel.SpellCheckMethodHandler mockHandler =
56 SpellCheckChannel spellCheckChannel = new SpellCheckChannel(mockBinaryMessenger);
57
58 spellCheckChannel.setSpellCheckMethodHandler(mockHandler);
59
60 verify(mockBinaryMessenger, times(1))
61 .setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());
62
63 BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
64 binaryMessageHandlerCaptor.getValue();
65
66 sendToBinaryMessageHandler(
67 binaryMessageHandler,
68 "SpellCheck.initiateSpellCheck",
69 Arrays.asList("en-US", "Hello, wrold!"));
70
71 verify(mockHandler)
72 .initiateSpellCheck(eq("en-US"), eq("Hello, wrold!"), any(MethodChannel.Result.class));
73 }
74
75 @Test
77 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
78 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
79 SpellCheckPlugin spellCheckPlugin =
80 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
81 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
82 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
83
84 when(fakeTextServicesManager.newSpellCheckerSession(
85 null, new Locale("en", "US"), spellCheckPlugin, true))
86 .thenReturn(fakeSpellCheckerSession);
87
88 spellCheckPlugin.initiateSpellCheck("en-US", "Hello, wrold!", mockResult);
89
90 verify(spellCheckPlugin).performSpellCheck("en-US", "Hello, wrold!");
91 }
92
93 @Test
95 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
96 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
97 SpellCheckPlugin spellCheckPlugin =
98 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
99 MethodChannel.Result mockPendingResult = mock(MethodChannel.Result.class);
100 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
101 spellCheckPlugin.pendingResult = mockPendingResult;
102
103 spellCheckPlugin.initiateSpellCheck("en-US", "Hello, wrold!", mockResult);
104
105 verify(mockResult).error("error", "Previous spell check request still pending.", null);
106 verify(spellCheckPlugin, never()).performSpellCheck("en-US", "Hello, wrold!");
107 }
108
109 @Test
111 Context fakeContext = mock(Context.class);
112 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
113 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
114 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
115 .thenReturn(fakeTextServicesManager);
116 SpellCheckPlugin spellCheckPlugin =
117 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
118 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
119
120 when(fakeTextServicesManager.newSpellCheckerSession(
121 null, new Locale("en", "US"), spellCheckPlugin, true))
122 .thenReturn(fakeSpellCheckerSession);
123
124 spellCheckPlugin.performSpellCheck("en-US", "Hello, wrold!");
125 spellCheckPlugin.destroy();
126
127 verify(fakeSpellCheckChannel).setSpellCheckMethodHandler(isNull());
128 verify(fakeSpellCheckerSession).close();
129 }
130
131 @Test
133 Context fakeContext = mock(Context.class);
134 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
135 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
136 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
137 .thenReturn(fakeTextServicesManager);
138 SpellCheckPlugin spellCheckPlugin =
139 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
140 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
141 Locale english_US = new Locale("en", "US");
142
143 when(fakeTextServicesManager.newSpellCheckerSession(null, english_US, spellCheckPlugin, true))
144 .thenReturn(fakeSpellCheckerSession);
145
146 int maxSuggestions = 5;
147 ArgumentCaptor<TextInfo[]> textInfosCaptor = ArgumentCaptor.forClass(TextInfo[].class);
148 ArgumentCaptor<Integer> maxSuggestionsCaptor = ArgumentCaptor.forClass(Integer.class);
149
150 spellCheckPlugin.performSpellCheck("en-US", "Hello, wrold!");
151
152 verify(fakeSpellCheckerSession)
153 .getSentenceSuggestions(textInfosCaptor.capture(), maxSuggestionsCaptor.capture());
154 assertEquals("Hello, wrold!", textInfosCaptor.getValue()[0].getText());
155 assertEquals(Integer.valueOf(maxSuggestions), maxSuggestionsCaptor.getValue());
156 }
157
158 @Test
160 Context fakeContext = mock(Context.class);
161 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
162 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
163 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
164 .thenReturn(fakeTextServicesManager);
165 SpellCheckPlugin spellCheckPlugin =
166 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
167 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
168 Locale english_US = new Locale("en", "US");
169
170 when(fakeTextServicesManager.newSpellCheckerSession(null, english_US, spellCheckPlugin, true))
171 .thenReturn(fakeSpellCheckerSession);
172
173 spellCheckPlugin.performSpellCheck("en-US", "Hello, worl!");
174 spellCheckPlugin.performSpellCheck("en-US", "Hello, world!");
175
176 verify(fakeTextServicesManager, times(1))
177 .newSpellCheckerSession(null, english_US, spellCheckPlugin, true);
178 }
179
180 @Test
182 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
183 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
184 SpellCheckPlugin spellCheckPlugin =
185 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
186 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
187 spellCheckPlugin.pendingResult = mockResult;
188
189 spellCheckPlugin.onGetSentenceSuggestions(new SentenceSuggestionsInfo[] {});
190
191 verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
192 }
193
194 @Test
196 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
197 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
198 SpellCheckPlugin spellCheckPlugin =
199 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
200 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
201 spellCheckPlugin.pendingResult = mockResult;
202
203 spellCheckPlugin.onGetSentenceSuggestions(
204 new SentenceSuggestionsInfo[] {
205 new SentenceSuggestionsInfo(
206 (new SuggestionsInfo[] {
207 new SuggestionsInfo(
208 SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO,
209 new String[] {"world", "word", "old"})
210 }),
211 new int[] {7},
212 new int[] {5})
213 });
214
215 ArrayList<HashMap<String, Object>> expectedResults = new ArrayList<HashMap<String, Object>>();
216 HashMap<String, Object> expectedResult = new HashMap<String, Object>();
217
218 expectedResult.put(SpellCheckPlugin.START_INDEX_KEY, 7);
219 expectedResult.put(SpellCheckPlugin.END_INDEX_KEY, 12);
220 expectedResult.put(
222 new ArrayList<String>(Arrays.asList("world", "word", "old")));
223 expectedResults.add(expectedResult);
224
225 verify(mockResult).success(expectedResults);
226 }
227
228 @Test
230 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
231 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
232 SpellCheckPlugin spellCheckPlugin =
233 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
234 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
235 spellCheckPlugin.pendingResult = mockResult;
236
237 spellCheckPlugin.onGetSentenceSuggestions(
238 new SentenceSuggestionsInfo[] {
239 new SentenceSuggestionsInfo(
240 (new SuggestionsInfo[] {
241 new SuggestionsInfo(
242 SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO,
243 // This is the suggestion that may be provided by the Samsung spell checker:
244 new String[] {""})
245 }),
246 new int[] {7},
247 new int[] {5})
248 });
249
250 verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
251 }
252
253 @Test
255 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
256 SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
257 SpellCheckPlugin spellCheckPlugin =
258 spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
259 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
260 spellCheckPlugin.pendingResult = mockResult;
261
262 spellCheckPlugin.onGetSentenceSuggestions(
263 new SentenceSuggestionsInfo[] {
264 // This "suggestion" may be provided by the Samsung spell checker:
265 null
266 });
267
268 verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
269 }
270}
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18
void setSpellCheckMethodHandler( @Nullable SpellCheckMethodHandler spellCheckMethodHandler)
ByteBuffer encodeMethodCall(@NonNull MethodCall methodCall)
void performSpellCheck(@NonNull String locale, @NonNull String text)
void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results)
void initiateSpellCheck( @NonNull String locale, @NonNull String text, @NonNull MethodChannel.Result result)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530