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