1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.util.Log;
9 import org.chromium.base.test.util.UrlUtils;
10 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
11 import org.chromium.content_public.browser.LoadUrlParams;
12 import org.chromium.content_public.browser.NavigationController;
13 import org.chromium.content_shell_apk.ContentShellActivity;
14 import org.chromium.content_shell_apk.ContentShellTestBase;
17 * Provides test environment for ContentView Test Shell.
18 * This is a helper class for Content Shell tests.
20 public class ContentViewTestBase extends ContentShellTestBase {
22 protected TestCallbackHelperContainer mTestCallbackHelperContainer;
25 * Sets up the ContentView and injects the supplied object. Intended to be called from setUp().
27 protected void setUpContentView(final Object object, final String name) throws Exception {
28 // This starts the activity, so must be called on the test thread.
29 final ContentShellActivity activity = launchContentShellWithUrl(
30 UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>"));
32 waitForActiveShellToBeDoneLoading();
34 // On the UI thread, load an empty page and wait for it to finish
35 // loading so that the Java object is injected.
37 runTestOnUiThread(new Runnable() {
40 ContentViewCore viewCore = activity.getActiveContentViewCore();
41 viewCore.addPossiblyUnsafeJavascriptInterface(object, name, null);
42 mTestCallbackHelperContainer = new TestCallbackHelperContainer(viewCore);
46 loadDataSync(activity.getActiveContentViewCore().getWebContents()
47 .getNavigationController(), "<!DOCTYPE html><title></title>", "text/html",
49 } catch (Throwable e) {
50 throw new RuntimeException(
51 "Failed to set up ContentView: " + Log.getStackTraceString(e));
56 * Loads data on the UI thread and blocks until onPageFinished is called.
57 * TODO(cramya): Move method to a separate util file once UiUtils.java moves into base.
59 protected void loadDataSync(final NavigationController navigationController, final String data,
60 final String mimeType, final boolean isBase64Encoded) throws Throwable {
61 loadUrl(navigationController, mTestCallbackHelperContainer,
62 LoadUrlParams.createLoadDataParams(data, mimeType, isBase64Encoded));