This source file includes following definitions.
- FindOtherBrowser
- SetUpCommandLine
- LoadExtensions
- LoadAndWaitForLaunch
- LoadNamedExtension
- InstallNamedExtension
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <map>
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/management/management_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_management_policy.h"
#include "extensions/common/manifest.h"
using extensions::Extension;
using extensions::Manifest;
namespace {
Browser* FindOtherBrowser(Browser* browser) {
Browser* found = NULL;
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
if (*it == browser)
continue;
found = *it;
}
return found;
}
}
class ExtensionManagementApiTest : public ExtensionApiTest {
public:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnablePanels);
}
virtual void LoadExtensions() {
base::FilePath basedir = test_data_dir_.AppendASCII("management");
LoadNamedExtension(basedir, "enabled_extension");
LoadNamedExtension(basedir, "enabled_app");
LoadNamedExtension(basedir, "description");
LoadNamedExtension(basedir, "permissions");
LoadNamedExtension(basedir, "short_name");
LoadNamedExtension(basedir, "disabled_extension");
DisableExtension(extension_ids_["disabled_extension"]);
LoadNamedExtension(basedir, "disabled_app");
DisableExtension(extension_ids_["disabled_app"]);
}
void LoadAndWaitForLaunch(const std::string& app_path,
std::string* out_app_id) {
ExtensionTestMessageListener launched_app("launched app", false);
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_path)));
if (out_app_id)
*out_app_id = last_loaded_extension_id();
ASSERT_TRUE(launched_app.WaitUntilSatisfied());
}
protected:
void LoadNamedExtension(const base::FilePath& path,
const std::string& name) {
const Extension* extension = LoadExtension(path.AppendASCII(name));
ASSERT_TRUE(extension);
extension_ids_[name] = extension->id();
}
void InstallNamedExtension(const base::FilePath& path,
const std::string& name,
Manifest::Location install_source) {
const Extension* extension = InstallExtension(path.AppendASCII(name), 1,
install_source);
ASSERT_TRUE(extension);
extension_ids_[name] = extension->id();
}
std::map<std::string, std::string> extension_ids_;
};
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) {
LoadExtensions();
base::FilePath basedir = test_data_dir_.AppendASCII("management");
InstallNamedExtension(basedir, "internal_extension", Manifest::INTERNAL);
InstallNamedExtension(basedir, "external_extension",
Manifest::EXTERNAL_PREF);
InstallNamedExtension(basedir, "admin_extension",
Manifest::EXTERNAL_POLICY_DOWNLOAD);
ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html"));
}
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, NoPermission) {
LoadExtensions();
ASSERT_TRUE(RunExtensionSubtest("management/no_permission", "test.html"));
}
#if defined(OS_WIN)
#define MAYBE_Uninstall DISABLED_Uninstall
#else
#define MAYBE_Uninstall Uninstall
#endif
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, MAYBE_Uninstall) {
LoadExtensions();
extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true);
ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html"));
}
#if defined(OS_WIN)
#define MAYBE_ManagementPolicyAllowed DISABLED_ManagementPolicyAllowed
#else
#define MAYBE_ManagementPolicyAllowed ManagementPolicyAllowed
#endif
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest,
MAYBE_ManagementPolicyAllowed) {
LoadExtensions();
extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true);
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"],
false));
extensions::ExtensionSystem::Get(
browser()->profile())->management_policy()->UnregisterAllProviders();
ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
"allowed.html"));
EXPECT_FALSE(service->GetExtensionById(extension_ids_["enabled_extension"],
true));
}
#if defined(OS_WIN)
#define MAYBE_ManagementPolicyProhibited DISABLED_ManagementPolicyProhibited
#else
#define MAYBE_ManagementPolicyProhibited ManagementPolicyProhibited
#endif
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest,
MAYBE_ManagementPolicyProhibited) {
LoadExtensions();
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"],
false));
extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get(
browser()->profile())->management_policy();
policy->UnregisterAllProviders();
extensions::TestManagementPolicyProvider provider(
extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
policy->RegisterProvider(&provider);
ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
"prohibited.html"));
}
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, DISABLED_LaunchPanelApp) {
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
ExtensionTestMessageListener launcher_loaded("launcher loaded", false);
ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("management/launch_on_install")));
ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied());
std::string app_id;
LoadAndWaitForLaunch("management/launch_app_panel", &app_id);
ASSERT_FALSE(HasFatalFailure());
ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
Browser* app_browser = FindOtherBrowser(browser());
ASSERT_TRUE(app_browser->is_type_popup());
ASSERT_TRUE(app_browser->is_app());
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_BROWSER_CLOSED,
content::Source<Browser>(app_browser));
chrome::CloseWindow(app_browser);
signal.Wait();
UninstallExtension(app_id);
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
ASSERT_FALSE(service->GetExtensionById(app_id, true));
extensions::SetLaunchType(service, app_id, extensions::LAUNCH_TYPE_REGULAR);
std::string app_id_new;
LoadAndWaitForLaunch("management/launch_app_panel", &app_id_new);
ASSERT_FALSE(HasFatalFailure());
ASSERT_EQ(app_id, app_id_new);
ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
app_browser = FindOtherBrowser(browser());
ASSERT_TRUE(app_browser->is_type_popup());
ASSERT_TRUE(app_browser->is_app());
}
#if defined(OS_WIN)
#define MAYBE_LaunchTabApp DISABLED_LaunchTabApp
#else
#define MAYBE_LaunchTabApp LaunchTabApp
#endif
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, MAYBE_LaunchTabApp) {
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
ExtensionTestMessageListener launcher_loaded("launcher loaded", false);
ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("management/launch_on_install")));
ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied());
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
ASSERT_EQ(1, browser()->tab_strip_model()->count());
std::string app_id;
LoadAndWaitForLaunch("management/launch_app_tab", &app_id);
ASSERT_FALSE(HasFatalFailure());
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
ASSERT_EQ(2, browser()->tab_strip_model()->count());
UninstallExtension(app_id);
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
ASSERT_FALSE(service->GetExtensionById(app_id, true));
extensions::SetLaunchType(service, app_id, extensions::LAUNCH_TYPE_WINDOW);
std::string app_id_new;
LoadAndWaitForLaunch("management/launch_app_tab", &app_id_new);
ASSERT_FALSE(HasFatalFailure());
ASSERT_EQ(app_id, app_id_new);
#if defined(OS_MACOSX)
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
ASSERT_EQ(2, browser()->tab_strip_model()->count());
#else
ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
browser()->host_desktop_type()));
Browser* app_browser = FindOtherBrowser(browser());
ASSERT_TRUE(app_browser->is_app());
#endif
}