HtmlWebViewClient extends WebViewClient { boolean launchIntentForUserClick(Context context, Intent intent, String errorMessage) { if (!mHtmlWebView.wasClicked()) { return false; } boolean wasIntentStarted = Utils.executeIntent(context, intent, errorMessage); if (wasIntentStarted) { mHtmlWebViewListener.onClicked(); mHtmlWebView.onResetUserClick(); } return wasIntentStarted; } 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 launchIntentForUserClick_shouldStartActivityAndResetClickStatusAndReturnTrue() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); Context context = mock(Context.class); Intent intent = mock(Intent.class); boolean result = subject.launchIntentForUserClick(context, intent, null); verify(context).startActivity(eq(intent)); verify(htmlWebView).onResetUserClick(); assertThat(result).isTrue(); }
@Test public void launchIntentForUserClick_whenUserHasNotClicked_shouldNotStartActivityAndReturnFalse() throws Exception { stub(htmlWebView.wasClicked()).toReturn(false); Context context = mock(Context.class); Intent intent = mock(Intent.class); boolean result = subject.launchIntentForUserClick(context, intent, null); verify(context, never()).startActivity(any(Intent.class)); verify(htmlWebView, never()).onResetUserClick(); assertThat(result).isFalse(); }
@Test public void launchIntentForUserClick_whenNoMatchingActivity_shouldNotStartActivityAndReturnFalse() throws Exception { Context context = mock(Context.class); Intent intent = mock(Intent.class); stub(htmlWebView.wasClicked()).toReturn(true); doThrow(new ActivityNotFoundException()).when(context).startActivity(any(Intent.class)); boolean result = subject.launchIntentForUserClick(context, intent, null); verify(htmlWebView, never()).onResetUserClick(); assertThat(result).isFalse(); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); }
@Test public void launchIntentForUserClick_whenContextIsNull_shouldNotStartActivityAndReturnFalse() throws Exception { stub(htmlWebView.wasClicked()).toReturn(true); Intent intent = new Intent(); boolean result = subject.launchIntentForUserClick(null, intent, null); assertThat(Robolectric.getShadowApplication().getNextStartedActivity()).isNull(); verify(htmlWebView, never()).onResetUserClick(); assertThat(result).isFalse(); }