Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: chrome/browser/search_engines/template_url_service_unittest.cc

Issue 376413002: Stop using TemplateURLServiceTestUtil to initialize TemplateURLServiceFactory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix TemplateURLServiceWithoutFallbackTest Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 } // namespace 151 } // namespace
152 152
153 153
154 // TemplateURLServiceTest ----------------------------------------------------- 154 // TemplateURLServiceTest -----------------------------------------------------
155 155
156 class TemplateURLServiceTest : public testing::Test { 156 class TemplateURLServiceTest : public testing::Test {
157 public: 157 public:
158 TemplateURLServiceTest(); 158 TemplateURLServiceTest();
159 159
160 // testing::Test 160 // testing::Test:
161 virtual void SetUp(); 161 virtual void SetUp() OVERRIDE;
162 virtual void TearDown(); 162 virtual void TearDown() OVERRIDE;
163 163
164 TemplateURL* AddKeywordWithDate(const std::string& short_name, 164 TemplateURL* AddKeywordWithDate(const std::string& short_name,
165 const std::string& keyword, 165 const std::string& keyword,
166 const std::string& url, 166 const std::string& url,
167 const std::string& suggest_url, 167 const std::string& suggest_url,
168 const std::string& alternate_url, 168 const std::string& alternate_url,
169 const std::string& favicon_url, 169 const std::string& favicon_url,
170 bool safe_for_autoreplace, 170 bool safe_for_autoreplace,
171 const std::string& encodings, 171 const std::string& encodings,
172 Time date_created, 172 Time date_created,
173 Time last_modified); 173 Time last_modified);
174 174
175 // Verifies the two TemplateURLs are equal. 175 // Verifies the two TemplateURLs are equal.
176 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); 176 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual);
177 177
178 // Create an URL that appears to have been prepopulated, but won't be in the 178 // Create an URL that appears to have been prepopulated, but won't be in the
179 // current data. The caller owns the returned TemplateURL*. 179 // current data. The caller owns the returned TemplateURL*.
180 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, 180 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace,
181 int prepopulate_id); 181 int prepopulate_id);
182 182
183 // Helper methods to make calling TemplateURLServiceTestUtil methods less 183 // Helper methods to make calling TemplateURLServiceTestUtil methods less
184 // visually noisy in the test code. 184 // visually noisy in the test code.
185 void VerifyObserverCount(int expected_changed_count); 185 void VerifyObserverCount(int expected_changed_count);
186 void VerifyObserverFired(); 186 void VerifyObserverFired();
187 TemplateURLService* model() { return test_util_.model(); } 187 TemplateURLService* model() { return test_util_->model(); }
188 const SearchTermsData& search_terms_data() { 188 const SearchTermsData& search_terms_data() {
189 return model()->search_terms_data(); 189 return model()->search_terms_data();
190 } 190 }
191 191
192 protected: 192 protected:
193 TemplateURLServiceTestUtil test_util_; 193 scoped_ptr<TemplateURLServiceTestUtil> test_util_;
194 194
195 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest); 195 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest);
196 }; 196 };
197 197
198 class TemplateURLServiceWithoutFallbackTest : public TemplateURLServiceTest { 198 class TemplateURLServiceWithoutFallbackTest : public TemplateURLServiceTest {
199 public: 199 public:
200 TemplateURLServiceWithoutFallbackTest() : TemplateURLServiceTest() {} 200 TemplateURLServiceWithoutFallbackTest() : TemplateURLServiceTest() {}
201 201
202 virtual void SetUp() OVERRIDE { 202 virtual void SetUp() OVERRIDE {
203 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true); 203 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
204 TemplateURLServiceTest::SetUp(); 204 TemplateURLServiceTest::SetUp();
205 } 205 }
206 206
207 virtual void TearDown() OVERRIDE { 207 virtual void TearDown() OVERRIDE {
208 TemplateURLServiceTest::TearDown(); 208 TemplateURLServiceTest::TearDown();
209 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false); 209 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
210 } 210 }
211 }; 211 };
212 212
213 TemplateURLServiceTest::TemplateURLServiceTest() { 213 TemplateURLServiceTest::TemplateURLServiceTest() {
214 } 214 }
215 215
216 void TemplateURLServiceTest::SetUp() { 216 void TemplateURLServiceTest::SetUp() {
217 test_util_.SetUp(); 217 test_util_.reset(new TemplateURLServiceTestUtil);
218 } 218 }
219 219
220 void TemplateURLServiceTest::TearDown() { 220 void TemplateURLServiceTest::TearDown() {
221 test_util_.TearDown(); 221 test_util_.reset();
222 } 222 }
223 223
224 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( 224 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate(
225 const std::string& short_name, 225 const std::string& short_name,
226 const std::string& keyword, 226 const std::string& keyword,
227 const std::string& url, 227 const std::string& url,
228 const std::string& suggest_url, 228 const std::string& suggest_url,
229 const std::string& alternate_url, 229 const std::string& alternate_url,
230 const std::string& favicon_url, 230 const std::string& favicon_url,
231 bool safe_for_autoreplace, 231 bool safe_for_autoreplace,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 data.show_in_default_list = true; 267 data.show_in_default_list = true;
268 data.safe_for_autoreplace = safe_for_autoreplace; 268 data.safe_for_autoreplace = safe_for_autoreplace;
269 data.input_encodings.push_back("UTF-8"); 269 data.input_encodings.push_back("UTF-8");
270 data.date_created = Time::FromTimeT(100); 270 data.date_created = Time::FromTimeT(100);
271 data.last_modified = Time::FromTimeT(100); 271 data.last_modified = Time::FromTimeT(100);
272 data.prepopulate_id = prepopulate_id; 272 data.prepopulate_id = prepopulate_id;
273 return new TemplateURL(data); 273 return new TemplateURL(data);
274 } 274 }
275 275
276 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { 276 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) {
277 EXPECT_EQ(expected_changed_count, test_util_.GetObserverCount()); 277 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount());
278 test_util_.ResetObserverCount(); 278 test_util_->ResetObserverCount();
279 } 279 }
280 280
281 void TemplateURLServiceTest::VerifyObserverFired() { 281 void TemplateURLServiceTest::VerifyObserverFired() {
282 EXPECT_LE(1, test_util_.GetObserverCount()); 282 EXPECT_LE(1, test_util_->GetObserverCount());
283 test_util_.ResetObserverCount(); 283 test_util_->ResetObserverCount();
284 } 284 }
285 285
286 286
287 // Actual tests --------------------------------------------------------------- 287 // Actual tests ---------------------------------------------------------------
288 288
289 TEST_F(TemplateURLServiceTest, Load) { 289 TEST_F(TemplateURLServiceTest, Load) {
290 test_util_.VerifyLoad(); 290 test_util_->VerifyLoad();
291 } 291 }
292 292
293 TEST_F(TemplateURLServiceTest, AddUpdateRemove) { 293 TEST_F(TemplateURLServiceTest, AddUpdateRemove) {
294 // Add a new TemplateURL. 294 // Add a new TemplateURL.
295 test_util_.VerifyLoad(); 295 test_util_->VerifyLoad();
296 const size_t initial_count = model()->GetTemplateURLs().size(); 296 const size_t initial_count = model()->GetTemplateURLs().size();
297 297
298 TemplateURLData data; 298 TemplateURLData data;
299 data.short_name = ASCIIToUTF16("google"); 299 data.short_name = ASCIIToUTF16("google");
300 data.SetKeyword(ASCIIToUTF16("keyword")); 300 data.SetKeyword(ASCIIToUTF16("keyword"));
301 data.SetURL("http://www.google.com/foo/bar"); 301 data.SetURL("http://www.google.com/foo/bar");
302 data.favicon_url = GURL("http://favicon.url"); 302 data.favicon_url = GURL("http://favicon.url");
303 data.safe_for_autoreplace = true; 303 data.safe_for_autoreplace = true;
304 data.date_created = Time::FromTimeT(100); 304 data.date_created = Time::FromTimeT(100);
305 data.last_modified = Time::FromTimeT(100); 305 data.last_modified = Time::FromTimeT(100);
306 data.sync_guid = "00000000-0000-0000-0000-000000000001"; 306 data.sync_guid = "00000000-0000-0000-0000-000000000001";
307 TemplateURL* t_url = new TemplateURL(data); 307 TemplateURL* t_url = new TemplateURL(data);
308 model()->Add(t_url); 308 model()->Add(t_url);
309 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 309 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
310 NULL)); 310 NULL));
311 VerifyObserverCount(1); 311 VerifyObserverCount(1);
312 base::RunLoop().RunUntilIdle(); 312 base::RunLoop().RunUntilIdle();
313 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 313 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
314 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword())); 314 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword()));
315 // We need to make a second copy as the model takes ownership of |t_url| and 315 // We need to make a second copy as the model takes ownership of |t_url| and
316 // will delete it. We have to do this after calling Add() since that gives 316 // will delete it. We have to do this after calling Add() since that gives
317 // |t_url| its ID. 317 // |t_url| its ID.
318 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 318 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
319 319
320 // Reload the model to verify it was actually saved to the database. 320 // Reload the model to verify it was actually saved to the database.
321 test_util_.ResetModel(true); 321 test_util_->ResetModel(true);
322 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 322 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
323 TemplateURL* loaded_url = 323 TemplateURL* loaded_url =
324 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 324 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
325 ASSERT_TRUE(loaded_url != NULL); 325 ASSERT_TRUE(loaded_url != NULL);
326 AssertEquals(*cloned_url, *loaded_url); 326 AssertEquals(*cloned_url, *loaded_url);
327 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 327 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
328 NULL)); 328 NULL));
329 329
330 // We expect the last_modified time to be updated to the present time on an 330 // We expect the last_modified time to be updated to the present time on an
331 // explicit reset. We have to set up the expectation here because ResetModel 331 // explicit reset. We have to set up the expectation here because ResetModel
332 // resets the TimeProvider in the TemplateURLService. 332 // resets the TimeProvider in the TemplateURLService.
333 StrictMock<base::MockTimeProvider> mock_time; 333 StrictMock<base::MockTimeProvider> mock_time;
334 model()->set_time_provider(&base::MockTimeProvider::StaticNow); 334 model()->set_time_provider(&base::MockTimeProvider::StaticNow);
335 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); 335 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337)));
336 336
337 // Mutate an element and verify it succeeded. 337 // Mutate an element and verify it succeeded.
338 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), 338 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"),
339 "c"); 339 "c");
340 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); 340 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name());
341 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); 341 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword());
342 ASSERT_EQ("c", loaded_url->url()); 342 ASSERT_EQ("c", loaded_url->url());
343 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); 343 ASSERT_FALSE(loaded_url->safe_for_autoreplace());
344 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 344 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
345 NULL)); 345 NULL));
346 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); 346 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL));
347 cloned_url.reset(new TemplateURL(loaded_url->data())); 347 cloned_url.reset(new TemplateURL(loaded_url->data()));
348 base::RunLoop().RunUntilIdle(); 348 base::RunLoop().RunUntilIdle();
349 test_util_.ResetModel(true); 349 test_util_->ResetModel(true);
350 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 350 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
351 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); 351 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
352 ASSERT_TRUE(loaded_url != NULL); 352 ASSERT_TRUE(loaded_url != NULL);
353 AssertEquals(*cloned_url, *loaded_url); 353 AssertEquals(*cloned_url, *loaded_url);
354 // We changed a TemplateURL in the service, so ensure that the time was 354 // We changed a TemplateURL in the service, so ensure that the time was
355 // updated. 355 // updated.
356 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); 356 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified());
357 357
358 // Remove an element and verify it succeeded. 358 // Remove an element and verify it succeeded.
359 model()->Remove(loaded_url); 359 model()->Remove(loaded_url);
360 VerifyObserverCount(1); 360 VerifyObserverCount(1);
361 test_util_.ResetModel(true); 361 test_util_->ResetModel(true);
362 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); 362 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size());
363 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); 363 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL);
364 } 364 }
365 365
366 TEST_F(TemplateURLServiceTest, AddSameKeyword) { 366 TEST_F(TemplateURLServiceTest, AddSameKeyword) {
367 test_util_.VerifyLoad(); 367 test_util_->VerifyLoad();
368 368
369 AddKeywordWithDate( 369 AddKeywordWithDate(
370 "first", "keyword", "http://test1", std::string(), std::string(), 370 "first", "keyword", "http://test1", std::string(), std::string(),
371 std::string(), true, "UTF-8", Time(), Time()); 371 std::string(), true, "UTF-8", Time(), Time());
372 VerifyObserverCount(1); 372 VerifyObserverCount(1);
373 373
374 // Test what happens when we try to add a TemplateURL with the same keyword as 374 // Test what happens when we try to add a TemplateURL with the same keyword as
375 // one in the model. 375 // one in the model.
376 TemplateURLData data; 376 TemplateURLData data;
377 data.short_name = ASCIIToUTF16("second"); 377 data.short_name = ASCIIToUTF16("second");
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 model()->Add(t_url2); 410 model()->Add(t_url2);
411 VerifyObserverCount(1); 411 VerifyObserverCount(1);
412 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); 412 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
413 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); 413 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name());
414 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); 414 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword());
415 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); 415 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
416 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); 416 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword());
417 } 417 }
418 418
419 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { 419 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) {
420 test_util_.VerifyLoad(); 420 test_util_->VerifyLoad();
421 421
422 TemplateURL* original1 = AddKeywordWithDate( 422 TemplateURL* original1 = AddKeywordWithDate(
423 "replaceable", "keyword1", "http://test1", std::string(), std::string(), 423 "replaceable", "keyword1", "http://test1", std::string(), std::string(),
424 std::string(), true, "UTF-8", Time(), Time()); 424 std::string(), true, "UTF-8", Time(), Time());
425 TemplateURL* original2 = AddKeywordWithDate( 425 TemplateURL* original2 = AddKeywordWithDate(
426 "nonreplaceable", "keyword2", "http://test2", std::string(), 426 "nonreplaceable", "keyword2", "http://test2", std::string(),
427 std::string(), std::string(), false, "UTF-8", Time(), Time()); 427 std::string(), std::string(), false, "UTF-8", Time(), Time());
428 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", 428 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3",
429 "http://test3"); 429 "http://test3");
430 TemplateURL* original3 = 430 TemplateURL* original3 =
(...skipping 20 matching lines...) Expand all
451 // They should override extension keywords added earlier. 451 // They should override extension keywords added earlier.
452 model()->RegisterOmniboxKeyword("test6", "test", "keyword3", "http://test6"); 452 model()->RegisterOmniboxKeyword("test6", "test", "keyword3", "http://test6");
453 TemplateURL* extension3 = 453 TemplateURL* extension3 =
454 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); 454 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"));
455 ASSERT_TRUE(extension3); 455 ASSERT_TRUE(extension3);
456 EXPECT_EQ(original3, 456 EXPECT_EQ(original3,
457 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_"))); 457 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_")));
458 } 458 }
459 459
460 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { 460 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) {
461 test_util_.VerifyLoad(); 461 test_util_->VerifyLoad();
462 462
463 // Similar to the AddSameKeyword test, but with an extension keyword masking a 463 // Similar to the AddSameKeyword test, but with an extension keyword masking a
464 // replaceable TemplateURL. We should still do correct conflict resolution 464 // replaceable TemplateURL. We should still do correct conflict resolution
465 // between the non-template URLs. 465 // between the non-template URLs.
466 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", 466 model()->RegisterOmniboxKeyword("test2", "extension", "keyword",
467 "http://test2"); 467 "http://test2");
468 TemplateURL* extension = 468 TemplateURL* extension =
469 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 469 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
470 ASSERT_TRUE(extension); 470 ASSERT_TRUE(extension);
471 // Adding a keyword that matches the extension should cause the extension 471 // Adding a keyword that matches the extension should cause the extension
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 // Try removing foo3. This should delete foo3, but leave foo1 untouched. 611 // Try removing foo3. This should delete foo3, but leave foo1 untouched.
612 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago, 612 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago,
613 now + one_day + one_day); 613 now + one_day + one_day);
614 EXPECT_EQ(1U, model()->GetTemplateURLs().size()); 614 EXPECT_EQ(1U, model()->GetTemplateURLs().size());
615 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); 615 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
616 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace()); 616 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
617 } 617 }
618 618
619 TEST_F(TemplateURLServiceTest, Reset) { 619 TEST_F(TemplateURLServiceTest, Reset) {
620 // Add a new TemplateURL. 620 // Add a new TemplateURL.
621 test_util_.VerifyLoad(); 621 test_util_->VerifyLoad();
622 const size_t initial_count = model()->GetTemplateURLs().size(); 622 const size_t initial_count = model()->GetTemplateURLs().size();
623 TemplateURLData data; 623 TemplateURLData data;
624 data.short_name = ASCIIToUTF16("google"); 624 data.short_name = ASCIIToUTF16("google");
625 data.SetKeyword(ASCIIToUTF16("keyword")); 625 data.SetKeyword(ASCIIToUTF16("keyword"));
626 data.SetURL("http://www.google.com/foo/bar"); 626 data.SetURL("http://www.google.com/foo/bar");
627 data.favicon_url = GURL("http://favicon.url"); 627 data.favicon_url = GURL("http://favicon.url");
628 data.date_created = Time::FromTimeT(100); 628 data.date_created = Time::FromTimeT(100);
629 data.last_modified = Time::FromTimeT(100); 629 data.last_modified = Time::FromTimeT(100);
630 TemplateURL* t_url = new TemplateURL(data); 630 TemplateURL* t_url = new TemplateURL(data);
631 model()->Add(t_url); 631 model()->Add(t_url);
(...skipping 15 matching lines...) Expand all
647 ASSERT_EQ(new_url, t_url->url()); 647 ASSERT_EQ(new_url, t_url->url());
648 648
649 // Make sure the mappings in the model were updated. 649 // Make sure the mappings in the model were updated.
650 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); 650 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword));
651 ASSERT_TRUE( 651 ASSERT_TRUE(
652 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); 652 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL);
653 653
654 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 654 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
655 655
656 // Reload the model from the database and make sure the change took. 656 // Reload the model from the database and make sure the change took.
657 test_util_.ResetModel(true); 657 test_util_->ResetModel(true);
658 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 658 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
659 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); 659 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword);
660 ASSERT_TRUE(read_url); 660 ASSERT_TRUE(read_url);
661 AssertEquals(*cloned_url, *read_url); 661 AssertEquals(*cloned_url, *read_url);
662 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); 662 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified());
663 } 663 }
664 664
665 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { 665 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) {
666 // Add a new TemplateURL. 666 // Add a new TemplateURL.
667 test_util_.VerifyLoad(); 667 test_util_->VerifyLoad();
668 const size_t initial_count = model()->GetTemplateURLs().size(); 668 const size_t initial_count = model()->GetTemplateURLs().size();
669 TemplateURL* t_url = AddKeywordWithDate( 669 TemplateURL* t_url = AddKeywordWithDate(
670 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", 670 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
671 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); 671 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
672 test_util_.ResetObserverCount(); 672 test_util_->ResetObserverCount();
673 673
674 model()->SetUserSelectedDefaultSearchProvider(t_url); 674 model()->SetUserSelectedDefaultSearchProvider(t_url);
675 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); 675 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
676 ASSERT_TRUE(t_url->safe_for_autoreplace()); 676 ASSERT_TRUE(t_url->safe_for_autoreplace());
677 ASSERT_TRUE(t_url->show_in_default_list()); 677 ASSERT_TRUE(t_url->show_in_default_list());
678 678
679 // Setting the default search provider should have caused notification. 679 // Setting the default search provider should have caused notification.
680 VerifyObserverCount(1); 680 VerifyObserverCount(1);
681 base::RunLoop().RunUntilIdle(); 681 base::RunLoop().RunUntilIdle();
682 682
683 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 683 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
684 684
685 // Make sure when we reload we get a default search provider. 685 // Make sure when we reload we get a default search provider.
686 test_util_.ResetModel(true); 686 test_util_->ResetModel(true);
687 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 687 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
688 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 688 ASSERT_TRUE(model()->GetDefaultSearchProvider());
689 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); 689 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
690 } 690 }
691 691
692 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { 692 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) {
693 test_util_.ChangeModelToLoadState(); 693 test_util_->ChangeModelToLoadState();
694 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); 694 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL));
695 TemplateURL* t_url = AddKeywordWithDate( 695 TemplateURL* t_url = AddKeywordWithDate(
696 "name1", "foo", "http://foo1", "http://sugg1", std::string(), 696 "name1", "foo", "http://foo1", "http://sugg1", std::string(),
697 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); 697 "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
698 698
699 // Can still replace, newly added template url is marked safe to replace. 699 // Can still replace, newly added template url is marked safe to replace.
700 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 700 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
701 GURL("http://foo2"), NULL)); 701 GURL("http://foo2"), NULL));
702 702
703 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should 703 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
704 // no longer be replaceable. 704 // no longer be replaceable.
705 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), 705 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
706 t_url->url()); 706 t_url->url());
707 707
708 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 708 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
709 GURL("http://foo2"), NULL)); 709 GURL("http://foo2"), NULL));
710 } 710 }
711 711
712 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { 712 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) {
713 test_util_.ChangeModelToLoadState(); 713 test_util_->ChangeModelToLoadState();
714 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 714 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
715 GURL("http://foo.com"), NULL)); 715 GURL("http://foo.com"), NULL));
716 TemplateURL* t_url = AddKeywordWithDate( 716 TemplateURL* t_url = AddKeywordWithDate(
717 "name1", "foo", "http://foo.com", "http://sugg1", std::string(), 717 "name1", "foo", "http://foo.com", "http://sugg1", std::string(),
718 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); 718 "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
719 719
720 // Can still replace, newly added template url is marked safe to replace. 720 // Can still replace, newly added template url is marked safe to replace.
721 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), 721 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
722 GURL("http://foo.com"), NULL)); 722 GURL("http://foo.com"), NULL));
723 723
724 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should 724 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
725 // no longer be replaceable. 725 // no longer be replaceable.
726 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), 726 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
727 t_url->url()); 727 t_url->url());
728 728
729 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), 729 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
730 GURL("http://foo.com"), NULL)); 730 GURL("http://foo.com"), NULL));
731 } 731 }
732 732
733 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) { 733 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) {
734 // We should have a default search provider even if we haven't loaded. 734 // We should have a default search provider even if we haven't loaded.
735 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 735 ASSERT_TRUE(model()->GetDefaultSearchProvider());
736 736
737 // Now force the model to load and make sure we still have a default. 737 // Now force the model to load and make sure we still have a default.
738 test_util_.VerifyLoad(); 738 test_util_->VerifyLoad();
739 739
740 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 740 ASSERT_TRUE(model()->GetDefaultSearchProvider());
741 } 741 }
742 742
743 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { 743 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) {
744 test_util_.VerifyLoad(); 744 test_util_->VerifyLoad();
745 745
746 TemplateURLData data; 746 TemplateURLData data;
747 data.short_name = ASCIIToUTF16("a"); 747 data.short_name = ASCIIToUTF16("a");
748 data.safe_for_autoreplace = true; 748 data.safe_for_autoreplace = true;
749 data.SetURL("http://url/{searchTerms}"); 749 data.SetURL("http://url/{searchTerms}");
750 data.suggestions_url = "http://url2"; 750 data.suggestions_url = "http://url2";
751 data.instant_url = "http://instant"; 751 data.instant_url = "http://instant";
752 data.date_created = Time::FromTimeT(100); 752 data.date_created = Time::FromTimeT(100);
753 data.last_modified = Time::FromTimeT(100); 753 data.last_modified = Time::FromTimeT(100);
754 TemplateURL* t_url = new TemplateURL(data); 754 TemplateURL* t_url = new TemplateURL(data);
755 model()->Add(t_url); 755 model()->Add(t_url);
756 const TemplateURLID id = t_url->id(); 756 const TemplateURLID id = t_url->id();
757 757
758 model()->SetUserSelectedDefaultSearchProvider(t_url); 758 model()->SetUserSelectedDefaultSearchProvider(t_url);
759 base::RunLoop().RunUntilIdle(); 759 base::RunLoop().RunUntilIdle();
760 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 760 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
761 761
762 // Reset the model and don't load it. The template url we set as the default 762 // Reset the model and don't load it. The template url we set as the default
763 // should be pulled from prefs now. 763 // should be pulled from prefs now.
764 test_util_.ResetModel(false); 764 test_util_->ResetModel(false);
765 765
766 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs 766 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs
767 // value are persisted to prefs. 767 // value are persisted to prefs.
768 const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); 768 const TemplateURL* default_turl = model()->GetDefaultSearchProvider();
769 ASSERT_TRUE(default_turl); 769 ASSERT_TRUE(default_turl);
770 EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name()); 770 EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name());
771 EXPECT_EQ("http://url/{searchTerms}", default_turl->url()); 771 EXPECT_EQ("http://url/{searchTerms}", default_turl->url());
772 EXPECT_EQ("http://url2", default_turl->suggestions_url()); 772 EXPECT_EQ("http://url2", default_turl->suggestions_url());
773 EXPECT_EQ("http://instant", default_turl->instant_url()); 773 EXPECT_EQ("http://instant", default_turl->instant_url());
774 EXPECT_EQ(id, default_turl->id()); 774 EXPECT_EQ(id, default_turl->id());
775 775
776 // Now do a load and make sure the default search provider really takes. 776 // Now do a load and make sure the default search provider really takes.
777 test_util_.VerifyLoad(); 777 test_util_->VerifyLoad();
778 778
779 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 779 ASSERT_TRUE(model()->GetDefaultSearchProvider());
780 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); 780 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
781 } 781 }
782 782
783 TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) { 783 TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) {
784 test_util_.VerifyLoad(); 784 test_util_->VerifyLoad();
785 785
786 // Edit Google search engine. 786 // Edit Google search engine.
787 TemplateURL* google = model()->GetTemplateURLForKeyword( 787 TemplateURL* google = model()->GetTemplateURLForKeyword(
788 ASCIIToUTF16("google.com")); 788 ASCIIToUTF16("google.com"));
789 ASSERT_TRUE(google); 789 ASSERT_TRUE(google);
790 model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"), 790 model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"),
791 "http://www.foo.com/s?q={searchTerms}"); 791 "http://www.foo.com/s?q={searchTerms}");
792 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name()); 792 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name());
793 EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword()); 793 EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword());
794 794
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 833
834 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { 834 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) {
835 // Set a managed preference that establishes a default search provider. 835 // Set a managed preference that establishes a default search provider.
836 const char kName[] = "test1"; 836 const char kName[] = "test1";
837 const char kKeyword[] = "test.com"; 837 const char kKeyword[] = "test.com";
838 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; 838 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
839 const char kIconURL[] = "http://test.com/icon.jpg"; 839 const char kIconURL[] = "http://test.com/icon.jpg";
840 const char kEncodings[] = "UTF-16;UTF-32"; 840 const char kEncodings[] = "UTF-16;UTF-32";
841 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; 841 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
842 const char kSearchTermsReplacementKey[] = "espv"; 842 const char kSearchTermsReplacementKey[] = "espv";
843 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, 843 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
844 kSearchURL, std::string(), 844 kSearchURL, std::string(),
845 kIconURL, kEncodings, 845 kIconURL, kEncodings,
846 kAlternateURL, 846 kAlternateURL,
847 kSearchTermsReplacementKey); 847 kSearchTermsReplacementKey);
848 test_util_.VerifyLoad(); 848 test_util_->VerifyLoad();
849 // Verify that the default manager we are getting is the managed one. 849 // Verify that the default manager we are getting is the managed one.
850 TemplateURLData data; 850 TemplateURLData data;
851 data.short_name = ASCIIToUTF16(kName); 851 data.short_name = ASCIIToUTF16(kName);
852 data.SetKeyword(ASCIIToUTF16(kKeyword)); 852 data.SetKeyword(ASCIIToUTF16(kKeyword));
853 data.SetURL(kSearchURL); 853 data.SetURL(kSearchURL);
854 data.favicon_url = GURL(kIconURL); 854 data.favicon_url = GURL(kIconURL);
855 data.show_in_default_list = true; 855 data.show_in_default_list = true;
856 base::SplitString(kEncodings, ';', &data.input_encodings); 856 base::SplitString(kEncodings, ';', &data.input_encodings);
857 data.alternate_urls.push_back(kAlternateURL); 857 data.alternate_urls.push_back(kAlternateURL);
858 data.search_terms_replacement_key = kSearchTermsReplacementKey; 858 data.search_terms_replacement_key = kSearchTermsReplacementKey;
(...skipping 22 matching lines...) Expand all
881 { "http://x/foo?y=xx", base::string16() }, 881 { "http://x/foo?y=xx", base::string16() },
882 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, 882 { "http://x/foo?q=xx", ASCIIToUTF16("xx") },
883 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, 883 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") },
884 { "http://x/foo?q=b&q=xx", base::string16() }, 884 { "http://x/foo?q=b&q=xx", base::string16() },
885 { "http://x/foo#query=xx", ASCIIToUTF16("xx") }, 885 { "http://x/foo#query=xx", ASCIIToUTF16("xx") },
886 { "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") }, 886 { "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") },
887 { "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") }, 887 { "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") },
888 { "http://x/foo?query=b#q=xx", base::string16() }, 888 { "http://x/foo?query=b#q=xx", base::string16() },
889 }; 889 };
890 890
891 test_util_.ChangeModelToLoadState(); 891 test_util_->ChangeModelToLoadState();
892 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}", 892 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}",
893 "http://sugg1", "http://x/foo#query={searchTerms}", 893 "http://sugg1", "http://x/foo#query={searchTerms}",
894 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); 894 "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
895 895
896 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 896 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
897 TemplateURLService::URLVisitedDetails details = { 897 TemplateURLService::URLVisitedDetails details = {
898 GURL(data[i].url), false 898 GURL(data[i].url), false
899 }; 899 };
900 model()->UpdateKeywordSearchTermsForURL(details); 900 model()->UpdateKeywordSearchTermsForURL(details);
901 EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm()); 901 EXPECT_EQ(data[i].term, test_util_->GetAndClearSearchTerm());
902 } 902 }
903 } 903 }
904 904
905 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { 905 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) {
906 struct TestData { 906 struct TestData {
907 const std::string url; 907 const std::string url;
908 } data[] = { 908 } data[] = {
909 { "http://foo/" }, 909 { "http://foo/" },
910 { "http://x/bar?q=xx" }, 910 { "http://x/bar?q=xx" },
911 { "http://x/foo?y=xx" }, 911 { "http://x/foo?y=xx" },
912 }; 912 };
913 913
914 test_util_.ChangeModelToLoadState(); 914 test_util_->ChangeModelToLoadState();
915 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(), 915 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(),
916 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); 916 "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
917 917
918 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 918 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
919 TemplateURLService::URLVisitedDetails details = { 919 TemplateURLService::URLVisitedDetails details = {
920 GURL(data[i].url), false 920 GURL(data[i].url), false
921 }; 921 };
922 model()->UpdateKeywordSearchTermsForURL(details); 922 model()->UpdateKeywordSearchTermsForURL(details);
923 ASSERT_EQ(base::string16(), test_util_.GetAndClearSearchTerm()); 923 ASSERT_EQ(base::string16(), test_util_->GetAndClearSearchTerm());
924 } 924 }
925 } 925 }
926 926
927 TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { 927 TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) {
928 // NOTE: Do not load the prepopulate data, which also has a {google:baseURL} 928 // NOTE: Do not load the prepopulate data, which also has a {google:baseURL}
929 // keyword in it and would confuse this test. 929 // keyword in it and would confuse this test.
930 test_util_.ChangeModelToLoadState(); 930 test_util_->ChangeModelToLoadState();
931 931
932 test_util_.SetGoogleBaseURL(GURL("http://google.com/")); 932 test_util_->SetGoogleBaseURL(GURL("http://google.com/"));
933 const TemplateURL* t_url = AddKeywordWithDate( 933 const TemplateURL* t_url = AddKeywordWithDate(
934 "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1", 934 "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1",
935 std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); 935 std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
936 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); 936 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com"));
937 EXPECT_EQ("google.com", t_url->url_ref().GetHost(search_terms_data())); 937 EXPECT_EQ("google.com", t_url->url_ref().GetHost(search_terms_data()));
938 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); 938 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword());
939 939
940 // Change the Google base url. 940 // Change the Google base url.
941 test_util_.ResetObserverCount(); 941 test_util_->ResetObserverCount();
942 test_util_.SetGoogleBaseURL(GURL("http://google.co.uk/")); 942 test_util_->SetGoogleBaseURL(GURL("http://google.co.uk/"));
943 VerifyObserverCount(1); 943 VerifyObserverCount(1);
944 944
945 // Make sure the host->TemplateURL map was updated appropriately. 945 // Make sure the host->TemplateURL map was updated appropriately.
946 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); 946 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk"));
947 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); 947 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL);
948 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost(search_terms_data())); 948 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost(search_terms_data()));
949 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); 949 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
950 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( 950 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms(
951 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data())); 951 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data()));
952 952
953 // Now add a manual entry and then change the Google base URL such that the 953 // Now add a manual entry and then change the Google base URL such that the
954 // autogenerated Google search keyword would conflict. 954 // autogenerated Google search keyword would conflict.
955 TemplateURL* manual = AddKeywordWithDate( 955 TemplateURL* manual = AddKeywordWithDate(
956 "manual", "google.de", "http://google.de/search?q={searchTerms}", 956 "manual", "google.de", "http://google.de/search?q={searchTerms}",
957 std::string(), std::string(), std::string(), false, "UTF-8", Time(), 957 std::string(), std::string(), std::string(), false, "UTF-8", Time(),
958 Time()); 958 Time());
959 test_util_.SetGoogleBaseURL(GURL("http://google.de")); 959 test_util_->SetGoogleBaseURL(GURL("http://google.de"));
960 960
961 // Verify that the manual entry is untouched, and the autogenerated keyword 961 // Verify that the manual entry is untouched, and the autogenerated keyword
962 // has not changed. 962 // has not changed.
963 ASSERT_EQ(manual, 963 ASSERT_EQ(manual,
964 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de"))); 964 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de")));
965 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); 965 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data()));
966 ASSERT_EQ(t_url, 966 ASSERT_EQ(t_url,
967 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk"))); 967 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk")));
968 EXPECT_EQ("google.de", t_url->url_ref().GetHost(search_terms_data())); 968 EXPECT_EQ("google.de", t_url->url_ref().GetHost(search_terms_data()));
969 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); 969 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
970 970
971 // Change the base URL again and verify that the autogenerated keyword follows 971 // Change the base URL again and verify that the autogenerated keyword follows
972 // even though it didn't match the base URL, while the manual entry is still 972 // even though it didn't match the base URL, while the manual entry is still
973 // untouched. 973 // untouched.
974 test_util_.SetGoogleBaseURL(GURL("http://google.fr/")); 974 test_util_->SetGoogleBaseURL(GURL("http://google.fr/"));
975 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de")); 975 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de"));
976 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); 976 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data()));
977 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword()); 977 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword());
978 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr")); 978 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr"));
979 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL); 979 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL);
980 EXPECT_EQ("google.fr", t_url->url_ref().GetHost(search_terms_data())); 980 EXPECT_EQ("google.fr", t_url->url_ref().GetHost(search_terms_data()));
981 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword()); 981 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword());
982 } 982 }
983 983
984 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for 984 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for
985 // KEYWORD visits. 985 // KEYWORD visits.
986 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { 986 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) {
987 test_util_.VerifyLoad(); 987 test_util_->VerifyLoad();
988 ASSERT_TRUE(test_util_.profile()->CreateHistoryService(true, false)); 988 ASSERT_TRUE(test_util_->profile()->CreateHistoryService(true, false));
989 989
990 // Create a keyword. 990 // Create a keyword.
991 TemplateURL* t_url = AddKeywordWithDate( 991 TemplateURL* t_url = AddKeywordWithDate(
992 "keyword", "keyword", "http://foo.com/foo?query={searchTerms}", 992 "keyword", "keyword", "http://foo.com/foo?query={searchTerms}",
993 "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16", 993 "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16",
994 base::Time::Now(), base::Time::Now()); 994 base::Time::Now(), base::Time::Now());
995 995
996 // Add a visit that matches the url of the keyword. 996 // Add a visit that matches the url of the keyword.
997 HistoryService* history = 997 HistoryService* history =
998 HistoryServiceFactory::GetForProfile(test_util_.profile(), 998 HistoryServiceFactory::GetForProfile(test_util_->profile(),
999 Profile::EXPLICIT_ACCESS); 999 Profile::EXPLICIT_ACCESS);
1000 history->AddPage( 1000 history->AddPage(
1001 GURL(t_url->url_ref().ReplaceSearchTerms( 1001 GURL(t_url->url_ref().ReplaceSearchTerms(
1002 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")), 1002 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")),
1003 search_terms_data())), 1003 search_terms_data())),
1004 base::Time::Now(), NULL, 0, GURL(), history::RedirectList(), 1004 base::Time::Now(), NULL, 0, GURL(), history::RedirectList(),
1005 content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false); 1005 content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false);
1006 1006
1007 // Wait for history to finish processing the request. 1007 // Wait for history to finish processing the request.
1008 test_util_.profile()->BlockUntilHistoryProcessesPendingRequests(); 1008 test_util_->profile()->BlockUntilHistoryProcessesPendingRequests();
1009 1009
1010 // Query history for the generated url. 1010 // Query history for the generated url.
1011 base::CancelableTaskTracker tracker; 1011 base::CancelableTaskTracker tracker;
1012 QueryHistoryCallbackImpl callback; 1012 QueryHistoryCallbackImpl callback;
1013 history->QueryURL(GURL("http://keyword"), 1013 history->QueryURL(GURL("http://keyword"),
1014 true, 1014 true,
1015 base::Bind(&QueryHistoryCallbackImpl::Callback, 1015 base::Bind(&QueryHistoryCallbackImpl::Callback,
1016 base::Unretained(&callback)), 1016 base::Unretained(&callback)),
1017 &tracker); 1017 &tracker);
1018 1018
1019 // Wait for the request to be processed. 1019 // Wait for the request to be processed.
1020 test_util_.profile()->BlockUntilHistoryProcessesPendingRequests(); 1020 test_util_->profile()->BlockUntilHistoryProcessesPendingRequests();
1021 1021
1022 // And make sure the url and visit were added. 1022 // And make sure the url and visit were added.
1023 EXPECT_TRUE(callback.success); 1023 EXPECT_TRUE(callback.success);
1024 EXPECT_NE(0, callback.row.id()); 1024 EXPECT_NE(0, callback.row.id());
1025 ASSERT_EQ(1U, callback.visits.size()); 1025 ASSERT_EQ(1U, callback.visits.size());
1026 EXPECT_EQ(content::PAGE_TRANSITION_KEYWORD_GENERATED, 1026 EXPECT_EQ(content::PAGE_TRANSITION_KEYWORD_GENERATED,
1027 content::PageTransitionStripQualifier(callback.visits[0].transition)); 1027 content::PageTransitionStripQualifier(callback.visits[0].transition));
1028 } 1028 }
1029 1029
1030 // Make sure that the load routine deletes prepopulated engines that no longer 1030 // Make sure that the load routine deletes prepopulated engines that no longer
1031 // exist in the prepopulate data. 1031 // exist in the prepopulate data.
1032 TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { 1032 TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) {
1033 // Create a preloaded template url. Add it to a loaded model and wait for the 1033 // Create a preloaded template url. Add it to a loaded model and wait for the
1034 // saves to finish. 1034 // saves to finish.
1035 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); 1035 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
1036 test_util_.ChangeModelToLoadState(); 1036 test_util_->ChangeModelToLoadState();
1037 model()->Add(t_url); 1037 model()->Add(t_url);
1038 ASSERT_TRUE( 1038 ASSERT_TRUE(
1039 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); 1039 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
1040 base::RunLoop().RunUntilIdle(); 1040 base::RunLoop().RunUntilIdle();
1041 1041
1042 // Ensure that merging clears this engine. 1042 // Ensure that merging clears this engine.
1043 test_util_.ResetModel(true); 1043 test_util_->ResetModel(true);
1044 ASSERT_TRUE( 1044 ASSERT_TRUE(
1045 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); 1045 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
1046 1046
1047 // Wait for any saves to finish. 1047 // Wait for any saves to finish.
1048 base::RunLoop().RunUntilIdle(); 1048 base::RunLoop().RunUntilIdle();
1049 1049
1050 // Reload the model to verify that the database was updated as a result of the 1050 // Reload the model to verify that the database was updated as a result of the
1051 // merge. 1051 // merge.
1052 test_util_.ResetModel(true); 1052 test_util_->ResetModel(true);
1053 ASSERT_TRUE( 1053 ASSERT_TRUE(
1054 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); 1054 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
1055 } 1055 }
1056 1056
1057 // Make sure that load routine doesn't delete prepopulated engines that no 1057 // Make sure that load routine doesn't delete prepopulated engines that no
1058 // longer exist in the prepopulate data if it has been modified by the user. 1058 // longer exist in the prepopulate data if it has been modified by the user.
1059 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { 1059 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) {
1060 // Create a preloaded template url and add it to a loaded model. 1060 // Create a preloaded template url and add it to a loaded model.
1061 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999); 1061 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999);
1062 test_util_.ChangeModelToLoadState(); 1062 test_util_->ChangeModelToLoadState();
1063 model()->Add(t_url); 1063 model()->Add(t_url);
1064 1064
1065 // Do the copy after t_url is added so that the id is set. 1065 // Do the copy after t_url is added so that the id is set.
1066 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 1066 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1067 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); 1067 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1068 1068
1069 // Wait for any saves to finish. 1069 // Wait for any saves to finish.
1070 base::RunLoop().RunUntilIdle(); 1070 base::RunLoop().RunUntilIdle();
1071 1071
1072 // Ensure that merging won't clear it if the user has edited it. 1072 // Ensure that merging won't clear it if the user has edited it.
1073 test_util_.ResetModel(true); 1073 test_util_->ResetModel(true);
1074 const TemplateURL* url_for_unittest = 1074 const TemplateURL* url_for_unittest =
1075 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1075 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1076 ASSERT_TRUE(url_for_unittest != NULL); 1076 ASSERT_TRUE(url_for_unittest != NULL);
1077 AssertEquals(*cloned_url, *url_for_unittest); 1077 AssertEquals(*cloned_url, *url_for_unittest);
1078 1078
1079 // Wait for any saves to finish. 1079 // Wait for any saves to finish.
1080 base::RunLoop().RunUntilIdle(); 1080 base::RunLoop().RunUntilIdle();
1081 1081
1082 // Reload the model to verify that save/reload retains the item. 1082 // Reload the model to verify that save/reload retains the item.
1083 test_util_.ResetModel(true); 1083 test_util_->ResetModel(true);
1084 ASSERT_TRUE( 1084 ASSERT_TRUE(
1085 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); 1085 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
1086 } 1086 }
1087 1087
1088 // Make sure that load routine doesn't delete 1088 // Make sure that load routine doesn't delete
1089 // prepopulated engines that no longer exist in the prepopulate data if 1089 // prepopulated engines that no longer exist in the prepopulate data if
1090 // it has been modified by the user. 1090 // it has been modified by the user.
1091 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { 1091 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) {
1092 test_util_.VerifyLoad(); 1092 test_util_->VerifyLoad();
1093 // Verify that the default search provider is set to something. 1093 // Verify that the default search provider is set to something.
1094 TemplateURL* default_search = model()->GetDefaultSearchProvider(); 1094 TemplateURL* default_search = model()->GetDefaultSearchProvider();
1095 ASSERT_TRUE(default_search != NULL); 1095 ASSERT_TRUE(default_search != NULL);
1096 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data())); 1096 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data()));
1097 1097
1098 // Wait for any saves to finish. 1098 // Wait for any saves to finish.
1099 base::RunLoop().RunUntilIdle(); 1099 base::RunLoop().RunUntilIdle();
1100 1100
1101 // Reload the model and check that the default search provider 1101 // Reload the model and check that the default search provider
1102 // was properly saved. 1102 // was properly saved.
1103 test_util_.ResetModel(true); 1103 test_util_->ResetModel(true);
1104 default_search = model()->GetDefaultSearchProvider(); 1104 default_search = model()->GetDefaultSearchProvider();
1105 ASSERT_TRUE(default_search != NULL); 1105 ASSERT_TRUE(default_search != NULL);
1106 AssertEquals(*cloned_url, *default_search); 1106 AssertEquals(*cloned_url, *default_search);
1107 } 1107 }
1108 1108
1109 // Make sure that the load routine doesn't delete 1109 // Make sure that the load routine doesn't delete
1110 // prepopulated engines that no longer exist in the prepopulate data if 1110 // prepopulated engines that no longer exist in the prepopulate data if
1111 // it is the default search provider. 1111 // it is the default search provider.
1112 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { 1112 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) {
1113 // Set the default search provider to a preloaded template url which 1113 // Set the default search provider to a preloaded template url which
1114 // is not in the current set of preloaded template urls and save 1114 // is not in the current set of preloaded template urls and save
1115 // the result. 1115 // the result.
1116 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); 1116 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
1117 test_util_.ChangeModelToLoadState(); 1117 test_util_->ChangeModelToLoadState();
1118 model()->Add(t_url); 1118 model()->Add(t_url);
1119 model()->SetUserSelectedDefaultSearchProvider(t_url); 1119 model()->SetUserSelectedDefaultSearchProvider(t_url);
1120 // Do the copy after t_url is added and set as default so that its 1120 // Do the copy after t_url is added and set as default so that its
1121 // internal state is correct. 1121 // internal state is correct.
1122 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); 1122 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1123 1123
1124 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); 1124 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1125 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); 1125 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
1126 base::RunLoop().RunUntilIdle(); 1126 base::RunLoop().RunUntilIdle();
1127 1127
1128 // Ensure that merging won't clear the prepopulated template url 1128 // Ensure that merging won't clear the prepopulated template url
1129 // which is no longer present if it's the default engine. 1129 // which is no longer present if it's the default engine.
1130 test_util_.ResetModel(true); 1130 test_util_->ResetModel(true);
1131 { 1131 {
1132 const TemplateURL* keyword_url = 1132 const TemplateURL* keyword_url =
1133 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1133 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1134 ASSERT_TRUE(keyword_url != NULL); 1134 ASSERT_TRUE(keyword_url != NULL);
1135 AssertEquals(*cloned_url, *keyword_url); 1135 AssertEquals(*cloned_url, *keyword_url);
1136 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); 1136 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1137 } 1137 }
1138 1138
1139 // Wait for any saves to finish. 1139 // Wait for any saves to finish.
1140 base::RunLoop().RunUntilIdle(); 1140 base::RunLoop().RunUntilIdle();
1141 1141
1142 // Reload the model to verify that the update was saved. 1142 // Reload the model to verify that the update was saved.
1143 test_util_.ResetModel(true); 1143 test_util_->ResetModel(true);
1144 { 1144 {
1145 const TemplateURL* keyword_url = 1145 const TemplateURL* keyword_url =
1146 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1146 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1147 ASSERT_TRUE(keyword_url != NULL); 1147 ASSERT_TRUE(keyword_url != NULL);
1148 AssertEquals(*cloned_url, *keyword_url); 1148 AssertEquals(*cloned_url, *keyword_url);
1149 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); 1149 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1150 } 1150 }
1151 } 1151 }
1152 1152
1153 // Make sure that the load routine sets a default search provider if it was 1153 // Make sure that the load routine sets a default search provider if it was
1154 // missing and not managed. 1154 // missing and not managed.
1155 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { 1155 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) {
1156 // Force the model to load and make sure we have a default search provider. 1156 // Force the model to load and make sure we have a default search provider.
1157 test_util_.VerifyLoad(); 1157 test_util_->VerifyLoad();
1158 EXPECT_TRUE(model()->GetDefaultSearchProvider()); 1158 EXPECT_TRUE(model()->GetDefaultSearchProvider());
1159 1159
1160 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( 1160 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement(
1161 search_terms_data())); 1161 search_terms_data()));
1162 1162
1163 // Make default search provider unusable (no search terms). 1163 // Make default search provider unusable (no search terms).
1164 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(), 1164 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(),
1165 ASCIIToUTF16("test"), ASCIIToUTF16("test"), 1165 ASCIIToUTF16("test"), ASCIIToUTF16("test"),
1166 "http://example.com/"); 1166 "http://example.com/");
1167 base::RunLoop().RunUntilIdle(); 1167 base::RunLoop().RunUntilIdle();
1168 1168
1169 // Reset the model and load it. There should be a usable default search 1169 // Reset the model and load it. There should be a usable default search
1170 // provider. 1170 // provider.
1171 test_util_.ResetModel(true); 1171 test_util_->ResetModel(true);
1172 1172
1173 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 1173 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1174 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( 1174 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement(
1175 search_terms_data())); 1175 search_terms_data()));
1176 } 1176 }
1177 1177
1178 // Simulates failing to load the webdb and makes sure the default search 1178 // Simulates failing to load the webdb and makes sure the default search
1179 // provider is valid. 1179 // provider is valid.
1180 TEST_F(TemplateURLServiceTest, FailedInit) { 1180 TEST_F(TemplateURLServiceTest, FailedInit) {
1181 test_util_.VerifyLoad(); 1181 test_util_->VerifyLoad();
1182 1182
1183 test_util_.ClearModel(); 1183 test_util_->ClearModel();
1184 WebDataServiceFactory::GetKeywordWebDataForProfile( 1184 WebDataServiceFactory::GetKeywordWebDataForProfile(
1185 test_util_.profile(), Profile::EXPLICIT_ACCESS)->ShutdownDatabase(); 1185 test_util_->profile(), Profile::EXPLICIT_ACCESS)->ShutdownDatabase();
1186 1186
1187 test_util_.ResetModel(false); 1187 test_util_->ResetModel(false);
1188 model()->Load(); 1188 model()->Load();
1189 base::RunLoop().RunUntilIdle(); 1189 base::RunLoop().RunUntilIdle();
1190 1190
1191 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 1191 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1192 } 1192 }
1193 1193
1194 // Verifies that if the default search URL preference is managed, we report 1194 // Verifies that if the default search URL preference is managed, we report
1195 // the default search as managed. Also check that we are getting the right 1195 // the default search as managed. Also check that we are getting the right
1196 // values. 1196 // values.
1197 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { 1197 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) {
1198 test_util_.VerifyLoad(); 1198 test_util_->VerifyLoad();
1199 const size_t initial_count = model()->GetTemplateURLs().size(); 1199 const size_t initial_count = model()->GetTemplateURLs().size();
1200 test_util_.ResetObserverCount(); 1200 test_util_->ResetObserverCount();
1201 1201
1202 // Set a regular default search provider. 1202 // Set a regular default search provider.
1203 TemplateURL* regular_default = AddKeywordWithDate( 1203 TemplateURL* regular_default = AddKeywordWithDate(
1204 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", 1204 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
1205 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); 1205 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
1206 VerifyObserverCount(1); 1206 VerifyObserverCount(1);
1207 model()->SetUserSelectedDefaultSearchProvider(regular_default); 1207 model()->SetUserSelectedDefaultSearchProvider(regular_default);
1208 // Adding the URL and setting the default search provider should have caused 1208 // Adding the URL and setting the default search provider should have caused
1209 // notifications. 1209 // notifications.
1210 VerifyObserverCount(1); 1210 VerifyObserverCount(1);
1211 EXPECT_FALSE(model()->is_default_search_managed()); 1211 EXPECT_FALSE(model()->is_default_search_managed());
1212 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1212 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1213 1213
1214 // Set a managed preference that establishes a default search provider. 1214 // Set a managed preference that establishes a default search provider.
1215 const char kName[] = "test1"; 1215 const char kName[] = "test1";
1216 const char kKeyword[] = "test.com"; 1216 const char kKeyword[] = "test.com";
1217 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; 1217 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
1218 const char kIconURL[] = "http://test.com/icon.jpg"; 1218 const char kIconURL[] = "http://test.com/icon.jpg";
1219 const char kEncodings[] = "UTF-16;UTF-32"; 1219 const char kEncodings[] = "UTF-16;UTF-32";
1220 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; 1220 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
1221 const char kSearchTermsReplacementKey[] = "espv"; 1221 const char kSearchTermsReplacementKey[] = "espv";
1222 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, 1222 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
1223 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, 1223 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
1224 kSearchTermsReplacementKey); 1224 kSearchTermsReplacementKey);
1225 VerifyObserverFired(); 1225 VerifyObserverFired();
1226 EXPECT_TRUE(model()->is_default_search_managed()); 1226 EXPECT_TRUE(model()->is_default_search_managed());
1227 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1227 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1228 1228
1229 // Verify that the default manager we are getting is the managed one. 1229 // Verify that the default manager we are getting is the managed one.
1230 TemplateURLData data; 1230 TemplateURLData data;
1231 data.short_name = ASCIIToUTF16(kName); 1231 data.short_name = ASCIIToUTF16(kName);
1232 data.SetKeyword(ASCIIToUTF16(kKeyword)); 1232 data.SetKeyword(ASCIIToUTF16(kKeyword));
1233 data.SetURL(kSearchURL); 1233 data.SetURL(kSearchURL);
1234 data.favicon_url = GURL(kIconURL); 1234 data.favicon_url = GURL(kIconURL);
1235 data.show_in_default_list = true; 1235 data.show_in_default_list = true;
1236 base::SplitString(kEncodings, ';', &data.input_encodings); 1236 base::SplitString(kEncodings, ';', &data.input_encodings);
1237 data.alternate_urls.push_back(kAlternateURL); 1237 data.alternate_urls.push_back(kAlternateURL);
1238 data.search_terms_replacement_key = kSearchTermsReplacementKey; 1238 data.search_terms_replacement_key = kSearchTermsReplacementKey;
1239 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data)); 1239 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data));
1240 const TemplateURL* actual_managed_default = 1240 const TemplateURL* actual_managed_default =
1241 model()->GetDefaultSearchProvider(); 1241 model()->GetDefaultSearchProvider();
1242 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); 1242 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1243 EXPECT_TRUE(actual_managed_default->show_in_default_list()); 1243 EXPECT_TRUE(actual_managed_default->show_in_default_list());
1244 1244
1245 // Update the managed preference and check that the model has changed. 1245 // Update the managed preference and check that the model has changed.
1246 const char kNewName[] = "test2"; 1246 const char kNewName[] = "test2";
1247 const char kNewKeyword[] = "other.com"; 1247 const char kNewKeyword[] = "other.com";
1248 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; 1248 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}";
1249 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; 1249 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}";
1250 test_util_.SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, 1250 test_util_->SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword,
1251 kNewSearchURL, kNewSuggestURL, std::string(), std::string(), 1251 kNewSearchURL, kNewSuggestURL, std::string(), std::string(),
1252 std::string(), std::string()); 1252 std::string(), std::string());
1253 VerifyObserverFired(); 1253 VerifyObserverFired();
1254 EXPECT_TRUE(model()->is_default_search_managed()); 1254 EXPECT_TRUE(model()->is_default_search_managed());
1255 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1255 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1256 1256
1257 // Verify that the default manager we are now getting is the correct one. 1257 // Verify that the default manager we are now getting is the correct one.
1258 TemplateURLData data2; 1258 TemplateURLData data2;
1259 data2.short_name = ASCIIToUTF16(kNewName); 1259 data2.short_name = ASCIIToUTF16(kNewName);
1260 data2.SetKeyword(ASCIIToUTF16(kNewKeyword)); 1260 data2.SetKeyword(ASCIIToUTF16(kNewKeyword));
1261 data2.SetURL(kNewSearchURL); 1261 data2.SetURL(kNewSearchURL);
1262 data2.suggestions_url = kNewSuggestURL; 1262 data2.suggestions_url = kNewSuggestURL;
1263 data2.show_in_default_list = true; 1263 data2.show_in_default_list = true;
1264 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2)); 1264 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2));
1265 actual_managed_default = model()->GetDefaultSearchProvider(); 1265 actual_managed_default = model()->GetDefaultSearchProvider();
1266 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); 1266 ExpectSimilar(expected_managed_default2.get(), actual_managed_default);
1267 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); 1267 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1268 1268
1269 // Remove all the managed prefs and check that we are no longer managed. 1269 // Remove all the managed prefs and check that we are no longer managed.
1270 test_util_.RemoveManagedDefaultSearchPreferences(); 1270 test_util_->RemoveManagedDefaultSearchPreferences();
1271 VerifyObserverFired(); 1271 VerifyObserverFired();
1272 EXPECT_FALSE(model()->is_default_search_managed()); 1272 EXPECT_FALSE(model()->is_default_search_managed());
1273 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1273 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1274 1274
1275 // The default should now be the user preference. 1275 // The default should now be the user preference.
1276 const TemplateURL* actual_final_managed_default = 1276 const TemplateURL* actual_final_managed_default =
1277 model()->GetDefaultSearchProvider(); 1277 model()->GetDefaultSearchProvider();
1278 ExpectSimilar(regular_default, actual_final_managed_default); 1278 ExpectSimilar(regular_default, actual_final_managed_default);
1279 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); 1279 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true);
1280 1280
1281 // Disable the default search provider through policy. 1281 // Disable the default search provider through policy.
1282 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), 1282 test_util_->SetManagedDefaultSearchPreferences(false, std::string(),
1283 std::string(), std::string(), std::string(), std::string(), 1283 std::string(), std::string(), std::string(), std::string(),
1284 std::string(), std::string(), std::string()); 1284 std::string(), std::string(), std::string());
1285 VerifyObserverFired(); 1285 VerifyObserverFired();
1286 EXPECT_TRUE(model()->is_default_search_managed()); 1286 EXPECT_TRUE(model()->is_default_search_managed());
1287 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); 1287 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider());
1288 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1288 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1289 1289
1290 // Re-enable it. 1290 // Re-enable it.
1291 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, 1291 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
1292 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, 1292 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
1293 kSearchTermsReplacementKey); 1293 kSearchTermsReplacementKey);
1294 VerifyObserverFired(); 1294 VerifyObserverFired();
1295 EXPECT_TRUE(model()->is_default_search_managed()); 1295 EXPECT_TRUE(model()->is_default_search_managed());
1296 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1296 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1297 1297
1298 // Verify that the default manager we are getting is the managed one. 1298 // Verify that the default manager we are getting is the managed one.
1299 actual_managed_default = model()->GetDefaultSearchProvider(); 1299 actual_managed_default = model()->GetDefaultSearchProvider();
1300 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); 1300 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1301 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); 1301 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1302 1302
1303 // Clear the model and disable the default search provider through policy. 1303 // Clear the model and disable the default search provider through policy.
1304 // Verify that there is no default search provider after loading the model. 1304 // Verify that there is no default search provider after loading the model.
1305 // This checks against regressions of http://crbug.com/67180 1305 // This checks against regressions of http://crbug.com/67180
1306 1306
1307 // First, remove the preferences, reset the model, and set a default. 1307 // First, remove the preferences, reset the model, and set a default.
1308 test_util_.RemoveManagedDefaultSearchPreferences(); 1308 test_util_->RemoveManagedDefaultSearchPreferences();
1309 test_util_.ResetModel(true); 1309 test_util_->ResetModel(true);
1310 TemplateURL* new_default = 1310 TemplateURL* new_default =
1311 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); 1311 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1"));
1312 ASSERT_FALSE(new_default == NULL); 1312 ASSERT_FALSE(new_default == NULL);
1313 model()->SetUserSelectedDefaultSearchProvider(new_default); 1313 model()->SetUserSelectedDefaultSearchProvider(new_default);
1314 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); 1314 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider());
1315 1315
1316 // Now reset the model again but load it after setting the preferences. 1316 // Now reset the model again but load it after setting the preferences.
1317 test_util_.ResetModel(false); 1317 test_util_->ResetModel(false);
1318 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), 1318 test_util_->SetManagedDefaultSearchPreferences(false, std::string(),
1319 std::string(), std::string(), std::string(), std::string(), 1319 std::string(), std::string(), std::string(), std::string(),
1320 std::string(), std::string(), std::string()); 1320 std::string(), std::string(), std::string());
1321 test_util_.VerifyLoad(); 1321 test_util_->VerifyLoad();
1322 EXPECT_TRUE(model()->is_default_search_managed()); 1322 EXPECT_TRUE(model()->is_default_search_managed());
1323 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); 1323 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL);
1324 } 1324 }
1325 1325
1326 // Test that if we load a TemplateURL with an empty GUID, the load process 1326 // Test that if we load a TemplateURL with an empty GUID, the load process
1327 // assigns it a newly generated GUID. 1327 // assigns it a newly generated GUID.
1328 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { 1328 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) {
1329 // Add a new TemplateURL. 1329 // Add a new TemplateURL.
1330 test_util_.VerifyLoad(); 1330 test_util_->VerifyLoad();
1331 const size_t initial_count = model()->GetTemplateURLs().size(); 1331 const size_t initial_count = model()->GetTemplateURLs().size();
1332 1332
1333 TemplateURLData data; 1333 TemplateURLData data;
1334 data.short_name = ASCIIToUTF16("google"); 1334 data.short_name = ASCIIToUTF16("google");
1335 data.SetKeyword(ASCIIToUTF16("keyword")); 1335 data.SetKeyword(ASCIIToUTF16("keyword"));
1336 data.SetURL("http://www.google.com/foo/bar"); 1336 data.SetURL("http://www.google.com/foo/bar");
1337 data.sync_guid.clear(); 1337 data.sync_guid.clear();
1338 TemplateURL* t_url = new TemplateURL(data); 1338 TemplateURL* t_url = new TemplateURL(data);
1339 model()->Add(t_url); 1339 model()->Add(t_url);
1340 1340
1341 VerifyObserverCount(1); 1341 VerifyObserverCount(1);
1342 base::RunLoop().RunUntilIdle(); 1342 base::RunLoop().RunUntilIdle();
1343 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1343 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1344 1344
1345 // Reload the model to verify it was actually saved to the database and 1345 // Reload the model to verify it was actually saved to the database and
1346 // assigned a new GUID when brought back. 1346 // assigned a new GUID when brought back.
1347 test_util_.ResetModel(true); 1347 test_util_->ResetModel(true);
1348 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1348 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1349 const TemplateURL* loaded_url = 1349 const TemplateURL* loaded_url =
1350 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 1350 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1351 ASSERT_FALSE(loaded_url == NULL); 1351 ASSERT_FALSE(loaded_url == NULL);
1352 ASSERT_FALSE(loaded_url->sync_guid().empty()); 1352 ASSERT_FALSE(loaded_url->sync_guid().empty());
1353 } 1353 }
1354 1354
1355 // Test that if we load a TemplateURL with duplicate input encodings, the load 1355 // Test that if we load a TemplateURL with duplicate input encodings, the load
1356 // process de-dupes them. 1356 // process de-dupes them.
1357 TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { 1357 TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) {
1358 // Add a new TemplateURL. 1358 // Add a new TemplateURL.
1359 test_util_.VerifyLoad(); 1359 test_util_->VerifyLoad();
1360 const size_t initial_count = model()->GetTemplateURLs().size(); 1360 const size_t initial_count = model()->GetTemplateURLs().size();
1361 1361
1362 TemplateURLData data; 1362 TemplateURLData data;
1363 data.short_name = ASCIIToUTF16("google"); 1363 data.short_name = ASCIIToUTF16("google");
1364 data.SetKeyword(ASCIIToUTF16("keyword")); 1364 data.SetKeyword(ASCIIToUTF16("keyword"));
1365 data.SetURL("http://www.google.com/foo/bar"); 1365 data.SetURL("http://www.google.com/foo/bar");
1366 std::vector<std::string> encodings; 1366 std::vector<std::string> encodings;
1367 data.input_encodings.push_back("UTF-8"); 1367 data.input_encodings.push_back("UTF-8");
1368 data.input_encodings.push_back("UTF-8"); 1368 data.input_encodings.push_back("UTF-8");
1369 data.input_encodings.push_back("UTF-16"); 1369 data.input_encodings.push_back("UTF-16");
1370 data.input_encodings.push_back("UTF-8"); 1370 data.input_encodings.push_back("UTF-8");
1371 data.input_encodings.push_back("Big5"); 1371 data.input_encodings.push_back("Big5");
1372 data.input_encodings.push_back("UTF-16"); 1372 data.input_encodings.push_back("UTF-16");
1373 data.input_encodings.push_back("Big5"); 1373 data.input_encodings.push_back("Big5");
1374 data.input_encodings.push_back("Windows-1252"); 1374 data.input_encodings.push_back("Windows-1252");
1375 TemplateURL* t_url = new TemplateURL(data); 1375 TemplateURL* t_url = new TemplateURL(data);
1376 model()->Add(t_url); 1376 model()->Add(t_url);
1377 1377
1378 VerifyObserverCount(1); 1378 VerifyObserverCount(1);
1379 base::RunLoop().RunUntilIdle(); 1379 base::RunLoop().RunUntilIdle();
1380 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1380 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1381 const TemplateURL* loaded_url = 1381 const TemplateURL* loaded_url =
1382 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 1382 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1383 ASSERT_TRUE(loaded_url != NULL); 1383 ASSERT_TRUE(loaded_url != NULL);
1384 EXPECT_EQ(8U, loaded_url->input_encodings().size()); 1384 EXPECT_EQ(8U, loaded_url->input_encodings().size());
1385 1385
1386 // Reload the model to verify it was actually saved to the database and the 1386 // Reload the model to verify it was actually saved to the database and the
1387 // duplicate encodings were removed. 1387 // duplicate encodings were removed.
1388 test_util_.ResetModel(true); 1388 test_util_->ResetModel(true);
1389 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1389 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1390 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 1390 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1391 ASSERT_FALSE(loaded_url == NULL); 1391 ASSERT_FALSE(loaded_url == NULL);
1392 EXPECT_EQ(4U, loaded_url->input_encodings().size()); 1392 EXPECT_EQ(4U, loaded_url->input_encodings().size());
1393 } 1393 }
1394 1394
1395 TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) { 1395 TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) {
1396 test_util_.VerifyLoad(); 1396 test_util_->VerifyLoad();
1397 // Add third-party default search engine. 1397 // Add third-party default search engine.
1398 TemplateURL* user_dse = AddKeywordWithDate( 1398 TemplateURL* user_dse = AddKeywordWithDate(
1399 "user", "user", "http://www.goo.com/s?q={searchTerms}", 1399 "user", "user", "http://www.goo.com/s?q={searchTerms}",
1400 std::string(), std::string(), std::string(), 1400 std::string(), std::string(), std::string(),
1401 true, "UTF-8", Time(), Time()); 1401 true, "UTF-8", Time(), Time());
1402 model()->SetUserSelectedDefaultSearchProvider(user_dse); 1402 model()->SetUserSelectedDefaultSearchProvider(user_dse);
1403 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); 1403 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1404 1404
1405 TemplateURL* ext_dse = CreateKeywordWithDate( 1405 TemplateURL* ext_dse = CreateKeywordWithDate(
1406 model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}", 1406 model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}",
1407 std::string(), std::string(), std::string(), 1407 std::string(), std::string(), std::string(),
1408 true, true, "UTF-8", Time(), Time()); 1408 true, true, "UTF-8", Time(), Time());
1409 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( 1409 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1410 new TemplateURL::AssociatedExtensionInfo( 1410 new TemplateURL::AssociatedExtensionInfo(
1411 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext")); 1411 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext"));
1412 extension_info->wants_to_be_default_engine = true; 1412 extension_info->wants_to_be_default_engine = true;
1413 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); 1413 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1414 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); 1414 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
1415 1415
1416 model()->RemoveExtensionControlledTURL( 1416 model()->RemoveExtensionControlledTURL(
1417 "ext", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); 1417 "ext", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
1418 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); 1418 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
1419 } 1419 }
1420 1420
1421 TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) { 1421 TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) {
1422 test_util_.VerifyLoad(); 1422 test_util_->VerifyLoad();
1423 // Add third-party default search engine. 1423 // Add third-party default search engine.
1424 TemplateURL* user_dse = AddKeywordWithDate( 1424 TemplateURL* user_dse = AddKeywordWithDate(
1425 "user", "user", "http://www.goo.com/s?q={searchTerms}", 1425 "user", "user", "http://www.goo.com/s?q={searchTerms}",
1426 std::string(), std::string(), std::string(), 1426 std::string(), std::string(), std::string(),
1427 true, "UTF-8", Time(), Time()); 1427 true, "UTF-8", Time(), Time());
1428 model()->SetUserSelectedDefaultSearchProvider(user_dse); 1428 model()->SetUserSelectedDefaultSearchProvider(user_dse);
1429 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); 1429 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1430 1430
1431 TemplateURL* ext_dse = CreateKeywordWithDate( 1431 TemplateURL* ext_dse = CreateKeywordWithDate(
1432 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}", 1432 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}",
1433 std::string(), std::string(), std::string(), 1433 std::string(), std::string(), std::string(),
1434 true, false, "UTF-8", Time(), Time()); 1434 true, false, "UTF-8", Time(), Time());
1435 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( 1435 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1436 new TemplateURL::AssociatedExtensionInfo( 1436 new TemplateURL::AssociatedExtensionInfo(
1437 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); 1437 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1"));
1438 extension_info->wants_to_be_default_engine = false; 1438 extension_info->wants_to_be_default_engine = false;
1439 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); 1439 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1440 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); 1440 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1441 1441
1442 ext_dse = CreateKeywordWithDate( 1442 ext_dse = CreateKeywordWithDate(
1443 model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}", 1443 model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}",
1444 std::string(), std::string(), std::string(), 1444 std::string(), std::string(), std::string(),
1445 true, true, "UTF-8", Time(), Time()); 1445 true, true, "UTF-8", Time(), Time());
1446 extension_info.reset(new TemplateURL::AssociatedExtensionInfo( 1446 extension_info.reset(new TemplateURL::AssociatedExtensionInfo(
1447 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext2")); 1447 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext2"));
1448 extension_info->wants_to_be_default_engine = true; 1448 extension_info->wants_to_be_default_engine = true;
1449 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); 1449 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1450 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); 1450 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
1451 1451
1452 test_util_.ResetModel(true); 1452 test_util_->ResetModel(true);
1453 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); 1453 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user"));
1454 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); 1454 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
1455 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); 1455 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
1456 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); 1456 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2")));
1457 } 1457 }
1458 1458
1459 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { 1459 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) {
1460 // Set a managed preference that establishes a default search provider. 1460 // Set a managed preference that establishes a default search provider.
1461 const char kName[] = "test"; 1461 const char kName[] = "test";
1462 const char kKeyword[] = "test.com"; 1462 const char kKeyword[] = "test.com";
1463 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; 1463 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
1464 const char kIconURL[] = "http://test.com/icon.jpg"; 1464 const char kIconURL[] = "http://test.com/icon.jpg";
1465 const char kEncodings[] = "UTF-16;UTF-32"; 1465 const char kEncodings[] = "UTF-16;UTF-32";
1466 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; 1466 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
1467 const char kSearchTermsReplacementKey[] = "espv"; 1467 const char kSearchTermsReplacementKey[] = "espv";
1468 test_util_.SetManagedDefaultSearchPreferences( 1468 test_util_->SetManagedDefaultSearchPreferences(
1469 true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, 1469 true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings,
1470 kAlternateURL, kSearchTermsReplacementKey); 1470 kAlternateURL, kSearchTermsReplacementKey);
1471 test_util_.VerifyLoad(); 1471 test_util_->VerifyLoad();
1472 // Verify that the default manager we are getting is the managed one. 1472 // Verify that the default manager we are getting is the managed one.
1473 TemplateURLData data; 1473 TemplateURLData data;
1474 data.short_name = ASCIIToUTF16(kName); 1474 data.short_name = ASCIIToUTF16(kName);
1475 data.SetKeyword(ASCIIToUTF16(kKeyword)); 1475 data.SetKeyword(ASCIIToUTF16(kKeyword));
1476 data.SetURL(kSearchURL); 1476 data.SetURL(kSearchURL);
1477 data.favicon_url = GURL(kIconURL); 1477 data.favicon_url = GURL(kIconURL);
1478 data.show_in_default_list = true; 1478 data.show_in_default_list = true;
1479 base::SplitString(kEncodings, ';', &data.input_encodings); 1479 base::SplitString(kEncodings, ';', &data.input_encodings);
1480 data.alternate_urls.push_back(kAlternateURL); 1480 data.alternate_urls.push_back(kAlternateURL);
1481 data.search_terms_replacement_key = kSearchTermsReplacementKey; 1481 data.search_terms_replacement_key = kSearchTermsReplacementKey;
(...skipping 10 matching lines...) Expand all
1492 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( 1492 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1493 new TemplateURL::AssociatedExtensionInfo( 1493 new TemplateURL::AssociatedExtensionInfo(
1494 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); 1494 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1"));
1495 extension_info->wants_to_be_default_engine = true; 1495 extension_info->wants_to_be_default_engine = true;
1496 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); 1496 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1497 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); 1497 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
1498 EXPECT_TRUE(model()->is_default_search_managed()); 1498 EXPECT_TRUE(model()->is_default_search_managed());
1499 actual_managed_default = model()->GetDefaultSearchProvider(); 1499 actual_managed_default = model()->GetDefaultSearchProvider();
1500 ExpectSimilar(expected_managed_default.get(), actual_managed_default); 1500 ExpectSimilar(expected_managed_default.get(), actual_managed_default);
1501 } 1501 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698