Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1700)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/OfflineBackgroundTaskTest.java

Issue 2830843002: [Offline pages] Updates to background scheduling to use BTS (Closed)
Patch Set: Move code from BackgroundOfflinerTask to OfflineBackgroundTask Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.assertFalse;
8 import static org.junit.Assert.assertTrue;
9 import static org.mockito.Mockito.any;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.eq;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14
15 import android.app.Activity;
16 import android.content.Context;
17 import android.os.Bundle;
18
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.ArgumentMatchers;
26 import org.mockito.Captor;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.robolectric.RuntimeEnvironment;
30 import org.robolectric.annotation.Config;
31
32 import org.chromium.base.ActivityState;
33 import org.chromium.base.ApplicationStatus;
34 import org.chromium.base.BaseSwitches;
35 import org.chromium.base.Callback;
36 import org.chromium.base.CommandLine;
37 import org.chromium.base.ContextUtils;
38 import org.chromium.base.SysUtils;
39 import org.chromium.base.test.util.Feature;
40 import org.chromium.chrome.browser.DisableHistogramsRule;
41 import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler ;
42 import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler Factory;
43 import org.chromium.components.background_task_scheduler.TaskInfo;
44 import org.chromium.net.ConnectionType;
45 import org.chromium.testing.local.LocalRobolectricTestRunner;
46
47 /**
48 * Unit tests for OfflineBackgroundTask.
49 */
50 @RunWith(LocalRobolectricTestRunner.class)
51 @Config(manifest = Config.NONE, shadows = {ShadowDeviceConditions.class})
52 public class OfflineBackgroundTaskTest {
53 private static final boolean REQUIRE_POWER = true;
54 private static final boolean REQUIRE_UNMETERED = true;
55 private static final boolean POWER_CONNECTED = true;
56 private static final int MINIMUM_BATTERY_LEVEL = 33;
57 private static final String IS_LOW_END_DEVICE_SWITCH =
58 "--" + BaseSwitches.ENABLE_LOW_END_DEVICE_MODE;
59
60 @Rule
61 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR ule();
62
63 private Bundle mTaskExtras;
64 private long mTestTime;
65 private TriggerConditions mTriggerConditions =
66 new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE _UNMETERED);
67 private DeviceConditions mDeviceConditions = new DeviceConditions(
68 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTI ON_3G);
69 private Callback<Boolean> mCallback;
70 private Activity mTestActivity;
71
72 @Mock
73 private BackgroundSchedulerProcessor mBackgroundSchedulerProcessor;
74
75 @Mock
76 private BackgroundTaskScheduler mTaskScheduler;
77 @Captor
78 private ArgumentCaptor<TaskInfo> mTaskInfo;
79
80 @Before
81 public void setUp() throws Exception {
82 MockitoAnnotations.initMocks(this);
83 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on);
84 BackgroundTaskSchedulerFactory.setSchedulerForTesting(mTaskScheduler);
85 doReturn(true)
86 .when(mTaskScheduler)
87 .schedule(eq(RuntimeEnvironment.application), mTaskInfo.capture( ));
88
89 ShadowDeviceConditions.setCurrentConditions(mDeviceConditions);
90
91 // Build a bundle with trigger conditions.
92 mTaskExtras = new Bundle();
93 TaskExtrasPacker.packTimeInBundle(mTaskExtras);
94 TaskExtrasPacker.packTriggerConditionsInBundle(mTaskExtras, mTriggerCond itions);
95
96 mCallback = new Callback<Boolean>() {
97 @Override
98 public void onResult(Boolean result) {
99 // Result explicitly ignored.
100 }
101 };
102
103 // Run tests as a low-end device.
104 CommandLine.init(new String[] {"testcommand", IS_LOW_END_DEVICE_SWITCH}) ;
105
106 // Set up single, stopped Activity.
107 ApplicationStatus.destroyForJUnitTests();
108 mTestActivity = new Activity();
109 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.C REATED);
110 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S TOPPED);
111 }
112
113 @After
114 public void tearDown() throws Exception {
115 // Clean up static state for subsequent Robolectric tests.
116 CommandLine.reset();
117 SysUtils.reset();
118 ApplicationStatus.destroyForJUnitTests();
119 }
120
121 @Test
122 @Feature({"OfflinePages"})
123 public void testCheckConditions_BatteryConditions() {
124 // Setup low battery conditions with no power connected.
125 DeviceConditions deviceConditionsLowBattery = new DeviceConditions(
126 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONN ECTION_WIFI);
127 ShadowDeviceConditions.setCurrentConditions(deviceConditionsLowBattery);
128
129 /// Verify that conditions for processing are not met.
130 assertFalse(
131 OfflineBackgroundTask.checkConditions(RuntimeEnvironment.applica tion, mTaskExtras));
132
133 // Set battery percentage below minimum level, but connect batter.
Pete Williamson 2017/05/24 21:41:03 batter -> power
fgorski 2017/05/24 23:10:13 Done.
134 DeviceConditions deviceConditionsPowerConnected = new DeviceConditions(
135 POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNE CTION_WIFI);
136 ShadowDeviceConditions.setCurrentConditions(deviceConditionsPowerConnect ed);
137
138 // Now verify that same battery level, with power connected, will pass t he conditions.
139 assertTrue(
140 OfflineBackgroundTask.checkConditions(RuntimeEnvironment.applica tion, mTaskExtras));
141 }
142
143 @Test
144 @Feature({"OfflinePages"})
145 public void testCheckConditions_OnLowEndDevice() {
146 // Transition the test Activity to a running state.
147 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S TARTED);
148
149 // Verify that conditions for processing are not met.
150 assertFalse(
151 OfflineBackgroundTask.checkConditions(RuntimeEnvironment.applica tion, mTaskExtras));
152
153 // Switch activity state to stopped.
154 ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.S TOPPED);
155
156 // Now verify that condition check passes when Activity is stopped.
157 assertTrue(
158 OfflineBackgroundTask.checkConditions(RuntimeEnvironment.applica tion, mTaskExtras));
159 }
160
161 @Test
162 @Feature({"OfflinePages"})
163 public void testStartBackgroundRequests() {
164 // Set up bridge behavior.
165 doReturn(true)
166 .when(mBackgroundSchedulerProcessor)
167 .startScheduledProcessing(
168 any(DeviceConditions.class), ArgumentMatchers.<Callback< Boolean>>any());
169
170 assertTrue(OfflineBackgroundTask.startScheduledProcessing(mBackgroundSch edulerProcessor,
171 RuntimeEnvironment.application, mTaskExtras, mCallback));
172
173 // Check with BackgroundSchedulerProcessor that processing started.
174 verify(mBackgroundSchedulerProcessor, times(1))
175 .startScheduledProcessing(eq(mDeviceConditions), eq(mCallback));
176 }
177
178 @Test
179 @Feature({"OfflinePages"})
180 public void testStartBackgroundRequestsNotStarted() {
181 // Nothing scheduled yet.
182 verify(mTaskScheduler, times(0)).schedule(any(Context.class), any(TaskIn fo.class));
183
184 // Processing will not be started here.
185 doReturn(false)
186 .when(mBackgroundSchedulerProcessor)
187 .startScheduledProcessing(
188 any(DeviceConditions.class), ArgumentMatchers.<Callback< Boolean>>any());
189
190 assertFalse(OfflineBackgroundTask.startScheduledProcessing(mBackgroundSc hedulerProcessor,
191 RuntimeEnvironment.application, mTaskExtras, mCallback));
192
193 // Check with BackgroundSchedulerProcessor that it did not start.
194 verify(mBackgroundSchedulerProcessor, times(1))
195 .startScheduledProcessing(eq(mDeviceConditions), eq(mCallback));
196 }
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698