Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
dc93719
Add session management
camsim99 Jan 10, 2022
cd234ad
Handle receiving results
camsim99 Jan 10, 2022
f463846
Add framework logic, modify engine
camsim99 Jan 12, 2022
3dfae0a
Make engine build
camsim99 Jan 12, 2022
546692b
Add retrieving locale
camsim99 Jan 13, 2022
5f43ff5
Added comments for experimentation
camsim99 Jan 18, 2022
ee73219
Send string back temporarily, fix results representation
camsim99 Feb 2, 2022
624f68b
Formatting
camsim99 Feb 2, 2022
846b195
Merge branch 'main' of github.com:flutter/engine into issue_34688_dev
camsim99 Feb 28, 2022
b27704c
Merge remote-tracking branch 'upstream/main' into issue_34688_dev
camsim99 Mar 16, 2022
64f0ac7
Add new method channel
camsim99 Mar 18, 2022
0cc3776
Add platform setting for spell check
camsim99 Mar 18, 2022
33f2f40
Change method channel name
camsim99 Mar 21, 2022
eb3c731
Remove print statements
camsim99 Mar 22, 2022
1877242
Clean up
camsim99 Mar 23, 2022
5372aaa
Formatting
camsim99 Mar 24, 2022
d9adbdc
Clean PR
camsim99 Mar 28, 2022
34809db
Undo platform plugin changes
camsim99 Mar 28, 2022
10d154d
Clean up code
camsim99 Mar 30, 2022
00c228e
Begin adding tests
camsim99 Mar 30, 2022
e08171f
Formatting
camsim99 Mar 30, 2022
5ccc144
Correct sessions, remove unecessary line
camsim99 Mar 31, 2022
87ab60f
Add tests
camsim99 Mar 31, 2022
f4083d7
Formatting
camsim99 Mar 31, 2022
ce45427
Remove variable
camsim99 Mar 31, 2022
6972b0b
Begin fixing tests
camsim99 Apr 1, 2022
3eba4de
Address reviews
camsim99 Apr 7, 2022
963d9ae
Start fixing systesm setting
camsim99 Apr 8, 2022
6ea2ede
Continue addressing reviews
camsim99 Apr 8, 2022
32c7702
Continue addressing reviews
camsim99 Apr 8, 2022
e9ebd5b
Fix spell checkers check
camsim99 Apr 8, 2022
8298436
Begin fixing tests
camsim99 Apr 9, 2022
993b6c8
Reformat FlutterViewTest
camsim99 Apr 11, 2022
4179f42
Fixing tests
camsim99 Apr 11, 2022
f999488
Merge remote-tracking branch 'upstream/main' into issue_34688_dev
camsim99 Apr 11, 2022
ade72bc
Fix tests minus log errors
camsim99 Apr 11, 2022
c20b62c
Add context shadow to stub tsm request
camsim99 Apr 13, 2022
49583d4
Formatting
camsim99 Apr 13, 2022
49939aa
Fix np error
camsim99 Apr 14, 2022
0cf00f3
Add license
camsim99 Apr 14, 2022
48a4354
Update licenses file
camsim99 Apr 14, 2022
510f83a
Add nativeSpellCheckServiceDefined to other platform dispatchers
camsim99 Apr 15, 2022
d8e333b
Merge remote-tracking branch 'upstream/main' into issue_34688_dev
camsim99 Apr 15, 2022
61908d3
Remove setting
camsim99 Apr 15, 2022
29b095f
Fix typos
camsim99 Apr 15, 2022
ee28058
Update lib/ui/platform_dispatcher.dart
camsim99 Apr 18, 2022
4f0a074
Update lib/ui/window.dart
camsim99 Apr 18, 2022
6c4d437
Update shell/platform/android/io/flutter/embedding/android/FlutterVie…
camsim99 Apr 18, 2022
d73b915
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
66c24ed
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
04a2ce1
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
15d17f5
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
e6414b2
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
2882e3b
Update shell/platform/android/io/flutter/embedding/engine/systemchann…
camsim99 Apr 18, 2022
dfe4689
Update shell/platform/android/io/flutter/plugin/editing/SpellCheckPlu…
camsim99 Apr 18, 2022
cdc6b9c
Update shell/platform/android/io/flutter/plugin/editing/SpellCheckPlu…
camsim99 Apr 18, 2022
65b8850
Update shell/platform/android/io/flutter/plugin/editing/SpellCheckPlu…
camsim99 Apr 18, 2022
036b15b
Make review fixes
camsim99 Apr 18, 2022
188c2ad
Merge branch 'issue_34688_dev' of github.com:camsim99/engine into iss…
camsim99 Apr 18, 2022
8198d4e
Formatting
camsim99 Apr 18, 2022
a2b6c90
Fix spell check test
camsim99 Apr 18, 2022
2aac372
Address nits
camsim99 Apr 21, 2022
894f4e9
Send text to framework
camsim99 Apr 28, 2022
2a4ea66
Push for visibility
camsim99 May 4, 2022
c2a6550
Modify result response protocol
camsim99 May 6, 2022
6125e74
Merge branch 'main' into issue_34688_dev
camsim99 May 9, 2022
74d1477
Merge remote-tracking branch 'upstream/master' into issue_34688_dev
camsim99 May 10, 2022
cc8bf5e
Merge branch 'issue_34688_dev' of github.com:camsim99/engine into iss…
camsim99 May 10, 2022
5a175ec
Formatting and pull
camsim99 May 10, 2022
1c02784
Merge remote-tracking branch 'upstream/master' into issue_34688_dev
camsim99 May 12, 2022
3e74d61
Emulator fix
camsim99 May 13, 2022
975673c
Merge remote-tracking branch 'upstream/main' into issue_34688_dev
camsim99 May 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Send text to framework
  • Loading branch information
