OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.android_webview.test; | |
6 | |
7 import android.content.Context; | |
8 import android.test.suitebuilder.annotation.SmallTest; | |
9 import android.os.SystemClock; | |
10 import android.webkit.GeolocationPermissions; | |
11 | |
12 import org.chromium.android_webview.AwContents; | |
13 import org.chromium.base.test.util.Feature; | |
14 import org.chromium.content.browser.LocationProviderFactory; | |
15 import org.chromium.content.browser.test.util.Criteria; | |
16 import org.chromium.content.browser.test.util.CriteriaHelper; | |
17 import org.chromium.content.browser.test.util.MockLocationProvider; | |
18 | |
19 import java.util.List; | |
20 | |
21 public class GeolocationTest extends AwTestBase { | |
22 | |
23 private static final long TEST_TIMEOUT = 20000L; | |
bulach
2013/12/11 15:48:39
nit: looks huge! would 5secs work? also, "_MS" her
benm (inactive)
2013/12/11 20:08:51
Done.
| |
24 private static final int CHECK_INTERVAL = 100; | |
25 | |
26 private TestAwContentsClient mContentsClient; | |
27 private AwContents mAwContents; | |
28 private MockLocationProvider mMockLocationProvider; | |
29 | |
30 private static final String RAW_HTML = | |
31 "<!DOCTYPE html>\n" + | |
32 "<html>\n" + | |
33 " <head>\n" + | |
34 " <title>Geolocation</title>\n" + | |
35 " <script>\n" + | |
36 " var positionCount = 0;\n" + | |
37 " function gotPos(position) {\n" + | |
38 " positionCount++;\n" + | |
39 " }\n" + | |
40 " function initiate_getCurrentPosition() {\n" + | |
41 " navigator.geolocation.getCurrentPosition(\n" + | |
42 " gotPos, function() { }, { });\n" + | |
43 " }\n" + | |
44 " function initiate_watchPosition() {\n" + | |
45 " navigator.geolocation.watchPosition(\n" + | |
46 " gotPos, function() { }, { });\n" + | |
47 " }\n" + | |
48 " </script>\n" + | |
49 " </head>\n" + | |
50 " <body>\n" + | |
51 " </body>\n" + | |
52 "</html>"; | |
53 | |
54 @Override | |
55 public void setUp() throws Exception { | |
56 super.setUp(); | |
57 mContentsClient = new TestAwContentsClient() { | |
58 @Override | |
59 public void onGeolocationPermissionsShowPrompt(String origin, | |
60 GeolocationPermissions.Callback callback) { | |
61 callback.invoke(origin, true, true); | |
62 } | |
63 }; | |
64 mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAw Contents(); | |
65 enableJavaScriptOnUiThread(mAwContents); | |
66 setupGeolocation(); | |
67 } | |
68 | |
69 @Override | |
70 public void tearDown() throws Exception { | |
71 mMockLocationProvider.stopUpdates(); | |
72 GeolocationPermissions.getInstance().clearAll(); | |
73 super.tearDown(); | |
74 } | |
75 | |
76 private void setupGeolocation() { | |
77 getInstrumentation().runOnMainSync(new Runnable() { | |
78 @Override | |
79 public void run() { | |
80 mAwContents.getSettings().setGeolocationEnabled(true); | |
81 } | |
82 }); | |
83 mMockLocationProvider = new MockLocationProvider(); | |
84 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); | |
85 } | |
86 | |
87 private int getPositionCountFromJS() { | |
88 int result = -1; | |
89 try { | |
90 result = Integer.parseInt(executeJavaScriptAndWaitForResult( | |
91 mAwContents, mContentsClient, "positionCount")); | |
92 } catch (Exception e) { | |
93 fail("Unable to get positionCount"); | |
94 } | |
95 return result; | |
96 } | |
97 | |
98 private void ensureGeolocationRunning(final boolean running) throws Exceptio n { | |
99 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
100 @Override | |
101 public boolean isSatisfied() { | |
102 return mMockLocationProvider.isRunning() == running; | |
103 } | |
104 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
105 } | |
106 | |
107 | |
108 /** | |
109 * Ensure that a call to navigator.getCurrentPosition works in WebView. | |
110 */ | |
111 @SmallTest | |
bulach
2013/12/11 13:29:27
nit: I think all of these tests are at least Mediu
mkosiba (inactive)
2013/12/11 15:47:44
hmm.. I've been using Small for most of the integr
benm (inactive)
2013/12/11 20:08:51
Done.
| |
112 @Feature({"AndroidWebView"}) | |
113 public void testGetPosition() throws Throwable { | |
114 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
115 RAW_HTML, "text/html", false); | |
116 | |
117 mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); | |
118 | |
119 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
120 @Override | |
121 public boolean isSatisfied() { | |
122 return getPositionCountFromJS() == 1; | |
123 } | |
124 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
125 | |
126 mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); | |
127 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
128 @Override | |
129 public boolean isSatisfied() { | |
130 return getPositionCountFromJS() == 2; | |
131 } | |
132 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
133 } | |
134 | |
135 /** | |
136 * Ensure that a call to navigator.watchPosition works in WebView. | |
137 */ | |
138 @SmallTest | |
139 @Feature({"AndroidWebView"}) | |
140 public void testWatchPosition() throws Throwable { | |
141 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
142 RAW_HTML, "text/html", false); | |
143 | |
144 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
145 | |
146 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
147 @Override | |
148 public boolean isSatisfied() { | |
149 return getPositionCountFromJS() > 3; | |
bulach
2013/12/11 13:29:27
nit: isn't > 0 enough? or if you really want more
benm (inactive)
2013/12/11 20:08:51
3 was completely arbitrary :)
I wanted to make su
| |
150 } | |
151 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
152 } | |
153 | |
154 @SmallTest | |
155 @Feature({"AndroidWebView"}) | |
156 public void testPauseGeolocationOnPause() throws Throwable { | |
157 // Start a watch going. | |
158 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
159 RAW_HTML, "text/html", false); | |
160 | |
161 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
162 | |
163 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
164 @Override | |
165 public boolean isSatisfied() { | |
166 return getPositionCountFromJS() > 3; | |
167 } | |
168 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
169 | |
170 ensureGeolocationRunning(true); | |
171 | |
172 getInstrumentation().runOnMainSync(new Runnable() { | |
173 @Override | |
174 public void run() { | |
175 mAwContents.onPause(); | |
176 } | |
177 }); | |
178 | |
179 ensureGeolocationRunning(false); | |
180 | |
181 try { | |
182 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, "pos itionCount = 0"); | |
183 } catch (Exception e) { | |
184 fail("Unable to clear positionCount"); | |
185 } | |
186 assertEquals(0, getPositionCountFromJS()); | |
187 | |
188 getInstrumentation().runOnMainSync(new Runnable() { | |
189 @Override | |
190 public void run() { | |
191 mAwContents.onResume(); | |
192 } | |
193 }); | |
194 | |
195 ensureGeolocationRunning(true); | |
196 | |
197 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
198 @Override | |
199 public boolean isSatisfied() { | |
200 return getPositionCountFromJS() > 3; | |
201 } | |
202 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
203 } | |
204 | |
205 @SmallTest | |
206 @Feature({"AndroidWebView"}) | |
207 public void testPauseAwContentsBeforeNavigating() throws Throwable { | |
208 getInstrumentation().runOnMainSync(new Runnable() { | |
209 @Override | |
210 public void run() { | |
211 mAwContents.onPause(); | |
212 } | |
213 }); | |
214 | |
215 // Start a watch going. | |
216 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
217 RAW_HTML, "text/html", false); | |
218 | |
219 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
220 | |
221 assertEquals(0, getPositionCountFromJS()); | |
222 | |
223 ensureGeolocationRunning(false); | |
224 | |
225 getInstrumentation().runOnMainSync(new Runnable() { | |
226 @Override | |
227 public void run() { | |
228 mAwContents.onResume(); | |
229 } | |
230 }); | |
231 | |
232 ensureGeolocationRunning(true); | |
233 | |
234 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
235 @Override | |
236 public boolean isSatisfied() { | |
237 return getPositionCountFromJS() > 3; | |
238 } | |
239 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
240 | |
241 } | |
242 | |
243 @SmallTest | |
244 @Feature({"AndroidWebView"}) | |
245 public void testResumeWhenNotStarted() throws Throwable { | |
246 getInstrumentation().runOnMainSync(new Runnable() { | |
247 @Override | |
248 public void run() { | |
249 mAwContents.onPause(); | |
250 } | |
251 }); | |
252 | |
253 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
254 RAW_HTML, "text/html", false); | |
255 | |
256 getInstrumentation().runOnMainSync(new Runnable() { | |
257 @Override | |
258 public void run() { | |
259 mAwContents.onResume(); | |
260 } | |
261 }); | |
262 | |
263 ensureGeolocationRunning(false); | |
264 } | |
265 | |
266 } | |
OLD | NEW |