OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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.UiThreadTest; |
| 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 |
| 10 import org.chromium.base.test.util.Feature; |
| 11 import org.chromium.base.test.util.UrlUtils; |
| 12 import org.chromium.content.browser.LocationProvider; |
| 13 import org.chromium.content.browser.test.util.Criteria; |
| 14 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 16 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 17 |
| 18 public class ContentViewLocationTest extends ContentShellTestBase { |
| 19 |
| 20 private TestCallbackHelperContainer mTestCallbackHelperContainer; |
| 21 private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavasc
riptHelper; |
| 22 |
| 23 private void hideContentViewOnUiThread() { |
| 24 getInstrumentation().runOnMainSync(new Runnable() { |
| 25 @Override |
| 26 public void run() { |
| 27 getContentView().onHide(); |
| 28 } |
| 29 }); |
| 30 } |
| 31 |
| 32 private void showContentViewOnUiThread() { |
| 33 getInstrumentation().runOnMainSync(new Runnable() { |
| 34 @Override |
| 35 public void run() { |
| 36 getContentView().onShow(); |
| 37 } |
| 38 }); |
| 39 } |
| 40 |
| 41 private void pollForPositionCallback() throws Throwable{ |
| 42 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
| 43 "positionCount = 0"); |
| 44 mJavascriptHelper.waitUntilHasValue(); |
| 45 assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear
())); |
| 46 |
| 47 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 48 @Override |
| 49 public boolean isSatisfied() { |
| 50 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), "
positionCount"); |
| 51 try { |
| 52 mJavascriptHelper.waitUntilHasValue(); |
| 53 } catch (Exception e) { |
| 54 fail(); |
| 55 } |
| 56 return Integer.parseInt(mJavascriptHelper.getJsonResultAndCl
ear()) > 0; |
| 57 } |
| 58 })); |
| 59 } |
| 60 |
| 61 private void startGeolocationWatchPosition() throws Throwable { |
| 62 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
| 63 "initiate_watchPosition();"); |
| 64 mJavascriptHelper.waitUntilHasValue(); |
| 65 } |
| 66 |
| 67 @Override |
| 68 protected void setUp() throws Exception { |
| 69 super.setUp(); |
| 70 |
| 71 LocationProvider.enableMockLocationProvider(); |
| 72 |
| 73 try { |
| 74 startActivityWithTestUrl("content/geolocation.html"); |
| 75 } catch (Throwable t) { |
| 76 fail(); |
| 77 } |
| 78 |
| 79 mTestCallbackHelperContainer = new TestCallbackHelperContainer(getConten
tView()); |
| 80 mJavascriptHelper = mTestCallbackHelperContainer.getOnEvaluateJavaScript
ResultHelper(); |
| 81 |
| 82 assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
| 83 } |
| 84 |
| 85 @SmallTest |
| 86 @Feature({"Location"}) |
| 87 public void testWatchHideShowStop() throws Throwable { |
| 88 |
| 89 startGeolocationWatchPosition(); |
| 90 pollForPositionCallback(); |
| 91 assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
| 92 |
| 93 // Now hide the ContentView and ensure that geolocation stops. |
| 94 hideContentViewOnUiThread(); |
| 95 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 96 @Override |
| 97 public boolean isSatisfied() { |
| 98 return !getContentViewCore().isGeolocationActiveForTesting()
; |
| 99 } |
| 100 })); |
| 101 |
| 102 mJavascriptHelper.evaluateJavaScript(getContentViewCore(), |
| 103 "positionCount = 0"); |
| 104 mJavascriptHelper.waitUntilHasValue(); |
| 105 |
| 106 // Show the ContentView again and ensure that geolocation starts again. |
| 107 showContentViewOnUiThread(); |
| 108 pollForPositionCallback(); |
| 109 assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
| 110 |
| 111 // Navigate away and ensure that geolocation stops. |
| 112 loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParam
s("about:blank")); |
| 113 assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
| 114 } |
| 115 |
| 116 @SmallTest |
| 117 @Feature({"Location"}) |
| 118 public void testHideWatchResume() throws Throwable { |
| 119 hideContentViewOnUiThread(); |
| 120 startGeolocationWatchPosition(); |
| 121 assertFalse(getContentViewCore().isGeolocationActiveForTesting()); |
| 122 |
| 123 showContentViewOnUiThread(); |
| 124 pollForPositionCallback(); |
| 125 assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
| 126 } |
| 127 |
| 128 public void testWatchHideNewWatchShow() throws Throwable { |
| 129 startGeolocationWatchPosition(); |
| 130 pollForPositionCallback(); |
| 131 assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
| 132 hideContentViewOnUiThread(); |
| 133 |
| 134 // Make sure that when starting a new watch while paused we still don't |
| 135 // start up geolocation until we show the content view again. |
| 136 startGeolocationWatchPosition(); |
| 137 assertFalse(CriteriaHelper.pollForCriteria(new Criteria() { |
| 138 @Override |
| 139 public boolean isSatisfied() { |
| 140 return getContentViewCore().isGeolocationActiveForTesting(); |
| 141 } |
| 142 })); |
| 143 |
| 144 showContentViewOnUiThread(); |
| 145 pollForPositionCallback(); |
| 146 assertTrue(getContentViewCore().isGeolocationActiveForTesting()); |
| 147 } |
| 148 |
| 149 |
| 150 } |
OLD | NEW |