OLD | NEW |
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" |
11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
15 #include "base/test/mock_time_provider.h" | 15 #include "base/test/mock_time_provider.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "chrome/browser/history/history_service.h" | 18 #include "chrome/browser/history/history_service.h" |
19 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
20 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 20 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
21 #include "chrome/browser/webdata/web_data_service_factory.h" | |
22 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
23 #include "components/search_engines/keyword_web_data_service.h" | 22 #include "components/search_engines/keyword_web_data_service.h" |
24 #include "components/search_engines/search_host_to_urls_map.h" | 23 #include "components/search_engines/search_host_to_urls_map.h" |
25 #include "components/search_engines/search_terms_data.h" | 24 #include "components/search_engines/search_terms_data.h" |
26 #include "components/search_engines/template_url.h" | 25 #include "components/search_engines/template_url.h" |
27 #include "components/search_engines/template_url_prepopulate_data.h" | 26 #include "components/search_engines/template_url_prepopulate_data.h" |
28 #include "components/search_engines/template_url_service.h" | 27 #include "components/search_engines/template_url_service.h" |
29 #include "components/webdata/common/web_database.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
31 | 30 |
32 using base::ASCIIToUTF16; | 31 using base::ASCIIToUTF16; |
33 using base::Time; | 32 using base::Time; |
34 using base::TimeDelta; | 33 using base::TimeDelta; |
35 using ::testing::Return; | 34 using ::testing::Return; |
36 using ::testing::StrictMock; | 35 using ::testing::StrictMock; |
37 | 36 |
38 namespace { | 37 namespace { |
39 | 38 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 152 |
154 // Create an URL that appears to have been prepopulated, but won't be in the | 153 // Create an URL that appears to have been prepopulated, but won't be in the |
155 // current data. The caller owns the returned TemplateURL*. | 154 // current data. The caller owns the returned TemplateURL*. |
156 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, | 155 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, |
157 int prepopulate_id); | 156 int prepopulate_id); |
158 | 157 |
159 // Helper methods to make calling TemplateURLServiceTestUtil methods less | 158 // Helper methods to make calling TemplateURLServiceTestUtil methods less |
160 // visually noisy in the test code. | 159 // visually noisy in the test code. |
161 void VerifyObserverCount(int expected_changed_count); | 160 void VerifyObserverCount(int expected_changed_count); |
162 void VerifyObserverFired(); | 161 void VerifyObserverFired(); |
| 162 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } |
163 TemplateURLService* model() { return test_util_->model(); } | 163 TemplateURLService* model() { return test_util_->model(); } |
164 const SearchTermsData& search_terms_data() { | 164 const SearchTermsData& search_terms_data() { |
165 return model()->search_terms_data(); | 165 return model()->search_terms_data(); |
166 } | 166 } |
167 | 167 |
168 protected: | 168 private: |
| 169 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads. |
169 scoped_ptr<TemplateURLServiceTestUtil> test_util_; | 170 scoped_ptr<TemplateURLServiceTestUtil> test_util_; |
170 | 171 |
171 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest); | 172 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest); |
172 }; | 173 }; |
173 | 174 |
174 class TemplateURLServiceWithoutFallbackTest : public TemplateURLServiceTest { | 175 class TemplateURLServiceWithoutFallbackTest : public TemplateURLServiceTest { |
175 public: | 176 public: |
176 TemplateURLServiceWithoutFallbackTest() : TemplateURLServiceTest() {} | 177 TemplateURLServiceWithoutFallbackTest() : TemplateURLServiceTest() {} |
177 | 178 |
178 virtual void SetUp() OVERRIDE { | 179 virtual void SetUp() OVERRIDE { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 257 |
257 void TemplateURLServiceTest::VerifyObserverFired() { | 258 void TemplateURLServiceTest::VerifyObserverFired() { |
258 EXPECT_LE(1, test_util_->GetObserverCount()); | 259 EXPECT_LE(1, test_util_->GetObserverCount()); |
259 test_util_->ResetObserverCount(); | 260 test_util_->ResetObserverCount(); |
260 } | 261 } |
261 | 262 |
262 | 263 |
263 // Actual tests --------------------------------------------------------------- | 264 // Actual tests --------------------------------------------------------------- |
264 | 265 |
265 TEST_F(TemplateURLServiceTest, Load) { | 266 TEST_F(TemplateURLServiceTest, Load) { |
266 test_util_->VerifyLoad(); | 267 test_util()->VerifyLoad(); |
267 } | 268 } |
268 | 269 |
269 TEST_F(TemplateURLServiceTest, AddUpdateRemove) { | 270 TEST_F(TemplateURLServiceTest, AddUpdateRemove) { |
270 // Add a new TemplateURL. | 271 // Add a new TemplateURL. |
271 test_util_->VerifyLoad(); | 272 test_util()->VerifyLoad(); |
272 const size_t initial_count = model()->GetTemplateURLs().size(); | 273 const size_t initial_count = model()->GetTemplateURLs().size(); |
273 | 274 |
274 TemplateURLData data; | 275 TemplateURLData data; |
275 data.short_name = ASCIIToUTF16("google"); | 276 data.short_name = ASCIIToUTF16("google"); |
276 data.SetKeyword(ASCIIToUTF16("keyword")); | 277 data.SetKeyword(ASCIIToUTF16("keyword")); |
277 data.SetURL("http://www.google.com/foo/bar"); | 278 data.SetURL("http://www.google.com/foo/bar"); |
278 data.favicon_url = GURL("http://favicon.url"); | 279 data.favicon_url = GURL("http://favicon.url"); |
279 data.safe_for_autoreplace = true; | 280 data.safe_for_autoreplace = true; |
280 data.date_created = Time::FromTimeT(100); | 281 data.date_created = Time::FromTimeT(100); |
281 data.last_modified = Time::FromTimeT(100); | 282 data.last_modified = Time::FromTimeT(100); |
282 data.sync_guid = "00000000-0000-0000-0000-000000000001"; | 283 data.sync_guid = "00000000-0000-0000-0000-000000000001"; |
283 TemplateURL* t_url = new TemplateURL(data); | 284 TemplateURL* t_url = new TemplateURL(data); |
284 model()->Add(t_url); | 285 model()->Add(t_url); |
285 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), | 286 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), |
286 NULL)); | 287 NULL)); |
287 VerifyObserverCount(1); | 288 VerifyObserverCount(1); |
288 base::RunLoop().RunUntilIdle(); | 289 base::RunLoop().RunUntilIdle(); |
289 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 290 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
290 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword())); | 291 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword())); |
291 // We need to make a second copy as the model takes ownership of |t_url| and | 292 // We need to make a second copy as the model takes ownership of |t_url| and |
292 // will delete it. We have to do this after calling Add() since that gives | 293 // will delete it. We have to do this after calling Add() since that gives |
293 // |t_url| its ID. | 294 // |t_url| its ID. |
294 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 295 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
295 | 296 |
296 // Reload the model to verify it was actually saved to the database. | 297 // Reload the model to verify it was actually saved to the database. |
297 test_util_->ResetModel(true); | 298 test_util()->ResetModel(true); |
298 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 299 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
299 TemplateURL* loaded_url = | 300 TemplateURL* loaded_url = |
300 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 301 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
301 ASSERT_TRUE(loaded_url != NULL); | 302 ASSERT_TRUE(loaded_url != NULL); |
302 AssertEquals(*cloned_url, *loaded_url); | 303 AssertEquals(*cloned_url, *loaded_url); |
303 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), | 304 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), |
304 NULL)); | 305 NULL)); |
305 | 306 |
306 // We expect the last_modified time to be updated to the present time on an | 307 // We expect the last_modified time to be updated to the present time on an |
307 // explicit reset. We have to set up the expectation here because ResetModel | 308 // explicit reset. We have to set up the expectation here because ResetModel |
308 // resets the TimeProvider in the TemplateURLService. | 309 // resets the TimeProvider in the TemplateURLService. |
309 StrictMock<base::MockTimeProvider> mock_time; | 310 StrictMock<base::MockTimeProvider> mock_time; |
310 model()->set_time_provider(&base::MockTimeProvider::StaticNow); | 311 model()->set_time_provider(&base::MockTimeProvider::StaticNow); |
311 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); | 312 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); |
312 | 313 |
313 // Mutate an element and verify it succeeded. | 314 // Mutate an element and verify it succeeded. |
314 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), | 315 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), |
315 "c"); | 316 "c"); |
316 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); | 317 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); |
317 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); | 318 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); |
318 ASSERT_EQ("c", loaded_url->url()); | 319 ASSERT_EQ("c", loaded_url->url()); |
319 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); | 320 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); |
320 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), | 321 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), |
321 NULL)); | 322 NULL)); |
322 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); | 323 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); |
323 cloned_url.reset(new TemplateURL(loaded_url->data())); | 324 cloned_url.reset(new TemplateURL(loaded_url->data())); |
324 base::RunLoop().RunUntilIdle(); | 325 base::RunLoop().RunUntilIdle(); |
325 test_util_->ResetModel(true); | 326 test_util()->ResetModel(true); |
326 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 327 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
327 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); | 328 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); |
328 ASSERT_TRUE(loaded_url != NULL); | 329 ASSERT_TRUE(loaded_url != NULL); |
329 AssertEquals(*cloned_url, *loaded_url); | 330 AssertEquals(*cloned_url, *loaded_url); |
330 // We changed a TemplateURL in the service, so ensure that the time was | 331 // We changed a TemplateURL in the service, so ensure that the time was |
331 // updated. | 332 // updated. |
332 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); | 333 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); |
333 | 334 |
334 // Remove an element and verify it succeeded. | 335 // Remove an element and verify it succeeded. |
335 model()->Remove(loaded_url); | 336 model()->Remove(loaded_url); |
336 VerifyObserverCount(1); | 337 VerifyObserverCount(1); |
337 test_util_->ResetModel(true); | 338 test_util()->ResetModel(true); |
338 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); | 339 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); |
339 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); | 340 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); |
340 } | 341 } |
341 | 342 |
342 TEST_F(TemplateURLServiceTest, AddSameKeyword) { | 343 TEST_F(TemplateURLServiceTest, AddSameKeyword) { |
343 test_util_->VerifyLoad(); | 344 test_util()->VerifyLoad(); |
344 | 345 |
345 AddKeywordWithDate( | 346 AddKeywordWithDate( |
346 "first", "keyword", "http://test1", std::string(), std::string(), | 347 "first", "keyword", "http://test1", std::string(), std::string(), |
347 std::string(), true, "UTF-8", Time(), Time()); | 348 std::string(), true, "UTF-8", Time(), Time()); |
348 VerifyObserverCount(1); | 349 VerifyObserverCount(1); |
349 | 350 |
350 // Test what happens when we try to add a TemplateURL with the same keyword as | 351 // Test what happens when we try to add a TemplateURL with the same keyword as |
351 // one in the model. | 352 // one in the model. |
352 TemplateURLData data; | 353 TemplateURLData data; |
353 data.short_name = ASCIIToUTF16("second"); | 354 data.short_name = ASCIIToUTF16("second"); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 model()->Add(t_url2); | 387 model()->Add(t_url2); |
387 VerifyObserverCount(1); | 388 VerifyObserverCount(1); |
388 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 389 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
389 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); | 390 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); |
390 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); | 391 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); |
391 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | 392 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); |
392 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); | 393 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); |
393 } | 394 } |
394 | 395 |
395 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { | 396 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { |
396 test_util_->VerifyLoad(); | 397 test_util()->VerifyLoad(); |
397 | 398 |
398 TemplateURL* original1 = AddKeywordWithDate( | 399 TemplateURL* original1 = AddKeywordWithDate( |
399 "replaceable", "keyword1", "http://test1", std::string(), std::string(), | 400 "replaceable", "keyword1", "http://test1", std::string(), std::string(), |
400 std::string(), true, "UTF-8", Time(), Time()); | 401 std::string(), true, "UTF-8", Time(), Time()); |
401 TemplateURL* original2 = AddKeywordWithDate( | 402 TemplateURL* original2 = AddKeywordWithDate( |
402 "nonreplaceable", "keyword2", "http://test2", std::string(), | 403 "nonreplaceable", "keyword2", "http://test2", std::string(), |
403 std::string(), std::string(), false, "UTF-8", Time(), Time()); | 404 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
404 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", | 405 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", |
405 "http://test3"); | 406 "http://test3"); |
406 TemplateURL* original3 = | 407 TemplateURL* original3 = |
(...skipping 20 matching lines...) Expand all Loading... |
427 // They should override extension keywords added earlier. | 428 // They should override extension keywords added earlier. |
428 model()->RegisterOmniboxKeyword("test6", "test", "keyword3", "http://test6"); | 429 model()->RegisterOmniboxKeyword("test6", "test", "keyword3", "http://test6"); |
429 TemplateURL* extension3 = | 430 TemplateURL* extension3 = |
430 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); | 431 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); |
431 ASSERT_TRUE(extension3); | 432 ASSERT_TRUE(extension3); |
432 EXPECT_EQ(original3, | 433 EXPECT_EQ(original3, |
433 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_"))); | 434 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_"))); |
434 } | 435 } |
435 | 436 |
436 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { | 437 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { |
437 test_util_->VerifyLoad(); | 438 test_util()->VerifyLoad(); |
438 | 439 |
439 // Similar to the AddSameKeyword test, but with an extension keyword masking a | 440 // Similar to the AddSameKeyword test, but with an extension keyword masking a |
440 // replaceable TemplateURL. We should still do correct conflict resolution | 441 // replaceable TemplateURL. We should still do correct conflict resolution |
441 // between the non-template URLs. | 442 // between the non-template URLs. |
442 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", | 443 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", |
443 "http://test2"); | 444 "http://test2"); |
444 TemplateURL* extension = | 445 TemplateURL* extension = |
445 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 446 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
446 ASSERT_TRUE(extension); | 447 ASSERT_TRUE(extension); |
447 // Adding a keyword that matches the extension should cause the extension | 448 // Adding a keyword that matches the extension should cause the extension |
(...skipping 24 matching lines...) Expand all Loading... |
472 TemplateURL* t_url2 = new TemplateURL(data); | 473 TemplateURL* t_url2 = new TemplateURL(data); |
473 model()->Add(t_url2); | 474 model()->Add(t_url2); |
474 EXPECT_EQ(t_url2, | 475 EXPECT_EQ(t_url2, |
475 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 476 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
476 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); | 477 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); |
477 EXPECT_EQ(extension, | 478 EXPECT_EQ(extension, |
478 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); | 479 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); |
479 } | 480 } |
480 | 481 |
481 TEST_F(TemplateURLServiceTest, RestoreOmniboxExtensionKeyword) { | 482 TEST_F(TemplateURLServiceTest, RestoreOmniboxExtensionKeyword) { |
482 test_util_->VerifyLoad(); | 483 test_util()->VerifyLoad(); |
483 | 484 |
484 // Register an omnibox keyword. | 485 // Register an omnibox keyword. |
485 model()->RegisterOmniboxKeyword("test", "extension", "keyword", | 486 model()->RegisterOmniboxKeyword("test", "extension", "keyword", |
486 "chrome-extension://test"); | 487 "chrome-extension://test"); |
487 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 488 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
488 | 489 |
489 // Reload the data. | 490 // Reload the data. |
490 test_util_->ResetModel(true); | 491 test_util()->ResetModel(true); |
491 | 492 |
492 // Ensure the omnibox keyword is restored correctly. | 493 // Ensure the omnibox keyword is restored correctly. |
493 TemplateURL* t_url = | 494 TemplateURL* t_url = |
494 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 495 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
495 ASSERT_TRUE(t_url); | 496 ASSERT_TRUE(t_url); |
496 ASSERT_EQ(TemplateURL::OMNIBOX_API_EXTENSION, t_url->GetType()); | 497 ASSERT_EQ(TemplateURL::OMNIBOX_API_EXTENSION, t_url->GetType()); |
497 EXPECT_EQ("test", t_url->GetExtensionId()); | 498 EXPECT_EQ("test", t_url->GetExtensionId()); |
498 } | 499 } |
499 | 500 |
500 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { | 501 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 // Try removing foo3. This should delete foo3, but leave foo1 untouched. | 607 // Try removing foo3. This should delete foo3, but leave foo1 untouched. |
607 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago, | 608 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago, |
608 now + one_day + one_day); | 609 now + one_day + one_day); |
609 EXPECT_EQ(1U, model()->GetTemplateURLs().size()); | 610 EXPECT_EQ(1U, model()->GetTemplateURLs().size()); |
610 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); | 611 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); |
611 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace()); | 612 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace()); |
612 } | 613 } |
613 | 614 |
614 TEST_F(TemplateURLServiceTest, Reset) { | 615 TEST_F(TemplateURLServiceTest, Reset) { |
615 // Add a new TemplateURL. | 616 // Add a new TemplateURL. |
616 test_util_->VerifyLoad(); | 617 test_util()->VerifyLoad(); |
617 const size_t initial_count = model()->GetTemplateURLs().size(); | 618 const size_t initial_count = model()->GetTemplateURLs().size(); |
618 TemplateURLData data; | 619 TemplateURLData data; |
619 data.short_name = ASCIIToUTF16("google"); | 620 data.short_name = ASCIIToUTF16("google"); |
620 data.SetKeyword(ASCIIToUTF16("keyword")); | 621 data.SetKeyword(ASCIIToUTF16("keyword")); |
621 data.SetURL("http://www.google.com/foo/bar"); | 622 data.SetURL("http://www.google.com/foo/bar"); |
622 data.favicon_url = GURL("http://favicon.url"); | 623 data.favicon_url = GURL("http://favicon.url"); |
623 data.date_created = Time::FromTimeT(100); | 624 data.date_created = Time::FromTimeT(100); |
624 data.last_modified = Time::FromTimeT(100); | 625 data.last_modified = Time::FromTimeT(100); |
625 TemplateURL* t_url = new TemplateURL(data); | 626 TemplateURL* t_url = new TemplateURL(data); |
626 model()->Add(t_url); | 627 model()->Add(t_url); |
(...skipping 15 matching lines...) Expand all Loading... |
642 ASSERT_EQ(new_url, t_url->url()); | 643 ASSERT_EQ(new_url, t_url->url()); |
643 | 644 |
644 // Make sure the mappings in the model were updated. | 645 // Make sure the mappings in the model were updated. |
645 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); | 646 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); |
646 ASSERT_TRUE( | 647 ASSERT_TRUE( |
647 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); | 648 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); |
648 | 649 |
649 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 650 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
650 | 651 |
651 // Reload the model from the database and make sure the change took. | 652 // Reload the model from the database and make sure the change took. |
652 test_util_->ResetModel(true); | 653 test_util()->ResetModel(true); |
653 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 654 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
654 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); | 655 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); |
655 ASSERT_TRUE(read_url); | 656 ASSERT_TRUE(read_url); |
656 AssertEquals(*cloned_url, *read_url); | 657 AssertEquals(*cloned_url, *read_url); |
657 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); | 658 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); |
658 } | 659 } |
659 | 660 |
660 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { | 661 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { |
661 // Add a new TemplateURL. | 662 // Add a new TemplateURL. |
662 test_util_->VerifyLoad(); | 663 test_util()->VerifyLoad(); |
663 const size_t initial_count = model()->GetTemplateURLs().size(); | 664 const size_t initial_count = model()->GetTemplateURLs().size(); |
664 TemplateURL* t_url = AddKeywordWithDate( | 665 TemplateURL* t_url = AddKeywordWithDate( |
665 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", | 666 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", |
666 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 667 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
667 test_util_->ResetObserverCount(); | 668 test_util()->ResetObserverCount(); |
668 | 669 |
669 model()->SetUserSelectedDefaultSearchProvider(t_url); | 670 model()->SetUserSelectedDefaultSearchProvider(t_url); |
670 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); | 671 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); |
671 ASSERT_TRUE(t_url->safe_for_autoreplace()); | 672 ASSERT_TRUE(t_url->safe_for_autoreplace()); |
672 ASSERT_TRUE(t_url->show_in_default_list()); | 673 ASSERT_TRUE(t_url->show_in_default_list()); |
673 | 674 |
674 // Setting the default search provider should have caused notification. | 675 // Setting the default search provider should have caused notification. |
675 VerifyObserverCount(1); | 676 VerifyObserverCount(1); |
676 base::RunLoop().RunUntilIdle(); | 677 base::RunLoop().RunUntilIdle(); |
677 | 678 |
678 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 679 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
679 | 680 |
680 // Make sure when we reload we get a default search provider. | 681 // Make sure when we reload we get a default search provider. |
681 test_util_->ResetModel(true); | 682 test_util()->ResetModel(true); |
682 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 683 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
683 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 684 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
684 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); | 685 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); |
685 } | 686 } |
686 | 687 |
687 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { | 688 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { |
688 test_util_->ChangeModelToLoadState(); | 689 test_util()->ChangeModelToLoadState(); |
689 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); | 690 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); |
690 TemplateURL* t_url = AddKeywordWithDate( | 691 TemplateURL* t_url = AddKeywordWithDate( |
691 "name1", "foo", "http://foo1", "http://sugg1", std::string(), | 692 "name1", "foo", "http://foo1", "http://sugg1", std::string(), |
692 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 693 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
693 | 694 |
694 // Can still replace, newly added template url is marked safe to replace. | 695 // Can still replace, newly added template url is marked safe to replace. |
695 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 696 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
696 GURL("http://foo2"), NULL)); | 697 GURL("http://foo2"), NULL)); |
697 | 698 |
698 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should | 699 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should |
699 // no longer be replaceable. | 700 // no longer be replaceable. |
700 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), | 701 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), |
701 t_url->url()); | 702 t_url->url()); |
702 | 703 |
703 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 704 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
704 GURL("http://foo2"), NULL)); | 705 GURL("http://foo2"), NULL)); |
705 } | 706 } |
706 | 707 |
707 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { | 708 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { |
708 test_util_->ChangeModelToLoadState(); | 709 test_util()->ChangeModelToLoadState(); |
709 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 710 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
710 GURL("http://foo.com"), NULL)); | 711 GURL("http://foo.com"), NULL)); |
711 TemplateURL* t_url = AddKeywordWithDate( | 712 TemplateURL* t_url = AddKeywordWithDate( |
712 "name1", "foo", "http://foo.com", "http://sugg1", std::string(), | 713 "name1", "foo", "http://foo.com", "http://sugg1", std::string(), |
713 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 714 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
714 | 715 |
715 // Can still replace, newly added template url is marked safe to replace. | 716 // Can still replace, newly added template url is marked safe to replace. |
716 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), | 717 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), |
717 GURL("http://foo.com"), NULL)); | 718 GURL("http://foo.com"), NULL)); |
718 | 719 |
719 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should | 720 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should |
720 // no longer be replaceable. | 721 // no longer be replaceable. |
721 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), | 722 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), |
722 t_url->url()); | 723 t_url->url()); |
723 | 724 |
724 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), | 725 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), |
725 GURL("http://foo.com"), NULL)); | 726 GURL("http://foo.com"), NULL)); |
726 } | 727 } |
727 | 728 |
728 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) { | 729 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) { |
729 // We should have a default search provider even if we haven't loaded. | 730 // We should have a default search provider even if we haven't loaded. |
730 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 731 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
731 | 732 |
732 // Now force the model to load and make sure we still have a default. | 733 // Now force the model to load and make sure we still have a default. |
733 test_util_->VerifyLoad(); | 734 test_util()->VerifyLoad(); |
734 | 735 |
735 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 736 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
736 } | 737 } |
737 | 738 |
738 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { | 739 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { |
739 test_util_->VerifyLoad(); | 740 test_util()->VerifyLoad(); |
740 | 741 |
741 TemplateURLData data; | 742 TemplateURLData data; |
742 data.short_name = ASCIIToUTF16("a"); | 743 data.short_name = ASCIIToUTF16("a"); |
743 data.safe_for_autoreplace = true; | 744 data.safe_for_autoreplace = true; |
744 data.SetURL("http://url/{searchTerms}"); | 745 data.SetURL("http://url/{searchTerms}"); |
745 data.suggestions_url = "http://url2"; | 746 data.suggestions_url = "http://url2"; |
746 data.instant_url = "http://instant"; | 747 data.instant_url = "http://instant"; |
747 data.date_created = Time::FromTimeT(100); | 748 data.date_created = Time::FromTimeT(100); |
748 data.last_modified = Time::FromTimeT(100); | 749 data.last_modified = Time::FromTimeT(100); |
749 TemplateURL* t_url = new TemplateURL(data); | 750 TemplateURL* t_url = new TemplateURL(data); |
750 model()->Add(t_url); | 751 model()->Add(t_url); |
751 const TemplateURLID id = t_url->id(); | 752 const TemplateURLID id = t_url->id(); |
752 | 753 |
753 model()->SetUserSelectedDefaultSearchProvider(t_url); | 754 model()->SetUserSelectedDefaultSearchProvider(t_url); |
754 base::RunLoop().RunUntilIdle(); | 755 base::RunLoop().RunUntilIdle(); |
755 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 756 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
756 | 757 |
757 // Reset the model and don't load it. The template url we set as the default | 758 // Reset the model and don't load it. The template url we set as the default |
758 // should be pulled from prefs now. | 759 // should be pulled from prefs now. |
759 test_util_->ResetModel(false); | 760 test_util()->ResetModel(false); |
760 | 761 |
761 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs | 762 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs |
762 // value are persisted to prefs. | 763 // value are persisted to prefs. |
763 const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); | 764 const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); |
764 ASSERT_TRUE(default_turl); | 765 ASSERT_TRUE(default_turl); |
765 EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name()); | 766 EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name()); |
766 EXPECT_EQ("http://url/{searchTerms}", default_turl->url()); | 767 EXPECT_EQ("http://url/{searchTerms}", default_turl->url()); |
767 EXPECT_EQ("http://url2", default_turl->suggestions_url()); | 768 EXPECT_EQ("http://url2", default_turl->suggestions_url()); |
768 EXPECT_EQ("http://instant", default_turl->instant_url()); | 769 EXPECT_EQ("http://instant", default_turl->instant_url()); |
769 EXPECT_EQ(id, default_turl->id()); | 770 EXPECT_EQ(id, default_turl->id()); |
770 | 771 |
771 // Now do a load and make sure the default search provider really takes. | 772 // Now do a load and make sure the default search provider really takes. |
772 test_util_->VerifyLoad(); | 773 test_util()->VerifyLoad(); |
773 | 774 |
774 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 775 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
775 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); | 776 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); |
776 } | 777 } |
777 | 778 |
778 TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) { | 779 TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) { |
779 test_util_->VerifyLoad(); | 780 test_util()->VerifyLoad(); |
780 | 781 |
781 // Edit Google search engine. | 782 // Edit Google search engine. |
782 TemplateURL* google = model()->GetTemplateURLForKeyword( | 783 TemplateURL* google = model()->GetTemplateURLForKeyword( |
783 ASCIIToUTF16("google.com")); | 784 ASCIIToUTF16("google.com")); |
784 ASSERT_TRUE(google); | 785 ASSERT_TRUE(google); |
785 model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"), | 786 model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"), |
786 "http://www.foo.com/s?q={searchTerms}"); | 787 "http://www.foo.com/s?q={searchTerms}"); |
787 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name()); | 788 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name()); |
788 EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword()); | 789 EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword()); |
789 | 790 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 | 829 |
829 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { | 830 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { |
830 // Set a managed preference that establishes a default search provider. | 831 // Set a managed preference that establishes a default search provider. |
831 const char kName[] = "test1"; | 832 const char kName[] = "test1"; |
832 const char kKeyword[] = "test.com"; | 833 const char kKeyword[] = "test.com"; |
833 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | 834 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
834 const char kIconURL[] = "http://test.com/icon.jpg"; | 835 const char kIconURL[] = "http://test.com/icon.jpg"; |
835 const char kEncodings[] = "UTF-16;UTF-32"; | 836 const char kEncodings[] = "UTF-16;UTF-32"; |
836 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; | 837 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; |
837 const char kSearchTermsReplacementKey[] = "espv"; | 838 const char kSearchTermsReplacementKey[] = "espv"; |
838 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, | 839 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
839 kSearchURL, std::string(), | 840 kSearchURL, std::string(), |
840 kIconURL, kEncodings, | 841 kIconURL, kEncodings, |
841 kAlternateURL, | 842 kAlternateURL, |
842 kSearchTermsReplacementKey); | 843 kSearchTermsReplacementKey); |
843 test_util_->VerifyLoad(); | 844 test_util()->VerifyLoad(); |
844 // Verify that the default manager we are getting is the managed one. | 845 // Verify that the default manager we are getting is the managed one. |
845 TemplateURLData data; | 846 TemplateURLData data; |
846 data.short_name = ASCIIToUTF16(kName); | 847 data.short_name = ASCIIToUTF16(kName); |
847 data.SetKeyword(ASCIIToUTF16(kKeyword)); | 848 data.SetKeyword(ASCIIToUTF16(kKeyword)); |
848 data.SetURL(kSearchURL); | 849 data.SetURL(kSearchURL); |
849 data.favicon_url = GURL(kIconURL); | 850 data.favicon_url = GURL(kIconURL); |
850 data.show_in_default_list = true; | 851 data.show_in_default_list = true; |
851 base::SplitString(kEncodings, ';', &data.input_encodings); | 852 base::SplitString(kEncodings, ';', &data.input_encodings); |
852 data.alternate_urls.push_back(kAlternateURL); | 853 data.alternate_urls.push_back(kAlternateURL); |
853 data.search_terms_replacement_key = kSearchTermsReplacementKey; | 854 data.search_terms_replacement_key = kSearchTermsReplacementKey; |
(...skipping 22 matching lines...) Expand all Loading... |
876 { "http://x/foo?y=xx", base::string16() }, | 877 { "http://x/foo?y=xx", base::string16() }, |
877 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 878 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
878 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, | 879 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, |
879 { "http://x/foo?q=b&q=xx", base::string16() }, | 880 { "http://x/foo?q=b&q=xx", base::string16() }, |
880 { "http://x/foo#query=xx", ASCIIToUTF16("xx") }, | 881 { "http://x/foo#query=xx", ASCIIToUTF16("xx") }, |
881 { "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") }, | 882 { "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") }, |
882 { "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") }, | 883 { "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") }, |
883 { "http://x/foo?query=b#q=xx", base::string16() }, | 884 { "http://x/foo?query=b#q=xx", base::string16() }, |
884 }; | 885 }; |
885 | 886 |
886 test_util_->ChangeModelToLoadState(); | 887 test_util()->ChangeModelToLoadState(); |
887 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}", | 888 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}", |
888 "http://sugg1", "http://x/foo#query={searchTerms}", | 889 "http://sugg1", "http://x/foo#query={searchTerms}", |
889 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); | 890 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); |
890 | 891 |
891 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 892 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
892 TemplateURLService::URLVisitedDetails details = { | 893 TemplateURLService::URLVisitedDetails details = { |
893 GURL(data[i].url), false | 894 GURL(data[i].url), false |
894 }; | 895 }; |
895 model()->UpdateKeywordSearchTermsForURL(details); | 896 model()->UpdateKeywordSearchTermsForURL(details); |
896 EXPECT_EQ(data[i].term, test_util_->GetAndClearSearchTerm()); | 897 EXPECT_EQ(data[i].term, test_util()->GetAndClearSearchTerm()); |
897 } | 898 } |
898 } | 899 } |
899 | 900 |
900 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { | 901 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { |
901 struct TestData { | 902 struct TestData { |
902 const std::string url; | 903 const std::string url; |
903 } data[] = { | 904 } data[] = { |
904 { "http://foo/" }, | 905 { "http://foo/" }, |
905 { "http://x/bar?q=xx" }, | 906 { "http://x/bar?q=xx" }, |
906 { "http://x/foo?y=xx" }, | 907 { "http://x/foo?y=xx" }, |
907 }; | 908 }; |
908 | 909 |
909 test_util_->ChangeModelToLoadState(); | 910 test_util()->ChangeModelToLoadState(); |
910 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(), | 911 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(), |
911 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); | 912 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); |
912 | 913 |
913 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 914 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
914 TemplateURLService::URLVisitedDetails details = { | 915 TemplateURLService::URLVisitedDetails details = { |
915 GURL(data[i].url), false | 916 GURL(data[i].url), false |
916 }; | 917 }; |
917 model()->UpdateKeywordSearchTermsForURL(details); | 918 model()->UpdateKeywordSearchTermsForURL(details); |
918 ASSERT_EQ(base::string16(), test_util_->GetAndClearSearchTerm()); | 919 ASSERT_EQ(base::string16(), test_util()->GetAndClearSearchTerm()); |
919 } | 920 } |
920 } | 921 } |
921 | 922 |
922 TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { | 923 TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { |
923 // NOTE: Do not load the prepopulate data, which also has a {google:baseURL} | 924 // NOTE: Do not load the prepopulate data, which also has a {google:baseURL} |
924 // keyword in it and would confuse this test. | 925 // keyword in it and would confuse this test. |
925 test_util_->ChangeModelToLoadState(); | 926 test_util()->ChangeModelToLoadState(); |
926 | 927 |
927 test_util_->SetGoogleBaseURL(GURL("http://google.com/")); | 928 test_util()->SetGoogleBaseURL(GURL("http://google.com/")); |
928 const TemplateURL* t_url = AddKeywordWithDate( | 929 const TemplateURL* t_url = AddKeywordWithDate( |
929 "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1", | 930 "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1", |
930 std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); | 931 std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); |
931 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); | 932 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); |
932 EXPECT_EQ("google.com", t_url->url_ref().GetHost(search_terms_data())); | 933 EXPECT_EQ("google.com", t_url->url_ref().GetHost(search_terms_data())); |
933 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); | 934 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); |
934 | 935 |
935 // Change the Google base url. | 936 // Change the Google base url. |
936 test_util_->ResetObserverCount(); | 937 test_util()->ResetObserverCount(); |
937 test_util_->SetGoogleBaseURL(GURL("http://google.co.uk/")); | 938 test_util()->SetGoogleBaseURL(GURL("http://google.co.uk/")); |
938 VerifyObserverCount(1); | 939 VerifyObserverCount(1); |
939 | 940 |
940 // Make sure the host->TemplateURL map was updated appropriately. | 941 // Make sure the host->TemplateURL map was updated appropriately. |
941 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); | 942 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); |
942 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); | 943 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); |
943 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost(search_terms_data())); | 944 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost(search_terms_data())); |
944 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); | 945 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); |
945 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( | 946 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( |
946 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data())); | 947 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data())); |
947 | 948 |
948 // Now add a manual entry and then change the Google base URL such that the | 949 // Now add a manual entry and then change the Google base URL such that the |
949 // autogenerated Google search keyword would conflict. | 950 // autogenerated Google search keyword would conflict. |
950 TemplateURL* manual = AddKeywordWithDate( | 951 TemplateURL* manual = AddKeywordWithDate( |
951 "manual", "google.de", "http://google.de/search?q={searchTerms}", | 952 "manual", "google.de", "http://google.de/search?q={searchTerms}", |
952 std::string(), std::string(), std::string(), false, "UTF-8", Time(), | 953 std::string(), std::string(), std::string(), false, "UTF-8", Time(), |
953 Time()); | 954 Time()); |
954 test_util_->SetGoogleBaseURL(GURL("http://google.de")); | 955 test_util()->SetGoogleBaseURL(GURL("http://google.de")); |
955 | 956 |
956 // Verify that the manual entry is untouched, and the autogenerated keyword | 957 // Verify that the manual entry is untouched, and the autogenerated keyword |
957 // has not changed. | 958 // has not changed. |
958 ASSERT_EQ(manual, | 959 ASSERT_EQ(manual, |
959 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de"))); | 960 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de"))); |
960 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); | 961 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); |
961 ASSERT_EQ(t_url, | 962 ASSERT_EQ(t_url, |
962 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk"))); | 963 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk"))); |
963 EXPECT_EQ("google.de", t_url->url_ref().GetHost(search_terms_data())); | 964 EXPECT_EQ("google.de", t_url->url_ref().GetHost(search_terms_data())); |
964 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); | 965 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); |
965 | 966 |
966 // Change the base URL again and verify that the autogenerated keyword follows | 967 // Change the base URL again and verify that the autogenerated keyword follows |
967 // even though it didn't match the base URL, while the manual entry is still | 968 // even though it didn't match the base URL, while the manual entry is still |
968 // untouched. | 969 // untouched. |
969 test_util_->SetGoogleBaseURL(GURL("http://google.fr/")); | 970 test_util()->SetGoogleBaseURL(GURL("http://google.fr/")); |
970 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de")); | 971 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de")); |
971 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); | 972 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); |
972 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword()); | 973 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword()); |
973 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr")); | 974 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr")); |
974 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL); | 975 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL); |
975 EXPECT_EQ("google.fr", t_url->url_ref().GetHost(search_terms_data())); | 976 EXPECT_EQ("google.fr", t_url->url_ref().GetHost(search_terms_data())); |
976 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword()); | 977 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword()); |
977 } | 978 } |
978 | 979 |
979 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for | 980 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for |
980 // KEYWORD visits. | 981 // KEYWORD visits. |
981 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { | 982 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { |
982 test_util_->VerifyLoad(); | 983 test_util()->VerifyLoad(); |
983 ASSERT_TRUE(test_util_->profile()->CreateHistoryService(true, false)); | 984 ASSERT_TRUE(test_util()->profile()->CreateHistoryService(true, false)); |
984 | 985 |
985 // Create a keyword. | 986 // Create a keyword. |
986 TemplateURL* t_url = AddKeywordWithDate( | 987 TemplateURL* t_url = AddKeywordWithDate( |
987 "keyword", "keyword", "http://foo.com/foo?query={searchTerms}", | 988 "keyword", "keyword", "http://foo.com/foo?query={searchTerms}", |
988 "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16", | 989 "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16", |
989 base::Time::Now(), base::Time::Now()); | 990 base::Time::Now(), base::Time::Now()); |
990 | 991 |
991 // Add a visit that matches the url of the keyword. | 992 // Add a visit that matches the url of the keyword. |
992 HistoryService* history = | 993 HistoryService* history = |
993 HistoryServiceFactory::GetForProfile(test_util_->profile(), | 994 HistoryServiceFactory::GetForProfile(test_util()->profile(), |
994 Profile::EXPLICIT_ACCESS); | 995 Profile::EXPLICIT_ACCESS); |
995 history->AddPage( | 996 history->AddPage( |
996 GURL(t_url->url_ref().ReplaceSearchTerms( | 997 GURL(t_url->url_ref().ReplaceSearchTerms( |
997 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")), | 998 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")), |
998 search_terms_data())), | 999 search_terms_data())), |
999 base::Time::Now(), NULL, 0, GURL(), history::RedirectList(), | 1000 base::Time::Now(), NULL, 0, GURL(), history::RedirectList(), |
1000 content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false); | 1001 content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false); |
1001 | 1002 |
1002 // Wait for history to finish processing the request. | 1003 // Wait for history to finish processing the request. |
1003 test_util_->profile()->BlockUntilHistoryProcessesPendingRequests(); | 1004 test_util()->profile()->BlockUntilHistoryProcessesPendingRequests(); |
1004 | 1005 |
1005 // Query history for the generated url. | 1006 // Query history for the generated url. |
1006 base::CancelableTaskTracker tracker; | 1007 base::CancelableTaskTracker tracker; |
1007 QueryHistoryCallbackImpl callback; | 1008 QueryHistoryCallbackImpl callback; |
1008 history->QueryURL(GURL("http://keyword"), | 1009 history->QueryURL(GURL("http://keyword"), |
1009 true, | 1010 true, |
1010 base::Bind(&QueryHistoryCallbackImpl::Callback, | 1011 base::Bind(&QueryHistoryCallbackImpl::Callback, |
1011 base::Unretained(&callback)), | 1012 base::Unretained(&callback)), |
1012 &tracker); | 1013 &tracker); |
1013 | 1014 |
1014 // Wait for the request to be processed. | 1015 // Wait for the request to be processed. |
1015 test_util_->profile()->BlockUntilHistoryProcessesPendingRequests(); | 1016 test_util()->profile()->BlockUntilHistoryProcessesPendingRequests(); |
1016 | 1017 |
1017 // And make sure the url and visit were added. | 1018 // And make sure the url and visit were added. |
1018 EXPECT_TRUE(callback.success); | 1019 EXPECT_TRUE(callback.success); |
1019 EXPECT_NE(0, callback.row.id()); | 1020 EXPECT_NE(0, callback.row.id()); |
1020 ASSERT_EQ(1U, callback.visits.size()); | 1021 ASSERT_EQ(1U, callback.visits.size()); |
1021 EXPECT_EQ(content::PAGE_TRANSITION_KEYWORD_GENERATED, | 1022 EXPECT_EQ(content::PAGE_TRANSITION_KEYWORD_GENERATED, |
1022 content::PageTransitionStripQualifier(callback.visits[0].transition)); | 1023 content::PageTransitionStripQualifier(callback.visits[0].transition)); |
1023 } | 1024 } |
1024 | 1025 |
1025 // Make sure that the load routine deletes prepopulated engines that no longer | 1026 // Make sure that the load routine deletes prepopulated engines that no longer |
1026 // exist in the prepopulate data. | 1027 // exist in the prepopulate data. |
1027 TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { | 1028 TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { |
1028 // Create a preloaded template url. Add it to a loaded model and wait for the | 1029 // Create a preloaded template url. Add it to a loaded model and wait for the |
1029 // saves to finish. | 1030 // saves to finish. |
1030 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); | 1031 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); |
1031 test_util_->ChangeModelToLoadState(); | 1032 test_util()->ChangeModelToLoadState(); |
1032 model()->Add(t_url); | 1033 model()->Add(t_url); |
1033 ASSERT_TRUE( | 1034 ASSERT_TRUE( |
1034 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); | 1035 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); |
1035 base::RunLoop().RunUntilIdle(); | 1036 base::RunLoop().RunUntilIdle(); |
1036 | 1037 |
1037 // Ensure that merging clears this engine. | 1038 // Ensure that merging clears this engine. |
1038 test_util_->ResetModel(true); | 1039 test_util()->ResetModel(true); |
1039 ASSERT_TRUE( | 1040 ASSERT_TRUE( |
1040 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); | 1041 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); |
1041 | 1042 |
1042 // Wait for any saves to finish. | 1043 // Wait for any saves to finish. |
1043 base::RunLoop().RunUntilIdle(); | 1044 base::RunLoop().RunUntilIdle(); |
1044 | 1045 |
1045 // Reload the model to verify that the database was updated as a result of the | 1046 // Reload the model to verify that the database was updated as a result of the |
1046 // merge. | 1047 // merge. |
1047 test_util_->ResetModel(true); | 1048 test_util()->ResetModel(true); |
1048 ASSERT_TRUE( | 1049 ASSERT_TRUE( |
1049 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); | 1050 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); |
1050 } | 1051 } |
1051 | 1052 |
1052 // Make sure that load routine doesn't delete prepopulated engines that no | 1053 // Make sure that load routine doesn't delete prepopulated engines that no |
1053 // longer exist in the prepopulate data if it has been modified by the user. | 1054 // longer exist in the prepopulate data if it has been modified by the user. |
1054 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { | 1055 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { |
1055 // Create a preloaded template url and add it to a loaded model. | 1056 // Create a preloaded template url and add it to a loaded model. |
1056 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999); | 1057 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999); |
1057 test_util_->ChangeModelToLoadState(); | 1058 test_util()->ChangeModelToLoadState(); |
1058 model()->Add(t_url); | 1059 model()->Add(t_url); |
1059 | 1060 |
1060 // Do the copy after t_url is added so that the id is set. | 1061 // Do the copy after t_url is added so that the id is set. |
1061 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 1062 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
1062 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); | 1063 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); |
1063 | 1064 |
1064 // Wait for any saves to finish. | 1065 // Wait for any saves to finish. |
1065 base::RunLoop().RunUntilIdle(); | 1066 base::RunLoop().RunUntilIdle(); |
1066 | 1067 |
1067 // Ensure that merging won't clear it if the user has edited it. | 1068 // Ensure that merging won't clear it if the user has edited it. |
1068 test_util_->ResetModel(true); | 1069 test_util()->ResetModel(true); |
1069 const TemplateURL* url_for_unittest = | 1070 const TemplateURL* url_for_unittest = |
1070 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | 1071 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); |
1071 ASSERT_TRUE(url_for_unittest != NULL); | 1072 ASSERT_TRUE(url_for_unittest != NULL); |
1072 AssertEquals(*cloned_url, *url_for_unittest); | 1073 AssertEquals(*cloned_url, *url_for_unittest); |
1073 | 1074 |
1074 // Wait for any saves to finish. | 1075 // Wait for any saves to finish. |
1075 base::RunLoop().RunUntilIdle(); | 1076 base::RunLoop().RunUntilIdle(); |
1076 | 1077 |
1077 // Reload the model to verify that save/reload retains the item. | 1078 // Reload the model to verify that save/reload retains the item. |
1078 test_util_->ResetModel(true); | 1079 test_util()->ResetModel(true); |
1079 ASSERT_TRUE( | 1080 ASSERT_TRUE( |
1080 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); | 1081 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); |
1081 } | 1082 } |
1082 | 1083 |
1083 // Make sure that load routine doesn't delete | 1084 // Make sure that load routine doesn't delete |
1084 // prepopulated engines that no longer exist in the prepopulate data if | 1085 // prepopulated engines that no longer exist in the prepopulate data if |
1085 // it has been modified by the user. | 1086 // it has been modified by the user. |
1086 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { | 1087 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { |
1087 test_util_->VerifyLoad(); | 1088 test_util()->VerifyLoad(); |
1088 // Verify that the default search provider is set to something. | 1089 // Verify that the default search provider is set to something. |
1089 TemplateURL* default_search = model()->GetDefaultSearchProvider(); | 1090 TemplateURL* default_search = model()->GetDefaultSearchProvider(); |
1090 ASSERT_TRUE(default_search != NULL); | 1091 ASSERT_TRUE(default_search != NULL); |
1091 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data())); | 1092 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data())); |
1092 | 1093 |
1093 // Wait for any saves to finish. | 1094 // Wait for any saves to finish. |
1094 base::RunLoop().RunUntilIdle(); | 1095 base::RunLoop().RunUntilIdle(); |
1095 | 1096 |
1096 // Reload the model and check that the default search provider | 1097 // Reload the model and check that the default search provider |
1097 // was properly saved. | 1098 // was properly saved. |
1098 test_util_->ResetModel(true); | 1099 test_util()->ResetModel(true); |
1099 default_search = model()->GetDefaultSearchProvider(); | 1100 default_search = model()->GetDefaultSearchProvider(); |
1100 ASSERT_TRUE(default_search != NULL); | 1101 ASSERT_TRUE(default_search != NULL); |
1101 AssertEquals(*cloned_url, *default_search); | 1102 AssertEquals(*cloned_url, *default_search); |
1102 } | 1103 } |
1103 | 1104 |
1104 // Make sure that the load routine doesn't delete | 1105 // Make sure that the load routine doesn't delete |
1105 // prepopulated engines that no longer exist in the prepopulate data if | 1106 // prepopulated engines that no longer exist in the prepopulate data if |
1106 // it is the default search provider. | 1107 // it is the default search provider. |
1107 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { | 1108 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { |
1108 // Set the default search provider to a preloaded template url which | 1109 // Set the default search provider to a preloaded template url which |
1109 // is not in the current set of preloaded template urls and save | 1110 // is not in the current set of preloaded template urls and save |
1110 // the result. | 1111 // the result. |
1111 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); | 1112 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); |
1112 test_util_->ChangeModelToLoadState(); | 1113 test_util()->ChangeModelToLoadState(); |
1113 model()->Add(t_url); | 1114 model()->Add(t_url); |
1114 model()->SetUserSelectedDefaultSearchProvider(t_url); | 1115 model()->SetUserSelectedDefaultSearchProvider(t_url); |
1115 // Do the copy after t_url is added and set as default so that its | 1116 // Do the copy after t_url is added and set as default so that its |
1116 // internal state is correct. | 1117 // internal state is correct. |
1117 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 1118 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
1118 | 1119 |
1119 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); | 1120 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); |
1120 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); | 1121 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); |
1121 base::RunLoop().RunUntilIdle(); | 1122 base::RunLoop().RunUntilIdle(); |
1122 | 1123 |
1123 // Ensure that merging won't clear the prepopulated template url | 1124 // Ensure that merging won't clear the prepopulated template url |
1124 // which is no longer present if it's the default engine. | 1125 // which is no longer present if it's the default engine. |
1125 test_util_->ResetModel(true); | 1126 test_util()->ResetModel(true); |
1126 { | 1127 { |
1127 const TemplateURL* keyword_url = | 1128 const TemplateURL* keyword_url = |
1128 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | 1129 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); |
1129 ASSERT_TRUE(keyword_url != NULL); | 1130 ASSERT_TRUE(keyword_url != NULL); |
1130 AssertEquals(*cloned_url, *keyword_url); | 1131 AssertEquals(*cloned_url, *keyword_url); |
1131 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); | 1132 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); |
1132 } | 1133 } |
1133 | 1134 |
1134 // Wait for any saves to finish. | 1135 // Wait for any saves to finish. |
1135 base::RunLoop().RunUntilIdle(); | 1136 base::RunLoop().RunUntilIdle(); |
1136 | 1137 |
1137 // Reload the model to verify that the update was saved. | 1138 // Reload the model to verify that the update was saved. |
1138 test_util_->ResetModel(true); | 1139 test_util()->ResetModel(true); |
1139 { | 1140 { |
1140 const TemplateURL* keyword_url = | 1141 const TemplateURL* keyword_url = |
1141 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | 1142 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); |
1142 ASSERT_TRUE(keyword_url != NULL); | 1143 ASSERT_TRUE(keyword_url != NULL); |
1143 AssertEquals(*cloned_url, *keyword_url); | 1144 AssertEquals(*cloned_url, *keyword_url); |
1144 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); | 1145 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); |
1145 } | 1146 } |
1146 } | 1147 } |
1147 | 1148 |
1148 // Make sure that the load routine sets a default search provider if it was | 1149 // Make sure that the load routine sets a default search provider if it was |
1149 // missing and not managed. | 1150 // missing and not managed. |
1150 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { | 1151 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { |
1151 // Force the model to load and make sure we have a default search provider. | 1152 // Force the model to load and make sure we have a default search provider. |
1152 test_util_->VerifyLoad(); | 1153 test_util()->VerifyLoad(); |
1153 EXPECT_TRUE(model()->GetDefaultSearchProvider()); | 1154 EXPECT_TRUE(model()->GetDefaultSearchProvider()); |
1154 | 1155 |
1155 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( | 1156 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( |
1156 search_terms_data())); | 1157 search_terms_data())); |
1157 | 1158 |
1158 // Make default search provider unusable (no search terms). | 1159 // Make default search provider unusable (no search terms). |
1159 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(), | 1160 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(), |
1160 ASCIIToUTF16("test"), ASCIIToUTF16("test"), | 1161 ASCIIToUTF16("test"), ASCIIToUTF16("test"), |
1161 "http://example.com/"); | 1162 "http://example.com/"); |
1162 base::RunLoop().RunUntilIdle(); | 1163 base::RunLoop().RunUntilIdle(); |
1163 | 1164 |
1164 // Reset the model and load it. There should be a usable default search | 1165 // Reset the model and load it. There should be a usable default search |
1165 // provider. | 1166 // provider. |
1166 test_util_->ResetModel(true); | 1167 test_util()->ResetModel(true); |
1167 | 1168 |
1168 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 1169 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
1169 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( | 1170 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( |
1170 search_terms_data())); | 1171 search_terms_data())); |
1171 } | 1172 } |
1172 | 1173 |
1173 // Simulates failing to load the webdb and makes sure the default search | 1174 // Simulates failing to load the webdb and makes sure the default search |
1174 // provider is valid. | 1175 // provider is valid. |
1175 TEST_F(TemplateURLServiceTest, FailedInit) { | 1176 TEST_F(TemplateURLServiceTest, FailedInit) { |
1176 test_util_->VerifyLoad(); | 1177 test_util()->VerifyLoad(); |
1177 | 1178 |
1178 test_util_->ClearModel(); | 1179 test_util()->ClearModel(); |
1179 WebDataServiceFactory::GetKeywordWebDataForProfile( | 1180 test_util()->web_data_service()->ShutdownDatabase(); |
1180 test_util_->profile(), Profile::EXPLICIT_ACCESS)->ShutdownDatabase(); | |
1181 | 1181 |
1182 test_util_->ResetModel(false); | 1182 test_util()->ResetModel(false); |
1183 model()->Load(); | 1183 model()->Load(); |
1184 base::RunLoop().RunUntilIdle(); | 1184 base::RunLoop().RunUntilIdle(); |
1185 | 1185 |
1186 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 1186 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
1187 } | 1187 } |
1188 | 1188 |
1189 // Verifies that if the default search URL preference is managed, we report | 1189 // Verifies that if the default search URL preference is managed, we report |
1190 // the default search as managed. Also check that we are getting the right | 1190 // the default search as managed. Also check that we are getting the right |
1191 // values. | 1191 // values. |
1192 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { | 1192 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { |
1193 test_util_->VerifyLoad(); | 1193 test_util()->VerifyLoad(); |
1194 const size_t initial_count = model()->GetTemplateURLs().size(); | 1194 const size_t initial_count = model()->GetTemplateURLs().size(); |
1195 test_util_->ResetObserverCount(); | 1195 test_util()->ResetObserverCount(); |
1196 | 1196 |
1197 // Set a regular default search provider. | 1197 // Set a regular default search provider. |
1198 TemplateURL* regular_default = AddKeywordWithDate( | 1198 TemplateURL* regular_default = AddKeywordWithDate( |
1199 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", | 1199 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", |
1200 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 1200 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
1201 VerifyObserverCount(1); | 1201 VerifyObserverCount(1); |
1202 model()->SetUserSelectedDefaultSearchProvider(regular_default); | 1202 model()->SetUserSelectedDefaultSearchProvider(regular_default); |
1203 // Adding the URL and setting the default search provider should have caused | 1203 // Adding the URL and setting the default search provider should have caused |
1204 // notifications. | 1204 // notifications. |
1205 VerifyObserverCount(1); | 1205 VerifyObserverCount(1); |
1206 EXPECT_FALSE(model()->is_default_search_managed()); | 1206 EXPECT_FALSE(model()->is_default_search_managed()); |
1207 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1207 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1208 | 1208 |
1209 // Set a managed preference that establishes a default search provider. | 1209 // Set a managed preference that establishes a default search provider. |
1210 const char kName[] = "test1"; | 1210 const char kName[] = "test1"; |
1211 const char kKeyword[] = "test.com"; | 1211 const char kKeyword[] = "test.com"; |
1212 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | 1212 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
1213 const char kIconURL[] = "http://test.com/icon.jpg"; | 1213 const char kIconURL[] = "http://test.com/icon.jpg"; |
1214 const char kEncodings[] = "UTF-16;UTF-32"; | 1214 const char kEncodings[] = "UTF-16;UTF-32"; |
1215 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; | 1215 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; |
1216 const char kSearchTermsReplacementKey[] = "espv"; | 1216 const char kSearchTermsReplacementKey[] = "espv"; |
1217 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, | 1217 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
1218 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, | 1218 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, |
1219 kSearchTermsReplacementKey); | 1219 kSearchTermsReplacementKey); |
1220 VerifyObserverFired(); | 1220 VerifyObserverFired(); |
1221 EXPECT_TRUE(model()->is_default_search_managed()); | 1221 EXPECT_TRUE(model()->is_default_search_managed()); |
1222 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1222 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
1223 | 1223 |
1224 // Verify that the default manager we are getting is the managed one. | 1224 // Verify that the default manager we are getting is the managed one. |
1225 TemplateURLData data; | 1225 TemplateURLData data; |
1226 data.short_name = ASCIIToUTF16(kName); | 1226 data.short_name = ASCIIToUTF16(kName); |
1227 data.SetKeyword(ASCIIToUTF16(kKeyword)); | 1227 data.SetKeyword(ASCIIToUTF16(kKeyword)); |
1228 data.SetURL(kSearchURL); | 1228 data.SetURL(kSearchURL); |
1229 data.favicon_url = GURL(kIconURL); | 1229 data.favicon_url = GURL(kIconURL); |
1230 data.show_in_default_list = true; | 1230 data.show_in_default_list = true; |
1231 base::SplitString(kEncodings, ';', &data.input_encodings); | 1231 base::SplitString(kEncodings, ';', &data.input_encodings); |
1232 data.alternate_urls.push_back(kAlternateURL); | 1232 data.alternate_urls.push_back(kAlternateURL); |
1233 data.search_terms_replacement_key = kSearchTermsReplacementKey; | 1233 data.search_terms_replacement_key = kSearchTermsReplacementKey; |
1234 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data)); | 1234 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data)); |
1235 const TemplateURL* actual_managed_default = | 1235 const TemplateURL* actual_managed_default = |
1236 model()->GetDefaultSearchProvider(); | 1236 model()->GetDefaultSearchProvider(); |
1237 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); | 1237 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); |
1238 EXPECT_TRUE(actual_managed_default->show_in_default_list()); | 1238 EXPECT_TRUE(actual_managed_default->show_in_default_list()); |
1239 | 1239 |
1240 // Update the managed preference and check that the model has changed. | 1240 // Update the managed preference and check that the model has changed. |
1241 const char kNewName[] = "test2"; | 1241 const char kNewName[] = "test2"; |
1242 const char kNewKeyword[] = "other.com"; | 1242 const char kNewKeyword[] = "other.com"; |
1243 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; | 1243 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; |
1244 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; | 1244 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; |
1245 test_util_->SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, | 1245 test_util()->SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, |
1246 kNewSearchURL, kNewSuggestURL, std::string(), std::string(), | 1246 kNewSearchURL, kNewSuggestURL, std::string(), std::string(), |
1247 std::string(), std::string()); | 1247 std::string(), std::string()); |
1248 VerifyObserverFired(); | 1248 VerifyObserverFired(); |
1249 EXPECT_TRUE(model()->is_default_search_managed()); | 1249 EXPECT_TRUE(model()->is_default_search_managed()); |
1250 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1250 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
1251 | 1251 |
1252 // Verify that the default manager we are now getting is the correct one. | 1252 // Verify that the default manager we are now getting is the correct one. |
1253 TemplateURLData data2; | 1253 TemplateURLData data2; |
1254 data2.short_name = ASCIIToUTF16(kNewName); | 1254 data2.short_name = ASCIIToUTF16(kNewName); |
1255 data2.SetKeyword(ASCIIToUTF16(kNewKeyword)); | 1255 data2.SetKeyword(ASCIIToUTF16(kNewKeyword)); |
1256 data2.SetURL(kNewSearchURL); | 1256 data2.SetURL(kNewSearchURL); |
1257 data2.suggestions_url = kNewSuggestURL; | 1257 data2.suggestions_url = kNewSuggestURL; |
1258 data2.show_in_default_list = true; | 1258 data2.show_in_default_list = true; |
1259 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2)); | 1259 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2)); |
1260 actual_managed_default = model()->GetDefaultSearchProvider(); | 1260 actual_managed_default = model()->GetDefaultSearchProvider(); |
1261 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); | 1261 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); |
1262 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); | 1262 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); |
1263 | 1263 |
1264 // Remove all the managed prefs and check that we are no longer managed. | 1264 // Remove all the managed prefs and check that we are no longer managed. |
1265 test_util_->RemoveManagedDefaultSearchPreferences(); | 1265 test_util()->RemoveManagedDefaultSearchPreferences(); |
1266 VerifyObserverFired(); | 1266 VerifyObserverFired(); |
1267 EXPECT_FALSE(model()->is_default_search_managed()); | 1267 EXPECT_FALSE(model()->is_default_search_managed()); |
1268 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1268 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1269 | 1269 |
1270 // The default should now be the user preference. | 1270 // The default should now be the user preference. |
1271 const TemplateURL* actual_final_managed_default = | 1271 const TemplateURL* actual_final_managed_default = |
1272 model()->GetDefaultSearchProvider(); | 1272 model()->GetDefaultSearchProvider(); |
1273 ExpectSimilar(regular_default, actual_final_managed_default); | 1273 ExpectSimilar(regular_default, actual_final_managed_default); |
1274 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); | 1274 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); |
1275 | 1275 |
1276 // Disable the default search provider through policy. | 1276 // Disable the default search provider through policy. |
1277 test_util_->SetManagedDefaultSearchPreferences(false, std::string(), | 1277 test_util()->SetManagedDefaultSearchPreferences(false, std::string(), |
1278 std::string(), std::string(), std::string(), std::string(), | 1278 std::string(), std::string(), std::string(), std::string(), |
1279 std::string(), std::string(), std::string()); | 1279 std::string(), std::string(), std::string()); |
1280 VerifyObserverFired(); | 1280 VerifyObserverFired(); |
1281 EXPECT_TRUE(model()->is_default_search_managed()); | 1281 EXPECT_TRUE(model()->is_default_search_managed()); |
1282 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); | 1282 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); |
1283 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1283 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1284 | 1284 |
1285 // Re-enable it. | 1285 // Re-enable it. |
1286 test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, | 1286 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
1287 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, | 1287 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, |
1288 kSearchTermsReplacementKey); | 1288 kSearchTermsReplacementKey); |
1289 VerifyObserverFired(); | 1289 VerifyObserverFired(); |
1290 EXPECT_TRUE(model()->is_default_search_managed()); | 1290 EXPECT_TRUE(model()->is_default_search_managed()); |
1291 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1291 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
1292 | 1292 |
1293 // Verify that the default manager we are getting is the managed one. | 1293 // Verify that the default manager we are getting is the managed one. |
1294 actual_managed_default = model()->GetDefaultSearchProvider(); | 1294 actual_managed_default = model()->GetDefaultSearchProvider(); |
1295 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); | 1295 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); |
1296 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); | 1296 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); |
1297 | 1297 |
1298 // Clear the model and disable the default search provider through policy. | 1298 // Clear the model and disable the default search provider through policy. |
1299 // Verify that there is no default search provider after loading the model. | 1299 // Verify that there is no default search provider after loading the model. |
1300 // This checks against regressions of http://crbug.com/67180 | 1300 // This checks against regressions of http://crbug.com/67180 |
1301 | 1301 |
1302 // First, remove the preferences, reset the model, and set a default. | 1302 // First, remove the preferences, reset the model, and set a default. |
1303 test_util_->RemoveManagedDefaultSearchPreferences(); | 1303 test_util()->RemoveManagedDefaultSearchPreferences(); |
1304 test_util_->ResetModel(true); | 1304 test_util()->ResetModel(true); |
1305 TemplateURL* new_default = | 1305 TemplateURL* new_default = |
1306 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); | 1306 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); |
1307 ASSERT_FALSE(new_default == NULL); | 1307 ASSERT_FALSE(new_default == NULL); |
1308 model()->SetUserSelectedDefaultSearchProvider(new_default); | 1308 model()->SetUserSelectedDefaultSearchProvider(new_default); |
1309 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); | 1309 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); |
1310 | 1310 |
1311 // Now reset the model again but load it after setting the preferences. | 1311 // Now reset the model again but load it after setting the preferences. |
1312 test_util_->ResetModel(false); | 1312 test_util()->ResetModel(false); |
1313 test_util_->SetManagedDefaultSearchPreferences(false, std::string(), | 1313 test_util()->SetManagedDefaultSearchPreferences(false, std::string(), |
1314 std::string(), std::string(), std::string(), std::string(), | 1314 std::string(), std::string(), std::string(), std::string(), |
1315 std::string(), std::string(), std::string()); | 1315 std::string(), std::string(), std::string()); |
1316 test_util_->VerifyLoad(); | 1316 test_util()->VerifyLoad(); |
1317 EXPECT_TRUE(model()->is_default_search_managed()); | 1317 EXPECT_TRUE(model()->is_default_search_managed()); |
1318 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); | 1318 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); |
1319 } | 1319 } |
1320 | 1320 |
1321 // Test that if we load a TemplateURL with an empty GUID, the load process | 1321 // Test that if we load a TemplateURL with an empty GUID, the load process |
1322 // assigns it a newly generated GUID. | 1322 // assigns it a newly generated GUID. |
1323 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { | 1323 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { |
1324 // Add a new TemplateURL. | 1324 // Add a new TemplateURL. |
1325 test_util_->VerifyLoad(); | 1325 test_util()->VerifyLoad(); |
1326 const size_t initial_count = model()->GetTemplateURLs().size(); | 1326 const size_t initial_count = model()->GetTemplateURLs().size(); |
1327 | 1327 |
1328 TemplateURLData data; | 1328 TemplateURLData data; |
1329 data.short_name = ASCIIToUTF16("google"); | 1329 data.short_name = ASCIIToUTF16("google"); |
1330 data.SetKeyword(ASCIIToUTF16("keyword")); | 1330 data.SetKeyword(ASCIIToUTF16("keyword")); |
1331 data.SetURL("http://www.google.com/foo/bar"); | 1331 data.SetURL("http://www.google.com/foo/bar"); |
1332 data.sync_guid.clear(); | 1332 data.sync_guid.clear(); |
1333 TemplateURL* t_url = new TemplateURL(data); | 1333 TemplateURL* t_url = new TemplateURL(data); |
1334 model()->Add(t_url); | 1334 model()->Add(t_url); |
1335 | 1335 |
1336 VerifyObserverCount(1); | 1336 VerifyObserverCount(1); |
1337 base::RunLoop().RunUntilIdle(); | 1337 base::RunLoop().RunUntilIdle(); |
1338 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1338 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1339 | 1339 |
1340 // Reload the model to verify it was actually saved to the database and | 1340 // Reload the model to verify it was actually saved to the database and |
1341 // assigned a new GUID when brought back. | 1341 // assigned a new GUID when brought back. |
1342 test_util_->ResetModel(true); | 1342 test_util()->ResetModel(true); |
1343 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1343 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1344 const TemplateURL* loaded_url = | 1344 const TemplateURL* loaded_url = |
1345 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1345 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
1346 ASSERT_FALSE(loaded_url == NULL); | 1346 ASSERT_FALSE(loaded_url == NULL); |
1347 ASSERT_FALSE(loaded_url->sync_guid().empty()); | 1347 ASSERT_FALSE(loaded_url->sync_guid().empty()); |
1348 } | 1348 } |
1349 | 1349 |
1350 // Test that if we load a TemplateURL with duplicate input encodings, the load | 1350 // Test that if we load a TemplateURL with duplicate input encodings, the load |
1351 // process de-dupes them. | 1351 // process de-dupes them. |
1352 TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { | 1352 TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { |
1353 // Add a new TemplateURL. | 1353 // Add a new TemplateURL. |
1354 test_util_->VerifyLoad(); | 1354 test_util()->VerifyLoad(); |
1355 const size_t initial_count = model()->GetTemplateURLs().size(); | 1355 const size_t initial_count = model()->GetTemplateURLs().size(); |
1356 | 1356 |
1357 TemplateURLData data; | 1357 TemplateURLData data; |
1358 data.short_name = ASCIIToUTF16("google"); | 1358 data.short_name = ASCIIToUTF16("google"); |
1359 data.SetKeyword(ASCIIToUTF16("keyword")); | 1359 data.SetKeyword(ASCIIToUTF16("keyword")); |
1360 data.SetURL("http://www.google.com/foo/bar"); | 1360 data.SetURL("http://www.google.com/foo/bar"); |
1361 std::vector<std::string> encodings; | 1361 std::vector<std::string> encodings; |
1362 data.input_encodings.push_back("UTF-8"); | 1362 data.input_encodings.push_back("UTF-8"); |
1363 data.input_encodings.push_back("UTF-8"); | 1363 data.input_encodings.push_back("UTF-8"); |
1364 data.input_encodings.push_back("UTF-16"); | 1364 data.input_encodings.push_back("UTF-16"); |
1365 data.input_encodings.push_back("UTF-8"); | 1365 data.input_encodings.push_back("UTF-8"); |
1366 data.input_encodings.push_back("Big5"); | 1366 data.input_encodings.push_back("Big5"); |
1367 data.input_encodings.push_back("UTF-16"); | 1367 data.input_encodings.push_back("UTF-16"); |
1368 data.input_encodings.push_back("Big5"); | 1368 data.input_encodings.push_back("Big5"); |
1369 data.input_encodings.push_back("Windows-1252"); | 1369 data.input_encodings.push_back("Windows-1252"); |
1370 TemplateURL* t_url = new TemplateURL(data); | 1370 TemplateURL* t_url = new TemplateURL(data); |
1371 model()->Add(t_url); | 1371 model()->Add(t_url); |
1372 | 1372 |
1373 VerifyObserverCount(1); | 1373 VerifyObserverCount(1); |
1374 base::RunLoop().RunUntilIdle(); | 1374 base::RunLoop().RunUntilIdle(); |
1375 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1375 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1376 const TemplateURL* loaded_url = | 1376 const TemplateURL* loaded_url = |
1377 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1377 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
1378 ASSERT_TRUE(loaded_url != NULL); | 1378 ASSERT_TRUE(loaded_url != NULL); |
1379 EXPECT_EQ(8U, loaded_url->input_encodings().size()); | 1379 EXPECT_EQ(8U, loaded_url->input_encodings().size()); |
1380 | 1380 |
1381 // Reload the model to verify it was actually saved to the database and the | 1381 // Reload the model to verify it was actually saved to the database and the |
1382 // duplicate encodings were removed. | 1382 // duplicate encodings were removed. |
1383 test_util_->ResetModel(true); | 1383 test_util()->ResetModel(true); |
1384 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1384 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1385 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1385 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
1386 ASSERT_FALSE(loaded_url == NULL); | 1386 ASSERT_FALSE(loaded_url == NULL); |
1387 EXPECT_EQ(4U, loaded_url->input_encodings().size()); | 1387 EXPECT_EQ(4U, loaded_url->input_encodings().size()); |
1388 } | 1388 } |
1389 | 1389 |
1390 TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) { | 1390 TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) { |
1391 test_util_->VerifyLoad(); | 1391 test_util()->VerifyLoad(); |
1392 // Add third-party default search engine. | 1392 // Add third-party default search engine. |
1393 TemplateURL* user_dse = AddKeywordWithDate( | 1393 TemplateURL* user_dse = AddKeywordWithDate( |
1394 "user", "user", "http://www.goo.com/s?q={searchTerms}", | 1394 "user", "user", "http://www.goo.com/s?q={searchTerms}", |
1395 std::string(), std::string(), std::string(), | 1395 std::string(), std::string(), std::string(), |
1396 true, "UTF-8", Time(), Time()); | 1396 true, "UTF-8", Time(), Time()); |
1397 model()->SetUserSelectedDefaultSearchProvider(user_dse); | 1397 model()->SetUserSelectedDefaultSearchProvider(user_dse); |
1398 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | 1398 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); |
1399 | 1399 |
1400 TemplateURL* ext_dse = CreateKeywordWithDate( | 1400 TemplateURL* ext_dse = CreateKeywordWithDate( |
1401 model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}", | 1401 model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}", |
1402 std::string(), std::string(), std::string(), | 1402 std::string(), std::string(), std::string(), |
1403 true, true, "UTF-8", Time(), Time()); | 1403 true, true, "UTF-8", Time(), Time()); |
1404 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | 1404 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( |
1405 new TemplateURL::AssociatedExtensionInfo( | 1405 new TemplateURL::AssociatedExtensionInfo( |
1406 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext")); | 1406 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext")); |
1407 extension_info->wants_to_be_default_engine = true; | 1407 extension_info->wants_to_be_default_engine = true; |
1408 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1408 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
1409 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); | 1409 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); |
1410 | 1410 |
1411 model()->RemoveExtensionControlledTURL( | 1411 model()->RemoveExtensionControlledTURL( |
1412 "ext", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1412 "ext", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
1413 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); | 1413 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); |
1414 } | 1414 } |
1415 | 1415 |
1416 TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) { | 1416 TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) { |
1417 test_util_->VerifyLoad(); | 1417 test_util()->VerifyLoad(); |
1418 // Add third-party default search engine. | 1418 // Add third-party default search engine. |
1419 TemplateURL* user_dse = AddKeywordWithDate( | 1419 TemplateURL* user_dse = AddKeywordWithDate( |
1420 "user", "user", "http://www.goo.com/s?q={searchTerms}", | 1420 "user", "user", "http://www.goo.com/s?q={searchTerms}", |
1421 std::string(), std::string(), std::string(), | 1421 std::string(), std::string(), std::string(), |
1422 true, "UTF-8", Time(), Time()); | 1422 true, "UTF-8", Time(), Time()); |
1423 model()->SetUserSelectedDefaultSearchProvider(user_dse); | 1423 model()->SetUserSelectedDefaultSearchProvider(user_dse); |
1424 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | 1424 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); |
1425 | 1425 |
1426 TemplateURL* ext_dse = CreateKeywordWithDate( | 1426 TemplateURL* ext_dse = CreateKeywordWithDate( |
1427 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}", | 1427 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}", |
1428 std::string(), std::string(), std::string(), | 1428 std::string(), std::string(), std::string(), |
1429 true, false, "UTF-8", Time(), Time()); | 1429 true, false, "UTF-8", Time(), Time()); |
1430 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | 1430 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( |
1431 new TemplateURL::AssociatedExtensionInfo( | 1431 new TemplateURL::AssociatedExtensionInfo( |
1432 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); | 1432 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); |
1433 extension_info->wants_to_be_default_engine = false; | 1433 extension_info->wants_to_be_default_engine = false; |
1434 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1434 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
1435 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | 1435 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); |
1436 | 1436 |
1437 ext_dse = CreateKeywordWithDate( | 1437 ext_dse = CreateKeywordWithDate( |
1438 model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}", | 1438 model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}", |
1439 std::string(), std::string(), std::string(), | 1439 std::string(), std::string(), std::string(), |
1440 true, true, "UTF-8", Time(), Time()); | 1440 true, true, "UTF-8", Time(), Time()); |
1441 extension_info.reset(new TemplateURL::AssociatedExtensionInfo( | 1441 extension_info.reset(new TemplateURL::AssociatedExtensionInfo( |
1442 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext2")); | 1442 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext2")); |
1443 extension_info->wants_to_be_default_engine = true; | 1443 extension_info->wants_to_be_default_engine = true; |
1444 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1444 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
1445 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); | 1445 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); |
1446 | 1446 |
1447 test_util_->ResetModel(true); | 1447 test_util()->ResetModel(true); |
1448 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); | 1448 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); |
1449 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); | 1449 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); |
1450 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1450 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
1451 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); | 1451 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); |
1452 } | 1452 } |
1453 | 1453 |
1454 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { | 1454 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { |
1455 // Set a managed preference that establishes a default search provider. | 1455 // Set a managed preference that establishes a default search provider. |
1456 const char kName[] = "test"; | 1456 const char kName[] = "test"; |
1457 const char kKeyword[] = "test.com"; | 1457 const char kKeyword[] = "test.com"; |
1458 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | 1458 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
1459 const char kIconURL[] = "http://test.com/icon.jpg"; | 1459 const char kIconURL[] = "http://test.com/icon.jpg"; |
1460 const char kEncodings[] = "UTF-16;UTF-32"; | 1460 const char kEncodings[] = "UTF-16;UTF-32"; |
1461 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; | 1461 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; |
1462 const char kSearchTermsReplacementKey[] = "espv"; | 1462 const char kSearchTermsReplacementKey[] = "espv"; |
1463 test_util_->SetManagedDefaultSearchPreferences( | 1463 test_util()->SetManagedDefaultSearchPreferences( |
1464 true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, | 1464 true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, |
1465 kAlternateURL, kSearchTermsReplacementKey); | 1465 kAlternateURL, kSearchTermsReplacementKey); |
1466 test_util_->VerifyLoad(); | 1466 test_util()->VerifyLoad(); |
1467 // Verify that the default manager we are getting is the managed one. | 1467 // Verify that the default manager we are getting is the managed one. |
1468 TemplateURLData data; | 1468 TemplateURLData data; |
1469 data.short_name = ASCIIToUTF16(kName); | 1469 data.short_name = ASCIIToUTF16(kName); |
1470 data.SetKeyword(ASCIIToUTF16(kKeyword)); | 1470 data.SetKeyword(ASCIIToUTF16(kKeyword)); |
1471 data.SetURL(kSearchURL); | 1471 data.SetURL(kSearchURL); |
1472 data.favicon_url = GURL(kIconURL); | 1472 data.favicon_url = GURL(kIconURL); |
1473 data.show_in_default_list = true; | 1473 data.show_in_default_list = true; |
1474 base::SplitString(kEncodings, ';', &data.input_encodings); | 1474 base::SplitString(kEncodings, ';', &data.input_encodings); |
1475 data.alternate_urls.push_back(kAlternateURL); | 1475 data.alternate_urls.push_back(kAlternateURL); |
1476 data.search_terms_replacement_key = kSearchTermsReplacementKey; | 1476 data.search_terms_replacement_key = kSearchTermsReplacementKey; |
(...skipping 10 matching lines...) Expand all Loading... |
1487 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | 1487 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( |
1488 new TemplateURL::AssociatedExtensionInfo( | 1488 new TemplateURL::AssociatedExtensionInfo( |
1489 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); | 1489 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); |
1490 extension_info->wants_to_be_default_engine = true; | 1490 extension_info->wants_to_be_default_engine = true; |
1491 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1491 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
1492 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1492 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
1493 EXPECT_TRUE(model()->is_default_search_managed()); | 1493 EXPECT_TRUE(model()->is_default_search_managed()); |
1494 actual_managed_default = model()->GetDefaultSearchProvider(); | 1494 actual_managed_default = model()->GetDefaultSearchProvider(); |
1495 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 1495 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
1496 } | 1496 } |
OLD | NEW |