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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/base_switches.h" | |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 #include "chrome/browser/password_manager/password_store_factory.h" | 13 #include "chrome/browser/password_manager/password_store_factory.h" |
13 #include "chrome/browser/profiles/profile_attributes_entry.h" | 14 #include "chrome/browser/profiles/profile_attributes_entry.h" |
14 #include "chrome/browser/profiles/profile_attributes_storage.h" | 15 #include "chrome/browser/profiles/profile_attributes_storage.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/browser/profiles/profile_window.h" | 17 #include "chrome/browser/profiles/profile_window.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
29 | 30 |
30 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
31 #include "base/path_service.h" | 32 #include "base/path_service.h" |
32 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 33 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
33 #include "chrome/common/chrome_constants.h" | 34 #include "chrome/common/chrome_constants.h" |
34 #include "chrome/common/chrome_paths.h" | 35 #include "chrome/common/chrome_paths.h" |
35 #include "chromeos/chromeos_switches.h" | 36 #include "chromeos/chromeos_switches.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
37 #endif | 38 #endif |
38 | 39 |
40 #if defined(OS_WIN) | |
41 #include <windows.h> | |
42 #include "base/test/test_file_util.h" | |
43 #endif | |
44 | |
39 namespace { | 45 namespace { |
40 | 46 |
41 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing; | 47 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing; |
42 | 48 |
43 // An observer that returns back to test code after a new profile is | 49 // A callback that returns back to test code, and check if the result is |
44 // initialized. | 50 // expected. |
Peter Kasting
2016/06/16 07:02:11
Nit: Not grammatical, and mostly restates the code
| |
45 void OnUnblockOnProfileCreation(base::RunLoop* run_loop, | 51 void UnblockOnProfileCreation(Profile::CreateStatus expected_final_status, |
52 base::RunLoop* run_loop, | |
53 Profile* profile, | |
54 Profile::CreateStatus status) { | |
55 if (status != Profile::CREATE_STATUS_CREATED) { | |
56 EXPECT_EQ(expected_final_status, status); | |
57 run_loop->Quit(); | |
58 } | |
59 } | |
60 | |
61 // A callback that returns back to test code after a new profile is initialized. | |
62 void UnblockOnProfileInitialized(base::RunLoop* run_loop, | |
46 Profile* profile, | 63 Profile* profile, |
47 Profile::CreateStatus status) { | 64 Profile::CreateStatus status) { |
48 if (status == Profile::CREATE_STATUS_INITIALIZED) | 65 UnblockOnProfileCreation(Profile::CREATE_STATUS_INITIALIZED, run_loop, |
49 run_loop->Quit(); | 66 profile, status); |
50 } | 67 } |
51 | 68 |
52 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { | 69 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { |
53 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); | 70 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); |
54 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); | 71 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); |
55 // No browser should have been created for this profile yet. | 72 // No browser should have been created for this profile yet. |
56 EXPECT_EQ(chrome::GetBrowserCount(profile), 0U); | 73 EXPECT_EQ(chrome::GetBrowserCount(profile), 0U); |
57 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); | 74 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); |
58 if (status == Profile::CREATE_STATUS_INITIALIZED) | 75 if (status == Profile::CREATE_STATUS_INITIALIZED) |
59 base::MessageLoop::current()->QuitWhenIdle(); | 76 base::MessageLoop::current()->QuitWhenIdle(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 158 |
142 // TODO(jeremy): crbug.com/103355 - These tests should be enabled on all | 159 // TODO(jeremy): crbug.com/103355 - These tests should be enabled on all |
143 // platforms. | 160 // platforms. |
144 class ProfileManagerBrowserTest : public InProcessBrowserTest { | 161 class ProfileManagerBrowserTest : public InProcessBrowserTest { |
145 protected: | 162 protected: |
146 void SetUpCommandLine(base::CommandLine* command_line) override { | 163 void SetUpCommandLine(base::CommandLine* command_line) override { |
147 #if defined(OS_CHROMEOS) | 164 #if defined(OS_CHROMEOS) |
148 command_line->AppendSwitch( | 165 command_line->AppendSwitch( |
149 chromeos::switches::kIgnoreUserProfileMappingForTests); | 166 chromeos::switches::kIgnoreUserProfileMappingForTests); |
150 #endif | 167 #endif |
168 command_line->AppendSwitch(switches::kNoErrorDialogs); | |
151 } | 169 } |
152 }; | 170 }; |
153 | 171 |
154 #if defined(OS_MACOSX) | 172 #if defined(OS_MACOSX) |
155 | 173 |
156 // Delete single profile and make sure a new one is created. | 174 // Delete single profile and make sure a new one is created. |
157 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteSingletonProfile) { | 175 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteSingletonProfile) { |
158 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 176 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
159 ProfileAttributesStorage& storage = | 177 ProfileAttributesStorage& storage = |
160 profile_manager->GetProfileAttributesStorage(); | 178 profile_manager->GetProfileAttributesStorage(); |
161 ProfileRemovalObserver observer; | 179 ProfileRemovalObserver observer; |
162 | 180 |
163 // We should start out with 1 profile. | 181 // We should start out with 1 profile. |
164 ASSERT_EQ(1u, storage.GetNumberOfProfiles()); | 182 ASSERT_EQ(1u, storage.GetNumberOfProfiles()); |
165 | 183 |
166 // Delete singleton profile. | 184 // Delete singleton profile. |
167 base::FilePath singleton_profile_path = | 185 base::FilePath singleton_profile_path = |
168 storage.GetAllProfilesAttributes().front()->GetPath(); | 186 storage.GetAllProfilesAttributes().front()->GetPath(); |
169 EXPECT_FALSE(singleton_profile_path.empty()); | 187 EXPECT_FALSE(singleton_profile_path.empty()); |
170 base::RunLoop run_loop; | 188 base::RunLoop run_loop; |
171 profile_manager->ScheduleProfileForDeletion( | 189 profile_manager->ScheduleProfileForDeletion( |
172 singleton_profile_path, | 190 singleton_profile_path, |
173 base::Bind(&OnUnblockOnProfileCreation, &run_loop)); | 191 base::Bind(&UnblockOnProfileInitialized, &run_loop)); |
174 | 192 |
175 // Run the message loop until the profile is actually deleted (as indicated | 193 // Run the message loop until the profile is actually deleted (as indicated |
176 // by the callback above being called). | 194 // by the callback above being called). |
177 run_loop.Run(); | 195 run_loop.Run(); |
178 | 196 |
179 // Make sure a new profile was created automatically. | 197 // Make sure a new profile was created automatically. |
180 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | 198 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
181 base::FilePath new_profile_path = | 199 base::FilePath new_profile_path = |
182 storage.GetAllProfilesAttributes().front()->GetPath(); | 200 storage.GetAllProfilesAttributes().front()->GetPath(); |
183 EXPECT_NE(new_profile_path.value(), singleton_profile_path.value()); | 201 EXPECT_NE(new_profile_path.value(), singleton_profile_path.value()); |
(...skipping 14 matching lines...) Expand all Loading... | |
198 // Crashes/CHECKs. See crbug.com/104851 | 216 // Crashes/CHECKs. See crbug.com/104851 |
199 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) { | 217 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) { |
200 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 218 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
201 ProfileAttributesStorage& storage = | 219 ProfileAttributesStorage& storage = |
202 profile_manager->GetProfileAttributesStorage(); | 220 profile_manager->GetProfileAttributesStorage(); |
203 | 221 |
204 // Create an additional profile. | 222 // Create an additional profile. |
205 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); | 223 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); |
206 base::RunLoop run_loop; | 224 base::RunLoop run_loop; |
207 profile_manager->CreateProfileAsync( | 225 profile_manager->CreateProfileAsync( |
208 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop), | 226 new_path, base::Bind(&UnblockOnProfileInitialized, &run_loop), |
209 base::string16(), std::string(), std::string()); | 227 base::string16(), std::string(), std::string()); |
210 | 228 |
211 // Run the message loop to allow profile creation to take place; the loop is | 229 // Run the message loop to allow profile creation to take place; the loop is |
212 // terminated by OnUnblockOnProfileCreation when the profile is created. | 230 // terminated by UnblockOnProfileInitialized when the profile is created. |
213 run_loop.Run(); | 231 run_loop.Run(); |
214 | 232 |
215 ASSERT_EQ(2u, storage.GetNumberOfProfiles()); | 233 ASSERT_EQ(2u, storage.GetNumberOfProfiles()); |
216 | 234 |
217 // Delete all profiles. | 235 // Delete all profiles. |
218 std::vector<ProfileAttributesEntry*> entries = | 236 std::vector<ProfileAttributesEntry*> entries = |
219 storage.GetAllProfilesAttributes(); | 237 storage.GetAllProfilesAttributes(); |
220 std::vector<base::FilePath> old_profile_paths; | 238 std::vector<base::FilePath> old_profile_paths; |
221 for (ProfileAttributesEntry* entry : entries) { | 239 for (ProfileAttributesEntry* entry : entries) { |
222 base::FilePath profile_path = entry->GetPath(); | 240 base::FilePath profile_path = entry->GetPath(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U); | 308 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U); |
291 | 309 |
292 // Now close all browser windows. | 310 // Now close all browser windows. |
293 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | 311 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); |
294 for (std::vector<Profile*>::const_iterator it = profiles.begin(); | 312 for (std::vector<Profile*>::const_iterator it = profiles.begin(); |
295 it != profiles.end(); ++it) { | 313 it != profiles.end(); ++it) { |
296 BrowserList::CloseAllBrowsersWithProfile(*it); | 314 BrowserList::CloseAllBrowsersWithProfile(*it); |
297 } | 315 } |
298 } | 316 } |
299 | 317 |
300 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, | 318 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, SwitchToProfile) { |
301 SwitchToProfile) { | |
302 // If multiprofile mode is not enabled, you can't switch between profiles. | 319 // If multiprofile mode is not enabled, you can't switch between profiles. |
303 if (!profiles::IsMultipleProfilesEnabled()) | 320 if (!profiles::IsMultipleProfilesEnabled()) |
304 return; | 321 return; |
305 | 322 |
306 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 323 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
307 ProfileAttributesStorage& storage = | 324 ProfileAttributesStorage& storage = |
308 profile_manager->GetProfileAttributesStorage(); | 325 profile_manager->GetProfileAttributesStorage(); |
309 size_t initial_profile_count = profile_manager->GetNumberOfProfiles(); | 326 size_t initial_profile_count = profile_manager->GetNumberOfProfiles(); |
310 base::FilePath path_profile1 = GetFirstNonSigninProfile(storage); | 327 base::FilePath path_profile1 = GetFirstNonSigninProfile(storage); |
311 | 328 |
312 ASSERT_NE(0U, initial_profile_count); | 329 ASSERT_NE(0U, initial_profile_count); |
313 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); | 330 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); |
314 | 331 |
315 // Create an additional profile. | 332 // Create an additional profile. |
316 base::FilePath path_profile2 = | 333 base::FilePath path_profile2 = |
317 profile_manager->GenerateNextProfileDirectoryPath(); | 334 profile_manager->GenerateNextProfileDirectoryPath(); |
318 base::RunLoop run_loop; | 335 base::RunLoop run_loop; |
319 profile_manager->CreateProfileAsync( | 336 profile_manager->CreateProfileAsync( |
320 path_profile2, base::Bind(&OnUnblockOnProfileCreation, &run_loop), | 337 path_profile2, base::Bind(&UnblockOnProfileInitialized, &run_loop), |
321 base::string16(), std::string(), std::string()); | 338 base::string16(), std::string(), std::string()); |
322 | 339 |
323 // Run the message loop to allow profile creation to take place; the loop is | 340 // Run the message loop to allow profile creation to take place; the loop is |
324 // terminated by OnUnblockOnProfileCreation when the profile is created. | 341 // terminated by UnblockOnProfileInitialized when the profile is created. |
325 run_loop.Run(); | 342 run_loop.Run(); |
326 | 343 |
327 BrowserList* browser_list = BrowserList::GetInstance(); | 344 BrowserList* browser_list = BrowserList::GetInstance(); |
328 ASSERT_EQ(initial_profile_count + 1U, storage.GetNumberOfProfiles()); | 345 ASSERT_EQ(initial_profile_count + 1U, storage.GetNumberOfProfiles()); |
329 EXPECT_EQ(1U, browser_list->size()); | 346 EXPECT_EQ(1U, browser_list->size()); |
330 | 347 |
331 // Open a browser window for the first profile. | 348 // Open a browser window for the first profile. |
332 profiles::SwitchToProfile(path_profile1, false, kOnProfileSwitchDoNothing, | 349 profiles::SwitchToProfile(path_profile1, false, kOnProfileSwitchDoNothing, |
333 ProfileMetrics::SWITCH_PROFILE_ICON); | 350 ProfileMetrics::SWITCH_PROFILE_ICON); |
334 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); | 351 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); |
(...skipping 10 matching lines...) Expand all Loading... | |
345 // Switch to the first profile without opening a new window. | 362 // Switch to the first profile without opening a new window. |
346 profiles::SwitchToProfile(path_profile1, false, kOnProfileSwitchDoNothing, | 363 profiles::SwitchToProfile(path_profile1, false, kOnProfileSwitchDoNothing, |
347 ProfileMetrics::SWITCH_PROFILE_ICON); | 364 ProfileMetrics::SWITCH_PROFILE_ICON); |
348 EXPECT_EQ(2U, chrome::GetTotalBrowserCount()); | 365 EXPECT_EQ(2U, chrome::GetTotalBrowserCount()); |
349 EXPECT_EQ(2U, browser_list->size()); | 366 EXPECT_EQ(2U, browser_list->size()); |
350 | 367 |
351 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath()); | 368 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath()); |
352 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); | 369 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); |
353 } | 370 } |
354 | 371 |
372 #if defined(OS_WIN) | |
373 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, CreateProfileAsyncFail) { | |
374 base::FilePath user_data_dir = | |
375 g_browser_process->profile_manager()->user_data_dir(); | |
376 base::DenyFilePermission(user_data_dir, FILE_ADD_SUBDIRECTORY); | |
377 // Switch to a profile that cannot be loaded or created. This must fail | |
378 // gracefully with no crash. | |
379 base::RunLoop run_loop; | |
380 g_browser_process->profile_manager()->CreateProfileAsync( | |
381 user_data_dir.AppendASCII("Profile 1"), | |
382 base::Bind(&UnblockOnProfileCreation, | |
383 Profile::CREATE_STATUS_LOCAL_FAIL, // expected result | |
384 &run_loop), | |
385 base::string16(), | |
Peter Kasting
2016/06/16 07:02:11
Nit: Could combine all these on one line, but what
| |
386 std::string(), | |
387 std::string()); | |
388 run_loop.Run(); | |
389 | |
390 base::RunLoop run_loop2; | |
391 run_loop2.RunUntilIdle(); | |
Peter Kasting
2016/06/16 07:02:11
What is this doing?
WC Leung
2016/06/16 10:04:10
To wait for async initialization of profile is com
Peter Kasting
2016/06/16 10:59:59
Isn't that what the first run loop does?
| |
392 } | |
393 #endif // defined(OS_WIN) | |
394 | |
355 // Flakes on Windows: http://crbug.com/314905 | 395 // Flakes on Windows: http://crbug.com/314905 |
356 #if defined(OS_WIN) | 396 #if defined(OS_WIN) |
357 #define MAYBE_EphemeralProfile DISABLED_EphemeralProfile | 397 #define MAYBE_EphemeralProfile DISABLED_EphemeralProfile |
358 #else | 398 #else |
359 #define MAYBE_EphemeralProfile EphemeralProfile | 399 #define MAYBE_EphemeralProfile EphemeralProfile |
360 #endif | 400 #endif |
361 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, MAYBE_EphemeralProfile) { | 401 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, MAYBE_EphemeralProfile) { |
362 // If multiprofile mode is not enabled, you can't switch between profiles. | 402 // If multiprofile mode is not enabled, you can't switch between profiles. |
363 if (!profiles::IsMultipleProfilesEnabled()) | 403 if (!profiles::IsMultipleProfilesEnabled()) |
364 return; | 404 return; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 | 478 |
439 password_store->AddLogin(form); | 479 password_store->AddLogin(form); |
440 PasswordStoreConsumerVerifier verify_add; | 480 PasswordStoreConsumerVerifier verify_add; |
441 password_store->GetAutofillableLogins(&verify_add); | 481 password_store->GetAutofillableLogins(&verify_add); |
442 verify_add.Wait(); | 482 verify_add.Wait(); |
443 EXPECT_EQ(1u, verify_add.GetPasswords().size()); | 483 EXPECT_EQ(1u, verify_add.GetPasswords().size()); |
444 | 484 |
445 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 485 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
446 base::RunLoop run_loop; | 486 base::RunLoop run_loop; |
447 profile_manager->ScheduleProfileForDeletion( | 487 profile_manager->ScheduleProfileForDeletion( |
448 profile->GetPath(), base::Bind(&OnUnblockOnProfileCreation, &run_loop)); | 488 profile->GetPath(), base::Bind(&UnblockOnProfileInitialized, &run_loop)); |
449 run_loop.Run(); | 489 run_loop.Run(); |
450 | 490 |
451 PasswordStoreConsumerVerifier verify_delete; | 491 PasswordStoreConsumerVerifier verify_delete; |
452 password_store->GetAutofillableLogins(&verify_delete); | 492 password_store->GetAutofillableLogins(&verify_delete); |
453 verify_delete.Wait(); | 493 verify_delete.Wait(); |
454 EXPECT_EQ(0u, verify_delete.GetPasswords().size()); | 494 EXPECT_EQ(0u, verify_delete.GetPasswords().size()); |
455 } | 495 } |
456 #endif // !defined(OS_WIN) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | 496 #endif // !defined(OS_WIN) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
457 | 497 |
458 // Tests Profile::HasOffTheRecordProfile, Profile::IsValidProfile and the | 498 // Tests Profile::HasOffTheRecordProfile, Profile::IsValidProfile and the |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 | 532 |
493 EXPECT_FALSE(profile->HasOffTheRecordProfile()); | 533 EXPECT_FALSE(profile->HasOffTheRecordProfile()); |
494 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile)); | 534 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile)); |
495 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles()); | 535 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles()); |
496 // After destroying the incognito profile incognito preferences should be | 536 // After destroying the incognito profile incognito preferences should be |
497 // cleared so the default save path should be taken from the main profile. | 537 // cleared so the default save path should be taken from the main profile. |
498 EXPECT_FALSE(profile->GetOffTheRecordPrefs() | 538 EXPECT_FALSE(profile->GetOffTheRecordPrefs() |
499 ->GetFilePath(prefs::kSaveFileDefaultDirectory) | 539 ->GetFilePath(prefs::kSaveFileDefaultDirectory) |
500 .empty()); | 540 .empty()); |
501 } | 541 } |
OLD | NEW |