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_service_factory.h"
7 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/search_engines_helper.h"
9 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
10 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "components/search_engines/template_url.h"
13 #include "components/search_engines/template_url_service.h"
15 using base::ASCIIToUTF16
;
16 using sync_integration_test_util::AwaitCommitActivityCompletion
;
18 class TwoClientSearchEnginesSyncTest
: public SyncTest
{
20 TwoClientSearchEnginesSyncTest() : SyncTest(TWO_CLIENT
) {}
21 ~TwoClientSearchEnginesSyncTest() override
{}
24 DISALLOW_COPY_AND_ASSIGN(TwoClientSearchEnginesSyncTest
);
28 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, E2ETest_Add
) {
29 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
30 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
32 // Note that a random seed is needed due to the E2E nature of the tests, and
33 // the synced data persisting in the server across tests.
34 int search_engine_seed
= base::Time::Now().ToInternalValue();
35 search_engines_helper::AddSearchEngine(0, search_engine_seed
);
36 ASSERT_TRUE(search_engines_helper::HasSearchEngine(0, search_engine_seed
));
38 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
39 ASSERT_TRUE(search_engines_helper::HasSearchEngine(1, search_engine_seed
));
43 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, E2ETest_Delete
) {
44 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
45 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
47 // Note that a random seed is needed due to the E2E nature of the tests, and
48 // the synced data persisting in the server across tests.
49 int search_engine_seed
= base::Time::Now().ToInternalValue();
50 search_engines_helper::AddSearchEngine(0, search_engine_seed
);
51 ASSERT_TRUE(search_engines_helper::HasSearchEngine(0, search_engine_seed
));
53 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
54 ASSERT_TRUE(search_engines_helper::HasSearchEngine(1, search_engine_seed
));
56 search_engines_helper::DeleteSearchEngineBySeed(0, search_engine_seed
);
58 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
59 ASSERT_FALSE(search_engines_helper::HasSearchEngine(1, search_engine_seed
));
63 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, AddMultiple
) {
64 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
65 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
68 for (int i
= 0; i
< 3; ++i
)
69 search_engines_helper::AddSearchEngine(0, i
);
71 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
75 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, Duplicates
) {
76 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
77 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
79 // Add two entries with the same Name and URL (but different keywords).
80 // Note that we have to change the GUID of the duplicate.
81 search_engines_helper::AddSearchEngine(0, 0);
82 Profile
* profile
= sync_datatype_helper::test()->GetProfile(0);
83 TemplateURLServiceFactory::GetForProfile(profile
)->Add(
84 search_engines_helper::CreateTestTemplateURL(profile
, 0,
85 ASCIIToUTF16("somethingelse"), "newguid"));
86 search_engines_helper::GetVerifierService()->Add(
87 search_engines_helper::CreateTestTemplateURL(profile
, 0,
88 ASCIIToUTF16("somethingelse"), "newguid"));
89 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
93 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateKeyword
) {
94 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
95 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
97 search_engines_helper::AddSearchEngine(0, 0);
99 // Change the keyword.
100 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
102 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
103 ASCIIToUTF16("test0"), ASCIIToUTF16("newkeyword"),
104 "http://www.test0.com/");
106 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
110 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateUrl
) {
111 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
112 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
114 search_engines_helper::AddSearchEngine(0, 0);
116 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
119 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
120 ASCIIToUTF16("test0"), ASCIIToUTF16("test0"),
121 "http://www.wikipedia.org/q=%s");
123 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
127 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, UpdateName
) {
128 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
129 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
131 search_engines_helper::AddSearchEngine(0, 0);
133 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
135 // Change the short name.
136 search_engines_helper::EditSearchEngine(0, ASCIIToUTF16("test0"),
137 ASCIIToUTF16("New Name"), ASCIIToUTF16("test0"), "http://www.test0.com/");
139 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
143 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, ConflictKeyword
) {
144 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
146 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
148 // Add a different search engine to each client, but make their keywords
150 search_engines_helper::AddSearchEngine(0, 0);
151 search_engines_helper::AddSearchEngine(1, 1);
152 TemplateURLService
* service
=
153 search_engines_helper::GetServiceForBrowserContext(1);
154 TemplateURL
* turl
= service
->GetTemplateURLForKeyword(ASCIIToUTF16("test1"));
156 service
->ResetTemplateURL(turl
, turl
->short_name(), ASCIIToUTF16("test0"),
159 ASSERT_TRUE(AwaitQuiescence());
160 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
164 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, MergeMultiple
) {
165 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
167 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
169 // Set up some different search engines on each client, with some interesting
171 // client0: { SE0, SE1, SE2 }
172 for (int i
= 0; i
< 3; ++i
)
173 search_engines_helper::AddSearchEngine(0, i
);
175 // client1: { SE0, SE2, SE3, SE0 + different URL }
176 search_engines_helper::AddSearchEngine(1, 0);
177 search_engines_helper::AddSearchEngine(1, 2);
178 search_engines_helper::AddSearchEngine(1, 3);
179 Profile
* profile
= sync_datatype_helper::test()->GetProfile(1);
180 TemplateURLServiceFactory::GetForProfile(profile
)->Add(
181 search_engines_helper::CreateTestTemplateURL(profile
, 0,
182 ASCIIToUTF16("somethingelse.com"), "http://www.somethingelse.com/",
185 ASSERT_TRUE(AwaitQuiescence());
186 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
190 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, DisableSync
) {
191 ASSERT_TRUE(SetupSync());
192 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
194 ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
195 search_engines_helper::AddSearchEngine(0, 0);
197 AwaitCommitActivityCompletion(GetSyncService((0))));
198 ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0));
199 ASSERT_FALSE(search_engines_helper::ServiceMatchesVerifier(1));
201 ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
202 ASSERT_TRUE(AwaitQuiescence());
203 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
207 IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest
, SyncDefault
) {
208 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
209 ASSERT_TRUE(search_engines_helper::AllServicesMatch());
211 search_engines_helper::AddSearchEngine(0, 0);
212 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
214 // Change the default to the new search engine, sync, and ensure that it
215 // changed in the second client. AllServicesMatch does a default search
217 search_engines_helper::ChangeDefaultSearchProvider(0, 0);
218 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
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(search_engines_helper::AwaitAllServicesMatch());
231 search_engines_helper::ChangeDefaultSearchProvider(0, 0);
232 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());
234 // Change the default on the first client and delete the old default.
235 search_engines_helper::ChangeDefaultSearchProvider(0, 1);
236 search_engines_helper::DeleteSearchEngineBySeed(0, 0);
237 ASSERT_TRUE(search_engines_helper::AwaitAllServicesMatch());