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_win.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 { |
| 24 struct ReplaceDesktopSearchURLTestData { |
| 25 // Value of the |url| argument of |
| 26 // ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded on input. |
| 27 const base::char16* input_arg; |
| 28 |
| 29 // Expected value for the |url| argument on output, or nullptr if it should |
| 30 // remain unchanged. |
| 31 const GURL* 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 WindowsDesktopSearchTest : public testing::Test { |
| 42 public: |
| 43 WindowsDesktopSearchTest() : template_url_service_(nullptr, 0) { |
| 44 RegisterWindowsDesktopSearchRedirectionPref(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 kWindowsDesktopSearchRedirectionFeature.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(WindowsDesktopSearchTest); |
| 73 }; |
| 74 |
| 75 TEST_F(WindowsDesktopSearchTest, ReplaceDesktopSearchURL) { |
| 76 const GURL default_search_url_keyword( |
| 77 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword")); |
| 78 const GURL default_search_url_special_chars( |
| 79 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"\xE8 \xE9")); |
| 80 |
| 81 const ReplaceDesktopSearchURLTestData test_data[] = { |
| 82 {L"https://www.google.com", nullptr}, |
| 83 {L"https://www.bing.com/search", nullptr}, |
| 84 {L"https://www.bing.com/search?q=keyword&form=QBLH", nullptr}, |
| 85 {L"https://www.bing.com/search?q=keyword&form=WNSGPH", |
| 86 &default_search_url_keyword}, |
| 87 {L"https://www.bing.com/search?q=keyword&form=WNSBOX", |
| 88 &default_search_url_keyword}, |
| 89 {L"https://www.bing.com/search?q=keyword&FORM=WNSGPH", |
| 90 &default_search_url_keyword}, |
| 91 {L"https://www.bing.com/search?q=keyword&FORM=WNSBOX", |
| 92 &default_search_url_keyword}, |
| 93 {L"https://www.bing.com/search?form=WNSGPH&q=keyword", |
| 94 &default_search_url_keyword}, |
| 95 {L"https://www.bing.com/search?q=keyword&form=WNSGPH&other=stuff", |
| 96 &default_search_url_keyword}, |
| 97 {L"https://www.bing.com/search?q=%C3%A8+%C3%A9&form=WNSGPH", |
| 98 &default_search_url_special_chars}, |
| 99 }; |
| 100 |
| 101 SetFeatureEnabled(true); |
| 102 |
| 103 for (const auto& test : test_data) { |
| 104 GURL url(test.input_arg); |
| 105 bool should_show_infobar; |
| 106 ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 107 &prefs_, &template_url_service_, &url, &should_show_infobar); |
| 108 |
| 109 const GURL expected_output_arg(test.expected_output_arg == nullptr |
| 110 ? GURL(test.input_arg) |
| 111 : *test.expected_output_arg); |
| 112 EXPECT_EQ(expected_output_arg, url); |
| 113 } |
| 114 } |
| 115 |
| 116 TEST_F(WindowsDesktopSearchTest, ShouldReplaceDesktopSearchURL) { |
| 117 SetDefaultSearchEngine(TemplateURLPrepopulateData::google); |
| 118 const GURL desktop_search_url( |
| 119 L"https://www.bing.com/search?q=keyword&form=WNSGPH"); |
| 120 const GURL default_search_url( |
| 121 GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword")); |
| 122 |
| 123 const ShouldReplaceDesktopSearchURLTestData test_data[] = { |
| 124 {false, false, &desktop_search_url}, |
| 125 {true, false, &default_search_url}, |
| 126 {false, true, &desktop_search_url}, |
| 127 {true, true, &desktop_search_url}, |
| 128 }; |
| 129 |
| 130 for (const auto& test : test_data) { |
| 131 SetFeatureEnabled(test.feature_enabled); |
| 132 SetDefaultSearchEngine(test.default_search_engine_is_bing |
| 133 ? TemplateURLPrepopulateData::bing |
| 134 : TemplateURLPrepopulateData::google); |
| 135 |
| 136 // Check whether the desktop search URL is replaced. |
| 137 const GURL expected_return_value( |
| 138 test.expected_output_arg == &default_search_url ? desktop_search_url |
| 139 : GURL()); |
| 140 GURL url(desktop_search_url); |
| 141 bool should_show_infobar; |
| 142 ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 143 &prefs_, &template_url_service_, &url, &should_show_infobar); |
| 144 EXPECT_EQ(*test.expected_output_arg, url); |
| 145 } |
| 146 } |
| 147 |
| 148 TEST_F(WindowsDesktopSearchTest, ShouldShowInfobar) { |
| 149 SetFeatureEnabled(true); |
| 150 |
| 151 // An infobar should be shown only the first time that a desktop search is |
| 152 // redirected to the default search engine. |
| 153 GURL dummy_url(L"http://www.google.com"); |
| 154 bool should_show_infobar; |
| 155 ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 156 &prefs_, &template_url_service_, &dummy_url, &should_show_infobar); |
| 157 EXPECT_FALSE(should_show_infobar); |
| 158 |
| 159 GURL desktop_search_url(L"https://www.bing.com/search?q=keyword&form=WNSGPH"); |
| 160 ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 161 &prefs_, &template_url_service_, &desktop_search_url, |
| 162 &should_show_infobar); |
| 163 EXPECT_TRUE(should_show_infobar); |
| 164 ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 165 &prefs_, &template_url_service_, &desktop_search_url, |
| 166 &should_show_infobar); |
| 167 EXPECT_FALSE(should_show_infobar); |
| 168 } |
OLD | NEW |