Chromium Code Reviews| 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..b901f1fb29961e602f124a2aea328ca620cf6935 |
| --- /dev/null |
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/ContentViewLocationTest.java |
| @@ -0,0 +1,159 @@ |
| +// 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.MockLocationProvider; |
| +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 MockLocationProvider mMockLocationProvider; |
| + |
| + 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(); |
| + } |
| + |
| + private void ensureGeolocationRunning(final boolean running) throws Exception { |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return mMockLocationProvider.isRunning() == running; |
| + } |
| + })); |
| + } |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + |
| + mMockLocationProvider = new MockLocationProvider(); |
| + LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); |
| + |
| + try { |
| + startActivityWithTestUrl("content/geolocation.html"); |
| + } catch (Throwable t) { |
| + fail(); |
| + } |
| + |
| + mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentView()); |
| + mJavascriptHelper = mTestCallbackHelperContainer.getOnEvaluateJavaScriptResultHelper(); |
| + |
| + ensureGeolocationRunning(false); |
| + } |
| + |
| + @Override |
| + protected void tearDown() throws Exception { |
| + mMockLocationProvider.stopUpdates(); |
| + super.tearDown(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Location"}) |
| + public void testWatchHideShowStop() throws Throwable { |
| + |
| + startGeolocationWatchPosition(); |
| + pollForPositionCallback(); |
| + ensureGeolocationRunning(true); |
| + |
| + // Now hide the ContentView and ensure that geolocation stops. |
| + hideContentViewOnUiThread(); |
| + ensureGeolocationRunning(false); |
| + |
| + mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
| + "positionCount = 0"); |
| + mJavascriptHelper.waitUntilHasValue(); |
| + |
| + // Show the ContentView again and ensure that geolocation starts again. |
| + showContentViewOnUiThread(); |
| + pollForPositionCallback(); |
| + ensureGeolocationRunning(true); |
| + |
| + // Navigate away and ensure that geolocation stops. |
| + loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); |
| + ensureGeolocationRunning(false); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Location"}) |
| + public void testHideWatchResume() throws Throwable { |
| + hideContentViewOnUiThread(); |
| + startGeolocationWatchPosition(); |
| + ensureGeolocationRunning(false); |
| + |
| + showContentViewOnUiThread(); |
| + pollForPositionCallback(); |
| + ensureGeolocationRunning(true); |
| + } |
| + |
|
bulach
2013/12/11 15:48:39
nit: maybe @SmallTest / @Feature
benm (inactive)
2013/12/11 20:08:51
Done.
|
| + public void testWatchHideNewWatchShow() throws Throwable { |
|
mkosiba (inactive)
2013/12/11 15:47:44
Good tests! I think the only combination you didn'
benm (inactive)
2013/12/11 20:08:51
Thanks!
good idea, added.
|
| + startGeolocationWatchPosition(); |
| + pollForPositionCallback(); |
| + ensureGeolocationRunning(true); |
| + |
| + 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(); |
| + ensureGeolocationRunning(false); |
| + |
| + showContentViewOnUiThread(); |
| + pollForPositionCallback(); |
| + ensureGeolocationRunning(true); |
| + } |
| + |
| + |
|
mkosiba (inactive)
2013/12/11 15:47:44
uber-nit: extra newline
benm (inactive)
2013/12/11 20:08:51
Done.
|
| +} |