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.
5 package org.chromium.chrome.browser.sync;
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
9 import android.accounts.Account;
10 import android.app.Activity;
11 import android.content.ContentResolver;
12 import android.content.Context;
13 import android.content.Intent;
14 import android.os.Bundle;
15 import android.test.suitebuilder.annotation.SmallTest;
17 import org.chromium.base.ApplicationStatus;
18 import org.chromium.base.VisibleForTesting;
19 import org.chromium.base.test.util.Feature;
20 import org.chromium.chrome.shell.ChromeShellTestBase;
21 import org.chromium.content.browser.test.util.Criteria;
22 import org.chromium.content.browser.test.util.CriteriaHelper;
23 import org.chromium.sync.signin.AccountManagerHelper;
25 public class DelayedSyncControllerTest extends ChromeShellTestBase {
26 private static final Account TEST_ACCOUNT =
27 AccountManagerHelper.createAccountFromName("something@gmail.com");
28 private static final long WAIT_FOR_LAUNCHER_MS = scaleTimeout(10 * 1000);
29 private static final long POLL_INTERVAL_MS = 100;
30 private TestDelayedSyncController mController;
32 private static class TestDelayedSyncController extends DelayedSyncController {
33 private boolean mSyncRequested;
35 private TestDelayedSyncController() {}
38 void requestSyncOnBackgroundThread(Context context, Account account) {
39 mSyncRequested = true;
44 protected void setUp() throws Exception {
46 mController = new TestDelayedSyncController();
47 launchChromeShellWithBlankPage();
52 public void testManualSyncRequestsShouldAlwaysTriggerSync() throws InterruptedException {
53 // Sync should trigger for manual requests when Chrome is in the foreground.
54 assertTrue(isActivityResumed());
55 Bundle extras = new Bundle();
56 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
57 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
59 // Sync should trigger for manual requests when Chrome is in the background.
60 sendChromeToBackground(getActivity());
61 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
62 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
67 public void testSyncRequestsShouldTriggerSyncWhenChromeIsInForeground() {
68 assertTrue(isActivityResumed());
69 Bundle extras = new Bundle();
70 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
75 public void testSyncRequestsWhenChromeIsInBackgroundShouldBeDelayed()
76 throws InterruptedException {
77 sendChromeToBackground(getActivity());
78 Bundle extras = new Bundle();
79 assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
84 public void testDelayedSyncRequestsShouldBeTriggeredOnResume() throws InterruptedException {
85 // First make sure there are no delayed syncs.
86 mController.clearDelayedSyncs(getActivity());
87 assertFalse(mController.resumeDelayedSyncs(getActivity()));
88 assertFalse(mController.mSyncRequested);
90 // Trying to perform sync when Chrome is in the background should create a delayed sync.
91 sendChromeToBackground(getActivity());
92 Bundle extras = new Bundle();
93 assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
95 // Make sure the delayed sync can be resumed.
96 assertTrue(mController.resumeDelayedSyncs(getActivity()));
97 assertTrue(mController.mSyncRequested);
101 static void sendChromeToBackground(Activity activity) throws InterruptedException {
102 Intent intent = new Intent(Intent.ACTION_MAIN);
103 intent.addCategory(Intent.CATEGORY_HOME);
104 activity.startActivity(intent);
106 assertTrue("Activity should have been resumed",
107 CriteriaHelper.pollForCriteria(new Criteria() {
109 public boolean isSatisfied() {
110 return !isActivityResumed();
112 }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS));
115 private static boolean isActivityResumed() {
116 return ApplicationStatus.hasVisibleActivities();