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 <shlobj.h> | |
6 | |
7 #include <fstream> | 5 #include <fstream> |
8 #include <vector> | 6 #include <vector> |
9 | 7 |
10 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/md5.h" |
11 #include "base/path_service.h" | 10 #include "base/path_service.h" |
12 #include "base/md5.h" | |
13 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
14 #include "base/string16.h" | 12 #include "base/string16.h" |
15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
16 #include "base/test/test_shortcut_win.h" | 14 #include "base/test/test_shortcut_win.h" |
17 #include "base/win/shortcut.h" | 15 #include "base/win/shortcut.h" |
18 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
19 #include "chrome/installer/util/browser_distribution.h" | 17 #include "chrome/installer/util/browser_distribution.h" |
20 #include "chrome/installer/util/master_preferences.h" | 18 #include "chrome/installer/util/master_preferences.h" |
21 #include "chrome/installer/util/shell_util.h" | 19 #include "chrome/installer/util/shell_util.h" |
22 #include "chrome/installer/util/util_constants.h" | 20 #include "chrome/installer/util/util_constants.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 class ShellUtilTestWithDirAndDist : public testing::Test { | 25 class ShellUtilShortcutTest : public testing::Test { |
28 protected: | 26 protected: |
29 virtual void SetUp() { | 27 virtual void SetUp() OVERRIDE { |
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
31 dist_ = BrowserDistribution::GetDistribution(); | 28 dist_ = BrowserDistribution::GetDistribution(); |
32 ASSERT_TRUE(dist_ != NULL); | 29 ASSERT_TRUE(dist_ != NULL); |
| 30 |
| 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 32 ASSERT_TRUE(fake_user_desktop_.CreateUniqueTempDir()); |
| 33 ASSERT_TRUE(fake_common_desktop_.CreateUniqueTempDir()); |
| 34 ASSERT_TRUE(PathService::Override(base::DIR_USER_DESKTOP, |
| 35 fake_user_desktop_.path())); |
| 36 ASSERT_TRUE(PathService::Override(base::DIR_COMMON_DESKTOP, |
| 37 fake_common_desktop_.path())); |
33 } | 38 } |
34 | 39 |
35 BrowserDistribution* dist_; | 40 BrowserDistribution* dist_; |
36 | 41 |
37 ScopedTempDir temp_dir_; | 42 ScopedTempDir temp_dir_; |
| 43 |
| 44 ScopedTempDir fake_user_desktop_; |
| 45 ScopedTempDir fake_common_desktop_; |
38 }; | 46 }; |
39 | 47 |
40 // Returns the status of a call to base::win::VerifyShorcut for the properties | 48 // Returns the status of a call to base::win::VerifyShorcut for the properties |
41 // passed in. | 49 // passed in. |
42 // TODO(gab): This is only temporary while waiting for my upcoming CL that will | 50 // TODO(gab): This is only temporary while waiting for my upcoming CL that will |
43 // massively refactor the shell_util shortcut methods' interface (i.e. I didn't | 51 // massively refactor the shell_util shortcut methods' interface (i.e. I didn't |
44 // want to adapt every test here for this half-changed state as they will change | 52 // want to adapt every test here for this half-changed state as they will change |
45 // again very soon). | 53 // again very soon). |
46 base::win::VerifyShortcutStatus VerifyChromeShortcut( | 54 base::win::VerifyShortcutStatus VerifyChromeShortcut( |
47 const FilePath& exe_path, | 55 const FilePath& exe_path, |
48 const FilePath& shortcut_path, | 56 const FilePath& shortcut_path, |
49 const string16& description, | 57 const string16& description, |
50 int icon_index) { | 58 int icon_index) { |
51 base::win::ShortcutProperties expected_properties; | 59 base::win::ShortcutProperties expected_properties; |
52 expected_properties.set_target(exe_path); | 60 expected_properties.set_target(exe_path); |
53 expected_properties.set_description(description); | 61 expected_properties.set_description(description); |
54 expected_properties.set_icon(exe_path, icon_index); | 62 expected_properties.set_icon(exe_path, icon_index); |
55 return base::win::VerifyShortcut(shortcut_path, expected_properties); | 63 return base::win::VerifyShortcut(shortcut_path, expected_properties); |
56 } | 64 } |
57 | 65 |
58 } | 66 } |
59 | 67 |
60 // Test that we can open archives successfully. | 68 // Test that we can open archives successfully. |
61 TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) { | 69 TEST_F(ShellUtilShortcutTest, UpdateChromeShortcut) { |
62 // Create an executable in test path by copying ourself to it. | 70 // Create an executable in test path by copying ourself to it. |
63 wchar_t exe_full_path_str[MAX_PATH]; | 71 wchar_t exe_full_path_str[MAX_PATH]; |
64 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | 72 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); |
65 FilePath exe_full_path(exe_full_path_str); | 73 FilePath exe_full_path(exe_full_path_str); |
66 | 74 |
67 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); | 75 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); |
68 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); | 76 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); |
69 | 77 |
70 FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk"); | 78 FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk"); |
71 const string16 description(L"dummy description"); | 79 const string16 description(L"dummy description"); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 shortcut_path.value(), | 123 shortcut_path.value(), |
116 string16(), | 124 string16(), |
117 description2, | 125 description2, |
118 exe_path.value(), | 126 exe_path.value(), |
119 dist_->GetIconIndex(), | 127 dist_->GetIconIndex(), |
120 ShellUtil::SHORTCUT_NO_OPTIONS)); | 128 ShellUtil::SHORTCUT_NO_OPTIONS)); |
121 EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS, | 129 EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS, |
122 VerifyChromeShortcut(exe_path, shortcut_path, description2, 1)); | 130 VerifyChromeShortcut(exe_path, shortcut_path, description2, 1)); |
123 } | 131 } |
124 | 132 |
125 TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) { | 133 TEST_F(ShellUtilShortcutTest, CreateChromeDesktopShortcut) { |
126 // Run this test on Vista+ only if we are running elevated. | |
127 if (base::win::GetVersion() > base::win::VERSION_XP && !IsUserAnAdmin()) { | |
128 LOG(ERROR) << "Must be admin to run this test on Vista+"; | |
129 return; | |
130 } | |
131 | |
132 // Create an executable in test path by copying ourself to it. | 134 // Create an executable in test path by copying ourself to it. |
133 wchar_t exe_full_path_str[MAX_PATH]; | 135 wchar_t exe_full_path_str[MAX_PATH]; |
134 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | 136 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); |
135 FilePath exe_full_path(exe_full_path_str); | 137 FilePath exe_full_path(exe_full_path_str); |
136 | 138 |
137 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); | 139 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); |
138 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); | 140 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); |
139 | 141 |
140 const string16 description(L"dummy description"); | 142 const string16 description(L"dummy description"); |
141 | 143 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS, | 301 EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS, |
300 VerifyChromeShortcut( | 302 VerifyChromeShortcut( |
301 exe_path, second_profile_shortcut_path, description, 0)); | 303 exe_path, second_profile_shortcut_path, description, 0)); |
302 std::vector<string16> profile_names; | 304 std::vector<string16> profile_names; |
303 profile_names.push_back(default_profile_shortcut_name); | 305 profile_names.push_back(default_profile_shortcut_name); |
304 profile_names.push_back(second_profile_shortcut_name); | 306 profile_names.push_back(second_profile_shortcut_name); |
305 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( | 307 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( |
306 profile_names)); | 308 profile_names)); |
307 } | 309 } |
308 | 310 |
309 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdBasic) { | 311 TEST(ShellUtilTest, BuildAppModelIdBasic) { |
310 std::vector<string16> components; | 312 std::vector<string16> components; |
311 const string16 base_app_id(dist_->GetBaseAppId()); | 313 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 314 const string16 base_app_id(dist->GetBaseAppId()); |
312 components.push_back(base_app_id); | 315 components.push_back(base_app_id); |
313 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); | 316 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); |
314 } | 317 } |
315 | 318 |
316 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdManySmall) { | 319 TEST(ShellUtilTest, BuildAppModelIdManySmall) { |
317 std::vector<string16> components; | 320 std::vector<string16> components; |
318 const string16 suffixed_app_id(dist_->GetBaseAppId().append(L".gab")); | 321 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 322 const string16 suffixed_app_id(dist->GetBaseAppId().append(L".gab")); |
319 components.push_back(suffixed_app_id); | 323 components.push_back(suffixed_app_id); |
320 components.push_back(L"Default"); | 324 components.push_back(L"Default"); |
321 components.push_back(L"Test"); | 325 components.push_back(L"Test"); |
322 ASSERT_EQ(suffixed_app_id + L".Default.Test", | 326 ASSERT_EQ(suffixed_app_id + L".Default.Test", |
323 ShellUtil::BuildAppModelId(components)); | 327 ShellUtil::BuildAppModelId(components)); |
324 } | 328 } |
325 | 329 |
326 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongUsernameNormalProfile) { | 330 TEST(ShellUtilTest, BuildAppModelIdLongUsernameNormalProfile) { |
327 std::vector<string16> components; | 331 std::vector<string16> components; |
328 const string16 long_appname( | 332 const string16 long_appname( |
329 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 333 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |
330 L"that_goes_over_64_characters"); | 334 L"that_goes_over_64_characters"); |
331 components.push_back(long_appname); | 335 components.push_back(long_appname); |
332 components.push_back(L"Default"); | 336 components.push_back(L"Default"); |
333 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", | 337 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", |
334 ShellUtil::BuildAppModelId(components)); | 338 ShellUtil::BuildAppModelId(components)); |
335 } | 339 } |
336 | 340 |
337 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongEverything) { | 341 TEST(ShellUtilTest, BuildAppModelIdLongEverything) { |
338 std::vector<string16> components; | 342 std::vector<string16> components; |
339 const string16 long_appname( | 343 const string16 long_appname( |
340 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 344 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |
341 L"that_goes_over_64_characters"); | 345 L"that_goes_over_64_characters"); |
342 components.push_back(long_appname); | 346 components.push_back(long_appname); |
343 components.push_back( | 347 components.push_back( |
344 L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); | 348 L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); |
345 const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); | 349 const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); |
346 ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); | 350 ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); |
347 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", | 351 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", |
(...skipping 27 matching lines...) Expand all Loading... |
375 | 379 |
376 const string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", | 380 const string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", |
377 L"MZXW6YTB", L"MZXW6YTBOI"}; | 381 L"MZXW6YTB", L"MZXW6YTBOI"}; |
378 | 382 |
379 // Run the tests, with one more letter in the input every pass. | 383 // Run the tests, with one more letter in the input every pass. |
380 for (int i = 0; i < arraysize(expected); ++i) { | 384 for (int i = 0; i < arraysize(expected); ++i) { |
381 ASSERT_EQ(expected[i], | 385 ASSERT_EQ(expected[i], |
382 ShellUtil::ByteArrayToBase32(test_array, i)); | 386 ShellUtil::ByteArrayToBase32(test_array, i)); |
383 } | 387 } |
384 } | 388 } |
OLD | NEW |