OLD | NEW |
| (Empty) |
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.util.Log; | |
10 | |
11 import org.chromium.base.ThreadUtils; | |
12 import org.chromium.base.test.util.HostDrivenTest; | |
13 import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator; | |
14 import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory
; | |
15 import org.chromium.chrome.browser.identity.UuidBasedUniqueIdentificationGenerat
or; | |
16 import org.chromium.chrome.shell.ChromeShellActivity; | |
17 import org.chromium.chrome.shell.ChromeShellTestBase; | |
18 import org.chromium.chrome.shell.sync.SyncController; | |
19 import org.chromium.chrome.test.util.browser.sync.SyncTestUtil; | |
20 import org.chromium.content.browser.ContentViewCore; | |
21 import org.chromium.content.browser.test.util.Criteria; | |
22 import org.chromium.content.browser.test.util.CriteriaHelper; | |
23 import org.chromium.content.browser.test.util.JavaScriptUtils; | |
24 import org.chromium.sync.notifier.SyncStatusHelper; | |
25 import org.chromium.sync.signin.AccountManagerHelper; | |
26 import org.chromium.sync.signin.ChromeSigninController; | |
27 import org.chromium.sync.test.util.MockAccountManager; | |
28 import org.chromium.sync.test.util.MockSyncContentResolverDelegate; | |
29 | |
30 import java.util.concurrent.TimeoutException; | |
31 | |
32 /** | |
33 * Test suite for Sync. | |
34 */ | |
35 public class SyncTest extends ChromeShellTestBase { | |
36 private static final String TAG = "SyncTest"; | |
37 | |
38 private static final String FOREIGN_SESSION_TEST_MACHINE_ID = | |
39 "DeleteForeignSessionTest_Machine_1"; | |
40 | |
41 private SyncTestUtil.SyncTestContext mContext; | |
42 private MockAccountManager mAccountManager; | |
43 private SyncController mSyncController; | |
44 | |
45 @Override | |
46 protected void setUp() throws Exception { | |
47 super.setUp(); | |
48 | |
49 clearAppData(); | |
50 | |
51 // Mock out the account manager on the device. | |
52 mContext = new SyncTestUtil.SyncTestContext(getInstrumentation().getTarg
etContext()); | |
53 mAccountManager = new MockAccountManager(mContext, getInstrumentation().
getContext()); | |
54 AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAcc
ountManager); | |
55 MockSyncContentResolverDelegate syncContentResolverDelegate = | |
56 new MockSyncContentResolverDelegate(); | |
57 syncContentResolverDelegate.setMasterSyncAutomatically(true); | |
58 SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, syncContentR
esolverDelegate); | |
59 // This call initializes the ChromeSigninController to use our test cont
ext. | |
60 ChromeSigninController.get(mContext); | |
61 startChromeBrowserProcessSync(getInstrumentation().getTargetContext()); | |
62 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
63 @Override | |
64 public void run() { | |
65 mSyncController = SyncController.get(mContext); | |
66 } | |
67 }); | |
68 SyncTestUtil.verifySyncServerIsRunning(); | |
69 } | |
70 | |
71 @HostDrivenTest | |
72 public void testGetAboutSyncInfoYieldsValidData() throws Throwable { | |
73 setupTestAccountAndSignInToSync(FOREIGN_SESSION_TEST_MACHINE_ID); | |
74 | |
75 final SyncTestUtil.AboutSyncInfoGetter syncInfoGetter = | |
76 new SyncTestUtil.AboutSyncInfoGetter(getActivity()); | |
77 runTestOnUiThread(syncInfoGetter); | |
78 | |
79 boolean gotInfo = CriteriaHelper.pollForCriteria(new Criteria() { | |
80 @Override | |
81 public boolean isSatisfied() { | |
82 return !syncInfoGetter.getAboutInfo().isEmpty(); | |
83 } | |
84 }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); | |
85 | |
86 assertTrue("Couldn't get about info.", gotInfo); | |
87 } | |
88 | |
89 @HostDrivenTest | |
90 public void testAboutSyncPageDisplaysCurrentSyncStatus() throws InterruptedE
xception { | |
91 setupTestAccountAndSignInToSync(FOREIGN_SESSION_TEST_MACHINE_ID); | |
92 | |
93 loadUrlWithSanitization("chrome://sync"); | |
94 SyncTestUtil.AboutSyncInfoGetter aboutInfoGetter = | |
95 new SyncTestUtil.AboutSyncInfoGetter(getActivity()); | |
96 try { | |
97 runTestOnUiThread(aboutInfoGetter); | |
98 } catch (Throwable t) { | |
99 Log.w(TAG, | |
100 "Exception while trying to fetch about sync info from Profil
eSyncService.", t); | |
101 fail("Unable to fetch sync info from ProfileSyncService."); | |
102 } | |
103 assertFalse("About sync info should not be empty.", | |
104 aboutInfoGetter.getAboutInfo().isEmpty()); | |
105 assertTrue("About sync info should have sync summary status.", | |
106 aboutInfoGetter.getAboutInfo().containsKey(SyncTestUtil.SYNC_SUM
MARY_STATUS)); | |
107 final String expectedSyncSummary = | |
108 aboutInfoGetter.getAboutInfo().get(SyncTestUtil.SYNC_SUMMARY_STA
TUS); | |
109 | |
110 Criteria checker = new Criteria() { | |
111 @Override | |
112 public boolean isSatisfied() { | |
113 final ContentViewCore contentViewCore = getContentViewCore(getAc
tivity()); | |
114 String innerHtml = ""; | |
115 try { | |
116 innerHtml = JavaScriptUtils.executeJavaScriptAndWaitForResul
t( | |
117 contentViewCore, "document.documentElement.innerHTML
"); | |
118 } catch (InterruptedException e) { | |
119 Log.w(TAG, "Interrupted while polling about:sync page for sy
nc status.", e); | |
120 } catch (TimeoutException e) { | |
121 Log.w(TAG, "Interrupted while polling about:sync page for sy
nc status.", e); | |
122 } | |
123 return innerHtml.contains(expectedSyncSummary); | |
124 } | |
125 | |
126 }; | |
127 boolean hadExpectedStatus = CriteriaHelper.pollForCriteria( | |
128 checker, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL
_MS); | |
129 assertTrue("Sync status not present on about sync page: " + expectedSync
Summary, | |
130 hadExpectedStatus); | |
131 } | |
132 | |
133 @HostDrivenTest | |
134 public void testDisableAndEnableSync() throws InterruptedException { | |
135 setupTestAccountAndSignInToSync(FOREIGN_SESSION_TEST_MACHINE_ID); | |
136 Account account = | |
137 AccountManagerHelper.createAccountFromName(SyncTestUtil.DEFAULT_
TEST_ACCOUNT); | |
138 | |
139 // Disabling Android sync should turn Chrome sync engine off. | |
140 SyncStatusHelper.get(mContext).disableAndroidSync(account); | |
141 SyncTestUtil.verifySyncIsDisabled(mContext, account); | |
142 | |
143 // Enabling Android sync should turn Chrome sync engine on. | |
144 SyncStatusHelper.get(mContext).enableAndroidSync(account); | |
145 SyncTestUtil.ensureSyncInitialized(mContext); | |
146 SyncTestUtil.verifySignedInWithAccount(mContext, account); | |
147 } | |
148 | |
149 private void setupTestAccountAndSignInToSync( | |
150 final String syncClientIdentifier) | |
151 throws InterruptedException { | |
152 Account defaultTestAccount = SyncTestUtil.setupTestAccountThatAcceptsAll
AuthTokens( | |
153 mAccountManager, SyncTestUtil.DEFAULT_TEST_ACCOUNT, SyncTestUtil
.DEFAULT_PASSWORD); | |
154 | |
155 UniqueIdentificationGeneratorFactory.registerGenerator( | |
156 UuidBasedUniqueIdentificationGenerator.GENERATOR_ID, | |
157 new UniqueIdentificationGenerator() { | |
158 @Override | |
159 public String getUniqueId(String salt) { | |
160 return syncClientIdentifier; | |
161 } | |
162 }, true); | |
163 | |
164 SyncTestUtil.verifySyncIsSignedOut(getActivity()); | |
165 | |
166 final Activity activity = launchChromeShellWithBlankPage(); | |
167 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
168 @Override | |
169 public void run() { | |
170 mSyncController.signIn(activity, SyncTestUtil.DEFAULT_TEST_ACCOU
NT); | |
171 } | |
172 }); | |
173 | |
174 SyncTestUtil.verifySyncIsSignedIn(mContext, defaultTestAccount); | |
175 assertTrue("Sync everything should be enabled", | |
176 SyncTestUtil.isSyncEverythingEnabled(mContext)); | |
177 } | |
178 | |
179 private static ContentViewCore getContentViewCore(ChromeShellActivity activi
ty) { | |
180 return activity.getActiveContentViewCore(); | |
181 } | |
182 } | |
OLD | NEW |