two_client_apps_sync_test.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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::HasSameAppsAsVerifier;
22using apps_helper::IncognitoDisableApp;
23using apps_helper::IncognitoEnableApp;
24using apps_helper::InstallApp;
25using apps_helper::InstallAppsPendingForSync;
26using apps_helper::SetAppLaunchOrdinalForApp;
27using apps_helper::SetPageOrdinalForApp;
28using apps_helper::UninstallApp;
29
30class TwoClientAppsSyncTest : public SyncTest {
31 public:
32  TwoClientAppsSyncTest() : SyncTest(TWO_CLIENT) {}
33
34  virtual ~TwoClientAppsSyncTest() {}
35
36 private:
37  DISALLOW_COPY_AND_ASSIGN(TwoClientAppsSyncTest);
38};
39
40IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithNoApps) {
41  ASSERT_TRUE(SetupSync());
42
43  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
44}
45
46IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithSameApps) {
47  ASSERT_TRUE(SetupClients());
48
49  const int kNumApps = 5;
50  for (int i = 0; i < kNumApps; ++i) {
51    InstallApp(GetProfile(0), i);
52    InstallApp(GetProfile(1), i);
53    InstallApp(verifier(), i);
54  }
55
56  ASSERT_TRUE(SetupSync());
57
58  ASSERT_TRUE(AwaitQuiescence());
59
60  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
61}
62
63// Install some apps on both clients, some on only one client, some on only the
64// other, and sync.  Both clients should end up with all apps, and the app and
65// page ordinals should be identical.
66IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, StartWithDifferentApps) {
67  ASSERT_TRUE(SetupClients());
68
69  int i = 0;
70
71  const int kNumCommonApps = 5;
72  for (int j = 0; j < kNumCommonApps; ++i, ++j) {
73    InstallApp(GetProfile(0), i);
74    InstallApp(GetProfile(1), i);
75    InstallApp(verifier(), i);
76  }
77
78  const int kNumProfile0Apps = 10;
79  for (int j = 0; j < kNumProfile0Apps; ++i, ++j) {
80    InstallApp(GetProfile(0), i);
81    InstallApp(verifier(), i);
82    CopyNTPOrdinals(GetProfile(0), verifier(), i);
83  }
84
85  const int kNumProfile1Apps = 10;
86  for (int j = 0; j < kNumProfile1Apps; ++i, ++j) {
87    InstallApp(GetProfile(1), i);
88    InstallApp(verifier(), i);
89    CopyNTPOrdinals(GetProfile(1), verifier(), i);
90  }
91
92  FixNTPOrdinalCollisions(verifier());
93
94  ASSERT_TRUE(SetupSync());
95
96  ASSERT_TRUE(AwaitQuiescence());
97
98  InstallAppsPendingForSync(GetProfile(0));
99  InstallAppsPendingForSync(GetProfile(1));
100
101  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
102}
103
104// Install some apps on both clients, then sync.  Then install some apps on only
105// one client, some on only the other, and then sync again.  Both clients should
106// end up with all apps, and the app and page ordinals should be identical.
107IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, InstallDifferentApps) {
108  ASSERT_TRUE(SetupClients());
109
110  int i = 0;
111
112  const int kNumCommonApps = 5;
113  for (int j = 0; j < kNumCommonApps; ++i, ++j) {
114    InstallApp(GetProfile(0), i);
115    InstallApp(GetProfile(1), i);
116    InstallApp(verifier(), i);
117  }
118
119  ASSERT_TRUE(SetupSync());
120
121  ASSERT_TRUE(AwaitQuiescence());
122
123  const int kNumProfile0Apps = 10;
124  for (int j = 0; j < kNumProfile0Apps; ++i, ++j) {
125    InstallApp(GetProfile(0), i);
126    InstallApp(verifier(), i);
127    CopyNTPOrdinals(GetProfile(0), verifier(), i);
128  }
129
130  const int kNumProfile1Apps = 10;
131  for (int j = 0; j < kNumProfile1Apps; ++i, ++j) {
132    InstallApp(GetProfile(1), i);
133    InstallApp(verifier(), i);
134    CopyNTPOrdinals(GetProfile(1), verifier(), i);
135  }
136
137  FixNTPOrdinalCollisions(verifier());
138
139  ASSERT_TRUE(AwaitQuiescence());
140
141  InstallAppsPendingForSync(GetProfile(0));
142  InstallAppsPendingForSync(GetProfile(1));
143
144  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
145}
146
147// TCM ID - 3711279.
148IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Add) {
149  ASSERT_TRUE(SetupSync());
150  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
151
152  InstallApp(GetProfile(0), 0);
153  InstallApp(verifier(), 0);
154  ASSERT_TRUE(AwaitQuiescence());
155
156  InstallAppsPendingForSync(GetProfile(0));
157  InstallAppsPendingForSync(GetProfile(1));
158  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
159}
160
161// TCM ID - 3706267.
162IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Uninstall) {
163  ASSERT_TRUE(SetupSync());
164  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
165
166  InstallApp(GetProfile(0), 0);
167  InstallApp(verifier(), 0);
168  ASSERT_TRUE(AwaitQuiescence());
169
170  InstallAppsPendingForSync(GetProfile(0));
171  InstallAppsPendingForSync(GetProfile(1));
172  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
173
174  UninstallApp(GetProfile(0), 0);
175  UninstallApp(verifier(), 0);
176  ASSERT_TRUE(AwaitQuiescence());
177  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
178}
179
180// Install an app on one client, then sync. Then uninstall the app on the first
181// client and sync again. Now install a new app on the first client and sync.
182// Both client should only have the second app, with identical app and page
183// ordinals.
184IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UninstallThenInstall) {
185  ASSERT_TRUE(SetupSync());
186  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
187
188  InstallApp(GetProfile(0), 0);
189  InstallApp(verifier(), 0);
190  ASSERT_TRUE(AwaitQuiescence());
191
192  InstallAppsPendingForSync(GetProfile(0));
193  InstallAppsPendingForSync(GetProfile(1));
194  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
195
196  UninstallApp(GetProfile(0), 0);
197  UninstallApp(verifier(), 0);
198  ASSERT_TRUE(AwaitQuiescence());
199  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
200
201  InstallApp(GetProfile(0), 1);
202  InstallApp(verifier(), 1);
203  ASSERT_TRUE(AwaitQuiescence());
204  InstallAppsPendingForSync(GetProfile(0));
205  InstallAppsPendingForSync(GetProfile(1));
206  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
207}
208
209// TCM ID - 3699295.
210IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, Merge) {
211  ASSERT_TRUE(SetupSync());
212  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
213
214  InstallApp(GetProfile(0), 0);
215  InstallApp(GetProfile(1), 0);
216  ASSERT_TRUE(AwaitQuiescence());
217
218  UninstallApp(GetProfile(0), 0);
219  InstallApp(GetProfile(0), 1);
220  InstallApp(verifier(), 1);
221
222  InstallApp(GetProfile(0), 2);
223  InstallApp(GetProfile(1), 2);
224  InstallApp(verifier(), 2);
225
226  InstallApp(GetProfile(1), 3);
227  InstallApp(verifier(), 3);
228
229  ASSERT_TRUE(AwaitQuiescence());
230  InstallAppsPendingForSync(GetProfile(0));
231  InstallAppsPendingForSync(GetProfile(1));
232  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
233}
234
235// TCM ID - 7723126.
236IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateEnableDisableApp) {
237  ASSERT_TRUE(SetupSync());
238  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
239
240  InstallApp(GetProfile(0), 0);
241  InstallApp(GetProfile(1), 0);
242  InstallApp(verifier(), 0);
243  ASSERT_TRUE(AwaitQuiescence());
244  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
245
246  DisableApp(GetProfile(0), 0);
247  DisableApp(verifier(), 0);
248  ASSERT_TRUE(HasSameAppsAsVerifier(0));
249  ASSERT_FALSE(HasSameAppsAsVerifier(1));
250
251  ASSERT_TRUE(AwaitQuiescence());
252  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
253
254  EnableApp(GetProfile(1), 0);
255  EnableApp(verifier(), 0);
256  ASSERT_TRUE(HasSameAppsAsVerifier(1));
257  ASSERT_FALSE(HasSameAppsAsVerifier(0));
258
259  ASSERT_TRUE(AwaitQuiescence());
260  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
261}
262
263// TCM ID - 7706637.
264IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateIncognitoEnableDisable) {
265  ASSERT_TRUE(SetupSync());
266  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
267
268  InstallApp(GetProfile(0), 0);
269  InstallApp(GetProfile(1), 0);
270  InstallApp(verifier(), 0);
271  ASSERT_TRUE(AwaitQuiescence());
272  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
273
274  IncognitoEnableApp(GetProfile(0), 0);
275  IncognitoEnableApp(verifier(), 0);
276  ASSERT_TRUE(HasSameAppsAsVerifier(0));
277  ASSERT_FALSE(HasSameAppsAsVerifier(1));
278
279  ASSERT_TRUE(AwaitQuiescence());
280  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
281
282  IncognitoDisableApp(GetProfile(1), 0);
283  IncognitoDisableApp(verifier(), 0);
284  ASSERT_TRUE(HasSameAppsAsVerifier(1));
285  ASSERT_FALSE(HasSameAppsAsVerifier(0));
286
287  ASSERT_TRUE(AwaitQuiescence());
288  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
289}
290
291// TCM ID - 3718276.
292IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableApps) {
293  ASSERT_TRUE(SetupSync());
294  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
295
296  ASSERT_TRUE(GetClient(1)->DisableSyncForDatatype(syncer::APPS));
297  InstallApp(GetProfile(0), 0);
298  InstallApp(verifier(), 0);
299  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Installed an app."));
300  ASSERT_TRUE(HasSameAppsAsVerifier(0));
301  ASSERT_FALSE(HasSameAppsAsVerifier(1));
302
303  ASSERT_TRUE(GetClient(1)->EnableSyncForDatatype(syncer::APPS));
304  ASSERT_TRUE(AwaitQuiescence());
305
306  InstallAppsPendingForSync(GetProfile(0));
307  InstallAppsPendingForSync(GetProfile(1));
308  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
309}
310
311// Disable sync for the second client and then install an app on the first
312// client, then enable sync on the second client. Both clients should have the
313// same app with identical app and page ordinals.
314// TCM ID - 3720303.
315IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, DisableSync) {
316  ASSERT_TRUE(SetupSync());
317  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
318
319  ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
320  InstallApp(GetProfile(0), 0);
321  InstallApp(verifier(), 0);
322  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Installed an app."));
323  ASSERT_TRUE(HasSameAppsAsVerifier(0));
324  ASSERT_FALSE(HasSameAppsAsVerifier(1));
325
326  ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
327  ASSERT_TRUE(AwaitQuiescence());
328
329  InstallAppsPendingForSync(GetProfile(0));
330  InstallAppsPendingForSync(GetProfile(1));
331  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
332}
333
334// Install the same app on both clients, then sync. Change the page ordinal on
335// one client and sync. Both clients should have the updated page ordinal for
336// the app.
337IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdatePageOrdinal) {
338  ASSERT_TRUE(SetupSync());
339  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
340
341  syncer::StringOrdinal initial_page =
342      syncer::StringOrdinal::CreateInitialOrdinal();
343  InstallApp(GetProfile(0), 0);
344  InstallApp(GetProfile(1), 0);
345  InstallApp(verifier(), 0);
346  ASSERT_TRUE(AwaitQuiescence());
347  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
348
349  syncer::StringOrdinal second_page = initial_page.CreateAfter();
350  SetPageOrdinalForApp(GetProfile(0), 0, second_page);
351  SetPageOrdinalForApp(verifier(), 0, second_page);
352  ASSERT_TRUE(AwaitQuiescence());
353  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
354}
355
356// Install the same app on both clients, then sync. Change the app launch
357// ordinal on one client and sync. Both clients should have the updated app
358// launch ordinal for the app.
359IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateAppLaunchOrdinal) {
360  ASSERT_TRUE(SetupSync());
361  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
362
363  syncer::StringOrdinal initial_position =
364      syncer::StringOrdinal::CreateInitialOrdinal();
365  InstallApp(GetProfile(0), 0);
366  InstallApp(GetProfile(1), 0);
367  InstallApp(verifier(), 0);
368  ASSERT_TRUE(AwaitQuiescence());
369  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
370
371  syncer::StringOrdinal second_position = initial_position.CreateAfter();
372  SetAppLaunchOrdinalForApp(GetProfile(0), 0, second_position);
373  SetAppLaunchOrdinalForApp(verifier(), 0, second_position);
374  ASSERT_TRUE(AwaitQuiescence());
375  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
376}
377
378// Adjust the CWS location within a page on the first client and sync. Adjust
379// which page the CWS appears on and sync. Both clients should have the same
380// page and app launch ordinal values for the CWS.
381IN_PROC_BROWSER_TEST_F(TwoClientAppsSyncTest, UpdateCWSOrdinals) {
382  ASSERT_TRUE(SetupSync());
383  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
384
385  // Change the app launch ordinal.
386  syncer::StringOrdinal cws_app_launch_ordinal =
387      GetProfile(0)->GetExtensionService()->
388      extension_prefs()->extension_sorting()->GetAppLaunchOrdinal(
389          extension_misc::kWebStoreAppId);
390  GetProfile(0)->GetExtensionService()->extension_prefs()->extension_sorting()->
391      SetAppLaunchOrdinal(
392          extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
393  verifier()->GetExtensionService()->extension_prefs()->extension_sorting()->
394      SetAppLaunchOrdinal(
395          extension_misc::kWebStoreAppId, cws_app_launch_ordinal.CreateAfter());
396  ASSERT_TRUE(AwaitQuiescence());
397  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
398
399  // Change the page ordinal.
400  syncer::StringOrdinal cws_page_ordinal =
401      GetProfile(1)->GetExtensionService()->
402      extension_prefs()->extension_sorting()->GetPageOrdinal(
403          extension_misc::kWebStoreAppId);
404  GetProfile(1)->GetExtensionService()->extension_prefs()->
405      extension_sorting()->SetPageOrdinal(extension_misc::kWebStoreAppId,
406                                          cws_page_ordinal.CreateAfter());
407  verifier()->GetExtensionService()->extension_prefs()->
408      extension_sorting()->SetPageOrdinal(extension_misc::kWebStoreAppId,
409                                          cws_page_ordinal.CreateAfter());
410  ASSERT_TRUE(AwaitQuiescence());
411  ASSERT_TRUE(AllProfilesHaveSameAppsAsVerifier());
412}
413
414// TODO(akalin): Add tests exercising:
415//   - Offline installation/uninstallation behavior
416//   - App-specific properties
417