camsim99 committed Apr 28, 2022
commit 894f4e9fa0fd0ff8f7e8954a953c23f14b68cfb9
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
* io.flutter.plugin.editing.SpellCheckPlugin}, it will send to the framework the message {@code
* SpellCheck.updateSpellCheckResults} with the {@code ArrayList<String>} of encoded spell check
* results (see {@link
* io.flutter.plugin.editing.SpellCheckPlugin#onGetSentenceSuggestions(SentenceSuggestionsInfo[])}
* for details) as an argument.
* io.flutter.plugin.editing.SpellCheckPlugin.SpellCheckPluginSessionListener#onGetSentenceSuggestions(SentenceSuggestionsInfo[])}
* for details) with the text that these results correspond to appeneded to the front as an
* argument. For example, the argument may look like: {@code {"Hello, wrold!",
* "7.11.world\nword\nold"}}.
*
* <p>{@link io.flutter.plugin.editing.SpellCheckPlugin} implements {@link SpellCheckMethodHandler}
* to initiate spell check. Implement {@link SpellCheckMethodHandler} to respond to spell check
Expand Down Expand Up @@ -80,8 +82,9 @@ public SpellCheckChannel(@NonNull DartExecutor dartExecutor) {
channel.setMethodCallHandler(parsingMethodHandler);
}

