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/strings/utf_string_conversions.h"
6 #include "chrome/browser/search_engines/template_url.h"
7 #include "chrome/browser/search_engines/template_url_service.h"
8 #include "chrome/browser/search_engines/template_url_service_factory.h"
9 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
10 #include "chrome/browser/sync/test/integration/search_engines_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
12 #include "chrome/browser/sync/test/integration/sync_test.h"
14 using base::ASCIIToUTF16
;
16 class TwoClientSearchEnginesSyncTest
: public SyncTest
{
18 TwoClientSearchEnginesSyncTest() : SyncTest(TWO_CLIENT
) {}
19 virtual ~TwoClientSearchEnginesSyncTest() {}
22 DISALLOW_COPY_AND_ASSIGN(TwoClientSearchEnginesSyncTest
);
26 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, Add
) {
27 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
28 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
30 search_engines_helper::AddSearchEngine(0, 0);
32 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
33 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
37 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, AddMultiple
) {
38 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
39 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
42 for (int i
= 0; i
< 3; ++i
)
43 search_engines_helper::AddSearchEngine(0, i
);
45 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
46 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
50 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, Duplicates
) {
51 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
52 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
54 // Add two entries with the same Name and URL (but different keywords).
55 // Note that we have to change the GUID of the duplicate.
56 search_engines_helper::AddSearchEngine(0, 0);
57 Profile
* profile
= sync_datatype_helper::test()->GetProfile(0);
58 TemplateURLServiceFactory::GetForProfile(profile
)->Add(
59 search_engines_helper::CreateTestTemplateURL(profile
, 0,
60 ASCIIToUTF16("somethingelse"), "newguid"));
61 search_engines_helper::GetVerifierService()->Add(
62 search_engines_helper::CreateTestTemplateURL(profile
, 0,
63 ASCIIToUTF16("somethingelse"), "newguid"));
64 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
65 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
69 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateKeyword
) {
70 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
71 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
73 search_engines_helper::AddSearchEngine(0, 0);
75 // Change the keyword.
76 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
77 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
79 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
80 ASCIIToUTF16("test0"), ASCIIToUTF16("newkeyword"),
81 "http://www.test0.com/");
83 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
84 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
88 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateUrl
) {
89 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
90 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
92 search_engines_helper::AddSearchEngine(0, 0);
94 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
95 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
98 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
99 ASCIIToUTF16("test0"), ASCIIToUTF16("test0"),
100 "http://www.wikipedia.org/q=%s");
102 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
103 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
107 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateName
) {
108 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
109 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
111 search_engines_helper::AddSearchEngine(0, 0);
113 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
114 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
116 // Change the short name.
117 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
118 ASCIIToUTF16("New Name"), ASCIIToUTF16("test0"), "http://www.test0.com/");
120 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
121 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
125 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, Delete
) {
126 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
127 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
129 search_engines_helper::AddSearchEngine(0, 0);
131 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
132 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
134 search_engines_helper::DeleteSearchEngineBySeed(0, 0);
136 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
137 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
141 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, ConflictKeyword
) {
142 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
144 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
146 // Add a different search engine to each client, but make their keywords
148 search_engines_helper::AddSearchEngine(0, 0);
149 search_engines_helper::AddSearchEngine(1, 1);
150 TemplateURLService
* service
=
151 search_engines_helper::GetServiceForBrowserContext(1);
152 TemplateURL
* turl
= service
->GetTemplateURLForKeyword(ASCIIToUTF16("test1"));
154 service
->ResetTemplateURL(turl
, turl
->short_name(), ASCIIToUTF16("test0"),
157 ASSERT_TRUE(AwaitQuiescence());
158 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
162 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, MergeMultiple
) {
163 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
165 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
167 // Set up some different search engines on each client, with some interesting
169 // client0: { SE0, SE1, SE2 }
170 for (int i
= 0; i
< 3; ++i
)
171 search_engines_helper::AddSearchEngine(0, i
);
173 // client1: { SE0, SE2, SE3, SE0 + different URL }
174 search_engines_helper::AddSearchEngine(1, 0);
175 search_engines_helper::AddSearchEngine(1, 2);
176 search_engines_helper::AddSearchEngine(1, 3);
177 Profile
* profile
= sync_datatype_helper::test()->GetProfile(1);
178 TemplateURLServiceFactory::GetForProfile(profile
)->Add(
179 search_engines_helper::CreateTestTemplateURL(profile
, 0,
180 ASCIIToUTF16("somethingelse.com"), "http://www.somethingelse.com/",
183 ASSERT_TRUE(AwaitQuiescence());
184 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
188 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, DisableSync
) {
189 ASSERT_TRUE(SetupSync());
190 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
192 ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
193 search_engines_helper::AddSearchEngine(0, 0);
195 GetClient(0)->AwaitFullSyncCompletion());
196 ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0));
197 ASSERT_FALSE(search_engines_helper::ServiceMatchesVerifier(1));
199 ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
200 ASSERT_TRUE(AwaitQuiescence());
201 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
205 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, SyncDefault
) {
206 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
207 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
209 search_engines_helper::AddSearchEngine(0, 0);
210 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
212 // Change the default to the new search engine, sync, and ensure that it
213 // changed in the second client. AllServicesMatch does a default search
215 search_engines_helper::ChangeDefaultSearchProvider(0, 0);
216 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
218 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
221 // Ensure that we can change the search engine and immediately delete it
222 // without putting the clients out of sync.
223 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, DeleteSyncedDefault
) {
224 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
225 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
227 search_engines_helper::AddSearchEngine(0, 0);
228 search_engines_helper::AddSearchEngine(0, 1);
229 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
231 search_engines_helper::ChangeDefaultSearchProvider(0, 0);
232 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
233 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
235 // Change the default on the first client and delete the old default.
236 search_engines_helper::ChangeDefaultSearchProvider(0, 1);
237 search_engines_helper::DeleteSearchEngineBySeed(0, 0);
238 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
240 ASSERT_TRUE(search_engines_helper::AllServicesMatch());