| 1 | // Copyright 2013 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.sync; |
| 6 | |
| 7 | import android.accounts.Account; |
| 8 | import android.content.ContentResolver; |
| 9 | import android.content.Context; |
| 10 | import android.content.Intent; |
| 11 | import android.os.Bundle; |
| 12 | import android.test.suitebuilder.annotation.SmallTest; |
| 13 | |
| 14 | import org.chromium.base.ActivityStatus; |
| 15 | import org.chromium.base.test.util.Feature; |
| 16 | import org.chromium.chrome.testshell.ChromiumTestShellTestBase; |
| 17 | import org.chromium.content.browser.test.util.Criteria; |
| 18 | import org.chromium.content.browser.test.util.CriteriaHelper; |
| 19 | import org.chromium.sync.signin.AccountManagerHelper; |
| 20 | |
| 21 | public class DelayedSyncControllerTest extends ChromiumTestShellTestBase { |
| 22 | private static final Account TEST_ACCOUNT = |
| 23 | AccountManagerHelper.createAccountFromName("something@gmail.com"); |
| 24 | private static final long WAIT_FOR_LAUNCHER_MS = 10 * 1000; |
| 25 | private static final long POLL_INTERVAL_MS = 100; |
| 26 | private TestDelayedSyncController mController; |
| 27 | |
| 28 | private static class TestDelayedSyncController extends DelayedSyncController { |
| 29 | private boolean mSyncRequested; |
| 30 | |
| 31 | private TestDelayedSyncController() {} |
| 32 | |
| 33 | @Override |
| 34 | void requestSyncOnBackgroundThread(Context context, Account account) { |
| 35 | mSyncRequested = true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected void setUp() throws Exception { |
| 41 | super.setUp(); |
| 42 | mController = new TestDelayedSyncController(); |
| 43 | launchChromiumTestShellWithBlankPage(); |
| 44 | } |
| 45 | |
| 46 | @SmallTest |
| 47 | @Feature({"Sync"}) |
| 48 | public void testManualSyncRequestsShouldAlwaysTriggerSync() throws InterruptedException { |
| 49 | // Sync should trigger for manual requests when Chrome is in the foreground. |
| 50 | assertTrue(isActivityResumed()); |
| 51 | Bundle extras = new Bundle(); |
| 52 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 53 | assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
| 54 | |
| 55 | // Sync should trigger for manual requests when Chrome is in the background. |
| 56 | sendChromeToBackground(); |
| 57 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 58 | assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
| 59 | } |
| 60 | |
| 61 | @SmallTest |
| 62 | @Feature({"Sync"}) |
| 63 | public void testSyncRequestsShouldTriggerSyncWhenChromeIsInForeground() { |
| 64 | assertTrue(isActivityResumed()); |
| 65 | Bundle extras = new Bundle(); |
| 66 | assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
| 67 | } |
| 68 | |
| 69 | @SmallTest |
| 70 | @Feature({"Sync"}) |
| 71 | public void testSyncRequestsWhenChromeIsInBackgroundShouldBeDelayed() |
| 72 | throws InterruptedException { |
| 73 | sendChromeToBackground(); |
| 74 | Bundle extras = new Bundle(); |
| 75 | assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
| 76 | } |
| 77 | |
| 78 | @SmallTest |
| 79 | @Feature({"Sync"}) |
| 80 | public void testDelayedSyncRequestsShouldBeTriggeredOnResume() throws InterruptedException { |
| 81 | // First make sure there are no delayed syncs. |
| 82 | mController.clearDelayedSyncs(getActivity()); |
| 83 | assertFalse(mController.resumeDelayedSyncs(getActivity())); |
| 84 | assertFalse(mController.mSyncRequested); |
| 85 | |
| 86 | // Trying to perform sync when Chrome is in the background should create a delayed sync. |
| 87 | sendChromeToBackground(); |
| 88 | Bundle extras = new Bundle(); |
| 89 | assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
| 90 | |
| 91 | // Make sure the delayed sync can be resumed. |
| 92 | assertTrue(mController.resumeDelayedSyncs(getActivity())); |
| 93 | assertTrue(mController.mSyncRequested); |
| 94 | } |
| 95 | |
| 96 | private void sendChromeToBackground() throws InterruptedException { |
| 97 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 98 | intent.addCategory(Intent.CATEGORY_HOME); |
| 99 | getActivity().startActivity(intent); |
| 100 | |
| 101 | assertTrue("Activity should have been resumed", |
| 102 | CriteriaHelper.pollForCriteria(new Criteria() { |
| 103 | @Override |
| 104 | public boolean isSatisfied() { |
| 105 | return !isActivityResumed(); |
| 106 | } |
| 107 | }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS)); |
| 108 | } |
| 109 | |
| 110 | private static boolean isActivityResumed() { |
| 111 | return ActivityStatus.getState() == ActivityStatus.RESUMED; |
| 112 | } |
| 113 | } |