[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] | 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] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 51 | FilePath src_dir; |
| 52 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 53 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); |
| 54 | |
| 55 | file_util::CopyFile(src_dir.AppendASCII("Preferences"), |
| 56 | preferences_file_); |
| 57 | file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), |
| 58 | profile_dir, true); // recursive |
[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] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 63 | if (!load_extension_.value().empty()) { |
| 64 | command_line->AppendSwitchWithValue(switches::kLoadExtension, |
| 65 | load_extension_.ToWStringHack()); |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 66 | command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 67 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 70 | virtual void TearDown() { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 71 | EXPECT_TRUE(file_util::Delete(preferences_file_, false)); |
[email protected] | 020f49e | 2010-04-08 19:51:12 | [diff] [blame] | 72 | |
| 73 | // TODO(phajdan.jr): Check return values of the functions below, carefully. |
| 74 | file_util::Delete(user_scripts_dir_, true); |
[email protected] | 9eaa021d | 2010-04-08 19:55:34 | [diff] [blame] | 75 | file_util::Delete(extensions_dir_, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 78 | void WaitForServicesToStart(int num_expected_extensions, |
| 79 | bool expect_extensions_enabled) { |
| 80 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 81 | if (!service->is_ready()) |
| 82 | ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY); |
| 83 | ASSERT_TRUE(service->is_ready()); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 84 | |
| 85 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
| 86 | service->extensions()->size()); |
| 87 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 88 | |
| 89 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
| 90 | if (!master->ScriptsReady()) { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 91 | ui_test_utils::WaitForNotification( |
| 92 | NotificationType::USER_SCRIPTS_UPDATED); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 93 | } |
| 94 | ASSERT_TRUE(master->ScriptsReady()); |
| 95 | } |
| 96 | |
| 97 | void TestInjection(bool expect_css, bool expect_script) { |
| 98 | // 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] | 99 | FilePath test_file; |
| 100 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 101 | test_file = test_file.AppendASCII("extensions") |
| 102 | .AppendASCII("test_file.html"); |
| 103 | |
| 104 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 105 | |
| 106 | bool result = false; |
| 107 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 108 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 109 | L"window.domAutomationController.send(" |
| 110 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 111 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
| 112 | &result); |
| 113 | EXPECT_EQ(expect_css, result); |
| 114 | |
| 115 | result = false; |
| 116 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 117 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 118 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 119 | &result); |
| 120 | EXPECT_EQ(expect_script, result); |
| 121 | } |
| 122 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 123 | FilePath preferences_file_; |
| 124 | FilePath extensions_dir_; |
| 125 | FilePath user_scripts_dir_; |
| 126 | bool enable_extensions_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 127 | FilePath load_extension_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | |
| 131 | // ExtensionsStartupTest |
| 132 | // Ensures that we can startup the browser with --enable-extensions and some |
| 133 | // extensions installed and see them run and do basic things. |
| 134 | |
| 135 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 136 | public: |
| 137 | ExtensionsStartupTest() { |
| 138 | enable_extensions_ = true; |
| 139 | } |
| 140 | }; |
| 141 | |
[email protected] | c5b2c611 | 2010-06-11 00:02:14 | [diff] [blame] | 142 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | 4b77689 | 2010-03-19 22:57:33 | [diff] [blame] | 143 | WaitForServicesToStart(4, true); // 1 component extension and 3 others. |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 144 | TestInjection(true, true); |
| 145 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 146 | |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame^] | 147 | // Sometimes times out on Mac. http://crbug.com/48151 |
| 148 | #if defined(OS_MACOSX) |
| 149 | #define MAYBE_NoFileAccess DISABLED_NoFileAccess |
| 150 | #else |
| 151 | #define MAYBE_NoFileAccess NoFileAccess |
| 152 | #endif |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 153 | // Tests that disallowing file access on an extension prevents it from injecting |
| 154 | // script into a page with a file URL. |
[email protected] | b2f665f5 | 2010-07-02 01:32:09 | [diff] [blame^] | 155 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) { |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 156 | WaitForServicesToStart(4, true); // 1 component extension and 3 others. |
| 157 | |
| 158 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 159 | for (size_t i = 0; i < service->extensions()->size(); ++i) { |
| 160 | if (service->AllowFileAccess(service->extensions()->at(i))) { |
| 161 | service->SetAllowFileAccess(service->extensions()->at(i), false); |
| 162 | ui_test_utils::WaitForNotification( |
| 163 | NotificationType::USER_SCRIPTS_UPDATED); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | TestInjection(false, false); |
| 168 | } |
| 169 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 170 | // ExtensionsLoadTest |
| 171 | // Ensures that we can startup the browser with --load-extension and see them |
| 172 | // run. |
| 173 | |
| 174 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 175 | public: |
| 176 | ExtensionsLoadTest() { |
| 177 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 178 | load_extension_ = load_extension_ |
| 179 | .AppendASCII("extensions") |
| 180 | .AppendASCII("good") |
| 181 | .AppendASCII("Extensions") |
| 182 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 183 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 184 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 185 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 186 | |
[email protected] | 27d53738 | 2010-06-10 23:55:02 | [diff] [blame] | 187 | // Flaky (times out) on Mac/Windows. http://crbug.com/46301. |
[email protected] | 5e34a85 | 2010-06-21 14:44:49 | [diff] [blame] | 188 | #if defined(OS_MACOSX) || defined(OS_WIN) |
[email protected] | 27d53738 | 2010-06-10 23:55:02 | [diff] [blame] | 189 | #define MAYBE_Test FLAKY_Test |
| 190 | #else |
| 191 | #define MAYBE_Test Test |
| 192 | #endif |
| 193 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, MAYBE_Test) { |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 194 | WaitForServicesToStart(1, false); |
| 195 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 196 | } |