OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertFalse; | 8 import static org.junit.Assert.assertFalse; |
9 import static org.junit.Assert.assertNotNull; | 9 import static org.junit.Assert.assertNotNull; |
10 import static org.junit.Assert.assertNull; | 10 import static org.junit.Assert.assertNull; |
11 import static org.junit.Assert.assertSame; | 11 import static org.junit.Assert.assertSame; |
12 import static org.junit.Assert.assertTrue; | 12 import static org.junit.Assert.assertTrue; |
13 import static org.mockito.Mockito.any; | 13 import static org.mockito.Mockito.any; |
14 import static org.mockito.Mockito.when; | 14 import static org.mockito.Mockito.when; |
15 | 15 |
16 import android.app.Activity; | |
16 import android.content.Context; | 17 import android.content.Context; |
17 import android.os.Bundle; | 18 import android.os.Bundle; |
18 | 19 |
19 import com.google.android.gms.gcm.Task; | 20 import com.google.android.gms.gcm.Task; |
20 | 21 |
22 import org.chromium.base.ActivityState; | |
23 import org.chromium.base.ApplicationStatus; | |
21 import org.chromium.base.BaseChromiumApplication; | 24 import org.chromium.base.BaseChromiumApplication; |
25 import org.chromium.base.BaseSwitches; | |
26 import org.chromium.base.CommandLine; | |
22 import org.chromium.base.metrics.RecordHistogram; | 27 import org.chromium.base.metrics.RecordHistogram; |
23 import org.chromium.base.test.util.Feature; | 28 import org.chromium.base.test.util.Feature; |
24 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter; | 29 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter; |
25 import org.chromium.net.ConnectionType; | 30 import org.chromium.net.ConnectionType; |
26 import org.chromium.testing.local.LocalRobolectricTestRunner; | 31 import org.chromium.testing.local.LocalRobolectricTestRunner; |
27 import org.junit.Before; | 32 import org.junit.Before; |
28 import org.junit.Test; | 33 import org.junit.Test; |
29 import org.junit.runner.RunWith; | 34 import org.junit.runner.RunWith; |
30 import org.mockito.Mock; | 35 import org.mockito.Mock; |
31 import org.mockito.MockitoAnnotations; | 36 import org.mockito.MockitoAnnotations; |
32 import org.robolectric.Robolectric; | 37 import org.robolectric.Robolectric; |
33 import org.robolectric.annotation.Config; | 38 import org.robolectric.annotation.Config; |
34 | 39 |
35 /** | 40 /** |
36 * Unit tests for BackgroundOfflinerTask. | 41 * Unit tests for BackgroundOfflinerTask. |
37 */ | 42 */ |
38 @RunWith(LocalRobolectricTestRunner.class) | 43 @RunWith(LocalRobolectricTestRunner.class) |
39 @Config(manifest = Config.NONE, | 44 @Config(manifest = Config.NONE, |
40 application = BaseChromiumApplication.class, | 45 application = BaseChromiumApplication.class, |
41 shadows = { ShadowGcmNetworkManager.class }) | 46 shadows = { ShadowGcmNetworkManager.class }) |
42 public class BackgroundOfflinerTaskTest { | 47 public class BackgroundOfflinerTaskTest { |
43 private static final boolean REQUIRE_POWER = true; | 48 private static final boolean REQUIRE_POWER = true; |
44 private static final boolean REQUIRE_UNMETERED = true; | 49 private static final boolean REQUIRE_UNMETERED = true; |
45 private static final boolean POWER_CONNECTED = true; | 50 private static final boolean POWER_CONNECTED = true; |
46 private static final int MINIMUM_BATTERY_LEVEL = 33; | 51 private static final int MINIMUM_BATTERY_LEVEL = 33; |
52 private static final String IS_LOW_END_DEVICE_SWITCH = | |
53 "--" + BaseSwitches.ENABLE_LOW_END_DEVICE_MODE; | |
47 | 54 |
48 @Mock | 55 @Mock |
49 private OfflinePageUtils mOfflinePageUtils; | 56 private OfflinePageUtils mOfflinePageUtils; |
50 | 57 |
51 private Bundle mTaskExtras; | 58 private Bundle mTaskExtras; |
52 private long mTestTime; | 59 private long mTestTime; |
53 private StubBackgroundSchedulerProcessor mStubBackgroundSchedulerProcessor; | 60 private StubBackgroundSchedulerProcessor mStubBackgroundSchedulerProcessor; |
54 private TriggerConditions mTriggerConditions = | 61 private TriggerConditions mTriggerConditions = |
55 new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE _UNMETERED); | 62 new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE _UNMETERED); |
56 private DeviceConditions mDeviceConditions = new DeviceConditions( | 63 private DeviceConditions mDeviceConditions = new DeviceConditions( |
57 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTI ON_3G); | 64 !POWER_CONNECTED, MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTI ON_3G); |
58 | 65 |
59 @Before | 66 @Before |
60 public void setUp() throws Exception { | 67 public void setUp() throws Exception { |
61 MockitoAnnotations.initMocks(this); | 68 MockitoAnnotations.initMocks(this); |
62 when(mOfflinePageUtils.getDeviceConditionsImpl(any(Context.class))) | 69 when(mOfflinePageUtils.getDeviceConditionsImpl(any(Context.class))) |
63 .thenReturn(mDeviceConditions); | 70 .thenReturn(mDeviceConditions); |
64 | 71 |
65 // Build a bundle with trigger conditions. | 72 // Build a bundle with trigger conditions. |
66 mTaskExtras = new Bundle(); | 73 mTaskExtras = new Bundle(); |
67 TaskExtrasPacker.packTimeInBundle(mTaskExtras); | 74 TaskExtrasPacker.packTimeInBundle(mTaskExtras); |
68 TaskExtrasPacker.packTriggerConditionsInBundle(mTaskExtras, mTriggerCond itions); | 75 TaskExtrasPacker.packTriggerConditionsInBundle(mTaskExtras, mTriggerCond itions); |
69 | 76 |
70 OfflinePageUtils.setInstanceForTesting(mOfflinePageUtils); | 77 OfflinePageUtils.setInstanceForTesting(mOfflinePageUtils); |
71 mStubBackgroundSchedulerProcessor = new StubBackgroundSchedulerProcessor (); | 78 mStubBackgroundSchedulerProcessor = new StubBackgroundSchedulerProcessor (); |
72 RecordHistogram.disableForTests(); | 79 RecordHistogram.disableForTests(); |
73 ShadowGcmNetworkManager.clear(); | 80 ShadowGcmNetworkManager.clear(); |
81 CommandLine.init(new String[] {"testcommand", IS_LOW_END_DEVICE_SWITCH}) ; | |
74 } | 82 } |
75 | 83 |
76 @Test | 84 @Test |
77 @Feature({"OfflinePages"}) | 85 @Feature({"OfflinePages"}) |
78 public void testIncomingTask() { | 86 public void testIncomingTask() { |
79 BackgroundOfflinerTask task = | 87 BackgroundOfflinerTask task = |
80 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); | 88 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); |
81 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter (1); | 89 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter (1); |
82 task.processBackgroundRequests(mTaskExtras, mDeviceConditions, waiter); | 90 task.processBackgroundRequests(mTaskExtras, mDeviceConditions, waiter); |
83 | 91 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 // Now verify low battery level but with power connected will start proc essing. | 139 // Now verify low battery level but with power connected will start proc essing. |
132 DeviceConditions deviceConditionsPowerConnected = new DeviceConditions( | 140 DeviceConditions deviceConditionsPowerConnected = new DeviceConditions( |
133 POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNE CTION_WIFI); | 141 POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNE CTION_WIFI); |
134 when(mOfflinePageUtils.getDeviceConditionsImpl(any(Context.class))) | 142 when(mOfflinePageUtils.getDeviceConditionsImpl(any(Context.class))) |
135 .thenReturn(deviceConditionsPowerConnected); | 143 .thenReturn(deviceConditionsPowerConnected); |
136 BackgroundOfflinerTask task2 = | 144 BackgroundOfflinerTask task2 = |
137 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); | 145 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); |
138 ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaite r(1); | 146 ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaite r(1); |
139 assertTrue(task2.startBackgroundRequests(Robolectric.application, mTaskE xtras, waiter2)); | 147 assertTrue(task2.startBackgroundRequests(Robolectric.application, mTaskE xtras, waiter2)); |
140 } | 148 } |
149 | |
150 @Test | |
151 @Feature({"OfflinePages"}) | |
152 public void testStartBackgroundRequestsForRunningActivityOnLowEndDevice() { | |
153 BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackground SchedulerProcessor); | |
154 ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter (1); | |
155 assertNull("Nothing scheduled", ShadowGcmNetworkManager.getScheduledTask ()); | |
156 Activity testActivity = new Activity(); | |
Pete Williamson
2016/07/19 21:15:43
Maybe add a comment here:
// Create another activ
dougarnett
2016/07/19 21:29:06
Done.
| |
157 ApplicationStatus.onStateChangeForTesting(testActivity, ActivityState.CR EATED); | |
158 ApplicationStatus.onStateChangeForTesting(testActivity, ActivityState.ST ARTED); | |
159 assertFalse(task.startBackgroundRequests(Robolectric.application, mTaskE xtras, waiter)); | |
160 | |
161 // Check that the backup task was scheduled. | |
162 Task gcmTask = ShadowGcmNetworkManager.getScheduledTask(); | |
163 assertNotNull("Backup task scheduled", gcmTask); | |
164 assertEquals(mTriggerConditions, | |
165 TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getEx tras())); | |
166 | |
167 // Check that startProcessing was NOT called. | |
168 assertFalse(mStubBackgroundSchedulerProcessor.getStartProcessingCalled() ); | |
169 | |
170 // Now verify will start processing when Activity is stopped. | |
171 ApplicationStatus.onStateChangeForTesting(testActivity, ActivityState.ST OPPED); | |
172 BackgroundOfflinerTask task2 = | |
173 new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor); | |
174 ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaite r(1); | |
175 assertTrue(task2.startBackgroundRequests(Robolectric.application, mTaskE xtras, waiter2)); | |
176 } | |
141 } | 177 } |
OLD | NEW |