Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_startup_browsertest.cc
blobad5f1abafb580f96e2fdcebd77ac9703804ef750
1 // Copyright (c) 2012 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.
5 #include <vector>
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_util.h"
16 #include "chrome/browser/extensions/shared_user_script_master.h"
17 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "extensions/browser/extension_registry.h"
33 #include "extensions/browser/extension_system.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/extension_set.h"
36 #include "extensions/common/feature_switch.h"
37 #include "net/base/filename_util.h"
39 using extensions::FeatureSwitch;
41 // This file contains high-level startup tests for the extensions system. We've
42 // had many silly bugs where command line flags did not get propagated correctly
43 // into the services, so we didn't start correctly.
45 class ExtensionStartupTestBase : public InProcessBrowserTest {
46 public:
47 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) {
48 num_expected_extensions_ = 3;
51 protected:
52 // InProcessBrowserTest
53 void SetUpCommandLine(base::CommandLine* command_line) override {
54 if (load_extensions_.empty()) {
55 // If no |load_extensions_| were specified, allow unauthenticated
56 // extension settings to be loaded from Preferences as if they had been
57 // authenticated correctly before they were handed to the ExtensionSystem.
58 command_line->AppendSwitchASCII(
59 switches::kForceFieldTrials,
60 base::StringPrintf(
61 "%s/%s/",
62 chrome_prefs::internals::kSettingsEnforcementTrialName,
63 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement));
64 #if defined(OFFICIAL_BUILD) && defined(OS_WIN)
65 // In Windows official builds, it is not possible to disable settings
66 // authentication.
67 unauthenticated_load_allowed_ = false;
68 #endif
69 } else {
70 base::FilePath::StringType paths =
71 base::JoinString(load_extensions_,
72 base::FilePath::StringType(1, ','));
73 command_line->AppendSwitchNative(switches::kLoadExtension,
74 paths);
75 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
79 bool SetUpUserDataDirectory() override {
80 base::FilePath profile_dir;
81 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
82 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
83 base::CreateDirectory(profile_dir);
85 preferences_file_ = profile_dir.Append(chrome::kPreferencesFilename);
86 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
87 extensions_dir_ = profile_dir.AppendASCII("Extensions");
89 if (load_extensions_.empty()) {
90 base::FilePath src_dir;
91 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
92 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
94 base::CopyFile(src_dir.Append(chrome::kPreferencesFilename),
95 preferences_file_);
96 base::CopyDirectory(src_dir.AppendASCII("Extensions"),
97 profile_dir, true); // recursive
99 return true;
102 void SetUpInProcessBrowserTestFixture() override {
103 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
105 // Bots are on a domain, turn off the domain check for settings hardening in
106 // order to be able to test all SettingsEnforcement groups.
107 chrome_prefs::DisableDomainCheckForTesting();
110 void TearDown() override {
111 EXPECT_TRUE(base::DeleteFile(preferences_file_, false));
113 // TODO(phajdan.jr): Check return values of the functions below, carefully.
114 base::DeleteFile(user_scripts_dir_, true);
115 base::DeleteFile(extensions_dir_, true);
117 InProcessBrowserTest::TearDown();
120 void WaitForServicesToStart(int num_expected_extensions,
121 bool expect_extensions_enabled) {
122 extensions::ExtensionRegistry* registry =
123 extensions::ExtensionRegistry::Get(browser()->profile());
125 // Count the number of non-component extensions.
126 int found_extensions = 0;
127 for (const scoped_refptr<const extensions::Extension>& extension :
128 registry->enabled_extensions()) {
129 if (extension->location() != extensions::Manifest::COMPONENT)
130 found_extensions++;
133 if (!unauthenticated_load_allowed_)
134 num_expected_extensions = 0;
136 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
137 static_cast<uint32>(found_extensions));
139 ExtensionService* service = extensions::ExtensionSystem::Get(
140 browser()->profile())->extension_service();
141 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
143 content::WindowedNotificationObserver user_scripts_observer(
144 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
145 content::NotificationService::AllSources());
146 extensions::SharedUserScriptMaster* master =
147 extensions::ExtensionSystem::Get(browser()->profile())->
148 shared_user_script_master();
149 if (!master->scripts_ready())
150 user_scripts_observer.Wait();
151 ASSERT_TRUE(master->scripts_ready());
154 void TestInjection(bool expect_css, bool expect_script) {
155 if (!unauthenticated_load_allowed_) {
156 expect_css = false;
157 expect_script = false;
160 // Load a page affected by the content script and test to see the effect.
161 base::FilePath test_file;
162 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
163 test_file = test_file.AppendASCII("extensions")
164 .AppendASCII("test_file.html");
166 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
168 bool result = false;
169 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
170 browser()->tab_strip_model()->GetActiveWebContents(),
171 "window.domAutomationController.send("
172 " document.defaultView.getComputedStyle(document.body, null)."
173 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
174 &result));
175 EXPECT_EQ(expect_css, result);
177 result = false;
178 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
179 browser()->tab_strip_model()->GetActiveWebContents(),
180 "window.domAutomationController.send(document.title == 'Modified')",
181 &result));
182 EXPECT_EQ(expect_script, result);
185 base::FilePath preferences_file_;
186 base::FilePath extensions_dir_;
187 base::FilePath user_scripts_dir_;
188 // True unless unauthenticated extension settings are not allowed to be
189 // loaded in this configuration.
190 bool unauthenticated_load_allowed_;
191 // Extensions to load from the command line.
192 std::vector<base::FilePath::StringType> load_extensions_;
194 int num_expected_extensions_;
198 // ExtensionsStartupTest
199 // Ensures that we can startup the browser with --enable-extensions and some
200 // extensions installed and see them run and do basic things.
201 typedef ExtensionStartupTestBase ExtensionsStartupTest;
203 // Broken in official builds, http://crbug.com/474659
204 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, DISABLED_Test) {
205 WaitForServicesToStart(num_expected_extensions_, true);
206 TestInjection(true, true);
209 // Broken in official builds, http://crbug.com/474659
210 // Sometimes times out on Mac. http://crbug.com/48151
211 // Tests that disallowing file access on an extension prevents it from injecting
212 // script into a page with a file URL.
213 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, DISABLED_NoFileAccess) {
214 WaitForServicesToStart(num_expected_extensions_, true);
216 // Keep a separate list of extensions for which to disable file access, since
217 // doing so reloads them.
218 std::vector<const extensions::Extension*> extension_list;
220 extensions::ExtensionRegistry* registry =
221 extensions::ExtensionRegistry::Get(browser()->profile());
222 for (extensions::ExtensionSet::const_iterator it =
223 registry->enabled_extensions().begin();
224 it != registry->enabled_extensions().end(); ++it) {
225 if ((*it)->location() == extensions::Manifest::COMPONENT)
226 continue;
227 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile()))
228 extension_list.push_back(it->get());
231 for (size_t i = 0; i < extension_list.size(); ++i) {
232 content::WindowedNotificationObserver user_scripts_observer(
233 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
234 content::NotificationService::AllSources());
235 extensions::util::SetAllowFileAccess(
236 extension_list[i]->id(), browser()->profile(), false);
237 user_scripts_observer.Wait();
240 TestInjection(false, false);
243 // ExtensionsLoadTest
244 // Ensures that we can startup the browser with --load-extension and see them
245 // run.
246 class ExtensionsLoadTest : public ExtensionStartupTestBase {
247 public:
248 ExtensionsLoadTest() {
249 base::FilePath one_extension_path;
250 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
251 one_extension_path = one_extension_path
252 .AppendASCII("extensions")
253 .AppendASCII("good")
254 .AppendASCII("Extensions")
255 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
256 .AppendASCII("1.0.0.0");
257 load_extensions_.push_back(one_extension_path.value());
261 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
262 WaitForServicesToStart(1, true);
263 TestInjection(true, true);
266 // ExtensionsLoadMultipleTest
267 // Ensures that we can startup the browser with multiple extensions
268 // via --load-extension=X1,X2,X3.
269 class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
270 public:
271 ExtensionsLoadMultipleTest() {
272 base::FilePath one_extension_path;
273 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
274 one_extension_path = one_extension_path
275 .AppendASCII("extensions")
276 .AppendASCII("good")
277 .AppendASCII("Extensions")
278 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
279 .AppendASCII("1.0.0.0");
280 load_extensions_.push_back(one_extension_path.value());
282 base::FilePath second_extension_path;
283 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
284 second_extension_path = second_extension_path
285 .AppendASCII("extensions")
286 .AppendASCII("app");
287 load_extensions_.push_back(second_extension_path.value());
289 base::FilePath third_extension_path;
290 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
291 third_extension_path = third_extension_path
292 .AppendASCII("extensions")
293 .AppendASCII("app1");
294 load_extensions_.push_back(third_extension_path.value());
296 base::FilePath fourth_extension_path;
297 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
298 fourth_extension_path = fourth_extension_path
299 .AppendASCII("extensions")
300 .AppendASCII("app2");
301 load_extensions_.push_back(fourth_extension_path.value());
305 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
306 WaitForServicesToStart(4, true);
307 TestInjection(true, true);