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_shell_apk.ContentShellTestBase;
18 * Test suite for ensureing that Geolocation interacts as expected
19 * with ContentView APIs - e.g. that it's started and stopped as the
20 * ContentView is hidden or shown.
22 public class ContentViewLocationTest extends ContentShellTestBase {
24 private TestCallbackHelperContainer mTestCallbackHelperContainer;
25 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper;
26 private MockLocationProvider mMockLocationProvider;
28 private void hideContentViewOnUiThread() {
29 getInstrumentation().runOnMainSync(new Runnable() {
32 getContentViewCore().onHide();
37 private void showContentViewOnUiThread() {
38 getInstrumentation().runOnMainSync(new Runnable() {
41 getContentViewCore().onShow();
46 private void pollForPositionCallback() throws Throwable {
47 mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
49 mJavascriptHelper.waitUntilHasValue();
50 assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()));
52 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
54 public boolean isSatisfied() {
55 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), "positionCount");
57 mJavascriptHelper.waitUntilHasValue();
58 } catch (Exception e) {
61 return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0;
66 private void startGeolocationWatchPosition() throws Throwable {
67 mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
68 "initiate_watchPosition();");
69 mJavascriptHelper.waitUntilHasValue();
72 private void ensureGeolocationRunning(final boolean running) throws Exception {
73 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
75 public boolean isSatisfied() {
76 return mMockLocationProvider.isRunning() == running;
82 protected void setUp() throws Exception {
85 mMockLocationProvider = new MockLocationProvider();
86 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider);
89 startActivityWithTestUrl("content/geolocation.html");
90 } catch (Throwable t) {
94 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentViewCore());
95 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper();
97 ensureGeolocationRunning(false);
101 protected void tearDown() throws Exception {
102 mMockLocationProvider.stopUpdates();
107 @Feature({"Location"})
108 public void testWatchHideShowStop() throws Throwable {
110 startGeolocationWatchPosition();
111 pollForPositionCallback();
112 ensureGeolocationRunning(true);
114 // Now hide the ContentView and ensure that geolocation stops.
115 hideContentViewOnUiThread();
116 ensureGeolocationRunning(false);
118 mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
119 "positionCount = 0");
120 mJavascriptHelper.waitUntilHasValue();
122 // Show the ContentView again and ensure that geolocation starts again.
123 showContentViewOnUiThread();
124 pollForPositionCallback();
125 ensureGeolocationRunning(true);
127 // Navigate away and ensure that geolocation stops.
128 loadUrl(getContentViewCore(), mTestCallbackHelperContainer,
129 new LoadUrlParams("about:blank"));
130 ensureGeolocationRunning(false);
134 @Feature({"Location"})
135 public void testHideWatchResume() throws Throwable {
136 hideContentViewOnUiThread();
137 startGeolocationWatchPosition();
138 ensureGeolocationRunning(false);
140 showContentViewOnUiThread();
141 pollForPositionCallback();
142 ensureGeolocationRunning(true);
146 @Feature({"Location"})
147 public void testWatchHideNewWatchShow() throws Throwable {
148 startGeolocationWatchPosition();
149 pollForPositionCallback();
150 ensureGeolocationRunning(true);
152 hideContentViewOnUiThread();
154 // Make sure that when starting a new watch while paused we still don't
155 // start up geolocation until we show the content view again.
156 startGeolocationWatchPosition();
157 ensureGeolocationRunning(false);
159 showContentViewOnUiThread();
160 pollForPositionCallback();
161 ensureGeolocationRunning(true);
165 @Feature({"Location"})
166 public void testHideWatchStopShow() throws Throwable {
167 hideContentViewOnUiThread();
168 startGeolocationWatchPosition();
169 ensureGeolocationRunning(false);
171 loadUrl(getContentViewCore(), mTestCallbackHelperContainer,
172 new LoadUrlParams("about:blank"));
173 showContentViewOnUiThread();
174 ensureGeolocationRunning(false);