1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/history/history_service.h"
20 #include "chrome/browser/history/history_service_factory.h"
21 #include "chrome/browser/io_thread.h"
22 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/browser/prefs/incognito_mode_prefs.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_info_cache.h"
26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/common/chrome_constants.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/scoped_testing_local_state.h"
33 #include "chrome/test/base/test_browser_window.h"
34 #include "chrome/test/base/testing_browser_process.h"
35 #include "chrome/test/base/testing_profile.h"
36 #include "content/public/browser/notification_service.h"
37 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h"
41 #if defined(OS_CHROMEOS)
42 #include "chrome/browser/chromeos/login/mock_user_manager.h"
43 #include "chrome/browser/chromeos/login/user_manager.h"
44 #include "chrome/browser/chromeos/settings/cros_settings.h"
45 #include "chrome/browser/chromeos/settings/device_settings_service.h"
46 #include "chromeos/chromeos_switches.h"
49 using base::ASCIIToUTF16
;
50 using content::BrowserThread
;
54 // This global variable is used to check that value returned to different
55 // observers is the same.
56 Profile
* g_created_profile
;
58 class UnittestProfileManager
: public ::ProfileManagerWithoutInit
{
60 explicit UnittestProfileManager(const base::FilePath
& user_data_dir
)
61 : ::ProfileManagerWithoutInit(user_data_dir
) {}
64 virtual Profile
* CreateProfileHelper(
65 const base::FilePath
& file_path
) OVERRIDE
{
66 if (!base::PathExists(file_path
)) {
67 if (!base::CreateDirectory(file_path
))
70 return new TestingProfile(file_path
, NULL
);
73 virtual Profile
* CreateProfileAsyncHelper(const base::FilePath
& path
,
74 Delegate
* delegate
) OVERRIDE
{
75 // This is safe while all file operations are done on the FILE thread.
76 BrowserThread::PostTask(
77 BrowserThread::FILE, FROM_HERE
,
78 base::Bind(base::IgnoreResult(&base::CreateDirectory
), path
));
80 return new TestingProfile(path
, this);
86 class ProfileManagerTest
: public testing::Test
{
90 MOCK_METHOD2(OnProfileCreated
,
91 void(Profile
* profile
, Profile::CreateStatus status
));
95 : local_state_(TestingBrowserProcess::GetGlobal()) {
98 virtual void SetUp() {
99 // Create a new temporary directory, and store the path
100 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
101 TestingBrowserProcess::GetGlobal()->SetProfileManager(
102 new UnittestProfileManager(temp_dir_
.path()));
104 #if defined(OS_CHROMEOS)
105 CommandLine
* cl
= CommandLine::ForCurrentProcess();
106 cl
->AppendSwitch(switches::kTestType
);
110 virtual void TearDown() {
111 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
112 base::RunLoop().RunUntilIdle();
115 // Helper function to create a profile with |name| for a profile |manager|.
116 void CreateProfileAsync(ProfileManager
* manager
,
117 const std::string
& name
,
118 MockObserver
* mock_observer
) {
119 manager
->CreateProfileAsync(
120 temp_dir_
.path().AppendASCII(name
),
121 base::Bind(&MockObserver::OnProfileCreated
,
122 base::Unretained(mock_observer
)),
123 base::UTF8ToUTF16(name
),
128 #if defined(OS_CHROMEOS)
129 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
130 chromeos::ScopedTestCrosSettings test_cros_settings_
;
133 // The path to temporary directory used to contain the test operations.
134 base::ScopedTempDir temp_dir_
;
135 ScopedTestingLocalState local_state_
;
137 content::TestBrowserThreadBundle thread_bundle_
;
139 #if defined(OS_CHROMEOS)
140 chromeos::ScopedTestUserManager test_user_manager_
;
144 TEST_F(ProfileManagerTest
, GetProfile
) {
145 base::FilePath dest_path
= temp_dir_
.path();
146 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
148 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
150 // Successfully create a profile.
151 Profile
* profile
= profile_manager
->GetProfile(dest_path
);
152 EXPECT_TRUE(profile
);
154 // The profile already exists when we call GetProfile. Just load it.
155 EXPECT_EQ(profile
, profile_manager
->GetProfile(dest_path
));
158 TEST_F(ProfileManagerTest
, DefaultProfileDir
) {
159 base::FilePath expected_default
=
160 base::FilePath().AppendASCII(chrome::kInitialProfile
);
162 expected_default
.value(),
163 g_browser_process
->profile_manager()->GetInitialProfileDir().value());
166 #if defined(OS_CHROMEOS)
167 // This functionality only exists on Chrome OS.
168 TEST_F(ProfileManagerTest
, LoggedInProfileDir
) {
169 CommandLine
*cl
= CommandLine::ForCurrentProcess();
170 std::string
profile_dir(chrome::kTestUserProfileDir
);
172 cl
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, profile_dir
);
174 base::FilePath expected_default
=
175 base::FilePath().AppendASCII(chrome::kInitialProfile
);
176 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
177 EXPECT_EQ(expected_default
.value(),
178 profile_manager
->GetInitialProfileDir().value());
180 scoped_ptr
<chromeos::MockUserManager
> mock_user_manager
;
181 mock_user_manager
.reset(new chromeos::MockUserManager());
182 mock_user_manager
->SetActiveUser("user@gmail.com");
183 chromeos::User
* active_user
= mock_user_manager
->GetActiveUser();
184 profile_manager
->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
185 content::NotificationService::AllSources(),
186 content::Details
<const chromeos::User
>(active_user
));
187 base::FilePath
expected_logged_in(profile_dir
);
188 EXPECT_EQ(expected_logged_in
.value(),
189 profile_manager
->GetInitialProfileDir().value());
190 VLOG(1) << temp_dir_
.path().Append(
191 profile_manager
->GetInitialProfileDir()).value();
196 TEST_F(ProfileManagerTest
, CreateAndUseTwoProfiles
) {
197 base::FilePath dest_path1
= temp_dir_
.path();
198 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
200 base::FilePath dest_path2
= temp_dir_
.path();
201 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
203 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
205 // Successfully create the profiles.
206 TestingProfile
* profile1
=
207 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
208 ASSERT_TRUE(profile1
);
210 TestingProfile
* profile2
=
211 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
212 ASSERT_TRUE(profile2
);
214 // Force lazy-init of some profile services to simulate use.
215 ASSERT_TRUE(profile1
->CreateHistoryService(true, false));
216 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1
,
217 Profile::EXPLICIT_ACCESS
));
218 profile1
->CreateBookmarkModel(true);
219 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1
));
220 profile2
->CreateBookmarkModel(true);
221 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2
));
222 ASSERT_TRUE(profile2
->CreateHistoryService(true, false));
223 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2
,
224 Profile::EXPLICIT_ACCESS
));
226 // Make sure any pending tasks run before we destroy the profiles.
227 base::RunLoop().RunUntilIdle();
229 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
231 // Make sure history cleans up correctly.
232 base::RunLoop().RunUntilIdle();
235 MATCHER(NotFail
, "Profile creation failure status is not reported.") {
236 return arg
== Profile::CREATE_STATUS_CREATED
||
237 arg
== Profile::CREATE_STATUS_INITIALIZED
;
240 // Tests asynchronous profile creation mechanism.
241 // Crashes: http://crbug.com/89421
242 TEST_F(ProfileManagerTest
, DISABLED_CreateProfileAsync
) {
243 MockObserver mock_observer
;
244 EXPECT_CALL(mock_observer
, OnProfileCreated(
245 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
247 CreateProfileAsync(g_browser_process
->profile_manager(),
248 "New Profile", &mock_observer
);
250 base::RunLoop().RunUntilIdle();
253 MATCHER(SameNotNull
, "The same non-NULL value for all calls.") {
254 if (!g_created_profile
)
255 g_created_profile
= arg
;
256 return arg
!= NULL
&& arg
== g_created_profile
;
259 TEST_F(ProfileManagerTest
, CreateProfileAsyncMultipleRequests
) {
260 g_created_profile
= NULL
;
262 MockObserver mock_observer1
;
263 EXPECT_CALL(mock_observer1
, OnProfileCreated(
264 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
265 MockObserver mock_observer2
;
266 EXPECT_CALL(mock_observer2
, OnProfileCreated(
267 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
268 MockObserver mock_observer3
;
269 EXPECT_CALL(mock_observer3
, OnProfileCreated(
270 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
272 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
273 const std::string profile_name
= "New Profile";
274 CreateProfileAsync(profile_manager
, profile_name
, &mock_observer1
);
275 CreateProfileAsync(profile_manager
, profile_name
, &mock_observer2
);
276 CreateProfileAsync(profile_manager
, profile_name
, &mock_observer3
);
278 base::RunLoop().RunUntilIdle();
281 TEST_F(ProfileManagerTest
, CreateProfilesAsync
) {
282 const std::string profile_name1
= "New Profile 1";
283 const std::string profile_name2
= "New Profile 2";
285 MockObserver mock_observer
;
286 EXPECT_CALL(mock_observer
, OnProfileCreated(
287 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
289 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
291 CreateProfileAsync(profile_manager
, profile_name1
, &mock_observer
);
292 CreateProfileAsync(profile_manager
, profile_name2
, &mock_observer
);
294 base::RunLoop().RunUntilIdle();
297 TEST_F(ProfileManagerTest
, GetGuestProfilePath
) {
298 base::FilePath guest_path
= ProfileManager::GetGuestProfilePath();
299 base::FilePath expected_path
= temp_dir_
.path();
300 expected_path
= expected_path
.Append(chrome::kGuestProfileDir
);
301 EXPECT_EQ(expected_path
, guest_path
);
304 #if defined(OS_CHROMEOS)
305 class UnittestGuestProfileManager
: public UnittestProfileManager
{
307 explicit UnittestGuestProfileManager(const base::FilePath
& user_data_dir
)
308 : UnittestProfileManager(user_data_dir
) {}
311 virtual Profile
* CreateProfileHelper(
312 const base::FilePath
& file_path
) OVERRIDE
{
313 TestingProfile
* testing_profile
= new TestingProfile(file_path
, NULL
);
315 TestingProfile::Builder builder
;
316 builder
.SetIncognito();
317 builder
.SetGuestSession();
318 builder
.SetPath(ProfileManager::GetGuestProfilePath());
319 testing_profile
->SetOffTheRecordProfile(builder
.Build().PassAs
<Profile
>());
321 return testing_profile
;
325 class ProfileManagerGuestTest
: public ProfileManagerTest
{
327 virtual void SetUp() {
328 // Create a new temporary directory, and store the path
329 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
330 TestingBrowserProcess::GetGlobal()->SetProfileManager(
331 new UnittestGuestProfileManager(temp_dir_
.path()));
333 CommandLine
* cl
= CommandLine::ForCurrentProcess();
334 cl
->AppendSwitch(switches::kTestType
);
335 cl
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, "user");
336 cl
->AppendSwitch(chromeos::switches::kGuestSession
);
337 cl
->AppendSwitch(::switches::kIncognito
);
339 chromeos::UserManager::Get()->UserLoggedIn(
340 chromeos::UserManager::kGuestUserName
,
341 chromeos::UserManager::kGuestUserName
,
346 TEST_F(ProfileManagerGuestTest
, GuestProfileIngonito
) {
347 Profile
* primary_profile
= ProfileManager::GetPrimaryUserProfile();
348 EXPECT_TRUE(primary_profile
->IsOffTheRecord());
350 Profile
* active_profile
= ProfileManager::GetActiveUserProfile();
351 EXPECT_TRUE(active_profile
->IsOffTheRecord());
353 EXPECT_TRUE(active_profile
->IsSameProfile(primary_profile
));
357 TEST_F(ProfileManagerTest
, AutoloadProfilesWithBackgroundApps
) {
358 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
359 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
360 local_state_
.Get()->SetUserPref(prefs::kBackgroundModeEnabled
,
361 base::Value::CreateBooleanValue(true));
363 // Setting a pref which is not applicable to a system (i.e., Android in this
364 // case) does not necessarily create it. Don't bother continuing with the
365 // test if this pref doesn't exist because it will not load the profiles if
366 // it cannot verify that the pref for background mode is enabled.
367 if (!local_state_
.Get()->HasPrefPath(prefs::kBackgroundModeEnabled
))
370 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
371 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_1"),
372 ASCIIToUTF16("name_1"), base::string16(), 0,
374 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_2"),
375 ASCIIToUTF16("name_2"), base::string16(), 0,
377 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_3"),
378 ASCIIToUTF16("name_3"), base::string16(), 0,
380 cache
.SetBackgroundStatusOfProfileAtIndex(0, true);
381 cache
.SetBackgroundStatusOfProfileAtIndex(2, true);
382 EXPECT_EQ(3u, cache
.GetNumberOfProfiles());
384 profile_manager
->AutoloadProfiles();
386 EXPECT_EQ(2u, profile_manager
->GetLoadedProfiles().size());
389 TEST_F(ProfileManagerTest
, DoNotAutoloadProfilesIfBackgroundModeOff
) {
390 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
391 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
392 local_state_
.Get()->SetUserPref(prefs::kBackgroundModeEnabled
,
393 base::Value::CreateBooleanValue(false));
395 EXPECT_EQ(0u, cache
.GetNumberOfProfiles());
396 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_1"),
397 ASCIIToUTF16("name_1"), base::string16(), 0,
399 cache
.AddProfileToCache(cache
.GetUserDataDir().AppendASCII("path_2"),
400 ASCIIToUTF16("name_2"), base::string16(), 0,
402 cache
.SetBackgroundStatusOfProfileAtIndex(0, false);
403 cache
.SetBackgroundStatusOfProfileAtIndex(1, true);
404 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
406 profile_manager
->AutoloadProfiles();
408 EXPECT_EQ(0u, profile_manager
->GetLoadedProfiles().size());
411 TEST_F(ProfileManagerTest
, InitProfileUserPrefs
) {
412 base::FilePath dest_path
= temp_dir_
.path();
413 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
415 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
419 // Successfully create the profile
420 profile
= profile_manager
->GetProfile(dest_path
);
421 ASSERT_TRUE(profile
);
423 // Check that the profile name is non empty
424 std::string profile_name
=
425 profile
->GetPrefs()->GetString(prefs::kProfileName
);
426 EXPECT_FALSE(profile_name
.empty());
428 // Check that the profile avatar index is valid
429 size_t avatar_index
=
430 profile
->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex
);
431 EXPECT_TRUE(profile_manager
->GetProfileInfoCache().IsDefaultAvatarIconIndex(
435 // Tests that a new profile's entry in the profile info cache is setup with the
436 // same values that are in the profile prefs.
437 TEST_F(ProfileManagerTest
, InitProfileInfoCacheForAProfile
) {
438 base::FilePath dest_path
= temp_dir_
.path();
439 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile"));
441 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
442 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
444 // Successfully create the profile
445 Profile
* profile
= profile_manager
->GetProfile(dest_path
);
446 ASSERT_TRUE(profile
);
448 std::string profile_name
=
449 profile
->GetPrefs()->GetString(prefs::kProfileName
);
450 size_t avatar_index
=
451 profile
->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex
);
453 size_t profile_index
= cache
.GetIndexOfProfileWithPath(dest_path
);
455 // Check if the profile prefs are the same as the cache prefs
456 EXPECT_EQ(profile_name
,
457 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
458 EXPECT_EQ(avatar_index
,
459 cache
.GetAvatarIconIndexOfProfileAtIndex(profile_index
));
462 TEST_F(ProfileManagerTest
, GetLastUsedProfileAllowedByPolicy
) {
463 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
464 ASSERT_TRUE(profile_manager
);
466 Profile
* profile
= profile_manager
->GetLastUsedProfileAllowedByPolicy();
467 ASSERT_TRUE(profile
);
468 EXPECT_FALSE(profile
->IsOffTheRecord());
469 PrefService
* prefs
= profile
->GetPrefs();
470 EXPECT_EQ(IncognitoModePrefs::ENABLED
,
471 IncognitoModePrefs::GetAvailability(prefs
));
473 // Attach an incognito Profile to the TestingProfile.
474 ASSERT_FALSE(profile
->GetOffTheRecordProfile());
475 TestingProfile::Builder builder
;
476 builder
.SetIncognito();
477 scoped_ptr
<TestingProfile
> incognito_profile
= builder
.Build();
478 EXPECT_TRUE(incognito_profile
->IsOffTheRecord());
479 TestingProfile
* testing_profile
= static_cast<TestingProfile
*>(profile
);
480 testing_profile
->SetOffTheRecordProfile(incognito_profile
.PassAs
<Profile
>());
481 ASSERT_TRUE(profile
->GetOffTheRecordProfile());
483 IncognitoModePrefs::SetAvailability(prefs
, IncognitoModePrefs::DISABLED
);
485 profile_manager
->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
487 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
488 // incognito mode is forced.
489 IncognitoModePrefs::SetAvailability(prefs
, IncognitoModePrefs::FORCED
);
491 profile_manager
->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
494 #if !defined(OS_ANDROID)
495 // There's no Browser object on Android.
496 TEST_F(ProfileManagerTest
, LastOpenedProfiles
) {
497 base::FilePath dest_path1
= temp_dir_
.path();
498 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
500 base::FilePath dest_path2
= temp_dir_
.path();
501 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
503 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
505 // Successfully create the profiles.
506 TestingProfile
* profile1
=
507 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
508 ASSERT_TRUE(profile1
);
510 TestingProfile
* profile2
=
511 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
512 ASSERT_TRUE(profile2
);
514 std::vector
<Profile
*> last_opened_profiles
=
515 profile_manager
->GetLastOpenedProfiles();
516 ASSERT_EQ(0U, last_opened_profiles
.size());
518 // Create a browser for profile1.
519 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
520 scoped_ptr
<Browser
> browser1a(
521 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
523 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
524 ASSERT_EQ(1U, last_opened_profiles
.size());
525 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
528 Browser::CreateParams
profile2_params(profile2
, chrome::GetActiveDesktop());
529 scoped_ptr
<Browser
> browser2(
530 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
532 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
533 ASSERT_EQ(2U, last_opened_profiles
.size());
534 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
535 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
537 // Adding more browsers doesn't change anything.
538 scoped_ptr
<Browser
> browser1b(
539 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
540 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
541 ASSERT_EQ(2U, last_opened_profiles
.size());
542 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
543 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
545 // Close the browsers.
547 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
548 ASSERT_EQ(2U, last_opened_profiles
.size());
549 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
550 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
553 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
554 ASSERT_EQ(1U, last_opened_profiles
.size());
555 EXPECT_EQ(profile2
, last_opened_profiles
[0]);
558 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
559 ASSERT_EQ(0U, last_opened_profiles
.size());
562 TEST_F(ProfileManagerTest
, LastOpenedProfilesAtShutdown
) {
563 base::FilePath dest_path1
= temp_dir_
.path();
564 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
566 base::FilePath dest_path2
= temp_dir_
.path();
567 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
569 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
571 // Successfully create the profiles.
572 TestingProfile
* profile1
=
573 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
574 ASSERT_TRUE(profile1
);
576 TestingProfile
* profile2
=
577 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
578 ASSERT_TRUE(profile2
);
580 // Create a browser for profile1.
581 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
582 scoped_ptr
<Browser
> browser1(
583 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
586 Browser::CreateParams
profile2_params(profile2
, chrome::GetActiveDesktop());
587 scoped_ptr
<Browser
> browser2(
588 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
590 std::vector
<Profile
*> last_opened_profiles
=
591 profile_manager
->GetLastOpenedProfiles();
592 ASSERT_EQ(2U, last_opened_profiles
.size());
593 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
594 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
596 // Simulate a shutdown.
597 content::NotificationService::current()->Notify(
598 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
599 content::NotificationService::AllSources(),
600 content::NotificationService::NoDetails());
602 // Even if the browsers are destructed during shutdown, the profiles stay
607 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
608 ASSERT_EQ(2U, last_opened_profiles
.size());
609 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
610 EXPECT_EQ(profile2
, last_opened_profiles
[1]);
613 TEST_F(ProfileManagerTest
, LastOpenedProfilesDoesNotContainIncognito
) {
614 base::FilePath dest_path1
= temp_dir_
.path();
615 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("New Profile 1"));
616 base::FilePath dest_path2
= temp_dir_
.path();
617 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("New Profile 2"));
619 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
621 // Successfully create the profiles.
622 TestingProfile
* profile1
=
623 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
624 ASSERT_TRUE(profile1
);
626 // incognito profiles should not be managed by the profile manager but by the
628 TestingProfile::Builder builder
;
629 builder
.SetIncognito();
630 scoped_ptr
<TestingProfile
> profile2
= builder
.Build();
631 profile1
->SetOffTheRecordProfile(profile2
.PassAs
<Profile
>());
633 std::vector
<Profile
*> last_opened_profiles
=
634 profile_manager
->GetLastOpenedProfiles();
635 ASSERT_EQ(0U, last_opened_profiles
.size());
637 // Create a browser for profile1.
638 Browser::CreateParams
profile1_params(profile1
, chrome::GetActiveDesktop());
639 scoped_ptr
<Browser
> browser1(
640 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
642 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
643 ASSERT_EQ(1U, last_opened_profiles
.size());
644 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
647 Browser::CreateParams
profile2_params(profile1
->GetOffTheRecordProfile(),
648 chrome::GetActiveDesktop());
649 scoped_ptr
<Browser
> browser2a(
650 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
652 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
653 ASSERT_EQ(1U, last_opened_profiles
.size());
654 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
656 // Adding more browsers doesn't change anything.
657 scoped_ptr
<Browser
> browser2b(
658 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
659 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
660 ASSERT_EQ(1U, last_opened_profiles
.size());
661 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
663 // Close the browsers.
665 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
666 ASSERT_EQ(1U, last_opened_profiles
.size());
667 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
670 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
671 ASSERT_EQ(1U, last_opened_profiles
.size());
672 EXPECT_EQ(profile1
, last_opened_profiles
[0]);
675 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
676 ASSERT_EQ(0U, last_opened_profiles
.size());
678 #endif // !defined(OS_ANDROID)
680 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
681 // There's no Browser object on Android and there's no multi-profiles on Chrome.
682 TEST_F(ProfileManagerTest
, EphemeralProfilesDontEndUpAsLastProfile
) {
683 base::FilePath dest_path
= temp_dir_
.path();
684 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
686 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
688 TestingProfile
* profile
=
689 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path
));
690 ASSERT_TRUE(profile
);
691 profile
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
, true);
693 // Here the last used profile is still the "Default" profile.
694 Profile
* last_used_profile
= profile_manager
->GetLastUsedProfile();
695 EXPECT_NE(profile
, last_used_profile
);
697 // Create a browser for the profile.
698 Browser::CreateParams
profile_params(profile
, chrome::GetActiveDesktop());
699 scoped_ptr
<Browser
> browser(
700 chrome::CreateBrowserWithTestWindowForParams(&profile_params
));
701 last_used_profile
= profile_manager
->GetLastUsedProfile();
702 EXPECT_NE(profile
, last_used_profile
);
704 // Close the browser.
706 last_used_profile
= profile_manager
->GetLastUsedProfile();
707 EXPECT_NE(profile
, last_used_profile
);
710 TEST_F(ProfileManagerTest
, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown
) {
711 base::FilePath dest_path1
= temp_dir_
.path();
712 dest_path1
= dest_path1
.Append(FILE_PATH_LITERAL("Normal Profile"));
714 base::FilePath dest_path2
= temp_dir_
.path();
715 dest_path2
= dest_path2
.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
717 base::FilePath dest_path3
= temp_dir_
.path();
718 dest_path3
= dest_path3
.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
720 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
722 // Successfully create the profiles.
723 TestingProfile
* normal_profile
=
724 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path1
));
725 ASSERT_TRUE(normal_profile
);
727 // Add one ephemeral profile which should not end up in this list.
728 TestingProfile
* ephemeral_profile1
=
729 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path2
));
730 ASSERT_TRUE(ephemeral_profile1
);
731 ephemeral_profile1
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
,
734 // Add second ephemeral profile but don't mark it as such yet.
735 TestingProfile
* ephemeral_profile2
=
736 static_cast<TestingProfile
*>(profile_manager
->GetProfile(dest_path3
));
737 ASSERT_TRUE(ephemeral_profile2
);
739 // Create a browser for profile1.
740 Browser::CreateParams
profile1_params(normal_profile
,
741 chrome::GetActiveDesktop());
742 scoped_ptr
<Browser
> browser1(
743 chrome::CreateBrowserWithTestWindowForParams(&profile1_params
));
745 // Create browsers for the ephemeral profile.
746 Browser::CreateParams
profile2_params(ephemeral_profile1
,
747 chrome::GetActiveDesktop());
748 scoped_ptr
<Browser
> browser2(
749 chrome::CreateBrowserWithTestWindowForParams(&profile2_params
));
751 Browser::CreateParams
profile3_params(ephemeral_profile2
,
752 chrome::GetActiveDesktop());
753 scoped_ptr
<Browser
> browser3(
754 chrome::CreateBrowserWithTestWindowForParams(&profile3_params
));
756 std::vector
<Profile
*> last_opened_profiles
=
757 profile_manager
->GetLastOpenedProfiles();
758 ASSERT_EQ(2U, last_opened_profiles
.size());
759 EXPECT_EQ(normal_profile
, last_opened_profiles
[0]);
760 EXPECT_EQ(ephemeral_profile2
, last_opened_profiles
[1]);
762 // Mark the second profile ephemeral.
763 ephemeral_profile2
->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles
,
766 // Simulate a shutdown.
767 content::NotificationService::current()->Notify(
768 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
769 content::NotificationService::AllSources(),
770 content::NotificationService::NoDetails());
775 last_opened_profiles
= profile_manager
->GetLastOpenedProfiles();
776 ASSERT_EQ(1U, last_opened_profiles
.size());
777 EXPECT_EQ(normal_profile
, last_opened_profiles
[0]);
780 TEST_F(ProfileManagerTest
, ActiveProfileDeleted
) {
781 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
782 ASSERT_TRUE(profile_manager
);
784 // Create and load two profiles.
785 const std::string profile_name1
= "New Profile 1";
786 const std::string profile_name2
= "New Profile 2";
787 base::FilePath dest_path1
=
788 temp_dir_
.path().AppendASCII(profile_name1
);
789 base::FilePath dest_path2
=
790 temp_dir_
.path().AppendASCII(profile_name2
);
792 MockObserver mock_observer
;
793 EXPECT_CALL(mock_observer
, OnProfileCreated(
794 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
796 CreateProfileAsync(profile_manager
, profile_name1
, &mock_observer
);
797 CreateProfileAsync(profile_manager
, profile_name2
, &mock_observer
);
798 base::RunLoop().RunUntilIdle();
800 EXPECT_EQ(2u, profile_manager
->GetLoadedProfiles().size());
801 EXPECT_EQ(2u, profile_manager
->GetProfileInfoCache().GetNumberOfProfiles());
803 // Set the active profile.
804 PrefService
* local_state
= g_browser_process
->local_state();
805 local_state
->SetString(prefs::kProfileLastUsed
, profile_name1
);
807 // Delete the active profile.
808 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
809 ProfileManager::CreateCallback());
810 // Spin the message loop so that all the callbacks can finish running.
811 base::RunLoop().RunUntilIdle();
813 EXPECT_EQ(dest_path2
, profile_manager
->GetLastUsedProfile()->GetPath());
814 EXPECT_EQ(profile_name2
, local_state
->GetString(prefs::kProfileLastUsed
));
816 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
818 #if defined(OS_MACOSX)
819 // These tests are for a Mac-only code path that assumes the browser
820 // process isn't killed when all browser windows are closed.
821 TEST_F(ProfileManagerTest
, ActiveProfileDeletedNeedsToLoadNextProfile
) {
822 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
823 ASSERT_TRUE(profile_manager
);
825 // Create and load one profile, and just create a second profile.
826 const std::string profile_name1
= "New Profile 1";
827 const std::string profile_name2
= "New Profile 2";
828 base::FilePath dest_path1
=
829 temp_dir_
.path().AppendASCII(profile_name1
);
830 base::FilePath dest_path2
=
831 temp_dir_
.path().AppendASCII(profile_name2
);
833 MockObserver mock_observer
;
834 EXPECT_CALL(mock_observer
, OnProfileCreated(
835 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
836 CreateProfileAsync(profile_manager
, profile_name1
, &mock_observer
);
837 base::RunLoop().RunUntilIdle();
839 // Track the profile, but don't load it.
840 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
841 cache
.AddProfileToCache(dest_path2
, ASCIIToUTF16(profile_name2
),
842 base::string16(), 0, std::string());
843 base::RunLoop().RunUntilIdle();
845 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
846 EXPECT_EQ(2u, cache
.GetNumberOfProfiles());
848 // Set the active profile.
849 PrefService
* local_state
= g_browser_process
->local_state();
850 local_state
->SetString(prefs::kProfileLastUsed
,
851 dest_path1
.BaseName().MaybeAsASCII());
853 // Delete the active profile. This should switch and load the unloaded
855 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
856 ProfileManager::CreateCallback());
858 // Spin the message loop so that all the callbacks can finish running.
859 base::RunLoop().RunUntilIdle();
861 EXPECT_EQ(dest_path2
, profile_manager
->GetLastUsedProfile()->GetPath());
862 EXPECT_EQ(profile_name2
, local_state
->GetString(prefs::kProfileLastUsed
));
865 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
866 // by simulating a scenario in which the profile that is being loaded as
867 // the next active profile has also been marked for deletion, so the
868 // ProfileManager needs to recursively select a different next profile.
869 TEST_F(ProfileManagerTest
, ActiveProfileDeletedNextProfileDeletedToo
) {
870 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
871 ASSERT_TRUE(profile_manager
);
873 // Create and load one profile, and create two more profiles.
874 const std::string profile_name1
= "New Profile 1";
875 const std::string profile_name2
= "New Profile 2";
876 const std::string profile_name3
= "New Profile 3";
877 base::FilePath dest_path1
=
878 temp_dir_
.path().AppendASCII(profile_name1
);
879 base::FilePath dest_path2
=
880 temp_dir_
.path().AppendASCII(profile_name2
);
881 base::FilePath dest_path3
=
882 temp_dir_
.path().AppendASCII(profile_name3
);
884 MockObserver mock_observer
;
885 EXPECT_CALL(mock_observer
, OnProfileCreated(
886 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
887 CreateProfileAsync(profile_manager
, profile_name1
, &mock_observer
);
888 base::RunLoop().RunUntilIdle();
890 // Create the other profiles, but don't load them. Assign a fake avatar icon
891 // to ensure that profiles in the info cache are sorted by the profile name,
892 // and not randomly by the avatar name.
893 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
894 cache
.AddProfileToCache(dest_path2
, ASCIIToUTF16(profile_name2
),
895 ASCIIToUTF16(profile_name2
), 1, std::string());
896 cache
.AddProfileToCache(dest_path3
, ASCIIToUTF16(profile_name3
),
897 ASCIIToUTF16(profile_name3
), 2, std::string());
899 base::RunLoop().RunUntilIdle();
901 EXPECT_EQ(1u, profile_manager
->GetLoadedProfiles().size());
902 EXPECT_EQ(3u, cache
.GetNumberOfProfiles());
904 // Set the active profile.
905 PrefService
* local_state
= g_browser_process
->local_state();
906 local_state
->SetString(prefs::kProfileLastUsed
,
907 dest_path1
.BaseName().MaybeAsASCII());
909 // Delete the active profile, Profile1.
910 // This will post a CreateProfileAsync message, that tries to load Profile2,
911 // which checks that the profile is not being deleted, and then calls back
912 // FinishDeletingProfile for Profile1.
913 // Try to break this flow by setting the active profile to Profile2 in the
914 // middle (so after the first posted message), and trying to delete Profile2,
915 // so that the ProfileManager has to look for a different profile to load.
916 profile_manager
->ScheduleProfileForDeletion(dest_path1
,
917 ProfileManager::CreateCallback());
918 local_state
->SetString(prefs::kProfileLastUsed
,
919 dest_path2
.BaseName().MaybeAsASCII());
920 profile_manager
->ScheduleProfileForDeletion(dest_path2
,
921 ProfileManager::CreateCallback());
922 // Spin the message loop so that all the callbacks can finish running.
923 base::RunLoop().RunUntilIdle();
925 EXPECT_EQ(dest_path3
, profile_manager
->GetLastUsedProfile()->GetPath());
926 EXPECT_EQ(profile_name3
, local_state
->GetString(prefs::kProfileLastUsed
));
928 #endif // !defined(OS_MACOSX)