| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> |
| 6 |
| 5 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 6 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" |
| 7 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 11 #include "chrome/browser/extensions/test_management_policy.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | 14 #include "chrome/browser/ui/browser_finder.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 15 | 19 |
| 20 using extensions::Extension; |
| 21 |
| 16 namespace { | 22 namespace { |
| 17 | 23 |
| 18 // Find a browser other than |browser|. | 24 // Find a browser other than |browser|. |
| 19 Browser* FindOtherBrowser(Browser* browser) { | 25 Browser* FindOtherBrowser(Browser* browser) { |
| 20 Browser* found = NULL; | 26 Browser* found = NULL; |
| 21 for (BrowserList::const_iterator it = BrowserList::begin(); | 27 for (BrowserList::const_iterator it = BrowserList::begin(); |
| 22 it != BrowserList::end(); ++it) { | 28 it != BrowserList::end(); ++it) { |
| 23 if (*it == browser) | 29 if (*it == browser) |
| 24 continue; | 30 continue; |
| 25 found = *it; | 31 found = *it; |
| 26 } | 32 } |
| 27 | 33 |
| 28 return found; | 34 return found; |
| 29 } | 35 } |
| 30 | 36 |
| 31 } // namespace | 37 } // namespace |
| 32 | 38 |
| 33 class ExtensionManagementApiTest : public ExtensionApiTest { | 39 class ExtensionManagementApiTest : public ExtensionApiTest { |
| 34 public: | 40 public: |
| 35 virtual void SetUpCommandLine(CommandLine* command_line) { | 41 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 36 ExtensionApiTest::SetUpCommandLine(command_line); | 42 ExtensionApiTest::SetUpCommandLine(command_line); |
| 37 command_line->AppendSwitch(switches::kEnablePanels); | 43 command_line->AppendSwitch(switches::kEnablePanels); |
| 38 } | 44 } |
| 39 | 45 |
| 40 virtual void InstallExtensions() { | 46 virtual void InstallExtensions() { |
| 41 FilePath basedir = test_data_dir_.AppendASCII("management"); | 47 FilePath basedir = test_data_dir_.AppendASCII("management"); |
| 42 | 48 |
| 43 // Load 4 enabled items. | 49 // Load 4 enabled items. |
| 44 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("enabled_extension"))); | 50 InstallNamedExtension(basedir, "enabled_extension"); |
| 45 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("enabled_app"))); | 51 InstallNamedExtension(basedir, "enabled_app"); |
| 46 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("description"))); | 52 InstallNamedExtension(basedir, "description"); |
| 47 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("permissions"))); | 53 InstallNamedExtension(basedir, "permissions"); |
| 48 | 54 |
| 49 // Load 2 disabled items. | 55 // Load 2 disabled items. |
| 50 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("disabled_extension"))); | 56 InstallNamedExtension(basedir, "disabled_extension"); |
| 51 DisableExtension(last_loaded_extension_id_); | 57 DisableExtension(extension_ids_["disabled_extension"]); |
| 52 ASSERT_TRUE(LoadExtension(basedir.AppendASCII("disabled_app"))); | 58 InstallNamedExtension(basedir, "disabled_app"); |
| 53 DisableExtension(last_loaded_extension_id_); | 59 DisableExtension(extension_ids_["disabled_app"]); |
| 54 } | 60 } |
| 55 | 61 |
| 56 // Load an app, and wait for a message from app "management/launch_on_install" | 62 // Load an app, and wait for a message from app "management/launch_on_install" |
| 57 // indicating that the new app has been launched. | 63 // indicating that the new app has been launched. |
| 58 void LoadAndWaitForLaunch(const std::string& app_path, | 64 void LoadAndWaitForLaunch(const std::string& app_path, |
| 59 std::string* out_app_id) { | 65 std::string* out_app_id) { |
| 60 ExtensionTestMessageListener launched_app("launched app", false); | 66 ExtensionTestMessageListener launched_app("launched app", false); |
| 61 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_path))); | 67 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_path))); |
| 62 | 68 |
| 63 if (out_app_id) | 69 if (out_app_id) |
| 64 *out_app_id = last_loaded_extension_id_; | 70 *out_app_id = last_loaded_extension_id_; |
| 65 | 71 |
| 66 ASSERT_TRUE(launched_app.WaitUntilSatisfied()); | 72 ASSERT_TRUE(launched_app.WaitUntilSatisfied()); |
| 67 } | 73 } |
| 74 |
| 75 protected: |
| 76 void InstallNamedExtension(FilePath basedir, std::string name) { |
| 77 const Extension* extension = LoadExtension(basedir.AppendASCII(name)); |
| 78 ASSERT_TRUE(extension); |
| 79 extension_ids_[name] = extension->id(); |
| 80 } |
| 81 |
| 82 // Maps installed extension names to their IDs.. |
| 83 std::map<std::string, std::string> extension_ids_; |
| 68 }; | 84 }; |
| 69 | 85 |
| 70 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) { | 86 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) { |
| 71 InstallExtensions(); | 87 InstallExtensions(); |
| 72 ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html")); | 88 ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html")); |
| 73 } | 89 } |
| 74 | 90 |
| 75 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Uninstall) { | 91 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Uninstall) { |
| 76 InstallExtensions(); | 92 InstallExtensions(); |
| 77 ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html")); | 93 ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html")); |
| 78 } | 94 } |
| 79 | 95 |
| 96 // Tests actions on extensions when no management policy is in place. |
| 97 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, ManagementPolicyAllowed) { |
| 98 InstallExtensions(); |
| 99 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 100 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"], |
| 101 false)); |
| 102 |
| 103 // Ensure that all actions are allowed. |
| 104 ExtensionSystem::Get( |
| 105 browser()->profile())->management_policy()->UnregisterAllProviders(); |
| 106 |
| 107 ASSERT_TRUE(RunExtensionSubtest("management/management_policy", |
| 108 "allowed.html")); |
| 109 // The last thing the test does is uninstall the "enabled_extension". |
| 110 EXPECT_FALSE(service->GetExtensionById(extension_ids_["enabled_extension"], |
| 111 true)); |
| 112 } |
| 113 |
| 114 // Tests actions on extensions when management policy prohibits those actions. |
| 115 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, ManagementPolicyProhibited) { |
| 116 InstallExtensions(); |
| 117 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 118 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"], |
| 119 false)); |
| 120 |
| 121 // Prohibit status changes. |
| 122 extensions::ManagementPolicy* policy = |
| 123 ExtensionSystem::Get(browser()->profile())->management_policy(); |
| 124 policy->UnregisterAllProviders(); |
| 125 extensions::TestManagementPolicyProvider provider( |
| 126 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS); |
| 127 policy->RegisterProvider(&provider); |
| 128 ASSERT_TRUE(RunExtensionSubtest("management/management_policy", |
| 129 "prohibited.html")); |
| 130 } |
| 131 |
| 80 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, LaunchPanelApp) { | 132 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, LaunchPanelApp) { |
| 81 ExtensionService* service = browser()->profile()->GetExtensionService(); | 133 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 82 | 134 |
| 83 // Load an extension that calls launchApp() on any app that gets | 135 // Load an extension that calls launchApp() on any app that gets |
| 84 // installed. | 136 // installed. |
| 85 ExtensionTestMessageListener launcher_loaded("launcher loaded", false); | 137 ExtensionTestMessageListener launcher_loaded("launcher loaded", false); |
| 86 ASSERT_TRUE(LoadExtension( | 138 ASSERT_TRUE(LoadExtension( |
| 87 test_data_dir_.AppendASCII("management/launch_on_install"))); | 139 test_data_dir_.AppendASCII("management/launch_on_install"))); |
| 88 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied()); | 140 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied()); |
| 89 | 141 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 ASSERT_EQ(2, browser()->tab_count()); | 231 ASSERT_EQ(2, browser()->tab_count()); |
| 180 #else | 232 #else |
| 181 // Find the app's browser. Opening in a new window will create | 233 // Find the app's browser. Opening in a new window will create |
| 182 // a new browser. | 234 // a new browser. |
| 183 ASSERT_EQ(2u, browser::GetBrowserCount(browser()->profile())); | 235 ASSERT_EQ(2u, browser::GetBrowserCount(browser()->profile())); |
| 184 Browser* app_browser = FindOtherBrowser(browser()); | 236 Browser* app_browser = FindOtherBrowser(browser()); |
| 185 ASSERT_TRUE(app_browser->is_app()); | 237 ASSERT_TRUE(app_browser->is_app()); |
| 186 ASSERT_FALSE(app_browser->is_type_panel()); | 238 ASSERT_FALSE(app_browser->is_type_panel()); |
| 187 #endif | 239 #endif |
| 188 } | 240 } |
| OLD | NEW |