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