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

Unified Diff: base/test/test_file_util_win.cc

Issue 10914109: Refactoring and tests for the highly undertested file_util::CreateOrUpdateShortcutLink() method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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: base/test/test_file_util_win.cc
diff --git a/base/test/test_file_util_win.cc b/base/test/test_file_util_win.cc
index 41d69eac0acc06b69d51285c66d4459ed1a437a4..4a5dad66e18ff2d89f62eaf429044276eaa7f0a3 100644
--- a/base/test/test_file_util_win.cc
+++ b/base/test/test_file_util_win.cc
@@ -7,6 +7,9 @@
#include <aclapi.h>
#include <shlwapi.h>
#include <windows.h>
+#include <shlobj.h>
+#include <propkey.h>
+#include <propvarutil.h>
#include <vector>
@@ -14,9 +17,14 @@
#include "base/file_util.h"
#include "base/logging.h"
#include "base/string_split.h"
+#include "base/win/scoped_comptr.h"
#include "base/win/scoped_handle.h"
+#include "base/win/windows_version.h"
#include "base/threading/platform_thread.h"
+// propsys.lib is required for PropvariantTo*().
+#pragma comment(lib, "propsys.lib")
+
namespace file_util {
static const ptrdiff_t kOneMB = 1024 * 1024;
@@ -268,6 +276,101 @@ bool CopyRecursiveDirNoCache(const FilePath& source_dir,
return true;
}
+VerifyShortcutStatus VerifyShortcut(const string16& shortcut_path,
+ const ShortcutProperties& properties) {
+ base::win::ScopedComPtr<IShellLink> i_shell_link;
+ base::win::ScopedComPtr<IPersistFile> i_persist_file;
+ wchar_t temp_path[MAX_PATH] = {0};
+
+ wchar_t read_target[MAX_PATH] = {0};
+ wchar_t read_working_dir[MAX_PATH] = {0};
+ wchar_t read_arguments[MAX_PATH] = {0};
+ wchar_t read_description[MAX_PATH] = {0};
+ wchar_t read_icon[MAX_PATH] = {0};
+ int read_icon_index = 0;
+
+ // Initialize the shell interfaces.
+ if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL,
+ CLSCTX_INPROC_SERVER)) ||
+ FAILED(i_persist_file.QueryFrom(i_shell_link))) {
+ return VERIFY_SHORTCUT_FAILURE_UNEXPECTED;
+ }
+
+ // Load the shortcut.
+ if (FAILED(i_persist_file->Load(shortcut_path.c_str(), 0)))
+ return VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND;
+
+ if ((properties.bitfield & ShortcutProperties::PROPERTIES_TARGET) != 0 &&
robertshield 2012/09/06 02:37:34 None of the != 0s are needed here, are you using t
gab 2012/09/06 04:20:02 Right, done in file_util_win.cc as well.
+ (FAILED(i_shell_link->GetPath(read_target, MAX_PATH, NULL,
+ SLGP_SHORTPATH)) ||
+ ::GetShortPathName(properties.target.c_str(), temp_path,
+ MAX_PATH) == 0 ||
+ string16(read_target) != string16(temp_path))) {
+ return VERIFY_SHORTCUT_FAILURE_TARGET;
+ }
+
+ if ((properties.bitfield & ShortcutProperties::PROPERTIES_WORKING_DIR) != 0 &&
+ (FAILED(i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)) ||
+ string16(read_working_dir) != properties.working_dir)) {
+ return VERIFY_SHORTCUT_FAILURE_WORKING_DIR;
+ }
+
+ if ((properties.bitfield & ShortcutProperties::PROPERTIES_ARGUMENTS) != 0 &&
+ (FAILED(i_shell_link->GetArguments(read_arguments, MAX_PATH)) ||
+ string16(read_arguments) != properties.arguments)) {
+ return VERIFY_SHORTCUT_FAILURE_ARGUMENTS;
+ }
+
+ if ((properties.bitfield & ShortcutProperties::PROPERTIES_DESCRIPTION) != 0 &&
+ (FAILED(i_shell_link->GetDescription(read_description, MAX_PATH)) ||
+ string16(read_description) != properties.description)) {
+ return VERIFY_SHORTCUT_FAILURE_DESCRIPTION;
+ }
+
+ if ((properties.bitfield & ShortcutProperties::PROPERTIES_ICON) != 0 &&
+ (FAILED(i_shell_link->GetIconLocation(read_icon, MAX_PATH,
+ &read_icon_index)) ||
+ ::GetLongPathName(properties.icon.c_str(), temp_path, MAX_PATH) == 0 ||
+ string16(read_icon) != string16(temp_path) ||
+ read_icon_index != properties.icon_index)) {
+ return VERIFY_SHORTCUT_FAILURE_ICON;
+ }
+
+ if(base::win::GetVersion() >= base::win::VERSION_WIN7) {
+ base::win::ScopedComPtr<IPropertyStore> property_store;
+ // Note that, as mentioned on MSDN at http://goo.gl/M8h9g, if a property is
+ // not set, GetValue will return S_OK and the PROPVARIANT will be set to
+ // VT_EMPTY.
+ PROPVARIANT pv_app_id, pv_dual_mode;
+ if (FAILED(property_store.QueryFrom(i_shell_link)) ||
+ property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK ||
+ property_store->GetValue(PKEY_AppUserModel_IsDualMode,
+ &pv_dual_mode) != S_OK) {
+ return VERIFY_SHORTCUT_FAILURE_UNEXPECTED;
+ }
+
+ // Note, as mentioned on MSDN at http://goo.gl/hZ3sO, if |pv_app_id| is a
+ // VT_EMPTY it is successfully converted to the empty string.
+ wchar_t read_app_id[MAX_PATH] = {0};
+ PropVariantToString(pv_app_id, read_app_id, MAX_PATH);
+ if((properties.bitfield & ShortcutProperties::PROPERTIES_APP_ID) != 0 &&
+ string16(read_app_id) != properties.app_id) {
+ return VERIFY_SHORTCUT_FAILURE_APP_ID;
+ }
+
+ // Note, as mentioned on MSDN at http://goo.gl/9mBHB, if |pv_dual_mode| is a
+ // VT_EMPTY it is successfully converted to false.
+ BOOL read_dual_mode;
+ PropVariantToBoolean(pv_dual_mode, &read_dual_mode);
+ if((properties.bitfield & ShortcutProperties::PROPERTIES_DUAL_MODE) != 0 &&
+ static_cast<bool>(read_dual_mode) != properties.dual_mode) {
+ return VERIFY_SHORTCUT_FAILURE_DUAL_MODE;
+ }
+ }
+
+ return VERIFY_SHORTCUT_SUCCESS;
+}
+
// Checks if the volume supports Alternate Data Streams. This is required for
// the Zone Identifier implementation.
bool VolumeSupportsADS(const FilePath& path) {
« base/file_util_win.cc ('K') | « base/test/test_file_util.h ('k') | base/win/win_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698