| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.support.test.InstrumentationRegistry; |
| 7 import android.support.test.filters.MediumTest; | 8 import android.support.test.filters.MediumTest; |
| 8 | 9 |
| 10 import org.junit.After; |
| 11 import org.junit.Assert; |
| 12 import org.junit.Before; |
| 13 import org.junit.Rule; |
| 14 import org.junit.Test; |
| 15 import org.junit.runner.RunWith; |
| 16 |
| 17 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 9 import org.chromium.base.test.util.Feature; | 18 import org.chromium.base.test.util.Feature; |
| 10 import org.chromium.content.browser.test.util.Criteria; | 19 import org.chromium.content.browser.test.util.Criteria; |
| 11 import org.chromium.content.browser.test.util.CriteriaHelper; | 20 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; | 22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; |
| 14 import org.chromium.content_public.browser.LoadUrlParams; | 23 import org.chromium.content_public.browser.LoadUrlParams; |
| 15 import org.chromium.content_shell_apk.ContentShellTestBase; | 24 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
| 16 import org.chromium.device.geolocation.LocationProviderFactory; | 25 import org.chromium.device.geolocation.LocationProviderFactory; |
| 17 import org.chromium.device.geolocation.MockLocationProvider; | 26 import org.chromium.device.geolocation.MockLocationProvider; |
| 18 | 27 |
| 19 import java.util.concurrent.Callable; | 28 import java.util.concurrent.Callable; |
| 20 | 29 |
| 21 /** | 30 /** |
| 22 * Test suite for ensureing that Geolocation interacts as expected | 31 * Test suite for ensureing that Geolocation interacts as expected |
| 23 * with ContentView APIs - e.g. that it's started and stopped as the | 32 * with ContentView APIs - e.g. that it's started and stopped as the |
| 24 * ContentView is hidden or shown. | 33 * ContentView is hidden or shown. |
| 25 */ | 34 */ |
| 26 public class ContentViewLocationTest extends ContentShellTestBase { | 35 @RunWith(BaseJUnit4ClassRunner.class) |
| 36 public class ContentViewLocationTest { |
| 37 @Rule |
| 38 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 27 | 39 |
| 28 private TestCallbackHelperContainer mTestCallbackHelperContainer; | 40 private TestCallbackHelperContainer mTestCallbackHelperContainer; |
| 29 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavasc
riptHelper; | 41 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavasc
riptHelper; |
| 30 private MockLocationProvider mMockLocationProvider; | 42 private MockLocationProvider mMockLocationProvider; |
| 31 | 43 |
| 32 private void hideContentViewOnUiThread() { | 44 private void hideContentViewOnUiThread() { |
| 33 getInstrumentation().runOnMainSync(new Runnable() { | 45 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
| 34 @Override | 46 @Override |
| 35 public void run() { | 47 public void run() { |
| 36 getContentViewCore().onHide(); | 48 mActivityTestRule.getContentViewCore().onHide(); |
| 37 } | 49 } |
| 38 }); | 50 }); |
| 39 } | 51 } |
| 40 | 52 |
| 41 private void showContentViewOnUiThread() { | 53 private void showContentViewOnUiThread() { |
| 42 getInstrumentation().runOnMainSync(new Runnable() { | 54 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
| 43 @Override | 55 @Override |
| 44 public void run() { | 56 public void run() { |
| 45 getContentViewCore().onShow(); | 57 mActivityTestRule.getContentViewCore().onShow(); |
| 46 } | 58 } |
| 47 }); | 59 }); |
| 48 } | 60 } |
| 49 | 61 |
| 50 private void pollForPositionCallback() throws Throwable { | 62 private void pollForPositionCallback() throws Throwable { |
| 51 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), | 63 mJavascriptHelper.evaluateJavaScriptForTests( |
| 52 "positionCount = 0"); | 64 mActivityTestRule.getWebContents(), "positionCount = 0"); |
| 53 mJavascriptHelper.waitUntilHasValue(); | 65 mJavascriptHelper.waitUntilHasValue(); |
| 54 assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear
())); | 66 Assert.assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultA
ndClear())); |
| 55 | 67 |
| 56 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 68 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
| 57 @Override | 69 @Override |
| 58 public boolean isSatisfied() { | 70 public boolean isSatisfied() { |
| 59 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(
), "positionCount"); | 71 mJavascriptHelper.evaluateJavaScriptForTests( |
| 72 mActivityTestRule.getWebContents(), "positionCount")
; |
| 60 try { | 73 try { |
| 61 mJavascriptHelper.waitUntilHasValue(); | 74 mJavascriptHelper.waitUntilHasValue(); |
| 62 } catch (Exception e) { | 75 } catch (Exception e) { |
| 63 fail(); | 76 Assert.fail(); |
| 64 } | 77 } |
| 65 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl
ear()) > 0; | 78 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl
ear()) > 0; |
| 66 } | 79 } |
| 67 }); | 80 }); |
| 68 } | 81 } |
| 69 | 82 |
| 70 private void startGeolocationWatchPosition() throws Throwable { | 83 private void startGeolocationWatchPosition() throws Throwable { |
| 71 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), | 84 mJavascriptHelper.evaluateJavaScriptForTests( |
| 72 "initiate_watchPosition();"); | 85 mActivityTestRule.getWebContents(), "initiate_watchPosition();")
; |
| 73 mJavascriptHelper.waitUntilHasValue(); | 86 mJavascriptHelper.waitUntilHasValue(); |
| 74 } | 87 } |
| 75 | 88 |
| 76 private void ensureGeolocationRunning(final boolean running) throws Exceptio
n { | 89 private void ensureGeolocationRunning(final boolean running) throws Exceptio
n { |
| 77 CriteriaHelper.pollInstrumentationThread(Criteria.equals(running, new Ca
llable<Boolean>() { | 90 CriteriaHelper.pollInstrumentationThread(Criteria.equals(running, new Ca
llable<Boolean>() { |
| 78 @Override | 91 @Override |
| 79 public Boolean call() { | 92 public Boolean call() { |
| 80 return mMockLocationProvider.isRunning(); | 93 return mMockLocationProvider.isRunning(); |
| 81 } | 94 } |
| 82 })); | 95 })); |
| 83 } | 96 } |
| 84 | 97 |
| 85 @Override | 98 @Before |
| 86 protected void setUp() throws Exception { | 99 public void setUp() throws Exception { |
| 87 super.setUp(); | |
| 88 | |
| 89 mMockLocationProvider = new MockLocationProvider(); | 100 mMockLocationProvider = new MockLocationProvider(); |
| 90 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); | 101 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); |
| 91 | 102 |
| 92 try { | 103 try { |
| 93 startActivityWithTestUrl("content/test/data/android/geolocation.html
"); | 104 mActivityTestRule.launchContentShellWithUrlSync( |
| 105 "content/test/data/android/geolocation.html"); |
| 94 } catch (Throwable t) { | 106 } catch (Throwable t) { |
| 95 fail(); | 107 Assert.fail(); |
| 96 } | 108 } |
| 97 | 109 |
| 98 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getConten
tViewCore()); | 110 mTestCallbackHelperContainer = |
| 111 new TestCallbackHelperContainer(mActivityTestRule.getContentView
Core()); |
| 99 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper(); | 112 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper(); |
| 100 | 113 |
| 101 ensureGeolocationRunning(false); | 114 ensureGeolocationRunning(false); |
| 102 } | 115 } |
| 103 | 116 |
| 104 @Override | 117 @After |
| 105 protected void tearDown() throws Exception { | 118 public void tearDown() throws Exception { |
| 106 mMockLocationProvider.stopUpdates(); | 119 mMockLocationProvider.stopUpdates(); |
| 107 super.tearDown(); | |
| 108 } | 120 } |
| 109 | 121 |
| 122 @Test |
| 110 @MediumTest | 123 @MediumTest |
| 111 @Feature({"Location"}) | 124 @Feature({"Location"}) |
| 112 public void testWatchHideShowStop() throws Throwable { | 125 public void testWatchHideShowStop() throws Throwable { |
| 113 | |
| 114 startGeolocationWatchPosition(); | 126 startGeolocationWatchPosition(); |
| 115 pollForPositionCallback(); | 127 pollForPositionCallback(); |
| 116 ensureGeolocationRunning(true); | 128 ensureGeolocationRunning(true); |
| 117 | 129 |
| 118 // Now hide the ContentView and ensure that geolocation stops. | 130 // Now hide the ContentView and ensure that geolocation stops. |
| 119 hideContentViewOnUiThread(); | 131 hideContentViewOnUiThread(); |
| 120 ensureGeolocationRunning(false); | 132 ensureGeolocationRunning(false); |
| 121 | 133 |
| 122 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), | 134 mJavascriptHelper.evaluateJavaScriptForTests( |
| 123 "positionCount = 0"); | 135 mActivityTestRule.getWebContents(), "positionCount = 0"); |
| 124 mJavascriptHelper.waitUntilHasValue(); | 136 mJavascriptHelper.waitUntilHasValue(); |
| 125 | 137 |
| 126 // Show the ContentView again and ensure that geolocation starts again. | 138 // Show the ContentView again and ensure that geolocation starts again. |
| 127 showContentViewOnUiThread(); | 139 showContentViewOnUiThread(); |
| 128 pollForPositionCallback(); | 140 pollForPositionCallback(); |
| 129 ensureGeolocationRunning(true); | 141 ensureGeolocationRunning(true); |
| 130 | 142 |
| 131 // Navigate away and ensure that geolocation stops. | 143 // Navigate away and ensure that geolocation stops. |
| 132 loadUrl(getContentViewCore().getWebContents().getNavigationController(), | 144 mActivityTestRule.loadUrl( |
| 145 mActivityTestRule.getContentViewCore().getWebContents().getNavig
ationController(), |
| 133 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); | 146 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); |
| 134 ensureGeolocationRunning(false); | 147 ensureGeolocationRunning(false); |
| 135 } | 148 } |
| 136 | 149 |
| 150 @Test |
| 137 @MediumTest | 151 @MediumTest |
| 138 @Feature({"Location"}) | 152 @Feature({"Location"}) |
| 139 public void testHideWatchResume() throws Throwable { | 153 public void testHideWatchResume() throws Throwable { |
| 140 hideContentViewOnUiThread(); | 154 hideContentViewOnUiThread(); |
| 141 startGeolocationWatchPosition(); | 155 startGeolocationWatchPosition(); |
| 142 ensureGeolocationRunning(false); | 156 ensureGeolocationRunning(false); |
| 143 | 157 |
| 144 showContentViewOnUiThread(); | 158 showContentViewOnUiThread(); |
| 145 pollForPositionCallback(); | 159 pollForPositionCallback(); |
| 146 ensureGeolocationRunning(true); | 160 ensureGeolocationRunning(true); |
| 147 } | 161 } |
| 148 | 162 |
| 163 @Test |
| 149 @MediumTest | 164 @MediumTest |
| 150 @Feature({"Location"}) | 165 @Feature({"Location"}) |
| 151 public void testWatchHideNewWatchShow() throws Throwable { | 166 public void testWatchHideNewWatchShow() throws Throwable { |
| 152 startGeolocationWatchPosition(); | 167 startGeolocationWatchPosition(); |
| 153 pollForPositionCallback(); | 168 pollForPositionCallback(); |
| 154 ensureGeolocationRunning(true); | 169 ensureGeolocationRunning(true); |
| 155 | 170 |
| 156 hideContentViewOnUiThread(); | 171 hideContentViewOnUiThread(); |
| 157 | 172 |
| 158 // Make sure that when starting a new watch while paused we still don't | 173 // Make sure that when starting a new watch while paused we still don't |
| 159 // start up geolocation until we show the content view again. | 174 // start up geolocation until we show the content view again. |
| 160 startGeolocationWatchPosition(); | 175 startGeolocationWatchPosition(); |
| 161 ensureGeolocationRunning(false); | 176 ensureGeolocationRunning(false); |
| 162 | 177 |
| 163 showContentViewOnUiThread(); | 178 showContentViewOnUiThread(); |
| 164 pollForPositionCallback(); | 179 pollForPositionCallback(); |
| 165 ensureGeolocationRunning(true); | 180 ensureGeolocationRunning(true); |
| 166 } | 181 } |
| 167 | 182 |
| 183 @Test |
| 168 @MediumTest | 184 @MediumTest |
| 169 @Feature({"Location"}) | 185 @Feature({"Location"}) |
| 170 public void testHideWatchStopShow() throws Throwable { | 186 public void testHideWatchStopShow() throws Throwable { |
| 171 hideContentViewOnUiThread(); | 187 hideContentViewOnUiThread(); |
| 172 startGeolocationWatchPosition(); | 188 startGeolocationWatchPosition(); |
| 173 ensureGeolocationRunning(false); | 189 ensureGeolocationRunning(false); |
| 174 | 190 |
| 175 loadUrl(getContentViewCore().getWebContents().getNavigationController(), | 191 mActivityTestRule.loadUrl( |
| 192 mActivityTestRule.getContentViewCore().getWebContents().getNavig
ationController(), |
| 176 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); | 193 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); |
| 177 showContentViewOnUiThread(); | 194 showContentViewOnUiThread(); |
| 178 ensureGeolocationRunning(false); | 195 ensureGeolocationRunning(false); |
| 179 } | 196 } |
| 180 } | 197 } |
| OLD | NEW |