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