[email protected] | 55692c05 | 2011-01-27 20:13:05 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/file_path.h" |
| 9 | #include "base/file_util.h" |
| 10 | #include "base/path_service.h" |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 8cb5d5b | 2010-02-09 11:36:16 | [diff] [blame] | 13 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 16 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 17 | #include "chrome/common/chrome_paths.h" |
| 18 | #include "chrome/common/chrome_switches.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 19 | #include "chrome/test/base/in_process_browser_test.h" |
| 20 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 21 | #include "content/public/browser/notification_details.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 22 | #include "content/public/browser/notification_service.h" |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame^] | 23 | #include "content/public/browser/web_contents.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 24 | #include "net/base/net_util.h" |
| 25 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 26 | // This file contains high-level startup tests for the extensions system. We've |
| 27 | // had many silly bugs where command line flags did not get propagated correctly |
| 28 | // into the services, so we didn't start correctly. |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 29 | |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 30 | class ExtensionStartupTestBase : public InProcessBrowserTest { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 31 | public: |
[email protected] | faed6e1 | 2009-11-24 22:38:36 | [diff] [blame] | 32 | ExtensionStartupTestBase() : enable_extensions_(false) { |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 33 | num_expected_extensions_ = 3; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | protected: |
| 37 | // InProcessBrowserTest |
| 38 | virtual void SetUpCommandLine(CommandLine* command_line) { |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 39 | EnableDOMAutomation(); |
| 40 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 41 | FilePath profile_dir; |
| 42 | PathService::Get(chrome::DIR_USER_DATA, &profile_dir); |
| 43 | profile_dir = profile_dir.AppendASCII("Default"); |
| 44 | file_util::CreateDirectory(profile_dir); |
| 45 | |
| 46 | preferences_file_ = profile_dir.AppendASCII("Preferences"); |
| 47 | user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); |
| 48 | extensions_dir_ = profile_dir.AppendASCII("Extensions"); |
| 49 | |
| 50 | if (enable_extensions_) { |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 51 | if (load_extensions_.empty()) { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 52 | FilePath src_dir; |
| 53 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 54 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 55 | |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 56 | file_util::CopyFile(src_dir.AppendASCII("Preferences"), |
| 57 | preferences_file_); |
| 58 | file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), |
| 59 | profile_dir, true); // recursive |
| 60 | } |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 61 | } else { |
| 62 | command_line->AppendSwitch(switches::kDisableExtensions); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 65 | if (!load_extensions_.empty()) { |
| 66 | FilePath::StringType paths = JoinString(load_extensions_, ','); |
| 67 | command_line->AppendSwitchNative(switches::kLoadExtension, |
| 68 | paths); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 69 | command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 70 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 73 | virtual void TearDown() { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 74 | EXPECT_TRUE(file_util::Delete(preferences_file_, false)); |
[email protected] | 020f49e | 2010-04-08 19:51:12 | [diff] [blame] | 75 | |
| 76 | // TODO(phajdan.jr): Check return values of the functions below, carefully. |
| 77 | file_util::Delete(user_scripts_dir_, true); |
[email protected] | 9eaa021d | 2010-04-08 19:55:34 | [diff] [blame] | 78 | file_util::Delete(extensions_dir_, true); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 79 | |
| 80 | InProcessBrowserTest::TearDown(); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 83 | void WaitForServicesToStart(int num_expected_extensions, |
| 84 | bool expect_extensions_enabled) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 85 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 86 | |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 87 | // Count the number of non-component extensions. |
| 88 | int found_extensions = 0; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 89 | for (ExtensionSet::const_iterator it = service->extensions()->begin(); |
| 90 | it != service->extensions()->end(); ++it) |
| 91 | if ((*it)->location() != Extension::COMPONENT) |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 92 | found_extensions++; |
| 93 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 94 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 95 | static_cast<uint32>(found_extensions)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 96 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 97 | |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 98 | ui_test_utils::WindowedNotificationObserver user_scripts_observer( |
| 99 | chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 100 | content::NotificationService::AllSources()); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 101 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 102 | if (!master->ScriptsReady()) |
| 103 | user_scripts_observer.Wait(); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 104 | ASSERT_TRUE(master->ScriptsReady()); |
| 105 | } |
| 106 | |
| 107 | void TestInjection(bool expect_css, bool expect_script) { |
| 108 | // Load a page affected by the content script and test to see the effect. |
[email protected] | 6277144 | 2009-11-22 02:25:04 | [diff] [blame] | 109 | FilePath test_file; |
| 110 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 111 | test_file = test_file.AppendASCII("extensions") |
| 112 | .AppendASCII("test_file.html"); |
| 113 | |
| 114 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 115 | |
| 116 | bool result = false; |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 117 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame^] | 118 | browser()->GetSelectedWebContents()->GetRenderViewHost(), L"", |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 119 | L"window.domAutomationController.send(" |
| 120 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 121 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 122 | &result)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 123 | EXPECT_EQ(expect_css, result); |
| 124 | |
| 125 | result = false; |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 126 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame^] | 127 | browser()->GetSelectedWebContents()->GetRenderViewHost(), L"", |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 128 | L"window.domAutomationController.send(document.title == 'Modified')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 129 | &result)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 130 | EXPECT_EQ(expect_script, result); |
| 131 | } |
| 132 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 133 | FilePath preferences_file_; |
| 134 | FilePath extensions_dir_; |
| 135 | FilePath user_scripts_dir_; |
| 136 | bool enable_extensions_; |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 137 | // Extensions to load from the command line. |
| 138 | std::vector<FilePath::StringType> load_extensions_; |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 139 | |
| 140 | int num_expected_extensions_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
| 144 | // ExtensionsStartupTest |
| 145 | // Ensures that we can startup the browser with --enable-extensions and some |
| 146 | // extensions installed and see them run and do basic things. |
| 147 | |
| 148 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 149 | public: |
| 150 | ExtensionsStartupTest() { |
| 151 | enable_extensions_ = true; |
| 152 | } |
| 153 | }; |
| 154 | |
[email protected] | c5b2c611 | 2010-06-11 00:02:14 | [diff] [blame] | 155 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 156 | WaitForServicesToStart(num_expected_extensions_, true); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 157 | TestInjection(true, true); |
| 158 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 159 | |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame] | 160 | // Sometimes times out on Mac. http://crbug.com/48151 |
| 161 | #if defined(OS_MACOSX) |
| 162 | #define MAYBE_NoFileAccess DISABLED_NoFileAccess |
| 163 | #else |
| 164 | #define MAYBE_NoFileAccess NoFileAccess |
| 165 | #endif |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 166 | // Tests that disallowing file access on an extension prevents it from injecting |
| 167 | // script into a page with a file URL. |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame] | 168 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 169 | WaitForServicesToStart(num_expected_extensions_, true); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 170 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 171 | // Keep a separate list of extensions for which to disable file access, since |
| 172 | // doing so reloads them. |
| 173 | std::vector<const Extension*> extension_list; |
| 174 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 175 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 176 | for (ExtensionSet::const_iterator it = service->extensions()->begin(); |
| 177 | it != service->extensions()->end(); ++it) { |
| 178 | if ((*it)->location() == Extension::COMPONENT) |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 179 | continue; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 180 | if (service->AllowFileAccess(*it)) |
| 181 | extension_list.push_back(*it); |
| 182 | } |
| 183 | |
| 184 | for (size_t i = 0; i < extension_list.size(); ++i) { |
| 185 | ui_test_utils::WindowedNotificationObserver user_scripts_observer( |
| 186 | chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
| 187 | content::NotificationService::AllSources()); |
| 188 | service->SetAllowFileAccess(extension_list[i], false); |
| 189 | user_scripts_observer.Wait(); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TestInjection(false, false); |
| 193 | } |
| 194 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 195 | // ExtensionsLoadTest |
| 196 | // Ensures that we can startup the browser with --load-extension and see them |
| 197 | // run. |
| 198 | |
| 199 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 200 | public: |
| 201 | ExtensionsLoadTest() { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 202 | enable_extensions_ = true; |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 203 | FilePath one_extension_path; |
| 204 | PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path); |
| 205 | one_extension_path = one_extension_path |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 206 | .AppendASCII("extensions") |
| 207 | .AppendASCII("good") |
| 208 | .AppendASCII("Extensions") |
| 209 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 210 | .AppendASCII("1.0.0.0"); |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 211 | load_extensions_.push_back(one_extension_path.value()); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 212 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 213 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 214 | |
[email protected] | 4b10905 | 2011-04-29 12:53:24 | [diff] [blame] | 215 | // Fails inconsistently on Linux x64. http://crbug.com/80961 |
| 216 | #if defined(OS_LINUX) && defined(ARCH_CPU_64_BITS) |
| 217 | #define Maybe_Test FLAKY_Test |
| 218 | #else |
| 219 | #define Maybe_Test Test |
| 220 | #endif |
| 221 | |
| 222 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Maybe_Test) { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 223 | WaitForServicesToStart(1, true); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 224 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 225 | } |
[email protected] | bfc9356 | 2011-12-14 19:40:04 | [diff] [blame] | 226 | |
| 227 | // ExtensionsLoadMultipleTest |
| 228 | // Ensures that we can startup the browser with multiple extensions |
| 229 | // via --load-extension=X1,X2,X3. |
| 230 | class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase { |
| 231 | public: |
| 232 | ExtensionsLoadMultipleTest() { |
| 233 | enable_extensions_ = true; |
| 234 | FilePath one_extension_path; |
| 235 | PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path); |
| 236 | one_extension_path = one_extension_path |
| 237 | .AppendASCII("extensions") |
| 238 | .AppendASCII("good") |
| 239 | .AppendASCII("Extensions") |
| 240 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 241 | .AppendASCII("1.0.0.0"); |
| 242 | load_extensions_.push_back(one_extension_path.value()); |
| 243 | |
| 244 | FilePath second_extension_path; |
| 245 | PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path); |
| 246 | second_extension_path = second_extension_path |
| 247 | .AppendASCII("extensions") |
| 248 | .AppendASCII("app"); |
| 249 | load_extensions_.push_back(second_extension_path.value()); |
| 250 | |
| 251 | FilePath third_extension_path; |
| 252 | PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path); |
| 253 | third_extension_path = third_extension_path |
| 254 | .AppendASCII("extensions") |
| 255 | .AppendASCII("app1"); |
| 256 | load_extensions_.push_back(third_extension_path.value()); |
| 257 | |
| 258 | FilePath fourth_extension_path; |
| 259 | PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path); |
| 260 | fourth_extension_path = fourth_extension_path |
| 261 | .AppendASCII("extensions") |
| 262 | .AppendASCII("app2"); |
| 263 | load_extensions_.push_back(fourth_extension_path.value()); |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) { |
| 268 | WaitForServicesToStart(4, true); |
| 269 | TestInjection(true, true); |
| 270 | } |