/** Responsible for sending spell check results through this channel. */
public void updateSpellCheckResults(ArrayList<String> spellCheckResults) {
/** Responsible for sending spell check results and corresponding text through this channel. */
public void updateSpellCheckResults(ArrayList<String> spellCheckResults, String text) {
spellCheckResults.add(0, text);
channel.invokeMethod("SpellCheck.updateSpellCheckResults", spellCheckResults);
}

Expand Down
106 changes: 65 additions & 41 deletions shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
import io.flutter.plugin.localization.LocalizationPlugin;
import java.util.ArrayList;
Expand All @@ -21,12 +22,10 @@
*
* <p>The plugin handles requests for spell check sent by the {@link
* io.flutter.embedding.engine.systemchannels.SpellCheckChannel} via sending requests to the Android
* spell checker. It also receive the spell check results from the service and send them back to the
* framework through the {@link io.flutter.embedding.engine.systemchannels.SpellCheckChannel}.
* spell checker. It also receives the spell check results from the service and sends them back to
* the framework through the {@link io.flutter.embedding.engine.systemchannels.SpellCheckChannel}.
*/
public class SpellCheckPlugin
implements SpellCheckChannel.SpellCheckMethodHandler,
SpellCheckerSession.SpellCheckerSessionListener {
public class SpellCheckPlugin implements SpellCheckChannel.SpellCheckMethodHandler {

private final SpellCheckChannel mSpellCheckChannel;
private final TextServicesManager mTextServicesManager;
Expand Down Expand Up @@ -62,6 +61,11 @@ public void destroy() {
}
}

@VisibleForTesting
public SpellCheckPluginSessionListener createSpellCheckerSessionListener(String text) {
return new SpellCheckPluginSessionListener(text);
}

@Override
public void initiateSpellCheck(@NonNull String locale, @NonNull String text) {
performSpellCheck(locale, text);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle case when there's a pending initiateSpellCheck. Maybe, call result.error or result.success with an empty list. The general idea is that while the framework is waiting on a spellcheck request, a new request is issued, so the older one needs to be dropped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach of dropping older requests has a similar fault to the approach I pursued previously of saving the result and text awaiting a response to that result in the SpellCheckChannel class, which is that we cannot cancel requests to the Android spell checker once they have been submitted.

We already knew that, but our assumption was that if the SpellCheckPlugin has detected a result awaiting a response when another request to initiate spell check has come in, then that request will not reach the onGetSentenceSuggestions(...) callback, meaning that the Android spell checker consumed the request. The consequence of assuming such is that sometimes the plugin will attempt to respond to a new request with old spell check results. Depending on how much the text has been modified between the time those old results come in and the time that the new request to spell check was submitted, those old results could lead to some hard-to-handle errors on the framework side.

Given all of that, I decided to have the plugin only handle one request at a time by dropping new requests when an old one is still pending. This approach may lead to results lagging slightly behind, but (i) the spell check results in any given response will correspond to the text in that response, (ii) the spell check results will be returned in chronological order, and (iii) any impacts of these results lagging behind can be handled by the logic on the framework side. This is all because with this approach, we do not have to battle the unpredictable behavior of the Android spell checker. I believe also that in further iterations, other speedup techniques can be more easily explored given these guarantees.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this approach seem to work with decent performance in practice? Maybe this is the most solid approach in the real world even though it's not the fastest theoretically.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is there ever a risk of the spell checker never responding and locking up or is that another thing that we shouldn't worry about in practice?

Copy link
Contributor Author

@camsim99 camsim99 May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this approach seem to work with decent performance in practice? Maybe this is the most solid approach in the real world even though it's not the fastest theoretically.

Yes, exactly. From playing around with it (paired with making the corrections on the framework side if the process of returning results lag behind), I don't see any difference. If you are open to playing around with it, too, that'd be great!

Also, is there ever a risk of the spell checker never responding and locking up or is that another thing that we shouldn't worry about in practice?

I think we can rely on Android for this. Android seems to have some handling for issues like that (see here).

Expand All @@ -75,65 +79,85 @@ public void performSpellCheck(@NonNull String locale, @NonNull String text) {
if (mSpellCheckerSession != null) {
mSpellCheckerSession.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the bug we were looking at where the spell checker would give inconsistent results? So it seems like we start a new session every time the text changes. I think that's right by briefly looking at the android spell check docs. It seems like they want you to keep a session alive if you want to spell check multiple subsets of a larger piece of text? (Probably not a concern of Flutter's right now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is related to that bug. I do agree that this agrees with the flow in this diagram if you are interacting with the Android spell check service directly.

When it comes to larger pieces of text, we can definitely play around with this! Although, by the time this becomes a concern for Flutter, it would probably be more preferable to no longer be spell checking the entirety of the inputted text each time anyways.

}

mSpellCheckerSession =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity can we reuse the same session for different performSpellCheck calls? The configuration parameters used to create a new session don't seem to be tied to a particular spell check attempt (for instance it doesn't take text)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but I found that the spell check results lag behind when I didn't create a new session each time. It also seems like this is the intended way to interact with the spell checker as per this guide.

Copy link
Contributor

@LongCatIsLooong LongCatIsLooong Apr 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guide doesn't seem to imply that each spell check needs a new session.
The android spellchecker seems to only reset the session (resetSession()) when the service is turned on/off, and on locale change:

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/widget/SpellChecker.java;l=235;drc=408a5565ee6d61fa3dc642de349cedf607d08d94;bpv=0;bpt=1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NBD if it's cheap to reset the session. But I'm curious what do you mean by "results lag behind"? The listener may get the response to a previous request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NBD if it's cheap to reset the session. But I'm curious what do you mean by "results lag behind"? The listener may get the response to a previous request?

I reached out to someone more knowledgeable on the spell checker to find out how expensive it is, and they said it could be expensive, and that it would be ideal to reuse the same session (but this may only be the case until some bugs are fixed; they were unsure).

I had experimented with @justinmc in the past with reusing the session versus reseting it each time, and we noticed that the spell check results didn’t update for a particular word until two additional characters were added, but upon re-test, this appears to have been an issue on the framework side that I have since fixed. So, it would be ideal to reuse the session for each EditableText instance.

out of curiosity can we reuse the same session for different performSpellCheck calls? The configuration parameters used to create a new session don't seem to be tied to a particular spell check attempt (for instance it doesn't take text)

In terms of this proposal, the only problem with reusing the same session is that the SpellCheckerSessionListener is tied to that session, meaning we could not group the text with the spell check results that this listener receives by way of creating different listeners for different requests (see here). Any input on a work around for that?

If we end up having to reset the session each time in order to group the text with the spell check results that correspond to it, we may be looking at a solution that involves reusing the same session up until a certain cutoff of text length where we know the framework would need access to the text to correct for the increase in time it takes to spell check that length text and longer (or something of the like).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only problem with reusing the same session is that the SpellCheckerSessionListener is tied to that session, meaning we could not group the text with the spell check results that this listener receives by way of creating different listeners for different requests (see here). Any input on a work around for that?

If you are reusing the session, and since you said that calling getSentenceSuggestions multiple times before onGetSentenceSuggestions has been called, results in a single callback with the results for the last call, you could:

  1. Construct newSpellCheckerSession once.
  2. Define a setter in the listener that sets the latest MethodChannel.Result for the latest call to getSentenceSuggestions.
  3. The setter for the latest MethodChannel.Result, must resolve the current MethodChannel.Result by either returning an error, or succeeding with no results. It doesn't matter which one you use because the framework will need to handle either case.
  4. This simplifies the protocol quite a bit. e.g. You don't need to pass "text" to the response, and you don't need a different method to send the response.

Since MethodChannel.Result is the result object for the original request, it's a single request/response from the framework. The framework already knows the text, so it doesn't need additional bookeeping.

I hope that makes sense.

mTextServicesManager.newSpellCheckerSession(
null,
localeFromString,
this,
createSpellCheckerSessionListener(text),
/** referToSpellCheckerLanguageSettings= */
true);

TextInfo[] textInfos = new TextInfo[] {new TextInfo(text)};
mSpellCheckerSession.getSentenceSuggestions(textInfos, MAX_SPELL_CHECK_SUGGESTIONS);
}

/**
* Callback for Android spell check API that decomposes results and send results through the
* {@link SpellCheckChannel}.
*
* <p>Spell check results will be encoded as a string representing the span of that result, with
* the format [start_index.end_index.suggestion_1,suggestion_2,suggestion_3], where there may be
* up to 5 suggestions.
*/
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>();
class SpellCheckPluginSessionListener implements SpellCheckerSession.SpellCheckerSessionListener {
private final String text;

if (results.length == 0) {
mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans);
return;
public SpellCheckPluginSessionListener(String text) {
this.text = text;
}

SentenceSuggestionsInfo spellCheckResults = results[0];
@VisibleForTesting
public SpellCheckChannel getSpellCheckChannel() {
return mSpellCheckChannel;
}

for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) {
SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i);
int suggestionsCount = suggestionsInfo.getSuggestionsCount();
@VisibleForTesting
public String getText() {
return text;
}

if (suggestionsCount == 0) {
continue;
/**
* Callback for Android spell check API that decomposes results and send results through the
* {@link SpellCheckChannel}.
*
* <p>Spell check results will be encoded as a string representing the span of that result, with
* the format "start_index.end_index.suggestion_1/nsuggestion_2/nsuggestion_3", where there may
* be up to 5 suggestions.
*/
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>();
SpellCheckChannel spellCheckChannel = getSpellCheckChannel();

if (results.length == 0) {
spellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans, getText());
return;
}

String spellCheckerSuggestionSpan = "";
int start = spellCheckResults.getOffsetAt(i);
int end = start + spellCheckResults.getLengthAt(i) - 1;
SentenceSuggestionsInfo spellCheckResults = results[0];

spellCheckerSuggestionSpan += String.valueOf(start) + ".";
spellCheckerSuggestionSpan += String.valueOf(end) + ".";
for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) {
SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i);
int suggestionsCount = suggestionsInfo.getSuggestionsCount();

