ContentViewLocationTest.java revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package org.chromium.content.browser;
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.test.suitebuilder.annotation.MediumTest;
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.base.test.util.Feature;
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.content.browser.test.util.Criteria;
11a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochimport org.chromium.content.browser.test.util.CriteriaHelper;
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)import org.chromium.content.browser.test.util.MockLocationProvider;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
14a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochimport org.chromium.content_shell_apk.ContentShellTestBase;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Test suite for ensureing that Geolocation interacts as expected
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * with ContentView APIs - e.g. that it's started and stopped as the
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ContentView is hidden or shown.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public class ContentViewLocationTest extends ContentShellTestBase {
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private TestCallbackHelperContainer mTestCallbackHelperContainer;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private MockLocationProvider mMockLocationProvider;
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
27a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    private void hideContentViewOnUiThread() {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        getInstrumentation().runOnMainSync(new Runnable() {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                @Override
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                public void run() {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    getContentView().onHide();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        });
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    private void showContentViewOnUiThread() {
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        getInstrumentation().runOnMainSync(new Runnable() {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                @Override
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                public void run() {
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    getContentView().onShow();
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                }
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        });
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    private void pollForPositionCallback() throws Throwable {
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "positionCount = 0");
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        mJavascriptHelper.waitUntilHasValue();
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                @Override
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                public boolean isSatisfied() {
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                    mJavascriptHelper.evaluateJavaScript(getContentViewCore(), "positionCount");
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    try {
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                        mJavascriptHelper.waitUntilHasValue();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    } catch (Exception e) {
58a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                        fail();
59a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                    }
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0;
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                }
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }));
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private void startGeolocationWatchPosition() throws Throwable {
66a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                "initiate_watchPosition();");
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        mJavascriptHelper.waitUntilHasValue();
69a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    }
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private void ensureGeolocationRunning(final boolean running) throws Exception {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            @Override
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            public boolean isSatisfied() {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                return mMockLocationProvider.isRunning() == running;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }));
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    @Override
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    protected void setUp() throws Exception {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        super.setUp();
83
84        mMockLocationProvider = new MockLocationProvider();
85        LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider);
86
87        try {
88            startActivityWithTestUrl("content/geolocation.html");
89        } catch (Throwable t) {
90            fail();
91        }
92
93        mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentView());
94        mJavascriptHelper = mTestCallbackHelperContainer.getOnEvaluateJavaScriptResultHelper();
95
96        ensureGeolocationRunning(false);
97    }
98
99    @Override
100    protected void tearDown() throws Exception {
101         mMockLocationProvider.stopUpdates();
102         super.tearDown();
103    }
104
105    @MediumTest
106    @Feature({"Location"})
107    public void testWatchHideShowStop() throws Throwable {
108
109        startGeolocationWatchPosition();
110        pollForPositionCallback();
111        ensureGeolocationRunning(true);
112
113        // Now hide the ContentView and ensure that geolocation stops.
114        hideContentViewOnUiThread();
115        ensureGeolocationRunning(false);
116
117        mJavascriptHelper.evaluateJavaScript(getContentViewCore(),
118                "positionCount = 0");
119        mJavascriptHelper.waitUntilHasValue();
120
121        // Show the ContentView again and ensure that geolocation starts again.
122        showContentViewOnUiThread();
123        pollForPositionCallback();
124        ensureGeolocationRunning(true);
125
126        // Navigate away and ensure that geolocation stops.
127        loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
128        ensureGeolocationRunning(false);
129    }
130
131    @MediumTest
132    @Feature({"Location"})
133    public void testHideWatchResume() throws Throwable {
134        hideContentViewOnUiThread();
135        startGeolocationWatchPosition();
136        ensureGeolocationRunning(false);
137
138        showContentViewOnUiThread();
139        pollForPositionCallback();
140        ensureGeolocationRunning(true);
141    }
142
143    @MediumTest
144    @Feature({"Location"})
145    public void testWatchHideNewWatchShow() throws Throwable {
146        startGeolocationWatchPosition();
147        pollForPositionCallback();
148        ensureGeolocationRunning(true);
149
150        hideContentViewOnUiThread();
151
152        // Make sure that when starting a new watch while paused we still don't
153        // start up geolocation until we show the content view again.
154        startGeolocationWatchPosition();
155        ensureGeolocationRunning(false);
156
157        showContentViewOnUiThread();
158        pollForPositionCallback();
159        ensureGeolocationRunning(true);
160    }
161
162    @MediumTest
163    @Feature({"Location"})
164    public void testHideWatchStopShow() throws Throwable {
165        hideContentViewOnUiThread();
166        startGeolocationWatchPosition();
167        ensureGeolocationRunning(false);
168
169        loadUrl(getContentView(), mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
170        showContentViewOnUiThread();
171        ensureGeolocationRunning(false);
172    }
173}
174