[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] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 8cb5d5b | 2010-02-09 11:36:16 | [diff] [blame] | 12 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 15 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 16 | #include "chrome/common/chrome_paths.h" |
| 17 | #include "chrome/common/chrome_switches.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 21f1168 | 2011-03-02 16:45:42 | [diff] [blame] | 20 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame^] | 21 | #include "content/public/browser/notification_details.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 22 | #include "net/base/net_util.h" |
| 23 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 24 | // This file contains high-level startup tests for the extensions system. We've |
| 25 | // had many silly bugs where command line flags did not get propagated correctly |
| 26 | // into the services, so we didn't start correctly. |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 27 | |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 28 | class ExtensionStartupTestBase : public InProcessBrowserTest { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 29 | public: |
[email protected] | faed6e1 | 2009-11-24 22:38:36 | [diff] [blame] | 30 | ExtensionStartupTestBase() : enable_extensions_(false) { |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 31 | num_expected_extensions_ = 3; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | protected: |
| 35 | // InProcessBrowserTest |
| 36 | virtual void SetUpCommandLine(CommandLine* command_line) { |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 37 | EnableDOMAutomation(); |
| 38 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 39 | FilePath profile_dir; |
| 40 | PathService::Get(chrome::DIR_USER_DATA, &profile_dir); |
| 41 | profile_dir = profile_dir.AppendASCII("Default"); |
| 42 | file_util::CreateDirectory(profile_dir); |
| 43 | |
| 44 | preferences_file_ = profile_dir.AppendASCII("Preferences"); |
| 45 | user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); |
| 46 | extensions_dir_ = profile_dir.AppendASCII("Extensions"); |
| 47 | |
| 48 | if (enable_extensions_) { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 49 | if (load_extension_.empty()) { |
| 50 | FilePath src_dir; |
| 51 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 52 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 53 | |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 54 | file_util::CopyFile(src_dir.AppendASCII("Preferences"), |
| 55 | preferences_file_); |
| 56 | file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), |
| 57 | profile_dir, true); // recursive |
| 58 | } |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 59 | } else { |
| 60 | command_line->AppendSwitch(switches::kDisableExtensions); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 63 | if (!load_extension_.empty()) { |
| 64 | command_line->AppendSwitchPath(switches::kLoadExtension, load_extension_); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 65 | command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 66 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 69 | virtual void TearDown() { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 70 | EXPECT_TRUE(file_util::Delete(preferences_file_, false)); |
[email protected] | 020f49e | 2010-04-08 19:51:12 | [diff] [blame] | 71 | |
| 72 | // TODO(phajdan.jr): Check return values of the functions below, carefully. |
| 73 | file_util::Delete(user_scripts_dir_, true); |
[email protected] | 9eaa021d | 2010-04-08 19:55:34 | [diff] [blame] | 74 | file_util::Delete(extensions_dir_, true); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 75 | |
| 76 | InProcessBrowserTest::TearDown(); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 79 | void WaitForServicesToStart(int num_expected_extensions, |
| 80 | bool expect_extensions_enabled) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 81 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 82 | |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 83 | // Count the number of non-component extensions. |
| 84 | int found_extensions = 0; |
| 85 | for (size_t i = 0; i < service->extensions()->size(); i++) |
| 86 | if (service->extensions()->at(i)->location() != Extension::COMPONENT) |
| 87 | found_extensions++; |
| 88 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 89 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 90 | static_cast<uint32>(found_extensions)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 91 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 92 | |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 93 | ui_test_utils::WindowedNotificationObserver user_scripts_observer( |
| 94 | chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
| 95 | NotificationService::AllSources()); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 96 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 97 | if (!master->ScriptsReady()) |
| 98 | user_scripts_observer.Wait(); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 99 | ASSERT_TRUE(master->ScriptsReady()); |
| 100 | } |
| 101 | |
| 102 | void TestInjection(bool expect_css, bool expect_script) { |
| 103 | // 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] | 104 | FilePath test_file; |
| 105 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 106 | test_file = test_file.AppendASCII("extensions") |
| 107 | .AppendASCII("test_file.html"); |
| 108 | |
| 109 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 110 | |
| 111 | bool result = false; |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 112 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 113 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 114 | L"window.domAutomationController.send(" |
| 115 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 116 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 117 | &result)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 118 | EXPECT_EQ(expect_css, result); |
| 119 | |
| 120 | result = false; |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 121 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 122 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 123 | L"window.domAutomationController.send(document.title == 'Modified')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 124 | &result)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 125 | EXPECT_EQ(expect_script, result); |
| 126 | } |
| 127 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 128 | FilePath preferences_file_; |
| 129 | FilePath extensions_dir_; |
| 130 | FilePath user_scripts_dir_; |
| 131 | bool enable_extensions_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 132 | FilePath load_extension_; |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 133 | |
| 134 | int num_expected_extensions_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | |
| 138 | // ExtensionsStartupTest |
| 139 | // Ensures that we can startup the browser with --enable-extensions and some |
| 140 | // extensions installed and see them run and do basic things. |
| 141 | |
| 142 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 143 | public: |
| 144 | ExtensionsStartupTest() { |
| 145 | enable_extensions_ = true; |
| 146 | } |
| 147 | }; |
| 148 | |
[email protected] | c5b2c611 | 2010-06-11 00:02:14 | [diff] [blame] | 149 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 150 | WaitForServicesToStart(num_expected_extensions_, true); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 151 | TestInjection(true, true); |
| 152 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 153 | |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame] | 154 | // Sometimes times out on Mac. http://crbug.com/48151 |
| 155 | #if defined(OS_MACOSX) |
| 156 | #define MAYBE_NoFileAccess DISABLED_NoFileAccess |
| 157 | #else |
| 158 | #define MAYBE_NoFileAccess NoFileAccess |
| 159 | #endif |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 160 | // Tests that disallowing file access on an extension prevents it from injecting |
| 161 | // script into a page with a file URL. |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame] | 162 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame] | 163 | WaitForServicesToStart(num_expected_extensions_, true); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 164 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 165 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 166 | for (size_t i = 0; i < service->extensions()->size(); ++i) { |
[email protected] | fe13bf6 | 2010-08-26 14:33:19 | [diff] [blame] | 167 | if (service->extensions()->at(i)->location() == Extension::COMPONENT) |
| 168 | continue; |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 169 | if (service->AllowFileAccess(service->extensions()->at(i))) { |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 170 | ui_test_utils::WindowedNotificationObserver user_scripts_observer( |
| 171 | chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
| 172 | NotificationService::AllSources()); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 173 | service->SetAllowFileAccess(service->extensions()->at(i), false); |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 174 | user_scripts_observer.Wait(); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | TestInjection(false, false); |
| 179 | } |
| 180 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 181 | // ExtensionsLoadTest |
| 182 | // Ensures that we can startup the browser with --load-extension and see them |
| 183 | // run. |
| 184 | |
| 185 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 186 | public: |
| 187 | ExtensionsLoadTest() { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 188 | enable_extensions_ = true; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 189 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 190 | load_extension_ = load_extension_ |
| 191 | .AppendASCII("extensions") |
| 192 | .AppendASCII("good") |
| 193 | .AppendASCII("Extensions") |
| 194 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 195 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 196 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 197 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 198 | |
[email protected] | 4b10905 | 2011-04-29 12:53:24 | [diff] [blame] | 199 | // Fails inconsistently on Linux x64. http://crbug.com/80961 |
| 200 | #if defined(OS_LINUX) && defined(ARCH_CPU_64_BITS) |
| 201 | #define Maybe_Test FLAKY_Test |
| 202 | #else |
| 203 | #define Maybe_Test Test |
| 204 | #endif |
| 205 | |
| 206 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Maybe_Test) { |
[email protected] | d728e00 | 2010-12-08 04:46:23 | [diff] [blame] | 207 | WaitForServicesToStart(1, true); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 208 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 209 | } |