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.
9 #include "base/base_paths.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/scoped_path_override.h"
17 #include "base/test/test_shortcut_win.h"
18 #include "base/version.h"
19 #include "base/win/shortcut.h"
20 #include "chrome/installer/setup/install.h"
21 #include "chrome/installer/setup/install_worker.h"
22 #include "chrome/installer/setup/setup_constants.h"
23 #include "chrome/installer/util/browser_distribution.h"
24 #include "chrome/installer/util/install_util.h"
25 #include "chrome/installer/util/installer_state.h"
26 #include "chrome/installer/util/master_preferences.h"
27 #include "chrome/installer/util/master_preferences_constants.h"
28 #include "chrome/installer/util/product.h"
29 #include "chrome/installer/util/shell_util.h"
30 #include "chrome/installer/util/util_constants.h"
31 #include "testing/gtest/include/gtest/gtest.h"
35 class CreateVisualElementsManifestTest
: public testing::Test
{
37 void SetUp() override
{
38 // Create a temp directory for testing.
39 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
41 version_
= Version("0.0.0.0");
43 version_dir_
= test_dir_
.path().AppendASCII(version_
.GetString());
44 ASSERT_TRUE(base::CreateDirectory(version_dir_
));
47 test_dir_
.path().Append(installer::kVisualElementsManifest
);
50 void TearDown() override
{
51 // Clean up test directory manually so we can fail if it leaks.
52 ASSERT_TRUE(test_dir_
.Delete());
55 // The temporary directory used to contain the test operations.
56 base::ScopedTempDir test_dir_
;
58 // A dummy version number used to create the version directory.
61 // The path to |test_dir_|\|version_|.
62 base::FilePath version_dir_
;
64 // The path to VisualElementsManifest.xml.
65 base::FilePath manifest_path_
;
68 class InstallShortcutTest
: public testing::Test
{
70 void SetUp() override
{
71 EXPECT_EQ(S_OK
, CoInitialize(NULL
));
73 dist_
= BrowserDistribution::GetDistribution();
74 ASSERT_TRUE(dist_
!= NULL
);
75 product_
.reset(new installer::Product(dist_
));
77 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
78 chrome_exe_
= temp_dir_
.path().Append(installer::kChromeExe
);
79 EXPECT_EQ(0, base::WriteFile(chrome_exe_
, "", 0));
81 ShellUtil::ShortcutProperties
chrome_properties(ShellUtil::CURRENT_USER
);
82 product_
->AddDefaultShortcutProperties(chrome_exe_
, &chrome_properties
);
84 expected_properties_
.set_target(chrome_exe_
);
85 expected_properties_
.set_icon(chrome_properties
.icon
,
86 chrome_properties
.icon_index
);
87 expected_properties_
.set_app_id(chrome_properties
.app_id
);
88 expected_properties_
.set_description(chrome_properties
.description
);
89 expected_properties_
.set_dual_mode(false);
90 expected_start_menu_properties_
= expected_properties_
;
91 expected_start_menu_properties_
.set_dual_mode(
92 InstallUtil::ShouldInstallMetroProperties());
94 prefs_
.reset(GetFakeMasterPrefs(false, false, false));
96 ASSERT_TRUE(fake_user_desktop_
.CreateUniqueTempDir());
97 ASSERT_TRUE(fake_common_desktop_
.CreateUniqueTempDir());
98 ASSERT_TRUE(fake_user_quick_launch_
.CreateUniqueTempDir());
99 ASSERT_TRUE(fake_start_menu_
.CreateUniqueTempDir());
100 ASSERT_TRUE(fake_common_start_menu_
.CreateUniqueTempDir());
101 user_desktop_override_
.reset(
102 new base::ScopedPathOverride(base::DIR_USER_DESKTOP
,
103 fake_user_desktop_
.path()));
104 common_desktop_override_
.reset(
105 new base::ScopedPathOverride(base::DIR_COMMON_DESKTOP
,
106 fake_common_desktop_
.path()));
107 user_quick_launch_override_
.reset(
108 new base::ScopedPathOverride(base::DIR_USER_QUICK_LAUNCH
,
109 fake_user_quick_launch_
.path()));
110 start_menu_override_
.reset(
111 new base::ScopedPathOverride(base::DIR_START_MENU
,
112 fake_start_menu_
.path()));
113 common_start_menu_override_
.reset(
114 new base::ScopedPathOverride(base::DIR_COMMON_START_MENU
,
115 fake_common_start_menu_
.path()));
117 base::string16
shortcut_name(
118 dist_
->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME
) +
120 base::string16
alternate_shortcut_name(
121 dist_
->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME_ALTERNATE
) +
124 user_desktop_shortcut_
=
125 fake_user_desktop_
.path().Append(shortcut_name
);
126 user_quick_launch_shortcut_
=
127 fake_user_quick_launch_
.path().Append(shortcut_name
);
128 user_start_menu_shortcut_
=
129 fake_start_menu_
.path().Append(
130 dist_
->GetStartMenuShortcutSubfolder(
131 BrowserDistribution::SUBFOLDER_CHROME
))
132 .Append(shortcut_name
);
133 system_desktop_shortcut_
=
134 fake_common_desktop_
.path().Append(shortcut_name
);
135 system_start_menu_shortcut_
=
136 fake_common_start_menu_
.path().Append(
137 dist_
->GetStartMenuShortcutSubfolder(
138 BrowserDistribution::SUBFOLDER_CHROME
))
139 .Append(shortcut_name
);
140 user_alternate_desktop_shortcut_
=
141 fake_user_desktop_
.path().Append(alternate_shortcut_name
);
144 void TearDown() override
{
145 // Try to unpin potentially pinned shortcuts (although pinning isn't tested,
146 // the call itself might still have pinned the Start Menu shortcuts).
147 base::win::UnpinShortcutFromTaskbar(user_start_menu_shortcut_
);
148 base::win::UnpinShortcutFromTaskbar(system_start_menu_shortcut_
);
149 base::win::UnpinShortcutFromStart(user_start_menu_shortcut_
);
150 base::win::UnpinShortcutFromStart(system_start_menu_shortcut_
);
154 installer::MasterPreferences
* GetFakeMasterPrefs(
155 bool do_not_create_desktop_shortcut
,
156 bool do_not_create_quick_launch_shortcut
,
157 bool alternate_desktop_shortcut
) {
159 const char* pref_name
;
161 } desired_prefs
[] = {
162 { installer::master_preferences::kDoNotCreateDesktopShortcut
,
163 do_not_create_desktop_shortcut
},
164 { installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
165 do_not_create_quick_launch_shortcut
},
166 { installer::master_preferences::kAltShortcutText
,
167 alternate_desktop_shortcut
},
170 std::string
master_prefs("{\"distribution\":{");
171 for (size_t i
= 0; i
< arraysize(desired_prefs
); ++i
) {
172 master_prefs
+= (i
== 0 ? "\"" : ",\"");
173 master_prefs
+= desired_prefs
[i
].pref_name
;
174 master_prefs
+= "\":";
175 master_prefs
+= desired_prefs
[i
].is_desired
? "true" : "false";
177 master_prefs
+= "}}";
179 return new installer::MasterPreferences(master_prefs
);
182 base::win::ShortcutProperties expected_properties_
;
183 base::win::ShortcutProperties expected_start_menu_properties_
;
185 BrowserDistribution
* dist_
;
186 base::FilePath chrome_exe_
;
187 scoped_ptr
<installer::Product
> product_
;
188 scoped_ptr
<installer::MasterPreferences
> prefs_
;
190 base::ScopedTempDir temp_dir_
;
191 base::ScopedTempDir fake_user_desktop_
;
192 base::ScopedTempDir fake_common_desktop_
;
193 base::ScopedTempDir fake_user_quick_launch_
;
194 base::ScopedTempDir fake_start_menu_
;
195 base::ScopedTempDir fake_common_start_menu_
;
196 scoped_ptr
<base::ScopedPathOverride
> user_desktop_override_
;
197 scoped_ptr
<base::ScopedPathOverride
> common_desktop_override_
;
198 scoped_ptr
<base::ScopedPathOverride
> user_quick_launch_override_
;
199 scoped_ptr
<base::ScopedPathOverride
> start_menu_override_
;
200 scoped_ptr
<base::ScopedPathOverride
> common_start_menu_override_
;
202 base::FilePath user_desktop_shortcut_
;
203 base::FilePath user_quick_launch_shortcut_
;
204 base::FilePath user_start_menu_shortcut_
;
205 base::FilePath system_desktop_shortcut_
;
206 base::FilePath system_start_menu_shortcut_
;
207 base::FilePath user_alternate_desktop_shortcut_
;
212 // Test that VisualElementsManifest.xml is not created when VisualElements are
214 TEST_F(CreateVisualElementsManifestTest
, VisualElementsManifestNotCreated
) {
216 installer::CreateVisualElementsManifest(test_dir_
.path(), version_
));
217 ASSERT_FALSE(base::PathExists(manifest_path_
));
220 // Test that VisualElementsManifest.xml is created with the correct content when
221 // VisualElements are present.
222 TEST_F(CreateVisualElementsManifestTest
, VisualElementsManifestCreated
) {
223 ASSERT_TRUE(base::CreateDirectory(
224 version_dir_
.Append(installer::kVisualElements
)));
226 installer::CreateVisualElementsManifest(test_dir_
.path(), version_
));
227 ASSERT_TRUE(base::PathExists(manifest_path_
));
229 std::string read_manifest
;
230 ASSERT_TRUE(base::ReadFileToString(manifest_path_
, &read_manifest
));
232 #if defined(GOOGLE_CHROME_BUILD)
233 static const char kExpectedManifest
[] =
235 " <VisualElements\r\n"
236 " DisplayName='Google Chrome'\r\n"
237 " Logo='0.0.0.0\\VisualElements\\Logo.png'\r\n"
238 " SmallLogo='0.0.0.0\\VisualElements\\SmallLogo.png'\r\n"
239 " ForegroundText='light'\r\n"
240 " BackgroundColor='#323232'>\r\n"
241 " <DefaultTile ShowName='allLogos'/>\r\n"
242 " <SplashScreen Image='0.0.0.0\\VisualElements\\splash-620x300.png'/>"
244 " </VisualElements>\r\n"
247 static const char kExpectedManifest
[] =
249 " <VisualElements\r\n"
250 " DisplayName='Chromium'\r\n"
251 " Logo='0.0.0.0\\VisualElements\\Logo.png'\r\n"
252 " SmallLogo='0.0.0.0\\VisualElements\\SmallLogo.png'\r\n"
253 " ForegroundText='light'\r\n"
254 " BackgroundColor='#323232'>\r\n"
255 " <DefaultTile ShowName='allLogos'/>\r\n"
256 " <SplashScreen Image='0.0.0.0\\VisualElements\\splash-620x300.png'/>"
258 " </VisualElements>\r\n"
262 ASSERT_STREQ(kExpectedManifest
, read_manifest
.c_str());
265 TEST_F(InstallShortcutTest
, CreateAllShortcuts
) {
266 installer::CreateOrUpdateShortcuts(
267 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
268 installer::INSTALL_SHORTCUT_CREATE_ALL
);
269 base::win::ValidateShortcut(user_desktop_shortcut_
, expected_properties_
);
270 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
271 expected_properties_
);
272 base::win::ValidateShortcut(user_start_menu_shortcut_
,
273 expected_start_menu_properties_
);
276 TEST_F(InstallShortcutTest
, CreateAllShortcutsSystemLevel
) {
277 installer::CreateOrUpdateShortcuts(
278 chrome_exe_
, *product_
, *prefs_
, installer::ALL_USERS
,
279 installer::INSTALL_SHORTCUT_CREATE_ALL
);
280 base::win::ValidateShortcut(system_desktop_shortcut_
, expected_properties_
);
281 base::win::ValidateShortcut(system_start_menu_shortcut_
,
282 expected_start_menu_properties_
);
283 // The quick launch shortcut is always created per-user for the admin running
284 // the install (other users will get it via Active Setup).
285 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
286 expected_properties_
);
289 TEST_F(InstallShortcutTest
, CreateAllShortcutsAlternateDesktopName
) {
290 scoped_ptr
<installer::MasterPreferences
> prefs_alt_desktop(
291 GetFakeMasterPrefs(false, false, true));
292 installer::CreateOrUpdateShortcuts(
293 chrome_exe_
, *product_
, *prefs_alt_desktop
, installer::CURRENT_USER
,
294 installer::INSTALL_SHORTCUT_CREATE_ALL
);
295 base::win::ValidateShortcut(user_alternate_desktop_shortcut_
,
296 expected_properties_
);
297 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
298 expected_properties_
);
299 base::win::ValidateShortcut(user_start_menu_shortcut_
,
300 expected_start_menu_properties_
);
303 TEST_F(InstallShortcutTest
, CreateAllShortcutsButDesktopShortcut
) {
304 scoped_ptr
<installer::MasterPreferences
> prefs_no_desktop(
305 GetFakeMasterPrefs(true, false, false));
306 installer::CreateOrUpdateShortcuts(
307 chrome_exe_
, *product_
, *prefs_no_desktop
, installer::CURRENT_USER
,
308 installer::INSTALL_SHORTCUT_CREATE_ALL
);
309 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_
));
310 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
311 expected_properties_
);
312 base::win::ValidateShortcut(user_start_menu_shortcut_
,
313 expected_start_menu_properties_
);
316 TEST_F(InstallShortcutTest
, CreateAllShortcutsButQuickLaunchShortcut
) {
317 scoped_ptr
<installer::MasterPreferences
> prefs_no_ql(
318 GetFakeMasterPrefs(false, true, false));
319 installer::CreateOrUpdateShortcuts(
320 chrome_exe_
, *product_
, *prefs_no_ql
, installer::CURRENT_USER
,
321 installer::INSTALL_SHORTCUT_CREATE_ALL
);
322 base::win::ValidateShortcut(user_desktop_shortcut_
, expected_properties_
);
323 ASSERT_FALSE(base::PathExists(user_quick_launch_shortcut_
));
324 base::win::ValidateShortcut(user_start_menu_shortcut_
,
325 expected_start_menu_properties_
);
328 TEST_F(InstallShortcutTest
, ReplaceAll
) {
329 base::win::ShortcutProperties dummy_properties
;
330 base::FilePath dummy_target
;
331 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_target
));
332 dummy_properties
.set_target(dummy_target
);
333 dummy_properties
.set_working_dir(fake_user_desktop_
.path());
334 dummy_properties
.set_arguments(L
"--dummy --args");
335 dummy_properties
.set_app_id(L
"El.Dummiest");
337 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
338 user_desktop_shortcut_
, dummy_properties
,
339 base::win::SHORTCUT_CREATE_ALWAYS
));
340 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
341 user_quick_launch_shortcut_
, dummy_properties
,
342 base::win::SHORTCUT_CREATE_ALWAYS
));
343 ASSERT_TRUE(base::CreateDirectory(user_start_menu_shortcut_
.DirName()));
344 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
345 user_start_menu_shortcut_
, dummy_properties
,
346 base::win::SHORTCUT_CREATE_ALWAYS
));
348 installer::CreateOrUpdateShortcuts(
349 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
350 installer::INSTALL_SHORTCUT_REPLACE_EXISTING
);
351 base::win::ValidateShortcut(user_desktop_shortcut_
, expected_properties_
);
352 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
353 expected_properties_
);
354 base::win::ValidateShortcut(user_start_menu_shortcut_
,
355 expected_start_menu_properties_
);
358 TEST_F(InstallShortcutTest
, ReplaceExisting
) {
359 base::win::ShortcutProperties dummy_properties
;
360 base::FilePath dummy_target
;
361 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_target
));
362 dummy_properties
.set_target(dummy_target
);
363 dummy_properties
.set_working_dir(fake_user_desktop_
.path());
364 dummy_properties
.set_arguments(L
"--dummy --args");
365 dummy_properties
.set_app_id(L
"El.Dummiest");
367 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
368 user_desktop_shortcut_
, dummy_properties
,
369 base::win::SHORTCUT_CREATE_ALWAYS
));
370 ASSERT_TRUE(base::CreateDirectory(user_start_menu_shortcut_
.DirName()));
372 installer::CreateOrUpdateShortcuts(
373 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
374 installer::INSTALL_SHORTCUT_REPLACE_EXISTING
);
375 base::win::ValidateShortcut(user_desktop_shortcut_
, expected_properties_
);
376 ASSERT_FALSE(base::PathExists(user_quick_launch_shortcut_
));
377 ASSERT_FALSE(base::PathExists(user_start_menu_shortcut_
));
380 TEST_F(InstallShortcutTest
, CreateIfNoSystemLevelAllSystemShortcutsExist
) {
381 base::win::ShortcutProperties dummy_properties
;
382 base::FilePath dummy_target
;
383 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_target
));
384 dummy_properties
.set_target(dummy_target
);
386 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
387 system_desktop_shortcut_
, dummy_properties
,
388 base::win::SHORTCUT_CREATE_ALWAYS
));
389 ASSERT_TRUE(base::CreateDirectory(
390 system_start_menu_shortcut_
.DirName()));
391 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
392 system_start_menu_shortcut_
, dummy_properties
,
393 base::win::SHORTCUT_CREATE_ALWAYS
));
395 installer::CreateOrUpdateShortcuts(
396 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
397 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL
);
398 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_
));
399 ASSERT_FALSE(base::PathExists(user_start_menu_shortcut_
));
400 // There is no system-level quick launch shortcut, so creating the user-level
401 // one should always succeed.
402 ASSERT_TRUE(base::PathExists(user_quick_launch_shortcut_
));
405 TEST_F(InstallShortcutTest
, CreateIfNoSystemLevelNoSystemShortcutsExist
) {
406 installer::CreateOrUpdateShortcuts(
407 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
408 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL
);
409 base::win::ValidateShortcut(user_desktop_shortcut_
, expected_properties_
);
410 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
411 expected_properties_
);
412 base::win::ValidateShortcut(user_start_menu_shortcut_
,
413 expected_start_menu_properties_
);
416 TEST_F(InstallShortcutTest
, CreateIfNoSystemLevelSomeSystemShortcutsExist
) {
417 base::win::ShortcutProperties dummy_properties
;
418 base::FilePath dummy_target
;
419 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_target
));
420 dummy_properties
.set_target(dummy_target
);
422 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
423 system_desktop_shortcut_
, dummy_properties
,
424 base::win::SHORTCUT_CREATE_ALWAYS
));
426 installer::CreateOrUpdateShortcuts(
427 chrome_exe_
, *product_
, *prefs_
, installer::CURRENT_USER
,
428 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL
);
429 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_
));
430 base::win::ValidateShortcut(user_quick_launch_shortcut_
,
431 expected_properties_
);
432 base::win::ValidateShortcut(user_start_menu_shortcut_
,
433 expected_start_menu_properties_
);
436 TEST(EscapeXmlAttributeValueTest
, EscapeCrazyValue
) {
437 base::string16
val(L
"This has 'crazy' \"chars\" && < and > signs.");
438 static const wchar_t kExpectedEscapedVal
[] =
439 L
"This has 'crazy' \"chars\" && < and > signs.";
440 installer::EscapeXmlAttributeValueInSingleQuotes(&val
);
441 ASSERT_STREQ(kExpectedEscapedVal
, val
.c_str());
444 TEST(EscapeXmlAttributeValueTest
, DontEscapeNormalValue
) {
445 base::string16
val(L
"Google Chrome");
446 static const wchar_t kExpectedEscapedVal
[] = L
"Google Chrome";
447 installer::EscapeXmlAttributeValueInSingleQuotes(&val
);
448 ASSERT_STREQ(kExpectedEscapedVal
, val
.c_str());