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/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.h"
19 #include "chrome/browser/history/history_service_factory.h"
20 #include "chrome/browser/io_thread.h"
21 #include "chrome/browser/prefs/browser_prefs.h"
22 #include "chrome/browser/prefs/incognito_mode_prefs.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
25 #include "chrome/browser/profiles/profile_info_cache.h"
26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/profiles/profiles_state.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/grit/generated_resources.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 "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_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/profiles/profile_helper.h"
51 #include "chrome/browser/chromeos/settings/cros_settings.h"
52 #include "chrome/browser/chromeos/settings/device_settings_service.h"
53 #include "chromeos/chromeos_switches.h"
54 #include "chromeos/login/user_names.h"
55 #include "components/user_manager/user_manager.h"
56 #endif // defined(OS_CHROMEOS)
58 using base::ASCIIToUTF16
;
59 using content::BrowserThread
;
63 // This global variable is used to check that value returned to different
64 // observers is the same.
65 Profile
* g_created_profile
;
67 class UnittestProfileManager
: public ::ProfileManagerWithoutInit
{
69 explicit UnittestProfileManager(const base::FilePath
& user_data_dir
)
70 : ::ProfileManagerWithoutInit(user_data_dir
) {}
73 Profile
* CreateProfileHelper(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 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 void SetUp() override
{
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 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
114 cl
->AppendSwitch(switches::kTestType
);
118 void TearDown() override
{
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 // Helper function to register an user with id |user_id| and create profile
153 // with a correct path.
154 void RegisterUser(const std::string
& user_id
) {
155 chromeos::ProfileHelper
* profile_helper
= chromeos::ProfileHelper::Get();
156 const std::string user_id_hash
=
157 profile_helper
->GetUserIdHashByUserIdForTesting(user_id
);
158 user_manager::UserManager::Get()->UserLoggedIn(
159 user_id
, user_id_hash
, false);
160 g_browser_process
->profile_manager()->GetProfile(
161 profile_helper
->GetProfilePathByUserIdHash(user_id_hash
));
164 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
165 chromeos::ScopedTestCrosSettings test_cros_settings_
;
168 // The path to temporary directory used to contain the test operations.
169 base::ScopedTempDir temp_dir_
;
170 ScopedTestingLocalState local_state_
;
172 content::TestBrowserThreadBundle thread_bundle_
;
174 #if defined(OS_CHROMEOS)
175 chromeos::ScopedTestUserManager test_user_manager_
;
178 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest
);
181 TEST_F(ProfileManagerTest
, GetProfile
) {
182 base::FilePath dest_path
= temp_dir_
.path();
183 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
185 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
187 // Successfully create a profile.
188 Profile
* profile
= profile_manager
->GetProfile(dest_path
);
189 EXPECT_TRUE(profile
);
191 // The profile already exists when we call GetProfile. Just load it.
192 EXPECT_EQ(profile
, profile_manager
->GetProfile(dest_path
));
195 TEST_F(ProfileManagerTest
, DefaultProfileDir
) {
196 base::FilePath expected_default
=
197 base::FilePath().AppendASCII(chrome::kInitialProfile
);
199 expected_default
.value(),
200 g_browser_process
->profile_manager()->GetInitialProfileDir().value());
203 MATCHER(NotFail
, "Profile creation failure status is not reported.") {
204 return arg
== Profile::CREATE_STATUS_CREATED
||
205 arg
== Profile::CREATE_STATUS_INITIALIZED
;
208 MATCHER(SameNotNull
, "The same non-NULL value for all calls.") {
209 if (!g_created_profile
)
210 g_created_profile
= arg
;
211 return arg
!= NULL
&& arg
== g_created_profile
;
214 #if defined(OS_CHROMEOS)
216 // This functionality only exists on Chrome OS.
217 TEST_F(ProfileManagerTest
, LoggedInProfileDir
) {
218 base::FilePath expected_default
=
219 base::FilePath().AppendASCII(chrome::kInitialProfile
);
220 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
221 EXPECT_EQ(expected_default
.value(),
222 profile_manager
->GetInitialProfileDir().value());
224 const char kTestUserName
[] = "test-user@example.com";
225 chromeos::FakeUserManager
* user_manager
= new chromeos::FakeUserManager();
226 chromeos::ScopedUserManagerEnabler
enabler(user_manager
);
228 const user_manager::User
* active_user
= user_manager
->AddUser(kTestUserName
);
229 user_manager
->LoginUser(kTestUserName
);
230 user_manager
->SwitchActiveUser(kTestUserName
);
232 profile_manager
->Observe(
233 chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
234 content::NotificationService::AllSources(),
235 content::Details
<const user_manager::User
>(active_user
));
236 base::FilePath
expected_logged_in(
237 chromeos::ProfileHelper::GetUserProfileDir(active_user
->username_hash()));
238 EXPECT_EQ(expected_logged_in
.value(),
239 profile_manager
->GetInitialProfileDir().value());
240 VLOG(1) << temp_dir_
.path().Append(
241 profile_manager
->GetInitialProfileDir()).value();
246 TEST_F(ProfileManagerTest
, CreateAndUseTwoProfiles
) {
247 base::FilePath dest_path1
= temp_dir_
.path();
248 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
250 base::FilePath dest_path2
= temp_dir_
.path();
251 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
253 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
255 // Successfully create the profiles.
256 TestingProfile
* profile1
=
257 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
258 ASSERT_TRUE(profile1
);
260 TestingProfile
* profile2
=
261 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
262 ASSERT_TRUE(profile2
);
264 // Force lazy-init of some profile services to simulate use.
265 ASSERT_TRUE(profile1
->CreateHistoryService(true, false));
266 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
267 profile1
, ServiceAccessType::EXPLICIT_ACCESS
));
268 profile1
->CreateBookmarkModel(true);
269 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1
));
270 profile2
->CreateBookmarkModel(true);
271 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2
));
272 ASSERT_TRUE(profile2
->CreateHistoryService(true, false));
273 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
274 profile2
, ServiceAccessType::EXPLICIT_ACCESS
));
276 // Make sure any pending tasks run before we destroy the profiles.
277 base::RunLoop().RunUntilIdle();
279 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
281 // Make sure history cleans up correctly.
282 base::RunLoop().RunUntilIdle();
285 TEST_F(ProfileManagerTest
, CreateProfileAsyncMultipleRequests
) {
286 g_created_profile
= NULL
;
288 MockObserver mock_observer1
;
289 EXPECT_CALL(mock_observer1
, OnProfileCreated(
290 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
291 MockObserver mock_observer2
;
292 EXPECT_CALL(mock_observer2
, OnProfileCreated(
293 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
294 MockObserver mock_observer3
;
295 EXPECT_CALL(mock_observer3
, OnProfileCreated(
296 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
298 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
299 const std::string profile_name
= "New Profile";
300 CreateProfileAsync(profile_manager
, profile_name
, false, &mock_observer1
);
301 CreateProfileAsync(profile_manager
, profile_name
, false, &mock_observer2
);
302 CreateProfileAsync(profile_manager
, profile_name
, false, &mock_observer3
);
304 base::RunLoop().RunUntilIdle();
307 TEST_F(ProfileManagerTest
, CreateProfilesAsync
) {
308 const std::string profile_name1
= "New Profile 1";
309 const std::string profile_name2
= "New Profile 2";
311 MockObserver mock_observer
;
312 EXPECT_CALL(mock_observer
, OnProfileCreated(
313 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
315 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
317 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
318 CreateProfileAsync(profile_manager
, profile_name2
, false, &mock_observer
);
320 base::RunLoop().RunUntilIdle();
323 TEST_F(ProfileManagerTest
, CreateProfileAsyncCheckOmitted
) {
324 std::string name
= "0 Supervised Profile";
326 MockObserver mock_observer
;
327 EXPECT_CALL(mock_observer
, OnProfileCreated(
328 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
330 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
331 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
332 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
334 CreateProfileAsync(profile_manager
, name
, true, &mock_observer
);
335 base::RunLoop().RunUntilIdle();
337 EXPECT_EQ(1u, cache
.GetNumberOfProfiles());
338 // Supervised profiles should start out omitted from the profile list.
339 EXPECT_TRUE(cache
.IsOmittedProfileAtIndex(0));
341 name
= "1 Regular Profile";
342 CreateProfileAsync(profile_manager
, name
, false, &mock_observer
);
343 base::RunLoop().RunUntilIdle();
345 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
346 // Non-supervised profiles should be included in the profile list.
347 EXPECT_FALSE(cache
.IsOmittedProfileAtIndex(1));
350 TEST_F(ProfileManagerTest
, AddProfileToCacheCheckOmitted
) {
351 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
352 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
353 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
355 const base::FilePath supervised_path
=
356 temp_dir_
.path().AppendASCII("Supervised");
357 TestingProfile
* supervised_profile
=
358 new TestingProfile(supervised_path
, NULL
);
359 supervised_profile
->GetPrefs()->SetString(prefs::kSupervisedUserId
, "An ID");
361 // RegisterTestingProfile adds the profile to the cache and takes ownership.
362 profile_manager
->RegisterTestingProfile(supervised_profile
, true, false);
363 EXPECT_EQ(1u, cache
.GetNumberOfProfiles());
364 EXPECT_TRUE(cache
.IsOmittedProfileAtIndex(0));
366 const base::FilePath nonsupervised_path
= temp_dir_
.path().AppendASCII(
368 TestingProfile
* nonsupervised_profile
= new TestingProfile(nonsupervised_path
,
370 profile_manager
->RegisterTestingProfile(nonsupervised_profile
, true, false);
372 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
373 size_t supervised_index
= cache
.GetIndexOfProfileWithPath(supervised_path
);
374 EXPECT_TRUE(cache
.IsOmittedProfileAtIndex(supervised_index
));
375 size_t nonsupervised_index
=
376 cache
.GetIndexOfProfileWithPath(nonsupervised_path
);
377 EXPECT_FALSE(cache
.IsOmittedProfileAtIndex(nonsupervised_index
));
380 TEST_F(ProfileManagerTest
, GetGuestProfilePath
) {
381 base::FilePath guest_path
= ProfileManager::GetGuestProfilePath();
382 base::FilePath expected_path
= temp_dir_
.path();
383 expected_path
= expected_path
.Append(chrome::kGuestProfileDir
);
384 EXPECT_EQ(expected_path
, guest_path
);
387 class UnittestGuestProfileManager
: public UnittestProfileManager
{
389 explicit UnittestGuestProfileManager(const base::FilePath
& user_data_dir
)
390 : UnittestProfileManager(user_data_dir
) {}
393 Profile
* CreateProfileHelper(const base::FilePath
& file_path
) override
{
394 TestingProfile::Builder builder
;
395 builder
.SetGuestSession();
396 builder
.SetPath(file_path
);
397 TestingProfile
* testing_profile
= builder
.Build().release();
398 return testing_profile
;
402 class ProfileManagerGuestTest
: public ProfileManagerTest
{
404 void SetUp() override
{
405 // Create a new temporary directory, and store the path
406 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
407 TestingBrowserProcess::GetGlobal()->SetProfileManager(
408 new UnittestGuestProfileManager(temp_dir_
.path()));
410 #if defined(OS_CHROMEOS)
411 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
412 // This switch is needed to skip non-test specific behavior in
413 // ProfileManager (accessing DBusThreadManager).
414 cl
->AppendSwitch(switches::kTestType
);
416 cl
->AppendSwitch(chromeos::switches::kGuestSession
);
417 cl
->AppendSwitch(::switches::kIncognito
);
419 RegisterUser(chromeos::login::kGuestUserName
);
424 TEST_F(ProfileManagerGuestTest
, GetLastUsedProfileAllowedByPolicy
) {
425 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
426 ASSERT_TRUE(profile_manager
);
428 Profile
* profile
= profile_manager
->GetLastUsedProfileAllowedByPolicy();
429 ASSERT_TRUE(profile
);
430 EXPECT_TRUE(profile
->IsOffTheRecord());
433 #if defined(OS_CHROMEOS)
434 TEST_F(ProfileManagerGuestTest
, GuestProfileIngonito
) {
435 Profile
* primary_profile
= ProfileManager::GetPrimaryUserProfile();
436 EXPECT_TRUE(primary_profile
->IsOffTheRecord());
438 Profile
* active_profile
= ProfileManager::GetActiveUserProfile();
439 EXPECT_TRUE(active_profile
->IsOffTheRecord());
441 EXPECT_TRUE(active_profile
->IsSameProfile(primary_profile
));
443 Profile
* last_used_profile
= ProfileManager::GetLastUsedProfile();
444 EXPECT_TRUE(last_used_profile
->IsOffTheRecord());
446 EXPECT_TRUE(last_used_profile
->IsSameProfile(active_profile
));
450 TEST_F(ProfileManagerTest
, AutoloadProfilesWithBackgroundApps
) {
451 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
452 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
453 local_state_
.Get()->SetUserPref(prefs::kBackgroundModeEnabled
,
454 new base::FundamentalValue(true));
456 // Setting a pref which is not applicable to a system (i.e., Android in this
457 // case) does not necessarily create it. Don't bother continuing with the
458 // test if this pref doesn't exist because it will not load the profiles if
459 // it cannot verify that the pref for background mode is enabled.
460 if (!local_state_
.Get()->HasPrefPath(prefs::kBackgroundModeEnabled
))
463 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
464 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_1"),
465 ASCIIToUTF16("name_1"), base::string16(), 0,
467 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_2"),
468 ASCIIToUTF16("name_2"), base::string16(), 0,
470 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_3"),
471 ASCIIToUTF16("name_3"), base::string16(), 0,
473 cache
.SetBackgroundStatusOfProfileAtIndex(0, true);
474 cache
.SetBackgroundStatusOfProfileAtIndex(2, true);
475 EXPECT_EQ(3u, cache
.GetNumberOfProfiles());
477 profile_manager
->AutoloadProfiles();
479 EXPECT_EQ(2u, profile_manager
->GetLoadedProfiles().size());
482 TEST_F(ProfileManagerTest
, DoNotAutoloadProfilesIfBackgroundModeOff
) {
483 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
484 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
485 local_state_
.Get()->SetUserPref(prefs::kBackgroundModeEnabled
,
486 new base::FundamentalValue(false));
488 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
489 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_1"),
490 ASCIIToUTF16("name_1"), base::string16(), 0,
492 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_2"),
493 ASCIIToUTF16("name_2"), base::string16(), 0,
495 cache
.SetBackgroundStatusOfProfileAtIndex(0, false);
496 cache
.SetBackgroundStatusOfProfileAtIndex(1, true);
497 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
499 profile_manager
->AutoloadProfiles();
501 EXPECT_EQ(0u, profile_manager
->GetLoadedProfiles().size());
504 TEST_F(ProfileManagerTest
, InitProfileUserPrefs
) {
505 base::FilePath dest_path
= temp_dir_
.path();
506 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
508 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
512 // Successfully create the profile
513 profile
= profile_manager
->GetProfile(dest_path
);
514 ASSERT_TRUE(profile
);
516 // Check that the profile name is non empty
517 std::string profile_name
=
518 profile
->GetPrefs()->GetString(prefs::kProfileName
);
519 EXPECT_FALSE(profile_name
.empty());
521 // Check that the profile avatar index is valid
522 size_t avatar_index
=
523 profile
->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex
);
524 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
528 // Tests that a new profile's entry in the profile info cache is setup with the
529 // same values that are in the profile prefs.
530 TEST_F(ProfileManagerTest
, InitProfileInfoCacheForAProfile
) {
531 base::FilePath dest_path
= temp_dir_
.path();
532 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
534 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
535 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
537 // Successfully create the profile
538 Profile
* profile
= profile_manager
->GetProfile(dest_path
);
539 ASSERT_TRUE(profile
);
541 std::string profile_name
=
542 profile
->GetPrefs()->GetString(prefs::kProfileName
);
543 size_t avatar_index
=
544 profile
->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex
);
546 size_t profile_index
= cache
.GetIndexOfProfileWithPath(dest_path
);
548 // Check if the profile prefs are the same as the cache prefs
549 EXPECT_EQ(profile_name
,
550 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
551 EXPECT_EQ(avatar_index
,
552 cache
.GetAvatarIconIndexOfProfileAtIndex(profile_index
));
555 TEST_F(ProfileManagerTest
, GetLastUsedProfileAllowedByPolicy
) {
556 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
557 ASSERT_TRUE(profile_manager
);
559 #if defined(OS_CHROMEOS)
560 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that
561 // is forced to be incognito. That's why we need to create at least one user
562 // to get a regular profile.
563 RegisterUser("test-user@example.com");
566 Profile
* profile
= profile_manager
->GetLastUsedProfileAllowedByPolicy();
567 ASSERT_TRUE(profile
);
568 EXPECT_FALSE(profile
->IsOffTheRecord());
569 PrefService
* prefs
= profile
->GetPrefs();
570 EXPECT_EQ(IncognitoModePrefs::ENABLED
,
571 IncognitoModePrefs::GetAvailability(prefs
));
573 ASSERT_TRUE(profile
->GetOffTheRecordProfile());
575 IncognitoModePrefs::SetAvailability(prefs
, IncognitoModePrefs::DISABLED
);
577 profile_manager
->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
579 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
580 // incognito mode is forced.
581 IncognitoModePrefs::SetAvailability(prefs
, IncognitoModePrefs::FORCED
);
583 profile_manager
->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
586 #if !defined(OS_ANDROID)
587 // There's no Browser object on Android.
588 TEST_F(ProfileManagerTest
, LastOpenedProfiles
) {
589 base::FilePath dest_path1
= temp_dir_
.path();
590 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
592 base::FilePath dest_path2
= temp_dir_
.path();
593 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
595 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
597 // Successfully create the profiles.
598 TestingProfile
* profile1
=
599 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
600 ASSERT_TRUE(profile1
);
602 TestingProfile
* profile2
=
603 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
604 ASSERT_TRUE(profile2
);
606 std::vector
<Profile
*> last_opened_profiles
=
607 profile_manager
->GetLastOpenedProfiles();
608 ASSERT_EQ(0U, last_opened_profiles
.size());
610 // Create a browser for profile1.
611 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
612 scoped_ptr
<Browser
> browser1a(
613 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
615 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
616 ASSERT_EQ(1U, last_opened_profiles
.size());
617 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
620 Browser::CreateParams
profile2_params(profile2
, chrome::GetActiveDesktop());
621 scoped_ptr
<Browser
> browser2(
622 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
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]);
629 // Adding more browsers doesn't change anything.
630 scoped_ptr
<Browser
> browser1b(
631 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
632 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
633 ASSERT_EQ(2U, last_opened_profiles
.size());
634 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
635 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
637 // Close the browsers.
639 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
640 ASSERT_EQ(2U, last_opened_profiles
.size());
641 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
642 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
645 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
646 ASSERT_EQ(1U, last_opened_profiles
.size());
647 EXPECT_EQ(profile2
, last_opened_profiles
[0]);
650 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
651 ASSERT_EQ(0U, last_opened_profiles
.size());
654 TEST_F(ProfileManagerTest
, LastOpenedProfilesAtShutdown
) {
655 base::FilePath dest_path1
= temp_dir_
.path();
656 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
658 base::FilePath dest_path2
= temp_dir_
.path();
659 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
661 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
663 // Successfully create the profiles.
664 TestingProfile
* profile1
=
665 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
666 ASSERT_TRUE(profile1
);
668 TestingProfile
* profile2
=
669 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
670 ASSERT_TRUE(profile2
);
672 // Create a browser for profile1.
673 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
674 scoped_ptr
<Browser
> browser1(
675 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
678 Browser::CreateParams
profile2_params(profile2
, chrome::GetActiveDesktop());
679 scoped_ptr
<Browser
> browser2(
680 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
682 std::vector
<Profile
*> last_opened_profiles
=
683 profile_manager
->GetLastOpenedProfiles();
684 ASSERT_EQ(2U, last_opened_profiles
.size());
685 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
686 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
688 // Simulate a shutdown.
689 content::NotificationService::current()->Notify(
690 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
691 content::NotificationService::AllSources(),
692 content::NotificationService::NoDetails());
694 // Even if the browsers are destructed during shutdown, the profiles stay
699 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
700 ASSERT_EQ(2U, last_opened_profiles
.size());
701 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
702 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
705 TEST_F(ProfileManagerTest
, LastOpenedProfilesDoesNotContainIncognito
) {
706 base::FilePath dest_path1
= temp_dir_
.path();
707 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
708 base::FilePath dest_path2
= temp_dir_
.path();
709 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
711 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
713 // Successfully create the profiles.
714 TestingProfile
* profile1
=
715 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
716 ASSERT_TRUE(profile1
);
718 std::vector
<Profile
*> last_opened_profiles
=
719 profile_manager
->GetLastOpenedProfiles();
720 ASSERT_EQ(0U, last_opened_profiles
.size());
722 // Create a browser for profile1.
723 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
724 scoped_ptr
<Browser
> browser1(
725 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
727 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
728 ASSERT_EQ(1U, last_opened_profiles
.size());
729 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
732 Browser::CreateParams
profile2_params(profile1
->GetOffTheRecordProfile(),
733 chrome::GetActiveDesktop());
734 scoped_ptr
<Browser
> browser2a(
735 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
737 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
738 ASSERT_EQ(1U, last_opened_profiles
.size());
739 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
741 // Adding more browsers doesn't change anything.
742 scoped_ptr
<Browser
> browser2b(
743 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
744 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
745 ASSERT_EQ(1U, last_opened_profiles
.size());
746 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
748 // Close the browsers.
750 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
751 ASSERT_EQ(1U, last_opened_profiles
.size());
752 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
755 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
756 ASSERT_EQ(1U, last_opened_profiles
.size());
757 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
760 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
761 ASSERT_EQ(0U, last_opened_profiles
.size());
763 #endif // !defined(OS_ANDROID)
765 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
766 // There's no Browser object on Android and there's no multi-profiles on Chrome.
767 TEST_F(ProfileManagerTest
, EphemeralProfilesDontEndUpAsLastProfile
) {
768 base::FilePath dest_path
= temp_dir_
.path();
769 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
771 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
773 TestingProfile
* profile
=
774 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path
));
775 ASSERT_TRUE(profile
);
776 profile
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
, true);
778 // Here the last used profile is still the "Default" profile.
779 Profile
* last_used_profile
= profile_manager
->GetLastUsedProfile();
780 EXPECT_NE(profile
, last_used_profile
);
782 // Create a browser for the profile.
783 Browser::CreateParams
profile_params(profile
, chrome::GetActiveDesktop());
784 scoped_ptr
<Browser
> browser(
785 chrome::CreateBrowserWithTestWindowForParams(&profile_params
));
786 last_used_profile
= profile_manager
->GetLastUsedProfile();
787 EXPECT_NE(profile
, last_used_profile
);
789 // Close the browser.
791 last_used_profile
= profile_manager
->GetLastUsedProfile();
792 EXPECT_NE(profile
, last_used_profile
);
795 TEST_F(ProfileManagerTest
, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown
) {
796 base::FilePath dest_path1
= temp_dir_
.path();
797 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("Normal Profile"));
799 base::FilePath dest_path2
= temp_dir_
.path();
800 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
802 base::FilePath dest_path3
= temp_dir_
.path();
803 dest_path3
= dest_path3
.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
805 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
807 // Successfully create the profiles.
808 TestingProfile
* normal_profile
=
809 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
810 ASSERT_TRUE(normal_profile
);
812 // Add one ephemeral profile which should not end up in this list.
813 TestingProfile
* ephemeral_profile1
=
814 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
815 ASSERT_TRUE(ephemeral_profile1
);
816 ephemeral_profile1
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
,
819 // Add second ephemeral profile but don't mark it as such yet.
820 TestingProfile
* ephemeral_profile2
=
821 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path3
));
822 ASSERT_TRUE(ephemeral_profile2
);
824 // Create a browser for profile1.
825 Browser::CreateParams
profile1_params(normal_profile
,
826 chrome::GetActiveDesktop());
827 scoped_ptr
<Browser
> browser1(
828 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
830 // Create browsers for the ephemeral profile.
831 Browser::CreateParams
profile2_params(ephemeral_profile1
,
832 chrome::GetActiveDesktop());
833 scoped_ptr
<Browser
> browser2(
834 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
836 Browser::CreateParams
profile3_params(ephemeral_profile2
,
837 chrome::GetActiveDesktop());
838 scoped_ptr
<Browser
> browser3(
839 chrome::CreateBrowserWithTestWindowForParams(&profile3_params
));
841 std::vector
<Profile
*> last_opened_profiles
=
842 profile_manager
->GetLastOpenedProfiles();
843 ASSERT_EQ(2U, last_opened_profiles
.size());
844 EXPECT_EQ(normal_profile
, last_opened_profiles
[0]);
845 EXPECT_EQ(ephemeral_profile2
, last_opened_profiles
[1]);
847 // Mark the second profile ephemeral.
848 ephemeral_profile2
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
,
851 // Simulate a shutdown.
852 content::NotificationService::current()->Notify(
853 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
854 content::NotificationService::AllSources(),
855 content::NotificationService::NoDetails());
860 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
861 ASSERT_EQ(1U, last_opened_profiles
.size());
862 EXPECT_EQ(normal_profile
, last_opened_profiles
[0]);
865 TEST_F(ProfileManagerTest
, ActiveProfileDeleted
) {
866 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
867 ASSERT_TRUE(profile_manager
);
869 // Create and load two profiles.
870 const std::string profile_name1
= "New Profile 1";
871 const std::string profile_name2
= "New Profile 2";
872 base::FilePath dest_path1
= temp_dir_
.path().AppendASCII(profile_name1
);
873 base::FilePath dest_path2
= temp_dir_
.path().AppendASCII(profile_name2
);
875 MockObserver mock_observer
;
876 EXPECT_CALL(mock_observer
, OnProfileCreated(
877 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
879 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
880 CreateProfileAsync(profile_manager
, profile_name2
, false, &mock_observer
);
881 base::RunLoop().RunUntilIdle();
883 EXPECT_EQ(2u, profile_manager
->GetLoadedProfiles().size());
884 EXPECT_EQ(2u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
886 // Set the active profile.
887 PrefService
* local_state
= g_browser_process
->local_state();
888 local_state
->SetString(prefs::kProfileLastUsed
, profile_name1
);
890 // Delete the active profile.
891 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
892 ProfileManager::CreateCallback());
893 // Spin the message loop so that all the callbacks can finish running.
894 base::RunLoop().RunUntilIdle();
896 EXPECT_EQ(dest_path2
, profile_manager
->GetLastUsedProfile()->GetPath());
897 EXPECT_EQ(profile_name2
, local_state
->GetString(prefs::kProfileLastUsed
));
900 TEST_F(ProfileManagerTest
, LastProfileDeleted
) {
901 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
902 ASSERT_TRUE(profile_manager
);
904 // Create and load a profile.
905 const std::string profile_name1
= "New Profile 1";
906 base::FilePath dest_path1
= temp_dir_
.path().AppendASCII(profile_name1
);
908 MockObserver mock_observer
;
909 EXPECT_CALL(mock_observer
, OnProfileCreated(
910 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
912 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
913 base::RunLoop().RunUntilIdle();
915 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
916 EXPECT_EQ(1u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
918 // Set it as the active profile.
919 PrefService
* local_state
= g_browser_process
->local_state();
920 local_state
->SetString(prefs::kProfileLastUsed
, profile_name1
);
922 // Delete the active profile.
923 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
924 ProfileManager::CreateCallback());
925 // Spin the message loop so that all the callbacks can finish running.
926 base::RunLoop().RunUntilIdle();
928 // A new profile should have been created
929 const std::string profile_name2
= "Profile 1";
930 base::FilePath dest_path2
= temp_dir_
.path().AppendASCII(profile_name2
);
932 EXPECT_EQ(dest_path2
, profile_manager
->GetLastUsedProfile()->GetPath());
933 EXPECT_EQ(profile_name2
, local_state
->GetString(prefs::kProfileLastUsed
));
934 EXPECT_EQ(dest_path2
,
935 profile_manager
->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
938 TEST_F(ProfileManagerTest
, LastProfileDeletedWithGuestActiveProfile
) {
939 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
940 ASSERT_TRUE(profile_manager
);
942 // Create and load a profile.
943 const std::string profile_name1
= "New Profile 1";
944 base::FilePath dest_path1
= temp_dir_
.path().AppendASCII(profile_name1
);
946 MockObserver mock_observer
;
947 EXPECT_CALL(mock_observer
, OnProfileCreated(
948 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
950 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
951 base::RunLoop().RunUntilIdle();
953 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
954 EXPECT_EQ(1u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
956 // Create the profile and register it.
957 const std::string guest_profile_name
=
958 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
960 TestingProfile::Builder builder
;
961 builder
.SetGuestSession();
962 builder
.SetPath(ProfileManager::GetGuestProfilePath());
963 TestingProfile
* guest_profile
= builder
.Build().release();
964 guest_profile
->set_profile_name(guest_profile_name
);
965 // Registering the profile passes ownership to the ProfileManager.
966 profile_manager
->RegisterTestingProfile(guest_profile
, false, false);
968 // The Guest profile does not get added to the ProfileInfoCache.
969 EXPECT_EQ(2u, profile_manager
->GetLoadedProfiles().size());
970 EXPECT_EQ(1u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
972 // Set the Guest profile as the active profile.
973 PrefService
* local_state
= g_browser_process
->local_state();
974 local_state
->SetString(prefs::kProfileLastUsed
, guest_profile_name
);
976 // Delete the other profile.
977 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
978 ProfileManager::CreateCallback());
979 // Spin the message loop so that all the callbacks can finish running.
980 base::RunLoop().RunUntilIdle();
982 // A new profile should have been created.
983 const std::string profile_name2
= "Profile 1";
984 base::FilePath dest_path2
= temp_dir_
.path().AppendASCII(profile_name2
);
986 EXPECT_EQ(3u, profile_manager
->GetLoadedProfiles().size());
987 EXPECT_EQ(1u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
988 EXPECT_EQ(dest_path2
,
989 profile_manager
->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
992 TEST_F(ProfileManagerTest
, ProfileDisplayNameResetsDefaultName
) {
993 if (!profiles::IsMultipleProfilesEnabled())
996 // The command line is reset at the end of every test by the test suite.
997 switches::EnableNewAvatarMenuForTesting(
998 base::CommandLine::ForCurrentProcess());
1000 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1001 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1002 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
1004 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1005 const base::string16 default_profile_name
=
1006 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
1007 const base::string16 profile_name1
= cache
.ChooseNameForNewProfile(0);
1008 Profile
* profile1
= AddProfileToCache(profile_manager
,
1009 "path_1", profile_name1
);
1010 EXPECT_EQ(default_profile_name
,
1011 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1013 // Multiple profiles means displaying the actual profile names.
1014 const base::string16 profile_name2
= cache
.ChooseNameForNewProfile(1);
1015 Profile
* profile2
= AddProfileToCache(profile_manager
,
1016 "path_2", profile_name2
);
1017 EXPECT_EQ(profile_name1
,
1018 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1019 EXPECT_EQ(profile_name2
,
1020 profiles::GetAvatarNameForProfile(profile2
->GetPath()));
1022 // Deleting a profile means returning to the default name.
1023 profile_manager
->ScheduleProfileForDeletion(profile2
->GetPath(),
1024 ProfileManager::CreateCallback());
1025 // Spin the message loop so that all the callbacks can finish running.
1026 base::RunLoop().RunUntilIdle();
1027 EXPECT_EQ(default_profile_name
,
1028 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1031 TEST_F(ProfileManagerTest
, ProfileDisplayNamePreservesCustomName
) {
1032 if (!profiles::IsMultipleProfilesEnabled())
1035 // The command line is reset at the end of every test by the test suite.
1036 switches::EnableNewAvatarMenuForTesting(
1037 base::CommandLine::ForCurrentProcess());
1039 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1040 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1041 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
1043 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1044 const base::string16 default_profile_name
=
1045 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
1046 const base::string16 profile_name1
= cache
.ChooseNameForNewProfile(0);
1047 Profile
* profile1
= AddProfileToCache(profile_manager
,
1048 "path_1", profile_name1
);
1049 EXPECT_EQ(default_profile_name
,
1050 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1052 // We should display custom names for local profiles.
1053 const base::string16 custom_profile_name
= ASCIIToUTF16("Batman");
1054 cache
.SetNameOfProfileAtIndex(0, custom_profile_name
);
1055 cache
.SetProfileIsUsingDefaultNameAtIndex(0, false);
1056 EXPECT_EQ(custom_profile_name
, cache
.GetNameOfProfileAtIndex(0));
1057 EXPECT_EQ(custom_profile_name
,
1058 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1060 // Multiple profiles means displaying the actual profile names.
1061 const base::string16 profile_name2
= cache
.ChooseNameForNewProfile(1);
1062 Profile
* profile2
= AddProfileToCache(profile_manager
,
1063 "path_2", profile_name2
);
1064 EXPECT_EQ(custom_profile_name
,
1065 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1066 EXPECT_EQ(profile_name2
,
1067 profiles::GetAvatarNameForProfile(profile2
->GetPath()));
1069 // Deleting a profile means returning to the original, custom name.
1070 profile_manager
->ScheduleProfileForDeletion(profile2
->GetPath(),
1071 ProfileManager::CreateCallback());
1072 // Spin the message loop so that all the callbacks can finish running.
1073 base::RunLoop().RunUntilIdle();
1074 EXPECT_EQ(custom_profile_name
,
1075 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1078 TEST_F(ProfileManagerTest
, ProfileDisplayNamePreservesSignedInName
) {
1079 if (!profiles::IsMultipleProfilesEnabled())
1082 // The command line is reset at the end of every test by the test suite.
1083 switches::EnableNewAvatarMenuForTesting(
1084 base::CommandLine::ForCurrentProcess());
1086 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1087 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1088 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
1090 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1091 const base::string16 default_profile_name
=
1092 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
1093 const base::string16 profile_name1
= cache
.ChooseNameForNewProfile(0);
1094 Profile
* profile1
= AddProfileToCache(profile_manager
,
1095 "path_1", profile_name1
);
1096 EXPECT_EQ(default_profile_name
,
1097 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1099 // For a signed in profile with a default name we still display
1100 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
1101 cache
.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("user@gmail.com"));
1102 EXPECT_EQ(profile_name1
, cache
.GetNameOfProfileAtIndex(0));
1103 EXPECT_EQ(default_profile_name
,
1104 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1106 // For a signed in profile with a non-default Gaia given name we display the
1108 cache
.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("user@gmail.com"));
1109 const base::string16
gaia_given_name(ASCIIToUTF16("given name"));
1110 cache
.SetGAIAGivenNameOfProfileAtIndex(0, gaia_given_name
);
1111 EXPECT_EQ(gaia_given_name
, cache
.GetNameOfProfileAtIndex(0));
1112 EXPECT_EQ(gaia_given_name
,
1113 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1115 // Multiple profiles means displaying the actual profile names.
1116 const base::string16 profile_name2
= cache
.ChooseNameForNewProfile(1);
1117 Profile
* profile2
= AddProfileToCache(profile_manager
,
1118 "path_2", profile_name2
);
1119 EXPECT_EQ(gaia_given_name
,
1120 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1121 EXPECT_EQ(profile_name2
,
1122 profiles::GetAvatarNameForProfile(profile2
->GetPath()));
1124 // Deleting a profile means returning to the original, actual profile name.
1125 profile_manager
->ScheduleProfileForDeletion(profile2
->GetPath(),
1126 ProfileManager::CreateCallback());
1127 // Spin the message loop so that all the callbacks can finish running.
1128 base::RunLoop().RunUntilIdle();
1129 EXPECT_EQ(gaia_given_name
,
1130 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1133 TEST_F(ProfileManagerTest
, ProfileDisplayNameIsEmailIfDefaultName
) {
1134 if (!profiles::IsMultipleProfilesEnabled())
1137 // The command line is reset at the end of every test by the test suite.
1138 switches::EnableNewAvatarMenuForTesting(
1139 base::CommandLine::ForCurrentProcess());
1141 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1142 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1143 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
1145 // Create two signed in profiles, with both new and legacy default names, and
1146 // a profile with a custom name.
1147 Profile
* profile1
= AddProfileToCache(
1148 profile_manager
, "path_1", ASCIIToUTF16("Person 1"));
1149 Profile
* profile2
= AddProfileToCache(
1150 profile_manager
, "path_2", ASCIIToUTF16("Default Profile"));
1151 const base::string16
profile_name3(ASCIIToUTF16("Batman"));
1152 Profile
* profile3
= AddProfileToCache(
1153 profile_manager
, "path_3", profile_name3
);
1154 EXPECT_EQ(3u, cache
.GetNumberOfProfiles());
1156 // Sign in all profiles, and make sure they do not have a Gaia name set.
1157 const base::string16
email1(ASCIIToUTF16("user1@gmail.com"));
1158 const base::string16
email2(ASCIIToUTF16("user2@gmail.com"));
1159 const base::string16
email3(ASCIIToUTF16("user3@gmail.com"));
1161 int index
= cache
.GetIndexOfProfileWithPath(profile1
->GetPath());
1162 cache
.SetUserNameOfProfileAtIndex(index
, email1
);
1163 cache
.SetGAIAGivenNameOfProfileAtIndex(index
, base::string16());
1164 cache
.SetGAIANameOfProfileAtIndex(index
, base::string16());
1166 // This may resort the cache, so be extra cautious to use the right profile.
1167 index
= cache
.GetIndexOfProfileWithPath(profile2
->GetPath());
1168 cache
.SetUserNameOfProfileAtIndex(index
, email2
);
1169 cache
.SetGAIAGivenNameOfProfileAtIndex(index
, base::string16());
1170 cache
.SetGAIANameOfProfileAtIndex(index
, base::string16());
1172 index
= cache
.GetIndexOfProfileWithPath(profile3
->GetPath());
1173 cache
.SetUserNameOfProfileAtIndex(index
, email3
);
1174 cache
.SetGAIAGivenNameOfProfileAtIndex(index
, base::string16());
1175 cache
.SetGAIANameOfProfileAtIndex(index
, base::string16());
1177 // The profiles with default names should display the email address.
1178 EXPECT_EQ(email1
, profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1179 EXPECT_EQ(email2
, profiles::GetAvatarNameForProfile(profile2
->GetPath()));
1181 // The profile with the custom name should display that.
1182 EXPECT_EQ(profile_name3
,
1183 profiles::GetAvatarNameForProfile(profile3
->GetPath()));
1185 // Adding a Gaia name to a profile that previously had a default name should
1186 // start displaying it.
1187 const base::string16
gaia_given_name(ASCIIToUTF16("Robin"));
1188 cache
.SetGAIAGivenNameOfProfileAtIndex(
1189 cache
.GetIndexOfProfileWithPath(profile1
->GetPath()), gaia_given_name
);
1190 EXPECT_EQ(gaia_given_name
,
1191 profiles::GetAvatarNameForProfile(profile1
->GetPath()));
1193 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1195 #if defined(OS_MACOSX)
1196 // These tests are for a Mac-only code path that assumes the browser
1197 // process isn't killed when all browser windows are closed.
1198 TEST_F(ProfileManagerTest
, ActiveProfileDeletedNeedsToLoadNextProfile
) {
1199 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1200 ASSERT_TRUE(profile_manager
);
1202 // Create and load one profile, and just create a second profile.
1203 const std::string profile_name1
= "New Profile 1";
1204 const std::string profile_name2
= "New Profile 2";
1205 base::FilePath dest_path1
= temp_dir_
.path().AppendASCII(profile_name1
);
1206 base::FilePath dest_path2
= temp_dir_
.path().AppendASCII(profile_name2
);
1208 MockObserver mock_observer
;
1209 EXPECT_CALL(mock_observer
, OnProfileCreated(
1210 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1211 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
1212 base::RunLoop().RunUntilIdle();
1214 // Track the profile, but don't load it.
1215 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1216 cache
.AddProfileToCache(dest_path2
, ASCIIToUTF16(profile_name2
),
1217 base::string16(), 0, std::string());
1218 base::RunLoop().RunUntilIdle();
1220 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
1221 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
1223 // Set the active profile.
1224 PrefService
* local_state
= g_browser_process
->local_state();
1225 local_state
->SetString(prefs::kProfileLastUsed
,
1226 dest_path1
.BaseName().MaybeAsASCII());
1228 // Delete the active profile. This should switch and load the unloaded
1230 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
1231 ProfileManager::CreateCallback());
1233 // Spin the message loop so that all the callbacks can finish running.
1234 base::RunLoop().RunUntilIdle();
1236 EXPECT_EQ(dest_path2
, profile_manager
->GetLastUsedProfile()->GetPath());
1237 EXPECT_EQ(profile_name2
, local_state
->GetString(prefs::kProfileLastUsed
));
1240 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1241 // by simulating a scenario in which the profile that is being loaded as
1242 // the next active profile has also been marked for deletion, so the
1243 // ProfileManager needs to recursively select a different next profile.
1244 TEST_F(ProfileManagerTest
, ActiveProfileDeletedNextProfileDeletedToo
) {
1245 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
1246 ASSERT_TRUE(profile_manager
);
1248 // Create and load one profile, and create two more profiles.
1249 const std::string profile_name1
= "New Profile 1";
1250 const std::string profile_name2
= "New Profile 2";
1251 const std::string profile_name3
= "New Profile 3";
1252 base::FilePath dest_path1
= temp_dir_
.path().AppendASCII(profile_name1
);
1253 base::FilePath dest_path2
= temp_dir_
.path().AppendASCII(profile_name2
);
1254 base::FilePath dest_path3
= temp_dir_
.path().AppendASCII(profile_name3
);
1256 MockObserver mock_observer
;
1257 EXPECT_CALL(mock_observer
, OnProfileCreated(
1258 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1259 CreateProfileAsync(profile_manager
, profile_name1
, false, &mock_observer
);
1260 base::RunLoop().RunUntilIdle();
1262 // Create the other profiles, but don't load them. Assign a fake avatar icon
1263 // to ensure that profiles in the info cache are sorted by the profile name,
1264 // and not randomly by the avatar name.
1265 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
1266 cache
.AddProfileToCache(dest_path2
, ASCIIToUTF16(profile_name2
),
1267 ASCIIToUTF16(profile_name2
), 1, std::string());
1268 cache
.AddProfileToCache(dest_path3
, ASCIIToUTF16(profile_name3
),
1269 ASCIIToUTF16(profile_name3
), 2, std::string());
1271 base::RunLoop().RunUntilIdle();
1273 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
1274 EXPECT_EQ(3u, cache
.GetNumberOfProfiles());
1276 // Set the active profile.
1277 PrefService
* local_state
= g_browser_process
->local_state();
1278 local_state
->SetString(prefs::kProfileLastUsed
,
1279 dest_path1
.BaseName().MaybeAsASCII());
1281 // Delete the active profile, Profile1.
1282 // This will post a CreateProfileAsync message, that tries to load Profile2,
1283 // which checks that the profile is not being deleted, and then calls back
1284 // FinishDeletingProfile for Profile1.
1285 // Try to break this flow by setting the active profile to Profile2 in the
1286 // middle (so after the first posted message), and trying to delete Profile2,
1287 // so that the ProfileManager has to look for a different profile to load.
1288 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
1289 ProfileManager::CreateCallback());
1290 local_state
->SetString(prefs::kProfileLastUsed
,
1291 dest_path2
.BaseName().MaybeAsASCII());
1292 profile_manager
->ScheduleProfileForDeletion(dest_path2
,
1293 ProfileManager::CreateCallback());
1294 // Spin the message loop so that all the callbacks can finish running.
1295 base::RunLoop().RunUntilIdle();
1297 EXPECT_EQ(dest_path3
, profile_manager
->GetLastUsedProfile()->GetPath());
1298 EXPECT_EQ(profile_name3
, local_state
->GetString(prefs::kProfileLastUsed
));
1300 #endif // !defined(OS_MACOSX)