Upstream version 6.35.131.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.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;
33
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();
40
41     class TestXWalkClientBase extends XWalkClient {
42         TestHelperBridge mInnerContentsClient;
43         public TestXWalkClientBase(TestHelperBridge client) {
44             super(getXWalkView());
45             mInnerContentsClient = client;
46         }
47
48         @Override
49         public void onPageStarted(XWalkView view, String url) {
50             mInnerContentsClient.onPageStarted(url);
51         }
52
53         @Override
54         public void onPageFinished(XWalkView view, String url) {
55             mInnerContentsClient.onPageFinished(url);
56         }
57     }
58
59     class TestXWalkClient extends TestXWalkClientBase {
60         public TestXWalkClient() {
61             super(mTestHelperBridge);
62         }
63     }
64
65     class TestXWalkWebChromeClientBase extends XWalkWebChromeClient {
66         TestHelperBridge mInnerContentsClient;
67         public TestXWalkWebChromeClientBase(TestHelperBridge client) {
68             super(getXWalkView());
69             mInnerContentsClient = client;
70         }
71
72         @Override
73         public void onReceivedTitle(XWalkView view, String title) {
74             mInnerContentsClient.onTitleChanged(title);
75         }
76     }
77
78     class TestXWalkWebChromeClient extends TestXWalkWebChromeClientBase {
79         public TestXWalkWebChromeClient() {
80             super(mTestHelperBridge);
81         }
82     }
83
84     class TestXWalkResourceClientBase extends XWalkResourceClient {
85         TestHelperBridge mInnerContentsClient;
86         public TestXWalkResourceClientBase(TestHelperBridge client) {
87             super(mXWalkView);
88             mInnerContentsClient = client;
89         }
90
91         @Override
92         public void onLoadStarted(XWalkView view, String url) {
93             mTestHelperBridge.onLoadStarted(url);
94         }
95
96         @Override
97         public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
98             mTestHelperBridge.onReceivedLoadError(errorCode, description, failingUrl);
99         }
100
101         @Override
102         public WebResourceResponse shouldInterceptLoadRequest(XWalkView view,
103                 String url) {
104             return mTestHelperBridge.shouldInterceptLoadRequest(url);
105         }
106     }
107
108     class TestXWalkResourceClient extends TestXWalkResourceClientBase {
109         public TestXWalkResourceClient() {
110             super(mTestHelperBridge);
111         }
112     }
113
114     void setXWalkClient(final XWalkClient client) {
115         getInstrumentation().runOnMainSync(new Runnable() {
116             @Override
117             public void run() {
118                 getXWalkView().setXWalkClient(client);
119             }
120         });
121     }
122
123     void setXWalkWebChromeClient(final XWalkWebChromeClient client) {
124         getInstrumentation().runOnMainSync(new Runnable() {
125             @Override
126             public void run() {
127                 getXWalkView().setXWalkWebChromeClient(client);
128             }
129         });
130     }
131
132     void setResourceClient(final XWalkResourceClient client) {
133         getInstrumentation().runOnMainSync(new Runnable() {
134             @Override
135             public void run() {
136                 getXWalkView().setResourceClient(client);
137             }
138         });
139     }
140
141     static class ViewPair {
142         private final XWalkView content0;
143         private final TestHelperBridge client0;
144         private final XWalkView content1;
145         private final TestHelperBridge client1;
146
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;
153         }
154
155         XWalkView getContent0() {
156             return content0;
157         }
158
159         TestHelperBridge getClient0() {
160             return client0;
161         }
162
163         XWalkView getContent1() {
164             return content1;
165         }
166
167         TestHelperBridge getClient1() {
168             return client1;
169         }
170     }
171
172     public XWalkViewTestBase() {
173         super(XWalkViewTestRunnerActivity.class);
174     }
175
176     @Override
177     protected void setUp() throws Exception {
178         super.setUp();
179
180         // Must call getActivity() here but not in main thread.
181         final Activity activity = getActivity();
182         getInstrumentation().runOnMainSync(new Runnable() {
183             @Override
184             public void run() {
185                 mXWalkView = new XWalkView(activity, activity);
186                 getActivity().addView(mXWalkView);
187                 // mXWalkView.getXWalkViewContentForTest().installWebContentsObserverForTest(mTestHelperBridge);
188             }
189         });
190     }
191
192     protected boolean pollOnUiThread(final Callable<Boolean> callable) throws Exception {
193         return CriteriaHelper.pollForCriteria(new Criteria() {
194             @Override
195             public boolean isSatisfied() {
196                 try {
197                     return runTestOnUiThreadAndGetResult(callable);
198                 } catch (Throwable e) {
199                     return false;
200                 }
201             }
202         });
203     }
204
205     protected void loadJavaScriptUrl(final String url) throws Exception {
206         if (!url.startsWith("javascript:")) {
207             Log.w(TAG, "loadJavascriptUrl only accepts javascript: url");
208             return;
209         }
210         loadUrlAsync(url);
211     }
212
213     protected void loadUrlSync(final String url) throws Exception {
214         CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
215         int currentCallCount = pageFinishedHelper.getCallCount();
216         loadUrlAsync(url);
217
218         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
219                 TimeUnit.SECONDS);
220     }
221
222     protected void loadUrlAsync(final String url) throws Exception {
223         getInstrumentation().runOnMainSync(new Runnable() {
224             @Override
225             public void run() {
226                 mXWalkView.load(url, null);
227             }
228         });
229     }
230
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,
237                 TimeUnit.SECONDS);
238     }
239
240     protected void loadDataAsync(final String url, final String data, final String mimeType,
241              final boolean isBase64Encoded) throws Exception {
242         getInstrumentation().runOnMainSync(new Runnable() {
243             @Override
244             public void run() {
245                 mXWalkView.load(url, data);
246             }
247         });
248     }
249
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);
256
257         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
258                 TimeUnit.SECONDS);
259     }
260
261     protected void loadUrlAsyncByContent(final XWalkView xWalkContent,
262             final String url) throws Exception {
263         getInstrumentation().runOnMainSync(new Runnable() {
264             @Override
265             public void run() {
266                 xWalkContent.load(url, null);
267             }
268         });
269     }
270
271     protected String getTitleOnUiThread() throws Exception {
272         return runTestOnUiThreadAndGetResult(new Callable<String>() {
273             @Override
274             public String call() throws Exception {
275                 return mXWalkView.getTitle();
276             }
277         });
278     }
279
280     protected <R> R runTestOnUiThreadAndGetResult(Callable<R> callable)
281             throws Exception {
282         FutureTask<R> task = new FutureTask<R>(callable);
283         getInstrumentation().waitForIdleSync();
284         getInstrumentation().runOnMainSync(task);
285         return task.get();
286     }
287
288     protected String getFileContent(String fileName) {
289         try {
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);
295             inputStream.close();
296
297             String fileContent = new String(buffer);
298             return fileContent;
299         } catch (IOException e) {
300             throw new RuntimeException(e);
301         }
302     }
303
304     protected String getTitleOnUiThreadByContent(final XWalkView xWalkContent) throws Exception {
305         return runTestOnUiThreadAndGetResult(new Callable<String>() {
306             @Override
307             public String call() throws Exception {
308                 String title = xWalkContent.getTitle();
309                 return title;
310             }
311         });
312     }
313
314     protected XWalkSettings getXWalkSettingsOnUiThreadByContent(
315             final XWalkView xwalkContent) throws Exception {
316         return runTestOnUiThreadAndGetResult(new Callable<XWalkSettings>() {
317             @Override
318             public XWalkSettings call() throws Exception {
319                 return xwalkContent.getSettings();
320             }
321         });
322     }
323
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() {
332             @Override
333             public void run() {
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);
339             }
340         });
341
342         return xWalkViewContainer.get();
343     }
344
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>();
359
360         getInstrumentation().runOnMainSync(new Runnable() {
361             @Override
362             public void run() {
363                 viewPair.set(new ViewPair(walkView0, helperBridge0, walkView1, helperBridge1));
364             }
365         });
366
367         return viewPair.get();
368     }
369
370     protected void loadAssetFile(String fileName) throws Exception {
371         String fileContent = getFileContent(fileName);
372         loadDataSync(fileName, fileContent, "text/html", false);
373     }
374
375     public void loadAssetFileAndWaitForTitle(String fileName) throws Exception {
376         CallbackHelper getTitleHelper = mTestHelperBridge.getOnTitleUpdatedHelper();
377         int currentCallCount = getTitleHelper.getCallCount();
378         String fileContent = getFileContent(fileName);
379
380         loadDataSync(fileName, fileContent, "text/html", false);
381
382         getTitleHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
383                 TimeUnit.SECONDS);
384     }
385
386     protected XWalkView getXWalkView() {
387         return mXWalkView;
388     }
389
390     protected void runTestWaitPageFinished(Runnable runnable) throws Exception{
391         CallbackHelper pageFinishedHelper = mTestHelperBridge.getOnPageFinishedHelper();
392         int currentCallCount = pageFinishedHelper.getCallCount();
393         runnable.run();
394         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
395                 TimeUnit.SECONDS);
396     }
397
398     protected void reloadSync(final int mode) throws Exception {
399         runTestWaitPageFinished(new Runnable(){
400             @Override
401             public void run() {
402                 getInstrumentation().runOnMainSync(new Runnable() {
403                     @Override
404                     public void run() {
405                         mXWalkView.reload(mode);
406                     }
407                 });
408             }
409         });
410     }
411
412     protected void goBackSync() throws Throwable {
413         runTestWaitPageFinished(new Runnable(){
414             @Override
415             public void run() {
416                 getInstrumentation().runOnMainSync(new Runnable() {
417                     @Override
418                     public void run() {
419                         mXWalkView.getNavigationHistory().navigate(
420                             XWalkNavigationHistory.Direction.BACKWARD, 1);
421                     }
422                 });
423             }
424         });
425     }
426
427     protected void goForwardSync() throws Throwable {
428         runTestWaitPageFinished(new Runnable(){
429             @Override
430             public void run() {
431                 getInstrumentation().runOnMainSync(new Runnable() {
432                     @Override
433                     public void run() {
434                         mXWalkView.getNavigationHistory().navigate(
435                             XWalkNavigationHistory.Direction.FORWARD, 1);
436                     }
437                 });
438             }
439         });
440     }
441
442     protected void clearHistoryOnUiThread() throws Exception {
443         getInstrumentation().runOnMainSync(new Runnable() {
444             @Override
445             public void run() {
446                 mXWalkView.getNavigationHistory().clear();
447             }
448         });
449     }
450
451     protected boolean canGoBackOnUiThread() throws Throwable {
452         return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
453             @Override
454             public Boolean call() {
455                 return mXWalkView.getNavigationHistory().canGoBack();
456             }
457         });
458     }
459
460     protected boolean canGoForwardOnUiThread() throws Throwable {
461         return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
462             @Override
463             public Boolean call() {
464                 return mXWalkView.getNavigationHistory().canGoForward();
465             }
466         });
467     }
468
469     protected String executeJavaScriptAndWaitForResult(final String code) throws Exception {
470
471         final TestHelperBridge.OnEvaluateJavaScriptResultHelper helper =
472                 mTestHelperBridge.getOnEvaluateJavaScriptResultHelper();
473         getInstrumentation().runOnMainSync(new Runnable() {
474             @Override
475             public void run() {
476                 helper.evaluateJavascript(mXWalkView, code);
477             }
478         });
479         helper.waitUntilHasValue();
480         Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
481         return helper.getJsonResultAndClear();
482     }
483
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);
497         ViewPair viewPair =
498                 createViewsOnMainSync(helperBridge0, helperBridge1, viewClient0, viewClient1,
499                         resourceClient0, resourceClient1, chromeClient0, chromeClient1,
500                                 getActivity());
501
502         return viewPair;
503     }
504 }