1package io.flutter.plugin.editing;
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;
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;
33import org.junit.runner.RunWith;
34import org.mockito.ArgumentCaptor;
36@RunWith(AndroidJUnit4.class)
39 private static void sendToBinaryMessageHandler(
43 binaryMessageHandler.onMessage(
47 @SuppressWarnings(
"deprecation")
54 SpellCheckChannel.SpellCheckMethodHandler mockHandler =
60 verify(mockBinaryMessenger,
times(1))
61 .setMessageHandler(
any(String.class), binaryMessageHandlerCaptor.capture());
63 BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
64 binaryMessageHandlerCaptor.getValue();
66 sendToBinaryMessageHandler(
68 "SpellCheck.initiateSpellCheck",
69 Arrays.asList(
"en-US",
"Hello, wrold!"));
78 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
82 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
84 when(fakeTextServicesManager.newSpellCheckerSession(
85 null,
new Locale(
"en",
"US"), spellCheckPlugin,
true))
86 .thenReturn(fakeSpellCheckerSession);
90 verify(spellCheckPlugin).performSpellCheck(
"en-US",
"Hello, wrold!");
96 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
101 spellCheckPlugin.pendingResult = mockPendingResult;
105 verify(mockResult).error(
"error",
"Previous spell check request still pending.",
null);
106 verify(spellCheckPlugin, never()).performSpellCheck(
"en-US",
"Hello, wrold!");
111 Context fakeContext = mock(Context.class);
113 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
114 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
115 .thenReturn(fakeTextServicesManager);
118 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
120 when(fakeTextServicesManager.newSpellCheckerSession(
121 null,
new Locale(
"en",
"US"), spellCheckPlugin,
true))
122 .thenReturn(fakeSpellCheckerSession);
127 verify(fakeSpellCheckChannel).setSpellCheckMethodHandler(isNull());
128 verify(fakeSpellCheckerSession).close();
133 Context fakeContext = mock(Context.class);
135 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
136 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
137 .thenReturn(fakeTextServicesManager);
140 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
141 Locale english_US =
new Locale(
"en",
"US");
143 when(fakeTextServicesManager.newSpellCheckerSession(
null, english_US, spellCheckPlugin,
true))
144 .thenReturn(fakeSpellCheckerSession);
146 int maxSuggestions = 5;
147 ArgumentCaptor<TextInfo[]> textInfosCaptor = ArgumentCaptor.forClass(TextInfo[].
class);
148 ArgumentCaptor<Integer> maxSuggestionsCaptor = ArgumentCaptor.forClass(Integer.class);
152 verify(fakeSpellCheckerSession)
153 .getSentenceSuggestions(textInfosCaptor.capture(), maxSuggestionsCaptor.capture());
154 assertEquals(
"Hello, wrold!", textInfosCaptor.getValue()[0].getText());
155 assertEquals(Integer.valueOf(maxSuggestions), maxSuggestionsCaptor.getValue());
160 Context fakeContext = mock(Context.class);
162 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
163 when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
164 .thenReturn(fakeTextServicesManager);
167 SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
168 Locale english_US =
new Locale(
"en",
"US");
170 when(fakeTextServicesManager.newSpellCheckerSession(
null, english_US, spellCheckPlugin,
true))
171 .thenReturn(fakeSpellCheckerSession);
176 verify(fakeTextServicesManager,
times(1))
177 .newSpellCheckerSession(
null, english_US, spellCheckPlugin,
true);
182 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
187 spellCheckPlugin.pendingResult = mockResult;
191 verify(mockResult).success(
new ArrayList<HashMap<String, Object>>());
196 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
201 spellCheckPlugin.pendingResult = mockResult;
204 new SentenceSuggestionsInfo[] {
205 new SentenceSuggestionsInfo(
206 (
new SuggestionsInfo[] {
208 SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO,
209 new String[] {
"world",
"word",
"old"})
215 ArrayList<HashMap<String, Object>> expectedResults =
new ArrayList<HashMap<String, Object>>();
216 HashMap<String, Object> expectedResult =
new HashMap<String, Object>();
222 new ArrayList<String>(Arrays.asList(
"world",
"word",
"old")));
223 expectedResults.add(expectedResult);
225 verify(mockResult).success(expectedResults);
230 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
235 spellCheckPlugin.pendingResult = mockResult;
238 new SentenceSuggestionsInfo[] {
239 new SentenceSuggestionsInfo(
240 (
new SuggestionsInfo[] {
242 SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO,
250 verify(mockResult).success(
new ArrayList<HashMap<String, Object>>());
255 TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
260 spellCheckPlugin.pendingResult = mockResult;
263 new SentenceSuggestionsInfo[] {
268 verify(mockResult).success(
new ArrayList<HashMap<String, Object>>());
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
void setSpellCheckMethodHandler( @Nullable SpellCheckMethodHandler spellCheckMethodHandler)
ByteBuffer encodeMethodCall(@NonNull MethodCall methodCall)
static final StandardMethodCodec INSTANCE
void initiateSpellCheckThrowsErrorWhenResultPending()
void onGetSentenceSuggestionsResultsWithSuccessAndResultsProperly()
void destroyClosesSpellCheckerSessionAndClearsSpellCheckMethodHandler()
void initiateSpellCheckPerformsSpellCheckWhenNoResultPending()
void respondsToSpellCheckChannelMessage()
void performSpellCheckSendsRequestToAndroidSpellCheckService()
void performSpellCheckCreatesNewSpellCheckerSession()
void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsWhenSuggestionsAreInvalid2()
void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsWhenSuggestionsAreInvalid()
void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsProperly()
static final String SUGGESTIONS_KEY
void performSpellCheck(@NonNull String locale, @NonNull String text)
static final String START_INDEX_KEY
static final String END_INDEX_KEY
void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results)
void initiateSpellCheck( @NonNull String locale, @NonNull String text, @NonNull MethodChannel.Result result)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
SIT bool any(const Vec< 1, T > &x)