[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()); |
| 66 | } |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 69 | virtual void TearDown() { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 70 | EXPECT_TRUE(file_util::Delete(preferences_file_, false)); |
[email protected] | 020f49e | 2010-04-08 19:51:12 | [diff] [blame^] | 71 | |
| 72 | // TODO(phajdan.jr): Check return values of the functions below, carefully. |
| 73 | file_util::Delete(user_scripts_dir_, true); |
| 74 | file_util::Delete(extensions_dir_, true)); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 77 | void WaitForServicesToStart(int num_expected_extensions, |
| 78 | bool expect_extensions_enabled) { |
| 79 | ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 80 | if (!service->is_ready()) |
| 81 | ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY); |
| 82 | ASSERT_TRUE(service->is_ready()); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 83 | |
| 84 | ASSERT_EQ(static_cast<uint32>(num_expected_extensions), |
| 85 | service->extensions()->size()); |
| 86 | ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); |
| 87 | |
| 88 | UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); |
| 89 | if (!master->ScriptsReady()) { |
[email protected] | 3e59bac | 2010-04-08 16:16:55 | [diff] [blame] | 90 | ui_test_utils::WaitForNotification( |
| 91 | NotificationType::USER_SCRIPTS_UPDATED); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 92 | } |
| 93 | ASSERT_TRUE(master->ScriptsReady()); |
| 94 | } |
| 95 | |
| 96 | void TestInjection(bool expect_css, bool expect_script) { |
| 97 | // 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] | 98 | FilePath test_file; |
| 99 | PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
| 100 | test_file = test_file.AppendASCII("extensions") |
| 101 | .AppendASCII("test_file.html"); |
| 102 | |
| 103 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 104 | |
| 105 | bool result = false; |
| 106 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 107 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 108 | L"window.domAutomationController.send(" |
| 109 | L"document.defaultView.getComputedStyle(document.body, null)." |
| 110 | L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", |
| 111 | &result); |
| 112 | EXPECT_EQ(expect_css, result); |
| 113 | |
| 114 | result = false; |
| 115 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 116 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 117 | L"window.domAutomationController.send(document.title == 'Modified')", |
| 118 | &result); |
| 119 | EXPECT_EQ(expect_script, result); |
| 120 | } |
| 121 | |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 122 | FilePath preferences_file_; |
| 123 | FilePath extensions_dir_; |
| 124 | FilePath user_scripts_dir_; |
| 125 | bool enable_extensions_; |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 126 | FilePath load_extension_; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
| 130 | // ExtensionsStartupTest |
| 131 | // Ensures that we can startup the browser with --enable-extensions and some |
| 132 | // extensions installed and see them run and do basic things. |
| 133 | |
| 134 | class ExtensionsStartupTest : public ExtensionStartupTestBase { |
| 135 | public: |
| 136 | ExtensionsStartupTest() { |
| 137 | enable_extensions_ = true; |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
[email protected] | 4b77689 | 2010-03-19 22:57:33 | [diff] [blame] | 142 | WaitForServicesToStart(4, true); // 1 component extension and 3 others. |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 143 | TestInjection(true, true); |
| 144 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 145 | |
| 146 | // ExtensionsLoadTest |
| 147 | // Ensures that we can startup the browser with --load-extension and see them |
| 148 | // run. |
| 149 | |
| 150 | class ExtensionsLoadTest : public ExtensionStartupTestBase { |
| 151 | public: |
| 152 | ExtensionsLoadTest() { |
| 153 | PathService::Get(chrome::DIR_TEST_DATA, &load_extension_); |
| 154 | load_extension_ = load_extension_ |
| 155 | .AppendASCII("extensions") |
| 156 | .AppendASCII("good") |
| 157 | .AppendASCII("Extensions") |
| 158 | .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 159 | .AppendASCII("1.0.0.0"); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 160 | } |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 161 | }; |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 162 | |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 163 | IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) { |
| 164 | WaitForServicesToStart(1, false); |
| 165 | TestInjection(true, true); |
[email protected] | f0488f2f | 2009-07-01 05:25:22 | [diff] [blame] | 166 | } |