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 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; |
23 | |
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; |
30 | |
31 | private static class TestDelayedSyncController extends DelayedSyncController { |
32 | private boolean mSyncRequested; |
33 | |
34 | private TestDelayedSyncController() {} |
35 | |
36 | @Override |
37 | void requestSyncOnBackgroundThread(Context context, Account account) { |
38 | mSyncRequested = true; |
39 | } |
40 | } |
41 | |
42 | @Override |
43 | protected void setUp() throws Exception { |
44 | super.setUp(); |
45 | mController = new TestDelayedSyncController(); |
46 | launchChromiumTestShellWithBlankPage(); |
47 | } |
48 | |
49 | @SmallTest |
50 | @Feature({"Sync"}) |
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)); |
57 | |
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)); |
62 | } |
63 | |
64 | @SmallTest |
65 | @Feature({"Sync"}) |
66 | public void testSyncRequestsShouldTriggerSyncWhenChromeIsInForeground() { |
67 | assertTrue(isActivityResumed()); |
68 | Bundle extras = new Bundle(); |
69 | assertTrue(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
70 | } |
71 | |
72 | @SmallTest |
73 | @Feature({"Sync"}) |
74 | public void testSyncRequestsWhenChromeIsInBackgroundShouldBeDelayed() |
75 | throws InterruptedException { |
76 | sendChromeToBackground(getActivity()); |
77 | Bundle extras = new Bundle(); |
78 | assertFalse(mController.shouldPerformSync(getActivity(), extras, TEST_ACCOUNT)); |
79 | } |
80 | |
81 | @SmallTest |
82 | @Feature({"Sync"}) |
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); |
88 | |
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)); |
93 | |
94 | // Make sure the delayed sync can be resumed. |
95 | assertTrue(mController.resumeDelayedSyncs(getActivity())); |
96 | assertTrue(mController.mSyncRequested); |
97 | } |
98 | |
99 | @VisibleForTesting |
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); |
104 | |
105 | assertTrue("Activity should have been resumed", |
106 | CriteriaHelper.pollForCriteria(new Criteria() { |
107 | @Override |
108 | public boolean isSatisfied() { |
109 | return !isActivityResumed(); |
110 | } |
111 | }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS)); |
112 | } |
113 | |
114 | private static boolean isActivityResumed() { |
115 | return ActivityStatus.getState() == ActivityStatus.RESUMED; |
116 | } |
117 | } |