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 "base/run_loop.h" | |
| 6 #include "base/strings/string_split.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 10 #include "chrome/test/base/search_test_utils.h" | |
| 11 #include "components/policy/core/browser/browser_policy_connector.h" | |
| 12 #include "components/policy/core/common/mock_configuration_policy_provider.h" | |
| 13 #include "components/policy/core/common/policy_details.h" | |
| 14 #include "components/policy/core/common/policy_map.h" | |
| 15 #include "components/policy/core/common/policy_types.h" | |
| 16 #include "components/policy/core/common/schema.h" | |
| 17 #include "components/policy/policy_constants.h" | |
| 18 #include "components/search_engines/default_search_manager.h" | |
| 19 #include "components/search_engines/search_engines_test_util.h" | |
| 20 #include "components/search_engines/template_url_data.h" | |
| 21 #include "components/search_engines/template_url_prepopulate_data.h" | |
| 22 #include "components/search_engines/template_url_service.h" | |
| 23 #include "components/version_info/version_info.h" | |
| 24 #include "extensions/common/features/feature_channel.h" | |
| 25 | |
| 26 namespace { | |
| 27 // TemplateURLData with search engines settings from test extension manifest. | |
| 28 std::unique_ptr<TemplateURLData> TestExtensionSearchEngine(PrefService* prefs) { | |
| 29 auto result = base::MakeUnique<TemplateURLData>(); | |
| 30 result->SetShortName(base::ASCIIToUTF16("name.de")); | |
| 31 result->SetKeyword(base::ASCIIToUTF16("keyword.de")); | |
| 32 result->SetURL("http://www.foo.de/s?q={searchTerms}&id=10"); | |
| 33 result->favicon_url = GURL("http://www.foo.de/favicon.ico?id=10"); | |
| 34 result->suggestions_url = "http://www.foo.de/suggest?q={searchTerms}&id=10"; | |
| 35 result->instant_url = "http://www.foo.de/instant?q={searchTerms}&id=10"; | |
| 36 result->image_url = "http://www.foo.de/image?q={searchTerms}&id=10"; | |
| 37 result->search_url_post_params = "search_lang=de"; | |
| 38 result->suggestions_url_post_params = "suggest_lang=de"; | |
| 39 result->instant_url_post_params = "instant_lang=de"; | |
| 40 result->image_url_post_params = "image_lang=de"; | |
| 41 result->alternate_urls.push_back("http://www.moo.de/s?q={searchTerms}&id=10"); | |
| 42 result->alternate_urls.push_back("http://www.noo.de/s?q={searchTerms}&id=10"); | |
| 43 result->input_encodings.push_back("UTF-8"); | |
| 44 | |
| 45 std::unique_ptr<TemplateURLData> google_engine( | |
| 46 TemplateURLPrepopulateData::GetPrepopulatedEngine(prefs, 1)); | |
| 47 // Values below are taken for Google engine settings, because extension | |
| 48 // manifest has prepopulate_id 1, which is Google. | |
| 49 result->search_terms_replacement_key = | |
| 50 google_engine->search_terms_replacement_key; | |
| 51 result->contextual_search_url = google_engine->contextual_search_url; | |
| 52 result->new_tab_url = google_engine->new_tab_url; | |
| 53 return result; | |
| 54 } | |
| 55 } // namespace | |
| 56 | |
| 57 // Browser test to check how DefaultSearchManager interacts with policy and | |
| 58 // extension overriden DSE. | |
| 59 class DefaultSearchManagerBrowserTest : public ExtensionBrowserTest { | |
| 60 protected: | |
| 61 void SetUpInProcessBrowserTestFixture() override { | |
| 62 EXPECT_CALL(provider_, IsInitializationComplete(testing::_)) | |
| 63 .WillRepeatedly(testing::Return(true)); | |
| 64 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | |
| 65 } | |
| 66 | |
| 67 void SetUpOnMainThread() override { | |
| 68 ExtensionBrowserTest::SetUpOnMainThread(); | |
| 69 search_test_utils::WaitForTemplateURLServiceToLoad( | |
| 70 TemplateURLServiceFactory::GetForProfile(browser()->profile())); | |
| 71 } | |
| 72 | |
| 73 void TearDownOnMainThread() override { ClearProviderPolicy(); } | |
| 74 | |
| 75 void ClearProviderPolicy() { | |
| 76 provider_.UpdateChromePolicy(policy::PolicyMap()); | |
| 77 base::RunLoop().RunUntilIdle(); | |
| 78 } | |
| 79 | |
| 80 void OverrideDefaultSearchByPolicy(const TemplateURLData& data) { | |
| 81 // Override the default search provider using policies. | |
| 82 policy::PolicyMap policies; | |
| 83 BuildDefaultSearchPolicy(data, &policies); | |
| 84 provider_.UpdateChromePolicy(policies); | |
| 85 base::RunLoop().RunUntilIdle(); | |
|
vasilii
2016/11/30 14:00:02
RunUntilIdle() is dangerous in the browser tests b
Alexander Yashkin
2016/12/05 18:16:48
Removed browser test, returned unittest.
| |
| 86 } | |
| 87 policy::MockConfigurationPolicyProvider provider_; | |
| 88 }; | |
| 89 | |
| 90 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 91 // Test that DefaultSearchManager handles extension-controlled DSEs correctly. | |
| 92 IN_PROC_BROWSER_TEST_F(DefaultSearchManagerBrowserTest, ExtensionsOverrides) { | |
|
vasilii
2016/11/30 14:00:02
I don't see why you need a separate file for this.
Alexander Yashkin
2016/12/05 18:16:48
Removed this browser test.
Returned to default_sea
| |
| 93 Profile* profile = browser()->profile(); | |
| 94 DefaultSearchManager manager(profile->GetPrefs(), | |
| 95 DefaultSearchManager::ObserverCallback()); | |
| 96 std::unique_ptr<TemplateURLData> user_data = | |
| 97 GenerateDummyTemplateURLData("user"); | |
| 98 manager.SetUserSelectedDefaultSearchEngine(*user_data); | |
| 99 | |
| 100 DefaultSearchManager::Source source = DefaultSearchManager::FROM_FALLBACK; | |
| 101 ExpectSimilar(user_data.get(), manager.GetDefaultSearchEngine(&source)); | |
| 102 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); | |
| 103 | |
| 104 #if defined(OS_MACOSX) | |
| 105 // On Mac, this API is limited to trunk. | |
| 106 extensions::ScopedCurrentChannel scoped_channel( | |
| 107 version_info::Channel::UNKNOWN); | |
| 108 #endif // OS_MACOSX | |
| 109 | |
| 110 // Install extension that overrides DSE. | |
| 111 const extensions::Extension* extension = LoadExtensionWithInstallParam( | |
| 112 test_data_dir_.AppendASCII("settings_override"), kFlagEnableFileAccess, | |
| 113 "10"); | |
| 114 | |
| 115 auto extension_dse(TestExtensionSearchEngine(profile->GetPrefs())); | |
| 116 ExpectSimilar(extension_dse.get(), manager.GetDefaultSearchEngine(&source)); | |
| 117 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source); | |
| 118 | |
| 119 // Check that policy overrides extension DSE. | |
| 120 std::unique_ptr<TemplateURLData> policy_data = | |
| 121 GenerateDummyTemplateURLData("policy"); | |
| 122 OverrideDefaultSearchByPolicy(*policy_data); | |
| 123 | |
| 124 ExpectSimilar(policy_data.get(), manager.GetDefaultSearchEngine(&source)); | |
| 125 EXPECT_EQ(DefaultSearchManager::FROM_POLICY, source); | |
| 126 | |
| 127 ClearProviderPolicy(); | |
| 128 // After policy is cleared expect that extension DSE is returned. | |
| 129 ExpectSimilar(extension_dse.get(), manager.GetDefaultSearchEngine(&source)); | |
| 130 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source); | |
| 131 | |
| 132 UnloadExtension(extension->id()); | |
| 133 // After extension unload expect that user DSE is default. | |
| 134 ExpectSimilar(user_data.get(), manager.GetDefaultSearchEngine(&source)); | |
| 135 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); | |
| 136 } | |
| 137 #endif | |
| OLD | NEW |