| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
| 6 | 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 | 8 |
| 9 import android.app.Instrumentation; | 9 import android.app.Instrumentation; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.os.Build; | |
| 12 import android.util.AndroidRuntimeException; | 11 import android.util.AndroidRuntimeException; |
| 13 import android.util.Log; | 12 |
| 14 import android.view.ViewGroup; | 13 import org.junit.Assert; |
| 15 | 14 |
| 16 import org.chromium.android_webview.AwBrowserContext; | 15 import org.chromium.android_webview.AwBrowserContext; |
| 17 import org.chromium.android_webview.AwBrowserProcess; | 16 import org.chromium.android_webview.AwBrowserProcess; |
| 18 import org.chromium.android_webview.AwContents; | 17 import org.chromium.android_webview.AwContents; |
| 19 import org.chromium.android_webview.AwContents.DependencyFactory; | |
| 20 import org.chromium.android_webview.AwContents.InternalAccessDelegate; | |
| 21 import org.chromium.android_webview.AwContents.NativeDrawGLFunctorFactory; | |
| 22 import org.chromium.android_webview.AwContentsClient; | 18 import org.chromium.android_webview.AwContentsClient; |
| 23 import org.chromium.android_webview.AwSettings; | 19 import org.chromium.android_webview.AwSettings; |
| 24 import org.chromium.android_webview.AwSwitches; | 20 import org.chromium.android_webview.test.AwTestBase.PopupInfo; |
| 21 import org.chromium.android_webview.test.AwTestBase.TestDependencyFactory; |
| 25 import org.chromium.android_webview.test.util.GraphicsTestUtils; | 22 import org.chromium.android_webview.test.util.GraphicsTestUtils; |
| 26 import org.chromium.android_webview.test.util.JSUtils; | 23 import org.chromium.android_webview.test.util.JSUtils; |
| 24 import org.chromium.base.Log; |
| 27 import org.chromium.base.ThreadUtils; | 25 import org.chromium.base.ThreadUtils; |
| 28 import org.chromium.base.test.BaseActivityInstrumentationTestCase; | |
| 29 import org.chromium.base.test.util.CallbackHelper; | 26 import org.chromium.base.test.util.CallbackHelper; |
| 30 import org.chromium.base.test.util.InMemorySharedPreferences; | 27 import org.chromium.base.test.util.InMemorySharedPreferences; |
| 31 import org.chromium.base.test.util.MinAndroidSdkLevel; | |
| 32 import org.chromium.base.test.util.parameter.CommandLineParameter; | |
| 33 import org.chromium.content.browser.test.util.Criteria; | 28 import org.chromium.content.browser.test.util.Criteria; |
| 34 import org.chromium.content.browser.test.util.CriteriaHelper; | 29 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 35 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; | 30 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; |
| 36 import org.chromium.content_public.browser.LoadUrlParams; | 31 import org.chromium.content_public.browser.LoadUrlParams; |
| 37 import org.chromium.net.test.util.TestWebServer; | 32 import org.chromium.net.test.util.TestWebServer; |
| 38 | 33 |
| 39 import java.lang.annotation.Annotation; | 34 import java.lang.annotation.Annotation; |
| 40 import java.lang.reflect.AnnotatedElement; | |
| 41 import java.lang.reflect.Method; | |
| 42 import java.util.Map; | 35 import java.util.Map; |
| 43 import java.util.concurrent.Callable; | 36 import java.util.concurrent.Callable; |
| 44 import java.util.concurrent.FutureTask; | 37 import java.util.concurrent.FutureTask; |
| 45 import java.util.concurrent.TimeUnit; | 38 import java.util.concurrent.TimeUnit; |
| 46 import java.util.regex.Matcher; | 39 import java.util.regex.Matcher; |
| 47 import java.util.regex.Pattern; | 40 import java.util.regex.Pattern; |
| 48 | 41 |
| 49 /** | 42 // TODO(yolandyan): move this class to its test rule once JUnit4 migration is ov
er |
| 50 * A base class for android_webview tests. WebView only runs on KitKat and later
, | 43 final class AwTestCommon { |
| 51 * so make sure no one attempts to run the tests on earlier OS releases. | |
| 52 * | |
| 53 * By default, all tests run both in single-process mode, and with sandboxed ren
derer. | |
| 54 * If a test doesn't yet work with sandboxed renderer, an entire class, or an in
dividual test | |
| 55 * method can be marked for single-process testing only by adding the following
annotation: | |
| 56 * | |
| 57 * @SkipCommandLineParameterization | |
| 58 */ | |
| 59 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT) | |
| 60 @CommandLineParameter({"", AwSwitches.WEBVIEW_SANDBOXED_RENDERER}) | |
| 61 public class AwTestBase extends BaseActivityInstrumentationTestCase<AwTestRunner
Activity> { | |
| 62 public static final long WAIT_TIMEOUT_MS = scaleTimeout(15000); | 44 public static final long WAIT_TIMEOUT_MS = scaleTimeout(15000); |
| 45 |
| 63 public static final int CHECK_INTERVAL = 100; | 46 public static final int CHECK_INTERVAL = 100; |
| 64 private static final String TAG = "AwTestBase"; | 47 |
| 48 private static final String TAG = "AwTestCommon"; |
| 49 |
| 65 private static final Pattern MAYBE_QUOTED_STRING = Pattern.compile("^(\"?)(.
*)\\1$"); | 50 private static final Pattern MAYBE_QUOTED_STRING = Pattern.compile("^(\"?)(.
*)\\1$"); |
| 66 | 51 |
| 67 // The browser context needs to be a process-wide singleton. | 52 // The browser context needs to be a process-wide singleton. |
| 68 private AwBrowserContext mBrowserContext; | 53 private AwBrowserContext mBrowserContext; |
| 69 | 54 |
| 70 public AwTestBase() { | 55 private final AwTestCommonCallback mCallback; |
| 71 super(AwTestRunnerActivity.class); | 56 |
| 57 AwTestCommon(AwTestCommonCallback callback) { |
| 58 mCallback = callback; |
| 72 } | 59 } |
| 73 | 60 |
| 74 @Override | 61 void setUp() throws Exception { |
| 75 protected void setUp() throws Exception { | |
| 76 if (needsAwBrowserContextCreated()) { | 62 if (needsAwBrowserContextCreated()) { |
| 77 createAwBrowserContext(); | 63 createAwBrowserContext(); |
| 78 } | 64 } |
| 79 | |
| 80 super.setUp(); | |
| 81 if (needsBrowserProcessStarted()) { | 65 if (needsBrowserProcessStarted()) { |
| 82 startBrowserProcess(); | 66 startBrowserProcess(); |
| 83 } | 67 } |
| 84 } | 68 } |
| 85 | 69 |
| 86 protected void createAwBrowserContext() { | 70 void createAwBrowserContext() { |
| 87 if (mBrowserContext != null) { | 71 if (mBrowserContext != null) { |
| 88 throw new AndroidRuntimeException("There should only be one browser
context."); | 72 throw new AndroidRuntimeException("There should only be one browser
context."); |
| 89 } | 73 } |
| 90 getActivity(); // The Activity must be launched in order to load native
code | 74 mCallback.getActivity(); // The Activity must be launched in order to lo
ad native code |
| 91 final InMemorySharedPreferences prefs = new InMemorySharedPreferences(); | 75 final InMemorySharedPreferences prefs = new InMemorySharedPreferences(); |
| 92 final Context appContext = getInstrumentation().getTargetContext().getAp
plicationContext(); | 76 final Context appContext = |
| 93 getInstrumentation().runOnMainSync(new Runnable() { | 77 mCallback.getInstrumentation().getTargetContext().getApplication
Context(); |
| 78 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 94 @Override | 79 @Override |
| 95 public void run() { | 80 public void run() { |
| 96 mBrowserContext = createAwBrowserContextOnUiThread(prefs, appCon
text); | 81 mBrowserContext = createAwBrowserContextOnUiThread(prefs, appCon
text); |
| 97 } | 82 } |
| 98 }); | 83 }); |
| 99 } | 84 } |
| 100 | 85 |
| 101 protected AwBrowserContext createAwBrowserContextOnUiThread( | 86 AwBrowserContext createAwBrowserContextOnUiThread( |
| 102 InMemorySharedPreferences prefs, Context appContext) { | 87 InMemorySharedPreferences prefs, Context appContext) { |
| 103 return new AwBrowserContext(prefs, appContext); | 88 return new AwBrowserContext(prefs, appContext); |
| 104 } | 89 } |
| 105 | 90 |
| 106 protected void startBrowserProcess() throws Exception { | 91 void startBrowserProcess() throws Exception { |
| 107 // The Activity must be launched in order for proper webview statics to
be setup. | 92 // The Activity must be launched in order for proper webview statics to
be setup. |
| 108 getActivity(); | 93 mCallback.getActivity(); |
| 109 getInstrumentation().runOnMainSync(new Runnable() { | 94 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 110 @Override | 95 @Override |
| 111 public void run() { | 96 public void run() { |
| 112 AwBrowserProcess.start(); | 97 AwBrowserProcess.start(); |
| 113 } | 98 } |
| 114 }); | 99 }); |
| 115 } | 100 } |
| 116 | 101 |
| 117 /** | 102 boolean needsAwBrowserContextCreated() { |
| 118 * Override this to return false if the test doesn't want to create an AwBro
wserContext | |
| 119 * automatically. | |
| 120 */ | |
| 121 protected boolean needsAwBrowserContextCreated() { | |
| 122 return true; | 103 return true; |
| 123 } | 104 } |
| 124 | 105 |
| 125 /** | 106 boolean needsBrowserProcessStarted() { |
| 126 * Override this to return false if the test doesn't want the browser startu
p sequence to | |
| 127 * be run automatically. | |
| 128 * @return Whether the instrumentation test requires the browser process to
already be started. | |
| 129 */ | |
| 130 protected boolean needsBrowserProcessStarted() { | |
| 131 return true; | 107 return true; |
| 132 } | 108 } |
| 133 | 109 |
| 134 /** | 110 public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable) throws Exce
ption { |
| 135 * Runs a {@link Callable} on the main thread, blocking until it is | |
| 136 * complete, and returns the result. Calls | |
| 137 * {@link Instrumentation#waitForIdleSync()} first to help avoid certain | |
| 138 * race conditions. | |
| 139 * | |
| 140 * @param <R> Type of result to return | |
| 141 */ | |
| 142 public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable) | |
| 143 throws Exception { | |
| 144 FutureTask<R> task = new FutureTask<R>(callable); | 111 FutureTask<R> task = new FutureTask<R>(callable); |
| 145 getInstrumentation().runOnMainSync(task); | 112 mCallback.getInstrumentation().runOnMainSync(task); |
| 146 return task.get(); | 113 return task.get(); |
| 147 } | 114 } |
| 148 | 115 |
| 149 public void enableJavaScriptOnUiThread(final AwContents awContents) { | 116 public void enableJavaScriptOnUiThread(final AwContents awContents) { |
| 150 getInstrumentation().runOnMainSync(new Runnable() { | 117 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 151 @Override | 118 @Override |
| 152 public void run() { | 119 public void run() { |
| 153 awContents.getSettings().setJavaScriptEnabled(true); | 120 awContents.getSettings().setJavaScriptEnabled(true); |
| 154 } | 121 } |
| 155 }); | 122 }); |
| 156 } | 123 } |
| 157 | 124 |
| 158 public void setNetworkAvailableOnUiThread(final AwContents awContents, | 125 public void setNetworkAvailableOnUiThread( |
| 159 final boolean networkUp) { | 126 final AwContents awContents, final boolean networkUp) { |
| 160 getInstrumentation().runOnMainSync(new Runnable() { | 127 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 161 @Override | 128 @Override |
| 162 public void run() { | 129 public void run() { |
| 163 awContents.setNetworkAvailable(networkUp); | 130 awContents.setNetworkAvailable(networkUp); |
| 164 } | 131 } |
| 165 }); | 132 }); |
| 166 } | 133 } |
| 167 | 134 |
| 168 /** | 135 public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 169 * Loads url on the UI thread and blocks until onPageFinished is called. | |
| 170 */ | |
| 171 public void loadUrlSync(final AwContents awContents, | |
| 172 CallbackHelper onPageFinishedHelper, | |
| 173 final String url) throws Exception { | 136 final String url) throws Exception { |
| 174 loadUrlSync(awContents, onPageFinishedHelper, url, null); | 137 loadUrlSync(awContents, onPageFinishedHelper, url, null); |
| 175 } | 138 } |
| 176 | 139 |
| 177 public void loadUrlSync(final AwContents awContents, | 140 public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 178 CallbackHelper onPageFinishedHelper, | 141 final String url, final Map<String, String> extraHeaders) throws Exc
eption { |
| 179 final String url, | |
| 180 final Map<String, String> extraHeaders) throws Exception { | |
| 181 int currentCallCount = onPageFinishedHelper.getCallCount(); | 142 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 182 loadUrlAsync(awContents, url, extraHeaders); | 143 loadUrlAsync(awContents, url, extraHeaders); |
| 183 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 144 onPageFinishedHelper.waitForCallback( |
| 184 TimeUnit.MILLISECONDS); | 145 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 185 } | 146 } |
| 186 | 147 |
| 187 public void loadUrlSyncAndExpectError(final AwContents awContents, | 148 public void loadUrlSyncAndExpectError(final AwContents awContents, |
| 188 CallbackHelper onPageFinishedHelper, | 149 CallbackHelper onPageFinishedHelper, CallbackHelper onReceivedErrorH
elper, |
| 189 CallbackHelper onReceivedErrorHelper, | |
| 190 final String url) throws Exception { | 150 final String url) throws Exception { |
| 191 int onErrorCallCount = onReceivedErrorHelper.getCallCount(); | 151 int onErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 192 int onFinishedCallCount = onPageFinishedHelper.getCallCount(); | 152 int onFinishedCallCount = onPageFinishedHelper.getCallCount(); |
| 193 loadUrlAsync(awContents, url); | 153 loadUrlAsync(awContents, url); |
| 194 onReceivedErrorHelper.waitForCallback(onErrorCallCount, 1, WAIT_TIMEOUT_
MS, | 154 onReceivedErrorHelper.waitForCallback( |
| 195 TimeUnit.MILLISECONDS); | 155 onErrorCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 196 onPageFinishedHelper.waitForCallback(onFinishedCallCount, 1, WAIT_TIMEOU
T_MS, | 156 onPageFinishedHelper.waitForCallback( |
| 197 TimeUnit.MILLISECONDS); | 157 onFinishedCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 198 } | 158 } |
| 199 | 159 |
| 200 /** | 160 public void loadUrlAsync(final AwContents awContents, final String url) thro
ws Exception { |
| 201 * Loads url on the UI thread but does not block. | |
| 202 */ | |
| 203 public void loadUrlAsync(final AwContents awContents, | |
| 204 final String url) throws Exception { | |
| 205 loadUrlAsync(awContents, url, null); | 161 loadUrlAsync(awContents, url, null); |
| 206 } | 162 } |
| 207 | 163 |
| 208 public void loadUrlAsync(final AwContents awContents, | 164 public void loadUrlAsync( |
| 209 final String url, | 165 final AwContents awContents, final String url, final Map<String, Str
ing> extraHeaders) { |
| 210 final Map<String, String> extraHeaders) { | 166 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 211 getInstrumentation().runOnMainSync(new Runnable() { | |
| 212 @Override | 167 @Override |
| 213 public void run() { | 168 public void run() { |
| 214 awContents.loadUrl(url, extraHeaders); | 169 awContents.loadUrl(url, extraHeaders); |
| 215 } | 170 } |
| 216 }); | 171 }); |
| 217 } | 172 } |
| 218 | 173 |
| 219 /** | 174 public void postUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 220 * Posts url on the UI thread and blocks until onPageFinished is called. | 175 final String url, byte[] postData) throws Exception { |
| 221 */ | |
| 222 public void postUrlSync(final AwContents awContents, | |
| 223 CallbackHelper onPageFinishedHelper, final String url, | |
| 224 byte[] postData) throws Exception { | |
| 225 int currentCallCount = onPageFinishedHelper.getCallCount(); | 176 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 226 postUrlAsync(awContents, url, postData); | 177 postUrlAsync(awContents, url, postData); |
| 227 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 178 onPageFinishedHelper.waitForCallback( |
| 228 TimeUnit.MILLISECONDS); | 179 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 229 } | 180 } |
| 230 | 181 |
| 231 /** | 182 public void postUrlAsync(final AwContents awContents, final String url, byte
[] postData) |
| 232 * Loads url on the UI thread but does not block. | 183 throws Exception { |
| 233 */ | |
| 234 public void postUrlAsync(final AwContents awContents, | |
| 235 final String url, byte[] postData) throws Exception { | |
| 236 class PostUrl implements Runnable { | 184 class PostUrl implements Runnable { |
| 237 byte[] mPostData; | 185 byte[] mPostData; |
| 238 public PostUrl(byte[] postData) { | 186 public PostUrl(byte[] postData) { |
| 239 mPostData = postData; | 187 mPostData = postData; |
| 240 } | 188 } |
| 241 @Override | 189 @Override |
| 242 public void run() { | 190 public void run() { |
| 243 awContents.postUrl(url, mPostData); | 191 awContents.postUrl(url, mPostData); |
| 244 } | 192 } |
| 245 } | 193 } |
| 246 getInstrumentation().runOnMainSync(new PostUrl(postData)); | 194 mCallback.getInstrumentation().runOnMainSync(new PostUrl(postData)); |
| 247 } | 195 } |
| 248 | 196 |
| 249 /** | 197 public void loadDataSync(final AwContents awContents, CallbackHelper onPageF
inishedHelper, |
| 250 * Loads data on the UI thread and blocks until onPageFinished is called. | 198 final String data, final String mimeType, final boolean isBase64Enco
ded) |
| 251 */ | 199 throws Exception { |
| 252 public void loadDataSync(final AwContents awContents, | |
| 253 CallbackHelper onPageFinishedHelper, | |
| 254 final String data, final String mimeType, | |
| 255 final boolean isBase64Encoded) throws Exception { | |
| 256 int currentCallCount = onPageFinishedHelper.getCallCount(); | 200 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 257 loadDataAsync(awContents, data, mimeType, isBase64Encoded); | 201 loadDataAsync(awContents, data, mimeType, isBase64Encoded); |
| 258 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 202 onPageFinishedHelper.waitForCallback( |
| 259 TimeUnit.MILLISECONDS); | 203 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 260 } | 204 } |
| 261 | 205 |
| 262 public void loadDataSyncWithCharset(final AwContents awContents, | 206 public void loadDataSyncWithCharset(final AwContents awContents, |
| 263 CallbackHelper onPageFinishedHelper, | 207 CallbackHelper onPageFinishedHelper, final String data, final String
mimeType, |
| 264 final String data, final String mimeType, | 208 final boolean isBase64Encoded, final String charset) throws Exceptio
n { |
| 265 final boolean isBase64Encoded, final String charset) | |
| 266 throws Exception { | |
| 267 int currentCallCount = onPageFinishedHelper.getCallCount(); | 209 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 268 getInstrumentation().runOnMainSync(new Runnable() { | 210 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 269 @Override | 211 @Override |
| 270 public void run() { | 212 public void run() { |
| 271 awContents.loadUrl(LoadUrlParams.createLoadDataParams( | 213 awContents.loadUrl(LoadUrlParams.createLoadDataParams( |
| 272 data, mimeType, isBase64Encoded, charset)); | 214 data, mimeType, isBase64Encoded, charset)); |
| 273 } | 215 } |
| 274 }); | 216 }); |
| 275 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 217 onPageFinishedHelper.waitForCallback( |
| 276 TimeUnit.MILLISECONDS); | 218 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 277 } | 219 } |
| 278 | 220 |
| 279 /** | 221 public void loadDataAsync(final AwContents awContents, final String data, fi
nal String mimeType, |
| 280 * Loads data on the UI thread but does not block. | 222 final boolean isBase64Encoded) throws Exception { |
| 281 */ | 223 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 282 public void loadDataAsync(final AwContents awContents, final String data, | |
| 283 final String mimeType, final boolean isBase64Encoded) | |
| 284 throws Exception { | |
| 285 getInstrumentation().runOnMainSync(new Runnable() { | |
| 286 @Override | 224 @Override |
| 287 public void run() { | 225 public void run() { |
| 288 awContents.loadData(data, mimeType, isBase64Encoded ? "base64" :
null); | 226 awContents.loadData(data, mimeType, isBase64Encoded ? "base64" :
null); |
| 289 } | 227 } |
| 290 }); | 228 }); |
| 291 } | 229 } |
| 292 | 230 |
| 293 public void loadDataWithBaseUrlSync(final AwContents awContents, | 231 public void loadDataWithBaseUrlSync(final AwContents awContents, |
| 294 CallbackHelper onPageFinishedHelper, final String data, final String
mimeType, | 232 CallbackHelper onPageFinishedHelper, final String data, final String
mimeType, |
| 295 final boolean isBase64Encoded, final String baseUrl, | 233 final boolean isBase64Encoded, final String baseUrl, final String hi
storyUrl) |
| 296 final String historyUrl) throws Throwable { | 234 throws Throwable { |
| 297 int currentCallCount = onPageFinishedHelper.getCallCount(); | 235 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 298 loadDataWithBaseUrlAsync(awContents, data, mimeType, isBase64Encoded, ba
seUrl, historyUrl); | 236 loadDataWithBaseUrlAsync(awContents, data, mimeType, isBase64Encoded, ba
seUrl, historyUrl); |
| 299 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 237 onPageFinishedHelper.waitForCallback( |
| 300 TimeUnit.MILLISECONDS); | 238 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 301 } | 239 } |
| 302 | 240 |
| 303 public void loadDataWithBaseUrlAsync(final AwContents awContents, | 241 public void loadDataWithBaseUrlAsync(final AwContents awContents, final Stri
ng data, |
| 304 final String data, final String mimeType, final boolean isBase64Enco
ded, | 242 final String mimeType, final boolean isBase64Encoded, final String b
aseUrl, |
| 305 final String baseUrl, final String historyUrl) throws Throwable { | 243 final String historyUrl) throws Throwable { |
| 306 runTestOnUiThread(new Runnable() { | 244 mCallback.runOnUiThread(new Runnable() { |
| 307 @Override | 245 @Override |
| 308 public void run() { | 246 public void run() { |
| 309 awContents.loadDataWithBaseURL( | 247 awContents.loadDataWithBaseURL( |
| 310 baseUrl, data, mimeType, isBase64Encoded ? "base64" : nu
ll, historyUrl); | 248 baseUrl, data, mimeType, isBase64Encoded ? "base64" : nu
ll, historyUrl); |
| 311 } | 249 } |
| 312 }); | 250 }); |
| 313 } | 251 } |
| 314 | 252 |
| 315 /** | 253 public void reloadSync(final AwContents awContents, CallbackHelper onPageFin
ishedHelper) |
| 316 * Reloads the current page synchronously. | 254 throws Exception { |
| 317 */ | |
| 318 public void reloadSync(final AwContents awContents, | |
| 319 CallbackHelper onPageFinishedHelper) throws Exception { | |
| 320 int currentCallCount = onPageFinishedHelper.getCallCount(); | 255 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 321 getInstrumentation().runOnMainSync(new Runnable() { | 256 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 322 @Override | 257 @Override |
| 323 public void run() { | 258 public void run() { |
| 324 awContents.getNavigationController().reload(true); | 259 awContents.getNavigationController().reload(true); |
| 325 } | 260 } |
| 326 }); | 261 }); |
| 327 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 262 onPageFinishedHelper.waitForCallback( |
| 328 TimeUnit.MILLISECONDS); | 263 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 329 } | 264 } |
| 330 | 265 |
| 331 /** | |
| 332 * Stops loading on the UI thread. | |
| 333 */ | |
| 334 public void stopLoading(final AwContents awContents) { | 266 public void stopLoading(final AwContents awContents) { |
| 335 getInstrumentation().runOnMainSync(new Runnable() { | 267 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 336 @Override | 268 @Override |
| 337 public void run() { | 269 public void run() { |
| 338 awContents.stopLoading(); | 270 awContents.stopLoading(); |
| 339 } | 271 } |
| 340 }); | 272 }); |
| 341 } | 273 } |
| 342 | 274 |
| 343 public void waitForVisualStateCallback(final AwContents awContents) throws E
xception { | 275 public void waitForVisualStateCallback(final AwContents awContents) throws E
xception { |
| 344 final CallbackHelper ch = new CallbackHelper(); | 276 final CallbackHelper ch = new CallbackHelper(); |
| 345 final int chCount = ch.getCallCount(); | 277 final int chCount = ch.getCallCount(); |
| 346 getInstrumentation().runOnMainSync(new Runnable() { | 278 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 347 @Override | 279 @Override |
| 348 public void run() { | 280 public void run() { |
| 349 final long requestId = 666; | 281 final long requestId = 666; |
| 350 awContents.insertVisualStateCallback(requestId, | 282 awContents.insertVisualStateCallback( |
| 351 new AwContents.VisualStateCallback() { | 283 requestId, new AwContents.VisualStateCallback() { |
| 352 @Override | 284 @Override |
| 353 public void onComplete(long id) { | 285 public void onComplete(long id) { |
| 354 assertEquals(requestId, id); | 286 Assert.assertEquals(requestId, id); |
| 355 ch.notifyCalled(); | 287 ch.notifyCalled(); |
| 356 } | 288 } |
| 357 }); | 289 }); |
| 358 } | 290 } |
| 359 }); | 291 }); |
| 360 ch.waitForCallback(chCount); | 292 ch.waitForCallback(chCount); |
| 361 } | 293 } |
| 362 | 294 |
| 363 public void insertVisualStateCallbackOnUIThread(final AwContents awContents, | 295 public void insertVisualStateCallbackOnUIThread(final AwContents awContents, |
| 364 final long requestId, final AwContents.VisualStateCallback callback)
{ | 296 final long requestId, final AwContents.VisualStateCallback callback)
{ |
| 365 getInstrumentation().runOnMainSync(new Runnable() { | 297 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 366 @Override | 298 @Override |
| 367 public void run() { | 299 public void run() { |
| 368 awContents.insertVisualStateCallback(requestId, callback); | 300 awContents.insertVisualStateCallback(requestId, callback); |
| 369 } | 301 } |
| 370 }); | 302 }); |
| 371 } | 303 } |
| 372 | 304 |
| 373 // Waits for the pixel at the center of AwContents to color up into expected
Color. | |
| 374 // Note that this is a stricter condition that waiting for a visual state ca
llback, | |
| 375 // as visual state callback only indicates that *something* has appeared in
WebView. | |
| 376 public void waitForPixelColorAtCenterOfView(final AwContents awContents, | 305 public void waitForPixelColorAtCenterOfView(final AwContents awContents, |
| 377 final AwTestContainerView testContainerView, final int expectedColor
) throws Exception { | 306 final AwTestContainerView testContainerView, final int expectedColor
) throws Exception { |
| 378 pollUiThread(new Callable<Boolean>() { | 307 pollUiThread(new Callable<Boolean>() { |
| 379 @Override | 308 @Override |
| 380 public Boolean call() throws Exception { | 309 public Boolean call() throws Exception { |
| 381 return GraphicsTestUtils.getPixelColorAtCenterOfView(awContents,
testContainerView) | 310 return GraphicsTestUtils.getPixelColorAtCenterOfView(awContents,
testContainerView) |
| 382 == expectedColor; | 311 == expectedColor; |
| 383 } | 312 } |
| 384 }); | 313 }); |
| 385 } | 314 } |
| 386 | 315 |
| 387 /** | 316 TestDependencyFactory createTestDependencyFactory() { |
| 388 * Checks the current test has |clazz| annotation. Note this swallows NoSuch
MethodException | |
| 389 * and returns false in that case. | |
| 390 */ | |
| 391 private boolean testMethodHasAnnotation(Class<? extends Annotation> clazz) { | |
| 392 String testName = getName(); | |
| 393 Method method = null; | |
| 394 try { | |
| 395 method = getClass().getMethod(testName); | |
| 396 } catch (NoSuchMethodException e) { | |
| 397 Log.w(TAG, "Test method name not found.", e); | |
| 398 return false; | |
| 399 } | |
| 400 | |
| 401 // Cast to AnnotatedElement to work around a compilation failure. | |
| 402 // Method.isAnnotationPresent() was removed in Java 8 (which is used by
the Android N SDK), | |
| 403 // so compilation with Java 7 fails. See crbug.com/608792. | |
| 404 return ((AnnotatedElement) method).isAnnotationPresent(clazz); | |
| 405 } | |
| 406 | |
| 407 /** | |
| 408 * Factory class used in creation of test AwContents instances. | |
| 409 * | |
| 410 * Test cases can provide subclass instances to the createAwTest* methods in
order to create an | |
| 411 * AwContents instance with injected test dependencies. | |
| 412 */ | |
| 413 public static class TestDependencyFactory extends AwContents.DependencyFacto
ry { | |
| 414 public AwTestContainerView createAwTestContainerView(AwTestRunnerActivit
y activity, | |
| 415 boolean allowHardwareAcceleration) { | |
| 416 return new AwTestContainerView(activity, allowHardwareAcceleration); | |
| 417 } | |
| 418 public AwSettings createAwSettings(Context context, boolean supportsLega
cyQuirks) { | |
| 419 return new AwSettings(context, false /* isAccessFromFileURLsGrantedB
yDefault */, | |
| 420 supportsLegacyQuirks, false /* allowEmptyDocumentPersistence
*/, | |
| 421 true /* allowGeolocationOnInsecureOrigins */, | |
| 422 false /* doNotUpdateSelectionOnMutatingSelectionRange */); | |
| 423 } | |
| 424 | |
| 425 public AwContents createAwContents(AwBrowserContext browserContext, View
Group containerView, | |
| 426 Context context, InternalAccessDelegate internalAccessAdapter, | |
| 427 NativeDrawGLFunctorFactory nativeDrawGLFunctorFactory, | |
| 428 AwContentsClient contentsClient, AwSettings settings, | |
| 429 DependencyFactory dependencyFactory) { | |
| 430 return new AwContents(browserContext, containerView, context, intern
alAccessAdapter, | |
| 431 nativeDrawGLFunctorFactory, contentsClient, settings, depend
encyFactory); | |
| 432 } | |
| 433 } | |
| 434 | |
| 435 protected TestDependencyFactory createTestDependencyFactory() { | |
| 436 return new TestDependencyFactory(); | 317 return new TestDependencyFactory(); |
| 437 } | 318 } |
| 438 | 319 |
| 439 public AwTestContainerView createAwTestContainerView( | 320 public AwTestContainerView createAwTestContainerView(final AwContentsClient
awContentsClient) { |
| 440 final AwContentsClient awContentsClient) { | |
| 441 return createAwTestContainerView(awContentsClient, false, null); | 321 return createAwTestContainerView(awContentsClient, false, null); |
| 442 } | 322 } |
| 443 | 323 |
| 444 public AwTestContainerView createAwTestContainerView(final AwContentsClient
awContentsClient, | 324 public AwTestContainerView createAwTestContainerView(final AwContentsClient
awContentsClient, |
| 445 boolean supportsLegacyQuirks, final TestDependencyFactory testDepend
encyFactory) { | 325 boolean supportsLegacyQuirks, final TestDependencyFactory testDepend
encyFactory) { |
| 446 AwTestContainerView testContainerView = createDetachedAwTestContainerVie
w( | 326 AwTestContainerView testContainerView = createDetachedAwTestContainerVie
w( |
| 447 awContentsClient, supportsLegacyQuirks, testDependencyFactory); | 327 awContentsClient, supportsLegacyQuirks, testDependencyFactory); |
| 448 getActivity().addView(testContainerView); | 328 mCallback.getActivity().addView(testContainerView); |
| 449 testContainerView.requestFocus(); | 329 testContainerView.requestFocus(); |
| 450 return testContainerView; | 330 return testContainerView; |
| 451 } | 331 } |
| 452 | 332 |
| 453 public AwBrowserContext getAwBrowserContext() { | 333 public AwBrowserContext getAwBrowserContext() { |
| 454 return mBrowserContext; | 334 return mBrowserContext; |
| 455 } | 335 } |
| 456 | 336 |
| 457 public AwTestContainerView createDetachedAwTestContainerView( | 337 public AwTestContainerView createDetachedAwTestContainerView( |
| 458 final AwContentsClient awContentsClient) { | 338 final AwContentsClient awContentsClient) { |
| 459 return createDetachedAwTestContainerView(awContentsClient, false, null); | 339 return createDetachedAwTestContainerView(awContentsClient, false, null); |
| 460 } | 340 } |
| 461 | 341 |
| 462 public AwTestContainerView createDetachedAwTestContainerView( | 342 public AwTestContainerView createDetachedAwTestContainerView( |
| 463 final AwContentsClient awContentsClient, boolean supportsLegacyQuirk
s, | 343 final AwContentsClient awContentsClient, boolean supportsLegacyQuirk
s, |
| 464 TestDependencyFactory testDependencyFactory) { | 344 TestDependencyFactory testDependencyFactory) { |
| 465 if (testDependencyFactory == null) { | 345 if (testDependencyFactory == null) { |
| 466 testDependencyFactory = createTestDependencyFactory(); | 346 testDependencyFactory = createTestDependencyFactory(); |
| 467 } | 347 } |
| 468 boolean allowHardwareAcceleration = isHardwareAcceleratedTest(); | 348 boolean allowHardwareAcceleration = isHardwareAcceleratedTest(); |
| 469 final AwTestContainerView testContainerView = | 349 final AwTestContainerView testContainerView = |
| 470 testDependencyFactory.createAwTestContainerView( | 350 testDependencyFactory.createAwTestContainerView( |
| 471 getActivity(), allowHardwareAcceleration); | 351 mCallback.getActivity(), allowHardwareAcceleration); |
| 472 | 352 |
| 473 AwSettings awSettings = | 353 AwSettings awSettings = testDependencyFactory.createAwSettings( |
| 474 testDependencyFactory.createAwSettings(getActivity(), supportsLe
gacyQuirks); | 354 mCallback.getActivity(), supportsLegacyQuirks); |
| 475 AwContents awContents = testDependencyFactory.createAwContents(mBrowserC
ontext, | 355 AwContents awContents = testDependencyFactory.createAwContents(mBrowserC
ontext, |
| 476 testContainerView, testContainerView.getContext(), | 356 testContainerView, testContainerView.getContext(), |
| 477 testContainerView.getInternalAccessDelegate(), | 357 testContainerView.getInternalAccessDelegate(), |
| 478 testContainerView.getNativeDrawGLFunctorFactory(), awContentsCli
ent, awSettings, | 358 testContainerView.getNativeDrawGLFunctorFactory(), awContentsCli
ent, awSettings, |
| 479 testDependencyFactory); | 359 testDependencyFactory); |
| 480 testContainerView.initialize(awContents); | 360 testContainerView.initialize(awContents); |
| 481 return testContainerView; | 361 return testContainerView; |
| 482 } | 362 } |
| 483 | 363 |
| 484 protected boolean isHardwareAcceleratedTest() { | 364 boolean isHardwareAcceleratedTest() { |
| 485 return !testMethodHasAnnotation(DisableHardwareAccelerationForTest.class
); | 365 return !mCallback.testMethodHasAnnotation(DisableHardwareAccelerationFor
Test.class); |
| 486 } | 366 } |
| 487 | 367 |
| 488 public AwTestContainerView createAwTestContainerViewOnMainSync( | 368 public AwTestContainerView createAwTestContainerViewOnMainSync(final AwConte
ntsClient client) |
| 489 final AwContentsClient client) throws Exception { | 369 throws Exception { |
| 490 return createAwTestContainerViewOnMainSync(client, false, null); | 370 return createAwTestContainerViewOnMainSync(client, false, null); |
| 491 } | 371 } |
| 492 | 372 |
| 493 public AwTestContainerView createAwTestContainerViewOnMainSync( | 373 public AwTestContainerView createAwTestContainerViewOnMainSync( |
| 494 final AwContentsClient client, final boolean supportsLegacyQuirks) { | 374 final AwContentsClient client, final boolean supportsLegacyQuirks) { |
| 495 return createAwTestContainerViewOnMainSync(client, supportsLegacyQuirks,
null); | 375 return createAwTestContainerViewOnMainSync(client, supportsLegacyQuirks,
null); |
| 496 } | 376 } |
| 497 | 377 |
| 498 public AwTestContainerView createAwTestContainerViewOnMainSync(final AwConte
ntsClient client, | 378 public AwTestContainerView createAwTestContainerViewOnMainSync(final AwConte
ntsClient client, |
| 499 final boolean supportsLegacyQuirks, final TestDependencyFactory test
DependencyFactory) { | 379 final boolean supportsLegacyQuirks, final TestDependencyFactory test
DependencyFactory) { |
| 500 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<AwTestC
ontainerView>() { | 380 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<AwTestC
ontainerView>() { |
| 501 @Override | 381 @Override |
| 502 public AwTestContainerView call() { | 382 public AwTestContainerView call() { |
| 503 return createAwTestContainerView( | 383 return createAwTestContainerView( |
| 504 client, supportsLegacyQuirks, testDependencyFactory); | 384 client, supportsLegacyQuirks, testDependencyFactory); |
| 505 } | 385 } |
| 506 }); | 386 }); |
| 507 } | 387 } |
| 508 | 388 |
| 509 public void destroyAwContentsOnMainSync(final AwContents awContents) { | 389 public void destroyAwContentsOnMainSync(final AwContents awContents) { |
| 510 if (awContents == null) return; | 390 if (awContents == null) return; |
| 511 getInstrumentation().runOnMainSync(new Runnable() { | 391 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 512 @Override | 392 @Override |
| 513 public void run() { | 393 public void run() { |
| 514 awContents.destroy(); | 394 awContents.destroy(); |
| 515 } | 395 } |
| 516 }); | 396 }); |
| 517 } | 397 } |
| 518 | 398 |
| 519 public String getTitleOnUiThread(final AwContents awContents) throws Excepti
on { | 399 public String getTitleOnUiThread(final AwContents awContents) throws Excepti
on { |
| 520 return runTestOnUiThreadAndGetResult(new Callable<String>() { | 400 return runTestOnUiThreadAndGetResult(new Callable<String>() { |
| 521 @Override | 401 @Override |
| 522 public String call() throws Exception { | 402 public String call() throws Exception { |
| 523 return awContents.getTitle(); | 403 return awContents.getTitle(); |
| 524 } | 404 } |
| 525 }); | 405 }); |
| 526 } | 406 } |
| 527 | 407 |
| 528 public AwSettings getAwSettingsOnUiThread( | 408 public AwSettings getAwSettingsOnUiThread(final AwContents awContents) throw
s Exception { |
| 529 final AwContents awContents) throws Exception { | |
| 530 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { | 409 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { |
| 531 @Override | 410 @Override |
| 532 public AwSettings call() throws Exception { | 411 public AwSettings call() throws Exception { |
| 533 return awContents.getSettings(); | 412 return awContents.getSettings(); |
| 534 } | 413 } |
| 535 }); | 414 }); |
| 536 } | 415 } |
| 537 | 416 |
| 538 /** | 417 String maybeStripDoubleQuotes(String raw) { |
| 539 * Verify double quotes in both sides of the raw string. Strip the double qu
otes and | 418 Assert.assertNotNull(raw); |
| 540 * returns rest of the string. | |
| 541 */ | |
| 542 protected String maybeStripDoubleQuotes(String raw) { | |
| 543 assertNotNull(raw); | |
| 544 Matcher m = MAYBE_QUOTED_STRING.matcher(raw); | 419 Matcher m = MAYBE_QUOTED_STRING.matcher(raw); |
| 545 assertTrue(m.matches()); | 420 Assert.assertTrue(m.matches()); |
| 546 return m.group(2); | 421 return m.group(2); |
| 547 } | 422 } |
| 548 | 423 |
| 549 /** | |
| 550 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the | |
| 551 * result of its execution in JSON format. | |
| 552 */ | |
| 553 public String executeJavaScriptAndWaitForResult(final AwContents awContents, | 424 public String executeJavaScriptAndWaitForResult(final AwContents awContents, |
| 554 TestAwContentsClient viewClient, final String code) throws Exception
{ | 425 TestAwContentsClient viewClient, final String code) throws Exception
{ |
| 555 return JSUtils.executeJavaScriptAndWaitForResult(this, awContents, | 426 return JSUtils.executeJavaScriptAndWaitForResult(mCallback.getInstrument
ation(), awContents, |
| 556 viewClient.getOnEvaluateJavaScriptResultHelper(), | 427 viewClient.getOnEvaluateJavaScriptResultHelper(), code); |
| 557 code); | |
| 558 } | 428 } |
| 559 | 429 |
| 560 /** | 430 /** |
| 561 * Executes JavaScript code within the given ContentView to get the text con
tent in | 431 * Executes JavaScript code within the given ContentView to get the text con
tent in |
| 562 * document body. Returns the result string without double quotes. | 432 * document body. Returns the result string without double quotes. |
| 563 */ | 433 */ |
| 564 protected String getJavaScriptResultBodyTextContent( | 434 String getJavaScriptResultBodyTextContent( |
| 565 final AwContents awContents, final TestAwContentsClient viewClient)
throws Exception { | 435 final AwContents awContents, final TestAwContentsClient viewClient)
throws Exception { |
| 566 String raw = executeJavaScriptAndWaitForResult( | 436 String raw = executeJavaScriptAndWaitForResult( |
| 567 awContents, viewClient, "document.body.textContent"); | 437 awContents, viewClient, "document.body.textContent"); |
| 568 return maybeStripDoubleQuotes(raw); | 438 return maybeStripDoubleQuotes(raw); |
| 569 } | 439 } |
| 570 | 440 |
| 571 /** | |
| 572 * Wrapper around CriteriaHelper.pollInstrumentationThread. This uses AwTest
Base-specifc | |
| 573 * timeouts and treats timeouts and exceptions as test failures automaticall
y. | |
| 574 */ | |
| 575 public static void pollInstrumentationThread(final Callable<Boolean> callabl
e) | 441 public static void pollInstrumentationThread(final Callable<Boolean> callabl
e) |
| 576 throws Exception { | 442 throws Exception { |
| 577 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 443 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
| 578 @Override | 444 @Override |
| 579 public boolean isSatisfied() { | 445 public boolean isSatisfied() { |
| 580 try { | 446 try { |
| 581 return callable.call(); | 447 return callable.call(); |
| 582 } catch (Throwable e) { | 448 } catch (Throwable e) { |
| 583 Log.e(TAG, "Exception while polling.", e); | 449 Log.e(TAG, "Exception while polling.", e); |
| 584 return false; | 450 return false; |
| 585 } | 451 } |
| 586 } | 452 } |
| 587 }, WAIT_TIMEOUT_MS, CHECK_INTERVAL); | 453 }, WAIT_TIMEOUT_MS, CHECK_INTERVAL); |
| 588 } | 454 } |
| 589 | 455 |
| 590 /** | |
| 591 * Wrapper around {@link AwTestBase#poll()} but runs the callable on the UI
thread. | |
| 592 */ | |
| 593 public void pollUiThread(final Callable<Boolean> callable) throws Exception
{ | 456 public void pollUiThread(final Callable<Boolean> callable) throws Exception
{ |
| 594 pollInstrumentationThread(new Callable<Boolean>() { | 457 pollInstrumentationThread(new Callable<Boolean>() { |
| 595 @Override | 458 @Override |
| 596 public Boolean call() throws Exception { | 459 public Boolean call() throws Exception { |
| 597 return runTestOnUiThreadAndGetResult(callable); | 460 return runTestOnUiThreadAndGetResult(callable); |
| 598 } | 461 } |
| 599 }); | 462 }); |
| 600 } | 463 } |
| 601 | 464 |
| 602 /** | 465 public void clearCacheOnUiThread(final AwContents awContents, final boolean
includeDiskFiles) |
| 603 * Clears the resource cache. Note that the cache is per-application, so thi
s will clear the | 466 throws Exception { |
| 604 * cache for all WebViews used. | 467 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 605 */ | |
| 606 public void clearCacheOnUiThread( | |
| 607 final AwContents awContents, | |
| 608 final boolean includeDiskFiles) throws Exception { | |
| 609 getInstrumentation().runOnMainSync(new Runnable() { | |
| 610 @Override | 468 @Override |
| 611 public void run() { | 469 public void run() { |
| 612 awContents.clearCache(includeDiskFiles); | 470 awContents.clearCache(includeDiskFiles); |
| 613 } | 471 } |
| 614 }); | 472 }); |
| 615 } | 473 } |
| 616 | 474 |
| 617 /** | |
| 618 * Returns pure page scale. | |
| 619 */ | |
| 620 public float getScaleOnUiThread(final AwContents awContents) throws Exceptio
n { | 475 public float getScaleOnUiThread(final AwContents awContents) throws Exceptio
n { |
| 621 return runTestOnUiThreadAndGetResult(new Callable<Float>() { | 476 return runTestOnUiThreadAndGetResult(new Callable<Float>() { |
| 622 @Override | 477 @Override |
| 623 public Float call() throws Exception { | 478 public Float call() throws Exception { |
| 624 return awContents.getPageScaleFactor(); | 479 return awContents.getPageScaleFactor(); |
| 625 } | 480 } |
| 626 }); | 481 }); |
| 627 } | 482 } |
| 628 | 483 |
| 629 /** | |
| 630 * Returns page scale multiplied by the screen density. | |
| 631 */ | |
| 632 public float getPixelScaleOnUiThread(final AwContents awContents) throws Exc
eption { | 484 public float getPixelScaleOnUiThread(final AwContents awContents) throws Exc
eption { |
| 633 return runTestOnUiThreadAndGetResult(new Callable<Float>() { | 485 return runTestOnUiThreadAndGetResult(new Callable<Float>() { |
| 634 @Override | 486 @Override |
| 635 public Float call() throws Exception { | 487 public Float call() throws Exception { |
| 636 return awContents.getScale(); | 488 return awContents.getScale(); |
| 637 } | 489 } |
| 638 }); | 490 }); |
| 639 } | 491 } |
| 640 | 492 |
| 641 /** | |
| 642 * Returns whether a user can zoom the page in. | |
| 643 */ | |
| 644 public boolean canZoomInOnUiThread(final AwContents awContents) throws Excep
tion { | 493 public boolean canZoomInOnUiThread(final AwContents awContents) throws Excep
tion { |
| 645 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { | 494 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
| 646 @Override | 495 @Override |
| 647 public Boolean call() throws Exception { | 496 public Boolean call() throws Exception { |
| 648 return awContents.canZoomIn(); | 497 return awContents.canZoomIn(); |
| 649 } | 498 } |
| 650 }); | 499 }); |
| 651 } | 500 } |
| 652 | 501 |
| 653 /** | |
| 654 * Returns whether a user can zoom the page out. | |
| 655 */ | |
| 656 public boolean canZoomOutOnUiThread(final AwContents awContents) throws Exce
ption { | 502 public boolean canZoomOutOnUiThread(final AwContents awContents) throws Exce
ption { |
| 657 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { | 503 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
| 658 @Override | 504 @Override |
| 659 public Boolean call() throws Exception { | 505 public Boolean call() throws Exception { |
| 660 return awContents.canZoomOut(); | 506 return awContents.canZoomOut(); |
| 661 } | 507 } |
| 662 }); | 508 }); |
| 663 } | 509 } |
| 664 | 510 |
| 665 public void killRenderProcessOnUiThreadAsync(final AwContents awContents) th
rows Exception { | 511 public void killRenderProcessOnUiThreadAsync(final AwContents awContents) th
rows Exception { |
| 666 getInstrumentation().runOnMainSync(new Runnable() { | 512 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 667 @Override | 513 @Override |
| 668 public void run() { | 514 public void run() { |
| 669 awContents.killRenderProcess(); | 515 awContents.killRenderProcess(); |
| 670 } | 516 } |
| 671 }); | 517 }); |
| 672 } | 518 } |
| 673 | 519 |
| 674 /** | |
| 675 * Loads the main html then triggers the popup window. | |
| 676 */ | |
| 677 public void triggerPopup(final AwContents parentAwContents, | 520 public void triggerPopup(final AwContents parentAwContents, |
| 678 TestAwContentsClient parentAwContentsClient, TestWebServer testWebSe
rver, | 521 TestAwContentsClient parentAwContentsClient, TestWebServer testWebSe
rver, |
| 679 String mainHtml, String popupHtml, String popupPath, String triggerS
cript) | 522 String mainHtml, String popupHtml, String popupPath, String triggerS
cript) |
| 680 throws Exception { | 523 throws Exception { |
| 681 enableJavaScriptOnUiThread(parentAwContents); | 524 enableJavaScriptOnUiThread(parentAwContents); |
| 682 getInstrumentation().runOnMainSync(new Runnable() { | 525 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 683 @Override | 526 @Override |
| 684 public void run() { | 527 public void run() { |
| 685 parentAwContents.getSettings().setSupportMultipleWindows(true); | 528 parentAwContents.getSettings().setSupportMultipleWindows(true); |
| 686 parentAwContents.getSettings().setJavaScriptCanOpenWindowsAutoma
tically(true); | 529 parentAwContents.getSettings().setJavaScriptCanOpenWindowsAutoma
tically(true); |
| 687 } | 530 } |
| 688 }); | 531 }); |
| 689 | 532 |
| 690 final String parentUrl = testWebServer.setResponse("/popupParent.html",
mainHtml, null); | 533 final String parentUrl = testWebServer.setResponse("/popupParent.html",
mainHtml, null); |
| 691 if (popupHtml != null) { | 534 if (popupHtml != null) { |
| 692 testWebServer.setResponse(popupPath, popupHtml, null); | 535 testWebServer.setResponse(popupPath, popupHtml, null); |
| 693 } else { | 536 } else { |
| 694 testWebServer.setResponseWithNoContentStatus(popupPath); | 537 testWebServer.setResponseWithNoContentStatus(popupPath); |
| 695 } | 538 } |
| 696 | 539 |
| 697 parentAwContentsClient.getOnCreateWindowHelper().setReturnValue(true); | 540 parentAwContentsClient.getOnCreateWindowHelper().setReturnValue(true); |
| 698 loadUrlSync(parentAwContents, parentAwContentsClient.getOnPageFinishedHe
lper(), parentUrl); | 541 loadUrlSync(parentAwContents, parentAwContentsClient.getOnPageFinishedHe
lper(), parentUrl); |
| 699 | 542 |
| 700 TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper = | 543 TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper = |
| 701 parentAwContentsClient.getOnCreateWindowHelper(); | 544 parentAwContentsClient.getOnCreateWindowHelper(); |
| 702 int currentCallCount = onCreateWindowHelper.getCallCount(); | 545 int currentCallCount = onCreateWindowHelper.getCallCount(); |
| 703 parentAwContents.evaluateJavaScriptForTests(triggerScript, null); | 546 parentAwContents.evaluateJavaScriptForTests(triggerScript, null); |
| 704 onCreateWindowHelper.waitForCallback( | 547 onCreateWindowHelper.waitForCallback( |
| 705 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); | 548 currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 706 } | 549 } |
| 707 | 550 |
| 708 /** | |
| 709 * POD object for holding references to helper objects of a popup window. | |
| 710 */ | |
| 711 public static class PopupInfo { | |
| 712 public final TestAwContentsClient popupContentsClient; | |
| 713 public final AwTestContainerView popupContainerView; | |
| 714 public final AwContents popupContents; | |
| 715 public PopupInfo(TestAwContentsClient popupContentsClient, | |
| 716 AwTestContainerView popupContainerView, AwContents popupContents
) { | |
| 717 this.popupContentsClient = popupContentsClient; | |
| 718 this.popupContainerView = popupContainerView; | |
| 719 this.popupContents = popupContents; | |
| 720 } | |
| 721 } | |
| 722 | |
| 723 /** | |
| 724 * Supplies the popup window with AwContents then waits for the popup window
to finish loading. | |
| 725 */ | |
| 726 public PopupInfo connectPendingPopup(final AwContents parentAwContents) thro
ws Exception { | 551 public PopupInfo connectPendingPopup(final AwContents parentAwContents) thro
ws Exception { |
| 727 TestAwContentsClient popupContentsClient; | 552 TestAwContentsClient popupContentsClient; |
| 728 AwTestContainerView popupContainerView; | 553 AwTestContainerView popupContainerView; |
| 729 final AwContents popupContents; | 554 final AwContents popupContents; |
| 730 popupContentsClient = new TestAwContentsClient(); | 555 popupContentsClient = new TestAwContentsClient(); |
| 731 popupContainerView = createAwTestContainerViewOnMainSync(popupContentsCl
ient); | 556 popupContainerView = createAwTestContainerViewOnMainSync(popupContentsCl
ient); |
| 732 popupContents = popupContainerView.getAwContents(); | 557 popupContents = popupContainerView.getAwContents(); |
| 733 enableJavaScriptOnUiThread(popupContents); | 558 enableJavaScriptOnUiThread(popupContents); |
| 734 | 559 |
| 735 getInstrumentation().runOnMainSync(new Runnable() { | 560 mCallback.getInstrumentation().runOnMainSync(new Runnable() { |
| 736 @Override | 561 @Override |
| 737 public void run() { | 562 public void run() { |
| 738 parentAwContents.supplyContentsForPopup(popupContents); | 563 parentAwContents.supplyContentsForPopup(popupContents); |
| 739 } | 564 } |
| 740 }); | 565 }); |
| 741 | 566 |
| 742 OnPageFinishedHelper onPageFinishedHelper = popupContentsClient.getOnPag
eFinishedHelper(); | 567 OnPageFinishedHelper onPageFinishedHelper = popupContentsClient.getOnPag
eFinishedHelper(); |
| 743 int finishCallCount = onPageFinishedHelper.getCallCount(); | 568 int finishCallCount = onPageFinishedHelper.getCallCount(); |
| 744 TestAwContentsClient.OnReceivedTitleHelper onReceivedTitleHelper = | 569 TestAwContentsClient.OnReceivedTitleHelper onReceivedTitleHelper = |
| 745 popupContentsClient.getOnReceivedTitleHelper(); | 570 popupContentsClient.getOnReceivedTitleHelper(); |
| 746 int titleCallCount = onReceivedTitleHelper.getCallCount(); | 571 int titleCallCount = onReceivedTitleHelper.getCallCount(); |
| 747 | 572 |
| 748 onPageFinishedHelper.waitForCallback(finishCallCount, 1, WAIT_TIMEOUT_MS
, | 573 onPageFinishedHelper.waitForCallback( |
| 749 TimeUnit.MILLISECONDS); | 574 finishCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 750 onReceivedTitleHelper.waitForCallback(titleCallCount, 1, WAIT_TIMEOUT_MS
, | 575 onReceivedTitleHelper.waitForCallback( |
| 751 TimeUnit.MILLISECONDS); | 576 titleCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 752 | 577 |
| 753 return new PopupInfo(popupContentsClient, popupContainerView, popupConte
nts); | 578 return new PopupInfo(popupContentsClient, popupContainerView, popupConte
nts); |
| 754 } | 579 } |
| 580 |
| 581 public interface AwTestCommonCallback { |
| 582 Instrumentation getInstrumentation(); |
| 583 AwTestRunnerActivity getActivity(); |
| 584 void runOnUiThread(Runnable runnable) throws Throwable; |
| 585 boolean testMethodHasAnnotation(Class<? extends Annotation> clazz); |
| 586 } |
| 755 } | 587 } |
| OLD | NEW |