test_shortcut_win.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/test/test_shortcut_win.h"
  5. #include <windows.h>
  6. #include <objbase.h>
  7. #include <shlobj.h>
  8. #include <propkey.h>
  9. #include <wrl/client.h>
  10. #include <string>
  11. #include "base/files/file_path.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "base/win/scoped_propvariant.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace base {
  17. namespace win {
  18. void ValidatePathsAreEqual(const FilePath& expected_path,
  19. const FilePath& actual_path) {
  20. wchar_t long_expected_path_chars[MAX_PATH] = {0};
  21. wchar_t long_actual_path_chars[MAX_PATH] = {0};
  22. // If |expected_path| is empty confirm immediately that |actual_path| is also
  23. // empty.
  24. if (expected_path.empty()) {
  25. EXPECT_TRUE(actual_path.empty());
  26. return;
  27. }
  28. // Proceed with LongPathName matching which will also confirm the paths exist.
  29. EXPECT_NE(0U, ::GetLongPathName(expected_path.value().c_str(),
  30. long_expected_path_chars, MAX_PATH))
  31. << "Failed to get LongPathName of " << expected_path.value();
  32. EXPECT_NE(0U, ::GetLongPathName(actual_path.value().c_str(),
  33. long_actual_path_chars, MAX_PATH))
  34. << "Failed to get LongPathName of " << actual_path.value();
  35. FilePath long_expected_path(long_expected_path_chars);
  36. FilePath long_actual_path(long_actual_path_chars);
  37. EXPECT_FALSE(long_expected_path.empty());
  38. EXPECT_FALSE(long_actual_path.empty());
  39. EXPECT_TRUE(base::FilePath::CompareEqualIgnoreCase(long_expected_path.value(),
  40. long_actual_path.value()));
  41. }
  42. void ValidateShortcut(const FilePath& shortcut_path,
  43. const ShortcutProperties& properties) {
  44. Microsoft::WRL::ComPtr<IShellLink> i_shell_link;
  45. Microsoft::WRL::ComPtr<IPersistFile> i_persist_file;
  46. wchar_t read_target[MAX_PATH] = {0};
  47. wchar_t read_working_dir[MAX_PATH] = {0};
  48. wchar_t read_arguments[MAX_PATH] = {0};
  49. wchar_t read_description[MAX_PATH] = {0};
  50. wchar_t read_icon[MAX_PATH] = {0};
  51. int read_icon_index = 0;
  52. HRESULT hr;
  53. // Initialize the shell interfaces.
  54. EXPECT_TRUE(SUCCEEDED(hr = ::CoCreateInstance(CLSID_ShellLink, NULL,
  55. CLSCTX_INPROC_SERVER,
  56. IID_PPV_ARGS(&i_shell_link))));
  57. if (FAILED(hr))
  58. return;
  59. EXPECT_TRUE(SUCCEEDED(hr = i_shell_link.As(&i_persist_file)));
  60. if (FAILED(hr))
  61. return;
  62. // Load the shortcut.
  63. EXPECT_TRUE(
  64. SUCCEEDED(hr = i_persist_file->Load(shortcut_path.value().c_str(), 0)))
  65. << "Failed to load shortcut at " << shortcut_path.value();
  66. if (FAILED(hr))
  67. return;
  68. if (properties.options & ShortcutProperties::PROPERTIES_TARGET) {
  69. EXPECT_TRUE(SUCCEEDED(
  70. i_shell_link->GetPath(read_target, MAX_PATH, NULL, SLGP_SHORTPATH)));
  71. ValidatePathsAreEqual(properties.target, FilePath(read_target));
  72. }
  73. if (properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) {
  74. EXPECT_TRUE(SUCCEEDED(
  75. i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)));
  76. ValidatePathsAreEqual(properties.working_dir, FilePath(read_working_dir));
  77. }
  78. if (properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) {
  79. EXPECT_TRUE(
  80. SUCCEEDED(i_shell_link->GetArguments(read_arguments, MAX_PATH)));
  81. EXPECT_EQ(properties.arguments, read_arguments);
  82. }
  83. if (properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) {
  84. EXPECT_TRUE(
  85. SUCCEEDED(i_shell_link->GetDescription(read_description, MAX_PATH)));
  86. EXPECT_EQ(properties.description, read_description);
  87. }
  88. if (properties.options & ShortcutProperties::PROPERTIES_ICON) {
  89. EXPECT_TRUE(SUCCEEDED(
  90. i_shell_link->GetIconLocation(read_icon, MAX_PATH, &read_icon_index)));
  91. ValidatePathsAreEqual(properties.icon, FilePath(read_icon));
  92. EXPECT_EQ(properties.icon_index, read_icon_index);
  93. }
  94. Microsoft::WRL::ComPtr<IPropertyStore> property_store;
  95. EXPECT_TRUE(SUCCEEDED(hr = i_shell_link.As(&property_store)));
  96. if (FAILED(hr))
  97. return;
  98. if (properties.options & ShortcutProperties::PROPERTIES_APP_ID) {
  99. ScopedPropVariant pv_app_id;
  100. EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_ID,
  101. pv_app_id.Receive()));
  102. switch (pv_app_id.get().vt) {
  103. case VT_EMPTY:
  104. EXPECT_TRUE(properties.app_id.empty());
  105. break;
  106. case VT_LPWSTR:
  107. EXPECT_EQ(properties.app_id, pv_app_id.get().pwszVal);
  108. break;
  109. default:
  110. ADD_FAILURE() << "Unexpected variant type: " << pv_app_id.get().vt;
  111. }
  112. }
  113. if (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) {
  114. ScopedPropVariant pv_dual_mode;
  115. EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_IsDualMode,
  116. pv_dual_mode.Receive()));
  117. switch (pv_dual_mode.get().vt) {
  118. case VT_EMPTY:
  119. EXPECT_FALSE(properties.dual_mode);
  120. break;
  121. case VT_BOOL:
  122. EXPECT_EQ(properties.dual_mode,
  123. static_cast<bool>(pv_dual_mode.get().boolVal));
  124. break;
  125. default:
  126. ADD_FAILURE() << "Unexpected variant type: " << pv_dual_mode.get().vt;
  127. }
  128. }
  129. }
  130. } // namespace win
  131. } // namespace base