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/strings/string16.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/prefs/pref_service.h" |
| 15 #include "components/search_engines/prepopulated_engines.h" |
| 16 #include "components/search_engines/template_url.h" |
| 17 #include "components/search_engines/template_url_prepopulate_data.h" |
| 18 #include "components/search_engines/template_url_service.h" |
| 19 #include "components/search_engines/util.h" |
| 20 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 struct ReplaceDesktopSearchURLTestData { |
| 27 // Value of the |url| argument of |
| 28 // ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded on input. |
| 29 const char* input_arg; |
| 30 |
| 31 // Expected value of the |url| argument of |
| 32 // ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded on output. |
| 33 const char* expected_output_arg; |
| 34 }; |
| 35 |
| 36 struct ShouldReplaceDesktopSearchURLTestData { |
| 37 bool feature_enabled; |
| 38 bool default_search_engine_is_bing; |
| 39 const GURL* expected_output_arg; |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 class DesktopSearchUtilsTest : public testing::Test { |
| 45 public: |
| 46 DesktopSearchUtilsTest() : template_url_service_(nullptr, 0) { |
| 47 RegisterDesktopSearchRedirectionPref(prefs_.registry()); |
| 48 } |
| 49 |
| 50 protected: |
| 51 void SetFeatureEnabled(bool enabled) { |
| 52 base::FeatureList::ClearInstanceForTesting(); |
| 53 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 54 if (enabled) { |
| 55 feature_list->InitializeFromCommandLine( |
| 56 kDesktopSearchRedirectionFeature.name, std::string()); |
| 57 } |
| 58 base::FeatureList::SetInstance(std::move(feature_list)); |
| 59 } |
| 60 |
| 61 void SetDefaultSearchEngine( |
| 62 const TemplateURLPrepopulateData::PrepopulatedEngine |
| 63 default_search_engine) { |
| 64 scoped_ptr<TemplateURLData> template_url_data = |
| 65 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( |
| 66 default_search_engine); |
| 67 TemplateURL template_url(*template_url_data); |
| 68 template_url_service_.SetUserSelectedDefaultSearchProvider(&template_url); |
| 69 } |
| 70 |
| 71 TemplateURLService template_url_service_; |
| 72 syncable_prefs::TestingPrefServiceSyncable prefs_; |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(DesktopSearchUtilsTest); |
| 76 }; |
| 77 |
| 78 // Checks that ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded correctly |
| 79 // replaces the URL it receives when the desktop search redirection feature is |
| 80 // enabled and the default search engine is not Bing. |
| 81 TEST_F(DesktopSearchUtilsTest, ReplaceDesktopSearchURL) { |
| 82 const std::string default_search_url_keyword( |
| 83 GetDefaultSearchURLForSearchTerms(&template_url_service_, |
| 84 base::WideToUTF16(L"keyword")) |
| 85 .spec()); |
| 86 const std::string default_search_url_special_chars( |
| 87 GetDefaultSearchURLForSearchTerms(&template_url_service_, |
| 88 base::WideToUTF16(L"\xE8 \xE9")) |
| 89 .spec()); |
| 90 |
| 91 const ReplaceDesktopSearchURLTestData test_data[] = { |
| 92 {"https://www.google.com/", "https://www.google.com/"}, |
| 93 {"https://www.bing.com/search/", "https://www.bing.com/search/"}, |
| 94 {"https://www.bing.com/search?q=keyword&form=QBLH", |
| 95 "https://www.bing.com/search?q=keyword&form=QBLH"}, |
| 96 {"https://www.bing.com/search?q=keyword&form=WNSGPH", |
| 97 default_search_url_keyword.c_str()}, |
| 98 {"https://www.bing.com/search?q=keyword&form=WNSBOX", |
| 99 default_search_url_keyword.c_str()}, |
| 100 {"https://www.bing.com/search?q=keyword&FORM=WNSGPH", |
| 101 default_search_url_keyword.c_str()}, |
| 102 {"https://www.bing.com/search?q=keyword&FORM=WNSBOX", |
| 103 default_search_url_keyword.c_str()}, |
| 104 {"https://www.bing.com/search?form=WNSGPH&q=keyword", |
| 105 default_search_url_keyword.c_str()}, |
| 106 {"https://www.bing.com/search?q=keyword&form=WNSGPH&other=stuff", |
| 107 default_search_url_keyword.c_str()}, |
| 108 {"https://www.bing.com/search?q=%C3%A8+%C3%A9&form=WNSGPH", |
| 109 default_search_url_special_chars.c_str()}, |
| 110 }; |
| 111 |
| 112 SetFeatureEnabled(true); |
| 113 |
| 114 for (const auto& test : test_data) { |
| 115 GURL url(test.input_arg); |
| 116 ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 117 &prefs_, &template_url_service_, &url); |
| 118 EXPECT_EQ(test.expected_output_arg, url.spec()); |
| 119 } |
| 120 } |
| 121 |
| 122 // Checks that ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded doesn't |
| 123 // change the URL it receives when the desktop search redirection feature is |
| 124 // disabled or when the default search engine is Bing. |
| 125 TEST_F(DesktopSearchUtilsTest, ShouldReplaceDesktopSearchURL) { |
| 126 SetDefaultSearchEngine(TemplateURLPrepopulateData::google); |
| 127 const GURL desktop_search_url( |
| 128 "https://www.bing.com/search?q=keyword&form=WNSGPH"); |
| 129 const GURL default_search_url(GetDefaultSearchURLForSearchTerms( |
| 130 &template_url_service_, base::WideToUTF16(L"keyword"))); |
| 131 |
| 132 const ShouldReplaceDesktopSearchURLTestData test_data[] = { |
| 133 {false, false, &desktop_search_url}, |
| 134 {true, false, &default_search_url}, |
| 135 {false, true, &desktop_search_url}, |
| 136 {true, true, &desktop_search_url}, |
| 137 }; |
| 138 |
| 139 for (const auto& test : test_data) { |
| 140 SetFeatureEnabled(test.feature_enabled); |
| 141 SetDefaultSearchEngine(test.default_search_engine_is_bing |
| 142 ? TemplateURLPrepopulateData::bing |
| 143 : TemplateURLPrepopulateData::google); |
| 144 |
| 145 // Check whether the desktop search URL is replaced. |
| 146 GURL url(desktop_search_url); |
| 147 ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 148 &prefs_, &template_url_service_, &url); |
| 149 EXPECT_EQ(*test.expected_output_arg, url); |
| 150 } |
| 151 } |
OLD | NEW |