HtmlWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (handleSpecialMoPubScheme(url) || handlePhoneScheme(url) || handleNativeBrowserScheme(url)) { return true; } MoPubLog.d("Ad clicked. Click URL: " + url); if (!isWebSiteUrl(url) && IntentUtils.canHandleApplicationUrl(mContext, url)) { if (launchApplicationUrl(url)) { return true; } } showMoPubBrowserForUrl(url); return true; } HtmlWebViewClient(HtmlWebViewListener htmlWebViewListener, BaseHtmlWebView htmlWebView, String clickthrough, String redirect); @Override boolean shouldOverrideUrlLoading(WebView view, 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_withMoPubCustom_withUserClick_shouldStartCustomIntent() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onClicked(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNotNull(); assertThat(startedActivity.getAction()).isEqualTo("myFnc"); assertThat(startedActivity.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0); assertThat(startedActivity.getStringExtra(HtmlBannerWebView.EXTRA_AD_CLICK_DATA)).isEqualTo("myData"); }
@Test public void shouldOverrideUrlLoading_withMoPubCustom_withoutUserClick_shouldNotStartActivity() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); }
@Test public void shouldOverrideUrlLoading_withMoPubCustomAndNullData_withUserClick_shouldStartCustomIntent() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener).onClicked(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNotNull(); assertThat(startedActivity.getAction()).isEqualTo("myFnc"); assertThat(startedActivity.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0); assertThat(startedActivity.getStringExtra(HtmlBannerWebView.EXTRA_AD_CLICK_DATA)).isNull(); }
@Test public void shouldOverrideUrlLoading_withMoPubCustomAndNullData_withoutUserClick_shouldNotStartCustomIntent() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); boolean didOverrideUrl = subject.shouldOverrideUrlLoading(htmlWebView, "mopub: assertThat(didOverrideUrl).isTrue(); verify(htmlWebViewListener, never()).onClicked(); Intent startedActivity = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedActivity).isNull(); }
@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(); assertActivityStarted(); }
@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(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withCustomApplicationIntent_withUserClick_butCanNotHandleCustomIntent_shouldDefaultToMoPubBrowser() 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).onClicked(); Intent startedIntent = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(startedIntent).isNotNull(); assertThat(startedIntent.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); }
@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 = assertActivityStarted(); 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(); 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 = assertActivityStarted(); 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_shouldLoadAboutBlank() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, ""); Intent startedActivity = assertActivityStarted(); assertThat(startedActivity.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo("about:blank"); assertThat(startedActivity.getData()).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_withoutUserClick_shouldStartIntentWithActionView() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, "mopubnativebrowser: verify(htmlWebViewListener, never()).onClicked(); 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: subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, opaqueNativeBrowserUriString); Intent startedActivity = assertActivityStarted(); assertThat(startedActivity.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); assertThat(startedActivity.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(opaqueNativeBrowserUriString); assertThat(startedActivity.getData()).isNull(); verify(htmlWebViewListener).onClicked(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_butOpaqueUri_withoutUserClick_shouldNotLoad() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); String opaqueNativeBrowserUriString = "mopubnativebrowser:navigate?url=http: subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, opaqueNativeBrowserUriString); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withInvalidHostSchemeUrl_withUserClick_shouldNotInvokeNativeBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, "something: Intent startedActivity = assertActivityStarted(); assertThat(startedActivity.getAction()).isNotEqualTo("android.intent.action.VIEW"); verify(htmlWebViewListener).onClicked(); }
@Test public void shouldOverrideUrlLoading_withNativeBrowserScheme_withInvalidHostSchemeUrl_withoutUserClick_shouldNotInvokeNativeBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, null); subject.shouldOverrideUrlLoading(htmlWebView, "something: assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }