OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
14 #include "chrome/browser/extensions/user_script_master.h" | 14 #include "chrome/browser/extensions/user_script_master.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/extensions/api/requirements/requirements_handler.h" |
21 #include "chrome/common/extensions/feature_switch.h" | 22 #include "chrome/common/extensions/feature_switch.h" |
| 23 #include "chrome/common/extensions/manifest_handler.h" |
22 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
23 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
24 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
25 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
28 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
29 #include "net/base/net_util.h" | 31 #include "net/base/net_util.h" |
30 | 32 |
31 using extensions::FeatureSwitch; | |
32 | |
33 // This file contains high-level startup tests for the extensions system. We've | 33 // This file contains high-level startup tests for the extensions system. We've |
34 // had many silly bugs where command line flags did not get propagated correctly | 34 // had many silly bugs where command line flags did not get propagated correctly |
35 // into the services, so we didn't start correctly. | 35 // into the services, so we didn't start correctly. |
36 | 36 |
| 37 namespace extensions { |
| 38 |
37 class ExtensionStartupTestBase : public InProcessBrowserTest { | 39 class ExtensionStartupTestBase : public InProcessBrowserTest { |
38 public: | 40 public: |
39 ExtensionStartupTestBase() : | 41 ExtensionStartupTestBase() : |
40 enable_extensions_(false), | 42 enable_extensions_(false), |
41 override_sideload_wipeout_(FeatureSwitch::sideload_wipeout(), false) { | 43 override_sideload_wipeout_(FeatureSwitch::sideload_wipeout(), false) { |
42 num_expected_extensions_ = 3; | 44 num_expected_extensions_ = 3; |
43 } | 45 } |
44 | 46 |
45 protected: | 47 protected: |
46 // InProcessBrowserTest | 48 // InProcessBrowserTest |
47 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 50 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 51 |
48 if (!enable_extensions_) | 52 if (!enable_extensions_) |
49 command_line->AppendSwitch(switches::kDisableExtensions); | 53 command_line->AppendSwitch(switches::kDisableExtensions); |
50 | 54 |
51 if (!load_extensions_.empty()) { | 55 if (!load_extensions_.empty()) { |
52 base::FilePath::StringType paths = JoinString(load_extensions_, ','); | 56 base::FilePath::StringType paths = JoinString(load_extensions_, ','); |
53 command_line->AppendSwitchNative(switches::kLoadExtension, | 57 command_line->AppendSwitchNative(switches::kLoadExtension, |
54 paths); | 58 paths); |
55 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); | 59 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); |
56 } | 60 } |
| 61 |
| 62 (new RequirementsHandler)->Register(); |
| 63 } |
| 64 |
| 65 virtual void CleanUpOnMainThread() OVERRIDE { |
| 66 ManifestHandler::ClearRegistryForTesting(); |
| 67 InProcessBrowserTest::CleanUpOnMainThread(); |
57 } | 68 } |
58 | 69 |
59 virtual bool SetUpUserDataDirectory() OVERRIDE { | 70 virtual bool SetUpUserDataDirectory() OVERRIDE { |
60 base::FilePath profile_dir; | 71 base::FilePath profile_dir; |
61 PathService::Get(chrome::DIR_USER_DATA, &profile_dir); | 72 PathService::Get(chrome::DIR_USER_DATA, &profile_dir); |
62 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); | 73 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
63 file_util::CreateDirectory(profile_dir); | 74 file_util::CreateDirectory(profile_dir); |
64 | 75 |
65 preferences_file_ = profile_dir.AppendASCII("Preferences"); | 76 preferences_file_ = profile_dir.AppendASCII("Preferences"); |
66 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); | 77 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); |
(...skipping 17 matching lines...) Expand all Loading... |
84 | 95 |
85 // TODO(phajdan.jr): Check return values of the functions below, carefully. | 96 // TODO(phajdan.jr): Check return values of the functions below, carefully. |
86 file_util::Delete(user_scripts_dir_, true); | 97 file_util::Delete(user_scripts_dir_, true); |
87 file_util::Delete(extensions_dir_, true); | 98 file_util::Delete(extensions_dir_, true); |
88 | 99 |
89 InProcessBrowserTest::TearDown(); | 100 InProcessBrowserTest::TearDown(); |
90 } | 101 } |
91 | 102 |
92 void WaitForServicesToStart(int num_expected_extensions, | 103 void WaitForServicesToStart(int num_expected_extensions, |
93 bool expect_extensions_enabled) { | 104 bool expect_extensions_enabled) { |
94 ExtensionService* service = extensions::ExtensionSystem::Get( | 105 ExtensionService* service = ExtensionSystem::Get( |
95 browser()->profile())->extension_service(); | 106 browser()->profile())->extension_service(); |
96 | 107 |
97 // Count the number of non-component extensions. | 108 // Count the number of non-component extensions. |
98 int found_extensions = 0; | 109 int found_extensions = 0; |
99 for (ExtensionSet::const_iterator it = service->extensions()->begin(); | 110 for (ExtensionSet::const_iterator it = service->extensions()->begin(); |
100 it != service->extensions()->end(); ++it) | 111 it != service->extensions()->end(); ++it) |
101 if ((*it)->location() != extensions::Manifest::COMPONENT) | 112 if ((*it)->location() != Manifest::COMPONENT) |
102 found_extensions++; | 113 found_extensions++; |
103 | 114 |
104 ASSERT_EQ(static_cast<uint32>(num_expected_extensions), | 115 ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
105 static_cast<uint32>(found_extensions)); | 116 static_cast<uint32>(found_extensions)); |
106 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); | 117 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
107 | 118 |
108 content::WindowedNotificationObserver user_scripts_observer( | 119 content::WindowedNotificationObserver user_scripts_observer( |
109 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 120 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
110 content::NotificationService::AllSources()); | 121 content::NotificationService::AllSources()); |
111 extensions::UserScriptMaster* master = | 122 UserScriptMaster* master = ExtensionSystem::Get(browser()->profile())-> |
112 extensions::ExtensionSystem::Get(browser()->profile())-> | |
113 user_script_master(); | 123 user_script_master(); |
114 if (!master->ScriptsReady()) | 124 if (!master->ScriptsReady()) |
115 user_scripts_observer.Wait(); | 125 user_scripts_observer.Wait(); |
116 ASSERT_TRUE(master->ScriptsReady()); | 126 ASSERT_TRUE(master->ScriptsReady()); |
117 } | 127 } |
118 | 128 |
119 void TestInjection(bool expect_css, bool expect_script) { | 129 void TestInjection(bool expect_css, bool expect_script) { |
120 // Load a page affected by the content script and test to see the effect. | 130 // Load a page affected by the content script and test to see the effect. |
121 base::FilePath test_file; | 131 base::FilePath test_file; |
122 PathService::Get(chrome::DIR_TEST_DATA, &test_file); | 132 PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 #else | 188 #else |
179 #define MAYBE_NoFileAccess NoFileAccess | 189 #define MAYBE_NoFileAccess NoFileAccess |
180 #endif | 190 #endif |
181 // Tests that disallowing file access on an extension prevents it from injecting | 191 // Tests that disallowing file access on an extension prevents it from injecting |
182 // script into a page with a file URL. | 192 // script into a page with a file URL. |
183 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) { | 193 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) { |
184 WaitForServicesToStart(num_expected_extensions_, true); | 194 WaitForServicesToStart(num_expected_extensions_, true); |
185 | 195 |
186 // Keep a separate list of extensions for which to disable file access, since | 196 // Keep a separate list of extensions for which to disable file access, since |
187 // doing so reloads them. | 197 // doing so reloads them. |
188 std::vector<const extensions::Extension*> extension_list; | 198 std::vector<const Extension*> extension_list; |
189 | 199 |
190 ExtensionService* service = extensions::ExtensionSystem::Get( | 200 ExtensionService* service = ExtensionSystem::Get( |
191 browser()->profile())->extension_service(); | 201 browser()->profile())->extension_service(); |
192 for (ExtensionSet::const_iterator it = service->extensions()->begin(); | 202 for (ExtensionSet::const_iterator it = service->extensions()->begin(); |
193 it != service->extensions()->end(); ++it) { | 203 it != service->extensions()->end(); ++it) { |
194 if ((*it)->location() == extensions::Manifest::COMPONENT) | 204 if ((*it)->location() == Manifest::COMPONENT) |
195 continue; | 205 continue; |
196 if (service->AllowFileAccess(*it)) | 206 if (service->AllowFileAccess(*it)) |
197 extension_list.push_back(*it); | 207 extension_list.push_back(*it); |
198 } | 208 } |
199 | 209 |
200 for (size_t i = 0; i < extension_list.size(); ++i) { | 210 for (size_t i = 0; i < extension_list.size(); ++i) { |
201 content::WindowedNotificationObserver user_scripts_observer( | 211 content::WindowedNotificationObserver user_scripts_observer( |
202 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 212 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
203 content::NotificationService::AllSources()); | 213 content::NotificationService::AllSources()); |
204 service->SetAllowFileAccess(extension_list[i], false); | 214 service->SetAllowFileAccess(extension_list[i], false); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 .AppendASCII("extensions") | 283 .AppendASCII("extensions") |
274 .AppendASCII("app2"); | 284 .AppendASCII("app2"); |
275 load_extensions_.push_back(fourth_extension_path.value()); | 285 load_extensions_.push_back(fourth_extension_path.value()); |
276 } | 286 } |
277 }; | 287 }; |
278 | 288 |
279 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) { | 289 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) { |
280 WaitForServicesToStart(4, true); | 290 WaitForServicesToStart(4, true); |
281 TestInjection(true, true); | 291 TestInjection(true, true); |
282 } | 292 } |
| 293 |
| 294 } // namespace extensions |
OLD | NEW |