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.
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_avatar_icon_util.h"
26 #include "chrome/browser/profiles/profile_info_cache.h"
27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/browser/profiles/profiles_state.h"
29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/common/chrome_constants.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/test/base/scoped_testing_local_state.h"
35 #include "chrome/test/base/test_browser_window.h"
36 #include "chrome/test/base/testing_browser_process.h"
37 #include "chrome/test/base/testing_profile.h"
38 #include "components/signin/core/common/profile_management_switches.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/public/test/test_browser_thread_bundle.h"
42 #include "grit/generated_resources.h"
43 #include "testing/gmock/include/gmock/gmock.h"
44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "ui/base/l10n/l10n_util.h"
47 #if defined(OS_CHROMEOS)
48 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
49 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
50 #include "chrome/browser/chromeos/settings/cros_settings.h"
51 #include "chrome/browser/chromeos/settings/device_settings_service.h"
52 #include "chromeos/chromeos_switches.h"
53 #include "chromeos/login/user_names.h"
54 #include "components/user_manager/user_manager.h"
57 using base::ASCIIToUTF16;
58 using content::BrowserThread;
62 // This global variable is used to check that value returned to different
63 // observers is the same.
64 Profile* g_created_profile;
66 class UnittestProfileManager : public ::ProfileManagerWithoutInit {
68 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
69 : ::ProfileManagerWithoutInit(user_data_dir) {}
72 virtual Profile* CreateProfileHelper(
73 const base::FilePath& file_path) OVERRIDE {
74 if (!base::PathExists(file_path)) {
75 if (!base::CreateDirectory(file_path))
78 return new TestingProfile(file_path, NULL);
81 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
82 Delegate* delegate) OVERRIDE {
83 // This is safe while all file operations are done on the FILE thread.
84 BrowserThread::PostTask(
85 BrowserThread::FILE, FROM_HERE,
86 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
88 return new TestingProfile(path, this);
94 class ProfileManagerTest : public testing::Test {
98 MOCK_METHOD2(OnProfileCreated,
99 void(Profile* profile, Profile::CreateStatus status));
103 : local_state_(TestingBrowserProcess::GetGlobal()) {
106 virtual void SetUp() {
107 // Create a new temporary directory, and store the path
108 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
109 TestingBrowserProcess::GetGlobal()->SetProfileManager(
110 new UnittestProfileManager(temp_dir_.path()));
112 #if defined(OS_CHROMEOS)
113 CommandLine* cl = CommandLine::ForCurrentProcess();
114 cl->AppendSwitch(switches::kTestType);
118 virtual void TearDown() {
119 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
120 base::RunLoop().RunUntilIdle();
123 // Helper function to create a profile with |name| for a profile |manager|.
124 void CreateProfileAsync(ProfileManager* manager,
125 const std::string& name,
127 MockObserver* mock_observer) {
128 manager->CreateProfileAsync(
129 temp_dir_.path().AppendASCII(name),
130 base::Bind(&MockObserver::OnProfileCreated,
131 base::Unretained(mock_observer)),
132 base::UTF8ToUTF16(name),
133 base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(0)),
134 is_supervised ? "Dummy ID" : std::string());
137 // Helper function to add a profile with |profile_name| to
138 // |profile_manager|'s ProfileInfoCache, and return the profile created.
139 Profile* AddProfileToCache(ProfileManager* profile_manager,
140 const std::string& path_suffix,
141 const base::string16& profile_name) {
142 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
143 size_t num_profiles = cache.GetNumberOfProfiles();
144 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
145 cache.AddProfileToCache(path, profile_name,
146 base::string16(), 0, std::string());
147 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
148 return profile_manager->GetProfile(path);
151 #if defined(OS_CHROMEOS)
152 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
153 chromeos::ScopedTestCrosSettings test_cros_settings_;
156 // The path to temporary directory used to contain the test operations.
157 base::ScopedTempDir temp_dir_;
158 ScopedTestingLocalState local_state_;
160 content::TestBrowserThreadBundle thread_bundle_;
162 #if defined(OS_CHROMEOS)
163 chromeos::ScopedTestUserManager test_user_manager_;
167 TEST_F(ProfileManagerTest, GetProfile) {
168 base::FilePath dest_path = temp_dir_.path();
169 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
171 ProfileManager* profile_manager = g_browser_process->profile_manager();
173 // Successfully create a profile.
174 Profile* profile = profile_manager->GetProfile(dest_path);
175 EXPECT_TRUE(profile);
177 // The profile already exists when we call GetProfile. Just load it.
178 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
181 TEST_F(ProfileManagerTest, DefaultProfileDir) {
182 base::FilePath expected_default =
183 base::FilePath().AppendASCII(chrome::kInitialProfile);
185 expected_default.value(),
186 g_browser_process->profile_manager()->GetInitialProfileDir().value());
189 #if defined(OS_CHROMEOS)
190 // This functionality only exists on Chrome OS.
191 TEST_F(ProfileManagerTest, LoggedInProfileDir) {
192 CommandLine *cl = CommandLine::ForCurrentProcess();
193 std::string profile_dir(chrome::kTestUserProfileDir);
195 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir);
197 base::FilePath expected_default =
198 base::FilePath().AppendASCII(chrome::kInitialProfile);
199 ProfileManager* profile_manager = g_browser_process->profile_manager();
200 EXPECT_EQ(expected_default.value(),
201 profile_manager->GetInitialProfileDir().value());
203 scoped_ptr<chromeos::MockUserManager> mock_user_manager;
204 mock_user_manager.reset(new chromeos::MockUserManager());
205 mock_user_manager->SetActiveUser("user@gmail.com");
206 user_manager::User* active_user = mock_user_manager->GetActiveUser();
207 profile_manager->Observe(
208 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
209 content::NotificationService::AllSources(),
210 content::Details<const user_manager::User>(active_user));
211 base::FilePath expected_logged_in(profile_dir);
212 EXPECT_EQ(expected_logged_in.value(),
213 profile_manager->GetInitialProfileDir().value());
214 VLOG(1) << temp_dir_.path().Append(
215 profile_manager->GetInitialProfileDir()).value();
220 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
221 base::FilePath dest_path1 = temp_dir_.path();
222 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
224 base::FilePath dest_path2 = temp_dir_.path();
225 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
227 ProfileManager* profile_manager = g_browser_process->profile_manager();
229 // Successfully create the profiles.
230 TestingProfile* profile1 =
231 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
232 ASSERT_TRUE(profile1);
234 TestingProfile* profile2 =
235 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
236 ASSERT_TRUE(profile2);
238 // Force lazy-init of some profile services to simulate use.
239 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
240 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1,
241 Profile::EXPLICIT_ACCESS));
242 profile1->CreateBookmarkModel(true);
243 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
244 profile2->CreateBookmarkModel(true);
245 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
246 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
247 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2,
248 Profile::EXPLICIT_ACCESS));
250 // Make sure any pending tasks run before we destroy the profiles.
251 base::RunLoop().RunUntilIdle();
253 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
255 // Make sure history cleans up correctly.
256 base::RunLoop().RunUntilIdle();
259 MATCHER(NotFail, "Profile creation failure status is not reported.") {
260 return arg == Profile::CREATE_STATUS_CREATED ||
261 arg == Profile::CREATE_STATUS_INITIALIZED;
264 MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
265 if (!g_created_profile)
266 g_created_profile = arg;
267 return arg != NULL && arg == g_created_profile;
270 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
271 g_created_profile = NULL;
273 MockObserver mock_observer1;
274 EXPECT_CALL(mock_observer1, OnProfileCreated(
275 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
276 MockObserver mock_observer2;
277 EXPECT_CALL(mock_observer2, OnProfileCreated(
278 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
279 MockObserver mock_observer3;
280 EXPECT_CALL(mock_observer3, OnProfileCreated(
281 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
283 ProfileManager* profile_manager = g_browser_process->profile_manager();
284 const std::string profile_name = "New Profile";
285 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
286 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
287 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
289 base::RunLoop().RunUntilIdle();
292 TEST_F(ProfileManagerTest, CreateProfilesAsync) {
293 const std::string profile_name1 = "New Profile 1";
294 const std::string profile_name2 = "New Profile 2";
296 MockObserver mock_observer;
297 EXPECT_CALL(mock_observer, OnProfileCreated(
298 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
300 ProfileManager* profile_manager = g_browser_process->profile_manager();
302 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
303 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
305 base::RunLoop().RunUntilIdle();
308 TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
309 std::string name = "0 Supervised Profile";
311 MockObserver mock_observer;
312 EXPECT_CALL(mock_observer, OnProfileCreated(
313 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
315 ProfileManager* profile_manager = g_browser_process->profile_manager();
316 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
317 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
319 CreateProfileAsync(profile_manager, name, true, &mock_observer);
320 base::RunLoop().RunUntilIdle();
322 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
323 // Supervised profiles should start out omitted from the profile list.
324 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
326 name = "1 Regular Profile";
327 CreateProfileAsync(profile_manager, name, false, &mock_observer);
328 base::RunLoop().RunUntilIdle();
330 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
331 // Non-supervised profiles should be included in the profile list.
332 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
335 TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
336 ProfileManager* profile_manager = g_browser_process->profile_manager();
337 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
338 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
340 const base::FilePath supervised_path =
341 temp_dir_.path().AppendASCII("Supervised");
342 TestingProfile* supervised_profile =
343 new TestingProfile(supervised_path, NULL);
344 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
346 // RegisterTestingProfile adds the profile to the cache and takes ownership.
347 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
348 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
349 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
351 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII(
353 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
355 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
357 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
358 size_t supervised_index = cache.GetIndexOfProfileWithPath(supervised_path);
359 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(supervised_index));
360 size_t nonsupervised_index =
361 cache.GetIndexOfProfileWithPath(nonsupervised_path);
362 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonsupervised_index));
365 TEST_F(ProfileManagerTest, GetGuestProfilePath) {
366 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
367 base::FilePath expected_path = temp_dir_.path();
368 expected_path = expected_path.Append(chrome::kGuestProfileDir);
369 EXPECT_EQ(expected_path, guest_path);
372 class UnittestGuestProfileManager : public UnittestProfileManager {
374 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
375 : UnittestProfileManager(user_data_dir) {}
378 virtual Profile* CreateProfileHelper(
379 const base::FilePath& file_path) OVERRIDE {
380 TestingProfile::Builder builder;
381 builder.SetGuestSession();
382 builder.SetPath(file_path);
383 TestingProfile* testing_profile = builder.Build().release();
384 return testing_profile;
388 class ProfileManagerGuestTest : public ProfileManagerTest {
390 virtual void SetUp() {
391 // Create a new temporary directory, and store the path
392 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
393 TestingBrowserProcess::GetGlobal()->SetProfileManager(
394 new UnittestGuestProfileManager(temp_dir_.path()));
396 #if defined(OS_CHROMEOS)
397 CommandLine* cl = CommandLine::ForCurrentProcess();
398 // This switch is needed to skip non-test specific behavior in
399 // ProfileManager (accessing DBusThreadManager).
400 cl->AppendSwitch(switches::kTestType);
402 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile,
403 std::string(chrome::kProfileDirPrefix) +
404 chromeos::login::kGuestUserName);
405 cl->AppendSwitch(chromeos::switches::kGuestSession);
406 cl->AppendSwitch(::switches::kIncognito);
408 user_manager::UserManager::Get()->UserLoggedIn(
409 chromeos::login::kGuestUserName,
410 chromeos::login::kGuestUserName,
416 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
417 ProfileManager* profile_manager = g_browser_process->profile_manager();
418 ASSERT_TRUE(profile_manager);
420 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
421 ASSERT_TRUE(profile);
422 EXPECT_TRUE(profile->IsOffTheRecord());
425 #if defined(OS_CHROMEOS)
426 TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
427 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
428 EXPECT_TRUE(primary_profile->IsOffTheRecord());
430 Profile* active_profile = ProfileManager::GetActiveUserProfile();
431 EXPECT_TRUE(active_profile->IsOffTheRecord());
433 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
435 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
436 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
438 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
442 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
443 ProfileManager* profile_manager = g_browser_process->profile_manager();
444 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
445 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
446 new base::FundamentalValue(true));
448 // Setting a pref which is not applicable to a system (i.e., Android in this
449 // case) does not necessarily create it. Don't bother continuing with the
450 // test if this pref doesn't exist because it will not load the profiles if
451 // it cannot verify that the pref for background mode is enabled.
452 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
455 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
456 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
457 ASCIIToUTF16("name_1"), base::string16(), 0,
459 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
460 ASCIIToUTF16("name_2"), base::string16(), 0,
462 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
463 ASCIIToUTF16("name_3"), base::string16(), 0,
465 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
466 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
467 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
469 profile_manager->AutoloadProfiles();
471 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
474 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
475 ProfileManager* profile_manager = g_browser_process->profile_manager();
476 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
477 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
478 new base::FundamentalValue(false));
480 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
481 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
482 ASCIIToUTF16("name_1"), base::string16(), 0,
484 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
485 ASCIIToUTF16("name_2"), base::string16(), 0,
487 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
488 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
489 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
491 profile_manager->AutoloadProfiles();
493 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
496 TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
497 base::FilePath dest_path = temp_dir_.path();
498 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
500 ProfileManager* profile_manager = g_browser_process->profile_manager();
504 // Successfully create the profile
505 profile = profile_manager->GetProfile(dest_path);
506 ASSERT_TRUE(profile);
508 // Check that the profile name is non empty
509 std::string profile_name =
510 profile->GetPrefs()->GetString(prefs::kProfileName);
511 EXPECT_FALSE(profile_name.empty());
513 // Check that the profile avatar index is valid
514 size_t avatar_index =
515 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
516 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
520 // Tests that a new profile's entry in the profile info cache is setup with the
521 // same values that are in the profile prefs.
522 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
523 base::FilePath dest_path = temp_dir_.path();
524 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
526 ProfileManager* profile_manager = g_browser_process->profile_manager();
527 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
529 // Successfully create the profile
530 Profile* profile = profile_manager->GetProfile(dest_path);
531 ASSERT_TRUE(profile);
533 std::string profile_name =
534 profile->GetPrefs()->GetString(prefs::kProfileName);
535 size_t avatar_index =
536 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
538 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
540 // Check if the profile prefs are the same as the cache prefs
541 EXPECT_EQ(profile_name,
542 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
543 EXPECT_EQ(avatar_index,
544 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
547 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
548 ProfileManager* profile_manager = g_browser_process->profile_manager();
549 ASSERT_TRUE(profile_manager);
551 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
552 ASSERT_TRUE(profile);
553 EXPECT_FALSE(profile->IsOffTheRecord());
554 PrefService* prefs = profile->GetPrefs();
555 EXPECT_EQ(IncognitoModePrefs::ENABLED,
556 IncognitoModePrefs::GetAvailability(prefs));
558 ASSERT_TRUE(profile->GetOffTheRecordProfile());
560 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
562 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
564 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
565 // incognito mode is forced.
566 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
568 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
571 #if !defined(OS_ANDROID)
572 // There's no Browser object on Android.
573 TEST_F(ProfileManagerTest, LastOpenedProfiles) {
574 base::FilePath dest_path1 = temp_dir_.path();
575 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
577 base::FilePath dest_path2 = temp_dir_.path();
578 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
580 ProfileManager* profile_manager = g_browser_process->profile_manager();
582 // Successfully create the profiles.
583 TestingProfile* profile1 =
584 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
585 ASSERT_TRUE(profile1);
587 TestingProfile* profile2 =
588 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
589 ASSERT_TRUE(profile2);
591 std::vector<Profile*> last_opened_profiles =
592 profile_manager->GetLastOpenedProfiles();
593 ASSERT_EQ(0U, last_opened_profiles.size());
595 // Create a browser for profile1.
596 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
597 scoped_ptr<Browser> browser1a(
598 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
600 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
601 ASSERT_EQ(1U, last_opened_profiles.size());
602 EXPECT_EQ(profile1, last_opened_profiles[0]);
605 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
606 scoped_ptr<Browser> browser2(
607 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
609 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
610 ASSERT_EQ(2U, last_opened_profiles.size());
611 EXPECT_EQ(profile1, last_opened_profiles[0]);
612 EXPECT_EQ(profile2, last_opened_profiles[1]);
614 // Adding more browsers doesn't change anything.
615 scoped_ptr<Browser> browser1b(
616 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
617 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
618 ASSERT_EQ(2U, last_opened_profiles.size());
619 EXPECT_EQ(profile1, last_opened_profiles[0]);
620 EXPECT_EQ(profile2, last_opened_profiles[1]);
622 // Close the browsers.
624 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
625 ASSERT_EQ(2U, last_opened_profiles.size());
626 EXPECT_EQ(profile1, last_opened_profiles[0]);
627 EXPECT_EQ(profile2, last_opened_profiles[1]);
630 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
631 ASSERT_EQ(1U, last_opened_profiles.size());
632 EXPECT_EQ(profile2, last_opened_profiles[0]);
635 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
636 ASSERT_EQ(0U, last_opened_profiles.size());
639 TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
640 base::FilePath dest_path1 = temp_dir_.path();
641 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
643 base::FilePath dest_path2 = temp_dir_.path();
644 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
646 ProfileManager* profile_manager = g_browser_process->profile_manager();
648 // Successfully create the profiles.
649 TestingProfile* profile1 =
650 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
651 ASSERT_TRUE(profile1);
653 TestingProfile* profile2 =
654 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
655 ASSERT_TRUE(profile2);
657 // Create a browser for profile1.
658 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
659 scoped_ptr<Browser> browser1(
660 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
663 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
664 scoped_ptr<Browser> browser2(
665 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
667 std::vector<Profile*> last_opened_profiles =
668 profile_manager->GetLastOpenedProfiles();
669 ASSERT_EQ(2U, last_opened_profiles.size());
670 EXPECT_EQ(profile1, last_opened_profiles[0]);
671 EXPECT_EQ(profile2, last_opened_profiles[1]);
673 // Simulate a shutdown.
674 content::NotificationService::current()->Notify(
675 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
676 content::NotificationService::AllSources(),
677 content::NotificationService::NoDetails());
679 // Even if the browsers are destructed during shutdown, the profiles stay
684 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
685 ASSERT_EQ(2U, last_opened_profiles.size());
686 EXPECT_EQ(profile1, last_opened_profiles[0]);
687 EXPECT_EQ(profile2, last_opened_profiles[1]);
690 TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
691 base::FilePath dest_path1 = temp_dir_.path();
692 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
693 base::FilePath dest_path2 = temp_dir_.path();
694 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
696 ProfileManager* profile_manager = g_browser_process->profile_manager();
698 // Successfully create the profiles.
699 TestingProfile* profile1 =
700 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
701 ASSERT_TRUE(profile1);
703 // Incognito profiles should not be managed by the profile manager but by the
705 TestingProfile::Builder builder;
706 builder.SetIncognito();
707 scoped_ptr<TestingProfile> profile2 = builder.Build();
708 profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
710 std::vector<Profile*> last_opened_profiles =
711 profile_manager->GetLastOpenedProfiles();
712 ASSERT_EQ(0U, last_opened_profiles.size());
714 // Create a browser for profile1.
715 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
716 scoped_ptr<Browser> browser1(
717 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
719 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
720 ASSERT_EQ(1U, last_opened_profiles.size());
721 EXPECT_EQ(profile1, last_opened_profiles[0]);
724 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
725 chrome::GetActiveDesktop());
726 scoped_ptr<Browser> browser2a(
727 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
729 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
730 ASSERT_EQ(1U, last_opened_profiles.size());
731 EXPECT_EQ(profile1, last_opened_profiles[0]);
733 // Adding more browsers doesn't change anything.
734 scoped_ptr<Browser> browser2b(
735 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
736 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
737 ASSERT_EQ(1U, last_opened_profiles.size());
738 EXPECT_EQ(profile1, last_opened_profiles[0]);
740 // Close the browsers.
742 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
743 ASSERT_EQ(1U, last_opened_profiles.size());
744 EXPECT_EQ(profile1, last_opened_profiles[0]);
747 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
748 ASSERT_EQ(1U, last_opened_profiles.size());
749 EXPECT_EQ(profile1, last_opened_profiles[0]);
752 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
753 ASSERT_EQ(0U, last_opened_profiles.size());
755 #endif // !defined(OS_ANDROID)
757 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
758 // There's no Browser object on Android and there's no multi-profiles on Chrome.
759 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
760 base::FilePath dest_path = temp_dir_.path();
761 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
763 ProfileManager* profile_manager = g_browser_process->profile_manager();
765 TestingProfile* profile =
766 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
767 ASSERT_TRUE(profile);
768 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
770 // Here the last used profile is still the "Default" profile.
771 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
772 EXPECT_NE(profile, last_used_profile);
774 // Create a browser for the profile.
775 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
776 scoped_ptr<Browser> browser(
777 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
778 last_used_profile = profile_manager->GetLastUsedProfile();
779 EXPECT_NE(profile, last_used_profile);
781 // Close the browser.
783 last_used_profile = profile_manager->GetLastUsedProfile();
784 EXPECT_NE(profile, last_used_profile);
787 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
788 base::FilePath dest_path1 = temp_dir_.path();
789 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
791 base::FilePath dest_path2 = temp_dir_.path();
792 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
794 base::FilePath dest_path3 = temp_dir_.path();
795 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
797 ProfileManager* profile_manager = g_browser_process->profile_manager();
799 // Successfully create the profiles.
800 TestingProfile* normal_profile =
801 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
802 ASSERT_TRUE(normal_profile);
804 // Add one ephemeral profile which should not end up in this list.
805 TestingProfile* ephemeral_profile1 =
806 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
807 ASSERT_TRUE(ephemeral_profile1);
808 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
811 // Add second ephemeral profile but don't mark it as such yet.
812 TestingProfile* ephemeral_profile2 =
813 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
814 ASSERT_TRUE(ephemeral_profile2);
816 // Create a browser for profile1.
817 Browser::CreateParams profile1_params(normal_profile,
818 chrome::GetActiveDesktop());
819 scoped_ptr<Browser> browser1(
820 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
822 // Create browsers for the ephemeral profile.
823 Browser::CreateParams profile2_params(ephemeral_profile1,
824 chrome::GetActiveDesktop());
825 scoped_ptr<Browser> browser2(
826 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
828 Browser::CreateParams profile3_params(ephemeral_profile2,
829 chrome::GetActiveDesktop());
830 scoped_ptr<Browser> browser3(
831 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
833 std::vector<Profile*> last_opened_profiles =
834 profile_manager->GetLastOpenedProfiles();
835 ASSERT_EQ(2U, last_opened_profiles.size());
836 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
837 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
839 // Mark the second profile ephemeral.
840 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
843 // Simulate a shutdown.
844 content::NotificationService::current()->Notify(
845 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
846 content::NotificationService::AllSources(),
847 content::NotificationService::NoDetails());
852 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
853 ASSERT_EQ(1U, last_opened_profiles.size());
854 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
857 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
858 ProfileManager* profile_manager = g_browser_process->profile_manager();
859 ASSERT_TRUE(profile_manager);
861 // Create and load two profiles.
862 const std::string profile_name1 = "New Profile 1";
863 const std::string profile_name2 = "New Profile 2";
864 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
865 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
867 MockObserver mock_observer;
868 EXPECT_CALL(mock_observer, OnProfileCreated(
869 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
871 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
872 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
873 base::RunLoop().RunUntilIdle();
875 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
876 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
878 // Set the active profile.
879 PrefService* local_state = g_browser_process->local_state();
880 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
882 // Delete the active profile.
883 profile_manager->ScheduleProfileForDeletion(dest_path1,
884 ProfileManager::CreateCallback());
885 // Spin the message loop so that all the callbacks can finish running.
886 base::RunLoop().RunUntilIdle();
888 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
889 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
892 TEST_F(ProfileManagerTest, LastProfileDeleted) {
893 ProfileManager* profile_manager = g_browser_process->profile_manager();
894 ASSERT_TRUE(profile_manager);
896 // Create and load a profile.
897 const std::string profile_name1 = "New Profile 1";
898 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
900 MockObserver mock_observer;
901 EXPECT_CALL(mock_observer, OnProfileCreated(
902 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
904 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
905 base::RunLoop().RunUntilIdle();
907 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
908 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
910 // Set it as the active profile.
911 PrefService* local_state = g_browser_process->local_state();
912 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
914 // Delete the active profile.
915 profile_manager->ScheduleProfileForDeletion(dest_path1,
916 ProfileManager::CreateCallback());
917 // Spin the message loop so that all the callbacks can finish running.
918 base::RunLoop().RunUntilIdle();
920 // A new profile should have been created
921 const std::string profile_name2 = "Profile 1";
922 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
924 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
925 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
926 EXPECT_EQ(dest_path2,
927 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
930 TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
931 ProfileManager* profile_manager = g_browser_process->profile_manager();
932 ASSERT_TRUE(profile_manager);
934 // Create and load a profile.
935 const std::string profile_name1 = "New Profile 1";
936 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
938 MockObserver mock_observer;
939 EXPECT_CALL(mock_observer, OnProfileCreated(
940 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
942 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
943 base::RunLoop().RunUntilIdle();
945 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
946 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
948 // Create the profile and register it.
949 const std::string guest_profile_name =
950 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
952 TestingProfile::Builder builder;
953 builder.SetGuestSession();
954 builder.SetPath(ProfileManager::GetGuestProfilePath());
955 TestingProfile* guest_profile = builder.Build().release();
956 guest_profile->set_profile_name(guest_profile_name);
957 // Registering the profile passes ownership to the ProfileManager.
958 profile_manager->RegisterTestingProfile(guest_profile, false, false);
960 // The Guest profile does not get added to the ProfileInfoCache.
961 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
962 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
964 // Set the Guest profile as the active profile.
965 PrefService* local_state = g_browser_process->local_state();
966 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
968 // Delete the other profile.
969 profile_manager->ScheduleProfileForDeletion(dest_path1,
970 ProfileManager::CreateCallback());
971 // Spin the message loop so that all the callbacks can finish running.
972 base::RunLoop().RunUntilIdle();
974 // A new profile should have been created.
975 const std::string profile_name2 = "Profile 1";
976 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
978 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
979 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
980 EXPECT_EQ(dest_path2,
981 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
984 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
985 if (!profiles::IsMultipleProfilesEnabled())
988 // The command line is reset at the end of every test by the test suite.
989 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
991 ProfileManager* profile_manager = g_browser_process->profile_manager();
992 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
993 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
995 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
996 const base::string16 default_profile_name =
997 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
998 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
999 Profile* profile1 = AddProfileToCache(profile_manager,
1000 "path_1", profile_name1);
1001 EXPECT_EQ(default_profile_name,
1002 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1004 // Multiple profiles means displaying the actual profile names.
1005 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1006 Profile* profile2 = AddProfileToCache(profile_manager,
1007 "path_2", profile_name2);
1008 EXPECT_EQ(profile_name1,
1009 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1010 EXPECT_EQ(profile_name2,
1011 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1013 // Deleting a profile means returning to the default name.
1014 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1015 ProfileManager::CreateCallback());
1016 // Spin the message loop so that all the callbacks can finish running.
1017 base::RunLoop().RunUntilIdle();
1018 EXPECT_EQ(default_profile_name,
1019 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1022 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1023 if (!profiles::IsMultipleProfilesEnabled())
1026 // The command line is reset at the end of every test by the test suite.
1027 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
1029 ProfileManager* profile_manager = g_browser_process->profile_manager();
1030 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1031 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1033 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1034 const base::string16 default_profile_name =
1035 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1036 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1037 Profile* profile1 = AddProfileToCache(profile_manager,
1038 "path_1", profile_name1);
1039 EXPECT_EQ(default_profile_name,
1040 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1042 // We should display custom names for local profiles.
1043 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
1044 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
1045 cache.SetProfileIsUsingDefaultNameAtIndex(0, false);
1046 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
1047 EXPECT_EQ(custom_profile_name,
1048 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1050 // Multiple profiles means displaying the actual profile names.
1051 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1052 Profile* profile2 = AddProfileToCache(profile_manager,
1053 "path_2", profile_name2);
1054 EXPECT_EQ(custom_profile_name,
1055 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1056 EXPECT_EQ(profile_name2,
1057 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1059 // Deleting a profile means returning to the original, custom name.
1060 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1061 ProfileManager::CreateCallback());
1062 // Spin the message loop so that all the callbacks can finish running.
1063 base::RunLoop().RunUntilIdle();
1064 EXPECT_EQ(custom_profile_name,
1065 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1068 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1069 if (!profiles::IsMultipleProfilesEnabled())
1072 // The command line is reset at the end of every test by the test suite.
1073 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
1075 ProfileManager* profile_manager = g_browser_process->profile_manager();
1076 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1077 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1079 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1080 const base::string16 default_profile_name =
1081 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1082 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1083 Profile* profile1 = AddProfileToCache(profile_manager,
1084 "path_1", profile_name1);
1085 EXPECT_EQ(default_profile_name,
1086 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1088 // We should display the actual profile name for signed in profiles.
1089 cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("user@gmail.com"));
1090 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
1091 EXPECT_EQ(profile_name1,
1092 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1094 // Multiple profiles means displaying the actual profile names.
1095 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1096 Profile* profile2 = AddProfileToCache(profile_manager,
1097 "path_2", profile_name2);
1098 EXPECT_EQ(profile_name1,
1099 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1100 EXPECT_EQ(profile_name2,
1101 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1103 // Deleting a profile means returning to the original, actual profile name.
1104 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1105 ProfileManager::CreateCallback());
1106 // Spin the message loop so that all the callbacks can finish running.
1107 base::RunLoop().RunUntilIdle();
1108 EXPECT_EQ(profile_name1,
1109 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1111 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1113 #if defined(OS_MACOSX)
1114 // These tests are for a Mac-only code path that assumes the browser
1115 // process isn't killed when all browser windows are closed.
1116 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1117 ProfileManager* profile_manager = g_browser_process->profile_manager();
1118 ASSERT_TRUE(profile_manager);
1120 // Create and load one profile, and just create a second profile.
1121 const std::string profile_name1 = "New Profile 1";
1122 const std::string profile_name2 = "New Profile 2";
1123 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1124 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1126 MockObserver mock_observer;
1127 EXPECT_CALL(mock_observer, OnProfileCreated(
1128 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1129 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1130 base::RunLoop().RunUntilIdle();
1132 // Track the profile, but don't load it.
1133 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1134 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1135 base::string16(), 0, std::string());
1136 base::RunLoop().RunUntilIdle();
1138 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1139 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
1141 // Set the active profile.
1142 PrefService* local_state = g_browser_process->local_state();
1143 local_state->SetString(prefs::kProfileLastUsed,
1144 dest_path1.BaseName().MaybeAsASCII());
1146 // Delete the active profile. This should switch and load the unloaded
1148 profile_manager->ScheduleProfileForDeletion(dest_path1,
1149 ProfileManager::CreateCallback());
1151 // Spin the message loop so that all the callbacks can finish running.
1152 base::RunLoop().RunUntilIdle();
1154 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1155 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1158 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1159 // by simulating a scenario in which the profile that is being loaded as
1160 // the next active profile has also been marked for deletion, so the
1161 // ProfileManager needs to recursively select a different next profile.
1162 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1163 ProfileManager* profile_manager = g_browser_process->profile_manager();
1164 ASSERT_TRUE(profile_manager);
1166 // Create and load one profile, and create two more profiles.
1167 const std::string profile_name1 = "New Profile 1";
1168 const std::string profile_name2 = "New Profile 2";
1169 const std::string profile_name3 = "New Profile 3";
1170 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1171 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1172 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
1174 MockObserver mock_observer;
1175 EXPECT_CALL(mock_observer, OnProfileCreated(
1176 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1177 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1178 base::RunLoop().RunUntilIdle();
1180 // Create the other profiles, but don't load them. Assign a fake avatar icon
1181 // to ensure that profiles in the info cache are sorted by the profile name,
1182 // and not randomly by the avatar name.
1183 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1184 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1185 ASCIIToUTF16(profile_name2), 1, std::string());
1186 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
1187 ASCIIToUTF16(profile_name3), 2, std::string());
1189 base::RunLoop().RunUntilIdle();
1191 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1192 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1194 // Set the active profile.
1195 PrefService* local_state = g_browser_process->local_state();
1196 local_state->SetString(prefs::kProfileLastUsed,
1197 dest_path1.BaseName().MaybeAsASCII());
1199 // Delete the active profile, Profile1.
1200 // This will post a CreateProfileAsync message, that tries to load Profile2,
1201 // which checks that the profile is not being deleted, and then calls back
1202 // FinishDeletingProfile for Profile1.
1203 // Try to break this flow by setting the active profile to Profile2 in the
1204 // middle (so after the first posted message), and trying to delete Profile2,
1205 // so that the ProfileManager has to look for a different profile to load.
1206 profile_manager->ScheduleProfileForDeletion(dest_path1,
1207 ProfileManager::CreateCallback());
1208 local_state->SetString(prefs::kProfileLastUsed,
1209 dest_path2.BaseName().MaybeAsASCII());
1210 profile_manager->ScheduleProfileForDeletion(dest_path2,
1211 ProfileManager::CreateCallback());
1212 // Spin the message loop so that all the callbacks can finish running.
1213 base::RunLoop().RunUntilIdle();
1215 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1216 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1218 #endif // !defined(OS_MACOSX)