[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 | |
| 31 | class ExtensionStartupTestBase |
| 32 | : public InProcessBrowserTest, public NotificationObserver { |
| 33 | public: |
[email protected] | faed6e1 | 2009-11-24 22:38:36 | [diff] [blame] | 34 | ExtensionStartupTestBase() : enable_extensions_(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 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 64 | if (!load_extension_.value().empty()) { |
| 65 | command_line->AppendSwitchWithValue(switches::kLoadExtension, |
| 66 | load_extension_.ToWStringHack()); |
| 67 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // NotificationObserver |
| 71 | virtual void Observe(NotificationType type, |
| 72 | const NotificationSource& source, |
| 73 | const NotificationDetails& details) { |
| 74 | switch (type.value) { |
| 75 | case NotificationType::EXTENSIONS_READY: |
| 76 | case NotificationType::USER_SCRIPTS_UPDATED: |
| 77 | MessageLoopForUI::current()->Quit(); |
| 78 | break; |
[email protected] | 041cc77 | 2009-08-28 04:09:56 | [diff] [blame] | 79 | default: |
| 80 | NOTREACHED(); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | virtual void TearDown() { |
| 85 | file_util::Delete(preferences_file_, false); |
| 86 | file_util::Delete(user_scripts_dir_, true); |
| 87 | file_util::Delete(extensions_dir_, true); |
| 88 | } |
| 89 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 90 | void WaitForServicesToStart(int num_expected_extensions, |
| 91 | bool expect_extensions_enabled) { |
| 92 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 93 | if (!service->is_ready()) { |
| 94 | registrar_.Add(this, NotificationType::EXTENSIONS_READY, |
| 95 | NotificationService::AllSources()); |
| 96 | ui_test_utils::RunMessageLoop(); |
| 97 | registrar_.Remove(this, NotificationType::EXTENSIONS_READY, |
| 98 | NotificationService::AllSources()); |
| 99 | } |
| 100 | |
| 101 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
| 102 | service->extensions()->size()); |
| 103 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 104 | |
| 105 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
| 106 | if (!master->ScriptsReady()) { |
| 107 | // Wait for UserScriptMaster to finish its scan. |
| 108 | registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, |
| 109 | NotificationService::AllSources()); |
| 110 | ui_test_utils::RunMessageLoop(); |
| 111 | registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, |
| 112 | NotificationService::AllSources()); |
| 113 | } |
| 114 | ASSERT_TRUE(master->ScriptsReady()); |
| 115 | } |
| 116 | |
| 117 | void TestInjection(bool expect_css, bool expect_script) { |
| 118 | // 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] | 119 | FilePath test_file; |
| 120 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 121 | test_file = test_file.AppendASCII("extensions") |
| 122 | .AppendASCII("test_file.html"); |
| 123 | |
| 124 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 125 | |
| 126 | bool result = false; |
| 127 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 128 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 129 | L"window.domAutomationController.send(" |
| 130 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 131 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
| 132 | &result); |
| 133 | EXPECT_EQ(expect_css, result); |
| 134 | |
| 135 | result = false; |
| 136 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 137 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 138 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 139 | &result); |
| 140 | EXPECT_EQ(expect_script, result); |
| 141 | } |
| 142 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 143 | FilePath preferences_file_; |
| 144 | FilePath extensions_dir_; |
| 145 | FilePath user_scripts_dir_; |
| 146 | bool enable_extensions_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 147 | FilePath load_extension_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 148 | NotificationRegistrar registrar_; |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | // ExtensionsStartupTest |
| 153 | // Ensures that we can startup the browser with --enable-extensions and some |
| 154 | // extensions installed and see them run and do basic things. |
| 155 | |
| 156 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 157 | public: |
| 158 | ExtensionsStartupTest() { |
| 159 | enable_extensions_ = true; |
| 160 | } |
| 161 | }; |
| 162 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 163 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | 4b77689 | 2010-03-19 22:57:33 | [diff] [blame^] | 164 | WaitForServicesToStart(4, true); // 1 component extension and 3 others. |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 165 | TestInjection(true, true); |
| 166 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 167 | |
| 168 | // ExtensionsLoadTest |
| 169 | // Ensures that we can startup the browser with --load-extension and see them |
| 170 | // run. |
| 171 | |
| 172 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 173 | public: |
| 174 | ExtensionsLoadTest() { |
| 175 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 176 | load_extension_ = load_extension_ |
| 177 | .AppendASCII("extensions") |
| 178 | .AppendASCII("good") |
| 179 | .AppendASCII("Extensions") |
| 180 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 181 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 182 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 183 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 184 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 185 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) { |
| 186 | WaitForServicesToStart(1, false); |
| 187 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 188 | } |