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