Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / XWalkViewTestBase.java
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.
5
6 package org.xwalk.core.xwview.test;
7
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;
13
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;
20
21 import junit.framework.Assert;
22
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.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
28 import org.xwalk.core.XWalkClient;
29 import org.xwalk.core.XWalkContent;
30 import org.xwalk.core.XWalkContentsClient;
31 import org.xwalk.core.XWalkNavigationHistory;
32 import org.xwalk.core.XWalkResourceClient;
33 import org.xwalk.core.XWalkResourceClientImpl;
34 import org.xwalk.core.XWalkSettings;
35 import org.xwalk.core.XWalkView;
36 import org.xwalk.core.XWalkWebChromeClient;
37
38 public class XWalkViewTestBase
39        extends ActivityInstrumentationTestCase2<XWalkViewTestRunnerActivity> {
40     protected final static int WAIT_TIMEOUT_SECONDS = 15;
41     private final static String TAG = "XWalkViewTestBase";
42     private XWalkView mXWalkView;
43     final TestHelperBridge mTestHelperBridge = new TestHelperBridge();
44
45     class TestXWalkClientBase extends XWalkClient {
46         TestHelperBridge mInnerContentsClient;
47         public TestXWalkClientBase(TestHelperBridge client) {
48             super(getXWalkView().getContext(), getXWalkView());
49             mInnerContentsClient = client;
50         }
51
52         @Override
53         public void onPageStarted(XWalkView view, String url) {
54             mInnerContentsClient.onPageStarted(url);
55         }
56
57         @Override
58         public void onPageFinished(XWalkView view, String url) {
59             mInnerContentsClient.onPageFinished(url);
60         }
61     }
62
63     class TestXWalkClient extends TestXWalkClientBase {
64         public TestXWalkClient() {
65             super(mTestHelperBridge);
66         }
67     }
68
69     class TestXWalkWebChromeClientBase extends XWalkWebChromeClient {
70         TestHelperBridge mInnerContentsClient;
71         public TestXWalkWebChromeClientBase(TestHelperBridge client) {
72             super(getXWalkView().getContext(), getXWalkView());
73             mInnerContentsClient = client;
74         }
75
76         @Override
77         public void onReceivedTitle(XWalkView view, String title) {
78             mInnerContentsClient.onTitleChanged(title);
79         }
80     }
81
82     class TestXWalkWebChromeClient extends TestXWalkWebChromeClientBase {
83         public TestXWalkWebChromeClient() {
84             super(mTestHelperBridge);
85         }
86     }
87
88     class TestXWalkResourceClientBase extends XWalkResourceClient {
89         TestHelperBridge mInnerContentsClient;
90         public TestXWalkResourceClientBase(TestHelperBridge client) {
91             mInnerContentsClient = client;
92         }
93
94         @Override
95         public void onLoadStarted(XWalkView view, String url) {
96             mTestHelperBridge.onLoadStarted(url);
97         }
98
99         @Override
100         public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
101             mTestHelperBridge.onReceivedLoadError(errorCode, description, failingUrl);
102         }
103
104         @Override
105         public WebResourceResponse shouldInterceptLoadRequest(XWalkView view,
106                 String url) {
107             return mTestHelperBridge.shouldInterceptLoadRequest(url);
108         }
109     }
110
111     class TestXWalkResourceClient extends TestXWalkResourceClientBase {
112         public TestXWalkResourceClient() {
113             super(mTestHelperBridge);
114         }
115     }
116
117     void setXWalkClient(final XWalkClient client) {
118         getInstrumentation().runOnMainSync(new Runnable() {
119             @Override
120             public void run() {
121                 getXWalkView().setXWalkClient(client);
122             }
123         });
124     }
125
126     void setXWalkWebChromeClient(final XWalkWebChromeClient client) {
127         getInstrumentation().runOnMainSync(new Runnable() {
128             @Override
129             public void run() {
130                 getXWalkView().setXWalkWebChromeClient(client);
131             }
132         });
133     }
134
135     void setResourceClient(final XWalkResourceClient client) {
136         getInstrumentation().runOnMainSync(new Runnable() {
137             @Override
138             public void run() {
139                 getXWalkView().setResourceClient(client);
140             }
141         });
142     }
143
144     static class ViewPair {
145         private final XWalkContent content0;
146         private final TestHelperBridge client0;
147         private final XWalkContent content1;
148         private final TestHelperBridge client1;
149
150         ViewPair(XWalkContent content0, TestHelperBridge client0,
151                 XWalkContent content1, TestHelperBridge client1) {
152             this.content0 = content0;
153             this.client0 = client0;
154             this.content1 = content1;
155             this.client1 = client1;
156         }
157
158         XWalkContent getContent0() {
159             return content0;
160         }
161
162         TestHelperBridge getClient0() {
163             return client0;
164         }
165
166         XWalkContent getContent1() {
167             return content1;
168         }
169
170         TestHelperBridge getClient1() {
171             return client1;
172         }
173     }
174
175     public XWalkViewTestBase() {
176         super(XWalkViewTestRunnerActivity.class);
177     }
178
179     @Override
180     protected void setUp() throws Exception {
181         super.setUp();
182
183         // Must call getActivity() here but not in main thread.
184         final Activity activity = getActivity();
185         getInstrumentation().runOnMainSync(new Runnable() {
186             @Override
187             public void run() {
188                 mXWalkView = new XWalkView(activity, activity);
189                 getActivity().addView(mXWalkView);
190                 // mXWalkView.getXWalkViewContentForTest().installWebContentsObserverForTest(mTestHelperBridge);
191             }
192         });
193     }
194
195     protected boolean pollOnUiThread(final Callable<Boolean> callable) throws Exception {
196         return CriteriaHelper.pollForCriteria(new Criteria() {
197             @Override
198             public boolean isSatisfied() {
199                 try {
200                     return runTestOnUiThreadAndGetResult(callable);
201                 } catch (Throwable e) {
202                     return false;
203                 }
204             }
205         });
206     }
207
208     protected void loadJavaScriptUrl(final String url) throws Exception {
209         if (!url.startsWith("javascript:")) {
210             Log.w(TAG, "loadJavascriptUrl only accepts javascript: url");
211             return;
212         }
213         loadUrlAsync(url);
214     }
215
216     protected void loadUrlSync(final String url) throws Exception {
217         CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
218         int currentCallCount = pageFinishedHelper.getCallCount();
219         loadUrlAsync(url);
220
221         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
222                 TimeUnit.SECONDS);
223     }
224
225     protected void loadUrlAsync(final String url) throws Exception {
226         getInstrumentation().runOnMainSync(new Runnable() {
227             @Override
228             public void run() {
229                 mXWalkView.load(url, null);
230             }
231         });
232     }
233
234     protected void loadDataSync(final String data, final String mimeType,
235             final boolean isBase64Encoded) throws Exception {
236         CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
237         int currentCallCount = pageFinishedHelper.getCallCount();
238         loadDataAsync(data, mimeType, isBase64Encoded);
239         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
240                 TimeUnit.SECONDS);
241     }
242
243     protected void loadDataAsync(final String data, final String mimeType,
244              final boolean isBase64Encoded) throws Exception {
245         getInstrumentation().runOnMainSync(new Runnable() {
246             @Override
247             public void run() {
248                 mXWalkView.getXWalkViewContentForTest().getContentViewCoreForTest(
249                         ).loadUrl(LoadUrlParams.createLoadDataParams(
250                         data, mimeType, isBase64Encoded));
251             }
252         });
253     }
254
255     protected void loadUrlSyncByContent(final XWalkContent xWalkContent,
256             final TestHelperBridge contentsClient,
257             final String url) throws Exception {
258         CallbackHelper pageFinishedHelper = contentsClient.getOnPageFinishedHelper();
259         int currentCallCount = pageFinishedHelper.getCallCount();
260         loadUrlAsyncByContent(xWalkContent, url);
261
262         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
263                 TimeUnit.SECONDS);
264     }
265
266     protected void loadUrlAsyncByContent(final XWalkContent xWalkContent,
267             final String url) throws Exception {
268         getInstrumentation().runOnMainSync(new Runnable() {
269             @Override
270             public void run() {
271                 xWalkContent.loadUrl(url);
272             }
273         });
274     }
275
276     protected String getTitleOnUiThread() throws Exception {
277         return runTestOnUiThreadAndGetResult(new Callable<String>() {
278             @Override
279             public String call() throws Exception {
280                 return mXWalkView.getTitle();
281             }
282         });
283     }
284
285     protected <R> R runTestOnUiThreadAndGetResult(Callable<R> callable)
286             throws Exception {
287         FutureTask<R> task = new FutureTask<R>(callable);
288         getInstrumentation().waitForIdleSync();
289         getInstrumentation().runOnMainSync(task);
290         return task.get();
291     }
292
293     protected String getFileContent(String fileName) {
294         try {
295             Context context = getInstrumentation().getContext();
296             InputStream inputStream = context.getAssets().open(fileName);
297             int size = inputStream.available();
298             byte buffer[] = new byte[size];
299             inputStream.read(buffer);
300             inputStream.close();
301
302             String fileContent = new String(buffer);
303             return fileContent;
304         } catch (IOException e) {
305             throw new RuntimeException(e);
306         }
307     }
308
309     protected String getTitleOnUiThreadByContent(final XWalkContent xWalkContent) throws Exception {
310         return runTestOnUiThreadAndGetResult(new Callable<String>() {
311             @Override
312             public String call() throws Exception {
313                 String title = xWalkContent.getContentViewCoreForTest().getTitle();
314                 return title;
315             }
316         });
317     }
318
319     protected XWalkSettings getXWalkSettingsOnUiThreadByContent(
320             final XWalkContent xwalkContent) throws Exception {
321         return runTestOnUiThreadAndGetResult(new Callable<XWalkSettings>() {
322             @Override
323             public XWalkSettings call() throws Exception {
324                 return xwalkContent.getSettings();
325             }
326         });
327     }
328
329     protected XWalkView createXWalkViewContainerOnMainSync(
330             final Context context,
331             final XWalkClient client,
332             final XWalkResourceClient resourceClient,
333             final XWalkWebChromeClient webChromeClient) throws Exception {
334         final AtomicReference<XWalkView> xWalkViewContainer =
335                 new AtomicReference<XWalkView>();
336         getInstrumentation().runOnMainSync(new Runnable() {
337             @Override
338             public void run() {
339                 xWalkViewContainer.set(new XWalkView(context, getActivity()));
340                 getActivity().addView(xWalkViewContainer.get());
341                 xWalkViewContainer.get().setXWalkClient(client);
342                 xWalkViewContainer.get().setResourceClient(resourceClient);
343                 xWalkViewContainer.get().setXWalkWebChromeClient(webChromeClient);
344             }
345         });
346
347         return xWalkViewContainer.get();
348     }
349
350     protected XWalkContent getXWalkContentOnMainSync(final XWalkView view) throws Exception {
351         final AtomicReference<XWalkContent> xWalkContent =
352                 new AtomicReference<XWalkContent>();
353         getInstrumentation().runOnMainSync(new Runnable() {
354             @Override
355             public void run() {
356                 xWalkContent.set(view.getXWalkViewContentForTest());
357             }
358         });
359
360         return xWalkContent.get();
361     }
362
363     protected ViewPair createViewsOnMainSync(final TestHelperBridge helperBridge0,
364                                              final TestHelperBridge helperBridge1,
365                                              final XWalkClient client0,
366                                              final XWalkClient client1,
367                                              final XWalkResourceClient resourceClient0,
368                                              final XWalkResourceClient resourceClient1,
369                                              final XWalkWebChromeClient chromeClient0,
370                                              final XWalkWebChromeClient chromeClient1,
371                                              final Context context) throws Throwable {
372         final XWalkView walkView0 = createXWalkViewContainerOnMainSync(context,
373                 client0, resourceClient0, chromeClient0);
374         final XWalkView walkView1 = createXWalkViewContainerOnMainSync(context,
375                 client1, resourceClient1, chromeClient1);
376         final AtomicReference<ViewPair> viewPair = new AtomicReference<ViewPair>();
377
378         getInstrumentation().runOnMainSync(new Runnable() {
379             @Override
380             public void run() {
381                 XWalkContent content0 = walkView0.getXWalkViewContentForTest();
382                 XWalkContent content1 = walkView1.getXWalkViewContentForTest();
383                 viewPair.set(new ViewPair(content0, helperBridge0, content1, helperBridge1));
384             }
385         });
386
387         return viewPair.get();
388     }
389
390     protected void loadAssetFile(String fileName) throws Exception {
391         String fileContent = getFileContent(fileName);
392         loadDataSync(fileContent, "text/html", false);
393     }
394
395     public void loadAssetFileAndWaitForTitle(String fileName) throws Exception {
396         CallbackHelper getTitleHelper = mTestHelperBridge.getOnTitleUpdatedHelper();
397         int currentCallCount = getTitleHelper.getCallCount();
398         String fileContent = getFileContent(fileName);
399
400         loadDataSync(fileContent, "text/html", false);
401
402         getTitleHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
403                 TimeUnit.SECONDS);
404     }
405
406     protected XWalkView getXWalkView() {
407         return mXWalkView;
408     }
409
410     protected void runTestWaitPageFinished(Runnable runnable) throws Exception{
411         CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
412         int currentCallCount = pageFinishedHelper.getCallCount();
413         runnable.run();
414         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
415                 TimeUnit.SECONDS);
416     }
417
418     protected void reloadSync() throws Exception {
419         runTestWaitPageFinished(new Runnable(){
420             @Override
421             public void run() {
422                 getInstrumentation().runOnMainSync(new Runnable() {
423                     @Override
424                     public void run() {
425                         mXWalkView.reload();
426                     }
427                 });
428             }
429         });
430     }
431
432     protected void goBackSync() throws Throwable {
433         runTestWaitPageFinished(new Runnable(){
434             @Override
435             public void run() {
436                 getInstrumentation().runOnMainSync(new Runnable() {
437                     @Override
438                     public void run() {
439                         mXWalkView.getNavigationHistory().navigate(
440                             XWalkNavigationHistory.Direction.BACKWARD, 1);
441                     }
442                 });
443             }
444         });
445     }
446
447     protected void goForwardSync() throws Throwable {
448         runTestWaitPageFinished(new Runnable(){
449             @Override
450             public void run() {
451                 getInstrumentation().runOnMainSync(new Runnable() {
452                     @Override
453                     public void run() {
454                         mXWalkView.getNavigationHistory().navigate(
455                             XWalkNavigationHistory.Direction.FORWARD, 1);
456                     }
457                 });
458             }
459         });
460     }
461
462     protected void clearHistoryOnUiThread() throws Exception {
463         getInstrumentation().runOnMainSync(new Runnable() {
464             @Override
465             public void run() {
466                 mXWalkView.getNavigationHistory().clear();
467             }
468         });
469     }
470
471     protected boolean canGoBackOnUiThread() throws Throwable {
472         return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
473             @Override
474             public Boolean call() {
475                 return mXWalkView.getNavigationHistory().canGoBack();
476             }
477         });
478     }
479
480     protected boolean canGoForwardOnUiThread() throws Throwable {
481         return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
482             @Override
483             public Boolean call() {
484                 return mXWalkView.getNavigationHistory().canGoForward();
485             }
486         });
487     }
488
489     protected String executeJavaScriptAndWaitForResult(final String code) throws Exception {
490         final OnEvaluateJavaScriptResultHelper helper =
491                 mTestHelperBridge.getOnEvaluateJavaScriptResultHelper();
492         getInstrumentation().runOnMainSync(new Runnable() {
493             @Override
494             public void run() {
495                 XWalkContent content = mXWalkView.getXWalkViewContentForTest();
496                 helper.evaluateJavaScript(content.getContentViewCoreForTest(), code);
497             }
498         });
499         helper.waitUntilHasValue();
500         Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
501         return helper.getJsonResultAndClear();
502     }
503
504     protected ViewPair createViews() throws Throwable {
505         TestHelperBridge helperBridge0 = new TestHelperBridge();
506         TestHelperBridge helperBridge1 = new TestHelperBridge();
507         TestXWalkClientBase viewClient0 = new TestXWalkClientBase(helperBridge0);
508         TestXWalkClientBase viewClient1 = new TestXWalkClientBase(helperBridge1);
509         TestXWalkWebChromeClientBase chromeClient0 =
510                 new TestXWalkWebChromeClientBase(helperBridge0);
511         TestXWalkWebChromeClientBase chromeClient1 =
512                 new TestXWalkWebChromeClientBase(helperBridge1);
513         TestXWalkResourceClientBase resourceClient0 =
514                 new TestXWalkResourceClientBase(helperBridge0);
515         TestXWalkResourceClientBase resourceClient1 =
516                 new TestXWalkResourceClientBase(helperBridge1);
517         ViewPair viewPair =
518                 createViewsOnMainSync(helperBridge0, helperBridge1, viewClient0, viewClient1,
519                         resourceClient0, resourceClient1, chromeClient0, chromeClient1,
520                                 getActivity());
521
522         return viewPair;
523     }
524 }