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