Chromium Code Reviews| 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 <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/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 CommandLine* cl = CommandLine::ForCurrentProcess(); | 113 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 114 cl->AppendSwitch(switches::kTestType); | 114 cl->AppendSwitch(switches::kTestType); |
| 115 #endif | 115 #endif |
| 116 } | 116 } |
| 117 | 117 |
| 118 virtual void TearDown() { | 118 virtual void TearDown() { |
| 119 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); | 119 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
| 120 message_loop_.RunUntilIdle(); | 120 message_loop_.RunUntilIdle(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Helper function to create a profile with |name| for a profile |manager|. | |
| 124 void CreateProfileAsync(ProfileManager* manager, | |
| 125 const std::string& name, | |
| 126 MockObserver* mock_observer) { | |
| 127 manager->CreateProfileAsync( | |
| 128 temp_dir_.path().AppendASCII(name), | |
| 129 base::Bind(&MockObserver::OnProfileCreated, | |
| 130 base::Unretained(mock_observer)), | |
| 131 UTF8ToUTF16(name), | |
|
Alexei Svitkine (slow)
2013/06/22 14:14:09
Nice, I see you're now always setting the profile
noms
2013/06/26 15:24:45
Yes and no. We would still have to set the avatar
| |
| 132 string16(), | |
| 133 false); | |
| 134 } | |
| 135 | |
| 123 #if defined(OS_CHROMEOS) | 136 #if defined(OS_CHROMEOS) |
| 124 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 137 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 125 chromeos::ScopedTestCrosSettings test_cros_settings_; | 138 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 126 #endif | 139 #endif |
| 127 | 140 |
| 128 // The path to temporary directory used to contain the test operations. | 141 // The path to temporary directory used to contain the test operations. |
| 129 base::ScopedTempDir temp_dir_; | 142 base::ScopedTempDir temp_dir_; |
| 130 ScopedTestingLocalState local_state_; | 143 ScopedTestingLocalState local_state_; |
| 131 scoped_refptr<extensions::EventRouterForwarder> | 144 scoped_refptr<extensions::EventRouterForwarder> |
| 132 extension_event_router_forwarder_; | 145 extension_event_router_forwarder_; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 } | 246 } |
| 234 | 247 |
| 235 MATCHER(NotFail, "Profile creation failure status is not reported.") { | 248 MATCHER(NotFail, "Profile creation failure status is not reported.") { |
| 236 return arg == Profile::CREATE_STATUS_CREATED || | 249 return arg == Profile::CREATE_STATUS_CREATED || |
| 237 arg == Profile::CREATE_STATUS_INITIALIZED; | 250 arg == Profile::CREATE_STATUS_INITIALIZED; |
| 238 } | 251 } |
| 239 | 252 |
| 240 // Tests asynchronous profile creation mechanism. | 253 // Tests asynchronous profile creation mechanism. |
| 241 // Crashes: http://crbug.com/89421 | 254 // Crashes: http://crbug.com/89421 |
| 242 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { | 255 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { |
| 243 base::FilePath dest_path = | |
| 244 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); | |
| 245 | |
| 246 MockObserver mock_observer; | 256 MockObserver mock_observer; |
| 247 EXPECT_CALL(mock_observer, OnProfileCreated( | 257 EXPECT_CALL(mock_observer, OnProfileCreated( |
| 248 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); | 258 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 249 | 259 |
| 250 g_browser_process->profile_manager()->CreateProfileAsync(dest_path, | 260 CreateProfileAsync(g_browser_process->profile_manager(), |
| 251 base::Bind(&MockObserver::OnProfileCreated, | 261 "New Profile", &mock_observer); |
| 252 base::Unretained(&mock_observer)), | |
| 253 string16(), string16(), false); | |
| 254 | 262 |
| 255 message_loop_.RunUntilIdle(); | 263 message_loop_.RunUntilIdle(); |
| 256 } | 264 } |
| 257 | 265 |
| 258 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { | 266 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
| 259 if (!g_created_profile) | 267 if (!g_created_profile) |
| 260 g_created_profile = arg; | 268 g_created_profile = arg; |
| 261 return arg != NULL && arg == g_created_profile; | 269 return arg != NULL && arg == g_created_profile; |
| 262 } | 270 } |
| 263 | 271 |
| 264 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { | 272 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
| 265 base::FilePath dest_path = | |
| 266 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); | |
| 267 | |
| 268 g_created_profile = NULL; | 273 g_created_profile = NULL; |
| 269 | 274 |
| 270 MockObserver mock_observer1; | 275 MockObserver mock_observer1; |
| 271 EXPECT_CALL(mock_observer1, OnProfileCreated( | 276 EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 272 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 277 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 273 MockObserver mock_observer2; | 278 MockObserver mock_observer2; |
| 274 EXPECT_CALL(mock_observer2, OnProfileCreated( | 279 EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 275 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 280 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 276 MockObserver mock_observer3; | 281 MockObserver mock_observer3; |
| 277 EXPECT_CALL(mock_observer3, OnProfileCreated( | 282 EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 278 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 283 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 279 | 284 |
| 280 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 285 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 281 | 286 const std::string profile_name = "New Profile"; |
| 282 profile_manager->CreateProfileAsync(dest_path, | 287 CreateProfileAsync(profile_manager, profile_name, &mock_observer1); |
| 283 base::Bind(&MockObserver::OnProfileCreated, | 288 CreateProfileAsync(profile_manager, profile_name, &mock_observer2); |
| 284 base::Unretained(&mock_observer1)), | 289 CreateProfileAsync(profile_manager, profile_name, &mock_observer3); |
| 285 string16(), string16(), false); | |
| 286 profile_manager->CreateProfileAsync(dest_path, | |
| 287 base::Bind(&MockObserver::OnProfileCreated, | |
| 288 base::Unretained(&mock_observer2)), | |
| 289 string16(), string16(), false); | |
| 290 profile_manager->CreateProfileAsync(dest_path, | |
| 291 base::Bind(&MockObserver::OnProfileCreated, | |
| 292 base::Unretained(&mock_observer3)), | |
| 293 string16(), string16(), false); | |
| 294 | 290 |
| 295 message_loop_.RunUntilIdle(); | 291 message_loop_.RunUntilIdle(); |
| 296 } | 292 } |
| 297 | 293 |
| 298 TEST_F(ProfileManagerTest, CreateProfilesAsync) { | 294 TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
| 299 base::FilePath dest_path1 = | 295 const std::string profile_name1 = "New Profile 1"; |
| 300 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | 296 const std::string profile_name2 = "New Profile 2"; |
| 301 base::FilePath dest_path2 = | |
| 302 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | |
| 303 | 297 |
| 304 MockObserver mock_observer; | 298 MockObserver mock_observer; |
| 305 EXPECT_CALL(mock_observer, OnProfileCreated( | 299 EXPECT_CALL(mock_observer, OnProfileCreated( |
| 306 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); | 300 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
| 307 | 301 |
| 308 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 302 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 309 | 303 |
| 310 profile_manager->CreateProfileAsync(dest_path1, | 304 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); |
| 311 base::Bind(&MockObserver::OnProfileCreated, | 305 CreateProfileAsync(profile_manager, profile_name2, &mock_observer); |
| 312 base::Unretained(&mock_observer)), | |
| 313 string16(), string16(), false); | |
| 314 profile_manager->CreateProfileAsync(dest_path2, | |
| 315 base::Bind(&MockObserver::OnProfileCreated, | |
| 316 base::Unretained(&mock_observer)), | |
| 317 string16(), string16(), false); | |
| 318 | 306 |
| 319 message_loop_.RunUntilIdle(); | 307 message_loop_.RunUntilIdle(); |
| 320 } | 308 } |
| 321 | 309 |
| 322 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { | 310 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
| 323 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 311 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 324 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | 312 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 325 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, | 313 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
| 326 Value::CreateBooleanValue(true)); | 314 Value::CreateBooleanValue(true)); |
| 327 | 315 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 EXPECT_FALSE( | 431 EXPECT_FALSE( |
| 444 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); | 432 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 445 | 433 |
| 446 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when | 434 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when |
| 447 // incognito mode is forced. | 435 // incognito mode is forced. |
| 448 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); | 436 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); |
| 449 EXPECT_TRUE( | 437 EXPECT_TRUE( |
| 450 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); | 438 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 451 } | 439 } |
| 452 | 440 |
| 441 TEST_F(ProfileManagerTest, ActiveProfileDeleted) { | |
| 442 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 443 ASSERT_TRUE(profile_manager); | |
| 444 | |
| 445 // Create and load two profiles. | |
| 446 const std::string profile_name1 = "New Profile 1"; | |
| 447 const std::string profile_name2 = "New Profile 2"; | |
| 448 base::FilePath dest_path1 = | |
| 449 temp_dir_.path().AppendASCII(profile_name1); | |
| 450 base::FilePath dest_path2 = | |
| 451 temp_dir_.path().AppendASCII(profile_name2); | |
| 452 | |
| 453 MockObserver mock_observer; | |
| 454 EXPECT_CALL(mock_observer, OnProfileCreated( | |
| 455 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); | |
| 456 | |
| 457 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); | |
| 458 CreateProfileAsync(profile_manager, profile_name2, &mock_observer); | |
| 459 message_loop_.RunUntilIdle(); | |
| 460 | |
| 461 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); | |
| 462 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); | |
| 463 | |
| 464 // Set the active profile. | |
| 465 PrefService* local_state = g_browser_process->local_state(); | |
| 466 local_state->SetString(prefs::kProfileLastUsed, profile_name1); | |
| 467 | |
| 468 // Delete the active profile. | |
| 469 profile_manager->ScheduleProfileForDeletion(dest_path1, | |
| 470 ProfileManager::CreateCallback()); | |
| 471 // Spin the message loop so that all the callbacks can finish running. | |
| 472 message_loop_.RunUntilIdle(); | |
| 473 | |
| 474 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); | |
| 475 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); | |
| 476 } | |
| 477 | |
| 478 #if defined(OS_MACOSX) | |
| 479 // These tests are for a Mac-only code path that assumes the browser | |
| 480 // process isn't killed when all browser windows are closed. | |
| 481 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { | |
| 482 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 483 ASSERT_TRUE(profile_manager); | |
| 484 | |
| 485 // Create and load one profile, and just create a second profile. | |
| 486 const std::string profile_name1 = "New Profile 1"; | |
| 487 const std::string profile_name2 = "New Profile 2"; | |
| 488 base::FilePath dest_path1 = | |
| 489 temp_dir_.path().AppendASCII(profile_name1); | |
| 490 base::FilePath dest_path2 = | |
| 491 temp_dir_.path().AppendASCII(profile_name2); | |
| 492 | |
| 493 MockObserver mock_observer; | |
| 494 EXPECT_CALL(mock_observer, OnProfileCreated( | |
| 495 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); | |
| 496 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); | |
| 497 message_loop_.RunUntilIdle(); | |
| 498 | |
| 499 // Track the profile, but don't load it. | |
| 500 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | |
| 501 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), | |
| 502 string16(), 0, false); | |
| 503 message_loop_.RunUntilIdle(); | |
| 504 | |
| 505 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); | |
| 506 EXPECT_EQ(2u, cache.GetNumberOfProfiles()); | |
| 507 | |
| 508 // Set the active profile. | |
| 509 PrefService* local_state = g_browser_process->local_state(); | |
| 510 local_state->SetString(prefs::kProfileLastUsed, | |
| 511 dest_path1.BaseName().MaybeAsASCII()); | |
| 512 | |
| 513 // Delete the active profile. This should switch and load the unloaded | |
| 514 // profile. | |
| 515 profile_manager->ScheduleProfileForDeletion(dest_path1, | |
| 516 ProfileManager::CreateCallback()); | |
| 517 | |
| 518 // Spin the message loop so that all the callbacks can finish running. | |
| 519 message_loop_.RunUntilIdle(); | |
| 520 | |
| 521 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); | |
| 522 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); | |
| 523 } | |
| 524 | |
| 525 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded | |
| 526 // by simulating a scenario in which the profile that is being loaded as | |
| 527 // the next active profile has also been marked for deletion, so the | |
| 528 // ProfileManager needs to recursively select a different next profile. | |
| 529 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { | |
| 530 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 531 ASSERT_TRUE(profile_manager); | |
| 532 | |
| 533 // Create and load one profile, and create two more profiles. | |
| 534 const std::string profile_name1 = "New Profile 1"; | |
| 535 const std::string profile_name2 = "New Profile 2"; | |
| 536 const std::string profile_name3 = "New Profile 3"; | |
| 537 base::FilePath dest_path1 = | |
| 538 temp_dir_.path().AppendASCII(profile_name1); | |
| 539 base::FilePath dest_path2 = | |
| 540 temp_dir_.path().AppendASCII(profile_name2); | |
| 541 base::FilePath dest_path3 = | |
| 542 temp_dir_.path().AppendASCII(profile_name3); | |
| 543 | |
| 544 MockObserver mock_observer; | |
| 545 EXPECT_CALL(mock_observer, OnProfileCreated( | |
| 546 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); | |
| 547 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); | |
| 548 message_loop_.RunUntilIdle(); | |
| 549 | |
| 550 // Create the other profiles, but don't load them. Assign a fake avatar icon | |
| 551 // to ensure that profiles in the info cache are sorted by the profile name, | |
| 552 // and not randomly by the avatar name. | |
| 553 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | |
| 554 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), | |
| 555 ASCIIToUTF16(profile_name2), 1, false); | |
| 556 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3), | |
| 557 ASCIIToUTF16(profile_name3), 2, false); | |
| 558 | |
| 559 message_loop_.RunUntilIdle(); | |
| 560 | |
| 561 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); | |
| 562 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); | |
| 563 | |
| 564 // Set the active profile. | |
| 565 PrefService* local_state = g_browser_process->local_state(); | |
| 566 local_state->SetString(prefs::kProfileLastUsed, | |
| 567 dest_path1.BaseName().MaybeAsASCII()); | |
| 568 | |
| 569 // Delete the active profile, Profile1. | |
| 570 // This will post a CreateProfileAsync message, that tries to load Profile2, | |
| 571 // which checks that the profile is not being deleted, and then calls back | |
| 572 // FinishDeletingProfile for Profile1. | |
| 573 // Try to break this flow by setting the active profile to Profile2 in the | |
| 574 // middle (so after the first posted message), and trying to delete Profile2, | |
| 575 // so that the ProfileManager has to look for a different profile to load. | |
| 576 profile_manager->ScheduleProfileForDeletion(dest_path1, | |
| 577 ProfileManager::CreateCallback()); | |
| 578 local_state->SetString(prefs::kProfileLastUsed, | |
| 579 dest_path2.BaseName().MaybeAsASCII()); | |
| 580 profile_manager->ScheduleProfileForDeletion(dest_path2, | |
| 581 ProfileManager::CreateCallback()); | |
| 582 // Spin the message loop so that all the callbacks can finish running. | |
| 583 message_loop_.RunUntilIdle(); | |
| 584 | |
| 585 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); | |
| 586 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); | |
| 587 } | |
| 588 #endif // !defined(OS_MACOSX) | |
| 589 | |
| 453 #if !defined(OS_ANDROID) | 590 #if !defined(OS_ANDROID) |
| 454 // There's no Browser object on Android. | 591 // There's no Browser object on Android. |
| 455 TEST_F(ProfileManagerTest, LastOpenedProfiles) { | 592 TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
| 456 base::FilePath dest_path1 = temp_dir_.path(); | 593 base::FilePath dest_path1 = temp_dir_.path(); |
| 457 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 594 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 458 | 595 |
| 459 base::FilePath dest_path2 = temp_dir_.path(); | 596 base::FilePath dest_path2 = temp_dir_.path(); |
| 460 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 597 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 461 | 598 |
| 462 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 599 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 browser2b.reset(); | 770 browser2b.reset(); |
| 634 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 771 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 635 ASSERT_EQ(1U, last_opened_profiles.size()); | 772 ASSERT_EQ(1U, last_opened_profiles.size()); |
| 636 EXPECT_EQ(profile1, last_opened_profiles[0]); | 773 EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 637 | 774 |
| 638 browser1.reset(); | 775 browser1.reset(); |
| 639 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 776 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 640 ASSERT_EQ(0U, last_opened_profiles.size()); | 777 ASSERT_EQ(0U, last_opened_profiles.size()); |
| 641 } | 778 } |
| 642 #endif // !defined(OS_ANDROID) | 779 #endif // !defined(OS_ANDROID) |
| OLD | NEW |