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.
5 package org.chromium.content.browser;
7 import android.test.suitebuilder.annotation.MediumTest;
9 import org.chromium.base.test.util.Feature;
10 import org.chromium.content.browser.test.util.Criteria;
11 import org.chromium.content.browser.test.util.CriteriaHelper;
12 import org.chromium.content.browser.test.util.MockLocationProvider;
13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
14 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
15 import org.chromium.content_public.browser.LoadUrlParams;
16 import org.chromium.content_shell_apk.ContentShellTestBase;
19 * Test suite for ensureing that Geolocation interacts as expected
20 * with ContentView APIs - e.g. that it's started and stopped as the
21 * ContentView is hidden or shown.
23 public class ContentViewLocationTest extends ContentShellTestBase {
25 private TestCallbackHelperContainer mTestCallbackHelperContainer;
26 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper;
27 private MockLocationProvider mMockLocationProvider;
29 private void hideContentViewOnUiThread() {
30 getInstrumentation().runOnMainSync(new Runnable() {
33 getContentViewCore().onHide();
38 private void showContentViewOnUiThread() {
39 getInstrumentation().runOnMainSync(new Runnable() {
42 getContentViewCore().onShow();
47 private void pollForPositionCallback() throws Throwable {
48 mJavascriptHelper.evaluateJavaScript(getWebContents(),
50 mJavascriptHelper.waitUntilHasValue();
51 assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()));
53 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
55 public boolean isSatisfied() {
56 mJavascriptHelper.evaluateJavaScript(getWebContents(), "positionCount");
58 mJavascriptHelper.waitUntilHasValue();
59 } catch (Exception e) {
62 return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0;
67 private void startGeolocationWatchPosition() throws Throwable {
68 mJavascriptHelper.evaluateJavaScript(getWebContents(),
69 "initiate_watchPosition();");
70 mJavascriptHelper.waitUntilHasValue();
73 private void ensureGeolocationRunning(final boolean running) throws Exception {
74 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
76 public boolean isSatisfied() {
77 return mMockLocationProvider.isRunning() == running;
83 protected void setUp() throws Exception {
86 mMockLocationProvider = new MockLocationProvider();
87 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider);
90 startActivityWithTestUrl("content/geolocation.html");
91 } catch (Throwable t) {
95 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentViewCore());
96 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper();
98 ensureGeolocationRunning(false);
102 protected void tearDown() throws Exception {
103 mMockLocationProvider.stopUpdates();
108 @Feature({"Location"})
109 public void testWatchHideShowStop() throws Throwable {
111 startGeolocationWatchPosition();
112 pollForPositionCallback();
113 ensureGeolocationRunning(true);
115 // Now hide the ContentView and ensure that geolocation stops.
116 hideContentViewOnUiThread();
117 ensureGeolocationRunning(false);
119 mJavascriptHelper.evaluateJavaScript(getWebContents(),
120 "positionCount = 0");
121 mJavascriptHelper.waitUntilHasValue();
123 // Show the ContentView again and ensure that geolocation starts again.
124 showContentViewOnUiThread();
125 pollForPositionCallback();
126 ensureGeolocationRunning(true);
128 // Navigate away and ensure that geolocation stops.
129 loadUrl(getContentViewCore().getWebContents().getNavigationController(),
130 mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
131 ensureGeolocationRunning(false);
135 @Feature({"Location"})
136 public void testHideWatchResume() throws Throwable {
137 hideContentViewOnUiThread();
138 startGeolocationWatchPosition();
139 ensureGeolocationRunning(false);
141 showContentViewOnUiThread();
142 pollForPositionCallback();
143 ensureGeolocationRunning(true);
147 @Feature({"Location"})
148 public void testWatchHideNewWatchShow() throws Throwable {
149 startGeolocationWatchPosition();
150 pollForPositionCallback();
151 ensureGeolocationRunning(true);
153 hideContentViewOnUiThread();
155 // Make sure that when starting a new watch while paused we still don't
156 // start up geolocation until we show the content view again.
157 startGeolocationWatchPosition();
158 ensureGeolocationRunning(false);
160 showContentViewOnUiThread();
161 pollForPositionCallback();
162 ensureGeolocationRunning(true);
166 @Feature({"Location"})
167 public void testHideWatchStopShow() throws Throwable {
168 hideContentViewOnUiThread();
169 startGeolocationWatchPosition();
170 ensureGeolocationRunning(false);
172 loadUrl(getContentViewCore().getWebContents().getNavigationController(),
173 mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
174 showContentViewOnUiThread();
175 ensureGeolocationRunning(false);