| 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" |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 389 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 390 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); | 390 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); |
| 391 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); | 391 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); |
| 392 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | 392 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); |
| 393 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); | 393 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); |
| 394 } | 394 } |
| 395 | 395 |
| 396 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { | 396 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { |
| 397 test_util()->VerifyLoad(); | 397 test_util()->VerifyLoad(); |
| 398 | 398 |
| 399 TemplateURL* original1 = AddKeywordWithDate( | 399 AddKeywordWithDate( |
| 400 "replaceable", "keyword1", "http://test1", std::string(), std::string(), | 400 "replaceable", "keyword1", "http://test1", std::string(), std::string(), |
| 401 std::string(), true, "UTF-8", Time(), Time()); | 401 std::string(), true, "UTF-8", Time(), Time()); |
| 402 TemplateURL* original2 = AddKeywordWithDate( | 402 TemplateURL* original2 = AddKeywordWithDate( |
| 403 "nonreplaceable", "keyword2", "http://test2", std::string(), | 403 "nonreplaceable", "keyword2", "http://test2", std::string(), |
| 404 std::string(), std::string(), false, "UTF-8", Time(), Time()); | 404 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
| 405 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", | 405 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", |
| 406 "http://test3"); | 406 "http://test3"); |
| 407 TemplateURL* original3 = | 407 TemplateURL* original3 = |
| 408 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); | 408 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); |
| 409 ASSERT_TRUE(original3); | 409 ASSERT_TRUE(original3); |
| 410 | 410 |
| 411 // Add an extension keyword that conflicts with each of the above three | 411 // Extension keywords should override replaceable keywords. |
| 412 // keywords. | 412 model()->RegisterOmniboxKeyword("id1", "test", "keyword1", "http://test4"); |
| 413 // Both replaceable and non-replaceable keywords should be uniquified. | 413 TemplateURL* extension1 = model()->FindTemplateURLForExtension( |
| 414 model()->RegisterOmniboxKeyword("test4", "test", "keyword1", "http://test4"); | 414 "id1", TemplateURL::OMNIBOX_API_EXTENSION); |
| 415 TemplateURL* extension1 = | 415 EXPECT_TRUE(extension1); |
| 416 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")); | 416 EXPECT_EQ(extension1, |
| 417 ASSERT_TRUE(extension1); | 417 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); |
| 418 EXPECT_EQ(original1, | |
| 419 model()->GetTemplateURLForKeyword(ASCIIToUTF16("test1"))); | |
| 420 | 418 |
| 421 model()->RegisterOmniboxKeyword("test5", "test", "keyword2", "http://test5"); | 419 // They should not override non-replaceable keywords. |
| 422 TemplateURL* extension2 = | 420 model()->RegisterOmniboxKeyword("id2", "test", "keyword2", "http://test5"); |
| 423 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")); | 421 TemplateURL* extension2 = model()->FindTemplateURLForExtension( |
| 422 "id2", TemplateURL::OMNIBOX_API_EXTENSION); |
| 424 ASSERT_TRUE(extension2); | 423 ASSERT_TRUE(extension2); |
| 425 EXPECT_EQ(original2, | 424 EXPECT_EQ(original2, |
| 426 model()->GetTemplateURLForKeyword(ASCIIToUTF16("test2"))); | 425 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); |
| 427 | 426 |
| 428 // They should override extension keywords added earlier. | 427 // They should override extension keywords added earlier. |
| 429 model()->RegisterOmniboxKeyword("test6", "test", "keyword3", "http://test6"); | 428 model()->RegisterOmniboxKeyword("id3", "test", "keyword3", "http://test6"); |
| 430 TemplateURL* extension3 = | 429 TemplateURL* extension3 = model()->FindTemplateURLForExtension( |
| 431 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); | 430 "id3", TemplateURL::OMNIBOX_API_EXTENSION); |
| 432 ASSERT_TRUE(extension3); | 431 ASSERT_TRUE(extension3); |
| 433 EXPECT_EQ(original3, | 432 EXPECT_EQ(extension3, |
| 434 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_"))); | 433 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); |
| 435 } | 434 } |
| 436 | 435 |
| 437 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { | 436 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { |
| 438 test_util()->VerifyLoad(); | 437 test_util()->VerifyLoad(); |
| 439 | 438 |
| 440 // Similar to the AddSameKeyword test, but with an extension keyword masking a | 439 // Similar to the AddSameKeyword test, but with an extension keyword masking a |
| 441 // replaceable TemplateURL. We should still do correct conflict resolution | 440 // replaceable TemplateURL. We should still do correct conflict resolution |
| 442 // between the non-template URLs. | 441 // between the non-template URLs. |
| 443 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", | 442 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", |
| 444 "http://test2"); | 443 "http://test2"); |
| 445 TemplateURL* extension = | 444 TemplateURL* extension = |
| 446 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 445 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
| 447 ASSERT_TRUE(extension); | 446 ASSERT_TRUE(extension); |
| 448 // Adding a keyword that matches the extension should cause the extension | 447 // Adding a keyword that matches the extension. |
| 449 // to uniquify. | |
| 450 AddKeywordWithDate( | 448 AddKeywordWithDate( |
| 451 "replaceable", "keyword", "http://test1", std::string(), std::string(), | 449 "replaceable", "keyword", "http://test1", std::string(), std::string(), |
| 452 std::string(), true, "UTF-8", Time(), Time()); | 450 std::string(), true, "UTF-8", Time(), Time()); |
| 453 | 451 |
| 454 // Adding another replaceable keyword should remove the existing one, but | 452 // Adding another replaceable keyword should remove the existing one, but |
| 455 // leave the extension as is. | 453 // leave the extension as is. |
| 456 TemplateURLData data; | 454 TemplateURLData data; |
| 457 data.short_name = ASCIIToUTF16("name1"); | 455 data.short_name = ASCIIToUTF16("name1"); |
| 458 data.SetKeyword(ASCIIToUTF16("keyword")); | 456 data.SetKeyword(ASCIIToUTF16("keyword")); |
| 459 data.SetURL("http://test3"); | 457 data.SetURL("http://test3"); |
| 460 data.safe_for_autoreplace = true; | 458 data.safe_for_autoreplace = true; |
| 461 TemplateURL* t_url = new TemplateURL(data); | 459 TemplateURL* t_url = new TemplateURL(data); |
| 462 model()->Add(t_url); | 460 model()->Add(t_url); |
| 463 EXPECT_EQ(extension, | 461 EXPECT_EQ(extension, |
| 464 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); | 462 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 465 EXPECT_TRUE(model()->GetTemplateURLForHost("test1") == NULL); | |
| 466 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); | 463 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); |
| 467 | 464 |
| 468 // Adding a nonreplaceable keyword should remove the existing replaceable | 465 // Adding a nonreplaceable keyword should remove the existing replaceable |
| 469 // keyword. | 466 // keyword and replace the extension as the associated URL for this keyword, |
| 467 // but not evict the extension from the service entirely. |
| 470 data.short_name = ASCIIToUTF16("name2"); | 468 data.short_name = ASCIIToUTF16("name2"); |
| 471 data.SetURL("http://test4"); | 469 data.SetURL("http://test4"); |
| 472 data.safe_for_autoreplace = false; | 470 data.safe_for_autoreplace = false; |
| 473 TemplateURL* t_url2 = new TemplateURL(data); | 471 TemplateURL* t_url2 = new TemplateURL(data); |
| 474 model()->Add(t_url2); | 472 model()->Add(t_url2); |
| 475 EXPECT_EQ(t_url2, | 473 EXPECT_EQ(t_url2, |
| 476 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 474 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 477 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); | |
| 478 EXPECT_EQ(extension, | |
| 479 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); | |
| 480 } | 475 } |
| 481 | 476 |
| 482 TEST_F(TemplateURLServiceTest, RestoreOmniboxExtensionKeyword) { | 477 TEST_F(TemplateURLServiceTest, NotPersistOmniboxExtensionKeyword) { |
| 483 test_util()->VerifyLoad(); | 478 test_util()->VerifyLoad(); |
| 484 | 479 |
| 485 // Register an omnibox keyword. | 480 // Register an omnibox keyword. |
| 486 model()->RegisterOmniboxKeyword("test", "extension", "keyword", | 481 model()->RegisterOmniboxKeyword("test", "extension", "keyword", |
| 487 "chrome-extension://test"); | 482 "chrome-extension://test"); |
| 488 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 483 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 489 | 484 |
| 490 // Reload the data. | 485 // Reload the data. |
| 491 test_util()->ResetModel(true); | 486 test_util()->ResetModel(true); |
| 492 | 487 |
| 493 // Ensure the omnibox keyword is restored correctly. | 488 // Ensure the omnibox keyword is not persisted. |
| 494 TemplateURL* t_url = | 489 ASSERT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 495 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | |
| 496 ASSERT_TRUE(t_url); | |
| 497 ASSERT_EQ(TemplateURL::OMNIBOX_API_EXTENSION, t_url->GetType()); | |
| 498 EXPECT_EQ("test", t_url->GetExtensionId()); | |
| 499 } | 490 } |
| 500 | 491 |
| 501 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { | 492 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { |
| 502 Time now = Time::Now(); | 493 Time now = Time::Now(); |
| 503 TimeDelta one_day = TimeDelta::FromDays(1); | 494 TimeDelta one_day = TimeDelta::FromDays(1); |
| 504 Time month_ago = now - TimeDelta::FromDays(30); | 495 Time month_ago = now - TimeDelta::FromDays(30); |
| 505 | 496 |
| 506 // Nothing has been added. | 497 // Nothing has been added. |
| 507 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); | 498 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); |
| 508 | 499 |
| (...skipping 979 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1488 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | 1479 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( |
| 1489 new TemplateURL::AssociatedExtensionInfo( | 1480 new TemplateURL::AssociatedExtensionInfo( |
| 1490 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); | 1481 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); |
| 1491 extension_info->wants_to_be_default_engine = true; | 1482 extension_info->wants_to_be_default_engine = true; |
| 1492 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1483 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
| 1493 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1484 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
| 1494 EXPECT_TRUE(model()->is_default_search_managed()); | 1485 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1495 actual_managed_default = model()->GetDefaultSearchProvider(); | 1486 actual_managed_default = model()->GetDefaultSearchProvider(); |
| 1496 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 1487 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 1497 } | 1488 } |
| OLD | NEW |