[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 1 | // Copyright (c) 2008 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 | |
[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" |
| 11 | #include "chrome/browser/browser.h" |
| 12 | #include "chrome/browser/extensions/extensions_service.h" |
[email protected] | 8cb5d5b | 2010-02-09 11:36:16 | [diff] [blame] | 13 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 14 | #include "chrome/browser/profile.h" |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 15 | #include "chrome/browser/tab_contents/tab_contents.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" |
| 18 | #include "chrome/common/notification_details.h" |
| 19 | #include "chrome/common/notification_observer.h" |
| 20 | #include "chrome/common/notification_registrar.h" |
| 21 | #include "chrome/common/notification_service.h" |
| 22 | #include "chrome/common/notification_type.h" |
| 23 | #include "chrome/test/in_process_browser_test.h" |
| 24 | #include "chrome/test/ui_test_utils.h" |
| 25 | #include "net/base/net_util.h" |
| 26 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 27 | // This file contains high-level startup tests for the extensions system. We've |
| 28 | // had many silly bugs where command line flags did not get propagated correctly |
| 29 | // into the services, so we didn't start correctly. |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 30 | |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 31 | class ExtensionStartupTestBase : public InProcessBrowserTest { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 32 | public: |
[email protected] | faed6e1 | 2009-11-24 22:38:36 | [diff] [blame] | 33 | ExtensionStartupTestBase() : enable_extensions_(false) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame^] | 34 | #if defined(OS_CHROMEOS) |
| 35 | num_expected_extensions_ = 3; |
| 36 | #else |
| 37 | num_expected_extensions_ = 4; |
| 38 | #endif |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | // InProcessBrowserTest |
| 43 | virtual void SetUpCommandLine(CommandLine* command_line) { |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 44 | EnableDOMAutomation(); |
| 45 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 46 | FilePath profile_dir; |
| 47 | PathService::Get(chrome::DIR_USER_DATA, &profile_dir); |
| 48 | profile_dir = profile_dir.AppendASCII("Default"); |
| 49 | file_util::CreateDirectory(profile_dir); |
| 50 | |
| 51 | preferences_file_ = profile_dir.AppendASCII("Preferences"); |
| 52 | user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); |
| 53 | extensions_dir_ = profile_dir.AppendASCII("Extensions"); |
| 54 | |
| 55 | if (enable_extensions_) { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 56 | FilePath src_dir; |
| 57 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 58 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); |
| 59 | |
| 60 | file_util::CopyFile(src_dir.AppendASCII("Preferences"), |
| 61 | preferences_file_); |
| 62 | file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), |
| 63 | profile_dir, true); // recursive |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 64 | } else { |
| 65 | command_line->AppendSwitch(switches::kDisableExtensions); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 68 | if (!load_extension_.empty()) { |
| 69 | command_line->AppendSwitchPath(switches::kLoadExtension, load_extension_); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 70 | command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 71 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 74 | virtual void TearDown() { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 75 | EXPECT_TRUE(file_util::Delete(preferences_file_, false)); |
[email protected] | 020f49e | 2010-04-08 19:51:12 | [diff] [blame] | 76 | |
| 77 | // TODO(phajdan.jr): Check return values of the functions below, carefully. |
| 78 | file_util::Delete(user_scripts_dir_, true); |
[email protected] | 9eaa021d | 2010-04-08 19:55:34 | [diff] [blame] | 79 | file_util::Delete(extensions_dir_, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 82 | void WaitForServicesToStart(int num_expected_extensions, |
| 83 | bool expect_extensions_enabled) { |
| 84 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 85 | if (!service->is_ready()) |
| 86 | ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY); |
| 87 | ASSERT_TRUE(service->is_ready()); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 88 | |
| 89 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
| 90 | service->extensions()->size()); |
| 91 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 92 | |
| 93 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
| 94 | if (!master->ScriptsReady()) { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 95 | ui_test_utils::WaitForNotification( |
| 96 | NotificationType::USER_SCRIPTS_UPDATED); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 97 | } |
| 98 | ASSERT_TRUE(master->ScriptsReady()); |
| 99 | } |
| 100 | |
| 101 | void TestInjection(bool expect_css, bool expect_script) { |
| 102 | // 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] | 103 | FilePath test_file; |
| 104 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 105 | test_file = test_file.AppendASCII("extensions") |
| 106 | .AppendASCII("test_file.html"); |
| 107 | |
| 108 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 109 | |
| 110 | bool result = false; |
| 111 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 112 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 113 | L"window.domAutomationController.send(" |
| 114 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 115 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
| 116 | &result); |
| 117 | EXPECT_EQ(expect_css, result); |
| 118 | |
| 119 | result = false; |
| 120 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 121 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 122 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 123 | &result); |
| 124 | EXPECT_EQ(expect_script, result); |
| 125 | } |
| 126 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 127 | FilePath preferences_file_; |
| 128 | FilePath extensions_dir_; |
| 129 | FilePath user_scripts_dir_; |
| 130 | bool enable_extensions_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 131 | FilePath load_extension_; |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame^] | 132 | |
| 133 | int num_expected_extensions_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
| 137 | // ExtensionsStartupTest |
| 138 | // Ensures that we can startup the browser with --enable-extensions and some |
| 139 | // extensions installed and see them run and do basic things. |
| 140 | |
| 141 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 142 | public: |
| 143 | ExtensionsStartupTest() { |
| 144 | enable_extensions_ = true; |
| 145 | } |
| 146 | }; |
| 147 | |
[email protected] | c5b2c611 | 2010-06-11 00:02:14 | [diff] [blame] | 148 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | e50013c3 | 2010-08-18 21:05:24 | [diff] [blame^] | 149 | // 1 component extension and 2 or 3 others, depending on the platform. |
| 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 | // 1 component extension and 2 or 3 others, depending on the platform. |
| 164 | WaitForServicesToStart(num_expected_extensions_, true); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 165 | |
| 166 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 167 | for (size_t i = 0; i < service->extensions()->size(); ++i) { |
| 168 | if (service->AllowFileAccess(service->extensions()->at(i))) { |
| 169 | service->SetAllowFileAccess(service->extensions()->at(i), false); |
| 170 | ui_test_utils::WaitForNotification( |
| 171 | NotificationType::USER_SCRIPTS_UPDATED); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | TestInjection(false, false); |
| 176 | } |
| 177 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 178 | // ExtensionsLoadTest |
| 179 | // Ensures that we can startup the browser with --load-extension and see them |
| 180 | // run. |
| 181 | |
| 182 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 183 | public: |
| 184 | ExtensionsLoadTest() { |
| 185 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 186 | load_extension_ = load_extension_ |
| 187 | .AppendASCII("extensions") |
| 188 | .AppendASCII("good") |
| 189 | .AppendASCII("Extensions") |
| 190 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 191 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 192 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 193 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 194 | |
[email protected] | 27d53738 | 2010-06-10 23:55:02 | [diff] [blame] | 195 | // Flaky (times out) on Mac/Windows. http://crbug.com/46301. |
[email protected] | 5e34a85 | 2010-06-21 14:44:49 | [diff] [blame] | 196 | #if defined(OS_MACOSX) || defined(OS_WIN) |
[email protected] | 27d53738 | 2010-06-10 23:55:02 | [diff] [blame] | 197 | #define MAYBE_Test FLAKY_Test |
| 198 | #else |
| 199 | #define MAYBE_Test Test |
| 200 | #endif |
| 201 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, MAYBE_Test) { |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 202 | WaitForServicesToStart(1, false); |
| 203 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 204 | } |