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 3 // found in the LICENSE file. 4 5 #include <string> 6 7 #include "base/command_line.h" 8 #include "base/file_util.h" 9 #include "base/files/scoped_temp_dir.h" 10 #include "base/path_service.h" 11 #include "base/run_loop.h" 12 #include "base/strings/utf_string_conversions.h" 13 #include "base/values.h" 14 #include "build/build_config.h" 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/chromeos/settings/cros_settings.h" 19 #include "chrome/browser/history/history_service.h" 20 #include "chrome/browser/history/history_service_factory.h" 21 #include "chrome/browser/io_thread.h" 22 #include "chrome/browser/prefs/browser_prefs.h" 23 #include "chrome/browser/prefs/incognito_mode_prefs.h" 24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile_info_cache.h" 26 #include "chrome/browser/profiles/profile_manager.h" 27 #include "chrome/browser/ui/browser.h" 28 #include "chrome/common/chrome_constants.h" 29 #include "chrome/common/chrome_paths.h" 30 #include "chrome/common/chrome_switches.h" 31 #include "chrome/common/pref_names.h" 32 #include "chrome/test/base/scoped_testing_local_state.h" 33 #include "chrome/test/base/test_browser_window.h" 34 #include "chrome/test/base/testing_browser_process.h" 35 #include "chrome/test/base/testing_profile.h" 36 #include "content/public/browser/notification_service.h" 37 #include "content/public/test/test_browser_thread_bundle.h" 38 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gtest/include/gtest/gtest.h" 40 41 #if defined(OS_CHROMEOS) 42 #include "chrome/browser/chromeos/login/mock_user_manager.h" 43 #include "chrome/browser/chromeos/login/user_manager.h" 44 #include "chrome/browser/chromeos/settings/cros_settings.h" 45 #include "chrome/browser/chromeos/settings/device_settings_service.h" 46 #include "chromeos/chromeos_switches.h" 47 #endif 48 49 using content::BrowserThread; 50 51 namespace { 52 53 // This global variable is used to check that value returned to different 54 // observers is the same. 55 Profile* g_created_profile; 56 57 } // namespace 58 59 namespace testing { 60 61 class ProfileManager : public ::ProfileManagerWithoutInit { 62 public: 63 explicit ProfileManager(const base::FilePath& user_data_dir) 64 : ::ProfileManagerWithoutInit(user_data_dir) {} 65 66 protected: 67 virtual Profile* CreateProfileHelper( 68 const base::FilePath& file_path) OVERRIDE { 69 if (!base::PathExists(file_path)) { 70 if (!file_util::CreateDirectory(file_path)) 71 return NULL; 72 } 73 return new TestingProfile(file_path, NULL); 74 } 75 76 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, 77 Delegate* delegate) OVERRIDE { 78 // This is safe while all file operations are done on the FILE thread. 79 BrowserThread::PostTask( 80 BrowserThread::FILE, FROM_HERE, 81 base::Bind(base::IgnoreResult(&file_util::CreateDirectory), path)); 82 83 return new TestingProfile(path, this); 84 } 85 }; 86 87 } // namespace testing 88 89 class ProfileManagerTest : public testing::Test { 90 protected: 91 class MockObserver { 92 public: 93 MOCK_METHOD2(OnProfileCreated, 94 void(Profile* profile, Profile::CreateStatus status)); 95 }; 96 97 ProfileManagerTest() 98 : local_state_(TestingBrowserProcess::GetGlobal()) { 99 } 100 101 virtual void SetUp() { 102 // Create a new temporary directory, and store the path 103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 104 TestingBrowserProcess::GetGlobal()->SetProfileManager( 105 new testing::ProfileManager(temp_dir_.path())); 106 107 #if defined(OS_CHROMEOS) 108 CommandLine* cl = CommandLine::ForCurrentProcess(); 109 cl->AppendSwitch(switches::kTestType); 110 #endif 111 } 112 113 virtual void TearDown() { 114 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); 115 base::RunLoop().RunUntilIdle(); 116 } 117 118 // Helper function to create a profile with |name| for a profile |manager|. 119 void CreateProfileAsync(ProfileManager* manager, 120 const std::string& name, 121 MockObserver* mock_observer) { 122 manager->CreateProfileAsync( 123 temp_dir_.path().AppendASCII(name), 124 base::Bind(&MockObserver::OnProfileCreated, 125 base::Unretained(mock_observer)), 126 UTF8ToUTF16(name), 127 string16(), 128 std::string()); 129 } 130 131 #if defined(OS_CHROMEOS) 132 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 133 chromeos::ScopedTestCrosSettings test_cros_settings_; 134 #endif 135 136 // The path to temporary directory used to contain the test operations. 137 base::ScopedTempDir temp_dir_; 138 ScopedTestingLocalState local_state_; 139 140 content::TestBrowserThreadBundle thread_bundle_; 141 142 #if defined(OS_CHROMEOS) 143 chromeos::ScopedTestUserManager test_user_manager_; 144 #endif 145 }; 146 147 TEST_F(ProfileManagerTest, GetProfile) { 148 base::FilePath dest_path = temp_dir_.path(); 149 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 150 151 ProfileManager* profile_manager = g_browser_process->profile_manager(); 152 153 // Successfully create a profile. 154 Profile* profile = profile_manager->GetProfile(dest_path); 155 EXPECT_TRUE(profile); 156 157 // The profile already exists when we call GetProfile. Just load it. 158 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); 159 } 160 161 TEST_F(ProfileManagerTest, DefaultProfileDir) { 162 base::FilePath expected_default = 163 base::FilePath().AppendASCII(chrome::kInitialProfile); 164 EXPECT_EQ( 165 expected_default.value(), 166 g_browser_process->profile_manager()->GetInitialProfileDir().value()); 167 } 168 169 #if defined(OS_CHROMEOS) 170 // This functionality only exists on Chrome OS. 171 TEST_F(ProfileManagerTest, LoggedInProfileDir) { 172 CommandLine *cl = CommandLine::ForCurrentProcess(); 173 std::string profile_dir("my_user"); 174 175 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir); 176 177 base::FilePath expected_default = 178 base::FilePath().AppendASCII(chrome::kInitialProfile); 179 ProfileManager* profile_manager = g_browser_process->profile_manager(); 180 EXPECT_EQ(expected_default.value(), 181 profile_manager->GetInitialProfileDir().value()); 182 183 scoped_ptr<chromeos::MockUserManager> mock_user_manager; 184 mock_user_manager.reset(new chromeos::MockUserManager()); 185 mock_user_manager->SetActiveUser("user (at) gmail.com"); 186 chromeos::User* active_user = mock_user_manager->GetActiveUser(); 187 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, 188 content::NotificationService::AllSources(), 189 content::Details<const chromeos::User>(active_user)); 190 base::FilePath expected_logged_in(profile_dir); 191 EXPECT_EQ(expected_logged_in.value(), 192 profile_manager->GetInitialProfileDir().value()); 193 VLOG(1) << temp_dir_.path().Append( 194 profile_manager->GetInitialProfileDir()).value(); 195 } 196 197 #endif 198 199 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { 200 base::FilePath dest_path1 = temp_dir_.path(); 201 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 202 203 base::FilePath dest_path2 = temp_dir_.path(); 204 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 205 206 ProfileManager* profile_manager = g_browser_process->profile_manager(); 207 208 // Successfully create the profiles. 209 TestingProfile* profile1 = 210 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); 211 ASSERT_TRUE(profile1); 212 213 TestingProfile* profile2 = 214 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); 215 ASSERT_TRUE(profile2); 216 217 // Force lazy-init of some profile services to simulate use. 218 ASSERT_TRUE(profile1->CreateHistoryService(true, false)); 219 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1, 220 Profile::EXPLICIT_ACCESS)); 221 profile1->CreateBookmarkModel(true); 222 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1)); 223 profile2->CreateBookmarkModel(true); 224 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2)); 225 ASSERT_TRUE(profile2->CreateHistoryService(true, false)); 226 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2, 227 Profile::EXPLICIT_ACCESS)); 228 229 // Make sure any pending tasks run before we destroy the profiles. 230 base::RunLoop().RunUntilIdle(); 231 232 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); 233 234 // Make sure history cleans up correctly. 235 base::RunLoop().RunUntilIdle(); 236 } 237 238 MATCHER(NotFail, "Profile creation failure status is not reported.") { 239 return arg == Profile::CREATE_STATUS_CREATED || 240 arg == Profile::CREATE_STATUS_INITIALIZED; 241 } 242 243 // Tests asynchronous profile creation mechanism. 244 // Crashes: http://crbug.com/89421 245 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { 246 MockObserver mock_observer; 247 EXPECT_CALL(mock_observer, OnProfileCreated( 248 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); 249 250 CreateProfileAsync(g_browser_process->profile_manager(), 251 "New Profile", &mock_observer); 252 253 base::RunLoop().RunUntilIdle(); 254 } 255 256 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { 257 if (!g_created_profile) 258 g_created_profile = arg; 259 return arg != NULL && arg == g_created_profile; 260 } 261 262 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { 263 g_created_profile = NULL; 264 265 MockObserver mock_observer1; 266 EXPECT_CALL(mock_observer1, OnProfileCreated( 267 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 268 MockObserver mock_observer2; 269 EXPECT_CALL(mock_observer2, OnProfileCreated( 270 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 271 MockObserver mock_observer3; 272 EXPECT_CALL(mock_observer3, OnProfileCreated( 273 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 274 275 ProfileManager* profile_manager = g_browser_process->profile_manager(); 276 const std::string profile_name = "New Profile"; 277 CreateProfileAsync(profile_manager, profile_name, &mock_observer1); 278 CreateProfileAsync(profile_manager, profile_name, &mock_observer2); 279 CreateProfileAsync(profile_manager, profile_name, &mock_observer3); 280 281 base::RunLoop().RunUntilIdle(); 282 } 283 284 TEST_F(ProfileManagerTest, CreateProfilesAsync) { 285 const std::string profile_name1 = "New Profile 1"; 286 const std::string profile_name2 = "New Profile 2"; 287 288 MockObserver mock_observer; 289 EXPECT_CALL(mock_observer, OnProfileCreated( 290 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 291 292 ProfileManager* profile_manager = g_browser_process->profile_manager(); 293 294 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); 295 CreateProfileAsync(profile_manager, profile_name2, &mock_observer); 296 297 base::RunLoop().RunUntilIdle(); 298 } 299 300 TEST_F(ProfileManagerTest, GetGuestProfilePath) { 301 base::FilePath guest_path = ProfileManager::GetGuestProfilePath(); 302 base::FilePath expected_path = temp_dir_.path(); 303 expected_path = expected_path.Append(chrome::kGuestProfileDir); 304 EXPECT_EQ(expected_path, guest_path); 305 } 306 307 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { 308 ProfileManager* profile_manager = g_browser_process->profile_manager(); 309 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 310 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, 311 Value::CreateBooleanValue(true)); 312 313 // Setting a pref which is not applicable to a system (i.e., Android in this 314 // case) does not necessarily create it. Don't bother continuing with the 315 // test if this pref doesn't exist because it will not load the profiles if 316 // it cannot verify that the pref for background mode is enabled. 317 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled)) 318 return; 319 320 EXPECT_EQ(0u, cache.GetNumberOfProfiles()); 321 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), 322 ASCIIToUTF16("name_1"), string16(), 0, std::string()); 323 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), 324 ASCIIToUTF16("name_2"), string16(), 0, std::string()); 325 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), 326 ASCIIToUTF16("name_3"), string16(), 0, std::string()); 327 cache.SetBackgroundStatusOfProfileAtIndex(0, true); 328 cache.SetBackgroundStatusOfProfileAtIndex(2, true); 329 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); 330 331 profile_manager->AutoloadProfiles(); 332 333 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 334 } 335 336 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) { 337 ProfileManager* profile_manager = g_browser_process->profile_manager(); 338 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 339 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, 340 Value::CreateBooleanValue(false)); 341 342 EXPECT_EQ(0u, cache.GetNumberOfProfiles()); 343 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), 344 ASCIIToUTF16("name_1"), string16(), 0, std::string()); 345 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), 346 ASCIIToUTF16("name_2"), string16(), 0, std::string()); 347 cache.SetBackgroundStatusOfProfileAtIndex(0, false); 348 cache.SetBackgroundStatusOfProfileAtIndex(1, true); 349 EXPECT_EQ(2u, cache.GetNumberOfProfiles()); 350 351 profile_manager->AutoloadProfiles(); 352 353 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size()); 354 } 355 356 TEST_F(ProfileManagerTest, InitProfileUserPrefs) { 357 base::FilePath dest_path = temp_dir_.path(); 358 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 359 360 ProfileManager* profile_manager = g_browser_process->profile_manager(); 361 362 Profile* profile; 363 364 // Successfully create the profile 365 profile = profile_manager->GetProfile(dest_path); 366 ASSERT_TRUE(profile); 367 368 // Check that the profile name is non empty 369 std::string profile_name = 370 profile->GetPrefs()->GetString(prefs::kProfileName); 371 EXPECT_FALSE(profile_name.empty()); 372 373 // Check that the profile avatar index is valid 374 size_t avatar_index = 375 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); 376 EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex( 377 avatar_index)); 378 } 379 380 // Tests that a new profile's entry in the profile info cache is setup with the 381 // same values that are in the profile prefs. 382 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { 383 base::FilePath dest_path = temp_dir_.path(); 384 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 385 386 ProfileManager* profile_manager = g_browser_process->profile_manager(); 387 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 388 389 // Successfully create the profile 390 Profile* profile = profile_manager->GetProfile(dest_path); 391 ASSERT_TRUE(profile); 392 393 std::string profile_name = 394 profile->GetPrefs()->GetString(prefs::kProfileName); 395 size_t avatar_index = 396 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); 397 398 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path); 399 400 // Check if the profile prefs are the same as the cache prefs 401 EXPECT_EQ(profile_name, 402 UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); 403 EXPECT_EQ(avatar_index, 404 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); 405 } 406 407 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { 408 ProfileManager* profile_manager = g_browser_process->profile_manager(); 409 ASSERT_TRUE(profile_manager); 410 411 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); 412 ASSERT_TRUE(profile); 413 EXPECT_FALSE(profile->IsOffTheRecord()); 414 PrefService* prefs = profile->GetPrefs(); 415 EXPECT_EQ(IncognitoModePrefs::ENABLED, 416 IncognitoModePrefs::GetAvailability(prefs)); 417 418 // Attach an incognito Profile to the TestingProfile. 419 ASSERT_FALSE(profile->GetOffTheRecordProfile()); 420 TestingProfile* incognito_profile = new TestingProfile(); 421 incognito_profile->set_incognito(true); 422 EXPECT_TRUE(incognito_profile->IsOffTheRecord()); 423 TestingProfile* testing_profile = static_cast<TestingProfile*>(profile); 424 testing_profile->SetOffTheRecordProfile(incognito_profile); 425 ASSERT_TRUE(profile->GetOffTheRecordProfile()); 426 427 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); 428 EXPECT_FALSE( 429 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); 430 431 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when 432 // incognito mode is forced. 433 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); 434 EXPECT_TRUE( 435 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); 436 } 437 438 #if !defined(OS_ANDROID) 439 // There's no Browser object on Android. 440 TEST_F(ProfileManagerTest, LastOpenedProfiles) { 441 base::FilePath dest_path1 = temp_dir_.path(); 442 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 443 444 base::FilePath dest_path2 = temp_dir_.path(); 445 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 446 447 ProfileManager* profile_manager = g_browser_process->profile_manager(); 448 449 // Successfully create the profiles. 450 TestingProfile* profile1 = 451 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); 452 ASSERT_TRUE(profile1); 453 454 TestingProfile* profile2 = 455 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); 456 ASSERT_TRUE(profile2); 457 458 std::vector<Profile*> last_opened_profiles = 459 profile_manager->GetLastOpenedProfiles(); 460 ASSERT_EQ(0U, last_opened_profiles.size()); 461 462 // Create a browser for profile1. 463 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); 464 scoped_ptr<Browser> browser1a( 465 chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); 466 467 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 468 ASSERT_EQ(1U, last_opened_profiles.size()); 469 EXPECT_EQ(profile1, last_opened_profiles[0]); 470 471 // And for profile2. 472 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); 473 scoped_ptr<Browser> browser2( 474 chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); 475 476 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 477 ASSERT_EQ(2U, last_opened_profiles.size()); 478 EXPECT_EQ(profile1, last_opened_profiles[0]); 479 EXPECT_EQ(profile2, last_opened_profiles[1]); 480 481 // Adding more browsers doesn't change anything. 482 scoped_ptr<Browser> browser1b( 483 chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); 484 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 485 ASSERT_EQ(2U, last_opened_profiles.size()); 486 EXPECT_EQ(profile1, last_opened_profiles[0]); 487 EXPECT_EQ(profile2, last_opened_profiles[1]); 488 489 // Close the browsers. 490 browser1a.reset(); 491 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 492 ASSERT_EQ(2U, last_opened_profiles.size()); 493 EXPECT_EQ(profile1, last_opened_profiles[0]); 494 EXPECT_EQ(profile2, last_opened_profiles[1]); 495 496 browser1b.reset(); 497 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 498 ASSERT_EQ(1U, last_opened_profiles.size()); 499 EXPECT_EQ(profile2, last_opened_profiles[0]); 500 501 browser2.reset(); 502 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 503 ASSERT_EQ(0U, last_opened_profiles.size()); 504 } 505 506 TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) { 507 base::FilePath dest_path1 = temp_dir_.path(); 508 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 509 510 base::FilePath dest_path2 = temp_dir_.path(); 511 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 512 513 ProfileManager* profile_manager = g_browser_process->profile_manager(); 514 515 // Successfully create the profiles. 516 TestingProfile* profile1 = 517 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); 518 ASSERT_TRUE(profile1); 519 520 TestingProfile* profile2 = 521 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); 522 ASSERT_TRUE(profile2); 523 524 // Create a browser for profile1. 525 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); 526 scoped_ptr<Browser> browser1( 527 chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); 528 529 // And for profile2. 530 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); 531 scoped_ptr<Browser> browser2( 532 chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); 533 534 std::vector<Profile*> last_opened_profiles = 535 profile_manager->GetLastOpenedProfiles(); 536 ASSERT_EQ(2U, last_opened_profiles.size()); 537 EXPECT_EQ(profile1, last_opened_profiles[0]); 538 EXPECT_EQ(profile2, last_opened_profiles[1]); 539 540 // Simulate a shutdown. 541 content::NotificationService::current()->Notify( 542 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, 543 content::NotificationService::AllSources(), 544 content::NotificationService::NoDetails()); 545 546 // Even if the browsers are destructed during shutdown, the profiles stay 547 // open. 548 browser1.reset(); 549 browser2.reset(); 550 551 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 552 ASSERT_EQ(2U, last_opened_profiles.size()); 553 EXPECT_EQ(profile1, last_opened_profiles[0]); 554 EXPECT_EQ(profile2, last_opened_profiles[1]); 555 } 556 557 TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) { 558 base::FilePath dest_path1 = temp_dir_.path(); 559 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 560 base::FilePath dest_path2 = temp_dir_.path(); 561 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 562 563 ProfileManager* profile_manager = g_browser_process->profile_manager(); 564 565 // Successfully create the profiles. 566 TestingProfile* profile1 = 567 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); 568 ASSERT_TRUE(profile1); 569 570 // incognito profiles should not be managed by the profile manager but by the 571 // original profile. 572 TestingProfile* profile2 = new TestingProfile(); 573 ASSERT_TRUE(profile2); 574 profile2->set_incognito(true); 575 profile1->SetOffTheRecordProfile(profile2); 576 577 std::vector<Profile*> last_opened_profiles = 578 profile_manager->GetLastOpenedProfiles(); 579 ASSERT_EQ(0U, last_opened_profiles.size()); 580 581 // Create a browser for profile1. 582 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); 583 scoped_ptr<Browser> browser1( 584 chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); 585 586 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 587 ASSERT_EQ(1U, last_opened_profiles.size()); 588 EXPECT_EQ(profile1, last_opened_profiles[0]); 589 590 // And for profile2. 591 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); 592 scoped_ptr<Browser> browser2a( 593 chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); 594 595 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 596 ASSERT_EQ(1U, last_opened_profiles.size()); 597 EXPECT_EQ(profile1, last_opened_profiles[0]); 598 599 // Adding more browsers doesn't change anything. 600 scoped_ptr<Browser> browser2b( 601 chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); 602 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 603 ASSERT_EQ(1U, last_opened_profiles.size()); 604 EXPECT_EQ(profile1, last_opened_profiles[0]); 605 606 // Close the browsers. 607 browser2a.reset(); 608 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 609 ASSERT_EQ(1U, last_opened_profiles.size()); 610 EXPECT_EQ(profile1, last_opened_profiles[0]); 611 612 browser2b.reset(); 613 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 614 ASSERT_EQ(1U, last_opened_profiles.size()); 615 EXPECT_EQ(profile1, last_opened_profiles[0]); 616 617 browser1.reset(); 618 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 619 ASSERT_EQ(0U, last_opened_profiles.size()); 620 } 621 #endif // !defined(OS_ANDROID) 622 623 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 624 // There's no Browser object on Android and there's no multi-profiles on Chrome. 625 TEST_F(ProfileManagerTest, ActiveProfileDeleted) { 626 ProfileManager* profile_manager = g_browser_process->profile_manager(); 627 ASSERT_TRUE(profile_manager); 628 629 // Create and load two profiles. 630 const std::string profile_name1 = "New Profile 1"; 631 const std::string profile_name2 = "New Profile 2"; 632 base::FilePath dest_path1 = 633 temp_dir_.path().AppendASCII(profile_name1); 634 base::FilePath dest_path2 = 635 temp_dir_.path().AppendASCII(profile_name2); 636 637 MockObserver mock_observer; 638 EXPECT_CALL(mock_observer, OnProfileCreated( 639 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 640 641 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); 642 CreateProfileAsync(profile_manager, profile_name2, &mock_observer); 643 base::RunLoop().RunUntilIdle(); 644 645 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 646 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); 647 648 // Set the active profile. 649 PrefService* local_state = g_browser_process->local_state(); 650 local_state->SetString(prefs::kProfileLastUsed, profile_name1); 651 652 // Delete the active profile. 653 profile_manager->ScheduleProfileForDeletion(dest_path1, 654 ProfileManager::CreateCallback()); 655 // Spin the message loop so that all the callbacks can finish running. 656 base::RunLoop().RunUntilIdle(); 657 658 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); 659 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); 660 } 661 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 662 663 #if defined(OS_MACOSX) 664 // These tests are for a Mac-only code path that assumes the browser 665 // process isn't killed when all browser windows are closed. 666 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { 667 ProfileManager* profile_manager = g_browser_process->profile_manager(); 668 ASSERT_TRUE(profile_manager); 669 670 // Create and load one profile, and just create a second profile. 671 const std::string profile_name1 = "New Profile 1"; 672 const std::string profile_name2 = "New Profile 2"; 673 base::FilePath dest_path1 = 674 temp_dir_.path().AppendASCII(profile_name1); 675 base::FilePath dest_path2 = 676 temp_dir_.path().AppendASCII(profile_name2); 677 678 MockObserver mock_observer; 679 EXPECT_CALL(mock_observer, OnProfileCreated( 680 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 681 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); 682 base::RunLoop().RunUntilIdle(); 683 684 // Track the profile, but don't load it. 685 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 686 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), 687 string16(), 0, std::string()); 688 base::RunLoop().RunUntilIdle(); 689 690 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 691 EXPECT_EQ(2u, cache.GetNumberOfProfiles()); 692 693 // Set the active profile. 694 PrefService* local_state = g_browser_process->local_state(); 695 local_state->SetString(prefs::kProfileLastUsed, 696 dest_path1.BaseName().MaybeAsASCII()); 697 698 // Delete the active profile. This should switch and load the unloaded 699 // profile. 700 profile_manager->ScheduleProfileForDeletion(dest_path1, 701 ProfileManager::CreateCallback()); 702 703 // Spin the message loop so that all the callbacks can finish running. 704 base::RunLoop().RunUntilIdle(); 705 706 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); 707 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); 708 } 709 710 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded 711 // by simulating a scenario in which the profile that is being loaded as 712 // the next active profile has also been marked for deletion, so the 713 // ProfileManager needs to recursively select a different next profile. 714 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { 715 ProfileManager* profile_manager = g_browser_process->profile_manager(); 716 ASSERT_TRUE(profile_manager); 717 718 // Create and load one profile, and create two more profiles. 719 const std::string profile_name1 = "New Profile 1"; 720 const std::string profile_name2 = "New Profile 2"; 721 const std::string profile_name3 = "New Profile 3"; 722 base::FilePath dest_path1 = 723 temp_dir_.path().AppendASCII(profile_name1); 724 base::FilePath dest_path2 = 725 temp_dir_.path().AppendASCII(profile_name2); 726 base::FilePath dest_path3 = 727 temp_dir_.path().AppendASCII(profile_name3); 728 729 MockObserver mock_observer; 730 EXPECT_CALL(mock_observer, OnProfileCreated( 731 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 732 CreateProfileAsync(profile_manager, profile_name1, &mock_observer); 733 base::RunLoop().RunUntilIdle(); 734 735 // Create the other profiles, but don't load them. Assign a fake avatar icon 736 // to ensure that profiles in the info cache are sorted by the profile name, 737 // and not randomly by the avatar name. 738 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 739 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), 740 ASCIIToUTF16(profile_name2), 1, std::string()); 741 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3), 742 ASCIIToUTF16(profile_name3), 2, std::string()); 743 744 base::RunLoop().RunUntilIdle(); 745 746 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 747 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); 748 749 // Set the active profile. 750 PrefService* local_state = g_browser_process->local_state(); 751 local_state->SetString(prefs::kProfileLastUsed, 752 dest_path1.BaseName().MaybeAsASCII()); 753 754 // Delete the active profile, Profile1. 755 // This will post a CreateProfileAsync message, that tries to load Profile2, 756 // which checks that the profile is not being deleted, and then calls back 757 // FinishDeletingProfile for Profile1. 758 // Try to break this flow by setting the active profile to Profile2 in the 759 // middle (so after the first posted message), and trying to delete Profile2, 760 // so that the ProfileManager has to look for a different profile to load. 761 profile_manager->ScheduleProfileForDeletion(dest_path1, 762 ProfileManager::CreateCallback()); 763 local_state->SetString(prefs::kProfileLastUsed, 764 dest_path2.BaseName().MaybeAsASCII()); 765 profile_manager->ScheduleProfileForDeletion(dest_path2, 766 ProfileManager::CreateCallback()); 767 // Spin the message loop so that all the callbacks can finish running. 768 base::RunLoop().RunUntilIdle(); 769 770 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); 771 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); 772 } 773 #endif // !defined(OS_MACOSX) 774