Commit 59e03360 authored by tessamac@google.com's avatar tessamac@google.com

Tests for incognito app install, plus some cleanup.

BUG=62752
TEST=browser_tests

Review URL: http://codereview.chromium.org/5543001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72192 0039d316-1c4b-4281-b951-d872f2087c98
parent 2df1b365
...@@ -8,10 +8,12 @@ ...@@ -8,10 +8,12 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/file_path.h" #include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/string_number_conversions.h" #include "base/string_number_conversions.h"
#include "chrome/browser/browser_window.h" #include "chrome/browser/browser_window.h"
#include "chrome/browser/extensions/crx_installer.h" #include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_creator.h"
#include "chrome/browser/extensions/extension_error_reporter.h" #include "chrome/browser/extensions/extension_error_reporter.h"
#include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_install_ui.h" #include "chrome/browser/extensions/extension_install_ui.h"
...@@ -101,6 +103,40 @@ bool ExtensionBrowserTest::LoadExtensionIncognito(const FilePath& path) { ...@@ -101,6 +103,40 @@ bool ExtensionBrowserTest::LoadExtensionIncognito(const FilePath& path) {
return LoadExtensionImpl(path, true); return LoadExtensionImpl(path, true);
} }
FilePath ExtensionBrowserTest::PackExtension(const FilePath& dir_path) {
FilePath crx_path;
if (!PathService::Get(base::DIR_TEMP, &crx_path)) {
ADD_FAILURE() << "Failed to get DIR_TEMP from PathService.";
return FilePath();
}
crx_path = crx_path.AppendASCII("temp.crx");
if (!file_util::Delete(crx_path, false)) {
ADD_FAILURE() << "Failed to delete crx: " << crx_path.value();
return FilePath();
}
FilePath pem_path = crx_path.DirName().AppendASCII("temp.pem");
if (!file_util::Delete(pem_path, false)) {
ADD_FAILURE() << "Failed to delete pem: " << pem_path.value();
return FilePath();
}
scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
if (!creator->Run(dir_path,
crx_path,
FilePath(), // no existing pem, use empty path
pem_path)) {
ADD_FAILURE() << "ExtensionCreator::Run() failed.";
return FilePath();
}
if (!file_util::PathExists(crx_path)) {
ADD_FAILURE() << crx_path.value() << " was not created.";
return FilePath();
}
return crx_path;
}
// This class is used to simulate an installation abort by the user. // This class is used to simulate an installation abort by the user.
class MockAbortExtensionInstallUI : public ExtensionInstallUI { class MockAbortExtensionInstallUI : public ExtensionInstallUI {
public: public:
...@@ -120,11 +156,31 @@ class MockAbortExtensionInstallUI : public ExtensionInstallUI { ...@@ -120,11 +156,31 @@ class MockAbortExtensionInstallUI : public ExtensionInstallUI {
virtual void OnInstallFailure(const std::string& error) {} virtual void OnInstallFailure(const std::string& error) {}
}; };
class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
public:
MockAutoConfirmExtensionInstallUI(Profile* profile) :
ExtensionInstallUI(profile) {}
// Proceed without confirmation prompt.
virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
delegate->InstallUIProceed();
}
};
bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id, bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id,
const FilePath& path, const FilePath& path,
InstallUIType ui_type, InstallUIType ui_type,
int expected_change) { int expected_change) {
ExtensionService* service = browser()->profile()->GetExtensionService(); return InstallOrUpdateExtension(id, path, ui_type, expected_change,
browser()->profile());
}
bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id,
const FilePath& path,
InstallUIType ui_type,
int expected_change,
Profile* profile) {
ExtensionService* service = profile->GetExtensionService();
service->set_show_extensions_prompts(false); service->set_show_extensions_prompts(false);
size_t num_before = service->extensions()->size(); size_t num_before = service->extensions()->size();
...@@ -141,12 +197,23 @@ bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id, ...@@ -141,12 +197,23 @@ bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id,
if (ui_type == INSTALL_UI_TYPE_CANCEL) if (ui_type == INSTALL_UI_TYPE_CANCEL)
install_ui = new MockAbortExtensionInstallUI(); install_ui = new MockAbortExtensionInstallUI();
else if (ui_type == INSTALL_UI_TYPE_NORMAL) else if (ui_type == INSTALL_UI_TYPE_NORMAL)
install_ui = new ExtensionInstallUI(browser()->profile()); install_ui = new ExtensionInstallUI(profile);
else if (ui_type == INSTALL_UI_TYPE_AUTO_CONFIRM)
install_ui = new MockAutoConfirmExtensionInstallUI(profile);
// TODO(tessamac): Update callers to always pass an unpacked extension
// and then always pack the extension here.
FilePath crx_path = path;
if (crx_path.Extension() != FILE_PATH_LITERAL(".crx")) {
crx_path = PackExtension(path);
}
if (crx_path.empty())
return false;
scoped_refptr<CrxInstaller> installer( scoped_refptr<CrxInstaller> installer(
new CrxInstaller(service, install_ui)); new CrxInstaller(service, install_ui));
installer->set_expected_id(id); installer->set_expected_id(id);
installer->InstallCrx(path); installer->InstallCrx(crx_path);
ui_test_utils::RunMessageLoop(); ui_test_utils::RunMessageLoop();
} }
......
...@@ -29,6 +29,10 @@ class ExtensionBrowserTest ...@@ -29,6 +29,10 @@ class ExtensionBrowserTest
// Same as above, but enables the extension in incognito mode first. // Same as above, but enables the extension in incognito mode first.
bool LoadExtensionIncognito(const FilePath& path); bool LoadExtensionIncognito(const FilePath& path);
// Pack the extension in |dir_path| into a crx file and return its path.
// Return an empty FilePath if there were errors.
FilePath PackExtension(const FilePath& dir_path);
// |expected_change| indicates how many extensions should be installed (or // |expected_change| indicates how many extensions should be installed (or
// disabled, if negative). // disabled, if negative).
// 1 means you expect a new install, 0 means you expect an upgrade, -1 means // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
...@@ -52,6 +56,12 @@ class ExtensionBrowserTest ...@@ -52,6 +56,12 @@ class ExtensionBrowserTest
return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL, return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL,
expected_change); expected_change);
} }
bool InstallExtensionWithUIAutoConfirm(const FilePath& path,
int expected_change,
Profile* profile) {
return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_AUTO_CONFIRM,
expected_change, profile);
}
// Begins install process but simulates a user cancel. // Begins install process but simulates a user cancel.
bool StartInstallButCancel(const FilePath& path) { bool StartInstallButCancel(const FilePath& path) {
...@@ -109,11 +119,16 @@ class ExtensionBrowserTest ...@@ -109,11 +119,16 @@ class ExtensionBrowserTest
INSTALL_UI_TYPE_NONE, INSTALL_UI_TYPE_NONE,
INSTALL_UI_TYPE_CANCEL, INSTALL_UI_TYPE_CANCEL,
INSTALL_UI_TYPE_NORMAL, INSTALL_UI_TYPE_NORMAL,
INSTALL_UI_TYPE_AUTO_CONFIRM,
}; };
bool InstallOrUpdateExtension(const std::string& id, const FilePath& path, bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
InstallUIType ui_type, InstallUIType ui_type,
int expected_change); int expected_change);
bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
InstallUIType ui_type,
int expected_change,
Profile* profile);
bool LoadExtensionImpl(const FilePath& path, bool incognito_enabled); bool LoadExtensionImpl(const FilePath& path, bool incognito_enabled);
bool WaitForExtensionHostsToLoad(); bool WaitForExtensionHostsToLoad();
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/string_util.h"
#include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/theme_installed_infobar_delegate.h" #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
...@@ -60,3 +61,41 @@ IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest, ...@@ -60,3 +61,41 @@ IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
VerifyThemeInfoBarAndUndoInstall(); VerifyThemeInfoBarAndUndoInstall();
ASSERT_EQ(NULL, browser()->profile()->GetTheme()); ASSERT_EQ(NULL, browser()->profile()->GetTheme());
} }
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
AppInstallConfirmation) {
int num_tabs = browser()->tab_count();
FilePath app_dir = test_data_dir_.AppendASCII("app");
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir, 1,
browser()->profile()));
EXPECT_EQ(num_tabs + 1, browser()->tab_count());
TabContents* tab_contents = browser()->GetSelectedTabContents();
ASSERT_TRUE(tab_contents);
EXPECT_TRUE(StartsWithASCII(tab_contents->GetURL().spec(),
"chrome://newtab/#app-id=", // id changes
false));
}
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
AppInstallConfirmation_Incognito) {
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
Browser* incognito_browser = Browser::GetOrCreateTabbedBrowser(
incognito_profile);
int num_incognito_tabs = incognito_browser->tab_count();
int num_normal_tabs = browser()->tab_count();
FilePath app_dir = test_data_dir_.AppendASCII("app");
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir, 1,
incognito_profile));
EXPECT_EQ(num_incognito_tabs, incognito_browser->tab_count());
EXPECT_EQ(num_normal_tabs + 1, browser()->tab_count());
TabContents* tab_contents = browser()->GetSelectedTabContents();
ASSERT_TRUE(tab_contents);
EXPECT_TRUE(StartsWithASCII(tab_contents->GetURL().spec(),
"chrome://newtab/#app-id=", // id changes
false));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment