two_client_apps_sync_test.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
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"
15
16using apps_helper::AllProfilesHaveSameAppsAsVerifier;
17using apps_helper::CopyNTPOrdinals;
18using apps_helper::DisableApp;
19using apps_helper::EnableApp;
20using apps_helper::FixNTPOrdinalCollisions;
21using apps_helper::GetAppLaunchOrdinalForApp;
22using apps_helper::HasSameAppsAsVerifier;
23using apps_helper::IncognitoDisableApp;
24using apps_helper::IncognitoEnableApp;
25using apps_helper::InstallApp;
26using apps_helper::InstallAppsPendingForSync;
27using apps_helper::InstallPlatformApp;
28using apps_helper::SetAppLaunchOrdinalForApp;
29using apps_helper::SetPageOrdinalForApp;
30using apps_helper::UninstallApp;
31
32class TwoClientAppsSyncTest : public SyncTest {
33 public:
34  TwoClientAppsSyncTest() : SyncTest(TWO_CLIENT) {}
35
36  virtual ~TwoClientAppsSyncTest() {}
37
38 private:
39  DISALLOW_COPY_AND_ASSIGN(TwoClientAppsSyncTest);
40};
41
42IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithNoApps) {
43  ASSERT_TRUE(SetupSync());
44
45  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
46}
47
48IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithSameApps) {
49  ASSERT_TRUE(SetupClients());
50
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);
56  }
57
58  ASSERT_TRUE(SetupSync());
59
60  ASSERT_TRUE(AwaitQuiescence());
61
62  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
63}
64
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.
68IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithDifferentApps) {
69  ASSERT_TRUE(SetupClients());
70
71  int i = 0;
72
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);
78  }
79
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);
85  }
86
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);
92  }
93
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);
99  }
100
101  FixNTPOrdinalCollisions(verifier());
102
103  ASSERT_TRUE(SetupSync());
104
105  ASSERT_TRUE(AwaitQuiescence());
106
107  InstallAppsPendingForSync(GetProfile(0));
108  InstallAppsPendingForSync(GetProfile(1));
109
110  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
111}
112
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.
116IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, InstallDifferentApps) {
117  ASSERT_TRUE(SetupClients());
118
119  int i = 0;
120
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);
126  }
127
128  ASSERT_TRUE(SetupSync());
129
130  ASSERT_TRUE(AwaitQuiescence());
131
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);
137  }
138
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);
144  }
145
146  FixNTPOrdinalCollisions(verifier());
147
148  ASSERT_TRUE(AwaitQuiescence());
149
150  InstallAppsPendingForSync(GetProfile(0));
151  InstallAppsPendingForSync(GetProfile(1));
152
153  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
154}
155
156// TCM ID - 3711279.
157IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Add) {
158  ASSERT_TRUE(SetupSync());
159  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
160
161  InstallApp(GetProfile(0), 0);
162  InstallApp(verifier(), 0);
163  ASSERT_TRUE(AwaitQuiescence());
164
165  InstallAppsPendingForSync(GetProfile(0));
166  InstallAppsPendingForSync(GetProfile(1));
167  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
168}
169
170// TCM ID - 3706267.
171IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Uninstall) {
172  ASSERT_TRUE(SetupSync());
173  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
174
175  InstallApp(GetProfile(0), 0);
176  InstallApp(verifier(), 0);
177  ASSERT_TRUE(AwaitQuiescence());
178
179  InstallAppsPendingForSync(GetProfile(0));
180  InstallAppsPendingForSync(GetProfile(1));
181  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
182
183  UninstallApp(GetProfile(0), 0);
184  UninstallApp(verifier(), 0);
185  ASSERT_TRUE(AwaitQuiescence());
186  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
187}
188
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
192// ordinals.
193IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UninstallThenInstall) {
194  ASSERT_TRUE(SetupSync());
195  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
196
197  InstallApp(GetProfile(0), 0);
198  InstallApp(verifier(), 0);
199  ASSERT_TRUE(AwaitQuiescence());
200
201  InstallAppsPendingForSync(GetProfile(0));
202  InstallAppsPendingForSync(GetProfile(1));
203  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
204
205  UninstallApp(GetProfile(0), 0);
206  UninstallApp(verifier(), 0);
207  ASSERT_TRUE(AwaitQuiescence());
208  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
209
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());
216}
217
218// TCM ID - 3699295.
219// Flaky: http://crbug.com/226055
220IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DISABLED_Merge) {
221  ASSERT_TRUE(SetupSync());
222  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
223
224  InstallApp(GetProfile(0), 0);
225  InstallApp(GetProfile(1), 0);
226  ASSERT_TRUE(AwaitQuiescence());
227
228  UninstallApp(GetProfile(0), 0);
229  InstallApp(GetProfile(0), 1);
230  InstallApp(verifier(), 1);
231
232  InstallApp(GetProfile(0), 2);
233  InstallApp(GetProfile(1), 2);
234  InstallApp(verifier(), 2);
235
236  InstallApp(GetProfile(1), 3);
237  InstallApp(verifier(), 3);
238
239  ASSERT_TRUE(AwaitQuiescence());
240  InstallAppsPendingForSync(GetProfile(0));
241  InstallAppsPendingForSync(GetProfile(1));
242  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
243}
244
245// TCM ID - 7723126.
246IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateEnableDisableApp) {
247  ASSERT_TRUE(SetupSync());
248  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
249
250  InstallApp(GetProfile(0), 0);
251  InstallApp(GetProfile(1), 0);
252  InstallApp(verifier(), 0);
253  ASSERT_TRUE(AwaitQuiescence());
254  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
255
256  DisableApp(GetProfile(0), 0);
257  DisableApp(verifier(), 0);
258  ASSERT_TRUE(HasSameAppsAsVerifier(0));
259  ASSERT_FALSE(HasSameAppsAsVerifier(1));
260
261  ASSERT_TRUE(AwaitQuiescence());
262  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
263
264  EnableApp(GetProfile(1), 0);
265  EnableApp(verifier(), 0);
266  ASSERT_TRUE(HasSameAppsAsVerifier(1));
267  ASSERT_FALSE(HasSameAppsAsVerifier(0));
268
269  ASSERT_TRUE(AwaitQuiescence());
270  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
271}
272
273// TCM ID - 7706637.
274IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateIncognitoEnableDisable) {
275  ASSERT_TRUE(SetupSync());
276  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
277
278  InstallApp(GetProfile(0), 0);
279  InstallApp(GetProfile(1), 0);
280  InstallApp(verifier(), 0);
281  ASSERT_TRUE(AwaitQuiescence());
282  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
283
284  IncognitoEnableApp(GetProfile(0), 0);
285  IncognitoEnableApp(verifier(), 0);
286  ASSERT_TRUE(HasSameAppsAsVerifier(0));
287  ASSERT_FALSE(HasSameAppsAsVerifier(1));
288
289  ASSERT_TRUE(AwaitQuiescence());
290  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
291
292  IncognitoDisableApp(GetProfile(1), 0);
293  IncognitoDisableApp(verifier(), 0);
294  ASSERT_TRUE(HasSameAppsAsVerifier(1));
295  ASSERT_FALSE(HasSameAppsAsVerifier(0));
296
297  ASSERT_TRUE(AwaitQuiescence());
298  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
299}
300
301// TCM ID - 3718276.
302IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableApps) {
303  ASSERT_TRUE(SetupSync());
304  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
305
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));
312
313  ASSERT_TRUE(GetClient(1)->EnableSyncForDatatype(syncer::APPS));
314  ASSERT_TRUE(AwaitQuiescence());
315
316  InstallAppsPendingForSync(GetProfile(0));
317  InstallAppsPendingForSync(GetProfile(1));
318  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
319}
320
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.
324// TCM ID - 3720303.
325IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableSync) {
326  ASSERT_TRUE(SetupSync());
327  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
328
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));
335
336  ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
337  ASSERT_TRUE(AwaitQuiescence());
338
339  InstallAppsPendingForSync(GetProfile(0));
340  InstallAppsPendingForSync(GetProfile(1));
341  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
342}
343
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
346// the app.
347IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdatePageOrdinal) {
348  ASSERT_TRUE(SetupSync());
349  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
350
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());
358
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());
364}
365
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.
369IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateAppLaunchOrdinal) {
370  ASSERT_TRUE(SetupSync());
371  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
372
373  InstallApp(GetProfile(0), 0);
374  InstallApp(GetProfile(1), 0);
375  InstallApp(verifier(), 0);
376  ASSERT_TRUE(AwaitQuiescence());
377  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
378
379  syncer::StringOrdinal initial_position =
380      GetAppLaunchOrdinalForApp(GetProfile(0), 0);
381
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());
387}
388
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.
392IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateCWSOrdinals) {
393  ASSERT_TRUE(SetupSync());
394  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
395
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()->
402      SetAppLaunchOrdinal(
403          extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
404  verifier()->GetExtensionService()->extension_prefs()->extension_sorting()->
405      SetAppLaunchOrdinal(
406          extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
407  ASSERT_TRUE(AwaitQuiescence());
408  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
409
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());
423}
424
425// TODO(akalin): Add tests exercising:
426//   - Offline installation/uninstallation behavior
427//   - App-specific properties
428