1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 package org.xwalk.core.xwview.test;
8 import android.app.Activity;
9 import android.content.Context;
10 import android.test.ActivityInstrumentationTestCase2;
11 import android.util.Log;
12 import android.webkit.WebResourceResponse;
14 import java.io.InputStream;
15 import java.io.IOException;
16 import java.util.concurrent.atomic.AtomicReference;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.FutureTask;
19 import java.util.concurrent.TimeUnit;
21 import junit.framework.Assert;
23 import org.chromium.content.browser.LoadUrlParams;
24 import org.chromium.content.browser.test.util.CallbackHelper;
25 import org.chromium.content.browser.test.util.Criteria;
26 import org.chromium.content.browser.test.util.CriteriaHelper;
27 import org.xwalk.core.XWalkClient;
28 import org.xwalk.core.XWalkNavigationHistory;
29 import org.xwalk.core.XWalkResourceClient;
30 import org.xwalk.core.XWalkSettings;
31 import org.xwalk.core.XWalkView;
32 import org.xwalk.core.XWalkWebChromeClient;
34 public class XWalkViewTestBase
35 extends ActivityInstrumentationTestCase2<XWalkViewTestRunnerActivity> {
36 protected final static int WAIT_TIMEOUT_SECONDS = 15;
37 private final static String TAG = "XWalkViewTestBase";
38 private XWalkView mXWalkView;
39 final TestHelperBridge mTestHelperBridge = new TestHelperBridge();
41 class TestXWalkClientBase extends XWalkClient {
42 TestHelperBridge mInnerContentsClient;
43 public TestXWalkClientBase(TestHelperBridge client) {
44 super(getXWalkView());
45 mInnerContentsClient = client;
49 public void onPageStarted(XWalkView view, String url) {
50 mInnerContentsClient.onPageStarted(url);
54 public void onPageFinished(XWalkView view, String url) {
55 mInnerContentsClient.onPageFinished(url);
59 class TestXWalkClient extends TestXWalkClientBase {
60 public TestXWalkClient() {
61 super(mTestHelperBridge);
65 class TestXWalkWebChromeClientBase extends XWalkWebChromeClient {
66 TestHelperBridge mInnerContentsClient;
67 public TestXWalkWebChromeClientBase(TestHelperBridge client) {
68 super(getXWalkView());
69 mInnerContentsClient = client;
73 public void onReceivedTitle(XWalkView view, String title) {
74 mInnerContentsClient.onTitleChanged(title);
78 class TestXWalkWebChromeClient extends TestXWalkWebChromeClientBase {
79 public TestXWalkWebChromeClient() {
80 super(mTestHelperBridge);
84 class TestXWalkResourceClientBase extends XWalkResourceClient {
85 TestHelperBridge mInnerContentsClient;
86 public TestXWalkResourceClientBase(TestHelperBridge client) {
88 mInnerContentsClient = client;
92 public void onLoadStarted(XWalkView view, String url) {
93 mTestHelperBridge.onLoadStarted(url);
97 public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
98 mTestHelperBridge.onReceivedLoadError(errorCode, description, failingUrl);
102 public WebResourceResponse shouldInterceptLoadRequest(XWalkView view,
104 return mTestHelperBridge.shouldInterceptLoadRequest(url);
108 class TestXWalkResourceClient extends TestXWalkResourceClientBase {
109 public TestXWalkResourceClient() {
110 super(mTestHelperBridge);
114 void setXWalkClient(final XWalkClient client) {
115 getInstrumentation().runOnMainSync(new Runnable() {
118 getXWalkView().setXWalkClient(client);
123 void setXWalkWebChromeClient(final XWalkWebChromeClient client) {
124 getInstrumentation().runOnMainSync(new Runnable() {
127 getXWalkView().setXWalkWebChromeClient(client);
132 void setResourceClient(final XWalkResourceClient client) {
133 getInstrumentation().runOnMainSync(new Runnable() {
136 getXWalkView().setResourceClient(client);
141 static class ViewPair {
142 private final XWalkView content0;
143 private final TestHelperBridge client0;
144 private final XWalkView content1;
145 private final TestHelperBridge client1;
147 ViewPair(XWalkView content0, TestHelperBridge client0,
148 XWalkView content1, TestHelperBridge client1) {
149 this.content0 = content0;
150 this.client0 = client0;
151 this.content1 = content1;
152 this.client1 = client1;
155 XWalkView getContent0() {
159 TestHelperBridge getClient0() {
163 XWalkView getContent1() {
167 TestHelperBridge getClient1() {
172 public XWalkViewTestBase() {
173 super(XWalkViewTestRunnerActivity.class);
177 protected void setUp() throws Exception {
180 // Must call getActivity() here but not in main thread.
181 final Activity activity = getActivity();
182 getInstrumentation().runOnMainSync(new Runnable() {
185 mXWalkView = new XWalkView(activity, activity);
186 getActivity().addView(mXWalkView);
187 // mXWalkView.getXWalkViewContentForTest().installWebContentsObserverForTest(mTestHelperBridge);
192 protected boolean pollOnUiThread(final Callable<Boolean> callable) throws Exception {
193 return CriteriaHelper.pollForCriteria(new Criteria() {
195 public boolean isSatisfied() {
197 return runTestOnUiThreadAndGetResult(callable);
198 } catch (Throwable e) {
205 protected void loadJavaScriptUrl(final String url) throws Exception {
206 if (!url.startsWith("javascript:")) {
207 Log.w(TAG, "loadJavascriptUrl only accepts javascript: url");
213 protected void loadUrlSync(final String url) throws Exception {
214 CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
215 int currentCallCount = pageFinishedHelper.getCallCount();
218 pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
222 protected void loadUrlAsync(final String url) throws Exception {
223 getInstrumentation().runOnMainSync(new Runnable() {
226 mXWalkView.load(url, null);
231 protected void loadDataSync(final String url, final String data, final String mimeType,
232 final boolean isBase64Encoded) throws Exception {
233 CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
234 int currentCallCount = pageFinishedHelper.getCallCount();
235 loadDataAsync(url, data, mimeType, isBase64Encoded);
236 pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
240 protected void loadDataAsync(final String url, final String data, final String mimeType,
241 final boolean isBase64Encoded) throws Exception {
242 getInstrumentation().runOnMainSync(new Runnable() {
245 mXWalkView.load(url, data);
250 protected void loadUrlSyncByContent(final XWalkView xWalkContent,
251 final TestHelperBridge contentsClient,
252 final String url) throws Exception {
253 CallbackHelper pageFinishedHelper = contentsClient.getOnPageFinishedHelper();
254 int currentCallCount = pageFinishedHelper.getCallCount();
255 loadUrlAsyncByContent(xWalkContent, url);
257 pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
261 protected void loadUrlAsyncByContent(final XWalkView xWalkContent,
262 final String url) throws Exception {
263 getInstrumentation().runOnMainSync(new Runnable() {
266 xWalkContent.load(url, null);
271 protected String getTitleOnUiThread() throws Exception {
272 return runTestOnUiThreadAndGetResult(new Callable<String>() {
274 public String call() throws Exception {
275 return mXWalkView.getTitle();
280 protected <R> R runTestOnUiThreadAndGetResult(Callable<R> callable)
282 FutureTask<R> task = new FutureTask<R>(callable);
283 getInstrumentation().waitForIdleSync();
284 getInstrumentation().runOnMainSync(task);
288 protected String getFileContent(String fileName) {
290 Context context = getInstrumentation().getContext();
291 InputStream inputStream = context.getAssets().open(fileName);
292 int size = inputStream.available();
293 byte buffer[] = new byte[size];
294 inputStream.read(buffer);
297 String fileContent = new String(buffer);
299 } catch (IOException e) {
300 throw new RuntimeException(e);
304 protected String getTitleOnUiThreadByContent(final XWalkView xWalkContent) throws Exception {
305 return runTestOnUiThreadAndGetResult(new Callable<String>() {
307 public String call() throws Exception {
308 String title = xWalkContent.getTitle();
314 protected XWalkSettings getXWalkSettingsOnUiThreadByContent(
315 final XWalkView xwalkContent) throws Exception {
316 return runTestOnUiThreadAndGetResult(new Callable<XWalkSettings>() {
318 public XWalkSettings call() throws Exception {
319 return xwalkContent.getSettings();
324 protected XWalkView createXWalkViewContainerOnMainSync(
325 final Context context,
326 final XWalkClient client,
327 final XWalkResourceClient resourceClient,
328 final XWalkWebChromeClient webChromeClient) throws Exception {
329 final AtomicReference<XWalkView> xWalkViewContainer =
330 new AtomicReference<XWalkView>();
331 getInstrumentation().runOnMainSync(new Runnable() {
334 xWalkViewContainer.set(new XWalkView(context, getActivity()));
335 getActivity().addView(xWalkViewContainer.get());
336 xWalkViewContainer.get().setXWalkClient(client);
337 xWalkViewContainer.get().setResourceClient(resourceClient);
338 xWalkViewContainer.get().setXWalkWebChromeClient(webChromeClient);
342 return xWalkViewContainer.get();
345 protected ViewPair createViewsOnMainSync(final TestHelperBridge helperBridge0,
346 final TestHelperBridge helperBridge1,
347 final XWalkClient client0,
348 final XWalkClient client1,
349 final XWalkResourceClient resourceClient0,
350 final XWalkResourceClient resourceClient1,
351 final XWalkWebChromeClient chromeClient0,
352 final XWalkWebChromeClient chromeClient1,
353 final Context context) throws Throwable {
354 final XWalkView walkView0 = createXWalkViewContainerOnMainSync(context,
355 client0, resourceClient0, chromeClient0);
356 final XWalkView walkView1 = createXWalkViewContainerOnMainSync(context,
357 client1, resourceClient1, chromeClient1);
358 final AtomicReference<ViewPair> viewPair = new AtomicReference<ViewPair>();
360 getInstrumentation().runOnMainSync(new Runnable() {
363 viewPair.set(new ViewPair(walkView0, helperBridge0, walkView1, helperBridge1));
367 return viewPair.get();
370 protected void loadAssetFile(String fileName) throws Exception {
371 String fileContent = getFileContent(fileName);
372 loadDataSync(fileName, fileContent, "text/html", false);
375 public void loadAssetFileAndWaitForTitle(String fileName) throws Exception {
376 CallbackHelper getTitleHelper = mTestHelperBridge.getOnTitleUpdatedHelper();
377 int currentCallCount = getTitleHelper.getCallCount();
378 String fileContent = getFileContent(fileName);
380 loadDataSync(fileName, fileContent, "text/html", false);
382 getTitleHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
386 protected XWalkView getXWalkView() {
390 protected void runTestWaitPageFinished(Runnable runnable) throws Exception{
391 CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
392 int currentCallCount = pageFinishedHelper.getCallCount();
394 pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
398 protected void reloadSync(final int mode) throws Exception {
399 runTestWaitPageFinished(new Runnable(){
402 getInstrumentation().runOnMainSync(new Runnable() {
405 mXWalkView.reload(mode);
412 protected void goBackSync() throws Throwable {
413 runTestWaitPageFinished(new Runnable(){
416 getInstrumentation().runOnMainSync(new Runnable() {
419 mXWalkView.getNavigationHistory().navigate(
420 XWalkNavigationHistory.Direction.BACKWARD, 1);
427 protected void goForwardSync() throws Throwable {
428 runTestWaitPageFinished(new Runnable(){
431 getInstrumentation().runOnMainSync(new Runnable() {
434 mXWalkView.getNavigationHistory().navigate(
435 XWalkNavigationHistory.Direction.FORWARD, 1);
442 protected void clearHistoryOnUiThread() throws Exception {
443 getInstrumentation().runOnMainSync(new Runnable() {
446 mXWalkView.getNavigationHistory().clear();
451 protected boolean canGoBackOnUiThread() throws Throwable {
452 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
454 public Boolean call() {
455 return mXWalkView.getNavigationHistory().canGoBack();
460 protected boolean canGoForwardOnUiThread() throws Throwable {
461 return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
463 public Boolean call() {
464 return mXWalkView.getNavigationHistory().canGoForward();
469 protected String executeJavaScriptAndWaitForResult(final String code) throws Exception {
471 final TestHelperBridge.OnEvaluateJavaScriptResultHelper helper =
472 mTestHelperBridge.getOnEvaluateJavaScriptResultHelper();
473 getInstrumentation().runOnMainSync(new Runnable() {
476 helper.evaluateJavascript(mXWalkView, code);
479 helper.waitUntilHasValue();
480 Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
481 return helper.getJsonResultAndClear();
484 protected ViewPair createViews() throws Throwable {
485 TestHelperBridge helperBridge0 = new TestHelperBridge();
486 TestHelperBridge helperBridge1 = new TestHelperBridge();
487 TestXWalkClientBase viewClient0 = new TestXWalkClientBase(helperBridge0);
488 TestXWalkClientBase viewClient1 = new TestXWalkClientBase(helperBridge1);
489 TestXWalkWebChromeClientBase chromeClient0 =
490 new TestXWalkWebChromeClientBase(helperBridge0);
491 TestXWalkWebChromeClientBase chromeClient1 =
492 new TestXWalkWebChromeClientBase(helperBridge1);
493 TestXWalkResourceClientBase resourceClient0 =
494 new TestXWalkResourceClientBase(helperBridge0);
495 TestXWalkResourceClientBase resourceClient1 =
496 new TestXWalkResourceClientBase(helperBridge1);
498 createViewsOnMainSync(helperBridge0, helperBridge1, viewClient0, viewClient1,
499 resourceClient0, resourceClient1, chromeClient0, chromeClient1,