| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 package org.chromium.content.browser; | |
| 6 | |
| 7 import android.test.suitebuilder.annotation.MediumTest; | |
| 8 | |
| 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_shell_apk.ContentShellTestBase; | |
| 15 | |
| 16 /** | |
| 17 * Test suite for ensureing that Geolocation interacts as expected | |
| 18 * with ContentView APIs - e.g. that it's started and stopped as the | |
| 19 * ContentView is hidden or shown. | |
| 20 */ | |
| 21 public class ContentViewLocationTest extends ContentShellTestBase { | |
| 22 | |
| 23 private TestCallbackHelperContainer mTestCallbackHelperContainer; | |
| 24 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavasc
riptHelper; | |
| 25 private MockLocationProvider mMockLocationProvider; | |
| 26 | |
| 27 private void hideContentViewOnUiThread() { | |
| 28 getInstrumentation().runOnMainSync(new Runnable() { | |
| 29 @Override | |
| 30 public void run() { | |
| 31 getContentView().onHide(); | |
| 32 } | |
| 33 }); | |
| 34 } | |
| 35 | |
| 36 private void showContentViewOnUiThread() { | |
| 37 getInstrumentation().runOnMainSync(new Runnable() { | |
| 38 @Override | |
| 39 public void run() { | |
| 40 getContentView().onShow(); | |
| 41 } | |
| 42 }); | |
| 43 } | |
| 44 | |
| 45 private void pollForPositionCallback() throws Throwable { | |
| 46 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), | |
| 47 "positionCount = 0"); | |
| 48 mJavascriptHelper.waitUntilHasValue(); | |
| 49 assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear
())); | |
| 50 | |
| 51 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
| 52 @Override | |
| 53 public boolean isSatisfied() { | |
| 54 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), "
positionCount"); | |
| 55 try { | |
| 56 mJavascriptHelper.waitUntilHasValue(); | |
| 57 } catch (Exception e) { | |
| 58 fail(); | |
| 59 } | |
| 60 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl
ear()) > 0; | |
| 61 } | |
| 62 })); | |
| 63 } | |
| 64 | |
| 65 private void startGeolocationWatchPosition() throws Throwable { | |
| 66 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), | |
| 67 "initiate_watchPosition();"); | |
| 68 mJavascriptHelper.waitUntilHasValue(); | |
| 69 } | |
| 70 | |
| 71 private void ensureGeolocationRunning(final boolean running) throws Exceptio
n { | |
| 72 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
| 73 @Override | |
| 74 public boolean isSatisfied() { | |
| 75 return mMockLocationProvider.isRunning() == running; | |
| 76 } | |
| 77 })); | |
| 78 } | |
| 79 | |
| 80 @Override | |
| 81 protected void setUp() throws Exception { | |
| 82 super.setUp(); | |
| 83 | |
| 84 mMockLocationProvider = new MockLocationProvider(); | |
| 85 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); | |
| 86 | |
| 87 try { | |
| 88 startActivityWithTestUrl("content/geolocation.html"); | |
| 89 } catch (Throwable t) { | |
| 90 fail(); | |
| 91 } | |
| 92 | |
| 93 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getConten
tView()); | |
| 94 mJavascriptHelper = mTestCallbackHelperContainer.getOnEvaluateJavaScript
ResultHelper(); | |
| 95 | |
| 96 ensureGeolocationRunning(false); | |
| 97 } | |
| 98 | |
| 99 @Override | |
| 100 protected void tearDown() throws Exception { | |
| 101 mMockLocationProvider.stopUpdates(); | |
| 102 super.tearDown(); | |
| 103 } | |
| 104 | |
| 105 @MediumTest | |
| 106 @Feature({"Location"}) | |
| 107 public void testWatchHideShowStop() throws Throwable { | |
| 108 | |
| 109 startGeolocationWatchPosition(); | |
| 110 pollForPositionCallback(); | |
| 111 ensureGeolocationRunning(true); | |
| 112 | |
| 113 // Now hide the ContentView and ensure that geolocation stops. | |
| 114 hideContentViewOnUiThread(); | |
| 115 ensureGeolocationRunning(false); | |
| 116 | |
| 117 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), | |
| 118 "positionCount = 0"); | |
| 119 mJavascriptHelper.waitUntilHasValue(); | |
| 120 | |
| 121 // Show the ContentView again and ensure that geolocation starts again. | |
| 122 showContentViewOnUiThread(); | |
| 123 pollForPositionCallback(); | |
| 124 ensureGeolocationRunning(true); | |
| 125 | |
| 126 // Navigate away and ensure that geolocation stops. | |
| 127 loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParam
s("about:blank")); | |
| 128 ensureGeolocationRunning(false); | |
| 129 } | |
| 130 | |
| 131 @MediumTest | |
| 132 @Feature({"Location"}) | |
| 133 public void testHideWatchResume() throws Throwable { | |
| 134 hideContentViewOnUiThread(); | |
| 135 startGeolocationWatchPosition(); | |
| 136 ensureGeolocationRunning(false); | |
| 137 | |
| 138 showContentViewOnUiThread(); | |
| 139 pollForPositionCallback(); | |
| 140 ensureGeolocationRunning(true); | |
| 141 } | |
| 142 | |
| 143 @MediumTest | |
| 144 @Feature({"Location"}) | |
| 145 public void testWatchHideNewWatchShow() throws Throwable { | |
| 146 startGeolocationWatchPosition(); | |
| 147 pollForPositionCallback(); | |
| 148 ensureGeolocationRunning(true); | |
| 149 | |
| 150 hideContentViewOnUiThread(); | |
| 151 | |
| 152 // Make sure that when starting a new watch while paused we still don't | |
| 153 // start up geolocation until we show the content view again. | |
| 154 startGeolocationWatchPosition(); | |
| 155 ensureGeolocationRunning(false); | |
| 156 | |
| 157 showContentViewOnUiThread(); | |
| 158 pollForPositionCallback(); | |
| 159 ensureGeolocationRunning(true); | |
| 160 } | |
| 161 | |
| 162 @MediumTest | |
| 163 @Feature({"Location"}) | |
| 164 public void testHideWatchStopShow() throws Throwable { | |
| 165 hideContentViewOnUiThread(); | |
| 166 startGeolocationWatchPosition(); | |
| 167 ensureGeolocationRunning(false); | |
| 168 | |
| 169 loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParam
s("about:blank")); | |
| 170 showContentViewOnUiThread(); | |
| 171 ensureGeolocationRunning(false); | |
| 172 } | |
| 173 } | |
| OLD | NEW |