SsoDeeplink implements Deeplink { @Override public void execute() { execute(FlowVersion.DEFAULT); } private  SsoDeeplink(
            @NonNull Activity activity,
            @NonNull AppProtocol appProtocol,
            @NonNull String clientId,
            @NonNull String redirectUri,
            @NonNull Collection<Scope> requestedScopes,
            @NonNull Collection<String> requestedCustomScopes,
            @NonNull Collection<SupportedAppType> productFlowPriority,
            int requestCode); @Override void execute(); @Override boolean isSupported(); static final int DEFAULT_REQUEST_CODE; }
@Test public void execute_withRidesInstalled_andDefaultFlow_andNoProductPriority_shouldSetPackageAndStartActivityForResult() { enableSupport(DEFAULT); String packageName = "PACKAGE_NAME"; PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = packageName; when(appProtocol.getInstalledPackages(activity, UBER, MIN_UBER_RIDES_VERSION_SUPPORTED)) .thenReturn(Collections.singletonList(packageInfo)); ssoDeeplink.execute(); verify(appProtocol).getInstalledPackages(activity, UBER, MIN_UBER_RIDES_VERSION_SUPPORTED); verify(appProtocol, never()).getInstalledPackages(any(Context.class), eq(UBER_EATS), anyInt()); final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentCaptor.capture(), eq(REQUEST_CODE)); Intent intent = intentCaptor.getValue(); assertThat(intent.getPackage()).isEqualTo(packageName); assertThat(intent.getData().toString()).isEqualTo(DEFAULT_URI); }
@Test public void execute_withRidesInstalled_andRedirectToSdkFlow_andNoProductPriority_shouldSetPackageAndStartActivity() { enableSupport(REDIRECT_TO_SDK); String packageName = "PACKAGE_NAME"; PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = packageName; when(appProtocol.getInstalledPackages(activity, UBER, MIN_UBER_RIDES_VERSION_REDIRECT_FLOW_SUPPORTED)) .thenReturn(Collections.singletonList(packageInfo)); ssoDeeplink.execute(REDIRECT_TO_SDK); verify(appProtocol).getInstalledPackages(activity, UBER, MIN_UBER_RIDES_VERSION_REDIRECT_FLOW_SUPPORTED); verify(appProtocol, never()).getInstalledPackages(any(Context.class), eq(UBER_EATS), anyInt()); final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivity(intentCaptor.capture()); Intent intent = intentCaptor.getValue(); String expectedUri = "uber: + "&redirect_uri=com.example.app%3A%2F%2Fredirect&sdk_version=" + BuildConfig.VERSION_NAME; assertThat(intent.getData().toString()).isEqualTo(expectedUri); assertThat(intent.getPackage()).isEqualTo(packageName); }
@Test public void execute_withEatsProductFlowPriority_shouldLaunchEats() { enableSupport(DEFAULT); String eatsPackageName = "com.ubercab.eats"; PackageInfo eatsPackageInfo = new PackageInfo(); eatsPackageInfo.packageName = eatsPackageName; when(appProtocol.getInstalledPackages(activity, UBER_EATS, MIN_UBER_EATS_VERSION_SUPPORTED)) .thenReturn(Collections.singletonList(eatsPackageInfo)); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .activityRequestCode(REQUEST_CODE) .scopes(GENERAL_SCOPES) .appProtocol(appProtocol) .productFlowPriority(ImmutableList.of(UBER_EATS)) .build() .execute(); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentArgumentCaptor.capture(), anyInt()); assertThat(intentArgumentCaptor.getValue().getPackage()).isEqualTo(eatsPackageName); }
@Test public void execute_withCombinedProductFlowPriority_andBothAppsInstalled_shouldLaunchFirstPriorityApp() { enableSupport(DEFAULT); String eatsPackageName = "com.ubercab.eats"; PackageInfo eatsPackageInfo = new PackageInfo(); eatsPackageInfo.packageName = eatsPackageName; String ridesPackageName = "com.ubercab"; PackageInfo ridesPackageInfo = new PackageInfo(); ridesPackageInfo.packageName = ridesPackageName; when(appProtocol.getInstalledPackages(activity, UBER_EATS, MIN_UBER_EATS_VERSION_SUPPORTED)) .thenReturn(Collections.singletonList(eatsPackageInfo)); when(appProtocol.getInstalledPackages(activity, UBER, MIN_UBER_RIDES_VERSION_SUPPORTED)) .thenReturn(Collections.singletonList(ridesPackageInfo)); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .activityRequestCode(REQUEST_CODE) .scopes(GENERAL_SCOPES) .appProtocol(appProtocol) .productFlowPriority(ImmutableList.of(UBER, UBER_EATS)) .build() .execute(); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentArgumentCaptor.capture(), anyInt()); assertThat(intentArgumentCaptor.getValue().getPackage()).isEqualTo(ridesPackageName); }
@Test public void execute_withoutRequestCode_shouldUseDefaultRequestCode() { enableSupport(DEFAULT); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .appProtocol(appProtocol) .redirectUri(REDIRECT_URI) .build() .execute(); final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Integer> requestCodeCaptor = ArgumentCaptor.forClass(Integer.class); verify(activity).startActivityForResult(intentCaptor.capture(), requestCodeCaptor.capture()); Uri uri = intentCaptor.getValue().getData(); assertThat(uri.toString()).isEqualTo(DEFAULT_URI); assertThat(requestCodeCaptor.getValue()).isEqualTo(LoginManager.REQUEST_CODE_LOGIN_DEFAULT); }
@Test public void execute_withScopesAndCustomScopes_shouldSucceed() { enableSupport(DEFAULT); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .activityRequestCode(REQUEST_CODE) .scopes(GENERAL_SCOPES) .customScopes(Arrays.asList("sample", "test")) .appProtocol(appProtocol) .build() .execute(); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentArgumentCaptor.capture(), anyInt()); Uri uri = intentArgumentCaptor.getValue().getData(); assertThat(uri.getQueryParameter("scope")).contains("history", "profile", "sample", "test"); }
@Test public void execute_withOnlyCustomScopes_shouldSucceed() { enableSupport(DEFAULT); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .activityRequestCode(REQUEST_CODE) .scopes(Collections.<Scope>emptyList()) .customScopes(Arrays.asList("sample", "test")) .appProtocol(appProtocol) .build() .execute(); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentArgumentCaptor.capture(), anyInt()); Uri uri = intentArgumentCaptor.getValue().getData(); assertThat(uri.getQueryParameter("scope")).contains("sample", "test"); }
@Test public void execute_withoutRedirectUri_shouldUseDefaultUri() { enableSupport(REDIRECT_TO_SDK); packageManager.removeResolveInfosForIntent(redirectIntent, activity.getPackageName()); String expectedRedirectUri = activity.getPackageName().concat(".uberauth: Intent expectedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(expectedRedirectUri)); expectedIntent.setPackage(activity.getPackageName()); packageManager.addResolveInfoForIntent(expectedIntent, resolveInfo); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .appProtocol(appProtocol) .build() .execute(REDIRECT_TO_SDK); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivity(intentArgumentCaptor.capture()); Uri uri = intentArgumentCaptor.getValue().getData(); assertThat(uri.getQueryParameter("redirect_uri")).isEqualTo(expectedRedirectUri); }
@Test(expected = IllegalStateException.class) public void execute_withoutAnyScopes_shouldFail() { enableSupport(DEFAULT); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(Collections.<Scope>emptyList()) .customScopes(Collections.<String>emptyList()) .activityRequestCode(REQUEST_CODE) .appProtocol(appProtocol) .build() .execute(); }
@Test(expected = NullPointerException.class) public void execute_withoutClientId_shouldFail() { enableSupport(DEFAULT); new SsoDeeplink.Builder(activity) .scopes(GENERAL_SCOPES) .activityRequestCode(REQUEST_CODE) .appProtocol(appProtocol) .build() .execute(); }
@Test(expected = IllegalStateException.class) public void execute_withRidesBelowMinVersion_noProductPriority_shouldFail() { when(appProtocol.isInstalled(activity, UBER, MIN_UBER_RIDES_VERSION_SUPPORTED)).thenReturn(false); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .build() .execute(); }
@Test(expected = IllegalStateException.class) public void execute_withBothAppsBelowMinVersion_andCombinedProductPriority_shouldFail() { when(appProtocol.isInstalled(activity, UBER, MIN_UBER_RIDES_VERSION_SUPPORTED)).thenReturn(false); when(appProtocol.isInstalled(activity, UBER_EATS, MIN_UBER_EATS_VERSION_SUPPORTED)).thenReturn(false); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .productFlowPriority(ImmutableList.of(UBER, UBER_EATS)) .build() .execute(); }
@Test(expected = IllegalStateException.class) public void execute_withBothAppsBelowMinRedirectToSdkVersion_andCombinedProductPriority_shouldFail() { when(appProtocol.isInstalled(activity, UBER, MIN_UBER_RIDES_VERSION_REDIRECT_FLOW_SUPPORTED)).thenReturn(false); when(appProtocol.isInstalled(activity, UBER_EATS, MIN_UBER_EATS_VERSION_SUPPORTED)).thenReturn(false); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .productFlowPriority(ImmutableList.of(UBER, UBER_EATS)) .build() .execute(REDIRECT_TO_SDK); }
@Test(expected = IllegalStateException.class) public void execute_withRedirectToSdkFlowVersion_andCantResolveRedirectIntent_shouldFail() { enableSupport(REDIRECT_TO_SDK); packageManager.removeResolveInfosForIntent(redirectIntent, activity.getPackageName()); new SsoDeeplink.Builder(activity) .clientId(CLIENT_ID) .scopes(GENERAL_SCOPES) .build() .execute(REDIRECT_TO_SDK); }