Index: content/public/android/javatests/src/org/chromium/content/browser/ContentViewLocationTest.java |
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/ContentViewLocationTest.java b/content/public/android/javatests/src/org/chromium/content/browser/ContentViewLocationTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cac844d1a00d3cb51f01d2742dcf5e78b28ecfef |
--- /dev/null |
+++ b/content/public/android/javatests/src/org/chromium/content/browser/ContentViewLocationTest.java |
@@ -0,0 +1,150 @@ |
+// 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.content.browser; |
+ |
+import android.test.UiThreadTest; |
+import android.test.suitebuilder.annotation.SmallTest; |
+ |
+import org.chromium.base.test.util.Feature; |
+import org.chromium.base.test.util.UrlUtils; |
+import org.chromium.content.browser.LocationProvider; |
+import org.chromium.content.browser.test.util.Criteria; |
+import org.chromium.content.browser.test.util.CriteriaHelper; |
+import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
+import org.chromium.content_shell_apk.ContentShellTestBase; |
+ |
+public class ContentViewLocationTest extends ContentShellTestBase { |
+ |
+ private TestCallbackHelperContainer mTestCallbackHelperContainer; |
+ private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper; |
+ |
+ private void hideContentViewOnUiThread() { |
+ getInstrumentation().runOnMainSync(new Runnable() { |
+ @Override |
+ public void run() { |
+ getContentView().onHide(); |
+ } |
+ }); |
+ } |
+ |
+ private void showContentViewOnUiThread() { |
+ getInstrumentation().runOnMainSync(new Runnable() { |
+ @Override |
+ public void run() { |
+ getContentView().onShow(); |
+ } |
+ }); |
+ } |
+ |
+ private void pollForPositionCallback() throws Throwable{ |
+ mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
+ "positionCount = 0"); |
+ mJavascriptHelper.waitUntilHasValue(); |
+ assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear())); |
+ |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ mJavascriptHelper.evaluateJavaScript(getContentViewCore(), "positionCount"); |
+ try { |
+ mJavascriptHelper.waitUntilHasValue(); |
+ } catch (Exception e) { |
+ fail(); |
+ } |
+ return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0; |
+ } |
+ })); |
+ } |
+ |
+ private void startGeolocationWatchPosition() throws Throwable { |
+ mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
+ "initiate_watchPosition();"); |
+ mJavascriptHelper.waitUntilHasValue(); |
+ } |
+ |
+ @Override |
+ protected void setUp() throws Exception { |
+ super.setUp(); |
+ |
+ LocationProvider.enableMockLocationProvider(); |
+ |
+ try { |
+ startActivityWithTestUrl("content/geolocation.html"); |
+ } catch (Throwable t) { |
+ fail(); |
+ } |
+ |
+ mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentView()); |
+ mJavascriptHelper = mTestCallbackHelperContainer.getOnEvaluateJavaScriptResultHelper(); |
+ |
+ assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Location"}) |
+ public void testWatchHideShowStop() throws Throwable { |
+ |
+ startGeolocationWatchPosition(); |
+ pollForPositionCallback(); |
+ assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
+ |
+ // Now hide the ContentView and ensure that geolocation stops. |
+ hideContentViewOnUiThread(); |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return !getContentViewCore().isGeolocationActiveForTesting(); |
+ } |
+ })); |
+ |
+ mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
+ "positionCount = 0"); |
+ mJavascriptHelper.waitUntilHasValue(); |
+ |
+ // Show the ContentView again and ensure that geolocation starts again. |
+ showContentViewOnUiThread(); |
+ pollForPositionCallback(); |
+ assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
+ |
+ // Navigate away and ensure that geolocation stops. |
+ loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); |
+ assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Location"}) |
+ public void testHideWatchResume() throws Throwable { |
+ hideContentViewOnUiThread(); |
+ startGeolocationWatchPosition(); |
+ assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
+ |
+ showContentViewOnUiThread(); |
+ pollForPositionCallback(); |
+ assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
+ } |
+ |
+ public void testWatchHideNewWatchShow() throws Throwable { |
+ startGeolocationWatchPosition(); |
+ pollForPositionCallback(); |
+ assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
+ hideContentViewOnUiThread(); |
+ |
+ // Make sure that when starting a new watch while paused we still don't |
+ // start up geolocation until we show the content view again. |
+ startGeolocationWatchPosition(); |
+ assertFalse(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return getContentViewCore().isGeolocationActiveForTesting(); |
+ } |
+ })); |
+ |
+ showContentViewOnUiThread(); |
+ pollForPositionCallback(); |
+ assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
+ } |
+ |
+ |
+} |