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