Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2239)

Unified Diff: android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java

Issue 65273002: Add a mechanism to pause and resume geolocation requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new test case, fix a bug :) Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..4551c91895ab8b3372b596438bc4138c811114f6
--- /dev/null
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java
@@ -0,0 +1,251 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.android_webview.test;
+
+import android.content.Context;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.os.SystemClock;
+import android.webkit.GeolocationPermissions;
+
+import org.chromium.android_webview.AwContents;
+import org.chromium.base.test.util.Feature;
+import org.chromium.content.browser.LocationProvider;
+import org.chromium.content.browser.test.util.Criteria;
+import org.chromium.content.browser.test.util.CriteriaHelper;
+
+import java.util.List;
+
+public class GeolocationTest extends AwTestBase {
+
+ private static final long TEST_TIMEOUT = 20000L;
+ private static final int CHECK_INTERVAL = 100;
+
+ private TestAwContentsClient mContentsClient;
+ private AwContents mAwContents;
+
+ private static final String RAW_HTML =
+ "<!DOCTYPE html>\n" +
+ "<html>\n" +
+ " <head>\n" +
+ " <title>Geolocation</title>\n" +
+ " <script>\n" +
+ " var positionCount = 0;\n" +
+ " function gotPos(position) {\n" +
+ " positionCount++;\n" +
+ " }\n" +
+ " function initiate_getCurrentPosition() {\n" +
+ " navigator.geolocation.getCurrentPosition(\n" +
+ " gotPos, function() { }, { });\n" +
+ " }\n" +
+ " function initiate_watchPosition() {\n" +
+ " navigator.geolocation.watchPosition(\n" +
+ " gotPos, function() { }, { });\n" +
+ " }\n" +
+ " </script>\n" +
+ " </head>\n" +
+ " <body>\n" +
+ " </body>\n" +
+ "</html>";
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ mContentsClient = new TestAwContentsClient() {
+ @Override
+ public void onGeolocationPermissionsShowPrompt(String origin,
+ GeolocationPermissions.Callback callback) {
+ callback.invoke(origin, true, true);
+ }
+ };
+ mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAwContents();
+ enableJavaScriptOnUiThread(mAwContents);
+ setupGeolocation();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ GeolocationPermissions.getInstance().clearAll();
+ super.tearDown();
+ }
+
+ private void setupGeolocation() {
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.getSettings().setGeolocationEnabled(true);
+ }
+ });
+ LocationProvider.enableMockLocationProvider();
+ }
+
+ private int getPositionCountFromJS() {
+ int result = -1;
+ try {
+ result = Integer.parseInt(executeJavaScriptAndWaitForResult(
+ mAwContents, mContentsClient, "positionCount"));
+ } catch (Exception e) {
+ fail("Unable to get positionCount");
+ }
+ return result;
+ }
+
+ /**
+ * Ensure that a call to navigator.getCurrentPosition works in WebView.
+ */
+ @SmallTest
+ @Feature({"AndroidWebView"})
+ public void testGetPosition() throws Throwable {
+ loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
+ RAW_HTML, "text/html", false);
+
+ mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null);
+
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() == 1;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+
+ mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null);
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() == 2;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+ }
+
+ /**
+ * Ensure that a call to navigator.watchPosition works in WebView.
+ */
+ @SmallTest
+ @Feature({"AndroidWebView"})
+ public void testWatchPosition() throws Throwable {
+ loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
+ RAW_HTML, "text/html", false);
+
+ mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
+
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() > 3;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+ }
+
+ @SmallTest
+ @Feature({"AndroidWebView"})
+ public void testPauseGeolocationOnPause() throws Throwable {
+ // Start a watch going.
+ loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
+ RAW_HTML, "text/html", false);
+
+ mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
+
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() > 3;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+
+ assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onPause();
+ }
+ });
+
+ assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+
+ try {
+ executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, "positionCount = 0");
+ } catch (Exception e) {
+ fail("Unable to clear positionCount");
+ }
+ assertEquals(0, getPositionCountFromJS());
+
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onResume();
+ }
+ });
+
+ assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() > 3;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+ }
+
+ @SmallTest
+ @Feature({"AndroidWebView"})
+ public void testPauseAwContentsBeforeNavigating() throws Throwable {
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onPause();
+ }
+ });
+
+ // Start a watch going.
+ loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
+ RAW_HTML, "text/html", false);
+
+ mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
+
+ assertEquals(0, getPositionCountFromJS());
+ assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onResume();
+ }
+ });
+
+ assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getPositionCountFromJS() > 3;
+ }
+ }, TEST_TIMEOUT, CHECK_INTERVAL));
+
+ }
+
+ @SmallTest
+ @Feature({"AndroidWebView"})
+ public void testResumeWhenNotStarted() throws Throwable {
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onPause();
+ }
+ });
+
+ loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
+ RAW_HTML, "text/html", false);
+
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ mAwContents.onResume();
+ }
+ });
+
+ assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesting());
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698