10
10
import android .net .Uri ;
11
11
import android .os .Looper ;
12
12
13
+ import androidx .annotation .NonNull ;
13
14
import androidx .browser .customtabs .CustomTabsCallback ;
14
15
import androidx .browser .customtabs .CustomTabsClient ;
15
16
import androidx .browser .customtabs .CustomTabsIntent ;
19
20
import androidx .browser .trusted .TrustedWebActivityIntentBuilder ;
20
21
21
22
import org .junit .Before ;
23
+ import org .junit .Rule ;
22
24
import org .junit .Test ;
23
25
import org .junit .runner .RunWith ;
24
26
import org .mockito .ArgumentCaptor ;
40
42
import static org .hamcrest .Matchers .nullValue ;
41
43
import static org .hamcrest .core .Is .is ;
42
44
import static org .hamcrest .core .Is .isA ;
45
+ import static org .mockito .ArgumentMatchers .same ;
43
46
import static org .mockito .Matchers .any ;
44
47
import static org .mockito .Matchers .anyInt ;
45
48
import static org .mockito .Matchers .eq ;
49
+ import static org .mockito .Mockito .atLeastOnce ;
46
50
import static org .mockito .Mockito .doReturn ;
47
51
import static org .mockito .Mockito .doThrow ;
48
52
import static org .mockito .Mockito .mock ;
53
57
import static org .mockito .Mockito .when ;
54
58
55
59
import com .auth0 .android .authentication .AuthenticationException ;
60
+ import com .auth0 .android .request .internal .CommonThreadSwitcher ;
56
61
import com .auth0 .android .request .internal .ThreadSwitcher ;
62
+ import com .auth0 .android .util .CommonThreadSwitcherRule ;
57
63
import com .google .androidbrowserhelper .trusted .TwaLauncher ;
58
64
import com .google .androidbrowserhelper .trusted .splashscreens .SplashScreenStrategy ;
59
65
@@ -87,7 +93,17 @@ public void setUp() {
87
93
MockitoAnnotations .openMocks (this );
88
94
Activity activity = Robolectric .setupActivity (Activity .class );
89
95
context = spy (activity );
90
- mockThreadSwitcher = spy (ThreadSwitcher .class );
96
+ mockThreadSwitcher = spy (new ThreadSwitcher () {
97
+ @ Override
98
+ public void mainThread (@ NonNull Runnable runnable ) {
99
+ runnable .run ();
100
+ }
101
+
102
+ @ Override
103
+ public void backgroundThread (@ NonNull Runnable runnable ) {
104
+ runnable .run ();
105
+ }
106
+ });
91
107
92
108
//By default, a "compatible" browser is available
93
109
BrowserPicker browserPicker = mock (BrowserPicker .class );
@@ -135,10 +151,11 @@ public void shouldUnbindEvenIfNotBound() throws Exception {
135
151
@ Test
136
152
public void shouldBindAndLaunchUri () throws Exception {
137
153
bindService (controller , true );
138
- controller .launchUri (uri , false , mockThreadSwitcher , null );
139
154
connectBoundService ();
155
+ controller .launchUri (uri , false , mockThreadSwitcher , null );
140
156
141
157
verify (context , timeout (MAX_TEST_WAIT_TIME_MS )).startActivity (launchIntentCaptor .capture ());
158
+ verify (mockThreadSwitcher ).backgroundThread (any ());
142
159
Intent intent = launchIntentCaptor .getValue ();
143
160
assertThat (intent .getAction (), is (Intent .ACTION_VIEW ));
144
161
assertThat (intent .getPackage (), is (DEFAULT_BROWSER_PACKAGE ));
@@ -171,6 +188,7 @@ public void shouldBindAndLaunchUriAsTwa() throws Exception {
171
188
runnableArgumentCaptor .capture (),
172
189
fallbackStrategyArgumentCaptor .capture ());
173
190
191
+ verify (mockThreadSwitcher ).backgroundThread (any ());
174
192
assertThat (trustedWebActivityIntentBuilderArgumentCaptor .getValue (), is (notNullValue ()));
175
193
assertThat (customTabsCallbackArgumentCaptor .getValue (), is (nullValue ()));
176
194
assertThat (splashScreenStrategyArgumentCaptor .getValue (), is (nullValue ()));
@@ -187,6 +205,7 @@ public void shouldLaunchUriUsingFallbackWhenNoCompatibleBrowserIsAvailable() {
187
205
controller .launchUri (uri , false , mockThreadSwitcher , null );
188
206
189
207
verify (context , timeout (MAX_TEST_WAIT_TIME_MS )).startActivity (launchIntentCaptor .capture ());
208
+ verify (mockThreadSwitcher ).backgroundThread (any ());
190
209
Intent intent = launchIntentCaptor .getValue ();
191
210
assertThat (intent .getAction (), is (Intent .ACTION_VIEW ));
192
211
//A null package name would make the OS decide the best app to resolve the intent
@@ -207,10 +226,11 @@ public void shouldBindAndLaunchUriWithCustomization() throws Exception {
207
226
CustomTabsController controller = new CustomTabsController (context , ctOptions , twaLauncher );
208
227
209
228
bindService (controller , true );
210
- controller .launchUri (uri , false , mockThreadSwitcher , null );
211
229
connectBoundService ();
230
+ controller .launchUri (uri , false , mockThreadSwitcher , null );
212
231
213
232
verify (context , timeout (MAX_TEST_WAIT_TIME_MS )).startActivity (launchIntentCaptor .capture ());
233
+ verify (mockThreadSwitcher ).backgroundThread (any ());
214
234
Intent intent = launchIntentCaptor .getValue ();
215
235
assertThat (intent .getAction (), is (Intent .ACTION_VIEW ));
216
236
assertThat (intent .getPackage (), is (DEFAULT_BROWSER_PACKAGE ));
@@ -255,6 +275,7 @@ public void shouldBindAndLaunchUriWithCustomizationTwa() throws Exception {
255
275
runnableArgumentCaptor .capture (),
256
276
fallbackStrategyArgumentCaptor .capture ());
257
277
278
+ verify (mockThreadSwitcher ).backgroundThread (any ());
258
279
assertThat (trustedWebActivityIntentBuilderArgumentCaptor .getValue (), is (notNullValue ()));
259
280
assertThat (customTabsCallbackArgumentCaptor .getValue (), is (nullValue ()));
260
281
assertThat (splashScreenStrategyArgumentCaptor .getValue (), is (nullValue ()));
@@ -269,6 +290,7 @@ public void shouldFailToBindButLaunchUri() {
269
290
270
291
verify (context , timeout (MAX_TEST_WAIT_TIME_MS )).startActivity (launchIntentCaptor .capture ());
271
292
Intent intent = launchIntentCaptor .getValue ();
293
+ verify (mockThreadSwitcher ).backgroundThread (any ());
272
294
assertThat (intent .getAction (), is (Intent .ACTION_VIEW ));
273
295
assertThat (intent .getData (), is (uri ));
274
296
assertThat (intent .hasExtra (CustomTabsIntent .EXTRA_SESSION ), is (true ));
@@ -280,6 +302,7 @@ public void shouldNotLaunchUriIfContextNoLongerValid() {
280
302
bindService (controller , true );
281
303
controller .clearContext ();
282
304
controller .launchUri (uri , false , mockThreadSwitcher , null );
305
+ verify (mockThreadSwitcher , never ()).backgroundThread (any ());
283
306
verify (context , never ()).startActivity (any (Intent .class ));
284
307
}
285
308
@@ -291,6 +314,7 @@ public void shouldLaunchUriWithFallbackIfCustomTabIntentFails() {
291
314
controller .launchUri (uri , false , mockThreadSwitcher , null );
292
315
293
316
verify (context , timeout (MAX_TEST_WAIT_TIME_MS )).startActivity (launchIntentCaptor .capture ());
317
+ verify (mockThreadSwitcher ).backgroundThread (any ());
294
318
List <Intent > intents = launchIntentCaptor .getAllValues ();
295
319
296
320
Intent customTabIntent = intents .get (0 );
@@ -313,11 +337,11 @@ public void shouldThrowExceptionIfFailedToLaunchBecauseOfException() {
313
337
.when (context ).startActivity (any (Intent .class ));
314
338
controller .launchUri (uri , false , mockThreadSwitcher , (ex ) -> {
315
339
assertThat (ex , isA (AuthenticationException .class ));
316
- assertThat (ex .getCause (), is ( eq ( e ) ));
340
+ assertThat (ex .getCause (), equalTo ( e ));
317
341
assertThat (ex .getCode (), is ("a0.browser_not_available" ));
318
342
assertThat (ex .getDescription (), is ("Error launching browser for authentication" ));
319
343
verify (mockThreadSwitcher ).mainThread (any ());
320
- verify (mockThreadSwitcher , Mockito . never () ).backgroundThread (any ());
344
+ verify (mockThreadSwitcher ).backgroundThread (any ());
321
345
});
322
346
}
323
347
0 commit comments