DelayedSyncControllerTest.java revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 5package org.chromium.chrome.browser.sync; 6 7import android.accounts.Account; 8import android.app.Activity; 9import android.content.ContentResolver; 10import android.content.Context; 11import android.content.Intent; 12import android.os.Bundle; 13import android.test.suitebuilder.annotation.SmallTest; 14 15import com.google.common.annotations.VisibleForTesting; 16 17import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; 18 19import org.chromium.base.ApplicationStatus; 20import org.chromium.base.test.util.Feature; 21import org.chromium.chrome.shell.ChromeShellTestBase; 22import org.chromium.content.browser.test.util.Criteria; 23import org.chromium.content.browser.test.util.CriteriaHelper; 24import org.chromium.sync.signin.AccountManagerHelper; 25 26public class DelayedSyncControllerTest extends ChromeShellTestBase { 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 36 private TestDelayedSyncController() {} 37 38 @Override 39 void requestSyncOnBackgroundThread(Context context, Account account) { 40 mSyncRequested = true; 41 } 42 } 43 44 @Override 45 protected void setUp() throws Exception { 46 super.setUp(); 47 mController = new TestDelayedSyncController(); 48 launchChromeShellWithBlankPage(); 49 } 50 51 @SmallTest 52 @Feature({"Sync"}) 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"}) 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"}) 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"}) 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 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 116 private static boolean isActivityResumed() { 117 return ApplicationStatus.hasVisibleActivities(); 118 } 119} 120