1 // Copyright (c) 2012 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.
5 package org.chromium.content.browser;
7 import android.app.Activity;
8 import android.test.UiThreadTest;
9 import android.test.InstrumentationTestCase;
10 import android.test.suitebuilder.annotation.SmallTest;
12 import org.chromium.base.ActivityStatus;
13 import org.chromium.base.test.util.Feature;
16 * Test suite for LocationProvider.
18 public class LocationProviderTest extends InstrumentationTestCase {
19 private Activity mActivity;
20 private LocationProvider mLocationProvider;
24 mActivity = new Activity();
25 mLocationProvider = LocationProvider.create(getInstrumentation().getTargetContext());
29 * Verify a normal start/stop call pair without any activity pauses.
33 @Feature({"Location"})
34 public void testStartStop() throws Exception {
35 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
36 mLocationProvider.start(false);
37 assertTrue("Should be running", mLocationProvider.isRunning());
38 mLocationProvider.stop();
39 assertFalse("Should have stopped", mLocationProvider.isRunning());
43 * Verify a start/upgrade/stop call sequence without any activity pauses.
47 @Feature({"Location"})
48 public void testStartUpgradeStop() throws Exception {
49 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
50 mLocationProvider.start(false);
51 assertTrue("Should be running", mLocationProvider.isRunning());
52 mLocationProvider.start(true);
53 assertTrue("Should be running", mLocationProvider.isRunning());
54 mLocationProvider.stop();
55 assertFalse("Should have stopped", mLocationProvider.isRunning());
59 * Verify that pausing the activity stops location listener and when
60 * activity resumes it restarts listening.
64 @Feature({"Location"})
65 public void testStartPauseResumeStop() throws Exception {
66 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
67 mLocationProvider.start(false);
68 assertTrue("Should be running", mLocationProvider.isRunning());
69 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.PAUSED);
70 assertFalse("Should have paused", mLocationProvider.isRunning());
71 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
72 assertTrue("Should have resumed", mLocationProvider.isRunning());
73 mLocationProvider.stop();
74 assertFalse("Should have stopped", mLocationProvider.isRunning());
78 * Verify that calling start when the activity is paused doesn't start listening
79 * for location updates until activity resumes.
83 @Feature({"Location"})
84 public void testPauseStartResumeStop() throws Exception {
85 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.PAUSED);
86 mLocationProvider.start(false);
87 assertFalse("Should not be running", mLocationProvider.isRunning());
88 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
89 assertTrue("Should have resumed", mLocationProvider.isRunning());
90 mLocationProvider.stop();
91 assertFalse("Should have stopped", mLocationProvider.isRunning());
95 * Verify that calling start when the activity is being created doesn't start listening
96 * for location updates until activity resumes.
100 @Feature({"Location"})
101 public void testCreatedStartedStartResumeStop() throws Exception {
102 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.CREATED);
103 mLocationProvider.start(false);
104 assertFalse("Should not be running", mLocationProvider.isRunning());
105 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.STARTED);
106 mLocationProvider.start(false);
107 assertFalse("Should not be running", mLocationProvider.isRunning());
108 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
109 assertTrue("Should have resumed", mLocationProvider.isRunning());
110 mLocationProvider.stop();
111 assertFalse("Should have stopped", mLocationProvider.isRunning());
115 * Verify that calling start when the activity is being created then immediately paused doesn't
116 * start listening for location updates until activity resumes.
120 @Feature({"Location"})
121 public void testCreatedStartedStartPausedResumeStop() throws Exception {
122 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.CREATED);
123 mLocationProvider.start(false);
124 assertFalse("Should not be running", mLocationProvider.isRunning());
125 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.STARTED);
126 mLocationProvider.start(false);
127 assertFalse("Should not be running", mLocationProvider.isRunning());
128 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.PAUSED);
129 assertFalse("Should not be running", mLocationProvider.isRunning());
130 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
131 assertTrue("Should have resumed", mLocationProvider.isRunning());
132 mLocationProvider.stop();
133 assertFalse("Should have stopped", mLocationProvider.isRunning());
137 * Verify that calling start when the activity is stopped doesn't start listening
138 * for location updates until activity resumes.
142 @Feature({"Location"})
143 public void testStopStartResumeStop() throws Exception {
144 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.STOPPED);
145 mLocationProvider.start(false);
146 assertFalse("Should not be running", mLocationProvider.isRunning());
147 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
148 assertTrue("Should have resumed", mLocationProvider.isRunning());
149 mLocationProvider.stop();
150 assertFalse("Should have stopped", mLocationProvider.isRunning());
154 * Verify that upgrading when paused works as expected.
158 @Feature({"Location"})
159 public void testStartPauseUpgradeResumeStop() throws Exception {
160 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
161 mLocationProvider.start(false);
162 assertTrue("Should be running", mLocationProvider.isRunning());
163 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.PAUSED);
164 assertFalse("Should have paused", mLocationProvider.isRunning());
165 mLocationProvider.start(true);
166 assertFalse("Should be paused", mLocationProvider.isRunning());
167 ActivityStatus.onStateChangeForTesting(mActivity, ActivityStatus.RESUMED);
168 assertTrue("Should have resumed", mLocationProvider.isRunning());
169 mLocationProvider.stop();
170 assertFalse("Should have stopped", mLocationProvider.isRunning());