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

Side by Side Diff: components/background_task_scheduler/android/junit/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerTest.java

Issue 2819703002: [Android] Implements OS upgrade check and rescheduling (Closed)
Patch Set: Calling upgrade task from DeferredStartupHandler Created 3 years, 8 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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.components.background_task_scheduler; 5 package org.chromium.components.background_task_scheduler;
6 6
7 import android.os.Build;
8
9 import com.google.android.gms.common.ConnectionResult;
10 import com.google.android.gms.common.GoogleApiAvailability;
11 import com.google.android.gms.gcm.GcmNetworkManager;
12
7 import static org.junit.Assert.assertEquals; 13 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse; 14 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertTrue; 15 import static org.junit.Assert.assertTrue;
10 import static org.mockito.Mockito.doNothing; 16 import static org.mockito.Mockito.doNothing;
11 import static org.mockito.Mockito.doReturn; 17 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.eq; 18 import static org.mockito.Mockito.eq;
13 import static org.mockito.Mockito.times; 19 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify; 20 import static org.mockito.Mockito.verify;
15 21
16 import org.junit.Before; 22 import org.junit.Before;
17 import org.junit.Test; 23 import org.junit.Test;
18 import org.junit.runner.RunWith; 24 import org.junit.runner.RunWith;
19 import org.mockito.Mock; 25 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations; 26 import org.mockito.MockitoAnnotations;
21 import org.robolectric.RuntimeEnvironment; 27 import org.robolectric.RuntimeEnvironment;
22 import org.robolectric.annotation.Config; 28 import org.robolectric.annotation.Config;
29 import org.robolectric.internal.ShadowExtractor;
30 import org.robolectric.shadows.gms.Shadows;
31 import org.robolectric.shadows.gms.common.ShadowGoogleApiAvailability;
32 import org.robolectric.util.ReflectionHelpers;
23 33
24 import org.chromium.base.ContextUtils; 34 import org.chromium.base.ContextUtils;
25 import org.chromium.base.test.util.Feature; 35 import org.chromium.base.test.util.Feature;
26 import org.chromium.testing.local.LocalRobolectricTestRunner; 36 import org.chromium.testing.local.LocalRobolectricTestRunner;
27 37
28 import java.util.concurrent.TimeUnit; 38 import java.util.concurrent.TimeUnit;
29 39
30 /** Unit tests for {@link BackgroundTaskScheduler}. */ 40 /** Unit tests for {@link BackgroundTaskScheduler}. */
31 @RunWith(LocalRobolectricTestRunner.class) 41 @RunWith(LocalRobolectricTestRunner.class)
32 @Config(manifest = Config.NONE) 42 @Config(manifest = Config.NONE,
43 shadows = {ShadowGcmNetworkManager.class, ShadowGoogleApiAvailability.cl ass})
33 public class BackgroundTaskSchedulerTest { 44 public class BackgroundTaskSchedulerTest {
34 private static final TaskInfo TASK = 45 private static final TaskInfo TASK =
35 TaskInfo.createOneOffTask( 46 TaskInfo.createOneOffTask(
36 TaskIds.TEST, TestBackgroundTask.class, TimeUnit.DAY S.toMillis(1)) 47 TaskIds.TEST, TestBackgroundTask.class, TimeUnit.DAY S.toMillis(1))
37 .build(); 48 .build();
38 49
39 @Mock 50 @Mock
40 private BackgroundTaskSchedulerDelegate mDelegate; 51 private BackgroundTaskSchedulerDelegate mDelegate;
52 private ShadowGcmNetworkManager mGcmNetworkManager;
41 53
42 @Before 54 @Before
43 public void setUp() { 55 public void setUp() {
44 MockitoAnnotations.initMocks(this); 56 MockitoAnnotations.initMocks(this);
45 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on); 57 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on);
46 BackgroundTaskSchedulerFactory.setSchedulerForTesting( 58 BackgroundTaskSchedulerFactory.setSchedulerForTesting(
47 new BackgroundTaskScheduler(mDelegate)); 59 new BackgroundTaskScheduler(mDelegate));
48 TestBackgroundTask.reset(); 60 TestBackgroundTask.reset();
61
62 // Initialize Google Play Services and GCM Network Manager for upgrade t esting.
63 Shadows.shadowOf(GoogleApiAvailability.getInstance())
64 .setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS);
65 mGcmNetworkManager = (ShadowGcmNetworkManager) ShadowExtractor.extract(
66 GcmNetworkManager.getInstance(ContextUtils.getApplicationContext ()));
49 } 67 }
50 68
51 @Test 69 @Test
52 @Feature({"BackgroundTaskScheduler"}) 70 @Feature({"BackgroundTaskScheduler"})
53 public void testScheduleTask() { 71 public void testScheduleTask() {
54 doReturn(true).when(mDelegate).schedule(eq(RuntimeEnvironment.applicatio n), eq(TASK)); 72 doReturn(true).when(mDelegate).schedule(eq(RuntimeEnvironment.applicatio n), eq(TASK));
55 BackgroundTaskSchedulerFactory.getScheduler().schedule( 73 BackgroundTaskSchedulerFactory.getScheduler().schedule(
56 RuntimeEnvironment.application, TASK); 74 RuntimeEnvironment.application, TASK);
57 assertFalse(BackgroundTaskSchedulerPrefs.getScheduledTasks().contains( 75 assertFalse(BackgroundTaskSchedulerPrefs.getScheduledTasks().contains(
58 TASK.getBackgroundTaskClass())); 76 TASK.getBackgroundTaskClass()));
(...skipping 18 matching lines...) Expand all
77 public void testRescheduleTasks() { 95 public void testRescheduleTasks() {
78 BackgroundTaskSchedulerPrefs.addScheduledTask(TASK); 96 BackgroundTaskSchedulerPrefs.addScheduledTask(TASK);
79 97
80 assertEquals(0, TestBackgroundTask.getRescheduleCalls()); 98 assertEquals(0, TestBackgroundTask.getRescheduleCalls());
81 assertFalse(BackgroundTaskSchedulerPrefs.getScheduledTasks().isEmpty()); 99 assertFalse(BackgroundTaskSchedulerPrefs.getScheduledTasks().isEmpty());
82 BackgroundTaskSchedulerFactory.getScheduler().reschedule(RuntimeEnvironm ent.application); 100 BackgroundTaskSchedulerFactory.getScheduler().reschedule(RuntimeEnvironm ent.application);
83 101
84 assertEquals(1, TestBackgroundTask.getRescheduleCalls()); 102 assertEquals(1, TestBackgroundTask.getRescheduleCalls());
85 assertTrue(BackgroundTaskSchedulerPrefs.getScheduledTasks().isEmpty()); 103 assertTrue(BackgroundTaskSchedulerPrefs.getScheduledTasks().isEmpty());
86 } 104 }
105
106 @Test
107 @Feature({"BackgroundTaskScheduler"})
108 public void testCheckForOSUpgrade_PreMToMPlus() {
109 BackgroundTaskSchedulerPrefs.setLastSdkVersion(Build.VERSION_CODES.LOLLI POP);
110 BackgroundTaskSchedulerPrefs.addScheduledTask(TASK);
111 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V ERSION_CODES.M);
112
113 BackgroundTaskSchedulerFactory.getScheduler().checkForOSUpgrade(
114 RuntimeEnvironment.application);
115
116 assertEquals(Build.VERSION_CODES.M, BackgroundTaskSchedulerPrefs.getLast SdkVersion());
117 assertTrue(mGcmNetworkManager.getCanceledTaskTags().contains(
118 Integer.toString(TASK.getTaskId())));
119 assertEquals(1, TestBackgroundTask.getRescheduleCalls());
120 }
121
122 /** This scenario tests upgrade from M+ to M+ OS, which requires no reschedu ling. */
nyquist 2017/04/18 22:41:08 Could you update this comment to match the test?
fgorski 2017/04/20 22:36:07 Done.
123 @Test
124 @Feature({"BackgroundTaskScheduler"})
125 public void testCheckForOSUpgrade_PreMToPreM() {
126 BackgroundTaskSchedulerPrefs.setLastSdkVersion(Build.VERSION_CODES.KITKA T);
127 BackgroundTaskSchedulerPrefs.addScheduledTask(TASK);
128 ReflectionHelpers.setStaticField(
129 Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP);
130
131 BackgroundTaskSchedulerFactory.getScheduler().checkForOSUpgrade(
132 RuntimeEnvironment.application);
133
134 assertEquals(
135 Build.VERSION_CODES.LOLLIPOP, BackgroundTaskSchedulerPrefs.getLa stSdkVersion());
136 assertEquals(0, TestBackgroundTask.getRescheduleCalls());
137 }
138
139 /** This scenario tests upgrade from M+ to M+ OS, which requires no reschedu ling. */
140 @Test
141 @Feature({"BackgroundTaskScheduler"})
142 public void testCheckForOSUpgrade_MPlusToMPlus() {
143 BackgroundTaskSchedulerPrefs.setLastSdkVersion(Build.VERSION_CODES.M);
144 BackgroundTaskSchedulerPrefs.addScheduledTask(TASK);
145 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V ERSION_CODES.N);
146
147 BackgroundTaskSchedulerFactory.getScheduler().checkForOSUpgrade(
148 RuntimeEnvironment.application);
149
150 assertEquals(Build.VERSION_CODES.N, BackgroundTaskSchedulerPrefs.getLast SdkVersion());
151 assertEquals(0, TestBackgroundTask.getRescheduleCalls());
152 }
87 } 153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698