Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / util / runtime_client / src / org / xwalk / test / util / XWalkRuntimeClientTestUtilBase.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.test.util;
7
8 import android.app.Activity;
9 import android.app.Instrumentation;
10 import android.content.Context;
11
12 import java.io.InputStream;
13 import java.io.IOException;
14 import java.util.concurrent.TimeUnit;
15
16 import org.chromium.content.browser.test.util.CallbackHelper;
17 import org.xwalk.app.runtime.XWalkRuntimeView;
18
19 public class XWalkRuntimeClientTestUtilBase extends XWalkTestUtilBase<XWalkRuntimeView> {
20     public class PageStatusCallback {
21         public void onPageStarted(String url) {
22             mCallbackHelpers.onPageStarted(url);
23         }
24
25         public void onPageFinished(String url) {
26             mCallbackHelpers.didFinishLoad(url);
27         }
28
29         public void onReceivedLoadError(int errorCode, String description, String failingUrl) {
30             mCallbackHelpers.onReceivedError(errorCode, description, failingUrl);
31         }
32
33         public void onReceivedTitle(String title) {
34             mCallbackHelpers.onTitleUpdated(title);
35         }
36     }
37
38     public XWalkRuntimeClientTestUtilBase(XWalkRuntimeView runtimeView,
39             Instrumentation instrumentation) {
40         super(runtimeView, instrumentation);
41     }
42
43     @Override
44     public void loadUrlAsync(final String url) throws Exception {
45         getInstrumentation().runOnMainSync(new Runnable() {
46             @Override
47             public void run() {
48                 getTestedView().loadAppFromUrl(url);
49             }
50         });
51     }
52
53     @Override
54     public void loadDataAsync(final String data, final String mimeType,
55              final boolean isBase64Encoded) throws Exception {
56         getInstrumentation().runOnMainSync(new Runnable() {
57             @Override
58             public void run() {
59                 getTestedView().loadDataForTest(data, mimeType, isBase64Encoded);
60             }
61         });
62     }
63
64     public void loadManifestSync(String url) throws Exception {
65         CallbackHelper pageFinishedHelper = mCallbackHelpers.getOnPageFinishedHelper();
66         int currentCallCount = pageFinishedHelper.getCallCount();
67         loadManifestAsync(url);
68
69         pageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
70                 TimeUnit.SECONDS);
71     }
72
73     public void loadManifestAsync(final String url) throws Exception {
74         getInstrumentation().runOnMainSync(new Runnable() {
75             @Override
76             public void run() {
77                 getTestedView().loadAppFromManifest(url);
78             }
79         });
80     }
81
82     public void pauseActivity(final Context activity) throws Exception {
83         getInstrumentation().runOnMainSync(new Runnable() {
84             @Override
85             public void run() {
86                 ActivityToCausePause.pauseActivity(activity);
87             }
88         });
89     }
90
91     public void resumeActivity() throws Exception {
92         getInstrumentation().runOnMainSync(new Runnable() {
93             @Override
94             public void run() {
95                 ActivityToCausePause.resumeActivity();
96             }
97         });
98     }
99 }