| OLD | NEW |
| (Empty) |
| 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 | |
| 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 | |
| 16 /** | |
| 17 * Tests {@link BackgroundSyncLauncherService}. | |
| 18 */ | |
| 19 public class BackgroundSyncLauncherServiceTest extends InstrumentationTestCase { | |
| 20 private Context mContext; | |
| 21 private BackgroundSyncLauncher mLauncher; | |
| 22 private MockLauncherService mLauncherService; | |
| 23 | |
| 24 static class MockLauncherService extends BackgroundSyncLauncherService { | |
| 25 private boolean mDidStartService = false; | |
| 26 | |
| 27 @Override | |
| 28 protected void launchBrowser(Context context) { | |
| 29 mDidStartService = true; | |
| 30 } | |
| 31 | |
| 32 // Posts an assertion task to the UI thread. Since this is only called a
fter the call | |
| 33 // to onRunTask, it will be enqueued after any possible call to launchBr
owser, and we | |
| 34 // can reliably check whether launchBrowser was called. | |
| 35 protected void checkExpectations(final boolean expectedStartService) { | |
| 36 ThreadUtils.runOnUiThread(new Runnable() { | |
| 37 @Override | |
| 38 public void run() { | |
| 39 assertEquals("StartedService", expectedStartService, mDidSta
rtService); | |
| 40 } | |
| 41 }); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 @Override | |
| 46 protected void setUp() throws Exception { | |
| 47 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); | |
| 48 BackgroundSyncLauncher.setGCMEnabled(false); | |
| 49 RecordHistogram.disableForTests(); | |
| 50 mLauncher = BackgroundSyncLauncher.create(mContext); | |
| 51 mLauncherService = new MockLauncherService(); | |
| 52 } | |
| 53 | |
| 54 private void deleteLauncherInstance() { | |
| 55 mLauncher.destroy(); | |
| 56 mLauncher = null; | |
| 57 } | |
| 58 | |
| 59 private void startOnRunTaskAndVerify(boolean shouldStart) { | |
| 60 mLauncherService.onRunTask(null); | |
| 61 mLauncherService.checkExpectations(shouldStart); | |
| 62 } | |
| 63 | |
| 64 @SmallTest | |
| 65 @Feature({"BackgroundSync"}) | |
| 66 public void testNoFireWhenInstanceExists() { | |
| 67 startOnRunTaskAndVerify(false); | |
| 68 } | |
| 69 | |
| 70 @SmallTest | |
| 71 @Feature({"BackgroundSync"}) | |
| 72 public void testFiresWhenInstanceDoesNotExist() { | |
| 73 deleteLauncherInstance(); | |
| 74 startOnRunTaskAndVerify(true); | |
| 75 } | |
| 76 } | |
| OLD | NEW |