for (int j = 0; j < suggestionsCount; j++) {
spellCheckerSuggestionSpan += suggestionsInfo.getSuggestionAt(j) + "\n";
if (suggestionsCount == 0) {
continue;
}

String spellCheckerSuggestionSpan = "";
int start = spellCheckResults.getOffsetAt(i);
int end = start + spellCheckResults.getLengthAt(i) - 1;

spellCheckerSuggestionSpan += String.valueOf(start) + ".";
spellCheckerSuggestionSpan += String.valueOf(end) + ".";

for (int j = 0; j < suggestionsCount; j++) {
spellCheckerSuggestionSpan += suggestionsInfo.getSuggestionAt(j) + "\n";
}

spellCheckerSuggestionSpans.add(
spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1));
}

spellCheckerSuggestionSpans.add(
spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1));
spellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans, getText());
}

mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans);
}

@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
// Deprecated callback for Android spell check API; will not use.
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
// Deprecated callback for Android spell check API; will not use.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -72,10 +73,15 @@ public void destroyClosesSpellCheckerSessionAndClearsSpellCheckMethodHandler() {
when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
.thenReturn(fakeTextServicesManager);
SpellCheckPlugin spellCheckPlugin =
new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel);
spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
SpellCheckPlugin.SpellCheckPluginSessionListener fakeSpellCheckPluginSessionListener =
mock(SpellCheckPlugin.SpellCheckPluginSessionListener.class);

