4603a78dc5f84845a4ff4b3e8152ef2924c8cd26
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / TestHelperBridge.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 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.util.Log;
9 import android.webkit.ValueCallback;
10 import android.webkit.WebResourceResponse;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.TimeoutException;
18
19 import org.chromium.content.browser.test.util.CallbackHelper;
20 import org.chromium.content.browser.test.util.Criteria;
21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageStartedHelper;
23 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnReceivedErrorHelper;
24
25 import org.xwalk.core.XWalkView;
26
27 class TestHelperBridge {
28
29     // Two new helper classes for testing new APIs.
30     public class ShouldInterceptLoadRequestHelper extends CallbackHelper {
31         private List<String> mShouldInterceptRequestUrls = new ArrayList<String>();
32         private ConcurrentHashMap<String, WebResourceResponse> mReturnValuesByUrls
33             = new ConcurrentHashMap<String, WebResourceResponse>();
34         // This is read from the IO thread, so needs to be marked volatile.
35         private volatile WebResourceResponse mResourceResponseReturnValue = null;
36         private String mUrlToWaitFor;
37
38         void setReturnValue(WebResourceResponse value) {
39             mResourceResponseReturnValue = value;
40         }
41
42         void setReturnValueForUrl(String url, WebResourceResponse value) {
43             mReturnValuesByUrls.put(url, value);
44         }
45
46         public void setUrlToWaitFor(String url) {
47             mUrlToWaitFor = url;
48         }
49
50         public List<String> getUrls() {
51             assert getCallCount() > 0;
52             return mShouldInterceptRequestUrls;
53         }
54
55         public WebResourceResponse getReturnValue(String url) {
56             WebResourceResponse value = mReturnValuesByUrls.get(url);
57             if (value != null) return value;
58             return mResourceResponseReturnValue;
59         }
60
61         public void notifyCalled(String url) {
62             if (mUrlToWaitFor == null || mUrlToWaitFor.equals(url)) {
63                 mShouldInterceptRequestUrls.add(url);
64                 notifyCalled();
65             }
66         }
67     }
68
69     public class OnLoadStartedHelper extends CallbackHelper {
70         private String mUrl;
71
72         public String getUrl() {
73             assert getCallCount() > 0;
74             return mUrl;
75         }
76
77         public void notifyCalled(String url) {
78             mUrl = url;
79             notifyCalled();
80         }
81     }
82
83     class OnEvaluateJavaScriptResultHelper extends CallbackHelper {
84         private String mJsonResult;
85         public void evaluateJavascript(XWalkView xWalkView, String code) {
86             ValueCallback<String> callback =
87                 new ValueCallback<String>() {
88                     @Override
89                     public void onReceiveValue(String jsonResult) {
90                         notifyCalled(jsonResult);
91                     }
92                 };
93             xWalkView.evaluateJavascript(code, callback);
94             mJsonResult = null;
95         }
96
97         public boolean hasValue() {
98             return mJsonResult != null;
99         }
100
101         public boolean waitUntilHasValue() throws InterruptedException, TimeoutException {
102             waitUntilCriteria(getHasValueCriteria());
103             return hasValue();
104         }
105
106         public String getJsonResultAndClear() {
107             assert hasValue();
108             String result = mJsonResult;
109             mJsonResult = null;
110             return result;
111         }
112
113         public Criteria getHasValueCriteria() {
114             return new Criteria() {
115                 @Override
116                 public boolean isSatisfied() {
117                     return hasValue();
118                 }
119             };
120         }
121
122         public void notifyCalled(String jsonResult) {
123             assert !hasValue();
124             mJsonResult = jsonResult;
125             notifyCalled();
126         }
127     }
128
129     private String mChangedTitle;
130     private final OnPageStartedHelper mOnPageStartedHelper;
131     private final OnPageFinishedHelper mOnPageFinishedHelper;
132     private final OnReceivedErrorHelper mOnReceivedErrorHelper;
133
134     private final OnEvaluateJavaScriptResultHelper mOnEvaluateJavaScriptResultHelper;
135
136     private final OnTitleUpdatedHelper mOnTitleUpdatedHelper;
137     private final ShouldInterceptLoadRequestHelper mShouldInterceptLoadRequestHelper;
138     private final OnLoadStartedHelper mOnLoadStartedHelper;
139
140     public TestHelperBridge() {
141         mOnPageStartedHelper = new OnPageStartedHelper();
142         mOnPageFinishedHelper = new OnPageFinishedHelper();
143         mOnReceivedErrorHelper = new OnReceivedErrorHelper();
144         mOnEvaluateJavaScriptResultHelper = new OnEvaluateJavaScriptResultHelper();
145         mOnTitleUpdatedHelper = new OnTitleUpdatedHelper();
146         mShouldInterceptLoadRequestHelper = new ShouldInterceptLoadRequestHelper();
147         mOnLoadStartedHelper = new OnLoadStartedHelper();
148     }
149
150     public OnPageStartedHelper getOnPageStartedHelper() {
151         return mOnPageStartedHelper;
152     }
153
154     public OnPageFinishedHelper getOnPageFinishedHelper() {
155         return mOnPageFinishedHelper;
156     }
157
158     public OnReceivedErrorHelper getOnReceivedErrorHelper() {
159         return mOnReceivedErrorHelper;
160     }
161
162     public OnEvaluateJavaScriptResultHelper getOnEvaluateJavaScriptResultHelper() {
163         return mOnEvaluateJavaScriptResultHelper;
164     }
165
166     public OnTitleUpdatedHelper getOnTitleUpdatedHelper() {
167         return mOnTitleUpdatedHelper;
168     }
169
170     public ShouldInterceptLoadRequestHelper getShouldInterceptLoadRequestHelper() {
171         return mShouldInterceptLoadRequestHelper;
172     }
173
174     public OnLoadStartedHelper getOnLoadStartedHelper() {
175         return mOnLoadStartedHelper;
176     }
177
178     public void onTitleChanged(String title) {
179         mChangedTitle = title;
180         mOnTitleUpdatedHelper.notifyCalled(title);
181     }
182
183     public String getChangedTitle() {
184         return mChangedTitle;
185     }
186
187     public void onPageStarted(String url) {
188         mOnPageStartedHelper.notifyCalled(url);
189     }
190
191     public void onPageFinished(String url) {
192         mOnPageFinishedHelper.notifyCalled(url);
193     }
194
195     public void onReceivedLoadError(int errorCode, String description, String failingUrl) {
196         mOnReceivedErrorHelper.notifyCalled(errorCode, description, failingUrl);
197     }
198
199     public WebResourceResponse shouldInterceptLoadRequest(String url) {
200         WebResourceResponse response = mShouldInterceptLoadRequestHelper.getReturnValue(url);
201         mShouldInterceptLoadRequestHelper.notifyCalled(url);
202         return response;
203     }
204
205     public void onLoadStarted(String url) {
206         mOnLoadStartedHelper.notifyCalled(url);
207     }
208 }