OLD | NEW |
| (Empty) |
1 // Copyright 2016 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.chrome.browser.offlinepages; | |
6 | |
7 import static org.junit.Assert.assertEquals; | |
8 import static org.junit.Assert.assertFalse; | |
9 import static org.junit.Assert.assertNotNull; | |
10 import static org.junit.Assert.assertNull; | |
11 import static org.junit.Assert.assertSame; | |
12 import static org.junit.Assert.assertTrue; | |
13 | |
14 import android.app.Activity; | |
15 import android.content.Context; | |
16 import android.os.Bundle; | |
17 | |
18 import com.google.android.gms.common.ConnectionResult; | |
19 import com.google.android.gms.common.GoogleApiAvailability; | |
20 import com.google.android.gms.gcm.GcmNetworkManager; | |
21 import com.google.android.gms.gcm.Task; | |
22 | |
23 import org.junit.After; | |
24 import org.junit.Before; | |
25 import org.junit.Rule; | |
26 import org.junit.Test; | |
27 import org.junit.runner.RunWith; | |
28 import org.robolectric.RuntimeEnvironment; | |
29 import org.robolectric.annotation.Config; | |
30 import org.robolectric.internal.ShadowExtractor; | |
31 import org.robolectric.shadows.gms.Shadows; | |
32 import org.robolectric.shadows.gms.common.ShadowGoogleApiAvailability; | |
33 | |
34 import org.chromium.base.ActivityState; | |
35 import org.chromium.base.ApplicationStatus; | |
36 import org.chromium.base.BaseChromiumApplication; | |
37 import org.chromium.base.BaseSwitches; | |
38 import org.chromium.base.CommandLine; | |
39 import org.chromium.base.SysUtils; | |
40 import org.chromium.base.test.util.Feature; | |
41 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter; | |
42 import org.chromium.chrome.browser.DisableHistogramsRule; | |
43 import org.chromium.net.ConnectionType; | |
44 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
45 | |
46 /** | |
47 * Unit tests for BackgroundOfflinerTask. | |
48 */ | |
49 @RunWith(LocalRobolectricTestRunner.class) | |
50 @Config(manifest = Config.NONE, application = BaseChromiumApplication.class, sdk
= 21, | |
51 shadows = {ShadowGcmNetworkManager.class, ShadowGoogleApiAvailability.cl
ass, | |
52 ShadowDeviceConditions.class}) | |
53 public class BackgroundOfflinerTaskTest { | |
54 private static final boolean REQUIRE_POWER = true; | |
55 private static final boolean REQUIRE_UNMETERED = true; | |
56 private static final boolean POWER_CONNECTED = true; | |
57 private static final int MINIMUM_BATTERY_LEVEL = 33; | |
58 private static final String IS_LOW_END_DEVICE_SWITCH = | |
59 "--" + BaseSwitches.ENABLE_LOW_END_DEVICE_MODE; | |
60 | |
61 @Rule | |
62 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); | |
63 | |
64 private Bundle mTaskExtras; | |
65 private long mTestTime; | |
66 private StubBackgroundSchedulerProcessor mStubBackgroundSchedulerProcessor; | |
67 private TriggerConditions mTriggerConditions = | |
68 new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE
_UNMETERED); | |
69 private DeviceConditions mDeviceConditions = new DeviceConditions( | |
70 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTI
ON_3G); | |
71 private Activity mTestActivity; | |
72 | |
73 private Context mContext; | |
74 private ShadowGcmNetworkManager mGcmNetworkManager; | |
75 | |
76 @Before | |
77 public void setUp() throws Exception { | |
78 Shadows.shadowOf(GoogleApiAvailability.getInstance()) | |
79 .setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS); | |
80 ShadowDeviceConditions.setCurrentConditions(mDeviceConditions); | |
81 | |
82 // Build a bundle with trigger conditions. | |
83 mTaskExtras = new Bundle(); | |
84 TaskExtrasPacker.packTimeInBundle(mTaskExtras); | |
85 TaskExtrasPacker.packTriggerConditionsInBundle(mTaskExtras, mTriggerCond
itions); | |
86 | |
87 mStubBackgroundSchedulerProcessor = new StubBackgroundSchedulerProcessor
(); | |
88 mContext = RuntimeEnvironment.application; | |
89 mGcmNetworkManager = (ShadowGcmNetworkManager) ShadowExtractor.extract( | |
90 GcmNetworkManager.getInstance(mContext)); | |
91 mGcmNetworkManager.clear(); | |
92 | |
93 // Run tests as a low-end device. | |
94 CommandLine.init(new String[] {"testcommand", IS_LOW_END_DEVICE_SWITCH})
; | |
95 | |
96 // Set up single, stopped Activity. | |
97 ApplicationStatus.destroyForJUnitTests(); | |
98 mTestActivity = new Activity(); | |
99 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.C
REATED); | |
100 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S
TOPPED); | |
101 } | |
102 | |
103 @After | |
104 public void tearDown() throws Exception { | |
105 // Clean up static state for subsequent Robolectric tests. | |
106 CommandLine.reset(); | |
107 SysUtils.reset(); | |
108 ApplicationStatus.destroyForJUnitTests(); | |
109 } | |
110 | |
111 @Test | |
112 @Feature({"OfflinePages"}) | |
113 public void testStartBackgroundRequests() { | |
114 BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackground
SchedulerProcessor); | |
115 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter
(1); | |
116 assertNull("Nothing scheduled", mGcmNetworkManager.getScheduledTask()); | |
117 | |
118 task.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtras
, waiter); | |
119 | |
120 // Check that the backup task was scheduled. | |
121 Task gcmTask = mGcmNetworkManager.getScheduledTask(); | |
122 assertNotNull("Backup task scheduled", gcmTask); | |
123 assertEquals(mTriggerConditions, | |
124 TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getEx
tras())); | |
125 | |
126 // Check with BackgroundSchedulerProcessor that processing stated. | |
127 assertTrue(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
128 assertSame(mDeviceConditions, mStubBackgroundSchedulerProcessor.getDevic
eConditions()); | |
129 | |
130 // Waiter is only released at the end of the test, when the processing c
oncludes. | |
131 mStubBackgroundSchedulerProcessor.callback(); | |
132 waiter.startWaiting(); | |
133 } | |
134 | |
135 @Test | |
136 @Feature({"OfflinePages"}) | |
137 public void testStartBackgroundRequestsNotStarted() { | |
138 mStubBackgroundSchedulerProcessor.setFailToStart(true); | |
139 BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackground
SchedulerProcessor); | |
140 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter
(1); | |
141 assertNull("Nothing scheduled", mGcmNetworkManager.getScheduledTask()); | |
142 | |
143 task.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtras
, waiter); | |
144 waiter.startWaiting(); | |
145 | |
146 // Check that the backup task was scheduled. | |
147 Task gcmTask = mGcmNetworkManager.getScheduledTask(); | |
148 assertNotNull("Backup task scheduled", gcmTask); | |
149 assertEquals(mTriggerConditions, | |
150 TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getEx
tras())); | |
151 | |
152 // Check with BackgroundSchedulerProcessor that it did not start. | |
153 assertFalse(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
154 } | |
155 | |
156 @Test | |
157 @Feature({"OfflinePages"}) | |
158 public void testStartBackgroundRequestsForLowBatteryLevel() { | |
159 // Setup low battery conditions. | |
160 DeviceConditions deviceConditionsLowBattery = new DeviceConditions( | |
161 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONN
ECTION_WIFI); | |
162 ShadowDeviceConditions.setCurrentConditions(deviceConditionsLowBattery); | |
163 | |
164 BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackground
SchedulerProcessor); | |
165 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter
(1); | |
166 assertNull("Nothing scheduled", mGcmNetworkManager.getScheduledTask()); | |
167 task.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtras
, waiter); | |
168 waiter.startWaiting(); | |
169 | |
170 // Check that the backup task was scheduled. | |
171 Task gcmTask = mGcmNetworkManager.getScheduledTask(); | |
172 assertNotNull("Backup task scheduled", gcmTask); | |
173 assertEquals(mTriggerConditions, | |
174 TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getEx
tras())); | |
175 | |
176 // Check that startScheduledProcessing was NOT called. | |
177 assertFalse(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
178 | |
179 // Now verify low battery level but with power connected will start proc
essing. | |
180 DeviceConditions deviceConditionsPowerConnected = new DeviceConditions( | |
181 POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNE
CTION_WIFI); | |
182 ShadowDeviceConditions.setCurrentConditions(deviceConditionsPowerConnect
ed); | |
183 | |
184 BackgroundOfflinerTask task2 = | |
185 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); | |
186 ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaite
r(1); | |
187 task2.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtra
s, waiter2); | |
188 | |
189 assertTrue(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
190 | |
191 mStubBackgroundSchedulerProcessor.callback(); | |
192 waiter2.startWaiting(); | |
193 } | |
194 | |
195 @Test | |
196 @Feature({"OfflinePages"}) | |
197 public void testStartBackgroundRequestsForRunningActivityOnLowEndDevice() { | |
198 BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackground
SchedulerProcessor); | |
199 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter
(1); | |
200 assertNull("Nothing scheduled", mGcmNetworkManager.getScheduledTask()); | |
201 | |
202 // Transition the test Activity to a running state. | |
203 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S
TARTED); | |
204 | |
205 task.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtras
, waiter); | |
206 waiter.startWaiting(); | |
207 | |
208 // Check that the backup task was scheduled. | |
209 Task gcmTask = mGcmNetworkManager.getScheduledTask(); | |
210 assertNotNull("Backup task scheduled", gcmTask); | |
211 assertEquals(mTriggerConditions, | |
212 TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getEx
tras())); | |
213 | |
214 // Check that startScheduledProcessing was NOT called. | |
215 assertFalse(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
216 | |
217 // Now verify will start processing when Activity is stopped. | |
218 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S
TOPPED); | |
219 BackgroundOfflinerTask task2 = | |
220 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); | |
221 ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaite
r(1); | |
222 task2.startBackgroundRequests(RuntimeEnvironment.application, mTaskExtra
s, waiter2); | |
223 | |
224 assertTrue(mStubBackgroundSchedulerProcessor.getDidStartProcessing()); | |
225 | |
226 mStubBackgroundSchedulerProcessor.callback(); | |
227 waiter2.startWaiting(); | |
228 } | |
229 } | |
OLD | NEW |