Chromium Code Reviews| 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 "components/search_engines/template_url_service.h" | 5 #include "components/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/task/cancelable_task_tracker.h" | 22 #include "base/task/cancelable_task_tracker.h" |
| 23 #include "base/test/simple_test_clock.h" | 23 #include "base/test/simple_test_clock.h" |
| 24 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 26 #include "chrome/browser/history/history_service_factory.h" | 26 #include "chrome/browser/history/history_service_factory.h" |
| 27 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 27 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 28 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
| 29 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
| 30 #include "components/search_engines/keyword_web_data_service.h" | 30 #include "components/search_engines/keyword_web_data_service.h" |
| 31 #include "components/search_engines/search_engines_test_util.h" | |
| 31 #include "components/search_engines/search_host_to_urls_map.h" | 32 #include "components/search_engines/search_host_to_urls_map.h" |
| 32 #include "components/search_engines/search_terms_data.h" | 33 #include "components/search_engines/search_terms_data.h" |
| 33 #include "components/search_engines/template_url.h" | 34 #include "components/search_engines/template_url.h" |
| 34 #include "components/search_engines/template_url_prepopulate_data.h" | 35 #include "components/search_engines/template_url_prepopulate_data.h" |
| 35 #include "content/public/test/test_browser_thread_bundle.h" | 36 #include "content/public/test/test_browser_thread_bundle.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 38 |
| 38 using base::ASCIIToUTF16; | 39 using base::ASCIIToUTF16; |
| 39 using base::Time; | 40 using base::Time; |
| 40 using base::TimeDelta; | 41 using base::TimeDelta; |
| (...skipping 1433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1474 std::move(ext_dse), std::move(extension_info)); | 1475 std::move(ext_dse), std::move(extension_info)); |
| 1475 EXPECT_EQ(ext_dse_ptr, model()->GetDefaultSearchProvider()); | 1476 EXPECT_EQ(ext_dse_ptr, model()->GetDefaultSearchProvider()); |
| 1476 | 1477 |
| 1477 test_util()->ResetModel(true); | 1478 test_util()->ResetModel(true); |
| 1478 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); | 1479 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); |
| 1479 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); | 1480 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); |
| 1480 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1481 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
| 1481 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); | 1482 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); |
| 1482 } | 1483 } |
| 1483 | 1484 |
| 1485 // Check that default extension engine and user engines with same keywords are | |
| 1486 // handled corrected. | |
| 1487 TEST_F(TemplateURLServiceTest, DefaultExtensionAndUserEnginesWithSameKeywords) { | |
| 1488 test_util()->VerifyLoad(); | |
| 1489 // TemplateURLData used for all engines. | |
| 1490 auto turl_data = GenerateDummyTemplateURLData("ext2"); | |
|
Peter Kasting
2017/02/07 00:42:45
Nit: "ext2" is pretty random, especially when the
Alexander Yashkin
2017/02/07 08:09:19
Done
| |
| 1491 turl_data->safe_for_autoreplace = false; | |
| 1492 | |
| 1493 // Add non replaceable user engine. | |
| 1494 const TemplateURL* user1 = | |
| 1495 model()->Add(base::MakeUnique<TemplateURL>(*turl_data)); | |
| 1496 | |
| 1497 // Add default extension engine with same keyword. | |
| 1498 auto ext_dse = base::MakeUnique<TemplateURL>( | |
| 1499 *turl_data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1500 auto extension_info = | |
| 1501 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext2"); | |
| 1502 extension_info->wants_to_be_default_engine = true; | |
| 1503 | |
| 1504 model()->AddExtensionControlledTURL(std::move(ext_dse), | |
| 1505 std::move(extension_info)); | |
| 1506 | |
| 1507 // Add another non replaceable user engine with same keyword as extension. | |
| 1508 const TemplateURL* user2 = | |
| 1509 model()->Add(base::MakeUnique<TemplateURL>(*turl_data)); | |
| 1510 // Wait for any saves to finish. | |
| 1511 base::RunLoop().RunUntilIdle(); | |
| 1512 | |
| 1513 // Check extension DSE is set as default and its keyword is not changed. | |
| 1514 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); | |
| 1515 auto current_dse = model()->GetDefaultSearchProvider(); | |
| 1516 ExpectSimilar(turl_data.get(), ¤t_dse->data()); | |
| 1517 EXPECT_EQ(current_dse, | |
| 1518 model()->FindTemplateURLForExtension( | |
| 1519 "ext2", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)); | |
| 1520 | |
| 1521 // Expect that both user engine keywords are changed because of conflict | |
| 1522 // with extension engine keyword. | |
| 1523 EXPECT_NE(turl_data->keyword(), user1->keyword()); | |
| 1524 EXPECT_NE(turl_data->keyword(), user2->keyword()); | |
| 1525 // Check that user engine keywords are different. | |
| 1526 EXPECT_NE(user1->keyword(), user2->keyword()); | |
| 1527 } | |
| 1528 | |
| 1529 // Check that two extensions with same engine handled corrected. | |
|
Peter Kasting
2017/02/07 00:42:45
Nit: handled corrected -> are handled correctly
Alexander Yashkin
2017/02/07 08:09:19
Done, thanks.
| |
| 1530 TEST_F(TemplateURLServiceTest, ExtensionsWithSameKeywords) { | |
| 1531 test_util()->VerifyLoad(); | |
| 1532 | |
| 1533 // TemplateURLData used for all extension. | |
|
Peter Kasting
2017/02/07 00:42:45
Nit: extension -> extensions
Alexander Yashkin
2017/02/07 08:09:19
Done.
| |
| 1534 auto turl_data = GenerateDummyTemplateURLData("extension"); | |
| 1535 turl_data->safe_for_autoreplace = false; | |
| 1536 | |
| 1537 // Add non default extension engine. | |
| 1538 auto ext_dse = base::MakeUnique<TemplateURL>( | |
| 1539 *turl_data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1540 auto extension_info = | |
| 1541 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("extension_id1"); | |
| 1542 extension_info->wants_to_be_default_engine = false; | |
| 1543 | |
| 1544 auto extension1 = model()->AddExtensionControlledTURL( | |
|
Peter Kasting
2017/02/07 00:42:44
Nit: This should not use auto; it's a raw pointer
Alexander Yashkin
2017/02/07 08:09:19
Done.
| |
| 1545 std::move(ext_dse), std::move(extension_info)); | |
| 1546 | |
| 1547 // Add default extension engine. | |
| 1548 ext_dse = base::MakeUnique<TemplateURL>( | |
| 1549 *turl_data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1550 extension_info = | |
| 1551 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("extension_id2"); | |
| 1552 extension_info->wants_to_be_default_engine = true; | |
| 1553 | |
| 1554 model()->AddExtensionControlledTURL(std::move(ext_dse), | |
|
Peter Kasting
2017/02/07 00:42:45
Nit: Shouldn't this store the result in a temp |ex
Alexander Yashkin
2017/02/07 08:09:20
Done.
| |
| 1555 std::move(extension_info)); | |
| 1556 | |
| 1557 // Add another non default extension with same keyword. This action must not | |
| 1558 // change default extension keyword due to conflict. | |
| 1559 ext_dse = base::MakeUnique<TemplateURL>( | |
| 1560 *turl_data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1561 extension_info = | |
| 1562 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("extension_id3"); | |
| 1563 extension_info->wants_to_be_default_engine = false; | |
| 1564 | |
| 1565 auto extension3 = model()->AddExtensionControlledTURL( | |
| 1566 std::move(ext_dse), std::move(extension_info)); | |
| 1567 // Wait for any saves to finish. | |
|
Peter Kasting
2017/02/07 00:42:45
Nit: Blank line above this
Alexander Yashkin
2017/02/07 08:09:19
Done
| |
| 1568 base::RunLoop().RunUntilIdle(); | |
| 1569 | |
| 1570 // Check that extension2 is set as default. Its keyword must not be changed. | |
| 1571 auto current_dse = model()->GetDefaultSearchProvider(); | |
| 1572 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); | |
| 1573 ExpectSimilar(turl_data.get(), ¤t_dse->data()); | |
| 1574 EXPECT_EQ(current_dse, | |
| 1575 model()->FindTemplateURLForExtension( | |
| 1576 "extension_id2", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)); | |
| 1577 | |
| 1578 // Check that non default extensions keywords are updated. | |
| 1579 EXPECT_NE(extension1->keyword(), turl_data->keyword()); | |
| 1580 EXPECT_NE(extension3->keyword(), turl_data->keyword()); | |
| 1581 EXPECT_NE(extension1->keyword(), extension3->keyword()); | |
| 1582 } | |
| 1583 | |
| 1484 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { | 1584 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { |
| 1485 // Set a managed preference that establishes a default search provider. | 1585 // Set a managed preference that establishes a default search provider. |
| 1486 std::unique_ptr<TemplateURLData> managed = CreateTestSearchEngine(); | 1586 std::unique_ptr<TemplateURLData> managed = CreateTestSearchEngine(); |
| 1487 SetManagedDefaultSearchPreferences(*managed, true, test_util()->profile()); | 1587 SetManagedDefaultSearchPreferences(*managed, true, test_util()->profile()); |
| 1488 test_util()->VerifyLoad(); | 1588 test_util()->VerifyLoad(); |
| 1489 // Verify that the default manager we are getting is the managed one. | 1589 // Verify that the default manager we are getting is the managed one. |
| 1490 auto expected_managed_default = base::MakeUnique<TemplateURL>(*managed); | 1590 auto expected_managed_default = base::MakeUnique<TemplateURL>(*managed); |
| 1491 EXPECT_TRUE(model()->is_default_search_managed()); | 1591 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1492 const TemplateURL* actual_managed_default = | 1592 const TemplateURL* actual_managed_default = |
| 1493 model()->GetDefaultSearchProvider(); | 1593 model()->GetDefaultSearchProvider(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1542 TemplateURL* update_url = | 1642 TemplateURL* update_url = |
| 1543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1643 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
| 1544 const base::Time update_last_modified = update_url->last_modified(); | 1644 const base::Time update_last_modified = update_url->last_modified(); |
| 1545 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1645 model()->SetUserSelectedDefaultSearchProvider(update_url); |
| 1546 TemplateURL* reloaded_url = | 1646 TemplateURL* reloaded_url = |
| 1547 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1647 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
| 1548 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1648 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
| 1549 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1649 EXPECT_NE(original_last_modified, reloaded_last_modified); |
| 1550 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1650 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
| 1551 } | 1651 } |
| OLD | NEW |