1 // Copyright (c) 2012 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 #include "base/basictypes.h"
6 #include "chrome/browser/extensions/extension_service.h"
7 #include "chrome/browser/extensions/extension_sorting.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sync/profile_sync_service_harness.h"
10 #include "chrome/browser/sync/test/integration/apps_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_app_helper.h"
12 #include "chrome/browser/sync/test/integration/sync_test.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "sync/api/string_ordinal.h"
16 using apps_helper::AllProfilesHaveSameAppsAsVerifier;
17 using apps_helper::CopyNTPOrdinals;
18 using apps_helper::DisableApp;
19 using apps_helper::EnableApp;
20 using apps_helper::FixNTPOrdinalCollisions;
21 using apps_helper::GetAppLaunchOrdinalForApp;
22 using apps_helper::HasSameAppsAsVerifier;
23 using apps_helper::IncognitoDisableApp;
24 using apps_helper::IncognitoEnableApp;
25 using apps_helper::InstallApp;
26 using apps_helper::InstallAppsPendingForSync;
27 using apps_helper::InstallPlatformApp;
28 using apps_helper::SetAppLaunchOrdinalForApp;
29 using apps_helper::SetPageOrdinalForApp;
30 using apps_helper::UninstallApp;
32 class TwoClientAppsSyncTest : public SyncTest {
34 TwoClientAppsSyncTest() : SyncTest(TWO_CLIENT) {}
36 virtual ~TwoClientAppsSyncTest() {}
39 DISALLOW_COPY_AND_ASSIGN(TwoClientAppsSyncTest);
42 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithNoApps) {
43 ASSERT_TRUE(SetupSync());
45 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
48 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithSameApps) {
49 ASSERT_TRUE(SetupClients());
51 const int kNumApps = 5;
52 for (int i = 0; i < kNumApps; ++i) {
53 InstallApp(GetProfile(0), i);
54 InstallApp(GetProfile(1), i);
55 InstallApp(verifier(), i);
58 ASSERT_TRUE(SetupSync());
60 ASSERT_TRUE(AwaitQuiescence());
62 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
65 // Install some apps on both clients, some on only one client, some on only the
66 // other, and sync. Both clients should end up with all apps, and the app and
67 // page ordinals should be identical.
68 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithDifferentApps) {
69 ASSERT_TRUE(SetupClients());
73 const int kNumCommonApps = 5;
74 for (int j = 0; j < kNumCommonApps; ++i, ++j) {
75 InstallApp(GetProfile(0), i);
76 InstallApp(GetProfile(1), i);
77 InstallApp(verifier(), i);
80 const int kNumProfile0Apps = 10;
81 for (int j = 0; j < kNumProfile0Apps; ++i, ++j) {
82 InstallApp(GetProfile(0), i);
83 InstallApp(verifier(), i);
84 CopyNTPOrdinals(GetProfile(0), verifier(), i);
87 const int kNumProfile1Apps = 10;
88 for (int j = 0; j < kNumProfile1Apps; ++i, ++j) {
89 InstallApp(GetProfile(1), i);
90 InstallApp(verifier(), i);
91 CopyNTPOrdinals(GetProfile(1), verifier(), i);
94 const int kNumPlatformApps = 5;
95 for (int j = 0; j < kNumPlatformApps; ++i, ++j) {
96 InstallPlatformApp(GetProfile(1), i);
97 InstallPlatformApp(verifier(), i);
98 CopyNTPOrdinals(GetProfile(1), verifier(), i);
101 FixNTPOrdinalCollisions(verifier());
103 ASSERT_TRUE(SetupSync());
105 ASSERT_TRUE(AwaitQuiescence());
107 InstallAppsPendingForSync(GetProfile(0));
108 InstallAppsPendingForSync(GetProfile(1));
110 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
113 // Install some apps on both clients, then sync. Then install some apps on only
114 // one client, some on only the other, and then sync again. Both clients should
115 // end up with all apps, and the app and page ordinals should be identical.
116 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, InstallDifferentApps) {
117 ASSERT_TRUE(SetupClients());
121 const int kNumCommonApps = 5;
122 for (int j = 0; j < kNumCommonApps; ++i, ++j) {
123 InstallApp(GetProfile(0), i);
124 InstallApp(GetProfile(1), i);
125 InstallApp(verifier(), i);
128 ASSERT_TRUE(SetupSync());
130 ASSERT_TRUE(AwaitQuiescence());
132 const int kNumProfile0Apps = 10;
133 for (int j = 0; j < kNumProfile0Apps; ++i, ++j) {
134 InstallApp(GetProfile(0), i);
135 InstallApp(verifier(), i);
136 CopyNTPOrdinals(GetProfile(0), verifier(), i);
139 const int kNumProfile1Apps = 10;
140 for (int j = 0; j < kNumProfile1Apps; ++i, ++j) {
141 InstallApp(GetProfile(1), i);
142 InstallApp(verifier(), i);
143 CopyNTPOrdinals(GetProfile(1), verifier(), i);
146 FixNTPOrdinalCollisions(verifier());
148 ASSERT_TRUE(AwaitQuiescence());
150 InstallAppsPendingForSync(GetProfile(0));
151 InstallAppsPendingForSync(GetProfile(1));
153 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
157 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Add) {
158 ASSERT_TRUE(SetupSync());
159 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
161 InstallApp(GetProfile(0), 0);
162 InstallApp(verifier(), 0);
163 ASSERT_TRUE(AwaitQuiescence());
165 InstallAppsPendingForSync(GetProfile(0));
166 InstallAppsPendingForSync(GetProfile(1));
167 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
171 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Uninstall) {
172 ASSERT_TRUE(SetupSync());
173 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
175 InstallApp(GetProfile(0), 0);
176 InstallApp(verifier(), 0);
177 ASSERT_TRUE(AwaitQuiescence());
179 InstallAppsPendingForSync(GetProfile(0));
180 InstallAppsPendingForSync(GetProfile(1));
181 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
183 UninstallApp(GetProfile(0), 0);
184 UninstallApp(verifier(), 0);
185 ASSERT_TRUE(AwaitQuiescence());
186 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
189 // Install an app on one client, then sync. Then uninstall the app on the first
190 // client and sync again. Now install a new app on the first client and sync.
191 // Both client should only have the second app, with identical app and page
193 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UninstallThenInstall) {
194 ASSERT_TRUE(SetupSync());
195 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
197 InstallApp(GetProfile(0), 0);
198 InstallApp(verifier(), 0);
199 ASSERT_TRUE(AwaitQuiescence());
201 InstallAppsPendingForSync(GetProfile(0));
202 InstallAppsPendingForSync(GetProfile(1));
203 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
205 UninstallApp(GetProfile(0), 0);
206 UninstallApp(verifier(), 0);
207 ASSERT_TRUE(AwaitQuiescence());
208 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
210 InstallApp(GetProfile(0), 1);
211 InstallApp(verifier(), 1);
212 ASSERT_TRUE(AwaitQuiescence());
213 InstallAppsPendingForSync(GetProfile(0));
214 InstallAppsPendingForSync(GetProfile(1));
215 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
219 // Flaky: http://crbug.com/226055
220 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DISABLED_Merge) {
221 ASSERT_TRUE(SetupSync());
222 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
224 InstallApp(GetProfile(0), 0);
225 InstallApp(GetProfile(1), 0);
226 ASSERT_TRUE(AwaitQuiescence());
228 UninstallApp(GetProfile(0), 0);
229 InstallApp(GetProfile(0), 1);
230 InstallApp(verifier(), 1);
232 InstallApp(GetProfile(0), 2);
233 InstallApp(GetProfile(1), 2);
234 InstallApp(verifier(), 2);
236 InstallApp(GetProfile(1), 3);
237 InstallApp(verifier(), 3);
239 ASSERT_TRUE(AwaitQuiescence());
240 InstallAppsPendingForSync(GetProfile(0));
241 InstallAppsPendingForSync(GetProfile(1));
242 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
246 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateEnableDisableApp) {
247 ASSERT_TRUE(SetupSync());
248 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
250 InstallApp(GetProfile(0), 0);
251 InstallApp(GetProfile(1), 0);
252 InstallApp(verifier(), 0);
253 ASSERT_TRUE(AwaitQuiescence());
254 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
256 DisableApp(GetProfile(0), 0);
257 DisableApp(verifier(), 0);
258 ASSERT_TRUE(HasSameAppsAsVerifier(0));
259 ASSERT_FALSE(HasSameAppsAsVerifier(1));
261 ASSERT_TRUE(AwaitQuiescence());
262 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
264 EnableApp(GetProfile(1), 0);
265 EnableApp(verifier(), 0);
266 ASSERT_TRUE(HasSameAppsAsVerifier(1));
267 ASSERT_FALSE(HasSameAppsAsVerifier(0));
269 ASSERT_TRUE(AwaitQuiescence());
270 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
274 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateIncognitoEnableDisable) {
275 ASSERT_TRUE(SetupSync());
276 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
278 InstallApp(GetProfile(0), 0);
279 InstallApp(GetProfile(1), 0);
280 InstallApp(verifier(), 0);
281 ASSERT_TRUE(AwaitQuiescence());
282 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
284 IncognitoEnableApp(GetProfile(0), 0);
285 IncognitoEnableApp(verifier(), 0);
286 ASSERT_TRUE(HasSameAppsAsVerifier(0));
287 ASSERT_FALSE(HasSameAppsAsVerifier(1));
289 ASSERT_TRUE(AwaitQuiescence());
290 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
292 IncognitoDisableApp(GetProfile(1), 0);
293 IncognitoDisableApp(verifier(), 0);
294 ASSERT_TRUE(HasSameAppsAsVerifier(1));
295 ASSERT_FALSE(HasSameAppsAsVerifier(0));
297 ASSERT_TRUE(AwaitQuiescence());
298 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
302 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableApps) {
303 ASSERT_TRUE(SetupSync());
304 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
306 ASSERT_TRUE(GetClient(1)->DisableSyncForDatatype(syncer::APPS));
307 InstallApp(GetProfile(0), 0);
308 InstallApp(verifier(), 0);
309 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Installed an app."));
310 ASSERT_TRUE(HasSameAppsAsVerifier(0));
311 ASSERT_FALSE(HasSameAppsAsVerifier(1));
313 ASSERT_TRUE(GetClient(1)->EnableSyncForDatatype(syncer::APPS));
314 ASSERT_TRUE(AwaitQuiescence());
316 InstallAppsPendingForSync(GetProfile(0));
317 InstallAppsPendingForSync(GetProfile(1));
318 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
321 // Disable sync for the second client and then install an app on the first
322 // client, then enable sync on the second client. Both clients should have the
323 // same app with identical app and page ordinals.
325 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableSync) {
326 ASSERT_TRUE(SetupSync());
327 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
329 ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
330 InstallApp(GetProfile(0), 0);
331 InstallApp(verifier(), 0);
332 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Installed an app."));
333 ASSERT_TRUE(HasSameAppsAsVerifier(0));
334 ASSERT_FALSE(HasSameAppsAsVerifier(1));
336 ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
337 ASSERT_TRUE(AwaitQuiescence());
339 InstallAppsPendingForSync(GetProfile(0));
340 InstallAppsPendingForSync(GetProfile(1));
341 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
344 // Install the same app on both clients, then sync. Change the page ordinal on
345 // one client and sync. Both clients should have the updated page ordinal for
347 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdatePageOrdinal) {
348 ASSERT_TRUE(SetupSync());
349 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
351 syncer::StringOrdinal initial_page =
352 syncer::StringOrdinal::CreateInitialOrdinal();
353 InstallApp(GetProfile(0), 0);
354 InstallApp(GetProfile(1), 0);
355 InstallApp(verifier(), 0);
356 ASSERT_TRUE(AwaitQuiescence());
357 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
359 syncer::StringOrdinal second_page = initial_page.CreateAfter();
360 SetPageOrdinalForApp(GetProfile(0), 0, second_page);
361 SetPageOrdinalForApp(verifier(), 0, second_page);
362 ASSERT_TRUE(AwaitQuiescence());
363 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
366 // Install the same app on both clients, then sync. Change the app launch
367 // ordinal on one client and sync. Both clients should have the updated app
368 // launch ordinal for the app.
369 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateAppLaunchOrdinal) {
370 ASSERT_TRUE(SetupSync());
371 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
373 InstallApp(GetProfile(0), 0);
374 InstallApp(GetProfile(1), 0);
375 InstallApp(verifier(), 0);
376 ASSERT_TRUE(AwaitQuiescence());
377 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
379 syncer::StringOrdinal initial_position =
380 GetAppLaunchOrdinalForApp(GetProfile(0), 0);
382 syncer::StringOrdinal second_position = initial_position.CreateAfter();
383 SetAppLaunchOrdinalForApp(GetProfile(0), 0, second_position);
384 SetAppLaunchOrdinalForApp(verifier(), 0, second_position);
385 ASSERT_TRUE(AwaitQuiescence());
386 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
389 // Adjust the CWS location within a page on the first client and sync. Adjust
390 // which page the CWS appears on and sync. Both clients should have the same
391 // page and app launch ordinal values for the CWS.
392 IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateCWSOrdinals) {
393 ASSERT_TRUE(SetupSync());
394 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
396 // Change the app launch ordinal.
397 syncer::StringOrdinal cws_app_launch_ordinal =
398 GetProfile(0)->GetExtensionService()->
399 extension_prefs()->extension_sorting()->GetAppLaunchOrdinal(
400 extension_misc::kWebStoreAppId);
401 GetProfile(0)->GetExtensionService()->extension_prefs()->extension_sorting()->
403 extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
404 verifier()->GetExtensionService()->extension_prefs()->extension_sorting()->
406 extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
407 ASSERT_TRUE(AwaitQuiescence());
408 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
410 // Change the page ordinal.
411 syncer::StringOrdinal cws_page_ordinal =
412 GetProfile(1)->GetExtensionService()->
413 extension_prefs()->extension_sorting()->GetPageOrdinal(
414 extension_misc::kWebStoreAppId);
415 GetProfile(1)->GetExtensionService()->extension_prefs()->
416 extension_sorting()->SetPageOrdinal(extension_misc::kWebStoreAppId,
417 cws_page_ordinal.CreateAfter());
418 verifier()->GetExtensionService()->extension_prefs()->
419 extension_sorting()->SetPageOrdinal(extension_misc::kWebStoreAppId,
420 cws_page_ordinal.CreateAfter());
421 ASSERT_TRUE(AwaitQuiescence());
422 ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
425 // TODO(akalin): Add tests exercising:
426 // - Offline installation/uninstallation behavior
427 // - App-specific properties