when(spellCheckPlugin.createSpellCheckerSessionListener("Hello, wrold!"))
.thenReturn(fakeSpellCheckPluginSessionListener);
when(fakeTextServicesManager.newSpellCheckerSession(
null, new Locale("en", "US"), spellCheckPlugin, true))
null, new Locale("en", "US"), fakeSpellCheckPluginSessionListener, true))
.thenReturn(fakeSpellCheckerSession);

spellCheckPlugin.performSpellCheck("en-US", "Hello, wrold!");
Expand All @@ -93,13 +99,19 @@ public void performSpellCheckSendsRequestToAndroidSpellCheckService() {
when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
.thenReturn(fakeTextServicesManager);
SpellCheckPlugin spellCheckPlugin =
new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel);
spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
SpellCheckPlugin.SpellCheckPluginSessionListener fakeSpellCheckPluginSessionListener =
mock(SpellCheckPlugin.SpellCheckPluginSessionListener.class);
Locale english_US = new Locale("en", "US");
when(fakeTextServicesManager.newSpellCheckerSession(null, english_US, spellCheckPlugin, true))

when(spellCheckPlugin.createSpellCheckerSessionListener("Hello, wrold!"))
.thenReturn(fakeSpellCheckPluginSessionListener);
when(fakeTextServicesManager.newSpellCheckerSession(
null, english_US, fakeSpellCheckPluginSessionListener, true))
.thenReturn(fakeSpellCheckerSession);
int maxSuggestions = 5;

