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