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. 4 5 package org.chromium.content.browser; 6 7 import android.util.Log; 8 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; 13 14 public class ContentViewTestBase extends ContentShellTestBase { 15 16 protected TestCallbackHelperContainer mTestCallbackHelperContainer; 17 18 /** 19 * Sets up the ContentView and injects the supplied object. Intended to be called from setUp(). 20 */ setUpContentView(final Object object, final String name)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>")); 25 26 waitForActiveShellToBeDoneLoading(); 27 28 // On the UI thread, load an empty page and wait for it to finish 29 // loading so that the Java object is injected. 30 try { 31 runTestOnUiThread(new Runnable() { 32 @Override 33 public void run() { 34 ContentViewCore viewCore = activity.getActiveContentViewCore(); 35 viewCore.addPossiblyUnsafeJavascriptInterface(object, name, null); 36 mTestCallbackHelperContainer = new TestCallbackHelperContainer(viewCore); 37 } 38 }); 39 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)); 45 } 46 } 47 48 /** 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. 51 */ loadDataSync(final ContentViewCore contentViewCore, final String data, final String mimeType, final boolean isBase64Encoded)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)); 56 } 57 } 58