[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" |
| 13 | #include "chrome/browser/profile.h" |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 14 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 15 | #include "chrome/common/chrome_paths.h" |
| 16 | #include "chrome/common/chrome_switches.h" |
| 17 | #include "chrome/common/notification_details.h" |
| 18 | #include "chrome/common/notification_observer.h" |
| 19 | #include "chrome/common/notification_registrar.h" |
| 20 | #include "chrome/common/notification_service.h" |
| 21 | #include "chrome/common/notification_type.h" |
| 22 | #include "chrome/test/in_process_browser_test.h" |
| 23 | #include "chrome/test/ui_test_utils.h" |
| 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 | |
| 30 | class ExtensionStartupTestBase |
| 31 | : public InProcessBrowserTest, public NotificationObserver { |
| 32 | public: |
| 33 | ExtensionStartupTestBase() |
| 34 | : enable_extensions_(false), enable_user_scripts_(false) { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | protected: |
| 38 | // InProcessBrowserTest |
| 39 | virtual void SetUpCommandLine(CommandLine* command_line) { |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 40 | EnableDOMAutomation(); |
| 41 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 42 | FilePath profile_dir; |
| 43 | PathService::Get(chrome::DIR_USER_DATA, &profile_dir); |
| 44 | profile_dir = profile_dir.AppendASCII("Default"); |
| 45 | file_util::CreateDirectory(profile_dir); |
| 46 | |
| 47 | preferences_file_ = profile_dir.AppendASCII("Preferences"); |
| 48 | user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); |
| 49 | extensions_dir_ = profile_dir.AppendASCII("Extensions"); |
| 50 | |
| 51 | if (enable_extensions_) { |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 52 | FilePath src_dir; |
| 53 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 54 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); |
| 55 | |
| 56 | file_util::CopyFile(src_dir.AppendASCII("Preferences"), |
| 57 | preferences_file_); |
| 58 | file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), |
| 59 | profile_dir, true); // recursive |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 60 | } else { |
| 61 | command_line->AppendSwitch(switches::kDisableExtensions); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (enable_user_scripts_) { |
| 65 | command_line->AppendSwitch(switches::kEnableUserScripts); |
| 66 | |
| 67 | FilePath src_dir; |
| 68 | PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
| 69 | src_dir = src_dir.AppendASCII("extensions").AppendASCII("good") |
| 70 | .AppendASCII("Extensions") |
| 71 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 72 | .AppendASCII("1.0.0.0"); |
| 73 | |
| 74 | file_util::CreateDirectory(user_scripts_dir_); |
| 75 | file_util::CopyFile(src_dir.AppendASCII("script2.js"), |
| 76 | user_scripts_dir_.AppendASCII("script2.user.js")); |
| 77 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 78 | |
| 79 | if (!load_extension_.value().empty()) { |
| 80 | command_line->AppendSwitchWithValue(switches::kLoadExtension, |
| 81 | load_extension_.ToWStringHack()); |
| 82 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // NotificationObserver |
| 86 | virtual void Observe(NotificationType type, |
| 87 | const NotificationSource& source, |
| 88 | const NotificationDetails& details) { |
| 89 | switch (type.value) { |
| 90 | case NotificationType::EXTENSIONS_READY: |
| 91 | case NotificationType::USER_SCRIPTS_UPDATED: |
| 92 | MessageLoopForUI::current()->Quit(); |
| 93 | break; |
[email protected] | 041cc77 | 2009-08-28 04:09:56 | [diff] [blame] | 94 | default: |
| 95 | NOTREACHED(); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | virtual void TearDown() { |
| 100 | file_util::Delete(preferences_file_, false); |
| 101 | file_util::Delete(user_scripts_dir_, true); |
| 102 | file_util::Delete(extensions_dir_, true); |
| 103 | } |
| 104 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 105 | void WaitForServicesToStart(int num_expected_extensions, |
| 106 | bool expect_extensions_enabled) { |
| 107 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 108 | if (!service->is_ready()) { |
| 109 | registrar_.Add(this, NotificationType::EXTENSIONS_READY, |
| 110 | NotificationService::AllSources()); |
| 111 | ui_test_utils::RunMessageLoop(); |
| 112 | registrar_.Remove(this, NotificationType::EXTENSIONS_READY, |
| 113 | NotificationService::AllSources()); |
| 114 | } |
| 115 | |
| 116 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
| 117 | service->extensions()->size()); |
| 118 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 119 | |
| 120 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
| 121 | if (!master->ScriptsReady()) { |
| 122 | // Wait for UserScriptMaster to finish its scan. |
| 123 | registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, |
| 124 | NotificationService::AllSources()); |
| 125 | ui_test_utils::RunMessageLoop(); |
| 126 | registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, |
| 127 | NotificationService::AllSources()); |
| 128 | } |
| 129 | ASSERT_TRUE(master->ScriptsReady()); |
| 130 | } |
| 131 | |
| 132 | void TestInjection(bool expect_css, bool expect_script) { |
| 133 | // Load a page affected by the content script and test to see the effect. |
| 134 | FilePath test_file; |
| 135 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 136 | test_file = test_file.AppendASCII("extensions") |
| 137 | .AppendASCII("test_file.html"); |
| 138 | |
| 139 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
| 140 | |
| 141 | bool result = false; |
| 142 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 143 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 144 | L"window.domAutomationController.send(" |
| 145 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 146 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
| 147 | &result); |
| 148 | EXPECT_EQ(expect_css, result); |
| 149 | |
| 150 | result = false; |
| 151 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 152 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 153 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 154 | &result); |
| 155 | EXPECT_EQ(expect_script, result); |
| 156 | } |
| 157 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 158 | FilePath preferences_file_; |
| 159 | FilePath extensions_dir_; |
| 160 | FilePath user_scripts_dir_; |
| 161 | bool enable_extensions_; |
| 162 | bool enable_user_scripts_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 163 | FilePath load_extension_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 164 | NotificationRegistrar registrar_; |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | // ExtensionsStartupTest |
| 169 | // Ensures that we can startup the browser with --enable-extensions and some |
| 170 | // extensions installed and see them run and do basic things. |
| 171 | |
| 172 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 173 | public: |
| 174 | ExtensionsStartupTest() { |
| 175 | enable_extensions_ = true; |
| 176 | } |
| 177 | }; |
| 178 | |
[email protected] | 041cc77 | 2009-08-28 04:09:56 | [diff] [blame] | 179 | #if defined(OS_WIN) |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 180 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 181 | WaitForServicesToStart(3, true); |
| 182 | TestInjection(true, true); |
| 183 | } |
[email protected] | 041cc77 | 2009-08-28 04:09:56 | [diff] [blame] | 184 | #endif // defined(OS_WIN) |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 185 | |
| 186 | // ExtensionsLoadTest |
| 187 | // Ensures that we can startup the browser with --load-extension and see them |
| 188 | // run. |
| 189 | |
| 190 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 191 | public: |
| 192 | ExtensionsLoadTest() { |
| 193 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 194 | load_extension_ = load_extension_ |
| 195 | .AppendASCII("extensions") |
| 196 | .AppendASCII("good") |
| 197 | .AppendASCII("Extensions") |
| 198 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 199 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 200 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 201 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 202 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 203 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) { |
| 204 | WaitForServicesToStart(1, false); |
| 205 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | // ExtensionsStartupUserScriptTest |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 210 | // Tests that we can startup with --enable-user-scripts and run user scripts and |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 211 | // see them do basic things. |
| 212 | |
| 213 | class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase { |
| 214 | public: |
| 215 | ExtensionsStartupUserScriptTest() { |
| 216 | enable_user_scripts_ = true; |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) { |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 221 | WaitForServicesToStart(0, false); |
| 222 | TestInjection(false, true); |
| 223 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 224 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 225 | // Ensure we don't inject into chrome:// URLs |
| 226 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, NoInjectIntoChrome) { |
| 227 | WaitForServicesToStart(0, false); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 228 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 229 | ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 230 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 231 | bool result = false; |
| 232 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 233 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 234 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 235 | &result); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 236 | EXPECT_FALSE(result); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 237 | } |