Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ContentViewTestBase.java
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_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;
15
16 /**
17  * Provides test environment for ContentView Test Shell.
18  * This is a helper class for Content Shell tests.
19 */
20 public class ContentViewTestBase extends ContentShellTestBase {
21
22     protected TestCallbackHelperContainer mTestCallbackHelperContainer;
23
24     /**
25      * Sets up the ContentView and injects the supplied object. Intended to be called from setUp().
26      */
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>"));
31
32         waitForActiveShellToBeDoneLoading();
33
34         // On the UI thread, load an empty page and wait for it to finish
35         // loading so that the Java object is injected.
36         try {
37             runTestOnUiThread(new Runnable() {
38                 @Override
39                 public void run() {
40                     ContentViewCore viewCore = activity.getActiveContentViewCore();
41                     viewCore.addPossiblyUnsafeJavascriptInterface(object, name, null);
42                     mTestCallbackHelperContainer = new TestCallbackHelperContainer(viewCore);
43                 }
44             });
45
46             loadDataSync(activity.getActiveContentViewCore().getWebContents()
47                     .getNavigationController(), "<!DOCTYPE html><title></title>", "text/html",
48                             false);
49         } catch (Throwable e) {
50             throw new RuntimeException(
51                     "Failed to set up ContentView: " + Log.getStackTraceString(e));
52         }
53     }
54
55     /**
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.
58      */
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));
63     }
64 }