| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.precache; | 5 package org.chromium.chrome.browser.precache; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import com.google.android.gms.gcm.Task; | 10 import com.google.android.gms.gcm.Task; |
| 11 | 11 |
| 12 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.test.util.DisabledTest; | |
| 14 import org.chromium.base.test.util.Feature; | 14 import org.chromium.base.test.util.Feature; |
| 15 import org.chromium.base.test.util.RetryOnFailure; | |
| 16 import org.chromium.chrome.browser.sync.ProfileSyncService; | 15 import org.chromium.chrome.browser.sync.ProfileSyncService; |
| 17 import org.chromium.content.browser.test.NativeLibraryTestBase; | 16 import org.chromium.content.browser.test.NativeLibraryTestBase; |
| 18 | 17 |
| 19 import java.util.EnumSet; | 18 import java.util.EnumSet; |
| 20 import java.util.concurrent.Callable; | 19 import java.util.concurrent.Callable; |
| 21 | 20 |
| 22 /** | 21 /** |
| 23 * Unit tests for {@link PrecacheLauncher}. | 22 * Unit tests for {@link PrecacheLauncher}. |
| 24 * | 23 * |
| 25 * setUp/tearDown code was inspired by org.chromium.chrome.browser.sync.ui.Passp
hraseActivityTest. | 24 * setUp/tearDown code was inspired by org.chromium.chrome.browser.sync.ui.Passp
hraseActivityTest. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 75 |
| 77 @Override | 76 @Override |
| 78 boolean cancelTask(Context context, String tag) { | 77 boolean cancelTask(Context context, String tag) { |
| 79 return true; | 78 return true; |
| 80 } | 79 } |
| 81 } | 80 } |
| 82 | 81 |
| 83 @Override | 82 @Override |
| 84 protected void setUp() throws Exception { | 83 protected void setUp() throws Exception { |
| 85 super.setUp(); | 84 super.setUp(); |
| 85 ContextUtils.initApplicationContext(getTargetContext().getApplicationCon
text()); |
| 86 |
| 86 // This is a PrecacheLauncher with a stubbed out nativeShouldRun so we c
an change that on | 87 // This is a PrecacheLauncher with a stubbed out nativeShouldRun so we c
an change that on |
| 87 // the fly without needing to set up a sync backend. | 88 // the fly without needing to set up a sync backend. |
| 88 mLauncher = new PrecacheLauncherUnderTest(); | 89 mLauncher = new PrecacheLauncherUnderTest(); |
| 89 | 90 |
| 90 mPrecacheTaskScheduler = new MockPrecacheTaskScheduler(); | 91 mPrecacheTaskScheduler = new MockPrecacheTaskScheduler(); |
| 91 PrecacheController.setTaskScheduler(mPrecacheTaskScheduler); | 92 PrecacheController.setTaskScheduler(mPrecacheTaskScheduler); |
| 92 | 93 |
| 93 // The target context persists throughout the entire test run, and so le
aks state between | 94 // The target context persists throughout the entire test run, and so le
aks state between |
| 94 // tests. We reset the is_precaching_enabled pref to false to make the t
est run consistent, | 95 // tests. We reset the is_precaching_enabled pref to false to make the t
est run consistent, |
| 95 // in case another test class has modified this pref. | 96 // in case another test class has modified this pref. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 113 | 114 |
| 114 @Override | 115 @Override |
| 115 protected void tearDown() throws Exception { | 116 protected void tearDown() throws Exception { |
| 116 ProfileSyncService.overrideForTests(null); | 117 ProfileSyncService.overrideForTests(null); |
| 117 PrecacheController.setIsPrecachingEnabled(getTargetContext(), false); | 118 PrecacheController.setIsPrecachingEnabled(getTargetContext(), false); |
| 118 super.tearDown(); | 119 super.tearDown(); |
| 119 } | 120 } |
| 120 | 121 |
| 121 @SmallTest | 122 @SmallTest |
| 122 @Feature({"Precache"}) | 123 @Feature({"Precache"}) |
| 123 @RetryOnFailure | |
| 124 public void testUpdateEnabled_SyncNotReady_ThenDisabled() { | 124 public void testUpdateEnabled_SyncNotReady_ThenDisabled() { |
| 125 mLauncher.updateEnabled(getTargetContext()); | 125 mLauncher.updateEnabled(getTargetContext()); |
| 126 waitUntilUiThreadIdle(); | 126 waitUntilUiThreadIdle(); |
| 127 | 127 |
| 128 assertEquals(false, isPrecachingEnabled()); | 128 assertEquals(false, isPrecachingEnabled()); |
| 129 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, | 129 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, |
| 130 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, | 130 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, |
| 131 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), | 131 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), |
| 132 failureReasons()); | 132 failureReasons()); |
| 133 | 133 |
| 134 setSyncInitialized(true); | 134 setSyncInitialized(true); |
| 135 assertEquals(false, isPrecachingEnabled()); | 135 assertEquals(false, isPrecachingEnabled()); |
| 136 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); | 136 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); |
| 137 } | 137 } |
| 138 | 138 |
| 139 @SmallTest | 139 @SmallTest |
| 140 @Feature({"Precache"}) | 140 @Feature({"Precache"}) |
| 141 @RetryOnFailure | |
| 142 public void testUpdateEnabled_SyncNotReady_ThenEnabled() { | 141 public void testUpdateEnabled_SyncNotReady_ThenEnabled() { |
| 143 mLauncher.updateEnabled(getTargetContext()); | 142 mLauncher.updateEnabled(getTargetContext()); |
| 144 waitUntilUiThreadIdle(); | 143 waitUntilUiThreadIdle(); |
| 145 | 144 |
| 146 assertEquals(false, isPrecachingEnabled()); | 145 assertEquals(false, isPrecachingEnabled()); |
| 147 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, | 146 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, |
| 148 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, | 147 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, |
| 149 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), | 148 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), |
| 150 failureReasons()); | 149 failureReasons()); |
| 151 | 150 |
| 152 mLauncher.setShouldRun(true); | 151 mLauncher.setShouldRun(true); |
| 153 setSyncInitialized(true); | 152 setSyncInitialized(true); |
| 154 assertEquals(true, isPrecachingEnabled()); | 153 assertEquals(true, isPrecachingEnabled()); |
| 155 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); | 154 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); |
| 156 } | 155 } |
| 157 | 156 |
| 158 @SmallTest | 157 @SmallTest |
| 159 @Feature({"Precache"}) | 158 @Feature({"Precache"}) |
| 160 @RetryOnFailure | |
| 161 public void testUpdateEnabled_Disabled_ThenEnabled() { | 159 public void testUpdateEnabled_Disabled_ThenEnabled() { |
| 162 setSyncInitialized(true); | 160 setSyncInitialized(true); |
| 163 mLauncher.updateEnabled(getTargetContext()); | 161 mLauncher.updateEnabled(getTargetContext()); |
| 164 waitUntilUiThreadIdle(); | 162 waitUntilUiThreadIdle(); |
| 165 | 163 |
| 166 assertEquals(false, isPrecachingEnabled()); | 164 assertEquals(false, isPrecachingEnabled()); |
| 167 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); | 165 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); |
| 168 | 166 |
| 169 mLauncher.setShouldRun(true); | 167 mLauncher.setShouldRun(true); |
| 170 assertEquals(true, isPrecachingEnabled()); | 168 assertEquals(true, isPrecachingEnabled()); |
| 171 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); | 169 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); |
| 172 } | 170 } |
| 173 | 171 |
| 174 @SmallTest | 172 @SmallTest |
| 175 @Feature({"Precache"}) | 173 @Feature({"Precache"}) |
| 176 @RetryOnFailure | |
| 177 public void testUpdateEnabled_Enabled_ThenDisabled() { | 174 public void testUpdateEnabled_Enabled_ThenDisabled() { |
| 178 mLauncher.setShouldRun(true); | 175 mLauncher.setShouldRun(true); |
| 179 setSyncInitialized(true); | 176 setSyncInitialized(true); |
| 180 mLauncher.updateEnabled(getTargetContext()); | 177 mLauncher.updateEnabled(getTargetContext()); |
| 181 waitUntilUiThreadIdle(); | 178 waitUntilUiThreadIdle(); |
| 182 | 179 |
| 183 assertEquals(true, isPrecachingEnabled()); | 180 assertEquals(true, isPrecachingEnabled()); |
| 184 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); | 181 assertEquals(EnumSet.noneOf(FailureReason.class), failureReasons()); |
| 185 | 182 |
| 186 mLauncher.setShouldRun(false); | 183 mLauncher.setShouldRun(false); |
| 187 assertEquals(false, isPrecachingEnabled()); | 184 assertEquals(false, isPrecachingEnabled()); |
| 188 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); | 185 assertEquals(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), failu
reReasons()); |
| 189 } | 186 } |
| 190 | 187 |
| 191 @SmallTest | 188 @SmallTest |
| 192 @Feature({"Precache"}) | 189 @Feature({"Precache"}) |
| 193 @RetryOnFailure | |
| 194 @DisabledTest(message = "crbug.com/648749") | |
| 195 public void testUpdateEnabledNullProfileSyncService() { | 190 public void testUpdateEnabledNullProfileSyncService() { |
| 196 ProfileSyncService.overrideForTests(null); | 191 ProfileSyncService.overrideForTests(null); |
| 197 | 192 |
| 198 mLauncher.updateEnabled(getTargetContext()); | 193 mLauncher.updateEnabled(getTargetContext()); |
| 199 waitUntilUiThreadIdle(); | 194 waitUntilUiThreadIdle(); |
| 200 | 195 |
| 201 assertEquals(false, isPrecachingEnabled()); | 196 assertEquals(false, isPrecachingEnabled()); |
| 202 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, | 197 assertEquals(EnumSet.of(FailureReason.SYNC_NOT_INITIALIZED, |
| 203 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, | 198 FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENAB
LED, |
| 204 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), | 199 FailureReason.NATIVE_SHOULD_RUN_IS_FALSE), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 /** Notify listeners that sync preferences have changed. This is run by setS
houldRun. */ | 238 /** Notify listeners that sync preferences have changed. This is run by setS
houldRun. */ |
| 244 private void notifySyncChanged() { | 239 private void notifySyncChanged() { |
| 245 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 240 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 246 @Override | 241 @Override |
| 247 public void run() { | 242 public void run() { |
| 248 mSync.syncStateChanged(); | 243 mSync.syncStateChanged(); |
| 249 } | 244 } |
| 250 }); | 245 }); |
| 251 } | 246 } |
| 252 } | 247 } |
| OLD | NEW |