1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/utf_string_conversions.h"
#include "base/test/test_shortcut_win.h"
#include "base/win/shortcut.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_shortcut_manager.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/shell_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/test_browser_thread.h"
#include "grit/theme_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
using content::BrowserThread;
namespace {
base::win::VerifyShortcutStatus VerifyProfileShortcut(
const string16& profile_name) {
FilePath exe_path;
EXPECT_TRUE(PathService::Get(base::FILE_EXE, &exe_path));
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
// Get the desktop path of the current user.
FilePath shortcut_path;
ShellUtil::GetDesktopPath(false, &shortcut_path);
// Get the name of the shortcut with profile attached
string16 shortcut_name;
ShellUtil::GetChromeShortcutName(dist, false, profile_name,
&shortcut_name);
shortcut_path = shortcut_path.Append(shortcut_name);
// TODO(hallielaine): With this new struct method for VerifyShortcut you can
// now test more properties like: arguments, icon, icon_index, and app_id.
base::win::ShortcutProperties expected_properties;
expected_properties.set_target(exe_path);
expected_properties.set_description(dist->GetAppDescription());
expected_properties.set_dual_mode(false);
return base::win::VerifyShortcut(shortcut_path, expected_properties);
}
} // namespace
class ProfileShortcutManagerTest : public testing::Test {
protected:
ProfileShortcutManagerTest()
: ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_) {
}
virtual void SetUp() OVERRIDE {
// Mock the user's Desktop into a temp directory.
ASSERT_TRUE(fake_user_desktop_.CreateUniqueTempDir());
ASSERT_TRUE(PathService::Override(base::DIR_USER_DESKTOP,
fake_user_desktop_.path()));
TestingBrowserProcess* browser_process =
static_cast<TestingBrowserProcess*>(g_browser_process);
profile_manager_.reset(new TestingProfileManager(browser_process));
ASSERT_TRUE(profile_manager_->SetUp());
dest_path_ = profile_manager_->profile_info_cache()->GetUserDataDir();
dest_path_ = dest_path_.Append(FILE_PATH_LITERAL("My profile"));
file_util::CreateDirectoryW(dest_path_);
profile_name_ = ASCIIToUTF16("My profile");
second_dest_path_ =
profile_manager_->profile_info_cache()->GetUserDataDir();
second_dest_path_ =
second_dest_path_.Append(FILE_PATH_LITERAL("My profile 2"));
file_util::CreateDirectoryW(second_dest_path_);
second_profile_name_ = ASCIIToUTF16("My profile 2");
}
virtual void TearDown() OVERRIDE {
message_loop_.RunAllPending();
int num_profiles =
profile_manager_->profile_info_cache()->GetNumberOfProfiles();
// Remove all shortcuts except the last (since it will no longer have
// an appended name).
for (int i = 0; i < num_profiles; ++i) {
const FilePath profile_path =
profile_manager_->profile_info_cache()->GetPathOfProfileAtIndex(0);
string16 profile_name;
if (i != num_profiles - 1) {
profile_name =
profile_manager_->profile_info_cache()->GetNameOfProfileAtIndex(0);
}
profile_manager_->profile_info_cache()->DeleteProfileFromCache(
profile_path);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(profile_name));
ASSERT_FALSE(file_util::PathExists(profile_path.Append(
FILE_PATH_LITERAL("Google Profile.ico"))));
}
}
void SetupDefaultProfileShortcut() {
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(profile_name_));
// A non-badged shortcut for chrome is automatically created with the
// first profile (for the case when the user deletes their only
// profile).
profile_manager_->profile_info_cache()->AddProfileToCache(
dest_path_, profile_name_, string16(), 0);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
// We now have 1 profile, so we expect a new shortcut with no profile
// information.
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(string16()));
}
void SetupAndCreateTwoShortcuts() {
ASSERT_EQ(0, profile_manager_->profile_info_cache()->GetNumberOfProfiles());
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(profile_name_));
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(second_profile_name_));
profile_manager_->profile_info_cache()->AddProfileToCache(
dest_path_, profile_name_, string16(), 0);
profile_manager_->profile_info_cache()->AddProfileToCache(
second_dest_path_, second_profile_name_, string16(), 0);
profile_manager_->profile_manager()->profile_shortcut_manager()->
CreateProfileShortcut(second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(profile_name_));
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(second_profile_name_));
}
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
scoped_ptr<TestingProfileManager> profile_manager_;
ScopedTempDir fake_user_desktop_;
FilePath dest_path_;
string16 profile_name_;
FilePath second_dest_path_;
string16 second_profile_name_;
};
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreate) {
if (!profile_manager_->profile_manager()->profile_shortcut_manager())
return;
ProfileShortcutManagerTest::SetupDefaultProfileShortcut();
profile_manager_->profile_info_cache()->AddProfileToCache(
second_dest_path_, second_profile_name_, string16(), 0);
profile_manager_->profile_manager()->profile_shortcut_manager()->
CreateProfileShortcut(second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
// We now have 2 profiles, so we expect a new shortcut with profile
// information for this 2nd profile.
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(second_profile_name_));
ASSERT_TRUE(file_util::PathExists(second_dest_path_.Append(
FILE_PATH_LITERAL("Google Profile.ico"))));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsUpdate) {
if (!profile_manager_->profile_manager()->profile_shortcut_manager())
return;
ProfileShortcutManagerTest::SetupDefaultProfileShortcut();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(second_profile_name_));
profile_manager_->profile_info_cache()->AddProfileToCache(
second_dest_path_, second_profile_name_, string16(), 0);
profile_manager_->profile_manager()->profile_shortcut_manager()->
CreateProfileShortcut(second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(second_profile_name_));
// Cause an update in ProfileShortcutManager by modifying the profile info
// cache.
string16 new_profile_name = ASCIIToUTF16("New Profile Name");
profile_manager_->profile_info_cache()->SetNameOfProfileAtIndex(
profile_manager_->profile_info_cache()->GetIndexOfProfileWithPath(
second_dest_path_),
new_profile_name);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(second_profile_name_));
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(new_profile_name));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsDeleteSecondToLast) {
if (!profile_manager_->profile_manager()->profile_shortcut_manager())
return;
ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
// Delete one shortcut
profile_manager_->profile_info_cache()->DeleteProfileFromCache(
second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(second_profile_name_));
// Verify that the profile name has been removed from the remaining shortcut
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(string16()));
// Verify that an additional shortcut, with the default profile's name does
// not exist
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(profile_name_));
}
TEST_F(ProfileShortcutManagerTest, DesktopShortcutsCreateSecond) {
if (!profile_manager_->profile_manager()->profile_shortcut_manager())
return;
ProfileShortcutManagerTest::SetupAndCreateTwoShortcuts();
// Delete one shortcut
profile_manager_->profile_info_cache()->DeleteProfileFromCache(
second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
// Verify that a default shortcut exists (no profile name/avatar)
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(string16()));
// Verify that an additional shortcut, with the default profile's name does
// not exist
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(profile_name_));
// Create a second profile and shortcut
profile_manager_->profile_info_cache()->AddProfileToCache(
second_dest_path_, second_profile_name_, string16(), 0);
profile_manager_->profile_manager()->profile_shortcut_manager()->
CreateProfileShortcut(second_dest_path_);
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
MessageLoop::current()->Run();
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(second_profile_name_));
// Verify that the original shortcut received the profile's name
EXPECT_EQ(base::win::VERIFY_SHORTCUT_SUCCESS,
VerifyProfileShortcut(profile_name_));
// Verify that a default shortcut no longer exists
EXPECT_EQ(base::win::VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND,
VerifyProfileShortcut(string16()));
}
|