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 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;
15 import com.google.common.annotations.VisibleForTesting;
17 import org.chromium.base.ActivityStatus;
18 import org.chromium.base.test.util.Feature;
19 import org.chromium.chrome.testshell.ChromiumTestShellTestBase;
20 import org.chromium.content.browser.test.util.Criteria;
21 import org.chromium.content.browser.test.util.CriteriaHelper;
22 import org.chromium.sync.signin.AccountManagerHelper;
24 public class DelayedSyncControllerTest extends ChromiumTestShellTestBase {
25 private static final Account TEST_ACCOUNT =
26 AccountManagerHelper.createAccountFromName("something@gmail.com");
27 private static final long WAIT_FOR_LAUNCHER_MS = 10 * 1000;
28 private static final long POLL_INTERVAL_MS = 100;
29 private TestDelayedSyncController mController;
31 private static class TestDelayedSyncController extends DelayedSyncController {
32 private boolean mSyncRequested;
34 private TestDelayedSyncController() {}
37 void requestSyncOnBackgroundThread(Context context, Account account) {
38 mSyncRequested = true;
43 protected void setUp() throws Exception {
45 mController = new TestDelayedSyncController();
46 launchChromiumTestShellWithBlankPage();
51 public void testManualSyncRequestsShouldAlwaysTriggerSync() throws InterruptedException {
52 // Sync should trigger for manual requests when Chrome is in the foreground.
53 assertTrue(isActivityResumed());
54 Bundle extras = new Bundle();
55 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
56 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
58 // Sync should trigger for manual requests when Chrome is in the background.
59 sendChromeToBackground(getActivity());
60 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
61 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
66 public void testSyncRequestsShouldTriggerSyncWhenChromeIsInForeground() {
67 assertTrue(isActivityResumed());
68 Bundle extras = new Bundle();
69 assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
74 public void testSyncRequestsWhenChromeIsInBackgroundShouldBeDelayed()
75 throws InterruptedException {
76 sendChromeToBackground(getActivity());
77 Bundle extras = new Bundle();
78 assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
83 public void testDelayedSyncRequestsShouldBeTriggeredOnResume() throws InterruptedException {
84 // First make sure there are no delayed syncs.
85 mController.clearDelayedSyncs(getActivity());
86 assertFalse(mController.resumeDelayedSyncs(getActivity()));
87 assertFalse(mController.mSyncRequested);
89 // Trying to perform sync when Chrome is in the background should create a delayed sync.
90 sendChromeToBackground(getActivity());
91 Bundle extras = new Bundle();
92 assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT));
94 // Make sure the delayed sync can be resumed.
95 assertTrue(mController.resumeDelayedSyncs(getActivity()));
96 assertTrue(mController.mSyncRequested);
100 static void sendChromeToBackground(Activity activity) throws InterruptedException {
101 Intent intent = new Intent(Intent.ACTION_MAIN);
102 intent.addCategory(Intent.CATEGORY_HOME);
103 activity.startActivity(intent);
105 assertTrue("Activity should have been resumed",
106 CriteriaHelper.pollForCriteria(new Criteria() {
108 public boolean isSatisfied() {
109 return !isActivityResumed();
111 }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS));
114 private static boolean isActivityResumed() {
115 return ActivityStatus.isApplicationVisible();