ContentViewTestBase.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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 5package org.chromium.content.browser; 6 7import android.util.Log; 8 9import org.chromium.base.test.util.UrlUtils; 10import org.chromium.content.browser.test.util.TestCallbackHelperContainer; 11import org.chromium.content_shell_apk.ContentShellActivity; 12import org.chromium.content_shell_apk.ContentShellTestBase; 13 14public 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 */ 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 ContentView contentView = activity.getActiveContentView(); 35 contentView.getContentViewCore().addPossiblyUnsafeJavascriptInterface(object, 36 name, null); 37 mTestCallbackHelperContainer = 38 new TestCallbackHelperContainer(contentView); 39 } 40 }); 41 42 loadDataSync(activity.getActiveContentView(), 43 "<!DOCTYPE html><title></title>", "text/html", false); 44 } catch (Throwable e) { 45 throw new RuntimeException( 46 "Failed to set up ContentView: " + Log.getStackTraceString(e)); 47 } 48 } 49 50 /** 51 * Loads data on the UI thread and blocks until onPageFinished is called. 52 * TODO(cramya): Move method to a separate util file once UiUtils.java moves into base. 53 */ 54 protected void loadDataSync(final ContentView contentView, final String data, 55 final String mimeType, final boolean isBase64Encoded) throws Throwable { 56 loadUrl(contentView, mTestCallbackHelperContainer, LoadUrlParams.createLoadDataParams( 57 data, mimeType, isBase64Encoded)); 58 } 59} 60