UrlHandler { public void handleUrl(@NonNull final Context context, @NonNull final String destinationUrl) { handleUrl(context, destinationUrl, true); } private  UrlHandler(
            @NonNull final EnumSet<UrlAction> supportedUrlActions,
            @NonNull final ResultActions resultActions,
            @NonNull final MoPubSchemeListener moPubSchemeListener,
            final boolean skipShowMoPubBrowser); void handleUrl(@NonNull final Context context, @NonNull final String destinationUrl); void handleUrl(@NonNull final Context context, @NonNull final String destinationUrl,
            final boolean fromUserInteraction);  }
@Test public void urlHandler_withoutMoPubBrowser_shouldCallOnClickSuccessButNotStartActivity() { final String url = "http: new UrlHandler.Builder() .withSupportedUrlActions(OPEN_IN_APP_BROWSER) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .withoutMoPubBrowser() .build().handleUrl(context, url); verify(mockResultActions).urlHandlingSucceeded(url, OPEN_IN_APP_BROWSER); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); }
@Test public void urlHandler_withMatchingMoPubSchemeFinishLoad_shouldCallOnFinishLoad() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockMoPubSchemeListener).onFinishLoad(); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withMatchingMoPubSchemeClose_shouldCallOnClose() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockMoPubSchemeListener).onClose(); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withMatchingMoPubSchemeFailLoad_shouldCallOnFailLoad() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockMoPubSchemeListener).onFailLoad(); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withMatchingAboutSchemeUrl_shouldIgnoreClick() { final String url = "about:blank"; new UrlHandler.Builder() .withSupportedUrlActions( HANDLE_MOPUB_SCHEME, IGNORE_ABOUT_SCHEME, HANDLE_PHONE_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withMatchingPhoneSchemeUrl_shouldStartActivity() { final String url = "tel:1234567890"; new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedActivity.getData()).isEqualTo(Uri.parse(url)); }
@Test public void urlHandler_withValidNativeBrowserUrl_shouldCallOnClickSuccess_shouldStartActivity() { final String urlToLoad = "some_url"; final String url = "mopubnativebrowser: new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingSucceeded(url, OPEN_NATIVE_BROWSER); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedActivity.getData()).isEqualTo(Uri.parse(urlToLoad)); }
@Test public void urlHandler_withMatchingInAppBrowserHttpUrl_shouldCallOnClickSuccess_shouldStartActivity() { final String url = "http: new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingSucceeded(url, OPEN_IN_APP_BROWSER); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getComponent().getClassName()) .isEqualTo(MoPubBrowser.class.getName()); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(url); }
@Test public void urlHandler_withMatchingInAppBrowserHttpsUrl_shouldCallOnClickSuccess_shouldStartActivity() { final String url = "https: new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingSucceeded(url, OPEN_IN_APP_BROWSER); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getComponent().getClassName()) .isEqualTo(MoPubBrowser.class.getName()); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(url); }
@Test public void urlHandler_withMatchingShareUrl_shouldCallOnClickSuccess_shouldStartActivity() { final String shareTweetUrl = "mopubshare: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .build().handleUrl(context, shareTweetUrl); verify(mockResultActions).urlHandlingSucceeded(shareTweetUrl, HANDLE_SHARE_TWEET); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getAction()).isEqualTo(Intent.ACTION_CHOOSER); }
@Test public void urlHandler_withMatchingDeepLinkUrl_shouldCallOnClickSuccess_shouldStartActivity() { final String deepLinkUrl = "appscheme: Robolectric.packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(deepLinkUrl)), new ResolveInfo()); new UrlHandler.Builder() .withSupportedUrlActions(FOLLOW_DEEP_LINK) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, deepLinkUrl); verify(mockResultActions).urlHandlingSucceeded(deepLinkUrl, FOLLOW_DEEP_LINK); verifyNoMoreCallbacks(); final Intent startedActivity = Robolectric.getShadowApplication().peekNextStartedActivity(); assertThat(startedActivity.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedActivity.getData()).isEqualTo(Uri.parse(deepLinkUrl)); }
@Test public void urlHandler_withDualMatchingUnresolvableUrlActions_shouldCallOnClickFailOnLastMatchedAction() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, FOLLOW_DEEP_LINK); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withShareTweetAndDeepLink_shouldCallOnClickFailOnLastMatchedDeepLink() { final String url = "mopubshare: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_SHARE_TWEET, FOLLOW_DEEP_LINK) .withResultActions(mockResultActions) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, FOLLOW_DEEP_LINK); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withNoConfiguration_shouldDoNothing() { new UrlHandler.Builder().build().handleUrl(context, ""); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withoutDestinationUrl_shouldNotError() { new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, ""); verify(mockResultActions).urlHandlingFailed("", NOOP); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withoutSupportedUrlActions_shouldNotError() { new UrlHandler.Builder() .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, "about:blank"); verify(mockResultActions).urlHandlingFailed("about:blank", NOOP); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withoutClickListener_shouldNotError() { new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, "about:blank"); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withoutMoPubSchemeListener_shouldNotError() { new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME) .withResultActions(mockResultActions) .build().handleUrl(context, "about:blank"); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withoutMoPubBrowser_shouldNotError() { new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .withoutMoPubBrowser() .build().handleUrl(context, "about:blank"); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withNullDestinationURL_shouldDoNothing() { final String nullUrl = null; new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, nullUrl); verify(mockResultActions).urlHandlingFailed(nullUrl, NOOP); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withEmptyDestinationURL_shouldDoNothing() { final String emptyUrl = ""; new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, emptyUrl); verify(mockResultActions).urlHandlingFailed(emptyUrl, NOOP); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withInvalidDestinationURL_shouldDoNothing() { final String invalidUrl = "some_invalid_url"; new UrlHandler.Builder() .withSupportedUrlActions(IGNORE_ABOUT_SCHEME, HANDLE_MOPUB_SCHEME, FOLLOW_DEEP_LINK, OPEN_IN_APP_BROWSER, HANDLE_PHONE_SCHEME, OPEN_NATIVE_BROWSER, HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, invalidUrl); verify(mockResultActions).urlHandlingFailed(invalidUrl, NOOP); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withMatchingMoPubSchemeWithoutMoPubSchemeListener_shouldDoNothing() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME) .withResultActions(mockResultActions) .build().handleUrl(context, url); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withInvalidMoPubSchemeCustom_shouldNotError() { final String url = "mopub: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_MOPUB_SCHEME) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, HANDLE_MOPUB_SCHEME); verifyNoMoreCallbacks(); }
@Test public void urlHandler_withInvalidNativeBrowserUrl_shouldCallUrlHandlingFailed() { final String url = "mopubnativebrowser: new UrlHandler.Builder() .withSupportedUrlActions(OPEN_NATIVE_BROWSER) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, OPEN_NATIVE_BROWSER); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withInvalidHostInShareTweetUrl_shouldCallUrlHandlingFailed() { final String url = "mopubshare: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, NOOP); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withMissingQueryParametersInShareTweetUrl_shouldCallUrlHandlingFailed() { final String url = "mopubshare: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, HANDLE_SHARE_TWEET); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withEmptyQueryParametersInShareTweetUrl_shouldCallUrlHandlingFailed() { final String url = "mopubshare: new UrlHandler.Builder() .withSupportedUrlActions(HANDLE_SHARE_TWEET) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, url); verify(mockResultActions).urlHandlingFailed(url, HANDLE_SHARE_TWEET); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }
@Test public void urlHandler_withMatchingUnresolvableDeepLinkUrl_shouldCallUrlHandlingFailed() { final String deepLinkUrl = "appscheme: new UrlHandler.Builder() .withSupportedUrlActions(FOLLOW_DEEP_LINK) .withResultActions(mockResultActions) .withMoPubSchemeListener(mockMoPubSchemeListener) .build().handleUrl(context, deepLinkUrl); verify(mockResultActions).urlHandlingFailed(deepLinkUrl, FOLLOW_DEEP_LINK); verifyNoMoreCallbacks(); verifyNoStartedActivity(); }