30
30
import static org .mockito .Mockito .verifyNoMoreInteractions ;
31
31
import static org .mockito .Mockito .when ;
32
32
33
+ import android .content .Context ;
34
+ import android .os .Handler ;
35
+ import android .os .HandlerThread ;
33
36
import android .text .InputFilter ;
34
37
import android .text .Spannable ;
35
38
import android .text .SpannableString ;
39
+ import android .widget .EditText ;
36
40
import android .widget .TextView ;
37
41
38
42
import androidx .emoji2 .text .EmojiCompat ;
39
43
import androidx .emoji2 .util .EmojiMatcher ;
40
44
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
41
45
import androidx .test .filters .LargeTest ;
42
46
import androidx .test .filters .SdkSuppress ;
47
+ import androidx .test .platform .app .InstrumentationRegistry ;
43
48
44
49
import org .junit .Before ;
45
50
import org .junit .Test ;
@@ -191,7 +196,7 @@ public void emojiInputFilterAdded_beforeEmojiCompatInit_callsProcess() {
191
196
192
197
when (mEmojiCompat .getLoadState ()).thenReturn (EmojiCompat .LOAD_STATE_SUCCEEDED );
193
198
// trigger initialized
194
- captor .getValue (). onInitialized ();
199
+ (( Runnable ) captor .getValue ()). run ();
195
200
196
201
verify (mEmojiCompat ).process (eq (testString ));
197
202
}
@@ -223,7 +228,7 @@ public void emojiInputFilterAdded_beforeEmojiCompatInit_doesntCallSetText_ifSame
223
228
when (mEmojiCompat .process (eq (testString ))).thenReturn (testString );
224
229
when (mEmojiCompat .getLoadState ()).thenReturn (EmojiCompat .LOAD_STATE_SUCCEEDED );
225
230
// trigger initialized
226
- captor .getValue (). onInitialized ();
231
+ (( Runnable ) captor .getValue ()). run ();
227
232
228
233
// validate interactions don't do anything except check for update
229
234
verify (mTextView ).getFilters ();
@@ -233,4 +238,31 @@ public void emojiInputFilterAdded_beforeEmojiCompatInit_doesntCallSetText_ifSame
233
238
// if you add a safe interaction please update test
234
239
verifyNoMoreInteractions (mTextView );
235
240
}
241
+
242
+ @ Test
243
+ @ SdkSuppress (minSdkVersion = 19 )
244
+ public void initCallback_doesntCrashWhenNotAttached () {
245
+ Context context = InstrumentationRegistry .getInstrumentation ().getContext ();
246
+ EditText editText = new EditText (context );
247
+ EmojiInputFilter subject = new EmojiInputFilter (editText );
248
+ subject .getInitCallback ().onInitialized ();
249
+ }
250
+
251
+ @ Test
252
+ @ SdkSuppress (minSdkVersion = 29 )
253
+ public void initCallback_sendsToNonMainHandler_beforeSetText () {
254
+ // this is just testing that onInitialized dispatches to editText.getHandler before setText
255
+ EditText mockEditText = mock (EditText .class );
256
+ HandlerThread thread = new HandlerThread ("random thread" );
257
+ thread .start ();
258
+ Handler handler = new Handler (thread .getLooper ());
259
+ thread .quitSafely ();
260
+ when (mockEditText .getHandler ()).thenReturn (handler );
261
+ EmojiInputFilter subject = new EmojiInputFilter (mockEditText );
262
+ EmojiInputFilter .InitCallbackImpl initCallback =
263
+ (EmojiInputFilter .InitCallbackImpl ) subject .getInitCallback ();
264
+ initCallback .onInitialized ();
265
+
266
+ handler .hasCallbacks (initCallback );
267
+ }
236
268
}
0 commit comments