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

Side by Side Diff: chrome/installer/setup/install_unittest.cc

Issue 10836247: Refactor ShellUtil shortcut code -- single multi-purpose methods as opposed to many slighlty diffe… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respect microsoft's definition of correct C++ Created 8 years, 2 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 | « chrome/installer/setup/install.cc ('k') | chrome/installer/setup/install_worker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <objbase.h>
6
5 #include <string> 7 #include <string>
6 8
9 #include "base/file_path.h"
7 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/path_service.h"
8 #include "base/platform_file.h" 12 #include "base/platform_file.h"
9 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
10 #include "base/string16.h" 14 #include "base/string16.h"
11 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
12 #include "base/version.h" 16 #include "base/version.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/test/test_shortcut_win.h"
19 #include "base/win/shortcut.h"
13 #include "chrome/installer/setup/install.h" 20 #include "chrome/installer/setup/install.h"
21 #include "chrome/installer/setup/install_worker.h"
14 #include "chrome/installer/setup/setup_constants.h" 22 #include "chrome/installer/setup/setup_constants.h"
23 #include "chrome/installer/util/browser_distribution.h"
24 #include "chrome/installer/util/installer_state.h"
25 #include "chrome/installer/util/product.h"
26 #include "chrome/installer/util/util_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
16 28
17 namespace { 29 namespace {
18 30
19 class CreateVisualElementsManifestTest : public testing::Test { 31 class CreateVisualElementsManifestTest : public testing::Test {
20 protected: 32 protected:
21 virtual void SetUp() OVERRIDE { 33 virtual void SetUp() OVERRIDE {
22 // Create a temp directory for testing. 34 // Create a temp directory for testing.
23 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 35 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
24 36
(...skipping 17 matching lines...) Expand all
42 // A dummy version number used to create the version directory. 54 // A dummy version number used to create the version directory.
43 Version version_; 55 Version version_;
44 56
45 // The path to |test_dir_|\|version_|. 57 // The path to |test_dir_|\|version_|.
46 FilePath version_dir_; 58 FilePath version_dir_;
47 59
48 // The path to VisualElementsManifest.xml. 60 // The path to VisualElementsManifest.xml.
49 FilePath manifest_path_; 61 FilePath manifest_path_;
50 }; 62 };
51 63
64 class InstallShortcutTest : public testing::Test {
65 protected:
66 // A mock installer state on which .system_install() will return
67 // |system_install|, .target_path() will return |target_path|, and .is_msi()
68 // will return |is_msi|.
69 class MockInstallerState : public installer::InstallerState {
70 public:
71 MockInstallerState(const FilePath& target_path,
72 bool system_install,
73 bool is_msi) : InstallerState() {
74 target_path_ = target_path;
75 set_level(system_install ? SYSTEM_LEVEL : USER_LEVEL);
76 msi_ = is_msi;
77 }
78 };
79
80 virtual void SetUp() OVERRIDE {
81 EXPECT_EQ(S_OK, CoInitialize(NULL));
82
83 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
84 ASSERT_TRUE(dist != NULL);
85 product_.reset(new installer::Product(dist));
86
87 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
88 chrome_exe_ = temp_dir_.path().Append(installer::kChromeExe);
89 setup_exe_ = temp_dir_.path().Append(installer::kSetupExe);
90 EXPECT_EQ(0, file_util::WriteFile(chrome_exe_, "", 0));
91 EXPECT_EQ(0, file_util::WriteFile(setup_exe_, "", 0));
92
93 mock_user_installer_state_.reset(
94 new MockInstallerState(chrome_exe_.DirName(), false, false));
95 mock_system_installer_state_.reset(
96 new MockInstallerState(chrome_exe_.DirName(), true, false));
97
98 expected_properties_.set_target(chrome_exe_);
99 expected_properties_.set_dual_mode(true);
100
101 ASSERT_TRUE(fake_user_desktop_.CreateUniqueTempDir());
102 ASSERT_TRUE(fake_common_desktop_.CreateUniqueTempDir());
103 ASSERT_TRUE(fake_user_quick_launch_.CreateUniqueTempDir());
104 ASSERT_TRUE(fake_default_user_quick_launch_.CreateUniqueTempDir());
105 ASSERT_TRUE(fake_start_menu_.CreateUniqueTempDir());
106 ASSERT_TRUE(fake_common_start_menu_.CreateUniqueTempDir());
107 ASSERT_TRUE(PathService::Override(base::DIR_USER_DESKTOP,
108 fake_user_desktop_.path()));
109 ASSERT_TRUE(PathService::Override(base::DIR_COMMON_DESKTOP,
110 fake_common_desktop_.path()));
111 ASSERT_TRUE(PathService::Override(base::DIR_USER_QUICK_LAUNCH,
112 fake_user_quick_launch_.path()));
113 ASSERT_TRUE(PathService::Override(base::DIR_DEFAULT_USER_QUICK_LAUNCH,
114 fake_default_user_quick_launch_.path()));
115 ASSERT_TRUE(PathService::Override(base::DIR_START_MENU,
116 fake_start_menu_.path()));
117 ASSERT_TRUE(PathService::Override(base::DIR_COMMON_START_MENU,
118 fake_common_start_menu_.path()));
119
120 string16 shortcut_name(dist->GetAppShortCutName() + installer::kLnkExt);
121 string16 uninstall_shorcut_name(
122 dist->GetUninstallLinkName() + installer::kLnkExt);
123 string16 alternate_shortcut_name(
124 dist->GetAlternateApplicationName() + installer::kLnkExt);
125
126 user_desktop_shortcut_ =
127 fake_user_desktop_.path().Append(shortcut_name);
128 user_quick_launch_shortcut_ =
129 fake_user_quick_launch_.path().Append(shortcut_name);
130 user_start_menu_shortcut_ =
131 fake_start_menu_.path().Append(dist->GetAppShortCutName())
132 .Append(shortcut_name);
133 user_uninstall_shortcut_ =
134 fake_start_menu_.path().Append(dist->GetAppShortCutName())
135 .Append(uninstall_shorcut_name);
136 system_desktop_shortcut_ =
137 fake_common_desktop_.path().Append(shortcut_name);
138 system_quick_launch_shortcut_ =
139 fake_default_user_quick_launch_.path().Append(shortcut_name);
140 system_start_menu_shortcut_ =
141 fake_common_start_menu_.path().Append(dist->GetAppShortCutName())
142 .Append(shortcut_name);
143 system_uninstall_shortcut_ =
144 fake_common_start_menu_.path().Append(dist->GetAppShortCutName())
145 .Append(uninstall_shorcut_name);
146 user_alternate_desktop_shortcut_ =
147 fake_user_desktop_.path().Append(alternate_shortcut_name);
148 }
149
150 virtual void TearDown() OVERRIDE {
151 // Try to unpin potentially pinned shortcuts (although pinning isn't tested,
152 // the call itself might still have pinned the Start Menu shortcuts).
153 base::win::TaskbarUnpinShortcutLink(
154 user_start_menu_shortcut_.value().c_str());
155 base::win::TaskbarUnpinShortcutLink(
156 system_start_menu_shortcut_.value().c_str());
157 CoUninitialize();
158 }
159
160 base::win::ShortcutProperties GetExpectedUninstallShortcutProperties(
161 bool system_level) {
162 CommandLine arguments(CommandLine::NO_PROGRAM);
163 const installer::InstallerState& installer_state = system_level ?
164 *mock_system_installer_state_ : *mock_user_installer_state_;
165 AppendUninstallCommandLineFlags(installer_state, *product_, &arguments);
166
167 base::win::ShortcutProperties properties;
168 properties.set_target(setup_exe_);
169 properties.set_arguments(arguments.GetCommandLineString());
170 return properties;
171 }
172
173 base::win::ShortcutProperties expected_properties_;
174
175 FilePath chrome_exe_;
176 FilePath setup_exe_;
177 scoped_ptr<installer::Product> product_;
178 scoped_ptr<MockInstallerState> mock_user_installer_state_;
179 scoped_ptr<MockInstallerState> mock_system_installer_state_;
180
181 ScopedTempDir temp_dir_;
182 ScopedTempDir fake_user_desktop_;
183 ScopedTempDir fake_common_desktop_;
184 ScopedTempDir fake_user_quick_launch_;
185 ScopedTempDir fake_default_user_quick_launch_;
186 ScopedTempDir fake_start_menu_;
187 ScopedTempDir fake_common_start_menu_;
188
189 FilePath user_desktop_shortcut_;
190 FilePath user_quick_launch_shortcut_;
191 FilePath user_start_menu_shortcut_;
192 FilePath user_uninstall_shortcut_;
193 FilePath system_desktop_shortcut_;
194 FilePath system_quick_launch_shortcut_;
195 FilePath system_start_menu_shortcut_;
196 FilePath system_uninstall_shortcut_;
197 FilePath user_alternate_desktop_shortcut_;
198 };
199
52 } // namespace 200 } // namespace
53 201
54 // Test that VisualElementsManifest.xml is not created when VisualElements are 202 // Test that VisualElementsManifest.xml is not created when VisualElements are
55 // not present. 203 // not present.
56 TEST_F(CreateVisualElementsManifestTest, VisualElementsManifestNotCreated) { 204 TEST_F(CreateVisualElementsManifestTest, VisualElementsManifestNotCreated) {
57 ASSERT_TRUE( 205 ASSERT_TRUE(
58 installer::CreateVisualElementsManifest(test_dir_.path(), version_)); 206 installer::CreateVisualElementsManifest(test_dir_.path(), version_));
59 ASSERT_FALSE(file_util::PathExists(manifest_path_)); 207 ASSERT_FALSE(file_util::PathExists(manifest_path_));
60 } 208 }
61 209
(...skipping 19 matching lines...) Expand all
81 " BackgroundColor='white'>\r\n" 229 " BackgroundColor='white'>\r\n"
82 " <DefaultTile ShowName='allLogos'/>\r\n" 230 " <DefaultTile ShowName='allLogos'/>\r\n"
83 " <SplashScreen Image='0.0.0.0\\VisualElements\\splash-620x300.png'/>" 231 " <SplashScreen Image='0.0.0.0\\VisualElements\\splash-620x300.png'/>"
84 "\r\n" 232 "\r\n"
85 " </VisualElements>\r\n" 233 " </VisualElements>\r\n"
86 "</Application>"; 234 "</Application>";
87 235
88 ASSERT_STREQ(kExpectedManifest, read_manifest.c_str()); 236 ASSERT_STREQ(kExpectedManifest, read_manifest.c_str());
89 } 237 }
90 238
239 TEST_F(InstallShortcutTest, CreateAllShortcuts) {
240 CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
241 installer::INSTALL_SHORTCUT_CREATE_ALL, false);
242 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
243 base::win::ValidateShortcut(user_quick_launch_shortcut_,
244 expected_properties_);
245 base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
246 base::win::ShortcutProperties expected_uninstall_properties(
247 GetExpectedUninstallShortcutProperties(false));
248 base::win::ValidateShortcut(user_uninstall_shortcut_,
249 expected_uninstall_properties);
250 }
251
252 TEST_F(InstallShortcutTest, CreateAllShortcutsSystemLevel) {
253 CreateOrUpdateShortcuts(*mock_system_installer_state_, setup_exe_, *product_,
254 installer::INSTALL_SHORTCUT_CREATE_ALL, false);
255 base::win::ValidateShortcut(system_desktop_shortcut_, expected_properties_);
256 base::win::ValidateShortcut(system_quick_launch_shortcut_,
257 expected_properties_);
258 base::win::ValidateShortcut(system_start_menu_shortcut_,
259 expected_properties_);
260 base::win::ShortcutProperties expected_uninstall_properties(
261 GetExpectedUninstallShortcutProperties(true));
262 base::win::ValidateShortcut(system_uninstall_shortcut_,
263 expected_uninstall_properties);
264 // On system-level installs, the quick launch shortcut for the current user
265 // should also be installed.
266 base::win::ValidateShortcut(user_quick_launch_shortcut_,
267 expected_properties_);
268 }
269
270 TEST_F(InstallShortcutTest, CreateAllShortcutsAlternateDesktopName) {
271 CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
272 installer::INSTALL_SHORTCUT_CREATE_ALL, true);
273 base::win::ValidateShortcut(user_alternate_desktop_shortcut_,
274 expected_properties_);
275 base::win::ValidateShortcut(user_quick_launch_shortcut_,
276 expected_properties_);
277 base::win::ValidateShortcut(user_start_menu_shortcut_,
278 expected_properties_);
279 base::win::ShortcutProperties expected_uninstall_properties(
280 GetExpectedUninstallShortcutProperties(false));
281 base::win::ValidateShortcut(user_uninstall_shortcut_,
282 expected_uninstall_properties);
283 }
284
285 TEST_F(InstallShortcutTest, CreateMandatoryShortcuts) {
286 CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
287 installer::INSTALL_SHORTCUT_CREATE_MANDATORY, false);
288 ASSERT_FALSE(file_util::PathExists(user_desktop_shortcut_));
289 ASSERT_FALSE(file_util::PathExists(user_quick_launch_shortcut_));
290 base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
291 base::win::ShortcutProperties expected_uninstall_properties(
292 GetExpectedUninstallShortcutProperties(false));
293 base::win::ValidateShortcut(user_uninstall_shortcut_,
294 expected_uninstall_properties);
295 }
296
297 TEST_F(InstallShortcutTest, ReplaceAll) {
298 base::win::ShortcutProperties dummy_properties;
299 FilePath dummy_target;
300 ASSERT_TRUE(
301 file_util::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target));
302 dummy_properties.set_target(dummy_target);
303 dummy_properties.set_working_dir(fake_user_desktop_.path());
304 dummy_properties.set_arguments(L"--dummy --args");
305 dummy_properties.set_app_id(L"El.Dummiest");
306
307 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
308 user_desktop_shortcut_, dummy_properties,
309 base::win::SHORTCUT_CREATE_ALWAYS));
310 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
311 user_quick_launch_shortcut_, dummy_properties,
312 base::win::SHORTCUT_CREATE_ALWAYS));
313 ASSERT_TRUE(file_util::CreateDirectory(user_start_menu_shortcut_.DirName()));
314 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
315 user_start_menu_shortcut_, dummy_properties,
316 base::win::SHORTCUT_CREATE_ALWAYS));
317 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
318 user_uninstall_shortcut_, dummy_properties,
319 base::win::SHORTCUT_CREATE_ALWAYS));
320
321 CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
322 installer::INSTALL_SHORTCUT_REPLACE_EXISTING, false);
323 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
324 base::win::ValidateShortcut(user_quick_launch_shortcut_,
325 expected_properties_);
326 base::win::ValidateShortcut(user_start_menu_shortcut_,
327 expected_properties_);
328 base::win::ShortcutProperties expected_uninstall_properties(
329 GetExpectedUninstallShortcutProperties(false));
330 base::win::ValidateShortcut(user_uninstall_shortcut_,
331 expected_uninstall_properties);
332 }
333
334 TEST_F(InstallShortcutTest, ReplaceExisting) {
335 base::win::ShortcutProperties dummy_properties;
336 FilePath dummy_target;
337 ASSERT_TRUE(
338 file_util::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target));
339 dummy_properties.set_target(dummy_target);
340 dummy_properties.set_working_dir(fake_user_desktop_.path());
341 dummy_properties.set_arguments(L"--dummy --args");
342 dummy_properties.set_app_id(L"El.Dummiest");
343
344 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
345 user_desktop_shortcut_, dummy_properties,
346 base::win::SHORTCUT_CREATE_ALWAYS));
347 ASSERT_TRUE(file_util::CreateDirectory(user_start_menu_shortcut_.DirName()));
348 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink(
349 user_uninstall_shortcut_, dummy_properties,
350 base::win::SHORTCUT_CREATE_ALWAYS));
351
352 CreateOrUpdateShortcuts(*mock_user_installer_state_, setup_exe_, *product_,
353 installer::INSTALL_SHORTCUT_REPLACE_EXISTING, false);
354 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
355 base::win::ShortcutProperties expected_uninstall_properties(
356 GetExpectedUninstallShortcutProperties(false));
357 base::win::ValidateShortcut(user_uninstall_shortcut_,
358 expected_uninstall_properties);
359 ASSERT_FALSE(file_util::PathExists(user_quick_launch_shortcut_));
360 ASSERT_FALSE(file_util::PathExists(user_start_menu_shortcut_));
361 }
362
363 TEST_F(InstallShortcutTest, NoUninstallLinkForMSIInstalls) {
364 MockInstallerState no_msi_installer_state(chrome_exe_.DirName(), false, true);
365 CreateOrUpdateShortcuts(no_msi_installer_state, setup_exe_, *product_,
366 installer::INSTALL_SHORTCUT_CREATE_ALL, false);
367 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_);
368 base::win::ValidateShortcut(user_quick_launch_shortcut_,
369 expected_properties_);
370 base::win::ValidateShortcut(user_start_menu_shortcut_, expected_properties_);
371 ASSERT_FALSE(file_util::PathExists(user_uninstall_shortcut_));
372 }
373
91 TEST(EscapeXmlAttributeValueTest, EscapeCrazyValue) { 374 TEST(EscapeXmlAttributeValueTest, EscapeCrazyValue) {
92 string16 val(L"This has 'crazy' \"chars\" && < and > signs."); 375 string16 val(L"This has 'crazy' \"chars\" && < and > signs.");
93 static const wchar_t kExpectedEscapedVal[] = 376 static const wchar_t kExpectedEscapedVal[] =
94 L"This has &apos;crazy&apos; \"chars\" &amp;&amp; &lt; and > signs."; 377 L"This has &apos;crazy&apos; \"chars\" &amp;&amp; &lt; and > signs.";
95 installer::EscapeXmlAttributeValueInSingleQuotes(&val); 378 installer::EscapeXmlAttributeValueInSingleQuotes(&val);
96 ASSERT_STREQ(kExpectedEscapedVal, val.c_str()); 379 ASSERT_STREQ(kExpectedEscapedVal, val.c_str());
97 } 380 }
98 381
99 TEST(EscapeXmlAttributeValueTest, DontEscapeNormalValue) { 382 TEST(EscapeXmlAttributeValueTest, DontEscapeNormalValue) {
100 string16 val(L"Google Chrome"); 383 string16 val(L"Google Chrome");
101 static const wchar_t kExpectedEscapedVal[] = L"Google Chrome"; 384 static const wchar_t kExpectedEscapedVal[] = L"Google Chrome";
102 installer::EscapeXmlAttributeValueInSingleQuotes(&val); 385 installer::EscapeXmlAttributeValueInSingleQuotes(&val);
103 ASSERT_STREQ(kExpectedEscapedVal, val.c_str()); 386 ASSERT_STREQ(kExpectedEscapedVal, val.c_str());
104 } 387 }
OLDNEW
« no previous file with comments | « chrome/installer/setup/install.cc ('k') | chrome/installer/setup/install_worker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698