Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/win/shortcut.h" | 5 #include "base/win/shortcut.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/test/test_file_util.h" | 12 #include "base/test/test_file_util.h" |
| 13 #include "base/test/test_shortcut_win.h" | 13 #include "base/test/test_shortcut_win.h" |
| 14 #include "base/win/scoped_com_initializer.h" | 14 #include "base/win/scoped_com_initializer.h" |
| 15 #include "base/win/windows_version.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 namespace win { | 19 namespace win { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 static const char kFileContents[] = "This is a target."; | 23 static const char kFileContents[] = "This is a target."; |
| 23 static const char kFileContents2[] = "This is another target."; | 24 static const char kFileContents2[] = "This is another target."; |
| 24 | 25 |
| 25 class ShortcutTest : public testing::Test { | 26 class ShortcutTest : public testing::Test { |
| 26 protected: | 27 protected: |
| 27 virtual void SetUp() OVERRIDE { | 28 virtual void SetUp() OVERRIDE { |
| 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 29 ASSERT_TRUE(temp_dir_2_.CreateUniqueTempDir()); | 30 ASSERT_TRUE(temp_dir_2_.CreateUniqueTempDir()); |
| 30 | 31 |
| 31 link_file_ = temp_dir_.path().Append(L"My Link.lnk"); | 32 link_file_ = temp_dir_.path().Append(L"My Link.lnk"); |
| 32 | 33 |
| 33 // Shortcut 1's properties | 34 // Shortcut 1's properties |
| 34 { | 35 { |
| 35 const FilePath target_file(temp_dir_.path().Append(L"Target 1.txt")); | 36 const FilePath target_file(temp_dir_.path().Append(L"Target 1.txt")); |
| 36 file_util::WriteFile(target_file, kFileContents, | 37 file_util::WriteFile(target_file, kFileContents, |
| 37 arraysize(kFileContents)); | 38 arraysize(kFileContents)); |
| 38 | 39 |
| 39 link_properties_.set_target(target_file); | 40 link_properties_.set_target(target_file); |
| 40 link_properties_.set_working_dir(temp_dir_.path()); | 41 link_properties_.set_working_dir(temp_dir_.path()); |
| 41 link_properties_.set_arguments(L"--magic --awesome"); | 42 link_properties_.set_arguments(L"--magic --awesome"); |
| 42 link_properties_.set_description(L"Chrome is awesome."); | 43 link_properties_.set_description(L"Chrome is awesome."); |
| 43 link_properties_.set_icon(link_properties_.target, 4); | 44 link_properties_.set_icon(link_properties_.target, 4); |
| 44 link_properties_.set_app_id(L"Chrome"); | 45 if (GetVersion() >= VERSION_WIN7) { |
|
gab
2014/01/03 18:25:52
I think it's good to set these unconditionally (to
huangs
2014/01/03 20:19:35
Done.
| |
| 45 link_properties_.set_dual_mode(false); | 46 link_properties_.set_app_id(L"Chrome"); |
| 47 link_properties_.set_dual_mode(false); | |
| 48 } | |
| 46 } | 49 } |
| 47 | 50 |
| 48 // Shortcut 2's properties (all different from properties of shortcut 1). | 51 // Shortcut 2's properties (all different from properties of shortcut 1). |
| 49 { | 52 { |
| 50 const FilePath target_file_2(temp_dir_.path().Append(L"Target 2.txt")); | 53 const FilePath target_file_2(temp_dir_.path().Append(L"Target 2.txt")); |
| 51 file_util::WriteFile(target_file_2, kFileContents2, | 54 file_util::WriteFile(target_file_2, kFileContents2, |
| 52 arraysize(kFileContents2)); | 55 arraysize(kFileContents2)); |
| 53 | 56 |
| 54 FilePath icon_path_2; | 57 FilePath icon_path_2; |
| 55 base::CreateTemporaryFileInDir(temp_dir_.path(), &icon_path_2); | 58 base::CreateTemporaryFileInDir(temp_dir_.path(), &icon_path_2); |
| 56 | 59 |
| 57 link_properties_2_.set_target(target_file_2); | 60 link_properties_2_.set_target(target_file_2); |
| 58 link_properties_2_.set_working_dir(temp_dir_2_.path()); | 61 link_properties_2_.set_working_dir(temp_dir_2_.path()); |
| 59 link_properties_2_.set_arguments(L"--super --crazy"); | 62 link_properties_2_.set_arguments(L"--super --crazy"); |
| 60 link_properties_2_.set_description(L"The best in the west."); | 63 link_properties_2_.set_description(L"The best in the west."); |
| 61 link_properties_2_.set_icon(icon_path_2, 0); | 64 link_properties_2_.set_icon(icon_path_2, 0); |
| 62 link_properties_2_.set_app_id(L"Chrome.UserLevelCrazySuffix"); | 65 if (GetVersion() >= VERSION_WIN7) { |
|
gab
2014/01/03 18:25:52
Remove condition here too.
huangs
2014/01/03 20:19:35
Done.
| |
| 63 link_properties_2_.set_dual_mode(true); | 66 link_properties_2_.set_app_id(L"Chrome.UserLevelCrazySuffix"); |
| 67 link_properties_2_.set_dual_mode(true); | |
| 68 } | |
| 64 } | 69 } |
| 65 } | 70 } |
| 66 | 71 |
| 67 ScopedCOMInitializer com_initializer_; | 72 ScopedCOMInitializer com_initializer_; |
| 68 ScopedTempDir temp_dir_; | 73 ScopedTempDir temp_dir_; |
| 69 ScopedTempDir temp_dir_2_; | 74 ScopedTempDir temp_dir_2_; |
| 70 | 75 |
| 71 // The link file to be created/updated in the shortcut tests below. | 76 // The link file to be created/updated in the shortcut tests below. |
| 72 FilePath link_file_; | 77 FilePath link_file_; |
| 73 | 78 |
| 74 // Properties for the created shortcut. | 79 // Properties for the created shortcut. |
| 75 ShortcutProperties link_properties_; | 80 ShortcutProperties link_properties_; |
| 76 | 81 |
| 77 // Properties for the updated shortcut. | 82 // Properties for the updated shortcut. |
| 78 ShortcutProperties link_properties_2_; | 83 ShortcutProperties link_properties_2_; |
| 79 }; | 84 }; |
| 80 | 85 |
| 81 } // namespace | 86 } // namespace |
| 82 | 87 |
| 88 TEST_F(ShortcutTest, CreateAndResolveShortcutProperties) { | |
| 89 uint32 properties_use = ShortcutProperties::PROPERTIES_BASIC; | |
|
gab
2014/01/03 18:25:52
s/properties_use/valid_properties
huangs
2014/01/03 20:19:35
Done.
| |
| 90 if (GetVersion() >= VERSION_WIN7) | |
| 91 properties_use |= ShortcutProperties::PROPERTIES_WIN7; | |
| 92 | |
| 93 // Test all properties. | |
| 94 FilePath file_1(temp_dir_.path().Append(L"Link1.lnk")); | |
| 95 ASSERT_TRUE(CreateOrUpdateShortcutLink( | |
| 96 file_1, link_properties_, SHORTCUT_CREATE_ALWAYS)); | |
| 97 | |
| 98 ShortcutProperties properties_read_1; | |
| 99 ASSERT_TRUE( | |
| 100 ResolveShortcutProperties(file_1, properties_use, &properties_read_1)); | |
|
gab
2014/01/03 18:25:52
Still resolve on PROPERTIES_ALL and expect the met
huangs
2014/01/03 20:19:35
Done.
| |
| 101 EXPECT_EQ(link_properties_.options, properties_use); | |
| 102 EXPECT_EQ(link_properties_.target, properties_read_1.target); | |
| 103 EXPECT_EQ(link_properties_.working_dir, properties_read_1.working_dir); | |
| 104 EXPECT_EQ(link_properties_.arguments, properties_read_1.arguments); | |
| 105 EXPECT_EQ(link_properties_.description, properties_read_1.description); | |
| 106 EXPECT_EQ(link_properties_.icon, properties_read_1.icon); | |
| 107 EXPECT_EQ(link_properties_.icon_index, properties_read_1.icon_index); | |
| 108 if (GetVersion() >= VERSION_WIN7) { | |
| 109 EXPECT_EQ(link_properties_.app_id, properties_read_1.app_id); | |
| 110 EXPECT_EQ(link_properties_.dual_mode, properties_read_1.dual_mode); | |
| 111 } | |
| 112 | |
| 113 // Test simple shortcut with no special properties set. | |
| 114 FilePath file_2(temp_dir_.path().Append(L"Link2.lnk")); | |
| 115 ShortcutProperties only_target_properties; | |
| 116 only_target_properties.set_target(link_properties_.target); | |
| 117 ASSERT_TRUE(CreateOrUpdateShortcutLink( | |
| 118 file_2, only_target_properties, SHORTCUT_CREATE_ALWAYS)); | |
| 119 | |
| 120 ShortcutProperties properties_read_2; | |
| 121 ASSERT_TRUE( | |
| 122 ResolveShortcutProperties(file_2, properties_use, &properties_read_2)); | |
|
gab
2014/01/03 18:25:52
PROPERTIES_ALL here too
huangs
2014/01/03 20:19:35
Done.
| |
| 123 EXPECT_EQ(properties_use, properties_read_2.options); | |
| 124 EXPECT_EQ(only_target_properties.target, properties_read_2.target); | |
| 125 EXPECT_EQ(L"", properties_read_2.arguments); | |
| 126 EXPECT_EQ(L"", properties_read_2.description); | |
|
gab
2014/01/03 18:25:52
Why not test all properties?
This will be good in
huangs
2014/01/03 20:19:35
Test for (empty) defaults. Will need to watch try
| |
| 127 } | |
| 128 | |
| 83 TEST_F(ShortcutTest, CreateAndResolveShortcut) { | 129 TEST_F(ShortcutTest, CreateAndResolveShortcut) { |
| 84 ShortcutProperties only_target_properties; | 130 ShortcutProperties only_target_properties; |
| 85 only_target_properties.set_target(link_properties_.target); | 131 only_target_properties.set_target(link_properties_.target); |
| 86 | 132 |
| 87 ASSERT_TRUE(CreateOrUpdateShortcutLink( | 133 ASSERT_TRUE(CreateOrUpdateShortcutLink( |
| 88 link_file_, only_target_properties, SHORTCUT_CREATE_ALWAYS)); | 134 link_file_, only_target_properties, SHORTCUT_CREATE_ALWAYS)); |
| 89 | 135 |
| 90 FilePath resolved_name; | 136 FilePath resolved_name; |
| 91 EXPECT_TRUE(ResolveShortcut(link_file_, &resolved_name, NULL)); | 137 EXPECT_TRUE(ResolveShortcut(link_file_, &resolved_name, NULL)); |
| 92 | 138 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 ASSERT_TRUE(CreateOrUpdateShortcutLink( | 310 ASSERT_TRUE(CreateOrUpdateShortcutLink( |
| 265 link_file_, link_properties_2_, SHORTCUT_REPLACE_EXISTING)); | 311 link_file_, link_properties_2_, SHORTCUT_REPLACE_EXISTING)); |
| 266 | 312 |
| 267 ShortcutProperties expected_properties(link_properties_2_); | 313 ShortcutProperties expected_properties(link_properties_2_); |
| 268 expected_properties.set_arguments(link_properties_.arguments); | 314 expected_properties.set_arguments(link_properties_.arguments); |
| 269 ValidateShortcut(link_file_, expected_properties); | 315 ValidateShortcut(link_file_, expected_properties); |
| 270 } | 316 } |
| 271 | 317 |
| 272 } // namespace win | 318 } // namespace win |
| 273 } // namespace base | 319 } // namespace base |
| OLD | NEW |