org.robolectric.shadows.ShadowWebView Java Examples

The following examples show how to use org.robolectric.shadows.ShadowWebView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReceiptViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 6 votes vote down vote up
@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\""));
}
 
Example #2
Source File: ReceiptViewActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@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\""));
}
 
Example #3
Source File: OfflineWebActivityTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #4
Source File: LoginActivityTest.java    From rides-android-sdk with MIT License 6 votes vote down vote up
@Test
public void onLoginLoad_withResponseTypeToken_andForceWebview_andPrivilegedScopes_andRedirectToPlayStoreDisabled_shouldLoadWebview() {
    loginConfiguration = new SessionConfiguration.Builder()
            .setClientId(CLIENT_ID)
            .setRedirectUri(REDIRECT_URI)
            .setScopes(MIXED_SCOPES)
            .build();
    Intent intent = LoginActivity.newIntent(Robolectric.setupActivity(Activity.class), loginConfiguration,
            ResponseType.TOKEN, true);

    loginActivity = Robolectric.buildActivity(LoginActivity.class).withIntent(intent).create().get();
    ShadowWebView webview = Shadows.shadowOf(loginActivity.webView);

    String expectedUrl = AuthUtils.buildUrl(REDIRECT_URI, ResponseType.TOKEN, loginConfiguration);
    assertThat(webview.getLastLoadedUrl()).isEqualTo(expectedUrl);
}
 
Example #5
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@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 = Robolectric.buildActivity(WebActivity.class, intent);
    controller.create().start().resume().visible();
    activity = controller.get();
    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = shadowOf(webView);
    shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank");
    assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("comment");
}
 
Example #6
Source File: LoginActivityTest.java    From rides-android-sdk with MIT License 5 votes vote down vote up
@Test
public void onLoginLoad_withResponseTypeCode_andForceWebview_shouldLoadWebview() {
    Intent intent = LoginActivity.newIntent(Robolectric.setupActivity(Activity.class), loginConfiguration,
            ResponseType.CODE, true);

    loginActivity = Robolectric.buildActivity(LoginActivity.class).withIntent(intent).create().get();
    ShadowWebView webview = Shadows.shadowOf(loginActivity.webView);

    String expectedUrl = AuthUtils.buildUrl(REDIRECT_URI, ResponseType.CODE, loginConfiguration);
    assertThat(webview.getLastLoadedUrl()).isEqualTo(expectedUrl);
}
 
Example #7
Source File: LoginActivityTest.java    From rides-android-sdk with MIT License 5 votes vote down vote up
@Test
public void onLoginLoad_withResponseTypeToken_andForceWebview_andGeneralScopes_shouldLoadWebview() {
    Intent intent = LoginActivity.newIntent(Robolectric.setupActivity(Activity.class), loginConfiguration,
            ResponseType.TOKEN, true);

    loginActivity = Robolectric.buildActivity(LoginActivity.class).withIntent(intent).create().get();
    ShadowWebView webview = Shadows.shadowOf(loginActivity.webView);

    String expectedUrl = AuthUtils.buildUrl(REDIRECT_URI, ResponseType.TOKEN, loginConfiguration);
    assertThat(webview.getLastLoadedUrl()).isEqualTo(expectedUrl);
}
 
Example #8
Source File: WebViewActivityTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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,
                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);
    assertTrue(actionBar.isShowing());
    if (!TextUtils.isEmpty(title)) {
        assertEquals(title, actionBar.getTitle());
    }
    /*
    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);
    }
    */
}
 
Example #9
Source File: RobolectricSampleActivityTest.java    From android-opensource-library-56 with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #10
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@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 = Robolectric.buildActivity(WebActivity.class, intent);
    controller.create().start().resume().visible();
    activity = controller.get();
    verify(itemManager).getItem(eq("1"), eq(ItemManager.MODE_DEFAULT), listener.capture());
    listener.getValue().onResponse(new TestItem() {
        @Override
        public String getText() {
            return "text";
        }
    });
    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = shadowOf(webView);
    shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank");
    assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("text");
}
 
Example #11
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@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 = Robolectric.buildActivity(WebActivity.class, intent);
    controller.create().start().resume().visible();
    activity = controller.get();
    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 = 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");
}