protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String url = getIntent().getStringExtra(EXTRA_URL); if (TextUtils.isEmpty(url)) { finish(); return; setTitle(url); setContentView(R.layout.activity_offline_web); final NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.nested_scroll_view); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setOnClickListener(v -> scrollView.smoothScrollTo(0, 0)); setSupportActionBar(toolbar); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE); getSupportActionBar().setSubtitle(R.string.offline); final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress); final WebView webView = (WebView) findViewById(R.id.web_view); webView.setBackgroundColor(Color.TRANSPARENT); webView.setWebViewClient(new AdBlockWebViewClient(Preferences.adBlockEnabled(this)) {
@Test public void testLoadUrl() { controller = Robolectric.buildActivity(OfflineWebActivity.class, new Intent() .putExtra(OfflineWebActivity.EXTRA_URL, "http://example.com")); activity = controller .create() .get(); assertThat(activity.getTitle()).contains("http://example.com"); 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 testScrollToTop() { controller = Robolectric.buildActivity(OfflineWebActivity.class, new Intent() .putExtra(OfflineWebActivity.EXTRA_URL, "http://example.com")); activity = controller .create() .start() .resume() .visible() .get(); activity.findViewById(R.id.toolbar).performClick(); assertThat(((ShadowNestedScrollView) Shadow .extract(activity.findViewById(R.id.nested_scroll_view))).getSmoothScrollY()) .isEqualTo(0); } @Test
@Override public void onPageFinished(WebView view, String url) { setTitle(view.getTitle()); } });
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } }