OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.test.InstrumentationTestCase; |
| 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 |
| 11 import com.google.android.gms.gcm.TaskParams; |
| 12 |
| 13 import org.chromium.base.ThreadUtils; |
| 14 import org.chromium.base.metrics.RecordHistogram; |
| 15 import org.chromium.base.test.util.AdvancedMockContext; |
| 16 import org.chromium.base.test.util.Feature; |
| 17 import org.chromium.chrome.browser.ntp.snippets.SnippetsController; |
| 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; |
| 19 |
| 20 /** |
| 21 * Tests {@link ChromeBackgroundService}. |
| 22 */ |
| 23 public class ChromeBackgroundServiceTest extends InstrumentationTestCase { |
| 24 private Context mContext; |
| 25 private BackgroundSyncLauncher mSyncLauncher; |
| 26 private SnippetsLauncher mSnippetsLauncher; |
| 27 private MockSnippetsController mSnippetsController; |
| 28 private MockTaskService mTaskService; |
| 29 |
| 30 static class MockTaskService extends ChromeBackgroundService { |
| 31 private boolean mDidLaunchBrowser = false; |
| 32 |
| 33 @Override |
| 34 protected void launchBrowser(Context context) { |
| 35 mDidLaunchBrowser = true; |
| 36 } |
| 37 |
| 38 // Posts an assertion task to the UI thread. Since this is only called a
fter the call |
| 39 // to onRunTask, it will be enqueued after any possible call to launchBr
owser, and we |
| 40 // can reliably check whether launchBrowser was called. |
| 41 protected void checkExpectations(final boolean expectedLaunchBrowser) { |
| 42 ThreadUtils.runOnUiThread(new Runnable() { |
| 43 @Override |
| 44 public void run() { |
| 45 assertEquals("StartedService", expectedLaunchBrowser, mDidLa
unchBrowser); |
| 46 } |
| 47 }); |
| 48 } |
| 49 } |
| 50 |
| 51 static class MockSnippetsController extends SnippetsController { |
| 52 private boolean mDidFetchSnippets = false; |
| 53 |
| 54 MockSnippetsController() { |
| 55 super(null); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public void fetchSnippets(boolean overwrite) { |
| 60 mDidFetchSnippets = true; |
| 61 } |
| 62 |
| 63 protected void checkExpectations(final boolean expectedFetchSnippets) { |
| 64 ThreadUtils.runOnUiThread(new Runnable() { |
| 65 @Override |
| 66 public void run() { |
| 67 assertEquals("FetchedSnippets", expectedFetchSnippets, mDidF
etchSnippets); |
| 68 } |
| 69 }); |
| 70 } |
| 71 } |
| 72 |
| 73 @Override |
| 74 protected void setUp() throws Exception { |
| 75 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); |
| 76 BackgroundSyncLauncher.setGCMEnabled(false); |
| 77 RecordHistogram.disableForTests(); |
| 78 mSyncLauncher = BackgroundSyncLauncher.create(mContext); |
| 79 mSnippetsLauncher = SnippetsLauncher.create(mContext); |
| 80 mSnippetsController = new MockSnippetsController(); |
| 81 SnippetsController.setInstanceForTesting(mSnippetsController); |
| 82 mTaskService = new MockTaskService(); |
| 83 } |
| 84 |
| 85 private void deleteSyncLauncherInstance() { |
| 86 mSyncLauncher.destroy(); |
| 87 mSyncLauncher = null; |
| 88 } |
| 89 |
| 90 private void deleteSnippetsLauncherInstance() { |
| 91 mSnippetsLauncher.destroy(); |
| 92 mSnippetsLauncher = null; |
| 93 } |
| 94 |
| 95 private void startOnRunTaskAndVerify(String taskTag, boolean shouldStart, |
| 96 boolean shouldFetchSnippets) { |
| 97 mTaskService.onRunTask(new TaskParams(taskTag)); |
| 98 mTaskService.checkExpectations(shouldStart); |
| 99 mSnippetsController.checkExpectations(shouldFetchSnippets); |
| 100 } |
| 101 |
| 102 @SmallTest |
| 103 @Feature({"BackgroundSync"}) |
| 104 public void testBackgroundSyncNoLaunchBrowserWhenInstanceExists() { |
| 105 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, false, false); |
| 106 } |
| 107 |
| 108 @SmallTest |
| 109 @Feature({"BackgroundSync"}) |
| 110 public void testBackgroundSyncLaunchBrowserWhenInstanceDoesNotExist() { |
| 111 deleteSyncLauncherInstance(); |
| 112 startOnRunTaskAndVerify(BackgroundSyncLauncher.TASK_TAG, true, false); |
| 113 } |
| 114 |
| 115 @SmallTest |
| 116 @Feature({"NTPSnippets"}) |
| 117 public void testNTPSnippetsNoLaunchBrowserWhenInstanceExists() { |
| 118 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG, false, true); |
| 119 } |
| 120 |
| 121 @SmallTest |
| 122 @Feature({"NTPSnippets"}) |
| 123 public void testNTPSnippetsLaunchBrowserWhenInstanceDoesNotExist() { |
| 124 deleteSnippetsLauncherInstance(); |
| 125 startOnRunTaskAndVerify(SnippetsLauncher.TASK_TAG, true, true); |
| 126 } |
| 127 } |
OLD | NEW |