HtmlWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { if (mRedirectUrl != null && url.startsWith(mRedirectUrl)) { view.stopLoading(); showMoPubBrowserForUrl(url); } } 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 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 { stub(htmlWebView.wasClicked()).toReturn(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_shouldOpenInNativeBrowser() 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", "redirect"); subject.onPageStarted(htmlWebView, url, null); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(mockContext, times(2)).startActivity(intentArgumentCaptor.capture()); Intent intent = intentArgumentCaptor.getAllValues().get(1); assertThat(intent.getAction()).isEqualTo("android.intent.action.VIEW"); assertThat(intent.getData().toString()).isEqualTo("about:blank"); assertThat(intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK).isNotEqualTo(0); verify(htmlWebViewListener, times(1)).onClicked(); }
@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(); }