OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_browsertest.h" | 5 #include "chrome/browser/extensions/extension_browsertest.h" |
| 6 #include "chrome/browser/extensions/extension_service.h" |
6 #include "chrome/browser/extensions/extension_test_message_listener.h" | 7 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" |
7 | 10 |
8 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {}; | 11 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest { |
| 12 public: |
| 13 // Loads the extension at "test_data_dir_/|path_suffix|" and waits for it to |
| 14 // call chrome.test.sendMessge("ready") indicating that it's ready for the |
| 15 // test to proceed. |
| 16 void LoadExtensionAndWaitForReady(const char* path_suffix) { |
| 17 ExtensionTestMessageListener ready("ready", false); |
| 18 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(path_suffix))); |
| 19 ASSERT_TRUE(ready.WaitUntilSatisfied()); |
| 20 } |
| 21 |
| 22 void InstallThemeAndWait(const char* path_suffix, |
| 23 bool is_update, /* is this an update of a theme? */ |
| 24 const char* wait_message) { |
| 25 ExtensionTestMessageListener listener(wait_message, false); |
| 26 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII(path_suffix), |
| 27 is_update ? 0 : 1)); |
| 28 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 29 } |
| 30 |
| 31 |
| 32 void LoadExtensionWithTest(const char* extension_path, |
| 33 const char* js_file) { |
| 34 ExtensionTestMessageListener bootstrap("bootstrap", true); |
| 35 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(extension_path))); |
| 36 ASSERT_TRUE(bootstrap.WaitUntilSatisfied()); |
| 37 |
| 38 ExtensionTestMessageListener ready("ready", false); |
| 39 bootstrap.Reply(std::string(js_file)); |
| 40 ASSERT_TRUE(ready.WaitUntilSatisfied()); |
| 41 } |
| 42 |
| 43 void InstallTwoThemes() { |
| 44 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme.crx"), |
| 45 1)); |
| 46 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme2.crx"), |
| 47 0)); |
| 48 } |
| 49 }; |
9 | 50 |
10 // We test this here instead of in an ExtensionApiTest because normal extensions | 51 // We test this here instead of in an ExtensionApiTest because normal extensions |
11 // are not allowed to call the install function. | 52 // are not allowed to call the install function. |
12 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { | 53 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { |
13 ExtensionTestMessageListener listener1("ready", false); | 54 LoadExtensionAndWaitForReady("management/install_event"); |
14 ASSERT_TRUE(LoadExtension( | |
15 test_data_dir_.AppendASCII("management/install_event"))); | |
16 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | |
17 | 55 |
18 ExtensionTestMessageListener listener2("got_event", false); | 56 ExtensionTestMessageListener listener2("got_event", false); |
19 ASSERT_TRUE(LoadExtension( | 57 ASSERT_TRUE(LoadExtension( |
20 test_data_dir_.AppendASCII("api_test/management/enabled_extension"))); | 58 test_data_dir_.AppendASCII("api_test/management/enabled_extension"))); |
21 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 59 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
22 } | 60 } |
23 | 61 |
24 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) { | 62 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) { |
25 ExtensionTestMessageListener listener1("app_launched", false); | 63 ExtensionTestMessageListener listener1("app_launched", false); |
26 ExtensionTestMessageListener listener2("got_expected_error", false); | 64 ExtensionTestMessageListener listener2("got_expected_error", false); |
27 ASSERT_TRUE(LoadExtension( | 65 ASSERT_TRUE(LoadExtension( |
28 test_data_dir_.AppendASCII("management/simple_extension"))); | 66 test_data_dir_.AppendASCII("management/simple_extension"))); |
29 ASSERT_TRUE(LoadExtension( | 67 ASSERT_TRUE(LoadExtension( |
30 test_data_dir_.AppendASCII("management/packaged_app"))); | 68 test_data_dir_.AppendASCII("management/packaged_app"))); |
31 ASSERT_TRUE(LoadExtension( | 69 ASSERT_TRUE(LoadExtension( |
32 test_data_dir_.AppendASCII("management/launch_app"))); | 70 test_data_dir_.AppendASCII("management/launch_app"))); |
33 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 71 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
34 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 72 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
35 } | 73 } |
36 | 74 |
37 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, | 75 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, |
38 LaunchAppFromBackground) { | 76 LaunchAppFromBackground) { |
39 ExtensionTestMessageListener listener1("success", false); | 77 ExtensionTestMessageListener listener1("success", false); |
40 ASSERT_TRUE(LoadExtension( | 78 ASSERT_TRUE(LoadExtension( |
41 test_data_dir_.AppendASCII("management/packaged_app"))); | 79 test_data_dir_.AppendASCII("management/packaged_app"))); |
42 ASSERT_TRUE(LoadExtension( | 80 ASSERT_TRUE(LoadExtension( |
43 test_data_dir_.AppendASCII("management/launch_app_from_background"))); | 81 test_data_dir_.AppendASCII("management/launch_app_from_background"))); |
44 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 82 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
45 } | 83 } |
| 84 |
| 85 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ThemeBasics) { |
| 86 LoadExtensionWithTest("management/themes", "basics.js"); |
| 87 |
| 88 // Install one theme - we should send events for install and enable. |
| 89 InstallThemeAndWait("theme.crx", false, "step1"); |
| 90 |
| 91 ExtensionTestMessageListener step3("step3", true); |
| 92 ExtensionTestMessageListener step4("step4", true); |
| 93 |
| 94 // Install a second theme - we should send install/enable events for the |
| 95 // first theme and a disable event for the second one. |
| 96 InstallThemeAndWait("theme2.crx", true, "step2"); |
| 97 |
| 98 // Now tell the extension to go ahead and disable theme2. |
| 99 ASSERT_TRUE(step3.WaitUntilSatisfied()); |
| 100 step3.Reply("step3_ack"); |
| 101 ASSERT_TRUE(step4.WaitUntilSatisfied()); |
| 102 |
| 103 // Make sure the theme is no longer enabled. |
| 104 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 105 std::string id("pjpgmfcmabopnnfonnhmdjglfpjjfkbf"); |
| 106 ASSERT_EQ(NULL, service->GetExtensionById(id, false)); |
| 107 ASSERT_TRUE(service->GetExtensionById(id, true) != NULL); |
| 108 ASSERT_EQ(NULL, browser()->profile()->GetTheme()); |
| 109 } |
| 110 |
| 111 // Install theme1, then undo. |
| 112 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ThemeUndo1) { |
| 113 LoadExtensionWithTest("management/themes", "undo.js"); |
| 114 |
| 115 InstallThemeAndWait("theme.crx", false, "step1"); |
| 116 |
| 117 ExtensionTestMessageListener step2("step2", false); |
| 118 VerifyThemeInfoBarAndUndoInstall(); |
| 119 ASSERT_TRUE(step2.WaitUntilSatisfied()); |
| 120 } |
| 121 |
| 122 // Install theme1, then install theme2, then undo. |
| 123 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ThemeUndo2) { |
| 124 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme.crx"), |
| 125 1)); |
| 126 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme2.crx"), |
| 127 0)); |
| 128 |
| 129 LoadExtensionWithTest("management/themes", "undo2.js"); |
| 130 |
| 131 ExtensionTestMessageListener step1("step1", false); |
| 132 VerifyThemeInfoBarAndUndoInstall(); |
| 133 ASSERT_TRUE(step1.WaitUntilSatisfied()); |
| 134 } |
| 135 |
| 136 |
| 137 // Test "clear theme" functionality. |
| 138 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ClearTheme) { |
| 139 LoadExtensionWithTest("management/themes", "clear_theme.js"); |
| 140 |
| 141 InstallThemeAndWait("theme.crx", false, "step1"); |
| 142 |
| 143 ExtensionTestMessageListener step2("step2", false); |
| 144 browser()->profile()->ClearTheme(); |
| 145 ASSERT_TRUE(step2.WaitUntilSatisfied()); |
| 146 } |
| 147 |
| 148 // Install theme1, then uninstall theme1. |
| 149 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, UninstallTheme) { |
| 150 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme.crx"), |
| 151 1)); |
| 152 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 153 std::string id("iamefpfkojoapidjnbafmgkgncegbkad"); |
| 154 |
| 155 ASSERT_TRUE(service->GetExtensionById(id, false) != NULL); |
| 156 const Extension* theme = browser()->profile()->GetTheme(); |
| 157 ASSERT_TRUE(theme != NULL); |
| 158 ASSERT_EQ(id, theme->id()); |
| 159 |
| 160 LoadExtensionWithTest("management/themes", "uninstall.js"); |
| 161 |
| 162 theme = browser()->profile()->GetTheme(); |
| 163 ASSERT_EQ(NULL, theme); |
| 164 } |
| 165 |
| 166 // Install theme1, then install theme2, then enable theme1. |
| 167 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, EnableDisabledTheme) { |
| 168 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme.crx"), |
| 169 1)); |
| 170 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme2.crx"), |
| 171 0)); |
| 172 ExtensionTestMessageListener step1("step1", false); |
| 173 LoadExtensionWithTest("management/themes", "enable_disabled.js"); |
| 174 ASSERT_TRUE(step1.WaitUntilSatisfied()); |
| 175 |
| 176 std::string id("iamefpfkojoapidjnbafmgkgncegbkad"); |
| 177 const Extension* theme = browser()->profile()->GetTheme(); |
| 178 ASSERT_TRUE(theme != NULL); |
| 179 ASSERT_EQ(id, theme->id()); |
| 180 } |
| 181 |
| 182 // Install theme1, install theme2, disable theme2, enable theme1. |
| 183 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ThemeEnabling) { |
| 184 InstallTwoThemes(); |
| 185 ExtensionTestMessageListener step1("step1", true); |
| 186 ExtensionTestMessageListener step2("step2", false); |
| 187 |
| 188 LoadExtensionWithTest("management/themes", "enabling.js"); |
| 189 ASSERT_TRUE(step1.WaitUntilSatisfied()); |
| 190 |
| 191 const Extension* theme = browser()->profile()->GetTheme(); |
| 192 ASSERT_EQ(NULL, theme); |
| 193 |
| 194 step1.Reply("step1_ack"); |
| 195 ASSERT_TRUE(step2.WaitUntilSatisfied()); |
| 196 |
| 197 std::string id("iamefpfkojoapidjnbafmgkgncegbkad"); |
| 198 theme = browser()->profile()->GetTheme(); |
| 199 ASSERT_TRUE(theme != NULL); |
| 200 ASSERT_EQ(id, theme->id()); |
| 201 } |
| 202 |
| 203 // Install theme1, install theme2, uninstall theme2. |
| 204 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, |
| 205 UninstallEnabledTheme) { |
| 206 InstallTwoThemes(); |
| 207 ExtensionTestMessageListener step1("step1", true); |
| 208 ExtensionTestMessageListener step2("step2", false); |
| 209 LoadExtensionWithTest("management/themes", "uninstall_enabled.js"); |
| 210 ASSERT_TRUE(step1.WaitUntilSatisfied()); |
| 211 |
| 212 const Extension* theme = browser()->profile()->GetTheme(); |
| 213 ASSERT_EQ(NULL, theme); |
| 214 } |
| 215 |
| 216 // Install theme1, disable, enable. |
| 217 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, |
| 218 DisableThenEnableTheme) { |
| 219 ASSERT_TRUE(InstallExtensionWithUI(test_data_dir_.AppendASCII("theme.crx"), |
| 220 1)); |
| 221 |
| 222 ExtensionTestMessageListener step1("step1", false); |
| 223 LoadExtensionWithTest("management/themes", "disable_then_enable.js"); |
| 224 ASSERT_TRUE(step1.WaitUntilSatisfied()); |
| 225 |
| 226 std::string id("iamefpfkojoapidjnbafmgkgncegbkad"); |
| 227 const Extension* theme = browser()->profile()->GetTheme(); |
| 228 ASSERT_TRUE(theme != NULL); |
| 229 ASSERT_EQ(id, theme->id()); |
| 230 } |
OLD | NEW |