Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/search_engines/desktop_search_utils.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/feature_list.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "components/search_engines/prepopulated_engines.h" | |
| 15 #include "components/search_engines/template_url.h" | |
| 16 #include "components/search_engines/template_url_prepopulate_data.h" | |
| 17 #include "components/search_engines/template_url_service.h" | |
| 18 #include "components/search_engines/util.h" | |
| 19 #include "components/syncable_prefs/testing_pref_service_syncable.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace { | |
|
Peter Kasting
2016/01/22 00:38:57
Nit: Blank line below this and above the correspon
fdoray
2016/02/01 15:15:54
Done.
| |
| 24 struct ReplaceDesktopSearchURLTestData { | |
| 25 // Value of the |url| argument of | |
| 26 // ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded on input. | |
| 27 const char* input_arg; | |
| 28 | |
| 29 // Expected value of the |url| argument of | |
| 30 // ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded on output. | |
| 31 const char* expected_output_arg; | |
| 32 }; | |
| 33 | |
| 34 struct ShouldReplaceDesktopSearchURLTestData { | |
| 35 bool feature_enabled; | |
| 36 bool default_search_engine_is_bing; | |
| 37 const GURL* expected_output_arg; | |
| 38 }; | |
| 39 } // namespace | |
| 40 | |
| 41 class DesktopSearchUtilsTest : public testing::Test { | |
| 42 public: | |
| 43 DesktopSearchUtilsTest() : template_url_service_(nullptr, 0) { | |
| 44 RegisterDesktopSearchRedirectionPref(prefs_.registry()); | |
| 45 } | |
| 46 | |
| 47 protected: | |
| 48 void SetFeatureEnabled(bool enabled) { | |
| 49 base::FeatureList::ClearInstanceForTesting(); | |
| 50 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 51 if (enabled) { | |
| 52 feature_list->InitializeFromCommandLine( | |
| 53 kDesktopSearchRedirectionFeature.name, std::string()); | |
| 54 } | |
| 55 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 56 } | |
| 57 | |
| 58 void SetDefaultSearchEngine( | |
| 59 const TemplateURLPrepopulateData::PrepopulatedEngine | |
| 60 default_search_engine) { | |
| 61 scoped_ptr<TemplateURLData> template_url_data = | |
| 62 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( | |
| 63 default_search_engine); | |
| 64 TemplateURL template_url(*template_url_data); | |
| 65 template_url_service_.SetUserSelectedDefaultSearchProvider(&template_url); | |
| 66 } | |
| 67 | |
| 68 TemplateURLService template_url_service_; | |
| 69 syncable_prefs::TestingPrefServiceSyncable prefs_; | |
| 70 | |
| 71 private: | |
| 72 DISALLOW_COPY_AND_ASSIGN(DesktopSearchUtilsTest); | |
| 73 }; | |
| 74 | |
| 75 TEST_F(DesktopSearchUtilsTest, ReplaceDesktopSearchURL) { | |
|
Peter Kasting
2016/01/22 00:38:57
Nit: Add a brief comment above each test describin
fdoray
2016/02/01 15:15:54
Done.
| |
| 76 const std::string default_search_url_keyword( | |
| 77 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword") | |
| 78 .spec()); | |
| 79 const std::string default_search_url_special_chars( | |
| 80 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"\xE8 \xE9") | |
| 81 .spec()); | |
| 82 | |
| 83 const ReplaceDesktopSearchURLTestData test_data[] = { | |
| 84 {"https://www.google.com/", "https://www.google.com/"}, | |
| 85 {"https://www.bing.com/search/", "https://www.bing.com/search/"}, | |
| 86 {"https://www.bing.com/search?q=keyword&form=QBLH", | |
| 87 "https://www.bing.com/search?q=keyword&form=QBLH"}, | |
| 88 {"https://www.bing.com/search?q=keyword&form=WNSGPH", | |
| 89 default_search_url_keyword.c_str()}, | |
| 90 {"https://www.bing.com/search?q=keyword&form=WNSBOX", | |
| 91 default_search_url_keyword.c_str()}, | |
| 92 {"https://www.bing.com/search?q=keyword&FORM=WNSGPH", | |
| 93 default_search_url_keyword.c_str()}, | |
| 94 {"https://www.bing.com/search?q=keyword&FORM=WNSBOX", | |
| 95 default_search_url_keyword.c_str()}, | |
| 96 {"https://www.bing.com/search?form=WNSGPH&q=keyword", | |
| 97 default_search_url_keyword.c_str()}, | |
| 98 {"https://www.bing.com/search?q=keyword&form=WNSGPH&other=stuff", | |
| 99 default_search_url_keyword.c_str()}, | |
| 100 {"https://www.bing.com/search?q=%C3%A8+%C3%A9&form=WNSGPH", | |
| 101 default_search_url_special_chars.c_str()}, | |
| 102 }; | |
| 103 | |
| 104 SetFeatureEnabled(true); | |
| 105 | |
| 106 for (const auto& test : test_data) { | |
| 107 GURL url(test.input_arg); | |
| 108 ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded( | |
| 109 &prefs_, &template_url_service_, &url); | |
| 110 EXPECT_EQ(test.expected_output_arg, url.spec()); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 TEST_F(DesktopSearchUtilsTest, ShouldReplaceDesktopSearchURL) { | |
| 115 SetDefaultSearchEngine(TemplateURLPrepopulateData::google); | |
| 116 const GURL desktop_search_url( | |
| 117 L"https://www.bing.com/search?q=keyword&form=WNSGPH"); | |
| 118 const GURL default_search_url( | |
| 119 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword")); | |
| 120 | |
| 121 const ShouldReplaceDesktopSearchURLTestData test_data[] = { | |
| 122 {false, false, &desktop_search_url}, | |
| 123 {true, false, &default_search_url}, | |
| 124 {false, true, &desktop_search_url}, | |
| 125 {true, true, &desktop_search_url}, | |
| 126 }; | |
| 127 | |
| 128 for (const auto& test : test_data) { | |
| 129 SetFeatureEnabled(test.feature_enabled); | |
| 130 SetDefaultSearchEngine(test.default_search_engine_is_bing | |
| 131 ? TemplateURLPrepopulateData::bing | |
| 132 : TemplateURLPrepopulateData::google); | |
| 133 | |
| 134 // Check whether the desktop search URL is replaced. | |
| 135 GURL url(desktop_search_url); | |
| 136 ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded( | |
| 137 &prefs_, &template_url_service_, &url); | |
| 138 EXPECT_EQ(*test.expected_output_arg, url); | |
| 139 } | |
| 140 } | |
| OLD | NEW |