int maxSuggestions = 5;
ArgumentCaptor<TextInfo[]> textInfosCaptor = ArgumentCaptor.forClass(TextInfo[].class);
ArgumentCaptor<Integer> maxSuggestionsCaptor = ArgumentCaptor.forClass(Integer.class);

Expand All @@ -119,27 +131,41 @@ public void performSpellCheckCreatesNewSpellCheckerSession() {
when(fakeContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE))
.thenReturn(fakeTextServicesManager);
SpellCheckPlugin spellCheckPlugin =
new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel);
spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
SpellCheckerSession fakeSpellCheckerSession = mock(SpellCheckerSession.class);
SpellCheckPlugin.SpellCheckPluginSessionListener fakeSpellCheckPluginSessionListener =
mock(SpellCheckPlugin.SpellCheckPluginSessionListener.class);
Locale english_US = new Locale("en", "US");
when(fakeTextServicesManager.newSpellCheckerSession(null, english_US, spellCheckPlugin, true))

when(spellCheckPlugin.createSpellCheckerSessionListener("Hello, worl!"))
.thenReturn(fakeSpellCheckPluginSessionListener);
when(spellCheckPlugin.createSpellCheckerSessionListener("Hello, world!"))
.thenReturn(fakeSpellCheckPluginSessionListener);
when(fakeTextServicesManager.newSpellCheckerSession(
null, english_US, fakeSpellCheckPluginSessionListener, true))
.thenReturn(fakeSpellCheckerSession);

spellCheckPlugin.performSpellCheck("en-US", "Hello, worl!");
spellCheckPlugin.performSpellCheck("en-US", "Hello, world!");

verify(fakeTextServicesManager, times(2))
.newSpellCheckerSession(null, english_US, spellCheckPlugin, true);
.newSpellCheckerSession(null, english_US, fakeSpellCheckPluginSessionListener, true);
}

@Test
public void onGetSentenceSuggestionsProperlyRequestsUpdateSpellCheckResults() {
TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
SpellCheckPlugin spellCheckPlugin =
new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel);
spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
SpellCheckPlugin.SpellCheckPluginSessionListener fakeSpellCheckPluginSessionListener =
spy(spellCheckPlugin.createSpellCheckerSessionListener("Hello, wrold!"));

when(fakeSpellCheckPluginSessionListener.getSpellCheckChannel())
.thenReturn(fakeSpellCheckChannel);
when(fakeSpellCheckPluginSessionListener.getText()).thenReturn("Hello, wrold!");

spellCheckPlugin.onGetSentenceSuggestions(
fakeSpellCheckPluginSessionListener.onGetSentenceSuggestions(
new SentenceSuggestionsInfo[] {
new SentenceSuggestionsInfo(
(new SuggestionsInfo[] {
Expand All @@ -152,6 +178,7 @@ public void onGetSentenceSuggestionsProperlyRequestsUpdateSpellCheckResults() {
});

verify(fakeSpellCheckChannel)
.updateSpellCheckResults(new ArrayList<String>(Arrays.asList("7.11.world\nword\nold")));
.updateSpellCheckResults(
new ArrayList<String>(Arrays.asList("7.11.world\nword\nold")), "Hello, wrold!");
}
}