HtmlWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) { new UrlHandler.Builder() .withSupportedUrlActions( UrlAction.HANDLE_MOPUB_SCHEME, UrlAction.IGNORE_ABOUT_SCHEME, UrlAction.HANDLE_PHONE_SCHEME, UrlAction.OPEN_APP_MARKET, UrlAction.OPEN_NATIVE_BROWSER, UrlAction.OPEN_IN_APP_BROWSER, UrlAction.HANDLE_SHARE_TWEET, UrlAction.FOLLOW_DEEP_LINK) .withResultActions(new UrlHandler.ResultActions() { @Override public void urlHandlingSucceeded(@NonNull String url, @NonNull UrlAction urlAction) { if (mHtmlWebView.wasClicked()) { mHtmlWebViewListener.onClicked(); mHtmlWebView.onResetUserClick(); } } @Override public void urlHandlingFailed(@NonNull String url, @NonNull UrlAction lastFailedUrlAction) { } }) .withMoPubSchemeListener(new UrlHandler.MoPubSchemeListener() { @Override public void onFinishLoad() { mHtmlWebViewListener.onLoaded(mHtmlWebView); } @Override public void onClose() { mHtmlWebViewListener.onCollapsed(); } @Override public void onFailLoad() { mHtmlWebViewListener.onFailed(UNSPECIFIED); } }) .build().handleUrl(mContext, url, mHtmlWebView.wasClicked()); return true; } HtmlWebViewClient(HtmlWebViewListener htmlWebViewListener, BaseHtmlWebView htmlWebView, String clickthrough, String redirect); @Override boolean shouldOverrideUrlLoading(final WebView view, final String url); @Override void onPageStarted(WebView view, String url, Bitmap favicon);  }
@Test public void shouldOverrideUrlLoading_withMoPubFinishLoad_shouldCallAdDidLoad() throws Exception { boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onLoaded(eq(htmlWebView)); }
@Test public void shouldOverrideUrlLoading_withMoPubClose_shouldCallAdDidClose() throws Exception { boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onCollapsed(); }
@Test public void shouldOverrideUrlLoading_withMoPubFailLoad_shouldCallLoadFailUrl() throws Exception { boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onFailed(UNSPECIFIED); }
@Test public void shouldOverrideUrlLoading_withCustomApplicationIntent_withUserClick_andCanHandleCustomIntent_shouldTryToLaunchCustomIntent() throws Exception { String customUrl = "myintent: stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); Robolectric.packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(customUrl)), new ResolveInfo()); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, customUrl); assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onClicked(); Intent intent = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(intent).isNotNull(); }
@Test public void shouldOverrideUrlLoading_withCustomApplicationIntent_withoutUserClick_shouldNotTryToLaunchIntent() throws Exception { String customUrl = "myintent: stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, customUrl); assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withCustomApplicationIntent_withUserClick_butCanNotHandleCustomIntent_shouldFailSilently() throws Exception { String customUrl = "myintent: stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, customUrl); assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); Intent startedIntent = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedIntent).isNull(); }
@Test public void shouldOverrideUrlLoading_withHttpUrl_withUserClick_shouldOpenBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); String validUrl = "http: boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, validUrl); assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onClicked(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(validUrl); assertThat(startedActivity.getData()).isNull(); }
@Test public void shouldOverrideUrlLoading_withHttpUrl_withoutUserClick_shouldNotOpenBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); String validUrl = "http: boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, validUrl); assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withClickTrackingRedirect_withUserClick_shouldNotChangeUrl() throws Exception { String validUrl = "http: stub(htmlWebView.wasClicked()).toReturn(true); subject.shouldOverrideUrlLoading(htmlWebView, validUrl); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(validUrl); }
@Test public void shouldOverrideUrlLoading_withClickTrackingRedirect_withoutUserClick_shouldChangeUrl() throws Exception { String validUrl = "http: stub(htmlWebView.wasClicked()).toReturn(false); subject.shouldOverrideUrlLoading(htmlWebView, validUrl); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withEmptyUrl_withUserClick_shouldFailSilently() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, ""); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); }
@Test public void shouldOverrideUrlLoading_withEmptyUrl_withoutUserClick_shouldLoadAboutBlank() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, ""); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withUserClick_shouldStartIntentWithActionView() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopubnativebrowser: assertThat(shouldOverrideUrl).isTrue(); verify(htmlWebViewListener).onClicked(); verify(htmlWebView).onResetUserClick(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity.getAction()).isEqualTo("android.intent.action.VIEW"); assertThat(startedActivity.getData().toString()).isEqualTo("http: }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withoutUserClick_shouldStartIntentWithActionView() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopubnativebrowser: assertThat(shouldOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_butOpaqueUri_withUserClick_shouldNotBeHandledByNativeBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); String opaqueNativeBrowserUriString = "mopubnativebrowser:navigate?url=http%3A%2F%2Fwww.mopub.com"; subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, opaqueNativeBrowserUriString); assertThat(shouldOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_butOpaqueUri_withoutUserClick_shouldNotLoad() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); String opaqueNativeBrowserUriString = "mopubnativebrowser:navigate?url=http%3A%2F%2Fwww.mopub.com"; subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, opaqueNativeBrowserUriString); assertThat(shouldOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withInvalidHostSchemeUrl_withUserClick_shouldFailSilently() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "something: assertThat(shouldOverrideUrl).isTrue(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withInvalidHostSchemeUrl_withoutUserClick_shouldNotInvokeNativeBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); boolean shouldOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "something: assertThat(shouldOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withAboutBlankUrl_shouldFailSilently() { subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, "about:blank"); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); }