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

Side by Side Diff: base/win/shortcut_unittest.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: namespace s/Win/win 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/win/shortcut.cc ('k') | base/win/win_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
5 #include "base/win/shortcut.h"
6
7 #include <windows.h>
8 #include <objbase.h>
9
10 #include <string>
11
12 #include "base/file_path.h"
13 #include "base/file_util.h"
14 #include "base/scoped_temp_dir.h"
15 #include "base/test/test_file_util.h"
16 #include "base/test/test_shortcut_win.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace {
20
21 static const char kFileContents[] = "This is a target.";
22 static const char kFileContents2[] = "This is another target.";
23
24 class ShortcutTest : public testing::Test {
25 protected:
26 virtual void SetUp() OVERRIDE {
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
28 ASSERT_TRUE(temp_dir_2_.CreateUniqueTempDir());
29
30 EXPECT_EQ(S_OK, CoInitialize(NULL));
31
32 link_file_ = temp_dir_.path().Append(L"My Link.lnk");
33
34 // Shortcut 1's properties
35 {
36 const FilePath target_file(temp_dir_.path().Append(L"Target 1.txt"));
37 file_util::WriteFile(target_file, kFileContents,
38 arraysize(kFileContents));
39
40 link_properties_.set_target(target_file);
41 link_properties_.set_working_dir(temp_dir_.path());
42 link_properties_.set_arguments(L"--magic --awesome");
43 link_properties_.set_description(L"Chrome is awesome.");
44 link_properties_.set_icon(link_properties_.target, 4);
45 link_properties_.set_app_id(L"Chrome");
46 link_properties_.set_dual_mode(false);
47 }
48
49 // Shortcut 2's properties (all different from properties of shortcut 1).
50 {
51 const FilePath target_file_2(temp_dir_.path().Append(L"Target 2.txt"));
52 file_util::WriteFile(target_file_2, kFileContents2,
53 arraysize(kFileContents2));
54
55 FilePath icon_path_2;
56 file_util::CreateTemporaryFileInDir(temp_dir_.path(), &icon_path_2);
57
58 link_properties_2_.set_target(target_file_2);
59 link_properties_2_.set_working_dir(temp_dir_2_.path());
60 link_properties_2_.set_arguments(L"--super --crazy");
61 link_properties_2_.set_description(L"The best in the west.");
62 link_properties_2_.set_icon(icon_path_2, 0);
63 link_properties_2_.set_app_id(L"Chrome.UserLevelCrazySuffix");
64 link_properties_2_.set_dual_mode(true);
65 }
66 }
67
68 virtual void TearDown() OVERRIDE {
69 CoUninitialize();
70 }
71
72 ScopedTempDir temp_dir_;
73 ScopedTempDir temp_dir_2_;
74
75 // The link file to be created/updated in the shortcut tests below.
76 FilePath link_file_;
77
78 // Properties for the created shortcut.
79 base::win::ShortcutProperties link_properties_;
80
81 // Properties for the updated shortcut.
82 base::win::ShortcutProperties link_properties_2_;
83 };
84
85 } // namespace
86
87 TEST_F(ShortcutTest, CreateAndResolveShortcut) {
88 base::win::ShortcutProperties only_target_properties;
89 only_target_properties.set_target(link_properties_.target);
90
91 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
92 link_file_, only_target_properties, base::win::SHORTCUT_CREATE_ALWAYS));
93
94 FilePath resolved_name;
95 EXPECT_TRUE(base::win::ResolveShortcut(link_file_, &resolved_name, NULL));
96
97 char read_contents[arraysize(kFileContents)];
98 file_util::ReadFile(resolved_name, read_contents, arraysize(read_contents));
99 EXPECT_STREQ(kFileContents, read_contents);
100 }
101
102 TEST_F(ShortcutTest, ResolveShortcutWithArgs) {
103 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
104 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
105
106 FilePath resolved_name;
107 string16 args;
108 EXPECT_TRUE(base::win::ResolveShortcut(link_file_, &resolved_name, &args));
109
110 char read_contents[arraysize(kFileContents)];
111 file_util::ReadFile(resolved_name, read_contents, arraysize(read_contents));
112 EXPECT_STREQ(kFileContents, read_contents);
113 EXPECT_EQ(link_properties_.arguments, args);
114 }
115
116 TEST_F(ShortcutTest, CreateShortcutWithOnlySomeProperties) {
117 base::win::ShortcutProperties target_and_args_properties;
118 target_and_args_properties.set_target(link_properties_.target);
119 target_and_args_properties.set_arguments(link_properties_.arguments);
120
121 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
122 link_file_, target_and_args_properties,
123 base::win::SHORTCUT_CREATE_ALWAYS));
124
125 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
126 base::win::VerifyShortcut(link_file_, target_and_args_properties));
127 }
128
129 TEST_F(ShortcutTest, CreateShortcutVerifyProperties) {
130 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
131 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
132
133 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
134 base::win::VerifyShortcut(link_file_, link_properties_));
135 }
136
137 TEST_F(ShortcutTest, UpdateShortcutVerifyProperties) {
138 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
139 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
140
141 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
142 link_file_, link_properties_2_, base::win::SHORTCUT_UPDATE_EXISTING));
143
144 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
145 base::win::VerifyShortcut(link_file_, link_properties_2_));
146 }
147
148 TEST_F(ShortcutTest, UpdateShortcutUpdateOnlyTargetAndResolve) {
149 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
150 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
151
152 base::win::ShortcutProperties update_only_target_properties;
153 update_only_target_properties.set_target(link_properties_2_.target);
154
155 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
156 link_file_, update_only_target_properties,
157 base::win::SHORTCUT_UPDATE_EXISTING));
158
159 base::win::ShortcutProperties expected_properties = link_properties_;
160 expected_properties.set_target(link_properties_2_.target);
161 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
162 base::win::VerifyShortcut(link_file_, expected_properties));
163
164 FilePath resolved_name;
165 EXPECT_TRUE(base::win::ResolveShortcut(link_file_, &resolved_name, NULL));
166
167 char read_contents[arraysize(kFileContents2)];
168 file_util::ReadFile(resolved_name, read_contents, arraysize(read_contents));
169 EXPECT_STREQ(kFileContents2, read_contents);
170 }
171
172 TEST_F(ShortcutTest, UpdateShortcutMakeDualMode) {
173 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
174 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
175
176 base::win::ShortcutProperties make_dual_mode_properties;
177 make_dual_mode_properties.set_dual_mode(true);
178
179 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
180 link_file_, make_dual_mode_properties,
181 base::win::SHORTCUT_UPDATE_EXISTING));
182
183 base::win::ShortcutProperties expected_properties = link_properties_;
184 expected_properties.set_dual_mode(true);
185 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
186 base::win::VerifyShortcut(link_file_, expected_properties));
187 }
188
189 TEST_F(ShortcutTest, UpdateShortcutRemoveDualMode) {
190 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
191 link_file_, link_properties_2_, base::win::SHORTCUT_CREATE_ALWAYS));
192
193 base::win::ShortcutProperties remove_dual_mode_properties;
194 remove_dual_mode_properties.set_dual_mode(false);
195
196 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
197 link_file_, remove_dual_mode_properties,
198 base::win::SHORTCUT_UPDATE_EXISTING));
199
200 base::win::ShortcutProperties expected_properties = link_properties_2_;
201 expected_properties.set_dual_mode(false);
202 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
203 base::win::VerifyShortcut(link_file_, expected_properties));
204 }
205
206 TEST_F(ShortcutTest, UpdateShortcutClearArguments) {
207 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
208 link_file_, link_properties_, base::win::SHORTCUT_CREATE_ALWAYS));
209
210 base::win::ShortcutProperties clear_arguments_properties;
211 clear_arguments_properties.set_arguments(string16());
212
213 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
214 link_file_, clear_arguments_properties,
215 base::win::SHORTCUT_UPDATE_EXISTING));
216
217 base::win::ShortcutProperties expected_properties = link_properties_;
218 expected_properties.set_arguments(string16());
219 ASSERT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
220 base::win::VerifyShortcut(link_file_, expected_properties));
221 }
OLDNEW
« no previous file with comments | « base/win/shortcut.cc ('k') | base/win/win_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698