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

Side by Side Diff: chrome/browser/profiles/profile_manager_unittest.cc

Issue 8815005: Lands http://codereview.chromium.org/8801034/ in hopes of getting a (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 base::SystemMonitor::AllocateSystemIOPorts(); 90 base::SystemMonitor::AllocateSystemIOPorts();
91 #endif 91 #endif
92 system_monitor_dummy_.reset(new base::SystemMonitor); 92 system_monitor_dummy_.reset(new base::SystemMonitor);
93 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( 93 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread(
94 &io_thread_); 94 &io_thread_);
95 } 95 }
96 96
97 virtual void SetUp() { 97 virtual void SetUp() {
98 // Create a new temporary directory, and store the path 98 // Create a new temporary directory, and store the path
99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
100 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( 100 profile_manager_.reset(new testing::ProfileManager(temp_dir_.path()));
101 new testing::ProfileManager(temp_dir_.path()));
102 #if defined(OS_WIN) 101 #if defined(OS_WIN)
103 // Force the ProfileInfoCache to be created immediately, so we can 102 // Force the ProfileInfoCache to be created immediately, so we can
104 // remove the shortcut manager for testing. 103 // remove the shortcut manager for testing.
105 ProfileManager* profile_manager = g_browser_process->profile_manager(); 104 profile_manager_->GetProfileInfoCache();
106 profile_manager->GetProfileInfoCache(); 105 profile_manager_->RemoveProfileShortcutManagerForTesting();
107 profile_manager->RemoveProfileShortcutManagerForTesting();
108 #endif 106 #endif
109 #if defined(OS_CHROMEOS) 107 #if defined(OS_CHROMEOS)
110 CommandLine *cl = CommandLine::ForCurrentProcess(); 108 CommandLine *cl = CommandLine::ForCurrentProcess();
111 cl->AppendSwitch(switches::kTestType); 109 cl->AppendSwitch(switches::kTestType);
112 #endif 110 #endif
113 } 111 }
114 112
115 virtual void TearDown() { 113 virtual void TearDown() {
116 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( 114 profile_manager_.reset();
117 NULL);
118 message_loop_.RunAllPending(); 115 message_loop_.RunAllPending();
119 } 116 }
120 117
121 class MockObserver : public ProfileManagerObserver { 118 class MockObserver : public ProfileManagerObserver {
122 public: 119 public:
123 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); 120 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status));
124 }; 121 };
125 122
126 #if defined(OS_CHROMEOS) 123 #if defined(OS_CHROMEOS)
127 // Do not change order of stub_cros_enabler_, which needs to be constructed 124 // Do not change order of stub_cros_enabler_, which needs to be constructed
128 // before io_thread_ which requires CrosLibrary to be initialized to construct 125 // before io_thread_ which requires CrosLibrary to be initialized to construct
129 // its data member pref_proxy_config_tracker_ on ChromeOS. 126 // its data member pref_proxy_config_tracker_ on ChromeOS.
130 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; 127 chromeos::ScopedStubCrosEnabler stub_cros_enabler_;
131 #endif 128 #endif
132 129
133 // The path to temporary directory used to contain the test operations. 130 // The path to temporary directory used to contain the test operations.
134 ScopedTempDir temp_dir_; 131 ScopedTempDir temp_dir_;
135 ScopedTestingLocalState local_state_; 132 ScopedTestingLocalState local_state_;
136 scoped_refptr<ExtensionEventRouterForwarder> 133 scoped_refptr<ExtensionEventRouterForwarder>
137 extension_event_router_forwarder_; 134 extension_event_router_forwarder_;
138 135
139 MessageLoopForUI message_loop_; 136 MessageLoopForUI message_loop_;
140 content::TestBrowserThread ui_thread_; 137 content::TestBrowserThread ui_thread_;
141 content::TestBrowserThread db_thread_; 138 content::TestBrowserThread db_thread_;
142 content::TestBrowserThread file_thread_; 139 content::TestBrowserThread file_thread_;
143 // IOThread is necessary for the creation of some services below. 140 // IOThread is necessary for the creation of some services below.
144 IOThread io_thread_; 141 IOThread io_thread_;
145 142
146 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; 143 scoped_ptr<base::SystemMonitor> system_monitor_dummy_;
144
145 // Also will test profile deletion.
146 scoped_ptr<ProfileManager> profile_manager_;
147 }; 147 };
148 148
149 TEST_F(ProfileManagerTest, GetProfile) { 149 TEST_F(ProfileManagerTest, GetProfile) {
150 FilePath dest_path = temp_dir_.path(); 150 FilePath dest_path = temp_dir_.path();
151 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 151 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
152 152
153 ProfileManager* profile_manager = g_browser_process->profile_manager();
154
155 Profile* profile; 153 Profile* profile;
156 154
157 // Successfully create a profile. 155 // Successfully create a profile.
158 profile = profile_manager->GetProfile(dest_path); 156 profile = profile_manager_->GetProfile(dest_path);
159 EXPECT_TRUE(profile); 157 EXPECT_TRUE(profile);
160 158
161 // The profile already exists when we call GetProfile. Just load it. 159 // The profile already exists when we call GetProfile. Just load it.
162 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); 160 EXPECT_EQ(profile, profile_manager_->GetProfile(dest_path));
163 } 161 }
164 162
165 TEST_F(ProfileManagerTest, DefaultProfileDir) { 163 TEST_F(ProfileManagerTest, DefaultProfileDir) {
166 FilePath expected_default = 164 FilePath expected_default =
167 FilePath().AppendASCII(chrome::kInitialProfile); 165 FilePath().AppendASCII(chrome::kInitialProfile);
168 EXPECT_EQ( 166 EXPECT_EQ(expected_default.value(),
169 expected_default.value(), 167 profile_manager_->GetInitialProfileDir().value());
170 g_browser_process->profile_manager()->GetInitialProfileDir().value());
171 } 168 }
172 169
173 #if defined(OS_CHROMEOS) 170 #if defined(OS_CHROMEOS)
174 // This functionality only exists on Chrome OS. 171 // This functionality only exists on Chrome OS.
175 TEST_F(ProfileManagerTest, LoggedInProfileDir) { 172 TEST_F(ProfileManagerTest, LoggedInProfileDir) {
176 CommandLine *cl = CommandLine::ForCurrentProcess(); 173 CommandLine *cl = CommandLine::ForCurrentProcess();
177 std::string profile_dir("my_user"); 174 std::string profile_dir("my_user");
178 175
179 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); 176 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir);
180 177
181 FilePath expected_default = 178 FilePath expected_default =
182 FilePath().AppendASCII(chrome::kInitialProfile); 179 FilePath().AppendASCII(chrome::kInitialProfile);
183 ProfileManager* profile_manager = g_browser_process->profile_manager();
184 EXPECT_EQ(expected_default.value(), 180 EXPECT_EQ(expected_default.value(),
185 profile_manager->GetInitialProfileDir().value()); 181 profile_manager_->GetInitialProfileDir().value());
186 182
187 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, 183 profile_manager_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
188 content::NotificationService::AllSources(), 184 content::NotificationService::AllSources(),
189 content::NotificationService::NoDetails()); 185 content::NotificationService::NoDetails());
190 FilePath expected_logged_in(profile_dir); 186 FilePath expected_logged_in(profile_dir);
191 EXPECT_EQ(expected_logged_in.value(), 187 EXPECT_EQ(expected_logged_in.value(),
192 profile_manager->GetInitialProfileDir().value()); 188 profile_manager_->GetInitialProfileDir().value());
193 VLOG(1) << temp_dir_.path().Append( 189 VLOG(1) << temp_dir_.path().Append(
194 profile_manager->GetInitialProfileDir()).value(); 190 profile_manager_->GetInitialProfileDir()).value();
195 } 191 }
196 192
197 #endif 193 #endif
198 194
199 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { 195 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
200 FilePath dest_path1 = temp_dir_.path(); 196 FilePath dest_path1 = temp_dir_.path();
201 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 197 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
202 198
203 FilePath dest_path2 = temp_dir_.path(); 199 FilePath dest_path2 = temp_dir_.path();
204 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 200 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
205 201
206 ProfileManager* profile_manager = g_browser_process->profile_manager();
207
208 // Successfully create the profiles. 202 // Successfully create the profiles.
209 TestingProfile* profile1 = 203 TestingProfile* profile1 =
210 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); 204 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path1));
211 ASSERT_TRUE(profile1); 205 ASSERT_TRUE(profile1);
212 206
213 TestingProfile* profile2 = 207 TestingProfile* profile2 =
214 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); 208 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path2));
215 ASSERT_TRUE(profile2); 209 ASSERT_TRUE(profile2);
216 210
217 // Force lazy-init of some profile services to simulate use. 211 // Force lazy-init of some profile services to simulate use.
218 profile1->CreateHistoryService(true, false); 212 profile1->CreateHistoryService(true, false);
219 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); 213 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS));
220 profile1->CreateBookmarkModel(true); 214 profile1->CreateBookmarkModel(true);
221 EXPECT_TRUE(profile1->GetBookmarkModel()); 215 EXPECT_TRUE(profile1->GetBookmarkModel());
222 profile2->CreateBookmarkModel(true); 216 profile2->CreateBookmarkModel(true);
223 EXPECT_TRUE(profile2->GetBookmarkModel()); 217 EXPECT_TRUE(profile2->GetBookmarkModel());
224 profile2->CreateHistoryService(true, false); 218 profile2->CreateHistoryService(true, false);
225 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); 219 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS));
226 220
227 // Make sure any pending tasks run before we destroy the profiles. 221 // Make sure any pending tasks run before we destroy the profiles.
228 message_loop_.RunAllPending(); 222 message_loop_.RunAllPending();
229 223
230 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( 224 profile_manager_.reset();
231 NULL);
232 225
233 // Make sure history cleans up correctly. 226 // Make sure history cleans up correctly.
234 message_loop_.RunAllPending(); 227 message_loop_.RunAllPending();
235 } 228 }
236 229
237 MATCHER(NotFail, "Profile creation failure status is not reported.") { 230 MATCHER(NotFail, "Profile creation failure status is not reported.") {
238 return arg == ProfileManagerObserver::STATUS_CREATED || 231 return arg == ProfileManagerObserver::STATUS_CREATED ||
239 arg == ProfileManagerObserver::STATUS_INITIALIZED; 232 arg == ProfileManagerObserver::STATUS_INITIALIZED;
240 } 233 }
241 234
242 // Tests asynchronous profile creation mechanism. 235 // Tests asynchronous profile creation mechanism.
243 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { 236 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) {
244 FilePath dest_path = 237 FilePath dest_path =
245 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); 238 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile"));
246 239
247 MockObserver mock_observer; 240 MockObserver mock_observer;
248 EXPECT_CALL(mock_observer, OnProfileCreated( 241 EXPECT_CALL(mock_observer, OnProfileCreated(
249 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); 242 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
250 243
251 g_browser_process->profile_manager()->CreateProfileAsync(dest_path, 244 profile_manager_->CreateProfileAsync(dest_path, &mock_observer);
252 &mock_observer);
253 245
254 message_loop_.RunAllPending(); 246 message_loop_.RunAllPending();
255 } 247 }
256 248
257 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { 249 MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
258 if (!g_created_profile) 250 if (!g_created_profile)
259 g_created_profile = arg; 251 g_created_profile = arg;
260 return arg != NULL && arg == g_created_profile; 252 return arg != NULL && arg == g_created_profile;
261 } 253 }
262 254
263 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { 255 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
264 FilePath dest_path = 256 FilePath dest_path =
265 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); 257 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile"));
266 258
267 g_created_profile = NULL; 259 g_created_profile = NULL;
268 260
269 MockObserver mock_observer1; 261 MockObserver mock_observer1;
270 EXPECT_CALL(mock_observer1, OnProfileCreated( 262 EXPECT_CALL(mock_observer1, OnProfileCreated(
271 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 263 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
272 MockObserver mock_observer2; 264 MockObserver mock_observer2;
273 EXPECT_CALL(mock_observer2, OnProfileCreated( 265 EXPECT_CALL(mock_observer2, OnProfileCreated(
274 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 266 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
275 MockObserver mock_observer3; 267 MockObserver mock_observer3;
276 EXPECT_CALL(mock_observer3, OnProfileCreated( 268 EXPECT_CALL(mock_observer3, OnProfileCreated(
277 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 269 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
278 270
279 ProfileManager* profile_manager = g_browser_process->profile_manager(); 271 profile_manager_->CreateProfileAsync(dest_path, &mock_observer1);
280 272 profile_manager_->CreateProfileAsync(dest_path, &mock_observer2);
281 profile_manager->CreateProfileAsync(dest_path, &mock_observer1); 273 profile_manager_->CreateProfileAsync(dest_path, &mock_observer3);
282 profile_manager->CreateProfileAsync(dest_path, &mock_observer2);
283 profile_manager->CreateProfileAsync(dest_path, &mock_observer3);
284 274
285 message_loop_.RunAllPending(); 275 message_loop_.RunAllPending();
286 } 276 }
287 277
288 TEST_F(ProfileManagerTest, CreateProfilesAsync) { 278 TEST_F(ProfileManagerTest, CreateProfilesAsync) {
289 FilePath dest_path1 = 279 FilePath dest_path1 =
290 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); 280 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1"));
291 FilePath dest_path2 = 281 FilePath dest_path2 =
292 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); 282 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2"));
293 283
294 MockObserver mock_observer; 284 MockObserver mock_observer;
295 EXPECT_CALL(mock_observer, OnProfileCreated( 285 EXPECT_CALL(mock_observer, OnProfileCreated(
296 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 286 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
297 287
298 ProfileManager* profile_manager = g_browser_process->profile_manager(); 288 profile_manager_->CreateProfileAsync(dest_path1, &mock_observer);
299 289 profile_manager_->CreateProfileAsync(dest_path2, &mock_observer);
300 profile_manager->CreateProfileAsync(dest_path1, &mock_observer);
301 profile_manager->CreateProfileAsync(dest_path2, &mock_observer);
302 290
303 message_loop_.RunAllPending(); 291 message_loop_.RunAllPending();
304 } 292 }
305 293
306 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { 294 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
307 ProfileManager* profile_manager = g_browser_process->profile_manager(); 295 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
308 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
309 296
310 EXPECT_EQ(0u, cache.GetNumberOfProfiles()); 297 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
311 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), 298 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
312 ASCIIToUTF16("name_1"), string16(), 0); 299 ASCIIToUTF16("name_1"), string16(), 0);
313 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), 300 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
314 ASCIIToUTF16("name_2"), string16(), 0); 301 ASCIIToUTF16("name_2"), string16(), 0);
315 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), 302 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
316 ASCIIToUTF16("name_3"), string16(), 0); 303 ASCIIToUTF16("name_3"), string16(), 0);
317 cache.SetBackgroundStatusOfProfileAtIndex(0, true); 304 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
318 cache.SetBackgroundStatusOfProfileAtIndex(2, true); 305 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
319 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); 306 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
320 307
321 profile_manager->AutoloadProfiles(); 308 profile_manager_->AutoloadProfiles();
322 309
323 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 310 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size());
324 } 311 }
325
326 TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
327 FilePath dest_path = temp_dir_.path();
328 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
329
330 ProfileManager* profile_manager = g_browser_process->profile_manager();
331
332 Profile* profile;
333
334 // Successfully create the profile
335 profile = profile_manager->GetProfile(dest_path);
336 ASSERT_TRUE(profile);
337
338 // Check that the profile name is non empty
339 std::string profile_name =
340 profile->GetPrefs()->GetString(prefs::kProfileName);
341 EXPECT_FALSE(profile_name.empty());
342
343 // Check that the profile avatar index is valid
344 size_t avatar_index = profile->GetPrefs()->GetInteger(
345 prefs::kProfileAvatarIndex);
346 EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex(
347 avatar_index));
348 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_manager.cc ('k') | chrome/browser/ui/webui/options/manage_profile_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698