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