HtmlWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { if (mRedirectUrl != null && url.startsWith(mRedirectUrl)) { view.stopLoading(); if (mHtmlWebView.wasClicked()) { try { Intents.showMoPubBrowserForUrl(mContext, Uri.parse(url)); } catch (IntentNotResolvableException e) { MoPubLog.d(e.getMessage()); } } else { MoPubLog.d("Attempted to redirect without user interaction"); } } } 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 onPageStarted_whenLoadedUrlStartsWithRedirect_withUserClick_shouldOpenInBrowser() throws Exception { String url = "redirectUrlToLoad"; stub(htmlWebView.wasClicked()).toReturn(true); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, "redirect"); WebView view = mock(WebView.class); subject.onPageStarted(view, url, null); verify(view).stopLoading(); Intent intent = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(intent.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(url); assertThat(intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0); assertThat(intent.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); }
@Test public void onPageStarted_whenLoadedUrlStartsWithRedirect_withoutUserClick_shouldOpenInBrowser() throws Exception { String url = "redirectUrlToLoad"; stub(htmlWebView.wasClicked()).toReturn(false); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, null, "redirect"); WebView view = mock(WebView.class); subject.onPageStarted(view, url, null); verify(view).stopLoading(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void onPageStarted_whenLoadedUrlStartsWithRedirectAndHasClickthrough_withUserClick_shouldNotChangeUrl_shouldOpenInBrowser() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); String url = "redirectUrlToLoad"; WebView view = mock(WebView.class); subject.onPageStarted(view, url, null); verify(view).stopLoading(); Intent intent = Robolectric.getShadowApplication().getNextStartedActivity(); assertThat(intent.getStringExtra(MoPubBrowser.DESTINATION_URL_KEY)).isEqualTo(url); assertThat(intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0); assertThat(intent.getComponent().getClassName()).isEqualTo("com.mopub.common.MoPubBrowser"); }
@Test public void onPageStarted_whenLoadedUrlStartsWithRedirectAndHasClickthrough_withoutUserClick_shouldNotOpenInBrowser() throws Exception { when(htmlWebView.wasClicked()).thenReturn(false); String url = "redirectUrlToLoad"; WebView view = mock(WebView.class); subject.onPageStarted(view, url, null); verify(view).stopLoading(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void onPageStarted_whenLoadedUrlStartsWithRedirectAndHasClickthrough_withUserClick_whenMoPubBrowserCannotHandleIntent_shouldFailSilently() throws Exception { Context mockContext = mock(Context.class); stub(htmlWebView.wasClicked()).toReturn(true); stub(htmlWebView.getContext()).toReturn(mockContext); String url = "redirectUrlToLoad"; doThrow(new ActivityNotFoundException()) .doNothing() .when(mockContext).startActivity(any(Intent.class)); subject = new HtmlWebViewClient(htmlWebViewListener, htmlWebView, CLICKTHROUGH_URL, "redirect"); subject.onPageStarted(htmlWebView, url, null); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(mockContext).startActivity(intentArgumentCaptor.capture()); Intent intent = intentArgumentCaptor.getValue(); assertThat(intent.getData()).isNull(); verify(htmlWebViewListener, never()).onClicked(); verify(htmlWebView, never()).onResetUserClick(); }
@Test public void onPageStarted_whenLoadedUrlDoesntStartWithRedirect_shouldDoNothing() throws Exception { WebView view = mock(WebView.class); subject.onPageStarted(view, "this doesn't start with redirect", null); verify(view, never()).stopLoading(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }