@Test public void LoadReceipt() { Intent intent = new Intent(); final Bundle bundle = new Bundle(); bundle.putString("receipt", "receipt"); intent.putExtras(bundle); ActivityController activityController = Robolectric.buildActivity( ReceiptViewActivity.class).withIntent(intent).create(); activityController.start(); activityController.resume(); Activity activity = (Activity)activityController.get(); WebView receiptView = (WebView)activity.findViewById(R.id.imageView); ShadowWebView.LoadDataWithBaseURL loadedData = shadowOf(receiptView).getLastLoadDataWithBaseURL(); assertEquals("", loadedData.baseUrl); assertEquals("text/html", loadedData.mimeType); assertEquals("utf-8", loadedData.encoding); assertNull(loadedData.historyUrl); assertTrue(loadedData.data.contains("src=\"file://receipt\"")); }
@Test public void LoadReceipt() { activityController.start(); activityController.resume(); Activity activity = (Activity)activityController.get(); WebView receiptView = (WebView)activity.findViewById(R.id.imageView); ShadowWebView.LoadDataWithBaseURL loadedData = shadowOf(receiptView).getLastLoadDataWithBaseURL(); assertEquals("", loadedData.baseUrl); assertEquals("text/html", loadedData.mimeType); assertEquals("utf-8", loadedData.encoding); assertNull(loadedData.historyUrl); assertTrue(loadedData.data.contains("src=\"file://receipt\"")); }
@Test public void testLoadUrl() { activity = controller.withIntent(new Intent() .putExtra(OfflineWebActivity.EXTRA_URL, "http://example.com")) .create() .get(); assertThat(activity.getTitle()).contains("http://example.com"); WebView webView = (WebView) activity.findViewById(R.id.web_view); View progress = activity.findViewById(R.id.progress); ShadowWebView shadowWebView = shadowOf(webView); assertThat(shadowWebView.getLastLoadedUrl()) .contains("http://example.com"); shadowWebView.getWebViewClient().onPageFinished(webView, "http://example.com"); assertThat(activity.getTitle()).isNullOrEmpty(); // web view title shadowWebView.getWebChromeClient().onProgressChanged(webView, 50); assertThat(progress).isVisible(); shadowWebView.getWebChromeClient().onProgressChanged(webView, 100); assertThat(progress).isNotVisible(); }
@Test public void testStory() { TestWebItem item = new TestWebItem() { @NonNull @Override public String getType() { return STORY_TYPE; } @Override public String getId() { return "1"; } @Override public String getUrl() { return String.format(HackerNewsClient.WEB_ITEM_PATH, "1"); } @Override public String getDisplayedTitle() { return "Ask HN"; } }; Intent intent = new Intent(); intent.putExtra(WebActivity.EXTRA_ITEM, item); controller.withIntent(intent).create().start().resume().visible(); verify(itemManager).getItem(eq("1"), eq(ItemManager.MODE_DEFAULT), listener.capture()); listener.getValue().onResponse(new TestItem() { @Override public String getText() { return "text"; } }); WebView webView = (WebView) activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = shadowOf(webView); shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank"); assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("text"); }
@Test public void testComment() { TestItem item = new TestItem() { @NonNull @Override public String getType() { return COMMENT_TYPE; } @Override public String getId() { return "1"; } @Override public String getUrl() { return String.format(HackerNewsClient.WEB_ITEM_PATH, "1"); } @Override public String getText() { return "comment"; } }; Intent intent = new Intent(); intent.putExtra(WebActivity.EXTRA_ITEM, item); controller.withIntent(intent).create().start().resume().visible(); WebView webView = (WebView) activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = shadowOf(webView); shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank"); assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("comment"); }
@Test public void loadHtmlResponse_shouldCallLoadDataWithBaseURL() throws Exception { String htmlResponse = "some random html response"; subject.loadHtmlResponse(htmlResponse); ShadowWebView.LoadDataWithBaseURL lastLoadData = shadowOf(subject).getLastLoadDataWithBaseURL(); assertThat(lastLoadData.baseUrl).isEqualTo("http://ads.mopub.com/"); assertThat(lastLoadData.data).isEqualTo(htmlResponse); assertThat(lastLoadData.mimeType).isEqualTo("text/html"); assertThat(lastLoadData.encoding).isEqualTo("utf-8"); assertThat(lastLoadData.historyUrl).isNull(); }
@Test public void destroy_shouldRemoveSelfFromParent() throws Exception { ViewGroup parentView = mock(ViewGroup.class); ShadowWebView shadow = shadowOf(subject); shadow.setMyParent(parentView); subject.destroy(); verify(parentView).removeView(eq(subject)); assertThat(shadow.wasDestroyCalled()); }
@Test public void loadData_shouldCallLoadDataWithBaseURL() throws Exception { String data = "some random html response"; subject.loadData(data); ShadowWebView.LoadDataWithBaseURL lastLoadData = shadowOf(subject).getLastLoadDataWithBaseURL(); assertThat(lastLoadData.baseUrl).isEqualTo("http://ads.mopub.com/"); assertThat(lastLoadData.data).isEqualTo(data); assertThat(lastLoadData.mimeType).isEqualTo("text/html"); assertThat(lastLoadData.encoding).isEqualTo("utf-8"); assertThat(lastLoadData.historyUrl).isNull(); }
@Test public void destroy_shouldRemoveSelfFromParent_beforeCallingDestroy() throws Exception { subject = new BaseWebView(context); ViewGroup parent = mock(ViewGroup.class); ShadowWebView shadow = shadowOf(subject); shadow.setMyParent(parent); subject.destroy(); verify(parent).removeView(eq(subject)); assertThat(shadow.wasDestroyCalled()).isTrue(); }
/** * Generic method for testing proper display of WebViewDialogFragment * * @param url The url to load * @param title The title to show, if any */ private void test_StartWebViewActivity_LoadsUrlAndShowsTitle(@NonNull String url, @Nullable String title) throws PackageManager.NameNotFoundException { final WebViewActivity activity = Robolectric.buildActivity(WebViewActivity.class) .withIntent(WebViewActivity.newIntent( RuntimeEnvironment.application, url, title)).setup().get(); final View contentView = Shadows.shadowOf(activity).getContentView(); assertNotNull(contentView); final WebView webView = (WebView) contentView.findViewById(R.id.webView); assertNotNull(webView); final ShadowWebView shadowWebView = Shadows.shadowOf(webView); assertEquals(shadowWebView.getLastLoadedUrl(), url); final ActionBar actionBar = activity.getSupportActionBar(); assertNotNull(actionBar); assertThat(actionBar).isShowing(); if (!TextUtils.isEmpty(title)) { assertThat(actionBar).hasTitle(title); } /* Robolectric is not providing the correct default title which is why this code has been commented. else { final PackageManager pm = activity.getPackageManager(); final ActivityInfo aInfo = pm.getActivityInfo(activity.getComponentName(), 0); final String defaultTitle = aInfo.loadLabel(pm).toString(); assertThat(actionBar).hasTitle(defaultTitle); } */ }
@Test public void WebViewで読み込んだURLのテスト() { Activity activity = Robolectric .buildActivity(RobolectricSampleActivity.class).create().get(); WebView webView = (WebView) activity.findViewById(R.id.webview); ShadowWebView shadowWebView = Robolectric.shadowOf((WebView) webView); assertThat("http://robolectric.org/index.html", is(shadowWebView.getLastLoadedUrl())); }
@Test public void testMenu() { TestWebItem item = new TestWebItem() { @NonNull @Override public String getType() { return STORY_TYPE; } @Override public String getId() { return "1"; } @Override public String getUrl() { return String.format(HackerNewsClient.WEB_ITEM_PATH, "1"); } @Override public String getDisplayedTitle() { return "Ask HN"; } }; Intent intent = new Intent(); intent.putExtra(WebActivity.EXTRA_ITEM, item); controller.withIntent(intent).create().start().resume().visible(); verify(itemManager).getItem(eq("1"), eq(ItemManager.MODE_DEFAULT), listener.capture()); listener.getValue().onResponse(new TestItem() { @Override public String getText() { return "text"; } }); Fragment fragment = activity.getSupportFragmentManager() .findFragmentByTag(WebFragment.class.getName()); assertTrue(fragment.hasOptionsMenu()); fragment.onOptionsItemSelected(new RoboMenuItem(R.id.menu_font_options)); assertNotNull(ShadowDialog.getLatestDialog()); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putString(activity.getString(R.string.pref_readability_font), "DroidSans.ttf") .apply(); WebView webView = (WebView) activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = shadowOf(webView); shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank"); assertThat(shadowWebView.getLastLoadDataWithBaseURL().data) .contains("text") .contains("DroidSans.ttf"); }
public static ShadowWebView shadowOf(WebView instance) { return (ShadowWebView) shadowOf_(instance); }