BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_manager_unittest.cc
blob78e3273a37579eb6096ae28fe4cb7b6bd18c3ec7
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.
5 #include <string>
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/io_thread.h"
20 #include "chrome/browser/prefs/browser_prefs.h"
21 #include "chrome/browser/prefs/incognito_mode_prefs.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
24 #include "chrome/browser/profiles/profile_info_cache.h"
25 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/browser/profiles/profiles_state.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/grit/generated_resources.h"
33 #include "chrome/test/base/scoped_testing_local_state.h"
34 #include "chrome/test/base/test_browser_window.h"
35 #include "chrome/test/base/testing_browser_process.h"
36 #include "chrome/test/base/testing_profile.h"
37 #include "components/history/core/browser/history_service.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 "testing/gmock/include/gmock/gmock.h"
43 #include "testing/gtest/include/gtest/gtest.h"
44 #include "ui/base/l10n/l10n_util.h"
46 #if defined(OS_CHROMEOS)
47 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
48 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
49 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
50 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
51 #include "chrome/browser/chromeos/profiles/profile_helper.h"
52 #include "chrome/browser/chromeos/settings/cros_settings.h"
53 #include "chrome/browser/chromeos/settings/device_settings_service.h"
54 #include "chromeos/chromeos_switches.h"
55 #include "chromeos/login/user_names.h"
56 #include "components/user_manager/user_manager.h"
57 #endif // defined(OS_CHROMEOS)
59 using base::ASCIIToUTF16;
60 using content::BrowserThread;
62 namespace {
64 // This global variable is used to check that value returned to different
65 // observers is the same.
66 Profile* g_created_profile;
68 class UnittestProfileManager : public ::ProfileManagerWithoutInit {
69 public:
70 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
71 : ::ProfileManagerWithoutInit(user_data_dir) {}
73 protected:
74 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
75 if (!base::PathExists(file_path)) {
76 if (!base::CreateDirectory(file_path))
77 return NULL;
79 return new TestingProfile(file_path, NULL);
82 Profile* CreateProfileAsyncHelper(const base::FilePath& path,
83 Delegate* delegate) override {
84 // This is safe while all file operations are done on the FILE thread.
85 BrowserThread::PostTask(
86 BrowserThread::FILE, FROM_HERE,
87 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
89 return new TestingProfile(path, this);
93 } // namespace
95 class ProfileManagerTest : public testing::Test {
96 protected:
97 class MockObserver {
98 public:
99 MOCK_METHOD2(OnProfileCreated,
100 void(Profile* profile, Profile::CreateStatus status));
103 ProfileManagerTest()
104 : local_state_(TestingBrowserProcess::GetGlobal()) {
107 void SetUp() override {
108 // Create a new temporary directory, and store the path
109 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
110 TestingBrowserProcess::GetGlobal()->SetProfileManager(
111 new UnittestProfileManager(temp_dir_.path()));
113 #if defined(OS_CHROMEOS)
114 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
115 cl->AppendSwitch(switches::kTestType);
116 chromeos::WallpaperManager::Initialize();
117 #endif
120 void TearDown() override {
121 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
122 base::RunLoop().RunUntilIdle();
123 #if defined(OS_CHROMEOS)
124 chromeos::WallpaperManager::Shutdown();
125 #endif
128 // Helper function to create a profile with |name| for a profile |manager|.
129 void CreateProfileAsync(ProfileManager* manager,
130 const std::string& name,
131 bool is_supervised,
132 MockObserver* mock_observer) {
133 manager->CreateProfileAsync(
134 temp_dir_.path().AppendASCII(name),
135 base::Bind(&MockObserver::OnProfileCreated,
136 base::Unretained(mock_observer)),
137 base::UTF8ToUTF16(name),
138 base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(0)),
139 is_supervised ? "Dummy ID" : std::string());
142 // Helper function to add a profile with |profile_name| to
143 // |profile_manager|'s ProfileInfoCache, and return the profile created.
144 Profile* AddProfileToCache(ProfileManager* profile_manager,
145 const std::string& path_suffix,
146 const base::string16& profile_name) {
147 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
148 size_t num_profiles = cache.GetNumberOfProfiles();
149 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
150 cache.AddProfileToCache(path, profile_name,
151 std::string(), base::string16(), 0, std::string());
152 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
153 return profile_manager->GetProfile(path);
156 #if defined(OS_CHROMEOS)
157 // Helper function to register an user with id |user_id| and create profile
158 // with a correct path.
159 void RegisterUser(const std::string& user_id) {
160 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
161 const std::string user_id_hash =
162 profile_helper->GetUserIdHashByUserIdForTesting(user_id);
163 user_manager::UserManager::Get()->UserLoggedIn(
164 user_id, user_id_hash, false);
165 g_browser_process->profile_manager()->GetProfile(
166 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
169 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
170 chromeos::ScopedTestCrosSettings test_cros_settings_;
171 #endif
173 // The path to temporary directory used to contain the test operations.
174 base::ScopedTempDir temp_dir_;
175 ScopedTestingLocalState local_state_;
177 content::TestBrowserThreadBundle thread_bundle_;
179 #if defined(OS_CHROMEOS)
180 chromeos::ScopedTestUserManager test_user_manager_;
181 #endif
183 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
186 TEST_F(ProfileManagerTest, GetProfile) {
187 base::FilePath dest_path = temp_dir_.path();
188 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
190 ProfileManager* profile_manager = g_browser_process->profile_manager();
192 // Successfully create a profile.
193 Profile* profile = profile_manager->GetProfile(dest_path);
194 EXPECT_TRUE(profile);
196 // The profile already exists when we call GetProfile. Just load it.
197 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
200 TEST_F(ProfileManagerTest, DefaultProfileDir) {
201 base::FilePath expected_default =
202 base::FilePath().AppendASCII(chrome::kInitialProfile);
203 EXPECT_EQ(
204 expected_default.value(),
205 g_browser_process->profile_manager()->GetInitialProfileDir().value());
208 MATCHER(NotFail, "Profile creation failure status is not reported.") {
209 return arg == Profile::CREATE_STATUS_CREATED ||
210 arg == Profile::CREATE_STATUS_INITIALIZED;
213 MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
214 if (!g_created_profile)
215 g_created_profile = arg;
216 return arg != NULL && arg == g_created_profile;
219 #if defined(OS_CHROMEOS)
221 // This functionality only exists on Chrome OS.
222 TEST_F(ProfileManagerTest, LoggedInProfileDir) {
223 base::FilePath expected_default =
224 base::FilePath().AppendASCII(chrome::kInitialProfile);
225 ProfileManager* profile_manager = g_browser_process->profile_manager();
226 EXPECT_EQ(expected_default.value(),
227 profile_manager->GetInitialProfileDir().value());
229 const char kTestUserName[] = "test-user@example.com";
230 chromeos::FakeChromeUserManager* user_manager =
231 new chromeos::FakeChromeUserManager();
232 chromeos::ScopedUserManagerEnabler enabler(user_manager);
234 const user_manager::User* active_user = user_manager->AddUser(kTestUserName);
235 user_manager->LoginUser(kTestUserName);
236 user_manager->SwitchActiveUser(kTestUserName);
238 profile_manager->Observe(
239 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
240 content::NotificationService::AllSources(),
241 content::Details<const user_manager::User>(active_user));
242 base::FilePath expected_logged_in(
243 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
244 EXPECT_EQ(expected_logged_in.value(),
245 profile_manager->GetInitialProfileDir().value());
246 VLOG(1) << temp_dir_.path().Append(
247 profile_manager->GetInitialProfileDir()).value();
250 #endif
252 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
253 base::FilePath dest_path1 = temp_dir_.path();
254 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
256 base::FilePath dest_path2 = temp_dir_.path();
257 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
259 ProfileManager* profile_manager = g_browser_process->profile_manager();
261 // Successfully create the profiles.
262 TestingProfile* profile1 =
263 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
264 ASSERT_TRUE(profile1);
266 TestingProfile* profile2 =
267 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
268 ASSERT_TRUE(profile2);
270 // Force lazy-init of some profile services to simulate use.
271 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
272 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
273 profile1, ServiceAccessType::EXPLICIT_ACCESS));
274 profile1->CreateBookmarkModel(true);
275 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
276 profile2->CreateBookmarkModel(true);
277 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
278 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
279 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
280 profile2, ServiceAccessType::EXPLICIT_ACCESS));
282 // Make sure any pending tasks run before we destroy the profiles.
283 base::RunLoop().RunUntilIdle();
285 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
287 // Make sure history cleans up correctly.
288 base::RunLoop().RunUntilIdle();
291 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
292 g_created_profile = NULL;
294 MockObserver mock_observer1;
295 EXPECT_CALL(mock_observer1, OnProfileCreated(
296 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
297 MockObserver mock_observer2;
298 EXPECT_CALL(mock_observer2, OnProfileCreated(
299 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
300 MockObserver mock_observer3;
301 EXPECT_CALL(mock_observer3, OnProfileCreated(
302 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
304 ProfileManager* profile_manager = g_browser_process->profile_manager();
305 const std::string profile_name = "New Profile";
306 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
307 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
308 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
310 base::RunLoop().RunUntilIdle();
313 TEST_F(ProfileManagerTest, CreateProfilesAsync) {
314 const std::string profile_name1 = "New Profile 1";
315 const std::string profile_name2 = "New Profile 2";
317 MockObserver mock_observer;
318 EXPECT_CALL(mock_observer, OnProfileCreated(
319 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
321 ProfileManager* profile_manager = g_browser_process->profile_manager();
323 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
324 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
326 base::RunLoop().RunUntilIdle();
329 TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
330 std::string name = "0 Supervised Profile";
332 MockObserver mock_observer;
333 EXPECT_CALL(mock_observer, OnProfileCreated(
334 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
336 ProfileManager* profile_manager = g_browser_process->profile_manager();
337 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
338 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
340 CreateProfileAsync(profile_manager, name, true, &mock_observer);
341 base::RunLoop().RunUntilIdle();
343 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
344 // Supervised profiles should start out omitted from the profile list.
345 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
347 name = "1 Regular Profile";
348 CreateProfileAsync(profile_manager, name, false, &mock_observer);
349 base::RunLoop().RunUntilIdle();
351 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
352 // Non-supervised profiles should be included in the profile list.
353 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
356 TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
357 ProfileManager* profile_manager = g_browser_process->profile_manager();
358 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
359 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
361 const base::FilePath supervised_path =
362 temp_dir_.path().AppendASCII("Supervised");
363 TestingProfile* supervised_profile =
364 new TestingProfile(supervised_path, NULL);
365 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
367 // RegisterTestingProfile adds the profile to the cache and takes ownership.
368 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
369 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
370 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
372 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII(
373 "Non-Supervised");
374 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
375 NULL);
376 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
378 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
379 size_t supervised_index = cache.GetIndexOfProfileWithPath(supervised_path);
380 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(supervised_index));
381 size_t nonsupervised_index =
382 cache.GetIndexOfProfileWithPath(nonsupervised_path);
383 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonsupervised_index));
386 TEST_F(ProfileManagerTest, GetGuestProfilePath) {
387 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
388 base::FilePath expected_path = temp_dir_.path();
389 expected_path = expected_path.Append(chrome::kGuestProfileDir);
390 EXPECT_EQ(expected_path, guest_path);
393 TEST_F(ProfileManagerTest, GetSystemProfilePath) {
394 base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath();
395 base::FilePath expected_path = temp_dir_.path();
396 expected_path = expected_path.Append(chrome::kSystemProfileDir);
397 EXPECT_EQ(expected_path, system_profile_path);
400 class UnittestGuestProfileManager : public UnittestProfileManager {
401 public:
402 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
403 : UnittestProfileManager(user_data_dir) {}
405 protected:
406 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
407 TestingProfile::Builder builder;
408 builder.SetGuestSession();
409 builder.SetPath(file_path);
410 TestingProfile* testing_profile = builder.Build().release();
411 return testing_profile;
415 class ProfileManagerGuestTest : public ProfileManagerTest {
416 protected:
417 void SetUp() override {
418 // Create a new temporary directory, and store the path
419 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
420 TestingBrowserProcess::GetGlobal()->SetProfileManager(
421 new UnittestGuestProfileManager(temp_dir_.path()));
423 #if defined(OS_CHROMEOS)
424 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
425 // This switch is needed to skip non-test specific behavior in
426 // ProfileManager (accessing DBusThreadManager).
427 cl->AppendSwitch(switches::kTestType);
429 cl->AppendSwitch(chromeos::switches::kGuestSession);
430 cl->AppendSwitch(::switches::kIncognito);
432 chromeos::WallpaperManager::Initialize();
433 RegisterUser(chromeos::login::kGuestUserName);
434 #endif
438 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
439 ProfileManager* profile_manager = g_browser_process->profile_manager();
440 ASSERT_TRUE(profile_manager);
442 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
443 ASSERT_TRUE(profile);
444 EXPECT_TRUE(profile->IsOffTheRecord());
447 #if defined(OS_CHROMEOS)
448 TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
449 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
450 EXPECT_TRUE(primary_profile->IsOffTheRecord());
452 Profile* active_profile = ProfileManager::GetActiveUserProfile();
453 EXPECT_TRUE(active_profile->IsOffTheRecord());
455 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
457 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
458 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
460 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
462 #endif
464 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
465 ProfileManager* profile_manager = g_browser_process->profile_manager();
466 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
467 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
468 new base::FundamentalValue(true));
470 // Setting a pref which is not applicable to a system (i.e., Android in this
471 // case) does not necessarily create it. Don't bother continuing with the
472 // test if this pref doesn't exist because it will not load the profiles if
473 // it cannot verify that the pref for background mode is enabled.
474 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
475 return;
477 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
478 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
479 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
480 std::string());
481 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
482 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
483 std::string());
484 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
485 ASCIIToUTF16("name_3"), "34567", base::string16(), 0,
486 std::string());
487 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
488 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
489 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
491 profile_manager->AutoloadProfiles();
493 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
496 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
497 ProfileManager* profile_manager = g_browser_process->profile_manager();
498 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
499 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
500 new base::FundamentalValue(false));
502 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
503 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
504 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
505 std::string());
506 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
507 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
508 std::string());
509 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
510 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
511 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
513 profile_manager->AutoloadProfiles();
515 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
518 TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
519 base::FilePath dest_path = temp_dir_.path();
520 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
522 ProfileManager* profile_manager = g_browser_process->profile_manager();
524 Profile* profile;
526 // Successfully create the profile
527 profile = profile_manager->GetProfile(dest_path);
528 ASSERT_TRUE(profile);
530 // Check that the profile name is non empty
531 std::string profile_name =
532 profile->GetPrefs()->GetString(prefs::kProfileName);
533 EXPECT_FALSE(profile_name.empty());
535 // Check that the profile avatar index is valid
536 size_t avatar_index =
537 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
538 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
539 avatar_index));
542 // Tests that a new profile's entry in the profile info cache is setup with the
543 // same values that are in the profile prefs.
544 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
545 base::FilePath dest_path = temp_dir_.path();
546 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
548 ProfileManager* profile_manager = g_browser_process->profile_manager();
549 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
551 // Successfully create the profile
552 Profile* profile = profile_manager->GetProfile(dest_path);
553 ASSERT_TRUE(profile);
555 std::string profile_name =
556 profile->GetPrefs()->GetString(prefs::kProfileName);
557 size_t avatar_index =
558 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
560 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
562 // Check if the profile prefs are the same as the cache prefs
563 EXPECT_EQ(profile_name,
564 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
565 EXPECT_EQ(avatar_index,
566 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
569 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
570 ProfileManager* profile_manager = g_browser_process->profile_manager();
571 ASSERT_TRUE(profile_manager);
573 #if defined(OS_CHROMEOS)
574 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that
575 // is forced to be incognito. That's why we need to create at least one user
576 // to get a regular profile.
577 RegisterUser("test-user@example.com");
578 #endif
580 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
581 ASSERT_TRUE(profile);
582 EXPECT_FALSE(profile->IsOffTheRecord());
583 PrefService* prefs = profile->GetPrefs();
584 EXPECT_EQ(IncognitoModePrefs::ENABLED,
585 IncognitoModePrefs::GetAvailability(prefs));
587 ASSERT_TRUE(profile->GetOffTheRecordProfile());
589 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
590 EXPECT_FALSE(
591 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
593 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
594 // incognito mode is forced.
595 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
596 EXPECT_TRUE(
597 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
600 #if !defined(OS_ANDROID)
601 // There's no Browser object on Android.
602 TEST_F(ProfileManagerTest, LastOpenedProfiles) {
603 base::FilePath dest_path1 = temp_dir_.path();
604 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
606 base::FilePath dest_path2 = temp_dir_.path();
607 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
609 ProfileManager* profile_manager = g_browser_process->profile_manager();
611 // Successfully create the profiles.
612 TestingProfile* profile1 =
613 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
614 ASSERT_TRUE(profile1);
616 TestingProfile* profile2 =
617 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
618 ASSERT_TRUE(profile2);
620 std::vector<Profile*> last_opened_profiles =
621 profile_manager->GetLastOpenedProfiles();
622 ASSERT_EQ(0U, last_opened_profiles.size());
624 // Create a browser for profile1.
625 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
626 scoped_ptr<Browser> browser1a(
627 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
629 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
630 ASSERT_EQ(1U, last_opened_profiles.size());
631 EXPECT_EQ(profile1, last_opened_profiles[0]);
633 // And for profile2.
634 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
635 scoped_ptr<Browser> browser2(
636 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
638 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
639 ASSERT_EQ(2U, last_opened_profiles.size());
640 EXPECT_EQ(profile1, last_opened_profiles[0]);
641 EXPECT_EQ(profile2, last_opened_profiles[1]);
643 // Adding more browsers doesn't change anything.
644 scoped_ptr<Browser> browser1b(
645 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
646 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
647 ASSERT_EQ(2U, last_opened_profiles.size());
648 EXPECT_EQ(profile1, last_opened_profiles[0]);
649 EXPECT_EQ(profile2, last_opened_profiles[1]);
651 // Close the browsers.
652 browser1a.reset();
653 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
654 ASSERT_EQ(2U, last_opened_profiles.size());
655 EXPECT_EQ(profile1, last_opened_profiles[0]);
656 EXPECT_EQ(profile2, last_opened_profiles[1]);
658 browser1b.reset();
659 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
660 ASSERT_EQ(1U, last_opened_profiles.size());
661 EXPECT_EQ(profile2, last_opened_profiles[0]);
663 browser2.reset();
664 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
665 ASSERT_EQ(0U, last_opened_profiles.size());
668 TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
669 base::FilePath dest_path1 = temp_dir_.path();
670 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
672 base::FilePath dest_path2 = temp_dir_.path();
673 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
675 ProfileManager* profile_manager = g_browser_process->profile_manager();
677 // Successfully create the profiles.
678 TestingProfile* profile1 =
679 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
680 ASSERT_TRUE(profile1);
682 TestingProfile* profile2 =
683 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
684 ASSERT_TRUE(profile2);
686 // Create a browser for profile1.
687 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
688 scoped_ptr<Browser> browser1(
689 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
691 // And for profile2.
692 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
693 scoped_ptr<Browser> browser2(
694 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
696 std::vector<Profile*> last_opened_profiles =
697 profile_manager->GetLastOpenedProfiles();
698 ASSERT_EQ(2U, last_opened_profiles.size());
699 EXPECT_EQ(profile1, last_opened_profiles[0]);
700 EXPECT_EQ(profile2, last_opened_profiles[1]);
702 // Simulate a shutdown.
703 content::NotificationService::current()->Notify(
704 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
705 content::NotificationService::AllSources(),
706 content::NotificationService::NoDetails());
708 // Even if the browsers are destructed during shutdown, the profiles stay
709 // open.
710 browser1.reset();
711 browser2.reset();
713 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
714 ASSERT_EQ(2U, last_opened_profiles.size());
715 EXPECT_EQ(profile1, last_opened_profiles[0]);
716 EXPECT_EQ(profile2, last_opened_profiles[1]);
719 TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
720 base::FilePath dest_path1 = temp_dir_.path();
721 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
722 base::FilePath dest_path2 = temp_dir_.path();
723 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
725 ProfileManager* profile_manager = g_browser_process->profile_manager();
727 // Successfully create the profiles.
728 TestingProfile* profile1 =
729 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
730 ASSERT_TRUE(profile1);
732 std::vector<Profile*> last_opened_profiles =
733 profile_manager->GetLastOpenedProfiles();
734 ASSERT_EQ(0U, last_opened_profiles.size());
736 // Create a browser for profile1.
737 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
738 scoped_ptr<Browser> browser1(
739 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
741 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
742 ASSERT_EQ(1U, last_opened_profiles.size());
743 EXPECT_EQ(profile1, last_opened_profiles[0]);
745 // And for profile2.
746 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
747 chrome::GetActiveDesktop());
748 scoped_ptr<Browser> browser2a(
749 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
751 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
752 ASSERT_EQ(1U, last_opened_profiles.size());
753 EXPECT_EQ(profile1, last_opened_profiles[0]);
755 // Adding more browsers doesn't change anything.
756 scoped_ptr<Browser> browser2b(
757 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
758 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
759 ASSERT_EQ(1U, last_opened_profiles.size());
760 EXPECT_EQ(profile1, last_opened_profiles[0]);
762 // Close the browsers.
763 browser2a.reset();
764 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
765 ASSERT_EQ(1U, last_opened_profiles.size());
766 EXPECT_EQ(profile1, last_opened_profiles[0]);
768 browser2b.reset();
769 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
770 ASSERT_EQ(1U, last_opened_profiles.size());
771 EXPECT_EQ(profile1, last_opened_profiles[0]);
773 browser1.reset();
774 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
775 ASSERT_EQ(0U, last_opened_profiles.size());
777 #endif // !defined(OS_ANDROID)
779 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
780 // There's no Browser object on Android and there's no multi-profiles on Chrome.
781 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
782 base::FilePath dest_path = temp_dir_.path();
783 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
785 ProfileManager* profile_manager = g_browser_process->profile_manager();
787 TestingProfile* profile =
788 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
789 ASSERT_TRUE(profile);
790 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
792 // Here the last used profile is still the "Default" profile.
793 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
794 EXPECT_NE(profile, last_used_profile);
796 // Create a browser for the profile.
797 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
798 scoped_ptr<Browser> browser(
799 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
800 last_used_profile = profile_manager->GetLastUsedProfile();
801 EXPECT_NE(profile, last_used_profile);
803 // Close the browser.
804 browser.reset();
805 last_used_profile = profile_manager->GetLastUsedProfile();
806 EXPECT_NE(profile, last_used_profile);
809 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
810 base::FilePath dest_path1 = temp_dir_.path();
811 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
813 base::FilePath dest_path2 = temp_dir_.path();
814 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
816 base::FilePath dest_path3 = temp_dir_.path();
817 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
819 ProfileManager* profile_manager = g_browser_process->profile_manager();
821 // Successfully create the profiles.
822 TestingProfile* normal_profile =
823 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
824 ASSERT_TRUE(normal_profile);
826 // Add one ephemeral profile which should not end up in this list.
827 TestingProfile* ephemeral_profile1 =
828 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
829 ASSERT_TRUE(ephemeral_profile1);
830 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
831 true);
833 // Add second ephemeral profile but don't mark it as such yet.
834 TestingProfile* ephemeral_profile2 =
835 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
836 ASSERT_TRUE(ephemeral_profile2);
838 // Create a browser for profile1.
839 Browser::CreateParams profile1_params(normal_profile,
840 chrome::GetActiveDesktop());
841 scoped_ptr<Browser> browser1(
842 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
844 // Create browsers for the ephemeral profile.
845 Browser::CreateParams profile2_params(ephemeral_profile1,
846 chrome::GetActiveDesktop());
847 scoped_ptr<Browser> browser2(
848 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
850 Browser::CreateParams profile3_params(ephemeral_profile2,
851 chrome::GetActiveDesktop());
852 scoped_ptr<Browser> browser3(
853 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
855 std::vector<Profile*> last_opened_profiles =
856 profile_manager->GetLastOpenedProfiles();
857 ASSERT_EQ(2U, last_opened_profiles.size());
858 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
859 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
861 // Mark the second profile ephemeral.
862 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
863 true);
865 // Simulate a shutdown.
866 content::NotificationService::current()->Notify(
867 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
868 content::NotificationService::AllSources(),
869 content::NotificationService::NoDetails());
870 browser1.reset();
871 browser2.reset();
872 browser3.reset();
874 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
875 ASSERT_EQ(1U, last_opened_profiles.size());
876 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
879 TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
880 // Create two profiles, one of them ephemeral.
881 ProfileManager* profile_manager = g_browser_process->profile_manager();
882 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
883 ASSERT_EQ(0u, cache.GetNumberOfProfiles());
885 const std::string profile_name1 = "Homer";
886 base::FilePath path1 = temp_dir_.path().AppendASCII(profile_name1);
887 cache.AddProfileToCache(path1, base::UTF8ToUTF16(profile_name1),
888 std::string(), base::UTF8ToUTF16(profile_name1), 0,
889 std::string());
890 cache.SetProfileIsEphemeralAtIndex(0, true);
891 ASSERT_TRUE(base::CreateDirectory(path1));
893 const std::string profile_name2 = "Marge";
894 base::FilePath path2 = temp_dir_.path().AppendASCII(profile_name2);
895 cache.AddProfileToCache(path2, base::UTF8ToUTF16(profile_name2),
896 std::string(), base::UTF8ToUTF16(profile_name2), 0,
897 std::string());
898 ASSERT_EQ(2u, cache.GetNumberOfProfiles());
899 ASSERT_TRUE(base::CreateDirectory(path2));
901 // Set the active profile.
902 PrefService* local_state = g_browser_process->local_state();
903 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
905 profile_manager->CleanUpEphemeralProfiles();
906 base::RunLoop().RunUntilIdle();
908 // The ephemeral profile should be deleted, and the last used profile set to
909 // the other one.
910 EXPECT_FALSE(base::DirectoryExists(path1));
911 EXPECT_TRUE(base::DirectoryExists(path2));
912 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
913 ASSERT_EQ(1u, cache.GetNumberOfProfiles());
915 // Mark the remaining profile ephemeral and clean up.
916 cache.SetProfileIsEphemeralAtIndex(0, true);
917 profile_manager->CleanUpEphemeralProfiles();
918 base::RunLoop().RunUntilIdle();
920 // The profile should be deleted, and the last used profile set to a new one.
921 EXPECT_FALSE(base::DirectoryExists(path2));
922 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
923 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
926 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
927 ProfileManager* profile_manager = g_browser_process->profile_manager();
928 ASSERT_TRUE(profile_manager);
930 // Create and load two profiles.
931 const std::string profile_name1 = "New Profile 1";
932 const std::string profile_name2 = "New Profile 2";
933 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
934 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
936 MockObserver mock_observer;
937 EXPECT_CALL(mock_observer, OnProfileCreated(
938 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
940 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
941 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
942 base::RunLoop().RunUntilIdle();
944 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
945 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
947 // Set the active profile.
948 PrefService* local_state = g_browser_process->local_state();
949 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
951 // Delete the active profile.
952 profile_manager->ScheduleProfileForDeletion(dest_path1,
953 ProfileManager::CreateCallback());
954 // Spin the message loop so that all the callbacks can finish running.
955 base::RunLoop().RunUntilIdle();
957 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
958 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
961 TEST_F(ProfileManagerTest, LastProfileDeleted) {
962 ProfileManager* profile_manager = g_browser_process->profile_manager();
963 ASSERT_TRUE(profile_manager);
965 // Create and load a profile.
966 const std::string profile_name1 = "New Profile 1";
967 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
969 MockObserver mock_observer;
970 EXPECT_CALL(mock_observer, OnProfileCreated(
971 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
973 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
974 base::RunLoop().RunUntilIdle();
976 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
977 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
979 // Set it as the active profile.
980 PrefService* local_state = g_browser_process->local_state();
981 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
983 // Delete the active profile.
984 profile_manager->ScheduleProfileForDeletion(dest_path1,
985 ProfileManager::CreateCallback());
986 // Spin the message loop so that all the callbacks can finish running.
987 base::RunLoop().RunUntilIdle();
989 // A new profile should have been created
990 const std::string profile_name2 = "Profile 1";
991 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
993 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
994 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
995 EXPECT_EQ(dest_path2,
996 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
999 TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1000 ProfileManager* profile_manager = g_browser_process->profile_manager();
1001 ASSERT_TRUE(profile_manager);
1003 // Create and load a profile.
1004 const std::string profile_name1 = "New Profile 1";
1005 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1007 MockObserver mock_observer;
1008 EXPECT_CALL(mock_observer, OnProfileCreated(
1009 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1011 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1012 base::RunLoop().RunUntilIdle();
1014 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1015 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1017 // Create the profile and register it.
1018 const std::string guest_profile_name =
1019 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1021 TestingProfile::Builder builder;
1022 builder.SetGuestSession();
1023 builder.SetPath(ProfileManager::GetGuestProfilePath());
1024 TestingProfile* guest_profile = builder.Build().release();
1025 guest_profile->set_profile_name(guest_profile_name);
1026 // Registering the profile passes ownership to the ProfileManager.
1027 profile_manager->RegisterTestingProfile(guest_profile, false, false);
1029 // The Guest profile does not get added to the ProfileInfoCache.
1030 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
1031 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1033 // Set the Guest profile as the active profile.
1034 PrefService* local_state = g_browser_process->local_state();
1035 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1037 // Delete the other profile.
1038 profile_manager->ScheduleProfileForDeletion(dest_path1,
1039 ProfileManager::CreateCallback());
1040 // Spin the message loop so that all the callbacks can finish running.
1041 base::RunLoop().RunUntilIdle();
1043 // A new profile should have been created.
1044 const std::string profile_name2 = "Profile 1";
1045 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1047 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
1048 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1049 EXPECT_EQ(dest_path2,
1050 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
1053 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1054 if (!profiles::IsMultipleProfilesEnabled())
1055 return;
1057 // The command line is reset at the end of every test by the test suite.
1058 switches::EnableNewAvatarMenuForTesting(
1059 base::CommandLine::ForCurrentProcess());
1061 ProfileManager* profile_manager = g_browser_process->profile_manager();
1062 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1063 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1065 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1066 const base::string16 default_profile_name =
1067 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1068 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1069 Profile* profile1 = AddProfileToCache(profile_manager,
1070 "path_1", profile_name1);
1071 EXPECT_EQ(default_profile_name,
1072 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1074 // Multiple profiles means displaying the actual profile names.
1075 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1076 Profile* profile2 = AddProfileToCache(profile_manager,
1077 "path_2", profile_name2);
1078 EXPECT_EQ(profile_name1,
1079 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1080 EXPECT_EQ(profile_name2,
1081 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1083 // Deleting a profile means returning to the default name.
1084 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1085 ProfileManager::CreateCallback());
1086 // Spin the message loop so that all the callbacks can finish running.
1087 base::RunLoop().RunUntilIdle();
1088 EXPECT_EQ(default_profile_name,
1089 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1092 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1093 if (!profiles::IsMultipleProfilesEnabled())
1094 return;
1096 // The command line is reset at the end of every test by the test suite.
1097 switches::EnableNewAvatarMenuForTesting(
1098 base::CommandLine::ForCurrentProcess());
1100 ProfileManager* profile_manager = g_browser_process->profile_manager();
1101 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1102 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1104 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1105 const base::string16 default_profile_name =
1106 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1107 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1108 Profile* profile1 = AddProfileToCache(profile_manager,
1109 "path_1", profile_name1);
1110 EXPECT_EQ(default_profile_name,
1111 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1113 // We should display custom names for local profiles.
1114 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
1115 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
1116 cache.SetProfileIsUsingDefaultNameAtIndex(0, false);
1117 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
1118 EXPECT_EQ(custom_profile_name,
1119 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1121 // Multiple profiles means displaying the actual profile names.
1122 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1123 Profile* profile2 = AddProfileToCache(profile_manager,
1124 "path_2", profile_name2);
1125 EXPECT_EQ(custom_profile_name,
1126 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1127 EXPECT_EQ(profile_name2,
1128 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1130 // Deleting a profile means returning to the original, custom name.
1131 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1132 ProfileManager::CreateCallback());
1133 // Spin the message loop so that all the callbacks can finish running.
1134 base::RunLoop().RunUntilIdle();
1135 EXPECT_EQ(custom_profile_name,
1136 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1139 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1140 if (!profiles::IsMultipleProfilesEnabled())
1141 return;
1143 // The command line is reset at the end of every test by the test suite.
1144 switches::EnableNewAvatarMenuForTesting(
1145 base::CommandLine::ForCurrentProcess());
1147 ProfileManager* profile_manager = g_browser_process->profile_manager();
1148 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1149 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1151 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1152 const base::string16 default_profile_name =
1153 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1154 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1155 Profile* profile1 = AddProfileToCache(profile_manager,
1156 "path_1", profile_name1);
1157 EXPECT_EQ(default_profile_name,
1158 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1160 // For a signed in profile with a default name we still display
1161 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
1162 cache.SetAuthInfoOfProfileAtIndex(0, "12345", ASCIIToUTF16("user@gmail.com"));
1163 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
1164 EXPECT_EQ(default_profile_name,
1165 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1167 // For a signed in profile with a non-default Gaia given name we display the
1168 // Gaia given name.
1169 cache.SetAuthInfoOfProfileAtIndex(0, "12345", ASCIIToUTF16("user@gmail.com"));
1170 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
1171 cache.SetGAIAGivenNameOfProfileAtIndex(0, gaia_given_name);
1172 EXPECT_EQ(gaia_given_name, cache.GetNameOfProfileAtIndex(0));
1173 EXPECT_EQ(gaia_given_name,
1174 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1176 // Multiple profiles means displaying the actual profile names.
1177 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1178 Profile* profile2 = AddProfileToCache(profile_manager,
1179 "path_2", profile_name2);
1180 EXPECT_EQ(gaia_given_name,
1181 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1182 EXPECT_EQ(profile_name2,
1183 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1185 // Deleting a profile means returning to the original, actual profile name.
1186 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1187 ProfileManager::CreateCallback());
1188 // Spin the message loop so that all the callbacks can finish running.
1189 base::RunLoop().RunUntilIdle();
1190 EXPECT_EQ(gaia_given_name,
1191 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1194 TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1195 if (!profiles::IsMultipleProfilesEnabled())
1196 return;
1198 // The command line is reset at the end of every test by the test suite.
1199 switches::EnableNewAvatarMenuForTesting(
1200 base::CommandLine::ForCurrentProcess());
1202 ProfileManager* profile_manager = g_browser_process->profile_manager();
1203 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1204 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1206 // Create two signed in profiles, with both new and legacy default names, and
1207 // a profile with a custom name.
1208 Profile* profile1 = AddProfileToCache(
1209 profile_manager, "path_1", ASCIIToUTF16("Person 1"));
1210 Profile* profile2 = AddProfileToCache(
1211 profile_manager, "path_2", ASCIIToUTF16("Default Profile"));
1212 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
1213 Profile* profile3 = AddProfileToCache(
1214 profile_manager, "path_3", profile_name3);
1215 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1217 // Sign in all profiles, and make sure they do not have a Gaia name set.
1218 const base::string16 email1(ASCIIToUTF16("user1@gmail.com"));
1219 const base::string16 email2(ASCIIToUTF16("user2@gmail.com"));
1220 const base::string16 email3(ASCIIToUTF16("user3@gmail.com"));
1222 int index = cache.GetIndexOfProfileWithPath(profile1->GetPath());
1223 cache.SetAuthInfoOfProfileAtIndex(index, "12345", email1);
1224 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1225 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1227 // This may resort the cache, so be extra cautious to use the right profile.
1228 index = cache.GetIndexOfProfileWithPath(profile2->GetPath());
1229 cache.SetAuthInfoOfProfileAtIndex(index, "23456", email2);
1230 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1231 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1233 index = cache.GetIndexOfProfileWithPath(profile3->GetPath());
1234 cache.SetAuthInfoOfProfileAtIndex(index, "34567", email3);
1235 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1236 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1238 // The profiles with default names should display the email address.
1239 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1240 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1242 // The profile with the custom name should display that.
1243 EXPECT_EQ(profile_name3,
1244 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1246 // Adding a Gaia name to a profile that previously had a default name should
1247 // start displaying it.
1248 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
1249 cache.SetGAIAGivenNameOfProfileAtIndex(
1250 cache.GetIndexOfProfileWithPath(profile1->GetPath()), gaia_given_name);
1251 EXPECT_EQ(gaia_given_name,
1252 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1254 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1256 #if defined(OS_MACOSX)
1257 // These tests are for a Mac-only code path that assumes the browser
1258 // process isn't killed when all browser windows are closed.
1259 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1260 ProfileManager* profile_manager = g_browser_process->profile_manager();
1261 ASSERT_TRUE(profile_manager);
1263 // Create and load one profile, and just create a second profile.
1264 const std::string profile_name1 = "New Profile 1";
1265 const std::string profile_name2 = "New Profile 2";
1266 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1267 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1269 MockObserver mock_observer;
1270 EXPECT_CALL(mock_observer, OnProfileCreated(
1271 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1272 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1273 base::RunLoop().RunUntilIdle();
1275 // Track the profile, but don't load it.
1276 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1277 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1278 base::string16(), 0, std::string());
1279 base::RunLoop().RunUntilIdle();
1281 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1282 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
1284 // Set the active profile.
1285 PrefService* local_state = g_browser_process->local_state();
1286 local_state->SetString(prefs::kProfileLastUsed,
1287 dest_path1.BaseName().MaybeAsASCII());
1289 // Delete the active profile. This should switch and load the unloaded
1290 // profile.
1291 profile_manager->ScheduleProfileForDeletion(dest_path1,
1292 ProfileManager::CreateCallback());
1294 // Spin the message loop so that all the callbacks can finish running.
1295 base::RunLoop().RunUntilIdle();
1297 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1298 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1301 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1302 // by simulating a scenario in which the profile that is being loaded as
1303 // the next active profile has also been marked for deletion, so the
1304 // ProfileManager needs to recursively select a different next profile.
1305 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1306 ProfileManager* profile_manager = g_browser_process->profile_manager();
1307 ASSERT_TRUE(profile_manager);
1309 // Create and load one profile, and create two more profiles.
1310 const std::string profile_name1 = "New Profile 1";
1311 const std::string profile_name2 = "New Profile 2";
1312 const std::string profile_name3 = "New Profile 3";
1313 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1314 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1315 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
1317 MockObserver mock_observer;
1318 EXPECT_CALL(mock_observer, OnProfileCreated(
1319 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1320 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1321 base::RunLoop().RunUntilIdle();
1323 // Create the other profiles, but don't load them. Assign a fake avatar icon
1324 // to ensure that profiles in the info cache are sorted by the profile name,
1325 // and not randomly by the avatar name.
1326 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1327 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1328 "23456", ASCIIToUTF16(profile_name2), 1,
1329 std::string());
1330 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
1331 "34567", ASCIIToUTF16(profile_name3), 2,
1332 std::string());
1334 base::RunLoop().RunUntilIdle();
1336 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1337 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1339 // Set the active profile.
1340 PrefService* local_state = g_browser_process->local_state();
1341 local_state->SetString(prefs::kProfileLastUsed,
1342 dest_path1.BaseName().MaybeAsASCII());
1344 // Delete the active profile, Profile1.
1345 // This will post a CreateProfileAsync message, that tries to load Profile2,
1346 // which checks that the profile is not being deleted, and then calls back
1347 // FinishDeletingProfile for Profile1.
1348 // Try to break this flow by setting the active profile to Profile2 in the
1349 // middle (so after the first posted message), and trying to delete Profile2,
1350 // so that the ProfileManager has to look for a different profile to load.
1351 profile_manager->ScheduleProfileForDeletion(dest_path1,
1352 ProfileManager::CreateCallback());
1353 local_state->SetString(prefs::kProfileLastUsed,
1354 dest_path2.BaseName().MaybeAsASCII());
1355 profile_manager->ScheduleProfileForDeletion(dest_path2,
1356 ProfileManager::CreateCallback());
1357 // Spin the message loop so that all the callbacks can finish running.
1358 base::RunLoop().RunUntilIdle();
1360 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1361 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1363 #endif // !defined(OS_MACOSX)