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_shell_apk.ContentShellActivity;
12 import org.chromium.content_shell_apk.ContentShellTestBase;
14 public class ContentViewTestBase extends ContentShellTestBase {
16 protected TestCallbackHelperContainer mTestCallbackHelperContainer;
19 * Sets up the ContentView and injects the supplied object. Intended to be called from setUp().
21 protected void setUpContentView(final Object object, final String name) throws Exception {
22 // This starts the activity, so must be called on the test thread.
23 final ContentShellActivity activity = launchContentShellWithUrl(
24 UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>"));
26 waitForActiveShellToBeDoneLoading();
28 // On the UI thread, load an empty page and wait for it to finish
29 // loading so that the Java object is injected.
31 runTestOnUiThread(new Runnable() {
34 ContentViewCore viewCore = activity.getActiveContentViewCore();
35 viewCore.addPossiblyUnsafeJavascriptInterface(object, name, null);
36 mTestCallbackHelperContainer = new TestCallbackHelperContainer(viewCore);
40 loadDataSync(activity.getActiveContentViewCore(),
41 "<!DOCTYPE html><title></title>", "text/html", false);
42 } catch (Throwable e) {
43 throw new RuntimeException(
44 "Failed to set up ContentView: " + Log.getStackTraceString(e));
49 * Loads data on the UI thread and blocks until onPageFinished is called.
50 * TODO(cramya): Move method to a separate util file once UiUtils.java moves into base.
52 protected void loadDataSync(final ContentViewCore contentViewCore, final String data,
53 final String mimeType, final boolean isBase64Encoded) throws Throwable {
54 loadUrl(contentViewCore, mTestCallbackHelperContainer, LoadUrlParams.createLoadDataParams(
55 data, mimeType, isBase64Encoded));