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; | |
9 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
10 | 9 |
11 import com.google.android.gms.gcm.TaskParams; | 10 import com.google.android.gms.gcm.TaskParams; |
12 | 11 |
13 import org.junit.After; | 12 import org.junit.After; |
14 import org.junit.Assert; | 13 import org.junit.Assert; |
15 import org.junit.Before; | 14 import org.junit.Before; |
16 import org.junit.Test; | 15 import org.junit.Test; |
17 import org.junit.runner.RunWith; | 16 import org.junit.runner.RunWith; |
18 | 17 |
19 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
20 import org.chromium.base.ThreadUtils; | 19 import org.chromium.base.ThreadUtils; |
21 import org.chromium.base.metrics.RecordHistogram; | 20 import org.chromium.base.metrics.RecordHistogram; |
22 import org.chromium.base.test.util.AdvancedMockContext; | |
23 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
24 import org.chromium.base.test.util.RetryOnFailure; | 22 import org.chromium.base.test.util.RetryOnFailure; |
25 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; | 23 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; |
26 import org.chromium.chrome.browser.precache.PrecacheController; | 24 import org.chromium.chrome.browser.precache.PrecacheController; |
27 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | 25 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
28 | 26 |
29 /** | 27 /** |
30 * Tests {@link ChromeBackgroundService}. | 28 * Tests {@link ChromeBackgroundService}. |
31 */ | 29 */ |
32 @RunWith(ChromeJUnit4ClassRunner.class) | 30 @RunWith(ChromeJUnit4ClassRunner.class) |
33 @RetryOnFailure | 31 @RetryOnFailure |
34 public class ChromeBackgroundServiceTest { | 32 public class ChromeBackgroundServiceTest { |
35 private Context mContext; | |
36 private BackgroundSyncLauncher mSyncLauncher; | 33 private BackgroundSyncLauncher mSyncLauncher; |
37 private SnippetsLauncher mSnippetsLauncher; | 34 private SnippetsLauncher mSnippetsLauncher; |
38 private MockTaskService mTaskService; | 35 private MockTaskService mTaskService; |
39 | 36 |
40 static class MockTaskService extends ChromeBackgroundService { | 37 static class MockTaskService extends ChromeBackgroundService { |
41 private boolean mDidLaunchBrowser = false; | 38 private boolean mDidLaunchBrowser = false; |
42 private boolean mDidFetchSnippets = false; | 39 private boolean mDidFetchSnippets = false; |
43 private boolean mDidRescheduleFetching = false; | 40 private boolean mDidRescheduleFetching = false; |
44 private boolean mHasPrecacheInstance = true; | 41 private boolean mHasPrecacheInstance = true; |
45 private boolean mPrecachingStarted = false; | 42 private boolean mPrecachingStarted = false; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 }); | 97 }); |
101 } | 98 } |
102 | 99 |
103 protected void deletePrecacheInstance() { | 100 protected void deletePrecacheInstance() { |
104 mHasPrecacheInstance = false; | 101 mHasPrecacheInstance = false; |
105 } | 102 } |
106 } | 103 } |
107 | 104 |
108 @Before | 105 @Before |
109 public void setUp() throws Exception { | 106 public void setUp() throws Exception { |
110 mContext = new AdvancedMockContext( | |
111 InstrumentationRegistry.getInstrumentation().getTargetContext())
; | |
112 BackgroundSyncLauncher.setGCMEnabled(false); | 107 BackgroundSyncLauncher.setGCMEnabled(false); |
113 RecordHistogram.setDisabledForTests(true); | 108 RecordHistogram.setDisabledForTests(true); |
114 mSyncLauncher = BackgroundSyncLauncher.create(mContext); | 109 mSyncLauncher = BackgroundSyncLauncher.create(); |
115 mSnippetsLauncher = SnippetsLauncher.create(mContext); | 110 mSnippetsLauncher = SnippetsLauncher.create(); |
116 mTaskService = new MockTaskService(); | 111 mTaskService = new MockTaskService(); |
117 } | 112 } |
118 | 113 |
119 @After | 114 @After |
120 public void tearDown() throws Exception { | 115 public void tearDown() throws Exception { |
121 RecordHistogram.setDisabledForTests(false); | 116 RecordHistogram.setDisabledForTests(false); |
122 } | 117 } |
123 | 118 |
124 private void deleteSyncLauncherInstance() { | 119 private void deleteSyncLauncherInstance() { |
125 mSyncLauncher.destroy(); | 120 mSyncLauncher.destroy(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 deleteSnippetsLauncherInstance(); | 232 deleteSnippetsLauncherInstance(); |
238 // Set the pref indicating that fetching was scheduled before. | 233 // Set the pref indicating that fetching was scheduled before. |
239 ContextUtils.getAppSharedPreferences() | 234 ContextUtils.getAppSharedPreferences() |
240 .edit() | 235 .edit() |
241 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) | 236 .putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true) |
242 .apply(); | 237 .apply(); |
243 | 238 |
244 startOnInitializeTasksAndVerify(/*shouldStart=*/true, /*shouldReschedule
=*/true); | 239 startOnInitializeTasksAndVerify(/*shouldStart=*/true, /*shouldReschedule
=*/true); |
245 } | 240 } |
246 } | 241 } |
OLD | NEW |