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.location.Location; | |
9 import android.location.LocationManager; | |
10 import android.location.LocationProvider; | |
11 import android.test.suitebuilder.annotation.SmallTest; | |
12 import android.os.SystemClock; | |
13 import android.webkit.GeolocationPermissions; | |
14 | |
15 import org.chromium.android_webview.AwContents; | |
16 import org.chromium.base.test.util.Feature; | |
17 import org.chromium.content.browser.test.util.Criteria; | |
18 import org.chromium.content.browser.test.util.CriteriaHelper; | |
19 | |
20 import java.util.List; | |
21 | |
22 public class GeolocationTest extends AwTestBase { | |
23 | |
24 private static final long TEST_TIMEOUT = 20000L; | |
25 private static final int CHECK_INTERVAL = 100; | |
26 | |
27 private TestAwContentsClient mContentsClient; | |
28 private AwContents mAwContents; | |
29 | |
30 private static final String RAW_HTML = | |
mkosiba (inactive)
2013/12/02 10:49:31
nit, minimally shorter:
CommonResources.makeHtml
| |
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 private LocationManager mLocationManager; | |
55 private List<String> mProviders; | |
56 | |
57 @Override | |
58 public void setUp() throws Exception { | |
59 super.setUp(); | |
60 mContentsClient = new TestAwContentsClient() { | |
61 @Override | |
62 public void onGeolocationPermissionsShowPrompt(String origin, | |
63 GeolocationPermissions.Callback callback) { | |
64 callback.invoke(origin, true, true); | |
65 } | |
66 }; | |
67 mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAw Contents(); | |
68 enableJavaScriptOnUiThread(mAwContents); | |
69 setupGeolocation(); | |
70 } | |
71 | |
72 @Override | |
73 public void tearDown() throws Exception { | |
74 for (String provider : mProviders) { | |
75 try { | |
76 mLocationManager.clearTestProviderEnabled(provider); | |
77 mLocationManager.removeTestProvider(provider); | |
78 } catch (IllegalArgumentException e) {} | |
79 } | |
80 GeolocationPermissions.getInstance().clearAll(); | |
81 super.tearDown(); | |
82 } | |
83 | |
84 private void setupGeolocation() { | |
85 getInstrumentation().runOnMainSync(new Runnable() { | |
86 @Override | |
87 public void run() { | |
88 mAwContents.getSettings().setGeolocationEnabled(true); | |
89 } | |
90 }); | |
91 mLocationManager = (LocationManager)getActivity().getApplicationContext( ) | |
92 .getSystemService(Context.LOCATION_SERVICE); | |
93 mProviders = mLocationManager.getAllProviders(); | |
94 for (String provider : mProviders) { | |
95 // Can't mock passive provider. | |
96 if (provider.equals(LocationManager.PASSIVE_PROVIDER)) { | |
97 mProviders.remove(provider); | |
98 break; | |
99 } | |
100 } | |
101 for (String providerName : mProviders) { | |
102 LocationProvider provider = mLocationManager.getProvider(providerNam e); | |
103 mLocationManager.addTestProvider(provider.getName(), | |
104 provider.requiresNetwork(), //requiresNetwork, | |
105 provider.requiresSatellite(), // requiresSatellite, | |
106 provider.requiresCell(), // requiresCell, | |
107 provider.hasMonetaryCost(), // hasMonetaryCost, | |
108 provider.supportsAltitude(), // supportsAltitude, | |
109 provider.supportsSpeed(), // supportsSpeed, | |
110 provider.supportsBearing(), // supportsBearing, | |
111 provider.getPowerRequirement(), // powerRequirement | |
112 provider.getAccuracy()); // accuracy | |
113 mLocationManager.setTestProviderEnabled(provider.getName(), true); | |
114 } | |
115 } | |
116 | |
117 private void pushMockLocation() { | |
118 for (int i = 0; i < mProviders.size(); i++) { | |
119 Location location = new Location(mProviders.get(i)); | |
120 location.setLatitude(40); | |
121 location.setLongitude(40); | |
122 location.setAccuracy(1.0f); | |
123 location.setTime(java.lang.System.currentTimeMillis()); | |
124 location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()) ; | |
125 mLocationManager.setTestProviderLocation(mProviders.get(i), location ); | |
126 } | |
127 } | |
128 | |
129 private int getPositionCountFromJS() { | |
130 int result = -1; | |
131 try { | |
132 result = Integer.parseInt(executeJavaScriptAndWaitForResult( | |
133 mAwContents, mContentsClient, "positionCount")); | |
134 } catch (Exception e) { | |
135 fail("Unable to get positionCount"); | |
136 } | |
137 return result; | |
138 } | |
139 | |
140 /** | |
141 * Ensure that a call to navigator.getCurrentPosition works in WebView. | |
142 */ | |
143 @SmallTest | |
144 @Feature({"AndroidWebView"}) | |
145 public void testGetPosition() throws Throwable { | |
146 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
147 RAW_HTML, "text/html", false); | |
148 | |
149 mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); | |
150 pushMockLocation(); | |
151 | |
152 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
153 @Override | |
154 public boolean isSatisfied() { | |
155 return getPositionCountFromJS() == 1; | |
156 } | |
157 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
158 | |
159 mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); | |
160 pushMockLocation(); | |
161 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
162 @Override | |
163 public boolean isSatisfied() { | |
164 return getPositionCountFromJS() == 2; | |
165 } | |
166 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
167 } | |
168 | |
169 /** | |
170 * Ensure that a call to navigator.watchPosition works in WebView. | |
171 */ | |
172 @SmallTest | |
173 @Feature({"AndroidWebView"}) | |
174 public void testWatchPosition() throws Throwable { | |
175 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
176 RAW_HTML, "text/html", false); | |
177 | |
178 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
179 | |
180 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
181 @Override | |
182 public boolean isSatisfied() { | |
183 pushMockLocation(); | |
184 return getPositionCountFromJS() > 3; | |
185 } | |
186 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
187 } | |
188 | |
189 @SmallTest | |
190 @Feature({"AndroidWebView"}) | |
191 public void testPauseGeolocationOnPause() throws Throwable { | |
192 // Start a watch going. | |
193 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
194 RAW_HTML, "text/html", false); | |
195 | |
196 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
197 | |
198 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
199 @Override | |
200 public boolean isSatisfied() { | |
201 pushMockLocation(); | |
202 return getPositionCountFromJS() > 3; | |
203 } | |
204 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
205 | |
206 assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTestin g()); | |
207 | |
208 getInstrumentation().runOnMainSync(new Runnable() { | |
209 @Override | |
210 public void run() { | |
211 mAwContents.onPause(); | |
212 } | |
213 }); | |
214 | |
215 assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesti ng()); | |
216 | |
217 try { | |
218 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, "pos itionCount = 0"); | |
219 } catch (Exception e) { | |
220 fail("Unable to clear positionCount"); | |
221 } | |
222 assertEquals(0, getPositionCountFromJS()); | |
223 | |
224 getInstrumentation().runOnMainSync(new Runnable() { | |
225 @Override | |
226 public void run() { | |
227 mAwContents.onResume(); | |
228 } | |
229 }); | |
230 | |
231 assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTestin g()); | |
232 | |
233 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
234 @Override | |
235 public boolean isSatisfied() { | |
236 pushMockLocation(); | |
237 return getPositionCountFromJS() > 3; | |
238 } | |
239 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
240 } | |
241 | |
242 @SmallTest | |
243 @Feature({"AndroidWebView"}) | |
244 public void testPauseAwContentsBeforeNavigating() throws Throwable { | |
245 getInstrumentation().runOnMainSync(new Runnable() { | |
246 @Override | |
247 public void run() { | |
248 mAwContents.onPause(); | |
249 } | |
250 }); | |
251 | |
252 // Start a watch going. | |
253 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), | |
254 RAW_HTML, "text/html", false); | |
255 | |
256 mAwContents.evaluateJavaScript("initiate_watchPosition();", null); | |
257 | |
258 assertEquals(0, getPositionCountFromJS()); | |
259 assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesti ng()); | |
260 | |
261 getInstrumentation().runOnMainSync(new Runnable() { | |
262 @Override | |
263 public void run() { | |
264 mAwContents.onResume(); | |
265 } | |
266 }); | |
267 | |
268 assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTestin g()); | |
269 | |
270 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
271 @Override | |
272 public boolean isSatisfied() { | |
273 pushMockLocation(); | |
274 return getPositionCountFromJS() > 3; | |
275 } | |
276 }, TEST_TIMEOUT, CHECK_INTERVAL)); | |
277 | |
278 } | |
279 } | |
OLD | NEW |