Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4014)

Unified Diff: chrome/installer/setup/install_unittest.cc

Issue 10836247: Refactor ShellUtil shortcut code -- single multi-purpose methods as opposed to many slighlty diffe… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: brand new shell_util shortcut API + TESTS :)! Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/installer/setup/install_unittest.cc
diff --git a/chrome/installer/setup/install_unittest.cc b/chrome/installer/setup/install_unittest.cc
index 8d4606f8c32d9015fa745e3adf3b24b7464e8bcc..e6feb765deeffa445739b23e2dcb5e457780f95c 100644
--- a/chrome/installer/setup/install_unittest.cc
+++ b/chrome/installer/setup/install_unittest.cc
@@ -2,16 +2,27 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <objbase.h>
+
#include <string>
+#include "base/file_path.h"
#include "base/file_util.h"
+#include "base/path_service.h"
#include "base/platform_file.h"
#include "base/scoped_temp_dir.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "base/version.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/test/test_shortcut_win.h"
+#include "base/win/shortcut.h"
#include "chrome/installer/setup/install.h"
+#include "chrome/installer/setup/install_worker.h"
#include "chrome/installer/setup/setup_constants.h"
+#include "chrome/installer/util/browser_distribution.h"
+#include "chrome/installer/util/installer_state.h"
+#include "chrome/installer/util/product.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -49,6 +60,143 @@ class CreateVisualElementsManifestTest : public testing::Test {
FilePath manifest_path_;
};
+class InstallShorcutTest : public testing::Test {
+ protected:
+ // A mock installer state on which .system_install() will return
+ // |system_install|, .target_path() will return |target_path|, and .is_msi()
+ // will return |is_msi|.
+ class MockInstallerState : public installer::InstallerState {
+ public:
+ MockInstallerState(const FilePath& target_path,
+ bool system_install,
+ bool is_msi) : InstallerState() {
+ target_path_ = target_path;
+ set_level(system_install ? SYSTEM_LEVEL : USER_LEVEL);
+ msi_ = is_msi;
+ }
+ };
+
+ virtual void SetUp() OVERRIDE {
+ EXPECT_EQ(S_OK, CoInitialize(NULL));
+
+ BrowserDistribution* dist = BrowserDistribution::GetDistribution();
+ ASSERT_TRUE(dist != NULL);
+ product_.reset(new installer::Product(dist));
+
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ chrome_exe_ = temp_dir_.path().Append(installer::kChromeExe);
+ setup_exe_ = temp_dir_.path().Append(installer::kSetupExe);
+ EXPECT_EQ(0, file_util::WriteFile(chrome_exe_, "", 0));
+ EXPECT_EQ(0, file_util::WriteFile(setup_exe_, "", 0));
+
+ mock_user_installer_state_.reset(
+ new MockInstallerState(chrome_exe_.DirName(), false, false));
+ mock_system_installer_state_.reset(
+ new MockInstallerState(chrome_exe_.DirName(), true, false));
+
+ expected_properties_.set_target(chrome_exe_);
+ expected_properties_.set_dual_mode(true);
+
+ ASSERT_TRUE(fake_user_desktop_.CreateUniqueTempDir());
+ ASSERT_TRUE(fake_common_desktop_.CreateUniqueTempDir());
+ ASSERT_TRUE(fake_user_quick_launch_.CreateUniqueTempDir());
+ ASSERT_TRUE(fake_default_user_quick_launch_.CreateUniqueTempDir());
+ ASSERT_TRUE(fake_start_menu_.CreateUniqueTempDir());
+ ASSERT_TRUE(fake_common_start_menu_.CreateUniqueTempDir());
+ ASSERT_TRUE(PathService::Override(base::DIR_USER_DESKTOP,
+ fake_user_desktop_.path()));
+ ASSERT_TRUE(PathService::Override(base::DIR_COMMON_DESKTOP,
+ fake_common_desktop_.path()));
+ ASSERT_TRUE(PathService::Override(base::DIR_USER_QUICK_LAUNCH,
+ fake_user_quick_launch_.path()));
+ ASSERT_TRUE(PathService::Override(base::DIR_DEFAULT_USER_QUICK_LAUNCH,
+ fake_default_user_quick_launch_.path()));
+ ASSERT_TRUE(PathService::Override(base::DIR_START_MENU,
+ fake_start_menu_.path()));
+ ASSERT_TRUE(PathService::Override(base::DIR_COMMON_START_MENU,
+ fake_common_start_menu_.path()));
+
+ string16 shortcut_name(dist->GetAppShortCutName());
+ string16 uninstall_shorcut_name(dist->GetUninstallLinkName());
+ string16 alternate_shortcut_name(dist->GetAlternateApplicationName());
+ shortcut_name.append(L".lnk");
+ uninstall_shorcut_name.append(L".lnk");
+ alternate_shortcut_name.append(L".lnk");
+
+ user_desktop_shortcut_ =
+ fake_user_desktop_.path().Append(shortcut_name);
+ user_quick_launch_shortcut_ =
+ fake_user_quick_launch_.path().Append(shortcut_name);
+ user_start_menu_shortcut_ =
+ fake_start_menu_.path().Append(dist->GetAppShortCutName())
+ .Append(shortcut_name);
+ user_uninstall_shortcut_ =
+ fake_start_menu_.path().Append(dist->GetAppShortCutName())
+ .Append(uninstall_shorcut_name);
+ system_desktop_shortcut_ =
+ fake_common_desktop_.path().Append(shortcut_name);
+ system_quick_launch_shortcut_ =
+ fake_default_user_quick_launch_.path().Append(shortcut_name);
+ system_start_menu_shortcut_ =
+ fake_common_start_menu_.path().Append(dist->GetAppShortCutName())
+ .Append(shortcut_name);
+ system_uninstall_shortcut_ =
+ fake_common_start_menu_.path().Append(dist->GetAppShortCutName())
+ .Append(uninstall_shorcut_name);
+ user_alternate_desktop_shortcut_ =
+ fake_user_desktop_.path().Append(alternate_shortcut_name);
+ }
+
+ virtual void TearDown() OVERRIDE {
+ // Try to unpin potentially pinned shortcuts (although pinning isn't tested,
+ // the call itself might still have pinned the Start Menu shortcuts).
+ base::win::TaskbarUnpinShortcutLink(
+ user_start_menu_shortcut_.value().c_str());
+ base::win::TaskbarUnpinShortcutLink(
+ system_start_menu_shortcut_.value().c_str());
+ CoUninitialize();
+ }
+
+ base::win::ShortcutProperties GetExpectedUninstallShortcutProperties(
+ bool system_level) {
+ CommandLine arguments(CommandLine::NO_PROGRAM);
+ const installer::InstallerState& installer_state = system_level ?
+ *mock_system_installer_state_ : *mock_user_installer_state_;
+ AppendUninstallCommandLineFlags(installer_state, *product_, &arguments);
+
+ base::win::ShortcutProperties properties;
+ properties.set_target(setup_exe_);
+ properties.set_arguments(arguments.GetCommandLineString());
+ return properties;
+ }
+
+ base::win::ShortcutProperties expected_properties_;
+
+ FilePath chrome_exe_;
+ FilePath setup_exe_;
+ scoped_ptr<installer::Product> product_;
+ scoped_ptr<MockInstallerState> mock_user_installer_state_;
+ scoped_ptr<MockInstallerState> mock_system_installer_state_;
+
+ ScopedTempDir temp_dir_;
+ ScopedTempDir fake_user_desktop_;
+ ScopedTempDir fake_common_desktop_;
+ ScopedTempDir fake_user_quick_launch_;
+ ScopedTempDir fake_default_user_quick_launch_;
+ ScopedTempDir fake_start_menu_;
+ ScopedTempDir fake_common_start_menu_;
+
+ FilePath user_desktop_shortcut_;
+ FilePath user_quick_launch_shortcut_;
+ FilePath user_start_menu_shortcut_;
+ FilePath user_uninstall_shortcut_;
+ FilePath system_desktop_shortcut_;
+ FilePath system_quick_launch_shortcut_;
+ FilePath system_start_menu_shortcut_;
+ FilePath system_uninstall_shortcut_;
+ FilePath user_alternate_desktop_shortcut_;
+};
+
} // namespace
// Test that VisualElementsManifest.xml is not created when VisualElements are
@@ -88,6 +236,137 @@ TEST_F(CreateVisualElementsManifestTest, VisualElementsManifestCreated) {
ASSERT_STREQ(kExpectedManifest, read_manifest.c_str());
}
+TEST_F(InstallShorcutTest, CreateAllShortcuts) {
+ CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_CREATE_ALL, false);
+ base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
+ base::win::ValidateShortcut(user_quick_launch_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(false));
+ base::win::ValidateShortcut(user_uninstall_shortcut_,
+ expected_uninstall_properties);
+}
+
+TEST_F(InstallShorcutTest, CreateAllShortcutsSystemLevel) {
+ CreateOrUpdateShortcuts(*mock_system_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_CREATE_ALL, false);
+ base::win::ValidateShortcut(system_desktop_shortcut_, expected_properties_);
+ base::win::ValidateShortcut(system_quick_launch_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(system_start_menu_shortcut_,
+ expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(true));
+ base::win::ValidateShortcut(system_uninstall_shortcut_,
+ expected_uninstall_properties);
+}
+
+TEST_F(InstallShorcutTest, CreateAllShortcutsAlternateDesktopName) {
+ CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_CREATE_ALL, true);
+ base::win::ValidateShortcut(user_alternate_desktop_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(user_quick_launch_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(user_start_menu_shortcut_,
+ expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(false));
+ base::win::ValidateShortcut(user_uninstall_shortcut_,
+ expected_uninstall_properties);
+}
+
+TEST_F(InstallShorcutTest, CreateMandatoryShortcuts) {
+ CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_CREATE_MANDATORY, false);
+ ASSERT_FALSE(file_util::PathExists(user_desktop_shortcut_));
+ ASSERT_FALSE(file_util::PathExists(user_quick_launch_shortcut_));
+ base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(false));
+ base::win::ValidateShortcut(user_uninstall_shortcut_,
+ expected_uninstall_properties);
+}
+
+TEST_F(InstallShorcutTest, ReplaceAll) {
+ base::win::ShortcutProperties dummy_properties;
+ FilePath dummy_target;
+ ASSERT_TRUE(
+ file_util::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target));
+ dummy_properties.set_target(dummy_target);
+ dummy_properties.set_working_dir(fake_user_desktop_.path());
+ dummy_properties.set_arguments(L"--dummy --args");
+ dummy_properties.set_app_id(L"El.Dummiest");
+
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_desktop_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_quick_launch_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+ ASSERT_TRUE(file_util::CreateDirectory(user_start_menu_shortcut_.DirName()));
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_start_menu_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_uninstall_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+
+ CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_REPLACE_EXISTING, false);
+ base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
+ base::win::ValidateShortcut(user_quick_launch_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(user_start_menu_shortcut_,
+ expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(false));
+ base::win::ValidateShortcut(user_uninstall_shortcut_,
+ expected_uninstall_properties);
+}
+
+TEST_F(InstallShorcutTest, ReplaceExisting) {
+ base::win::ShortcutProperties dummy_properties;
+ FilePath dummy_target;
+ ASSERT_TRUE(
+ file_util::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target));
+ dummy_properties.set_target(dummy_target);
+ dummy_properties.set_working_dir(fake_user_desktop_.path());
+ dummy_properties.set_arguments(L"--dummy --args");
+ dummy_properties.set_app_id(L"El.Dummiest");
+
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_desktop_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+ ASSERT_TRUE(file_util::CreateDirectory(user_start_menu_shortcut_.DirName()));
+ ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
+ user_uninstall_shortcut_, dummy_properties,
+ base::win::SHORTCUT_CREATE_ALWAYS));
+
+ CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_REPLACE_EXISTING, false);
+ base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
+ base::win::ShortcutProperties expected_uninstall_properties(
+ GetExpectedUninstallShortcutProperties(false));
+ base::win::ValidateShortcut(user_uninstall_shortcut_,
+ expected_uninstall_properties);
+ ASSERT_FALSE(file_util::PathExists(user_quick_launch_shortcut_));
+ ASSERT_FALSE(file_util::PathExists(user_start_menu_shortcut_));
+}
+
+TEST_F(InstallShorcutTest, NoUninstallLinkForMSIInstalls) {
+ MockInstallerState no_msi_installer_state(chrome_exe_.DirName(), false, true);
+ CreateOrUpdateShortcuts(no_msi_installer_state, setup_exe_, *product_,
+ installer::INSTALL_SHORTCUT_CREATE_ALL, false);
+ base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
+ base::win::ValidateShortcut(user_quick_launch_shortcut_,
+ expected_properties_);
+ base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
+ ASSERT_FALSE(file_util::PathExists(user_uninstall_shortcut_));
+}
+
TEST(EscapeXmlAttributeValueTest, EscapeCrazyValue) {
string16 val(L"This has 'crazy' \"chars\" && < and > signs.");
static const wchar_t kExpectedEscapedVal[] =

Powered by Google App Engine
This is Rietveld 408576698