Chromium Code Reviews| Index: components/search_engines/desktop_search_utils_win_unittest.cc |
| diff --git a/components/search_engines/desktop_search_utils_win_unittest.cc b/components/search_engines/desktop_search_utils_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7551b1b0946b1107fc746552040983a0092df31b |
| --- /dev/null |
| +++ b/components/search_engines/desktop_search_utils_win_unittest.cc |
| @@ -0,0 +1,168 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/search_engines/desktop_search_utils_win.h" |
| + |
| +#include <string> |
| + |
| +#include "base/feature_list.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/string16.h" |
| +#include "components/search_engines/prepopulated_engines.h" |
| +#include "components/search_engines/template_url.h" |
| +#include "components/search_engines/template_url_prepopulate_data.h" |
| +#include "components/search_engines/template_url_service.h" |
| +#include "components/search_engines/util.h" |
| +#include "components/syncable_prefs/testing_pref_service_syncable.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| +struct ReplaceDesktopSearchURLTestData { |
| + // Value of the |url| argument of |
| + // ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded on input. |
| + const base::char16* input_arg; |
| + |
| + // Expected value for the |url| argument on output, or nullptr if it should |
| + // remain unchanged. |
| + const GURL* expected_output_arg; |
| +}; |
| + |
| +struct ShouldReplaceDesktopSearchURLTestData { |
| + bool feature_enabled; |
| + bool default_search_engine_is_bing; |
| + const GURL* expected_output_arg; |
| +}; |
| +} // namespace |
| + |
| +class WindowsDesktopSearchTest : public testing::Test { |
| + public: |
| + WindowsDesktopSearchTest() : template_url_service_(nullptr, 0) { |
| + RegisterWindowsDesktopSearchRedirectionPref(prefs_.registry()); |
| + } |
| + |
| + protected: |
| + void SetFeatureEnabled(bool enabled) { |
| + base::FeatureList::ClearInstanceForTesting(); |
| + scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| + if (enabled) { |
| + feature_list->InitializeFromCommandLine( |
| + kWindowsDesktopSearchRedirectionFeature.name, std::string()); |
| + } |
| + base::FeatureList::SetInstance(std::move(feature_list)); |
| + } |
| + |
| + void SetDefaultSearchEngine( |
| + const TemplateURLPrepopulateData::PrepopulatedEngine |
| + default_search_engine) { |
| + scoped_ptr<TemplateURLData> template_url_data = |
| + TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( |
| + default_search_engine); |
| + TemplateURL template_url(*template_url_data); |
| + template_url_service_.SetUserSelectedDefaultSearchProvider(&template_url); |
| + } |
| + |
| + TemplateURLService template_url_service_; |
| + syncable_prefs::TestingPrefServiceSyncable prefs_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(WindowsDesktopSearchTest); |
| +}; |
| + |
| +TEST_F(WindowsDesktopSearchTest, ReplaceDesktopSearchURL) { |
| + const GURL default_search_url_keyword( |
| + GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword")); |
| + const GURL default_search_url_special_chars( |
| + GetDefaultSearchURLForSearchTerms(&template_url_service_, L"\xE8 \xE9")); |
| + |
| + const ReplaceDesktopSearchURLTestData test_data[] = { |
| + {L"https://www.google.com", nullptr}, |
|
Peter Kasting
2016/01/20 03:03:02
Nit: I think this would be clearer if we just had
fdoray
2016/01/21 21:02:34
Done.
|
| + {L"https://www.bing.com/search", nullptr}, |
| + {L"https://www.bing.com/search?q=keyword&form=QBLH", nullptr}, |
| + {L"https://www.bing.com/search?q=keyword&form=WNSGPH", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?q=keyword&form=WNSBOX", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?q=keyword&FORM=WNSGPH", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?q=keyword&FORM=WNSBOX", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?form=WNSGPH&q=keyword", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?q=keyword&form=WNSGPH&other=stuff", |
| + &default_search_url_keyword}, |
| + {L"https://www.bing.com/search?q=%C3%A8+%C3%A9&form=WNSGPH", |
| + &default_search_url_special_chars}, |
| + }; |
| + |
| + SetFeatureEnabled(true); |
| + |
| + for (const auto& test : test_data) { |
| + GURL url(test.input_arg); |
| + bool should_show_infobar; |
| + ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + &prefs_, &template_url_service_, &url, &should_show_infobar); |
| + |
| + const GURL expected_output_arg(test.expected_output_arg == nullptr |
| + ? GURL(test.input_arg) |
| + : *test.expected_output_arg); |
| + EXPECT_EQ(expected_output_arg, url); |
| + } |
| +} |
| + |
| +TEST_F(WindowsDesktopSearchTest, ShouldReplaceDesktopSearchURL) { |
| + SetDefaultSearchEngine(TemplateURLPrepopulateData::google); |
| + const GURL desktop_search_url( |
| + L"https://www.bing.com/search?q=keyword&form=WNSGPH"); |
| + const GURL default_search_url( |
| + GetDefaultSearchURLForSearchTerms(&template_url_service_, L"keyword")); |
| + |
| + const ShouldReplaceDesktopSearchURLTestData test_data[] = { |
| + {false, false, &desktop_search_url}, |
| + {true, false, &default_search_url}, |
| + {false, true, &desktop_search_url}, |
| + {true, true, &desktop_search_url}, |
| + }; |
| + |
| + for (const auto& test : test_data) { |
| + SetFeatureEnabled(test.feature_enabled); |
| + SetDefaultSearchEngine(test.default_search_engine_is_bing |
| + ? TemplateURLPrepopulateData::bing |
| + : TemplateURLPrepopulateData::google); |
| + |
| + // Check whether the desktop search URL is replaced. |
| + const GURL expected_return_value( |
|
Peter Kasting
2016/01/20 03:03:02
This doesn't seem to be used.
fdoray
2016/01/21 21:02:34
Done.
|
| + test.expected_output_arg == &default_search_url ? desktop_search_url |
| + : GURL()); |
| + GURL url(desktop_search_url); |
| + bool should_show_infobar; |
| + ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + &prefs_, &template_url_service_, &url, &should_show_infobar); |
| + EXPECT_EQ(*test.expected_output_arg, url); |
| + } |
| +} |
| + |
| +TEST_F(WindowsDesktopSearchTest, ShouldShowInfobar) { |
| + SetFeatureEnabled(true); |
| + |
| + // An infobar should be shown only the first time that a desktop search is |
| + // redirected to the default search engine. |
| + GURL dummy_url(L"http://www.google.com"); |
| + bool should_show_infobar; |
| + ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + &prefs_, &template_url_service_, &dummy_url, &should_show_infobar); |
| + EXPECT_FALSE(should_show_infobar); |
| + |
| + GURL desktop_search_url(L"https://www.bing.com/search?q=keyword&form=WNSGPH"); |
| + ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + &prefs_, &template_url_service_, &desktop_search_url, |
| + &should_show_infobar); |
| + EXPECT_TRUE(should_show_infobar); |
| + ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + &prefs_, &template_url_service_, &desktop_search_url, |
| + &should_show_infobar); |
| + EXPECT_FALSE(should_show_infobar); |
| +} |