Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.components.background_task_scheduler; | |
| 6 | |
| 7 import static junit.framework.TestCase.assertFalse; | |
| 8 | |
| 9 import static org.junit.Assert.assertEquals; | |
| 10 import static org.junit.Assert.assertTrue; | |
| 11 | |
| 12 import android.annotation.TargetApi; | |
| 13 import android.app.job.JobInfo; | |
| 14 import android.content.Context; | |
| 15 import android.os.Build; | |
| 16 import android.os.Bundle; | |
| 17 import android.os.PersistableBundle; | |
| 18 import android.support.test.InstrumentationRegistry; | |
| 19 import android.support.test.filters.SmallTest; | |
| 20 | |
| 21 import org.junit.runner.RunWith; | |
| 22 | |
| 23 import org.chromium.base.test.BaseJUnit4ClassRunner; | |
| 24 import org.chromium.base.test.util.MinAndroidSdkLevel; | |
| 25 | |
| 26 import java.util.concurrent.TimeUnit; | |
| 27 | |
| 28 /** | |
| 29 * Tests for {@link BackgroundTaskSchedulerJobService}. | |
| 30 */ | |
| 31 @RunWith(BaseJUnit4ClassRunner.class) | |
| 32 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) | |
| 33 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1) | |
| 34 public class BackgroundTaskSchedulerJobServiceTest { | |
| 35 private static class TestBackgroundTask implements BackgroundTask { | |
| 36 @Override | |
| 37 public boolean onStartTask( | |
| 38 Context context, TaskParameters taskParameters, TaskFinishedCall back callback) { | |
| 39 return false; | |
| 40 } | |
| 41 | |
| 42 @Override | |
| 43 public boolean onStopTask(Context context, TaskParameters taskParameters ) { | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 @SmallTest | |
| 49 public void testOneOffTaskInfoWithWindowConversion() { | |
| 50 TaskInfo oneOffTask = | |
| 51 TaskInfo.createOneOffTask(42, TestBackgroundTask.class, | |
|
nyquist
2017/02/22 18:24:12
Self-review: Use an ID specified TaskIds for all t
nyquist
2017/02/24 23:41:12
Done.
| |
| 52 TimeUnit.MINUTES.toMillis(100), TimeUnit.MINUTES .toMillis(200)) | |
| 53 .build(); | |
| 54 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 55 InstrumentationRegistry.getContext(), oneOffTask); | |
| 56 assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); | |
| 57 assertFalse(jobInfo.isPeriodic()); | |
| 58 assertEquals( | |
| 59 oneOffTask.getOneOffInfo().getWindowStartTimeMs(), jobInfo.getMi nLatencyMillis()); | |
| 60 assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(), | |
| 61 jobInfo.getMaxExecutionDelayMillis()); | |
| 62 } | |
| 63 | |
| 64 @SmallTest | |
| 65 public void testOneOffTaskInfoWithDeadlineConversion() { | |
| 66 TaskInfo oneOffTask = TaskInfo.createOneOffTask(42, TestBackgroundTask.c lass, | |
| 67 TimeUnit.MINUTES.toMillis(200)) | |
| 68 .build(); | |
| 69 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 70 InstrumentationRegistry.getContext(), oneOffTask); | |
| 71 assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); | |
| 72 assertFalse(jobInfo.isPeriodic()); | |
| 73 assertEquals(oneOffTask.getOneOffInfo().getWindowEndTimeMs(), | |
| 74 jobInfo.getMaxExecutionDelayMillis()); | |
| 75 } | |
| 76 | |
| 77 @SmallTest | |
| 78 public void testPeriodicTaskInfoWithoutFlexConversion() { | |
| 79 TaskInfo periodicTask = TaskInfo.createPeriodicTask(42, TestBackgroundTa sk.class, | |
| 80 TimeUnit.MINUTES.toMillis(200)) | |
| 81 .build(); | |
| 82 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 83 InstrumentationRegistry.getContext(), periodicTask); | |
| 84 assertEquals(periodicTask.getTaskId(), jobInfo.getId()); | |
| 85 assertTrue(jobInfo.isPeriodic()); | |
| 86 assertEquals(periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.get IntervalMillis()); | |
| 87 } | |
| 88 | |
| 89 @SmallTest | |
| 90 public void testPeriodicTaskInfoWithFlexConversion() { | |
| 91 TaskInfo periodicTask = | |
| 92 TaskInfo.createPeriodicTask(42, TestBackgroundTask.class, | |
| 93 TimeUnit.MINUTES.toMillis(200), TimeUnit.MINUTES .toMillis(50)) | |
| 94 .build(); | |
| 95 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 96 InstrumentationRegistry.getContext(), periodicTask); | |
| 97 assertEquals(periodicTask.getTaskId(), jobInfo.getId()); | |
| 98 assertTrue(jobInfo.isPeriodic()); | |
| 99 assertEquals(periodicTask.getPeriodicInfo().getIntervalMs(), jobInfo.get IntervalMillis()); | |
| 100 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
| 101 assertEquals(periodicTask.getPeriodicInfo().getFlexMs(), jobInfo.get FlexMillis()); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 @SmallTest | |
| 106 public void testTaskInfoWithExtras() { | |
| 107 Bundle taskExtras = new Bundle(); | |
| 108 taskExtras.putString("foo", "bar"); | |
| 109 taskExtras.putBoolean("bools", true); | |
| 110 taskExtras.putLong("longs", 1342543L); | |
| 111 TaskInfo oneOffTask = TaskInfo.createOneOffTask(42, TestBackgroundTask.c lass, | |
| 112 TimeUnit.MINUTES.toMillis(200)) | |
| 113 .setExtras(taskExtras) | |
| 114 .build(); | |
| 115 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 116 InstrumentationRegistry.getContext(), oneOffTask); | |
| 117 assertEquals(oneOffTask.getTaskId(), jobInfo.getId()); | |
| 118 PersistableBundle jobExtras = jobInfo.getExtras(); | |
| 119 PersistableBundle persistableBundle = jobExtras.getPersistableBundle( | |
| 120 BackgroundTaskSchedulerJobService.BACKGROUND_TASK_EXTRAS_KEY); | |
| 121 assertEquals(taskExtras.keySet().size(), persistableBundle.keySet().size ()); | |
| 122 assertEquals(taskExtras.getString("foo"), persistableBundle.getString("f oo")); | |
| 123 assertEquals(taskExtras.getBoolean("bools"), persistableBundle.getBoolea n("bools")); | |
| 124 assertEquals(taskExtras.getLong("longs"), persistableBundle.getLong("lon gs")); | |
| 125 } | |
| 126 | |
| 127 @SmallTest | |
| 128 public void testTaskInfoWithManyConstraints() { | |
| 129 TaskInfo.Builder taskBuilder = TaskInfo.createOneOffTask( | |
| 130 42, TestBackgroundTask.class, TimeUnit.MINUTES.toMillis(200)); | |
| 131 | |
| 132 JobInfo jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTas kInfo( | |
| 133 InstrumentationRegistry.getContext(), taskBuilder.setIsPersisted (true).build()); | |
| 134 assertTrue(jobInfo.isPersisted()); | |
| 135 | |
| 136 jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( | |
| 137 InstrumentationRegistry.getContext(), | |
| 138 taskBuilder.setRequiredNetworkType(TaskInfo.NETWORK_TYPE_UNMETER ED).build()); | |
| 139 assertEquals(JobInfo.NETWORK_TYPE_UNMETERED, jobInfo.getNetworkType()); | |
| 140 | |
| 141 jobInfo = BackgroundTaskSchedulerJobService.createJobInfoFromTaskInfo( | |
| 142 InstrumentationRegistry.getContext(), | |
| 143 taskBuilder.setRequiresCharging(true).build()); | |
| 144 assertTrue(jobInfo.isRequireCharging()); | |
| 145 } | |
| 146 } | |
| OLD | NEW |