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.
5 #include "base/win/shortcut.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/test/test_file_util.h"
13 #include "base/test/test_shortcut_win.h"
14 #include "base/win/scoped_com_initializer.h"
15 #include "testing/gtest/include/gtest/gtest.h"
22 static const char kFileContents
[] = "This is a target.";
23 static const char kFileContents2
[] = "This is another target.";
25 class ShortcutTest
: public testing::Test
{
27 virtual void SetUp() OVERRIDE
{
28 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
29 ASSERT_TRUE(temp_dir_2_
.CreateUniqueTempDir());
31 link_file_
= temp_dir_
.path().Append(L
"My Link.lnk");
33 // Shortcut 1's properties
35 const FilePath
target_file(temp_dir_
.path().Append(L
"Target 1.txt"));
36 file_util::WriteFile(target_file
, kFileContents
,
37 arraysize(kFileContents
));
39 link_properties_
.set_target(target_file
);
40 link_properties_
.set_working_dir(temp_dir_
.path());
41 link_properties_
.set_arguments(L
"--magic --awesome");
42 link_properties_
.set_description(L
"Chrome is awesome.");
43 link_properties_
.set_icon(link_properties_
.target
, 4);
44 link_properties_
.set_app_id(L
"Chrome");
45 link_properties_
.set_dual_mode(false);
48 // Shortcut 2's properties (all different from properties of shortcut 1).
50 const FilePath
target_file_2(temp_dir_
.path().Append(L
"Target 2.txt"));
51 file_util::WriteFile(target_file_2
, kFileContents2
,
52 arraysize(kFileContents2
));
55 file_util::CreateTemporaryFileInDir(temp_dir_
.path(), &icon_path_2
);
57 link_properties_2_
.set_target(target_file_2
);
58 link_properties_2_
.set_working_dir(temp_dir_2_
.path());
59 link_properties_2_
.set_arguments(L
"--super --crazy");
60 link_properties_2_
.set_description(L
"The best in the west.");
61 link_properties_2_
.set_icon(icon_path_2
, 0);
62 link_properties_2_
.set_app_id(L
"Chrome.UserLevelCrazySuffix");
63 link_properties_2_
.set_dual_mode(true);
67 ScopedCOMInitializer com_initializer_
;
68 ScopedTempDir temp_dir_
;
69 ScopedTempDir temp_dir_2_
;
71 // The link file to be created/updated in the shortcut tests below.
74 // Properties for the created shortcut.
75 ShortcutProperties link_properties_
;
77 // Properties for the updated shortcut.
78 ShortcutProperties link_properties_2_
;
83 TEST_F(ShortcutTest
, CreateAndResolveShortcut
) {
84 ShortcutProperties only_target_properties
;
85 only_target_properties
.set_target(link_properties_
.target
);
87 ASSERT_TRUE(CreateOrUpdateShortcutLink(
88 link_file_
, only_target_properties
, SHORTCUT_CREATE_ALWAYS
));
90 FilePath resolved_name
;
91 EXPECT_TRUE(ResolveShortcut(link_file_
, &resolved_name
, NULL
));
93 char read_contents
[arraysize(kFileContents
)];
94 file_util::ReadFile(resolved_name
, read_contents
, arraysize(read_contents
));
95 EXPECT_STREQ(kFileContents
, read_contents
);
98 TEST_F(ShortcutTest
, ResolveShortcutWithArgs
) {
99 ASSERT_TRUE(CreateOrUpdateShortcutLink(
100 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
102 FilePath resolved_name
;
104 EXPECT_TRUE(ResolveShortcut(link_file_
, &resolved_name
, &args
));
106 char read_contents
[arraysize(kFileContents
)];
107 file_util::ReadFile(resolved_name
, read_contents
, arraysize(read_contents
));
108 EXPECT_STREQ(kFileContents
, read_contents
);
109 EXPECT_EQ(link_properties_
.arguments
, args
);
112 TEST_F(ShortcutTest
, CreateShortcutWithOnlySomeProperties
) {
113 ShortcutProperties target_and_args_properties
;
114 target_and_args_properties
.set_target(link_properties_
.target
);
115 target_and_args_properties
.set_arguments(link_properties_
.arguments
);
117 ASSERT_TRUE(CreateOrUpdateShortcutLink(
118 link_file_
, target_and_args_properties
,
119 SHORTCUT_CREATE_ALWAYS
));
121 ValidateShortcut(link_file_
, target_and_args_properties
);
124 TEST_F(ShortcutTest
, CreateShortcutVerifyProperties
) {
125 ASSERT_TRUE(CreateOrUpdateShortcutLink(
126 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
128 ValidateShortcut(link_file_
, link_properties_
);
131 TEST_F(ShortcutTest
, UpdateShortcutVerifyProperties
) {
132 ASSERT_TRUE(CreateOrUpdateShortcutLink(
133 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
135 ASSERT_TRUE(CreateOrUpdateShortcutLink(
136 link_file_
, link_properties_2_
, SHORTCUT_UPDATE_EXISTING
));
138 ValidateShortcut(link_file_
, link_properties_2_
);
141 TEST_F(ShortcutTest
, UpdateShortcutUpdateOnlyTargetAndResolve
) {
142 ASSERT_TRUE(CreateOrUpdateShortcutLink(
143 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
145 ShortcutProperties update_only_target_properties
;
146 update_only_target_properties
.set_target(link_properties_2_
.target
);
148 ASSERT_TRUE(CreateOrUpdateShortcutLink(
149 link_file_
, update_only_target_properties
,
150 SHORTCUT_UPDATE_EXISTING
));
152 ShortcutProperties expected_properties
= link_properties_
;
153 expected_properties
.set_target(link_properties_2_
.target
);
154 ValidateShortcut(link_file_
, expected_properties
);
156 FilePath resolved_name
;
157 EXPECT_TRUE(ResolveShortcut(link_file_
, &resolved_name
, NULL
));
159 char read_contents
[arraysize(kFileContents2
)];
160 file_util::ReadFile(resolved_name
, read_contents
, arraysize(read_contents
));
161 EXPECT_STREQ(kFileContents2
, read_contents
);
164 TEST_F(ShortcutTest
, UpdateShortcutMakeDualMode
) {
165 ASSERT_TRUE(CreateOrUpdateShortcutLink(
166 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
168 ShortcutProperties make_dual_mode_properties
;
169 make_dual_mode_properties
.set_dual_mode(true);
171 ASSERT_TRUE(CreateOrUpdateShortcutLink(
172 link_file_
, make_dual_mode_properties
,
173 SHORTCUT_UPDATE_EXISTING
));
175 ShortcutProperties expected_properties
= link_properties_
;
176 expected_properties
.set_dual_mode(true);
177 ValidateShortcut(link_file_
, expected_properties
);
180 TEST_F(ShortcutTest
, UpdateShortcutRemoveDualMode
) {
181 ASSERT_TRUE(CreateOrUpdateShortcutLink(
182 link_file_
, link_properties_2_
, SHORTCUT_CREATE_ALWAYS
));
184 ShortcutProperties remove_dual_mode_properties
;
185 remove_dual_mode_properties
.set_dual_mode(false);
187 ASSERT_TRUE(CreateOrUpdateShortcutLink(
188 link_file_
, remove_dual_mode_properties
,
189 SHORTCUT_UPDATE_EXISTING
));
191 ShortcutProperties expected_properties
= link_properties_2_
;
192 expected_properties
.set_dual_mode(false);
193 ValidateShortcut(link_file_
, expected_properties
);
196 TEST_F(ShortcutTest
, UpdateShortcutClearArguments
) {
197 ASSERT_TRUE(CreateOrUpdateShortcutLink(
198 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
200 ShortcutProperties clear_arguments_properties
;
201 clear_arguments_properties
.set_arguments(string16());
203 ASSERT_TRUE(CreateOrUpdateShortcutLink(
204 link_file_
, clear_arguments_properties
,
205 SHORTCUT_UPDATE_EXISTING
));
207 ShortcutProperties expected_properties
= link_properties_
;
208 expected_properties
.set_arguments(string16());
209 ValidateShortcut(link_file_
, expected_properties
);
212 TEST_F(ShortcutTest
, FailUpdateShortcutThatDoesNotExist
) {
213 ASSERT_FALSE(CreateOrUpdateShortcutLink(
214 link_file_
, link_properties_
, SHORTCUT_UPDATE_EXISTING
));
215 ASSERT_FALSE(file_util::PathExists(link_file_
));
218 TEST_F(ShortcutTest
, ReplaceShortcutAllProperties
) {
219 ASSERT_TRUE(CreateOrUpdateShortcutLink(
220 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
222 ASSERT_TRUE(CreateOrUpdateShortcutLink(
223 link_file_
, link_properties_2_
, SHORTCUT_REPLACE_EXISTING
));
225 ValidateShortcut(link_file_
, link_properties_2_
);
228 TEST_F(ShortcutTest
, ReplaceShortcutSomeProperties
) {
229 ASSERT_TRUE(CreateOrUpdateShortcutLink(
230 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
232 ShortcutProperties new_properties
;
233 new_properties
.set_target(link_properties_2_
.target
);
234 new_properties
.set_arguments(link_properties_2_
.arguments
);
235 new_properties
.set_description(link_properties_2_
.description
);
236 ASSERT_TRUE(CreateOrUpdateShortcutLink(
237 link_file_
, new_properties
, SHORTCUT_REPLACE_EXISTING
));
239 // Expect only properties in |new_properties| to be set, all other properties
240 // should have been overwritten.
241 ShortcutProperties
expected_properties(new_properties
);
242 expected_properties
.set_working_dir(FilePath());
243 expected_properties
.set_icon(FilePath(), 0);
244 expected_properties
.set_app_id(string16());
245 expected_properties
.set_dual_mode(false);
246 ValidateShortcut(link_file_
, expected_properties
);
249 TEST_F(ShortcutTest
, FailReplaceShortcutThatDoesNotExist
) {
250 ASSERT_FALSE(CreateOrUpdateShortcutLink(
251 link_file_
, link_properties_
, SHORTCUT_REPLACE_EXISTING
));
252 ASSERT_FALSE(file_util::PathExists(link_file_
));
255 // Test that the old arguments remain on the replaced shortcut when not
256 // otherwise specified.
257 TEST_F(ShortcutTest
, ReplaceShortcutKeepOldArguments
) {
258 ASSERT_TRUE(CreateOrUpdateShortcutLink(
259 link_file_
, link_properties_
, SHORTCUT_CREATE_ALWAYS
));
261 // Do not explicitly set the arguments.
262 link_properties_2_
.options
&=
263 ~ShortcutProperties::PROPERTIES_ARGUMENTS
;
264 ASSERT_TRUE(CreateOrUpdateShortcutLink(
265 link_file_
, link_properties_2_
, SHORTCUT_REPLACE_EXISTING
));
267 ShortcutProperties
expected_properties(link_properties_2_
);
268 expected_properties
.set_arguments(link_properties_
.arguments
);
269 ValidateShortcut(link_file_
, expected_properties
);