Skip to content

Commit ee73f00

Browse files
authored
Merge branch 'main' into test/github-ci-migration
2 parents cb19aa9 + 70d3d13 commit ee73f00

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, ThreadS
119119
return;
120120
}
121121

122-
new Thread(() -> {
122+
threadSwitcher.backgroundThread(() -> {
123123
try {
124124
if (launchAsTwa) {
125125
this.launchedAsTwa = true;
@@ -140,7 +140,7 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, ThreadS
140140
"a0.browser_not_available", "Error launching browser for authentication", ex);
141141
threadSwitcher.mainThread(() -> failureCallback.apply(e));
142142
}
143-
}).start();
143+
});
144144
}
145145

146146
private void launchAsDefault(Context context, Uri uri) {

auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.net.Uri;
1111
import android.os.Looper;
1212

13+
import androidx.annotation.NonNull;
1314
import androidx.browser.customtabs.CustomTabsCallback;
1415
import androidx.browser.customtabs.CustomTabsClient;
1516
import androidx.browser.customtabs.CustomTabsIntent;
@@ -19,6 +20,7 @@
1920
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
2021

2122
import org.junit.Before;
23+
import org.junit.Rule;
2224
import org.junit.Test;
2325
import org.junit.runner.RunWith;
2426
import org.mockito.ArgumentCaptor;
@@ -40,9 +42,11 @@
4042
import static org.hamcrest.Matchers.nullValue;
4143
import static org.hamcrest.core.Is.is;
4244
import static org.hamcrest.core.Is.isA;
45+
import static org.mockito.ArgumentMatchers.same;
4346
import static org.mockito.Matchers.any;
4447
import static org.mockito.Matchers.anyInt;
4548
import static org.mockito.Matchers.eq;
49+
import static org.mockito.Mockito.atLeastOnce;
4650
import static org.mockito.Mockito.doReturn;
4751
import static org.mockito.Mockito.doThrow;
4852
import static org.mockito.Mockito.mock;
@@ -53,7 +57,9 @@
5357
import static org.mockito.Mockito.when;
5458

5559
import com.auth0.android.authentication.AuthenticationException;
60+
import com.auth0.android.request.internal.CommonThreadSwitcher;
5661
import com.auth0.android.request.internal.ThreadSwitcher;
62+
import com.auth0.android.util.CommonThreadSwitcherRule;
5763
import com.google.androidbrowserhelper.trusted.TwaLauncher;
5864
import com.google.androidbrowserhelper.trusted.splashscreens.SplashScreenStrategy;
5965

@@ -87,7 +93,17 @@ public void setUp() {
8793
MockitoAnnotations.openMocks(this);
8894
Activity activity = Robolectric.setupActivity(Activity.class);
8995
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+
});
91107

