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