| 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; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
| 8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
| 9 import android.test.InstrumentationTestCase; | |
| 10 | 10 |
| 11 import com.google.android.gms.gcm.TaskParams; | 11 import com.google.android.gms.gcm.TaskParams; |
| 12 | 12 |
| 13 import org.junit.After; |
| 14 import org.junit.Assert; |
| 15 import org.junit.Before; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 |
| 13 import org.chromium.base.ContextUtils; | 19 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.base.ThreadUtils; | 20 import org.chromium.base.ThreadUtils; |
| 15 import org.chromium.base.metrics.RecordHistogram; | 21 import org.chromium.base.metrics.RecordHistogram; |
| 16 import org.chromium.base.test.util.AdvancedMockContext; | 22 import org.chromium.base.test.util.AdvancedMockContext; |
| 17 import org.chromium.base.test.util.Feature; | 23 import org.chromium.base.test.util.Feature; |
| 18 import org.chromium.base.test.util.RetryOnFailure; | 24 import org.chromium.base.test.util.RetryOnFailure; |
| 19 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; | 25 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; |
| 20 import org.chromium.chrome.browser.precache.PrecacheController; | 26 import org.chromium.chrome.browser.precache.PrecacheController; |
| 27 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 21 | 28 |
| 22 /** | 29 /** |
| 23 * Tests {@link ChromeBackgroundService}. | 30 * Tests {@link ChromeBackgroundService}. |
| 24 */ | 31 */ |
| 32 @RunWith(ChromeJUnit4ClassRunner.class) |
| 25 @RetryOnFailure | 33 @RetryOnFailure |
| 26 public class ChromeBackgroundServiceTest extends InstrumentationTestCase { | 34 public class ChromeBackgroundServiceTest { |
| 27 private Context mContext; | 35 private Context mContext; |
| 28 private BackgroundSyncLauncher mSyncLauncher; | 36 private BackgroundSyncLauncher mSyncLauncher; |
| 29 private SnippetsLauncher mSnippetsLauncher; | 37 private SnippetsLauncher mSnippetsLauncher; |
| 30 private MockTaskService mTaskService; | 38 private MockTaskService mTaskService; |
| 31 | 39 |
| 32 static class MockTaskService extends ChromeBackgroundService { | 40 static class MockTaskService extends ChromeBackgroundService { |
| 33 private boolean mDidLaunchBrowser = false; | 41 private boolean mDidLaunchBrowser = false; |
| 34 private boolean mDidFetchSnippets = false; | 42 private boolean mDidFetchSnippets = false; |
| 35 private boolean mDidRescheduleFetching = false; | 43 private boolean mDidRescheduleFetching = false; |
| 36 private boolean mHasPrecacheInstance = true; | 44 private boolean mHasPrecacheInstance = true; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 79 |
| 72 // Posts an assertion task to the UI thread. Since this is only called a
fter the call | 80 // Posts an assertion task to the UI thread. Since this is only called a
fter the call |
| 73 // to onRunTask, it will be enqueued after any possible call to launchBr
owser, and we | 81 // to onRunTask, it will be enqueued after any possible call to launchBr
owser, and we |
| 74 // can reliably check whether launchBrowser was called. | 82 // can reliably check whether launchBrowser was called. |
| 75 protected void checkExpectations(final boolean expectedLaunchBrowser, | 83 protected void checkExpectations(final boolean expectedLaunchBrowser, |
| 76 final boolean expectedPrecacheStarted, final boolean expectedFet
chSnippets, | 84 final boolean expectedPrecacheStarted, final boolean expectedFet
chSnippets, |
| 77 final boolean expectedRescheduleFetching) { | 85 final boolean expectedRescheduleFetching) { |
| 78 ThreadUtils.runOnUiThread(new Runnable() { | 86 ThreadUtils.runOnUiThread(new Runnable() { |
| 79 @Override | 87 @Override |
| 80 public void run() { | 88 public void run() { |
| 81 assertEquals("StartedService", expectedLaunchBrowser, mDidLa
unchBrowser); | 89 Assert.assertEquals("StartedService", expectedLaunchBrowser,
mDidLaunchBrowser); |
| 82 assertEquals("StartedPrecache", expectedPrecacheStarted, mPr
ecachingStarted); | 90 Assert.assertEquals( |
| 83 assertEquals("FetchedSnippets", expectedFetchSnippets, mDidF
etchSnippets); | 91 "StartedPrecache", expectedPrecacheStarted, mPrecach
ingStarted); |
| 84 assertEquals("RescheduledFetching", expectedRescheduleFetchi
ng, | 92 Assert.assertEquals( |
| 93 "FetchedSnippets", expectedFetchSnippets, mDidFetchS
nippets); |
| 94 Assert.assertEquals("RescheduledFetching", expectedReschedul
eFetching, |
| 85 mDidRescheduleFetching); | 95 mDidRescheduleFetching); |
| 86 } | 96 } |
| 87 }); | 97 }); |
| 88 } | 98 } |
| 89 | 99 |
| 90 protected void deletePrecacheInstance() { | 100 protected void deletePrecacheInstance() { |
| 91 mHasPrecacheInstance = false; | 101 mHasPrecacheInstance = false; |
| 92 } | 102 } |
| 93 } | 103 } |
| 94 | 104 |
| 95 @Override | 105 @Before |
| 96 protected void setUp() throws Exception { | 106 public void setUp() throws Exception { |
| 97 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); | 107 mContext = new AdvancedMockContext( |
| 108 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
| 98 BackgroundSyncLauncher.setGCMEnabled(false); | 109 BackgroundSyncLauncher.setGCMEnabled(false); |
| 99 RecordHistogram.setDisabledForTests(true); | 110 RecordHistogram.setDisabledForTests(true); |
| 100 mSyncLauncher = BackgroundSyncLauncher.create(mContext); | 111 mSyncLauncher = BackgroundSyncLauncher.create(mContext); |
| 101 mSnippetsLauncher = SnippetsLauncher.create(mContext); | 112 mSnippetsLauncher = SnippetsLauncher.create(mContext); |
| 102 mTaskService = new MockTaskService(); | 113 mTaskService = new MockTaskService(); |
| 103 } | 114 } |
| 104 | 115 |
| 105 @Override | 116 @After |
| 106 public void tearDown() throws Exception { | 117 public void tearDown() throws Exception { |
| 107 super.tearDown(); | |
| 108 RecordHistogram.setDisabledForTests(false); | 118 RecordHistogram.setDisabledForTests(false); |
| 109 } | 119 } |
| 110 | 120 |
| 111 private void deleteSyncLauncherInstance() { | 121 private void deleteSyncLauncherInstance() { |
| 112 mSyncLauncher.destroy(); | 122 mSyncLauncher.destroy(); |
| 113 mSyncLauncher = null; | 123 mSyncLauncher = null; |
| 114 } | 124 } |
| 115 | 125 |
| 116 private void deleteSnippetsLauncherInstance() { | 126 private void deleteSnippetsLauncherInstance() { |
| 117 mSnippetsLauncher.destroy(); | 127 mSnippetsLauncher.destroy(); |
| 118 mSnippetsLauncher = null; | 128 mSnippetsLauncher = null; |
| 119 } | 129 } |
| 120 | 130 |
| 121 private void startOnRunTaskAndVerify(String taskTag, boolean shouldStart, | 131 private void startOnRunTaskAndVerify(String taskTag, boolean shouldStart, |
| 122 boolean shouldPrecache, boolean shouldFetchSnippets) { | 132 boolean shouldPrecache, boolean shouldFetchSnippets) { |
| 123 mTaskService.onRunTask(new TaskParams(taskTag)); | 133 mTaskService.onRunTask(new TaskParams(taskTag)); |
| 124 mTaskService.checkExpectations(shouldStart, shouldPrecache, shouldFetchS
nippets, false); | 134 mTaskService.checkExpectations(shouldStart, shouldPrecache, shouldFetchS
nippets, false); |
| 125 } | 135 } |
| 126 | 136 |
| 137 @Test |
| 127 @SmallTest | 138 @SmallTest |
| 128 @Feature({"BackgroundSync"}) | 139 @Feature({"BackgroundSync"}) |
| 129 public void testBackgroundSyncNoLaunchBrowserWhenInstanceExists() { | 140 public void testBackgroundSyncNoLaunchBrowserWhenInstanceExists() { |
| 130 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, false, false, f
alse); | 141 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, false, false, f
alse); |
| 131 } | 142 } |
| 132 | 143 |
| 144 @Test |
| 133 @SmallTest | 145 @SmallTest |
| 134 @Feature({"BackgroundSync"}) | 146 @Feature({"BackgroundSync"}) |
| 135 public void testBackgroundSyncLaunchBrowserWhenInstanceDoesNotExist() { | 147 public void testBackgroundSyncLaunchBrowserWhenInstanceDoesNotExist() { |
| 136 deleteSyncLauncherInstance(); | 148 deleteSyncLauncherInstance(); |
| 137 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, true, false, fa
lse); | 149 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, true, false, fa
lse); |
| 138 } | 150 } |
| 139 | 151 |
| 152 @Test |
| 140 @SmallTest | 153 @SmallTest |
| 141 @Feature({"NTPSnippets"}) | 154 @Feature({"NTPSnippets"}) |
| 142 public void testNTPSnippetsFetchWifiNoLaunchBrowserWhenInstanceExists() { | 155 public void testNTPSnippetsFetchWifiNoLaunchBrowserWhenInstanceExists() { |
| 143 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_WIFI, false, false, tr
ue); | 156 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_WIFI, false, false, tr
ue); |
| 144 } | 157 } |
| 145 | 158 |
| 159 @Test |
| 146 @SmallTest | 160 @SmallTest |
| 147 @Feature({"NTPSnippets"}) | 161 @Feature({"NTPSnippets"}) |
| 148 public void testNTPSnippetsFetchFallbackNoLaunchBrowserWhenInstanceExists()
{ | 162 public void testNTPSnippetsFetchFallbackNoLaunchBrowserWhenInstanceExists()
{ |
| 149 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_FALLBACK, false, false
, true); | 163 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_FALLBACK, false, false
, true); |
| 150 } | 164 } |
| 151 | 165 |
| 166 @Test |
| 152 @SmallTest | 167 @SmallTest |
| 153 @Feature({"NTPSnippets"}) | 168 @Feature({"NTPSnippets"}) |
| 154 public void testNTPSnippetsFetchWifiLaunchBrowserWhenInstanceDoesNotExist()
{ | 169 public void testNTPSnippetsFetchWifiLaunchBrowserWhenInstanceDoesNotExist()
{ |
| 155 deleteSnippetsLauncherInstance(); | 170 deleteSnippetsLauncherInstance(); |
| 156 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_WIFI, true, false, tru
e); | 171 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_WIFI, true, false, tru
e); |
| 157 } | 172 } |
| 158 | 173 |
| 174 @Test |
| 159 @SmallTest | 175 @SmallTest |
| 160 @Feature({"NTPSnippets"}) | 176 @Feature({"NTPSnippets"}) |
| 161 public void testNTPSnippetsFetchFallbackLaunchBrowserWhenInstanceDoesNotExis
t() { | 177 public void testNTPSnippetsFetchFallbackLaunchBrowserWhenInstanceDoesNotExis
t() { |
| 162 deleteSnippetsLauncherInstance(); | 178 deleteSnippetsLauncherInstance(); |
| 163 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_FALLBACK, true, false,
true); | 179 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG_FALLBACK, true, false,
true); |
| 164 } | 180 } |
| 165 | 181 |
| 182 @Test |
| 166 @SmallTest | 183 @SmallTest |
| 167 @Feature({"Precache"}) | 184 @Feature({"Precache"}) |
| 168 public void testPrecacheNoLaunchBrowserWhenInstanceExists() { | 185 public void testPrecacheNoLaunchBrowserWhenInstanceExists() { |
| 169 startOnRunTaskAndVerify(PrecacheController.PERIODIC_TASK_TAG, false, fal
se, false); | 186 startOnRunTaskAndVerify(PrecacheController.PERIODIC_TASK_TAG, false, fal
se, false); |
| 170 } | 187 } |
| 171 | 188 |
| 189 @Test |
| 172 @SmallTest | 190 @SmallTest |
| 173 @Feature({"Precache"}) | 191 @Feature({"Precache"}) |
| 174 public void testPrecacheLaunchBrowserWhenInstanceDoesNotExist() { | 192 public void testPrecacheLaunchBrowserWhenInstanceDoesNotExist() { |
| 175 mTaskService.deletePrecacheInstance(); | 193 mTaskService.deletePrecacheInstance(); |
| 176 startOnRunTaskAndVerify(PrecacheController.PERIODIC_TASK_TAG, true, true
, false); | 194 startOnRunTaskAndVerify(PrecacheController.PERIODIC_TASK_TAG, true, true
, false); |
| 177 } | 195 } |
| 178 | 196 |
| 179 private void startOnInitializeTasksAndVerify(boolean shouldStart, boolean sh
ouldReschedule) { | 197 private void startOnInitializeTasksAndVerify(boolean shouldStart, boolean sh
ouldReschedule) { |
| 180 mTaskService.onInitializeTasks(); | 198 mTaskService.onInitializeTasks(); |
| 181 mTaskService.checkExpectations(shouldStart, false, false, shouldReschedu
le); | 199 mTaskService.checkExpectations(shouldStart, false, false, shouldReschedu
le); |
| 182 } | 200 } |
| 183 | 201 |
| 202 @Test |
| 184 @SmallTest | 203 @SmallTest |
| 185 @Feature({"NTPSnippets"}) | 204 @Feature({"NTPSnippets"}) |
| 186 public void testNTPSnippetsNoRescheduleWithoutPrefWhenInstanceExists() { | 205 public void testNTPSnippetsNoRescheduleWithoutPrefWhenInstanceExists() { |
| 187 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/false); | 206 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/false); |
| 188 } | 207 } |
| 189 | 208 |
| 209 @Test |
| 190 @SmallTest | 210 @SmallTest |
| 191 @Feature({"NTPSnippets"}) | 211 @Feature({"NTPSnippets"}) |
| 192 public void testNTPSnippetsNoRescheduleWithoutPrefWhenInstanceDoesNotExist()
{ | 212 public void testNTPSnippetsNoRescheduleWithoutPrefWhenInstanceDoesNotExist()
{ |
| 193 deleteSnippetsLauncherInstance(); | 213 deleteSnippetsLauncherInstance(); |
| 194 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/false); | 214 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/false); |
| 195 } | 215 } |
| 196 | 216 |
| 217 @Test |
| 197 @SmallTest | 218 @SmallTest |
| 198 @Feature({"NTPSnippets"}) | 219 @Feature({"NTPSnippets"}) |
| 199 public void testNTPSnippetsRescheduleWithPrefWhenInstanceExists() { | 220 public void testNTPSnippetsRescheduleWithPrefWhenInstanceExists() { |
| 200 // Set the pref indicating that fetching was scheduled before. | 221 // Set the pref indicating that fetching was scheduled before. |
| 201 ContextUtils.getAppSharedPreferences() | 222 ContextUtils.getAppSharedPreferences() |
| 202 .edit() | 223 .edit() |
| 203 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) | 224 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) |
| 204 .apply(); | 225 .apply(); |
| 205 | 226 |
| 206 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/true); | 227 startOnInitializeTasksAndVerify(/*shouldStart=*/false, /*shouldReschedul
e=*/true); |
| 207 } | 228 } |
| 208 | 229 |
| 230 @Test |
| 209 @SmallTest | 231 @SmallTest |
| 210 @Feature({"NTPSnippets"}) | 232 @Feature({"NTPSnippets"}) |
| 211 public void testNTPSnippetsRescheduleAndLaunchBrowserWithPrefWhenInstanceDoe
sNotExist() { | 233 public void testNTPSnippetsRescheduleAndLaunchBrowserWithPrefWhenInstanceDoe
sNotExist() { |
| 212 deleteSnippetsLauncherInstance(); | 234 deleteSnippetsLauncherInstance(); |
| 213 // Set the pref indicating that fetching was scheduled before. | 235 // Set the pref indicating that fetching was scheduled before. |
| 214 ContextUtils.getAppSharedPreferences() | 236 ContextUtils.getAppSharedPreferences() |
| 215 .edit() | 237 .edit() |
| 216 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) | 238 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) |
| 217 .apply(); | 239 .apply(); |
| 218 | 240 |
| 219 startOnInitializeTasksAndVerify(/*shouldStart=*/true, /*shouldReschedule
=*/true); | 241 startOnInitializeTasksAndVerify(/*shouldStart=*/true, /*shouldReschedule
=*/true); |
| 220 } | 242 } |
| 221 } | 243 } |
| OLD | NEW |