GoogleURLTrackerInfoBarDelegate: Initialize uninitialized member in constructor.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_manager_unittest.cc
bloba0f1660238aba45a90dbf322455ce2a6b3010850
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/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/history/history_service.h"
20 #include "chrome/browser/history/history_service_factory.h"
21 #include "chrome/browser/io_thread.h"
22 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/browser/prefs/incognito_mode_prefs.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
26 #include "chrome/browser/profiles/profile_info_cache.h"
27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/browser/profiles/profiles_state.h"
29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/common/chrome_constants.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/test/base/scoped_testing_local_state.h"
35 #include "chrome/test/base/test_browser_window.h"
36 #include "chrome/test/base/testing_browser_process.h"
37 #include "chrome/test/base/testing_profile.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/common/content_switches.h"
40 #include "content/public/test/test_browser_thread_bundle.h"
41 #include "grit/generated_resources.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/mock_user_manager.h"
48 #include "chrome/browser/chromeos/login/users/user_manager.h"
49 #include "chrome/browser/chromeos/settings/cros_settings.h"
50 #include "chrome/browser/chromeos/settings/device_settings_service.h"
51 #include "chromeos/chromeos_switches.h"
52 #endif
54 using base::ASCIIToUTF16;
55 using content::BrowserThread;
57 namespace {
59 // This global variable is used to check that value returned to different
60 // observers is the same.
61 Profile* g_created_profile;
63 class UnittestProfileManager : public ::ProfileManagerWithoutInit {
64 public:
65 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
66 : ::ProfileManagerWithoutInit(user_data_dir) {}
68 protected:
69 virtual Profile* CreateProfileHelper(
70 const base::FilePath& file_path) OVERRIDE {
71 if (!base::PathExists(file_path)) {
72 if (!base::CreateDirectory(file_path))
73 return NULL;
75 return new TestingProfile(file_path, NULL);
78 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
79 Delegate* delegate) OVERRIDE {
80 // This is safe while all file operations are done on the FILE thread.
81 BrowserThread::PostTask(
82 BrowserThread::FILE, FROM_HERE,
83 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
85 return new TestingProfile(path, this);
89 } // namespace
91 class ProfileManagerTest : public testing::Test {
92 protected:
93 class MockObserver {
94 public:
95 MOCK_METHOD2(OnProfileCreated,
96 void(Profile* profile, Profile::CreateStatus status));
99 ProfileManagerTest()
100 : local_state_(TestingBrowserProcess::GetGlobal()) {
103 virtual void SetUp() {
104 // Create a new temporary directory, and store the path
105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
106 TestingBrowserProcess::GetGlobal()->SetProfileManager(
107 new UnittestProfileManager(temp_dir_.path()));
109 #if defined(OS_CHROMEOS)
110 CommandLine* cl = CommandLine::ForCurrentProcess();
111 cl->AppendSwitch(switches::kTestType);
112 #endif
115 virtual void TearDown() {
116 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
117 base::RunLoop().RunUntilIdle();
120 // Helper function to create a profile with |name| for a profile |manager|.
121 void CreateProfileAsync(ProfileManager* manager,
122 const std::string& name,
123 bool is_managed,
124 MockObserver* mock_observer) {
125 manager->CreateProfileAsync(
126 temp_dir_.path().AppendASCII(name),
127 base::Bind(&MockObserver::OnProfileCreated,
128 base::Unretained(mock_observer)),
129 base::UTF8ToUTF16(name),
130 base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(0)),
131 is_managed ? "Dummy ID" : std::string());
134 // Helper function to add a profile with |profile_name| to
135 // |profile_manager|'s ProfileInfoCache, and return the profile created.
136 Profile* AddProfileToCache(ProfileManager* profile_manager,
137 const std::string& path_suffix,
138 const base::string16& profile_name) {
139 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
140 size_t num_profiles = cache.GetNumberOfProfiles();
141 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
142 cache.AddProfileToCache(path, profile_name,
143 base::string16(), 0, std::string());
144 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
145 return profile_manager->GetProfile(path);
148 #if defined(OS_CHROMEOS)
149 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
150 chromeos::ScopedTestCrosSettings test_cros_settings_;
151 #endif
153 // The path to temporary directory used to contain the test operations.
154 base::ScopedTempDir temp_dir_;
155 ScopedTestingLocalState local_state_;
157 content::TestBrowserThreadBundle thread_bundle_;
159 #if defined(OS_CHROMEOS)
160 chromeos::ScopedTestUserManager test_user_manager_;
161 #endif
164 TEST_F(ProfileManagerTest, GetProfile) {
165 base::FilePath dest_path = temp_dir_.path();
166 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
168 ProfileManager* profile_manager = g_browser_process->profile_manager();
170 // Successfully create a profile.
171 Profile* profile = profile_manager->GetProfile(dest_path);
172 EXPECT_TRUE(profile);
174 // The profile already exists when we call GetProfile. Just load it.
175 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
178 TEST_F(ProfileManagerTest, DefaultProfileDir) {
179 base::FilePath expected_default =
180 base::FilePath().AppendASCII(chrome::kInitialProfile);
181 EXPECT_EQ(
182 expected_default.value(),
183 g_browser_process->profile_manager()->GetInitialProfileDir().value());
186 #if defined(OS_CHROMEOS)
187 // This functionality only exists on Chrome OS.
188 TEST_F(ProfileManagerTest, LoggedInProfileDir) {
189 CommandLine *cl = CommandLine::ForCurrentProcess();
190 std::string profile_dir(chrome::kTestUserProfileDir);
192 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir);
194 base::FilePath expected_default =
195 base::FilePath().AppendASCII(chrome::kInitialProfile);
196 ProfileManager* profile_manager = g_browser_process->profile_manager();
197 EXPECT_EQ(expected_default.value(),
198 profile_manager->GetInitialProfileDir().value());
200 scoped_ptr<chromeos::MockUserManager> mock_user_manager;
201 mock_user_manager.reset(new chromeos::MockUserManager());
202 mock_user_manager->SetActiveUser("user@gmail.com");
203 chromeos::User* active_user = mock_user_manager->GetActiveUser();
204 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
205 content::NotificationService::AllSources(),
206 content::Details<const chromeos::User>(active_user));
207 base::FilePath expected_logged_in(profile_dir);
208 EXPECT_EQ(expected_logged_in.value(),
209 profile_manager->GetInitialProfileDir().value());
210 VLOG(1) << temp_dir_.path().Append(
211 profile_manager->GetInitialProfileDir()).value();
214 #endif
216 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
217 base::FilePath dest_path1 = temp_dir_.path();
218 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
220 base::FilePath dest_path2 = temp_dir_.path();
221 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
223 ProfileManager* profile_manager = g_browser_process->profile_manager();
225 // Successfully create the profiles.
226 TestingProfile* profile1 =
227 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
228 ASSERT_TRUE(profile1);
230 TestingProfile* profile2 =
231 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
232 ASSERT_TRUE(profile2);
234 // Force lazy-init of some profile services to simulate use.
235 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
236 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1,
237 Profile::EXPLICIT_ACCESS));
238 profile1->CreateBookmarkModel(true);
239 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
240 profile2->CreateBookmarkModel(true);
241 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
242 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
243 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2,
244 Profile::EXPLICIT_ACCESS));
246 // Make sure any pending tasks run before we destroy the profiles.
247 base::RunLoop().RunUntilIdle();
249 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
251 // Make sure history cleans up correctly.
252 base::RunLoop().RunUntilIdle();
255 MATCHER(NotFail, "Profile creation failure status is not reported.") {
256 return arg == Profile::CREATE_STATUS_CREATED ||
257 arg == Profile::CREATE_STATUS_INITIALIZED;
260 MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
261 if (!g_created_profile)
262 g_created_profile = arg;
263 return arg != NULL && arg == g_created_profile;
266 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
267 g_created_profile = NULL;
269 MockObserver mock_observer1;
270 EXPECT_CALL(mock_observer1, OnProfileCreated(
271 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
272 MockObserver mock_observer2;
273 EXPECT_CALL(mock_observer2, OnProfileCreated(
274 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
275 MockObserver mock_observer3;
276 EXPECT_CALL(mock_observer3, OnProfileCreated(
277 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
279 ProfileManager* profile_manager = g_browser_process->profile_manager();
280 const std::string profile_name = "New Profile";
281 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
282 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
283 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
285 base::RunLoop().RunUntilIdle();
288 TEST_F(ProfileManagerTest, CreateProfilesAsync) {
289 const std::string profile_name1 = "New Profile 1";
290 const std::string profile_name2 = "New Profile 2";
292 MockObserver mock_observer;
293 EXPECT_CALL(mock_observer, OnProfileCreated(
294 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
296 ProfileManager* profile_manager = g_browser_process->profile_manager();
298 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
299 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
301 base::RunLoop().RunUntilIdle();
304 TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
305 std::string name = "Managed Profile";
307 MockObserver mock_observer;
308 EXPECT_CALL(mock_observer, OnProfileCreated(
309 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
311 ProfileManager* profile_manager = g_browser_process->profile_manager();
312 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
313 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
315 CreateProfileAsync(profile_manager, name, true, &mock_observer);
316 base::RunLoop().RunUntilIdle();
318 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
319 // Managed profiles should start out omitted from the profile list.
320 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
322 name = "Regular Profile";
323 CreateProfileAsync(profile_manager, name, false, &mock_observer);
324 base::RunLoop().RunUntilIdle();
326 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
327 // Non-managed profiles should be included in the profile list.
328 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
331 TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
332 ProfileManager* profile_manager = g_browser_process->profile_manager();
333 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
334 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
336 const base::FilePath managed_path = temp_dir_.path().AppendASCII("Managed");
337 TestingProfile* managed_profile = new TestingProfile(managed_path, NULL);
338 managed_profile->GetPrefs()->SetString(prefs::kManagedUserId, "An ID");
340 // RegisterTestingProfile adds the profile to the cache and takes ownership.
341 profile_manager->RegisterTestingProfile(managed_profile, true, false);
342 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
343 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
345 const base::FilePath nonmanaged_path = temp_dir_.path().AppendASCII(
346 "Non-Managed");
347 TestingProfile* nonmanaged_profile = new TestingProfile(nonmanaged_path,
348 NULL);
349 profile_manager->RegisterTestingProfile(nonmanaged_profile, true, false);
351 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
352 size_t managed_index = cache.GetIndexOfProfileWithPath(managed_path);
353 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(managed_index));
354 size_t nonmanaged_index = cache.GetIndexOfProfileWithPath(nonmanaged_path);
355 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonmanaged_index));
358 TEST_F(ProfileManagerTest, GetGuestProfilePath) {
359 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
360 base::FilePath expected_path = temp_dir_.path();
361 expected_path = expected_path.Append(chrome::kGuestProfileDir);
362 EXPECT_EQ(expected_path, guest_path);
365 class UnittestGuestProfileManager : public UnittestProfileManager {
366 public:
367 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
368 : UnittestProfileManager(user_data_dir) {}
370 protected:
371 virtual Profile* CreateProfileHelper(
372 const base::FilePath& file_path) OVERRIDE {
373 TestingProfile::Builder builder;
374 builder.SetGuestSession();
375 builder.SetPath(file_path);
376 TestingProfile* testing_profile = builder.Build().release();
377 return testing_profile;
381 class ProfileManagerGuestTest : public ProfileManagerTest {
382 protected:
383 virtual void SetUp() {
384 // Create a new temporary directory, and store the path
385 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
386 TestingBrowserProcess::GetGlobal()->SetProfileManager(
387 new UnittestGuestProfileManager(temp_dir_.path()));
389 #if defined(OS_CHROMEOS)
390 CommandLine* cl = CommandLine::ForCurrentProcess();
391 // This switch is needed to skip non-test specific behavior in
392 // ProfileManager (accessing DBusThreadManager).
393 cl->AppendSwitch(switches::kTestType);
395 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile,
396 std::string(chrome::kProfileDirPrefix) +
397 chromeos::UserManager::kGuestUserName);
398 cl->AppendSwitch(chromeos::switches::kGuestSession);
399 cl->AppendSwitch(::switches::kIncognito);
401 chromeos::UserManager::Get()->UserLoggedIn(
402 chromeos::UserManager::kGuestUserName,
403 chromeos::UserManager::kGuestUserName,
404 false);
405 #endif
409 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
410 ProfileManager* profile_manager = g_browser_process->profile_manager();
411 ASSERT_TRUE(profile_manager);
413 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
414 ASSERT_TRUE(profile);
415 EXPECT_TRUE(profile->IsOffTheRecord());
418 #if defined(OS_CHROMEOS)
419 TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
420 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
421 EXPECT_TRUE(primary_profile->IsOffTheRecord());
423 Profile* active_profile = ProfileManager::GetActiveUserProfile();
424 EXPECT_TRUE(active_profile->IsOffTheRecord());
426 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
428 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
429 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
431 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
433 #endif
435 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
436 ProfileManager* profile_manager = g_browser_process->profile_manager();
437 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
438 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
439 base::Value::CreateBooleanValue(true));
441 // Setting a pref which is not applicable to a system (i.e., Android in this
442 // case) does not necessarily create it. Don't bother continuing with the
443 // test if this pref doesn't exist because it will not load the profiles if
444 // it cannot verify that the pref for background mode is enabled.
445 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
446 return;
448 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
449 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
450 ASCIIToUTF16("name_1"), base::string16(), 0,
451 std::string());
452 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
453 ASCIIToUTF16("name_2"), base::string16(), 0,
454 std::string());
455 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
456 ASCIIToUTF16("name_3"), base::string16(), 0,
457 std::string());
458 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
459 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
460 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
462 profile_manager->AutoloadProfiles();
464 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
467 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
468 ProfileManager* profile_manager = g_browser_process->profile_manager();
469 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
470 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
471 base::Value::CreateBooleanValue(false));
473 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
474 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
475 ASCIIToUTF16("name_1"), base::string16(), 0,
476 std::string());
477 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
478 ASCIIToUTF16("name_2"), base::string16(), 0,
479 std::string());
480 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
481 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
482 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
484 profile_manager->AutoloadProfiles();
486 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
489 TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
490 base::FilePath dest_path = temp_dir_.path();
491 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
493 ProfileManager* profile_manager = g_browser_process->profile_manager();
495 Profile* profile;
497 // Successfully create the profile
498 profile = profile_manager->GetProfile(dest_path);
499 ASSERT_TRUE(profile);
501 // Check that the profile name is non empty
502 std::string profile_name =
503 profile->GetPrefs()->GetString(prefs::kProfileName);
504 EXPECT_FALSE(profile_name.empty());
506 // Check that the profile avatar index is valid
507 size_t avatar_index =
508 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
509 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
510 avatar_index));
513 // Tests that a new profile's entry in the profile info cache is setup with the
514 // same values that are in the profile prefs.
515 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
516 base::FilePath dest_path = temp_dir_.path();
517 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
519 ProfileManager* profile_manager = g_browser_process->profile_manager();
520 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
522 // Successfully create the profile
523 Profile* profile = profile_manager->GetProfile(dest_path);
524 ASSERT_TRUE(profile);
526 std::string profile_name =
527 profile->GetPrefs()->GetString(prefs::kProfileName);
528 size_t avatar_index =
529 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
531 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
533 // Check if the profile prefs are the same as the cache prefs
534 EXPECT_EQ(profile_name,
535 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
536 EXPECT_EQ(avatar_index,
537 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
540 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
541 ProfileManager* profile_manager = g_browser_process->profile_manager();
542 ASSERT_TRUE(profile_manager);
544 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
545 ASSERT_TRUE(profile);
546 EXPECT_FALSE(profile->IsOffTheRecord());
547 PrefService* prefs = profile->GetPrefs();
548 EXPECT_EQ(IncognitoModePrefs::ENABLED,
549 IncognitoModePrefs::GetAvailability(prefs));
551 ASSERT_TRUE(profile->GetOffTheRecordProfile());
553 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
554 EXPECT_FALSE(
555 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
557 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
558 // incognito mode is forced.
559 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
560 EXPECT_TRUE(
561 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
564 #if !defined(OS_ANDROID)
565 // There's no Browser object on Android.
566 TEST_F(ProfileManagerTest, LastOpenedProfiles) {
567 base::FilePath dest_path1 = temp_dir_.path();
568 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
570 base::FilePath dest_path2 = temp_dir_.path();
571 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
573 ProfileManager* profile_manager = g_browser_process->profile_manager();
575 // Successfully create the profiles.
576 TestingProfile* profile1 =
577 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
578 ASSERT_TRUE(profile1);
580 TestingProfile* profile2 =
581 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
582 ASSERT_TRUE(profile2);
584 std::vector<Profile*> last_opened_profiles =
585 profile_manager->GetLastOpenedProfiles();
586 ASSERT_EQ(0U, last_opened_profiles.size());
588 // Create a browser for profile1.
589 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
590 scoped_ptr<Browser> browser1a(
591 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
593 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
594 ASSERT_EQ(1U, last_opened_profiles.size());
595 EXPECT_EQ(profile1, last_opened_profiles[0]);
597 // And for profile2.
598 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
599 scoped_ptr<Browser> browser2(
600 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
602 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
603 ASSERT_EQ(2U, last_opened_profiles.size());
604 EXPECT_EQ(profile1, last_opened_profiles[0]);
605 EXPECT_EQ(profile2, last_opened_profiles[1]);
607 // Adding more browsers doesn't change anything.
608 scoped_ptr<Browser> browser1b(
609 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
610 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
611 ASSERT_EQ(2U, last_opened_profiles.size());
612 EXPECT_EQ(profile1, last_opened_profiles[0]);
613 EXPECT_EQ(profile2, last_opened_profiles[1]);
615 // Close the browsers.
616 browser1a.reset();
617 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
618 ASSERT_EQ(2U, last_opened_profiles.size());
619 EXPECT_EQ(profile1, last_opened_profiles[0]);
620 EXPECT_EQ(profile2, last_opened_profiles[1]);
622 browser1b.reset();
623 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
624 ASSERT_EQ(1U, last_opened_profiles.size());
625 EXPECT_EQ(profile2, last_opened_profiles[0]);
627 browser2.reset();
628 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
629 ASSERT_EQ(0U, last_opened_profiles.size());
632 TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
633 base::FilePath dest_path1 = temp_dir_.path();
634 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
636 base::FilePath dest_path2 = temp_dir_.path();
637 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
639 ProfileManager* profile_manager = g_browser_process->profile_manager();
641 // Successfully create the profiles.
642 TestingProfile* profile1 =
643 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
644 ASSERT_TRUE(profile1);
646 TestingProfile* profile2 =
647 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
648 ASSERT_TRUE(profile2);
650 // Create a browser for profile1.
651 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
652 scoped_ptr<Browser> browser1(
653 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
655 // And for profile2.
656 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
657 scoped_ptr<Browser> browser2(
658 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
660 std::vector<Profile*> last_opened_profiles =
661 profile_manager->GetLastOpenedProfiles();
662 ASSERT_EQ(2U, last_opened_profiles.size());
663 EXPECT_EQ(profile1, last_opened_profiles[0]);
664 EXPECT_EQ(profile2, last_opened_profiles[1]);
666 // Simulate a shutdown.
667 content::NotificationService::current()->Notify(
668 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
669 content::NotificationService::AllSources(),
670 content::NotificationService::NoDetails());
672 // Even if the browsers are destructed during shutdown, the profiles stay
673 // open.
674 browser1.reset();
675 browser2.reset();
677 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
678 ASSERT_EQ(2U, last_opened_profiles.size());
679 EXPECT_EQ(profile1, last_opened_profiles[0]);
680 EXPECT_EQ(profile2, last_opened_profiles[1]);
683 TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
684 base::FilePath dest_path1 = temp_dir_.path();
685 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
686 base::FilePath dest_path2 = temp_dir_.path();
687 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
689 ProfileManager* profile_manager = g_browser_process->profile_manager();
691 // Successfully create the profiles.
692 TestingProfile* profile1 =
693 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
694 ASSERT_TRUE(profile1);
696 // Incognito profiles should not be managed by the profile manager but by the
697 // original profile.
698 TestingProfile::Builder builder;
699 builder.SetIncognito();
700 scoped_ptr<TestingProfile> profile2 = builder.Build();
701 profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
703 std::vector<Profile*> last_opened_profiles =
704 profile_manager->GetLastOpenedProfiles();
705 ASSERT_EQ(0U, last_opened_profiles.size());
707 // Create a browser for profile1.
708 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
709 scoped_ptr<Browser> browser1(
710 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
712 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
713 ASSERT_EQ(1U, last_opened_profiles.size());
714 EXPECT_EQ(profile1, last_opened_profiles[0]);
716 // And for profile2.
717 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
718 chrome::GetActiveDesktop());
719 scoped_ptr<Browser> browser2a(
720 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
722 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
723 ASSERT_EQ(1U, last_opened_profiles.size());
724 EXPECT_EQ(profile1, last_opened_profiles[0]);
726 // Adding more browsers doesn't change anything.
727 scoped_ptr<Browser> browser2b(
728 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
729 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
730 ASSERT_EQ(1U, last_opened_profiles.size());
731 EXPECT_EQ(profile1, last_opened_profiles[0]);
733 // Close the browsers.
734 browser2a.reset();
735 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
736 ASSERT_EQ(1U, last_opened_profiles.size());
737 EXPECT_EQ(profile1, last_opened_profiles[0]);
739 browser2b.reset();
740 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
741 ASSERT_EQ(1U, last_opened_profiles.size());
742 EXPECT_EQ(profile1, last_opened_profiles[0]);
744 browser1.reset();
745 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
746 ASSERT_EQ(0U, last_opened_profiles.size());
748 #endif // !defined(OS_ANDROID)
750 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
751 // There's no Browser object on Android and there's no multi-profiles on Chrome.
752 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
753 base::FilePath dest_path = temp_dir_.path();
754 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
756 ProfileManager* profile_manager = g_browser_process->profile_manager();
758 TestingProfile* profile =
759 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
760 ASSERT_TRUE(profile);
761 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
763 // Here the last used profile is still the "Default" profile.
764 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
765 EXPECT_NE(profile, last_used_profile);
767 // Create a browser for the profile.
768 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
769 scoped_ptr<Browser> browser(
770 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
771 last_used_profile = profile_manager->GetLastUsedProfile();
772 EXPECT_NE(profile, last_used_profile);
774 // Close the browser.
775 browser.reset();
776 last_used_profile = profile_manager->GetLastUsedProfile();
777 EXPECT_NE(profile, last_used_profile);
780 TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
781 base::FilePath dest_path1 = temp_dir_.path();
782 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
784 base::FilePath dest_path2 = temp_dir_.path();
785 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
787 base::FilePath dest_path3 = temp_dir_.path();
788 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
790 ProfileManager* profile_manager = g_browser_process->profile_manager();
792 // Successfully create the profiles.
793 TestingProfile* normal_profile =
794 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
795 ASSERT_TRUE(normal_profile);
797 // Add one ephemeral profile which should not end up in this list.
798 TestingProfile* ephemeral_profile1 =
799 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
800 ASSERT_TRUE(ephemeral_profile1);
801 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
802 true);
804 // Add second ephemeral profile but don't mark it as such yet.
805 TestingProfile* ephemeral_profile2 =
806 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
807 ASSERT_TRUE(ephemeral_profile2);
809 // Create a browser for profile1.
810 Browser::CreateParams profile1_params(normal_profile,
811 chrome::GetActiveDesktop());
812 scoped_ptr<Browser> browser1(
813 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
815 // Create browsers for the ephemeral profile.
816 Browser::CreateParams profile2_params(ephemeral_profile1,
817 chrome::GetActiveDesktop());
818 scoped_ptr<Browser> browser2(
819 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
821 Browser::CreateParams profile3_params(ephemeral_profile2,
822 chrome::GetActiveDesktop());
823 scoped_ptr<Browser> browser3(
824 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
826 std::vector<Profile*> last_opened_profiles =
827 profile_manager->GetLastOpenedProfiles();
828 ASSERT_EQ(2U, last_opened_profiles.size());
829 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
830 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
832 // Mark the second profile ephemeral.
833 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
834 true);
836 // Simulate a shutdown.
837 content::NotificationService::current()->Notify(
838 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
839 content::NotificationService::AllSources(),
840 content::NotificationService::NoDetails());
841 browser1.reset();
842 browser2.reset();
843 browser3.reset();
845 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
846 ASSERT_EQ(1U, last_opened_profiles.size());
847 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
850 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
851 ProfileManager* profile_manager = g_browser_process->profile_manager();
852 ASSERT_TRUE(profile_manager);
854 // Create and load two profiles.
855 const std::string profile_name1 = "New Profile 1";
856 const std::string profile_name2 = "New Profile 2";
857 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
858 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
860 MockObserver mock_observer;
861 EXPECT_CALL(mock_observer, OnProfileCreated(
862 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
864 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
865 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
866 base::RunLoop().RunUntilIdle();
868 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
869 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
871 // Set the active profile.
872 PrefService* local_state = g_browser_process->local_state();
873 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
875 // Delete the active profile.
876 profile_manager->ScheduleProfileForDeletion(dest_path1,
877 ProfileManager::CreateCallback());
878 // Spin the message loop so that all the callbacks can finish running.
879 base::RunLoop().RunUntilIdle();
881 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
882 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
885 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
886 if (!profiles::IsMultipleProfilesEnabled())
887 return;
889 // The command line is reset at the end of every test by the test suite.
890 CommandLine::ForCurrentProcess()->AppendSwitch(
891 switches::kNewProfileManagement);
893 ProfileManager* profile_manager = g_browser_process->profile_manager();
894 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
895 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
897 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
898 const base::string16 default_profile_name =
899 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
900 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
901 Profile* profile1 = AddProfileToCache(profile_manager,
902 "path_1", profile_name1);
903 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
905 // Multiple profiles means displaying the actual profile names.
906 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
907 Profile* profile2 = AddProfileToCache(profile_manager,
908 "path_2", profile_name2);
909 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
910 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
912 // Deleting a profile means returning to the default name.
913 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
914 ProfileManager::CreateCallback());
915 // Spin the message loop so that all the callbacks can finish running.
916 base::RunLoop().RunUntilIdle();
917 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
920 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
921 if (!profiles::IsMultipleProfilesEnabled())
922 return;
924 // The command line is reset at the end of every test by the test suite.
925 CommandLine::ForCurrentProcess()->AppendSwitch(
926 switches::kNewProfileManagement);
928 ProfileManager* profile_manager = g_browser_process->profile_manager();
929 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
930 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
932 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
933 const base::string16 default_profile_name =
934 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
935 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
936 Profile* profile1 = AddProfileToCache(profile_manager,
937 "path_1", profile_name1);
938 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
940 // We should display custom names for local profiles.
941 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
942 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
943 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
944 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
946 // Multiple profiles means displaying the actual profile names.
947 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
948 Profile* profile2 = AddProfileToCache(profile_manager,
949 "path_2", profile_name2);
950 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
951 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
953 // Deleting a profile means returning to the original, custom name.
954 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
955 ProfileManager::CreateCallback());
956 // Spin the message loop so that all the callbacks can finish running.
957 base::RunLoop().RunUntilIdle();
958 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
961 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
962 if (!profiles::IsMultipleProfilesEnabled())
963 return;
965 // The command line is reset at the end of every test by the test suite.
966 CommandLine::ForCurrentProcess()->AppendSwitch(
967 switches::kNewProfileManagement);
969 ProfileManager* profile_manager = g_browser_process->profile_manager();
970 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
971 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
973 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
974 const base::string16 default_profile_name =
975 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
976 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
977 Profile* profile1 = AddProfileToCache(profile_manager,
978 "path_1", profile_name1);
979 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
981 // We should display the actual profile name for signed in profiles.
982 cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("user@gmail.com"));
983 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
984 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
986 // Multiple profiles means displaying the actual profile names.
987 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
988 Profile* profile2 = AddProfileToCache(profile_manager,
989 "path_2", profile_name2);
990 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
991 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
993 // Deleting a profile means returning to the original, actual profile name.
994 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
995 ProfileManager::CreateCallback());
996 // Spin the message loop so that all the callbacks can finish running.
997 base::RunLoop().RunUntilIdle();
998 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
1000 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1002 #if defined(OS_MACOSX)
1003 // These tests are for a Mac-only code path that assumes the browser
1004 // process isn't killed when all browser windows are closed.
1005 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1006 ProfileManager* profile_manager = g_browser_process->profile_manager();
1007 ASSERT_TRUE(profile_manager);
1009 // Create and load one profile, and just create a second profile.
1010 const std::string profile_name1 = "New Profile 1";
1011 const std::string profile_name2 = "New Profile 2";
1012 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1013 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1015 MockObserver mock_observer;
1016 EXPECT_CALL(mock_observer, OnProfileCreated(
1017 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1018 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1019 base::RunLoop().RunUntilIdle();
1021 // Track the profile, but don't load it.
1022 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1023 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1024 base::string16(), 0, std::string());
1025 base::RunLoop().RunUntilIdle();
1027 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1028 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
1030 // Set the active profile.
1031 PrefService* local_state = g_browser_process->local_state();
1032 local_state->SetString(prefs::kProfileLastUsed,
1033 dest_path1.BaseName().MaybeAsASCII());
1035 // Delete the active profile. This should switch and load the unloaded
1036 // profile.
1037 profile_manager->ScheduleProfileForDeletion(dest_path1,
1038 ProfileManager::CreateCallback());
1040 // Spin the message loop so that all the callbacks can finish running.
1041 base::RunLoop().RunUntilIdle();
1043 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1044 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1047 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1048 // by simulating a scenario in which the profile that is being loaded as
1049 // the next active profile has also been marked for deletion, so the
1050 // ProfileManager needs to recursively select a different next profile.
1051 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1052 ProfileManager* profile_manager = g_browser_process->profile_manager();
1053 ASSERT_TRUE(profile_manager);
1055 // Create and load one profile, and create two more profiles.
1056 const std::string profile_name1 = "New Profile 1";
1057 const std::string profile_name2 = "New Profile 2";
1058 const std::string profile_name3 = "New Profile 3";
1059 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1060 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1061 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
1063 MockObserver mock_observer;
1064 EXPECT_CALL(mock_observer, OnProfileCreated(
1065 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1066 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1067 base::RunLoop().RunUntilIdle();
1069 // Create the other profiles, but don't load them. Assign a fake avatar icon
1070 // to ensure that profiles in the info cache are sorted by the profile name,
1071 // and not randomly by the avatar name.
1072 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1073 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1074 ASCIIToUTF16(profile_name2), 1, std::string());
1075 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
1076 ASCIIToUTF16(profile_name3), 2, std::string());
1078 base::RunLoop().RunUntilIdle();
1080 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1081 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1083 // Set the active profile.
1084 PrefService* local_state = g_browser_process->local_state();
1085 local_state->SetString(prefs::kProfileLastUsed,
1086 dest_path1.BaseName().MaybeAsASCII());
1088 // Delete the active profile, Profile1.
1089 // This will post a CreateProfileAsync message, that tries to load Profile2,
1090 // which checks that the profile is not being deleted, and then calls back
1091 // FinishDeletingProfile for Profile1.
1092 // Try to break this flow by setting the active profile to Profile2 in the
1093 // middle (so after the first posted message), and trying to delete Profile2,
1094 // so that the ProfileManager has to look for a different profile to load.
1095 profile_manager->ScheduleProfileForDeletion(dest_path1,
1096 ProfileManager::CreateCallback());
1097 local_state->SetString(prefs::kProfileLastUsed,
1098 dest_path2.BaseName().MaybeAsASCII());
1099 profile_manager->ScheduleProfileForDeletion(dest_path2,
1100 ProfileManager::CreateCallback());
1101 // Spin the message loop so that all the callbacks can finish running.
1102 base::RunLoop().RunUntilIdle();
1104 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1105 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1107 #endif // !defined(OS_MACOSX)