OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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.android_webview.test; |
| 6 |
| 7 import android.app.Instrumentation; |
| 8 import android.content.Context; |
| 9 import android.test.ActivityInstrumentationTestCase2; |
| 10 import android.view.View; |
| 11 |
| 12 import org.chromium.android_webview.AndroidWebViewUtil; |
| 13 import org.chromium.android_webview.AwContents; |
| 14 import org.chromium.content.browser.ContentSettings; |
| 15 import org.chromium.content.browser.ContentView; |
| 16 import org.chromium.content.browser.ContentViewClient; |
| 17 import org.chromium.content.browser.ContentViewCore; |
| 18 import org.chromium.content.browser.LoadUrlParams; |
| 19 import org.chromium.content.browser.test.CallbackHelper; |
| 20 |
| 21 import java.util.HashMap; |
| 22 import java.util.Map; |
| 23 import java.util.concurrent.Callable; |
| 24 import java.util.concurrent.ExecutionException; |
| 25 import java.util.concurrent.FutureTask; |
| 26 import java.util.concurrent.TimeUnit; |
| 27 import java.util.concurrent.TimeoutException; |
| 28 |
| 29 /** |
| 30 * A base class for android_webview tests. |
| 31 */ |
| 32 public class AndroidWebViewTestBase |
| 33 extends ActivityInstrumentationTestCase2<AndroidWebViewTestRunnerActivit
y> { |
| 34 protected static int WAIT_TIMEOUT_SECONDS = 15; |
| 35 |
| 36 public AndroidWebViewTestBase() { |
| 37 super(AndroidWebViewTestRunnerActivity.class); |
| 38 } |
| 39 |
| 40 @Override |
| 41 protected void setUp() throws Exception { |
| 42 final Context context = getActivity(); |
| 43 getInstrumentation().runOnMainSync(new Runnable() { |
| 44 @Override |
| 45 public void run() { |
| 46 ContentViewCore.initChromiumBrowserProcess( |
| 47 context, ContentView.MAX_RENDERERS_SINGLE_PROCESS); |
| 48 } |
| 49 }); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Runs a {@link Callable} on the main thread, blocking until it is |
| 54 * complete, and returns the result. Calls |
| 55 * {@link Instrumentation#waitForIdleSync()} first to help avoid certain |
| 56 * race conditions. |
| 57 * |
| 58 * @param <R> Type of result to return |
| 59 */ |
| 60 public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable) |
| 61 throws Throwable { |
| 62 FutureTask<R> task = new FutureTask<R>(callable); |
| 63 getInstrumentation().waitForIdleSync(); |
| 64 getInstrumentation().runOnMainSync(task); |
| 65 try { |
| 66 return task.get(); |
| 67 } catch (ExecutionException e) { |
| 68 // Unwrap the cause of the exception and re-throw it. |
| 69 throw e.getCause(); |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
| 74 * Loads url on the UI thread and blocks until onPageFinished is called. |
| 75 */ |
| 76 protected void loadUrlSync(final ContentViewCore contentViewCore, |
| 77 CallbackHelper onPageFinishedHelper, |
| 78 final String url) throws Throwable { |
| 79 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 80 loadUrlAsync(contentViewCore, url); |
| 81 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
| 82 TimeUnit.SECONDS); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Loads url on the UI thread but does not block. |
| 87 */ |
| 88 protected void loadUrlAsync(final ContentViewCore contentViewCore, |
| 89 final String url) throws Throwable { |
| 90 runTestOnUiThread(new Runnable() { |
| 91 @Override |
| 92 public void run() { |
| 93 contentViewCore.loadUrl(new LoadUrlParams(url)); |
| 94 } |
| 95 }); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Loads data on the UI thread and blocks until onPageFinished is called. |
| 100 */ |
| 101 protected void loadDataSync(final ContentViewCore contentViewCore, |
| 102 CallbackHelper onPageFinishedHelper, |
| 103 final String data, final String mimeType, |
| 104 final boolean isBase64Encoded) throws Throwable
{ |
| 105 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 106 loadDataAsync(contentViewCore, data, mimeType, isBase64Encoded); |
| 107 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
| 108 TimeUnit.SECONDS); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Loads data on the UI thread but does not block. |
| 113 */ |
| 114 protected void loadDataAsync(final ContentViewCore contentViewCore, final St
ring data, |
| 115 final String mimeType, final boolean isBase64En
coded) |
| 116 throws Throwable { |
| 117 runTestOnUiThread(new Runnable() { |
| 118 @Override |
| 119 public void run() { |
| 120 contentViewCore.loadUrl(LoadUrlParams.createLoadDataParams( |
| 121 data, mimeType, isBase64Encoded)); |
| 122 } |
| 123 }); |
| 124 } |
| 125 |
| 126 protected ContentView createContentView(boolean incognito, |
| 127 ContentViewClient contentViewClient)
{ |
| 128 int nativeWebContents = AndroidWebViewUtil.createNativeWebContents(incog
nito); |
| 129 ContentView contentView = ContentView.newInstance(getActivity(), nativeW
ebContents, |
| 130 ContentView.PERSONALIT
Y_VIEW); |
| 131 contentView.setContentViewClient(contentViewClient); |
| 132 return contentView; |
| 133 } |
| 134 } |
OLD | NEW |