92108
//By default, a "compatible" browser is available
93109
BrowserPicker browserPicker = mock(BrowserPicker.class);
@@ -135,10 +151,11 @@ public void shouldUnbindEvenIfNotBound() throws Exception {
135151
@Test
136152
public void shouldBindAndLaunchUri() throws Exception {
137153
bindService(controller, true);
138-
controller.launchUri(uri, false, mockThreadSwitcher, null);
139154
connectBoundService();
155+
controller.launchUri(uri, false, mockThreadSwitcher, null);
140156

141157
verify(context, timeout(MAX_TEST_WAIT_TIME_MS)).startActivity(launchIntentCaptor.capture());
158+
verify(mockThreadSwitcher).backgroundThread(any());
142159
Intent intent = launchIntentCaptor.getValue();
143160
assertThat(intent.getAction(), is(Intent.ACTION_VIEW));
144161
assertThat(intent.getPackage(), is(DEFAULT_BROWSER_PACKAGE));
@@ -171,6 +188,7 @@ public void shouldBindAndLaunchUriAsTwa() throws Exception {
171188
runnableArgumentCaptor.capture(),
172189
fallbackStrategyArgumentCaptor.capture());
173190

191+
verify(mockThreadSwitcher).backgroundThread(any());
174192
assertThat(trustedWebActivityIntentBuilderArgumentCaptor.getValue(), is(notNullValue()));
175193
assertThat(customTabsCallbackArgumentCaptor.getValue(), is(nullValue()));
176194
assertThat(splashScreenStrategyArgumentCaptor.getValue(), is(nullValue()));
@@ -187,6 +205,7 @@ public void shouldLaunchUriUsingFallbackWhenNoCompatibleBrowserIsAvailable() {
187205
controller.launchUri(uri, false, mockThreadSwitcher, null);
188206

189207
verify(context, timeout(MAX_TEST_WAIT_TIME_MS)).startActivity(launchIntentCaptor.capture());
208+
verify(mockThreadSwitcher).backgroundThread(any());
190209
Intent intent = launchIntentCaptor.getValue();
191210
assertThat(intent.getAction(), is(Intent.ACTION_VIEW));
192211
//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 {
207226
CustomTabsController controller = new CustomTabsController(context, ctOptions, twaLauncher);
208227

209228
bindService(controller, true);
210-
controller.launchUri(uri, false, mockThreadSwitcher, null);
211229
connectBoundService();
230+
controller.launchUri(uri, false, mockThreadSwitcher, null);
212231

213232
verify(context, timeout(MAX_TEST_WAIT_TIME_MS)).startActivity(launchIntentCaptor.capture());
233+
verify(mockThreadSwitcher).backgroundThread(any());
214234
Intent intent = launchIntentCaptor.getValue();
215235
assertThat(intent.getAction(), is(Intent.ACTION_VIEW));
216236
assertThat(intent.getPackage(), is(DEFAULT_BROWSER_PACKAGE));
@@ -255,6 +275,7 @@ public void shouldBindAndLaunchUriWithCustomizationTwa() throws Exception {
255275
runnableArgumentCaptor.capture(),
256276
fallbackStrategyArgumentCaptor.capture());
257277

278+
verify(mockThreadSwitcher).backgroundThread(any());
258279
assertThat(trustedWebActivityIntentBuilderArgumentCaptor.getValue(), is(notNullValue()));
259280
assertThat(customTabsCallbackArgumentCaptor.getValue(), is(nullValue()));
260281
assertThat(splashScreenStrategyArgumentCaptor.getValue(), is(nullValue()));
@@ -269,6 +290,7 @@ public void shouldFailToBindButLaunchUri() {
269290

270291
verify(context, timeout(MAX_TEST_WAIT_TIME_MS)).startActivity(launchIntentCaptor.capture());
271292
Intent intent = launchIntentCaptor.getValue();
293+
verify(mockThreadSwitcher).backgroundThread(any());
272294
assertThat(intent.getAction(), is(Intent.ACTION_VIEW));
273295
assertThat(intent.getData(), is(uri));
274296
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_SESSION), is(true));
@@ -280,6 +302,7 @@ public void shouldNotLaunchUriIfContextNoLongerValid() {
280302
bindService(controller, true);
281303
controller.clearContext();
282304
controller.launchUri(uri, false, mockThreadSwitcher, null);
305+
verify(mockThreadSwitcher, never()).backgroundThread(any());
283306
verify(context, never()).startActivity(any(Intent.class));
284307
}
285308

@@ -291,6 +314,7 @@ public void shouldLaunchUriWithFallbackIfCustomTabIntentFails() {
291314
controller.launchUri(uri, false, mockThreadSwitcher, null);
292315

293316
verify(context, timeout(MAX_TEST_WAIT_TIME_MS)).startActivity(launchIntentCaptor.capture());
317+
verify(mockThreadSwitcher).backgroundThread(any());
294318
List<Intent> intents = launchIntentCaptor.getAllValues();
295319

296320
Intent customTabIntent = intents.get(0);
@@ -313,11 +337,11 @@ public void shouldThrowExceptionIfFailedToLaunchBecauseOfException() {
313337
.when(context).startActivity(any(Intent.class));
314338
controller.launchUri(uri, false, mockThreadSwitcher, (ex) -> {
315339
assertThat(ex, isA(AuthenticationException.class));
316-
assertThat(ex.getCause(), is(eq(e)));
340+
assertThat(ex.getCause(), equalTo(e));
317341
assertThat(ex.getCode(), is("a0.browser_not_available"));
318342
assertThat(ex.getDescription(), is("Error launching browser for authentication"));
319343
verify(mockThreadSwitcher).mainThread(any());
320-
verify(mockThreadSwitcher, Mockito.never()).backgroundThread(any());
344+
verify(mockThreadSwitcher).backgroundThread(any());
321345
});
322346
}
323347

0 commit comments

Comments
 (0)