Sharing shell_util_unittest code to verify shortcuts for creation of profile...

Sharing shell_util_unittest code to verify shortcuts for creation of profile shortcuts for Windows. profile_manager_unittest needs to be able to verify shortcuts created as well. 

BUG=NONE
TEST=installer_util_unittests --gtest_filter=ShellUtilTest*

Review URL: https://chromiumcodereview.appspot.com/10826188

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151091 0039d316-1c4b-4281-b951-d872f2087c98
parent 9160e2aa
......@@ -1657,6 +1657,45 @@ bool ShellUtil::UpdateChromeShortcut(BrowserDistribution* dist,
ConvertShellUtilShortcutOptionsToFileUtil(options));
}
ShellUtil::VerifyShortcutStatus ShellUtil::VerifyChromeShortcut(
const string16& exe_path, const string16& shortcut,
const string16& description, int icon_index) {
base::win::ScopedComPtr<IShellLink> i_shell_link;
base::win::ScopedComPtr<IPersistFile> i_persist_file;
wchar_t long_path[MAX_PATH] = {0};
wchar_t short_path[MAX_PATH] = {0};
wchar_t file_path[MAX_PATH] = {0};
wchar_t icon_path[MAX_PATH] = {0};
wchar_t desc[MAX_PATH] = {0};
int index = 0;
// Get the shortcut's properties.
if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER)) ||
FAILED(i_persist_file.QueryFrom(i_shell_link)) ||
FAILED(i_persist_file->Load(shortcut.c_str(), 0)) ||
::GetLongPathName(exe_path.c_str(), long_path, MAX_PATH) == 0 ||
::GetShortPathName(exe_path.c_str(), short_path, MAX_PATH) == 0 ||
FAILED(i_shell_link->GetPath(file_path, MAX_PATH, NULL,
SLGP_UNCPRIORITY)) ||
FAILED(i_shell_link->GetIconLocation(icon_path, MAX_PATH, &index)) ||
FAILED(i_shell_link->GetDescription(desc, MAX_PATH)) ||
FAILED(i_shell_link->GetDescription(desc, MAX_PATH)))
return VERIFY_SHORTCUT_FAILURE_UNEXPECTED;
FilePath path(file_path);
if (path != FilePath(long_path) && path != FilePath(short_path))
return VERIFY_SHORTCUT_FAILURE_PATH;
if (string16(desc) != string16(description))
return VERIFY_SHORTCUT_FAILURE_DESCRIPTION;
if (index != icon_index)
return VERIFY_SHORTCUT_FAILURE_ICON_INDEX;
return VERIFY_SHORTCUT_SUCCESS;
}
bool ShellUtil::GetUserSpecificRegistrySuffix(string16* suffix) {
// Use a thread-safe cache for the user's suffix.
static base::LazyInstance<UserSpecificRegistrySuffix>::Leaky suffix_instance =
......
......@@ -35,6 +35,14 @@ class ShellUtil {
SYSTEM_LEVEL = 0x2 // Make any shell changes only at the system level
};
enum VerifyShortcutStatus {
VERIFY_SHORTCUT_SUCCESS = 0,
VERIFY_SHORTCUT_FAILURE_UNEXPECTED,
VERIFY_SHORTCUT_FAILURE_PATH,
VERIFY_SHORTCUT_FAILURE_DESCRIPTION,
VERIFY_SHORTCUT_FAILURE_ICON_INDEX,
};
// Relative path of the URL Protocol registry entry (prefixed with '\').
static const wchar_t* kRegURLProtocol;
......@@ -412,6 +420,16 @@ class ShellUtil {
int icon_index,
uint32 options);
// Verify that a shortcut exists with the expected information.
// |exe_path| The shortcut's exe.
// |shortcut| The path to the shortcut.
// |description| The shortcut's description.
// |icon_index| The icon's index in the exe.
static VerifyShortcutStatus VerifyChromeShortcut(const string16& exe_path,
const string16& shortcut,
const string16& description,
int icon_index);
// Sets |suffix| to the base 32 encoding of the md5 hash of this user's sid
// preceded by a dot.
// This is guaranteed to be unique on the machine and 27 characters long
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <fstream>
......@@ -15,7 +13,6 @@
#include "base/scoped_temp_dir.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/win/scoped_comptr.h"
#include "base/win/windows_version.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/master_preferences.h"
......@@ -24,71 +21,6 @@
#include "testing/gtest/include/gtest/gtest.h"
namespace {
bool VerifyChromeShortcut(const std::wstring& exe_path,
const std::wstring& shortcut,
const std::wstring& description,
int icon_index) {
base::win::ScopedComPtr<IShellLink> i_shell_link;
base::win::ScopedComPtr<IPersistFile> i_persist_file;
// Get pointer to the IShellLink interface
bool failed = FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER));
EXPECT_FALSE(failed) << "Failed to get IShellLink";
if (failed)
return false;
// Query IShellLink for the IPersistFile interface
failed = FAILED(i_persist_file.QueryFrom(i_shell_link));
EXPECT_FALSE(failed) << "Failed to get IPersistFile";
if (failed)
return false;
failed = FAILED(i_persist_file->Load(shortcut.c_str(), 0));
EXPECT_FALSE(failed) << "Failed to load shortcut " << shortcut.c_str();
if (failed)
return false;
wchar_t long_path[MAX_PATH] = {0};
wchar_t short_path[MAX_PATH] = {0};
failed = ((::GetLongPathName(exe_path.c_str(), long_path, MAX_PATH) == 0) ||
(::GetShortPathName(exe_path.c_str(), short_path, MAX_PATH) == 0));
EXPECT_FALSE(failed) << "Failed to get long and short path names for "
<< exe_path;
if (failed)
return false;
wchar_t file_path[MAX_PATH] = {0};
failed = ((FAILED(i_shell_link->GetPath(file_path, MAX_PATH, NULL,
SLGP_UNCPRIORITY))) ||
((FilePath(file_path) != FilePath(long_path)) &&
(FilePath(file_path) != FilePath(short_path))));
EXPECT_FALSE(failed) << "File path " << file_path << " did not match with "
<< exe_path;
if (failed)
return false;
wchar_t desc[MAX_PATH] = {0};
failed = ((FAILED(i_shell_link->GetDescription(desc, MAX_PATH))) ||
(std::wstring(desc) != std::wstring(description)));
EXPECT_FALSE(failed) << "Description " << desc << " did not match with "
<< description;
if (failed)
return false;
wchar_t icon_path[MAX_PATH] = {0};
int index = 0;
failed = ((FAILED(i_shell_link->GetIconLocation(icon_path, MAX_PATH,
&index))) ||
((FilePath(file_path) != FilePath(long_path)) &&
(FilePath(file_path) != FilePath(short_path))) ||
(index != icon_index));
EXPECT_FALSE(failed);
if (failed)
return false;
return true;
}
class ShellUtilTestWithDirAndDist : public testing::Test {
protected:
......@@ -102,7 +34,8 @@ class ShellUtilTestWithDirAndDist : public testing::Test {
ScopedTempDir temp_dir_;
};
};
}
// Test that we can open archives successfully.
TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) {
......@@ -115,7 +48,7 @@ TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) {
EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path));
FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk");
const std::wstring description(L"dummy description");
const string16 description(L"dummy description");
EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(
dist_,
exe_path.value(),
......@@ -125,9 +58,9 @@ TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) {
exe_path.value(),
dist_->GetIconIndex(),
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, 0));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), shortcut_path.value(), description, 0));
// Now specify an icon index in master prefs and make sure it works.
FilePath prefs_path = temp_dir_.path().AppendASCII(
......@@ -152,13 +85,13 @@ TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) {
exe_path.value(),
dist_->GetIconIndex(),
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, 1));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), shortcut_path.value(), description, 1));
// Now change only description to update shortcut and make sure icon index
// doesn't change.
const std::wstring description2(L"dummy description 2");
const string16 description2(L"dummy description 2");
EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(dist_,
exe_path.value(),
shortcut_path.value(),
......@@ -167,9 +100,9 @@ TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) {
exe_path.value(),
dist_->GetIconIndex(),
ShellUtil::SHORTCUT_NO_OPTIONS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description2, 1));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), shortcut_path.value(), description2, 1));
}
TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
......@@ -187,25 +120,25 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe");
EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path));
const std::wstring description(L"dummy description");
const string16 description(L"dummy description");
FilePath user_desktop_path;
EXPECT_TRUE(ShellUtil::GetDesktopPath(false, &user_desktop_path));
FilePath system_desktop_path;
EXPECT_TRUE(ShellUtil::GetDesktopPath(true, &system_desktop_path));
std::wstring shortcut_name;
string16 shortcut_name;
EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false, L"",
&shortcut_name));
std::wstring default_profile_shortcut_name;
const std::wstring default_profile_user_name = L"Minsk";
string16 default_profile_shortcut_name;
const string16 default_profile_user_name = L"Minsk";
EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false,
default_profile_user_name,
&default_profile_shortcut_name));
std::wstring second_profile_shortcut_name;
const std::wstring second_profile_user_name = L"Pinsk";
string16 second_profile_shortcut_name;
const string16 second_profile_user_name = L"Pinsk";
EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false,
second_profile_user_name,
&second_profile_shortcut_name));
......@@ -228,10 +161,9 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::CURRENT_USER,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
user_shortcut_path.value(),
description,
0));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), user_shortcut_path.value(), description, 0));
EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut(
dist_,
ShellUtil::CURRENT_USER,
......@@ -248,9 +180,9 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::SYSTEM_LEVEL,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
system_shortcut_path.value(),
description,
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), system_shortcut_path.value(), description,
0));
EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut(
dist_,
......@@ -279,9 +211,9 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::CURRENT_USER,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
system_shortcut_path.value(),
description,
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), system_shortcut_path.value(), description,
0));
EXPECT_FALSE(file_util::PathExists(user_shortcut_path));
EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut(
......@@ -311,13 +243,12 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::SYSTEM_LEVEL,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
user_shortcut_path.value(),
description,
0));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
system_shortcut_path.value(),
description,
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), user_shortcut_path.value(), description, 0));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), system_shortcut_path.value(), description,
0));
EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut(
dist_,
......@@ -340,10 +271,10 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::CURRENT_USER,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
default_profile_shortcut_path.value(),
description,
0));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), default_profile_shortcut_path.value(),
description, 0));
EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut(
dist_,
exe_path.value(),
......@@ -354,10 +285,10 @@ TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) {
dist_->GetIconIndex(),
ShellUtil::CURRENT_USER,
ShellUtil::SHORTCUT_CREATE_ALWAYS));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
second_profile_shortcut_path.value(),
description,
0));
EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS,
ShellUtil::VerifyChromeShortcut(
exe_path.value(), second_profile_shortcut_path.value(),
description, 0));
std::vector<string16> profile_names;
profile_names.push_back(default_profile_shortcut_name);
profile_names.push_back(second_profile_shortcut_name);
......
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