Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.filters.MediumTest; | 7 import android.support.test.filters.MediumTest; |
| 8 | 8 |
| 9 import org.chromium.base.test.util.Feature; | 9 import org.chromium.base.test.util.Feature; |
| 10 import org.chromium.content.browser.test.util.Criteria; | 10 import org.chromium.content.browser.test.util.Criteria; |
| 11 import org.chromium.content.browser.test.util.CriteriaHelper; | 11 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval uateJavaScriptResultHelper; | 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval uateJavaScriptResultHelper; |
| 14 import org.chromium.content_public.browser.LoadUrlParams; | 14 import org.chromium.content_public.browser.LoadUrlParams; |
| 15 import org.chromium.content_shell_apk.ContentShellTestBase; | |
| 16 import org.chromium.device.geolocation.LocationProviderFactory; | 15 import org.chromium.device.geolocation.LocationProviderFactory; |
| 17 import org.chromium.device.geolocation.MockLocationProvider; | 16 import org.chromium.device.geolocation.MockLocationProvider; |
| 18 | 17 |
| 19 import java.util.concurrent.Callable; | 18 import java.util.concurrent.Callable; |
| 19 import org.junit.Rule; | |
| 20 import org.junit.Test; | |
| 21 import org.chromium.base.test.BaseJUnit4ClassRunner; | |
| 22 import org.junit.runner.RunWith; | |
| 23 import android.support.test.InstrumentationRegistry; | |
| 24 import org.junit.Assert; | |
| 25 import org.junit.After; | |
| 26 import org.junit.Before; | |
| 27 import org.chromium.content_shell_apk.ContentShellActivityTestRule; | |
| 20 | 28 |
| 21 /** | 29 /** |
| 22 * Test suite for ensureing that Geolocation interacts as expected | 30 * Test suite for ensureing that Geolocation interacts as expected |
| 23 * with ContentView APIs - e.g. that it's started and stopped as the | 31 * with ContentView APIs - e.g. that it's started and stopped as the |
| 24 * ContentView is hidden or shown. | 32 * ContentView is hidden or shown. |
| 25 */ | 33 */ |
| 26 public class ContentViewLocationTest extends ContentShellTestBase { | 34 @RunWith(BaseJUnit4ClassRunner.class) |
| 35 public class ContentViewLocationTest { | |
| 36 | |
| 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(mActivityTestRule.getWebCon tents(), |
| 52 "positionCount = 0"); | 64 "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(mActivityTestRu le.getWebContents(), "positionCount"); |
| 60 try { | 72 try { |
| 61 mJavascriptHelper.waitUntilHasValue(); | 73 mJavascriptHelper.waitUntilHasValue(); |
| 62 } catch (Exception e) { | 74 } catch (Exception e) { |
| 63 fail(); | 75 Assert.fail(); |
| 64 } | 76 } |
| 65 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl ear()) > 0; | 77 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl ear()) > 0; |
| 66 } | 78 } |
| 67 }); | 79 }); |
| 68 } | 80 } |
| 69 | 81 |
| 70 private void startGeolocationWatchPosition() throws Throwable { | 82 private void startGeolocationWatchPosition() throws Throwable { |
| 71 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), | 83 mJavascriptHelper.evaluateJavaScriptForTests(mActivityTestRule.getWebCon tents(), |
| 72 "initiate_watchPosition();"); | 84 "initiate_watchPosition();"); |
| 73 mJavascriptHelper.waitUntilHasValue(); | 85 mJavascriptHelper.waitUntilHasValue(); |
| 74 } | 86 } |
| 75 | 87 |
| 76 private void ensureGeolocationRunning(final boolean running) throws Exceptio n { | 88 private void ensureGeolocationRunning(final boolean running) throws Exceptio n { |
| 77 CriteriaHelper.pollInstrumentationThread(Criteria.equals(running, new Ca llable<Boolean>() { | 89 CriteriaHelper.pollInstrumentationThread(Criteria.equals(running, new Ca llable<Boolean>() { |
| 78 @Override | 90 @Override |
| 79 public Boolean call() { | 91 public Boolean call() { |
| 80 return mMockLocationProvider.isRunning(); | 92 return mMockLocationProvider.isRunning(); |
| 81 } | 93 } |
| 82 })); | 94 })); |
| 83 } | 95 } |
| 84 | 96 |
| 85 @Override | 97 @Before |
| 86 protected void setUp() throws Exception { | 98 |
| 87 super.setUp(); | 99 public void setUp() throws Exception { |
| 88 | 100 |
| 89 mMockLocationProvider = new MockLocationProvider(); | 101 mMockLocationProvider = new MockLocationProvider(); |
| 90 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); | 102 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); |
| 91 | 103 |
| 92 try { | 104 try { |
| 93 startActivityWithTestUrl("content/test/data/android/geolocation.html "); | 105 mActivityTestRule.launchContentShellWithUrlSync("content/test/data/a ndroid/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 = new TestCallbackHelperContainer(mActivity TestRule.getContentViewCore()); |
| 99 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper(); | 111 mJavascriptHelper = new OnEvaluateJavaScriptResultHelper(); |
| 100 | 112 |
| 101 ensureGeolocationRunning(false); | 113 ensureGeolocationRunning(false); |
| 102 } | 114 } |
| 103 | 115 |
| 104 @Override | 116 @After |
| 105 protected void tearDown() throws Exception { | 117 |
|
jbudorick
2017/03/01 23:10:19
No bonus line here.
the real yoland
2017/03/08 23:35:31
Done
| |
| 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 | 126 |
| 114 startGeolocationWatchPosition(); | 127 startGeolocationWatchPosition(); |
| 115 pollForPositionCallback(); | 128 pollForPositionCallback(); |
| 116 ensureGeolocationRunning(true); | 129 ensureGeolocationRunning(true); |
| 117 | 130 |
| 118 // Now hide the ContentView and ensure that geolocation stops. | 131 // Now hide the ContentView and ensure that geolocation stops. |
| 119 hideContentViewOnUiThread(); | 132 hideContentViewOnUiThread(); |
| 120 ensureGeolocationRunning(false); | 133 ensureGeolocationRunning(false); |
| 121 | 134 |
| 122 mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), | 135 mJavascriptHelper.evaluateJavaScriptForTests(mActivityTestRule.getWebCon tents(), |
| 123 "positionCount = 0"); | 136 "positionCount = 0"); |
| 124 mJavascriptHelper.waitUntilHasValue(); | 137 mJavascriptHelper.waitUntilHasValue(); |
| 125 | 138 |
| 126 // Show the ContentView again and ensure that geolocation starts again. | 139 // Show the ContentView again and ensure that geolocation starts again. |
| 127 showContentViewOnUiThread(); | 140 showContentViewOnUiThread(); |
| 128 pollForPositionCallback(); | 141 pollForPositionCallback(); |
| 129 ensureGeolocationRunning(true); | 142 ensureGeolocationRunning(true); |
| 130 | 143 |
| 131 // Navigate away and ensure that geolocation stops. | 144 // Navigate away and ensure that geolocation stops. |
| 132 loadUrl(getContentViewCore().getWebContents().getNavigationController(), | 145 mActivityTestRule.loadUrl(mActivityTestRule.getContentViewCore().getWebC ontents().getNavigationController(), |
| 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(mActivityTestRule.getContentViewCore().getWebC ontents().getNavigationController(), |
| 176 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); | 192 mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); |
| 177 showContentViewOnUiThread(); | 193 showContentViewOnUiThread(); |
| 178 ensureGeolocationRunning(false); | 194 ensureGeolocationRunning(false); |
| 179 } | 195 } |
| 180 } | 196 } |
| OLD | NEW |