blob: 0ae5714ffb78a4423e2fa81d35e6922e9c9adb0e [file] [log] [blame]
[email protected]52edf812012-01-12 11:48:461// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d99bcaa2010-12-05 20:15:565#include <string>
6
[email protected]943aed5542010-06-11 15:57:087#include "base/command_line.h"
[email protected]537c1082011-12-02 02:37:178#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
[email protected]37ac95b2013-07-23 23:39:3511#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0812#include "base/strings/utf_string_conversions.h"
[email protected]29d70252011-04-28 02:16:5813#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3214#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4915#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3516#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0317#include "chrome/browser/chrome_notification_types.h"
[email protected]363f5272013-04-23 17:21:4218#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]6a2c09f2013-01-25 04:50:0719#include "chrome/browser/history/history_service.h"
[email protected]9718a722012-06-19 20:10:5320#include "chrome/browser/history/history_service_factory.h"
[email protected]63e26822011-07-16 19:07:3521#include "chrome/browser/io_thread.h"
[email protected]81ad7f4a2011-03-16 01:33:2922#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5923#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3324#include "chrome/browser/profiles/profile.h"
[email protected]279170832011-10-12 23:38:0325#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3326#include "chrome/browser/profiles/profile_manager.h"
[email protected]d8748142012-05-16 21:13:4327#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0828#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0830#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5831#include "chrome/common/pref_names.h"
[email protected]7688968a2013-02-12 21:45:1332#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2933#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3534#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1735#include "chrome/test/base/testing_profile.h"
[email protected]ad50def52011-10-19 23:17:0736#include "content/public/browser/notification_service.h"
[email protected]37ac95b2013-07-23 23:39:3537#include "content/public/test/test_browser_thread_bundle.h"
[email protected]844a1002011-04-19 11:37:2138#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2939#include "testing/gtest/include/gtest/gtest.h"
40
[email protected]775caec2011-09-01 17:14:3241#if defined(OS_CHROMEOS)
[email protected]eb5bb2e2013-04-15 20:09:1942#include "chrome/browser/chromeos/login/mock_user_manager.h"
[email protected]9a68d3a2013-04-22 16:26:5443#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]363f5272013-04-23 17:21:4244#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]e4854dc2013-04-24 00:11:5145#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]931d1042013-04-05 17:50:4446#include "chromeos/chromeos_switches.h"
[email protected]775caec2011-09-01 17:14:3247#endif
48
[email protected]6778fed2013-12-24 20:09:3749using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3950using content::BrowserThread;
51
[email protected]844a1002011-04-19 11:37:2152namespace {
[email protected]f4e706252012-01-10 17:11:3653
[email protected]844a1002011-04-19 11:37:2154// This global variable is used to check that value returned to different
55// observers is the same.
56Profile* g_created_profile;
57
[email protected]9819fd02013-08-22 10:49:3958class UnittestProfileManager : public ::ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1759 public:
[email protected]9819fd02013-08-22 10:49:3960 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
[email protected]537c1082011-12-02 02:37:1761 : ::ProfileManagerWithoutInit(user_data_dir) {}
62
63 protected:
[email protected]650b2d52013-02-10 03:41:4564 virtual Profile* CreateProfileHelper(
65 const base::FilePath& file_path) OVERRIDE {
[email protected]7567484142013-07-11 17:36:0766 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5467 if (!base::CreateDirectory(file_path))
[email protected]537c1082011-12-02 02:37:1768 return NULL;
69 }
70 return new TestingProfile(file_path, NULL);
71 }
72
[email protected]650b2d52013-02-10 03:41:4573 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:1774 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,
[email protected]426d1c92013-12-03 20:08:5478 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:1779
80 return new TestingProfile(path, this);
81 }
82};
83
[email protected]9819fd02013-08-22 10:49:3984} // namespace
[email protected]537c1082011-12-02 02:37:1785
[email protected]583844c2011-08-27 00:38:3586class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:4787 protected:
[email protected]f4e706252012-01-10 17:11:3688 class MockObserver {
89 public:
90 MOCK_METHOD2(OnProfileCreated,
91 void(Profile* profile, Profile::CreateStatus status));
92 };
93
[email protected]6cad5bf2011-03-10 21:21:5594 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:3595 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:0996 }
97
initial.commit09911bf2008-07-26 23:55:2998 virtual void SetUp() {
[email protected]3a305db2011-04-12 13:40:5399 // Create a new temporary directory, and store the path
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22101 TestingBrowserProcess::GetGlobal()->SetProfileManager(
[email protected]9819fd02013-08-22 10:49:39102 new UnittestProfileManager(temp_dir_.path()));
[email protected]f89b50f2012-08-16 21:36:50103
[email protected]537c1082011-12-02 02:37:17104#if defined(OS_CHROMEOS)
[email protected]19506d542013-10-15 23:11:06105 CommandLine* cl = CommandLine::ForCurrentProcess();
106 cl->AppendSwitch(switches::kTestType);
[email protected]537c1082011-12-02 02:37:17107#endif
initial.commit09911bf2008-07-26 23:55:29108 }
[email protected]81ad7f4a2011-03-16 01:33:29109
initial.commit09911bf2008-07-26 23:55:29110 virtual void TearDown() {
[email protected]c494d082013-01-04 20:41:22111 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]37ac95b2013-07-23 23:39:35112 base::RunLoop().RunUntilIdle();
initial.commit09911bf2008-07-26 23:55:29113 }
[email protected]f0a51fb52009-03-05 12:46:38114
[email protected]1c62b2f2013-06-28 00:15:00115 // 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)),
[email protected]6778fed2013-12-24 20:09:37123 base::UTF8ToUTF16(name),
[email protected]a04db822013-12-11 19:14:40124 base::string16(),
[email protected]a6e01b42013-08-05 14:13:13125 std::string());
[email protected]1c62b2f2013-06-28 00:15:00126 }
127
[email protected]6f96cbcb2011-11-04 02:26:07128#if defined(OS_CHROMEOS)
[email protected]e4854dc2013-04-24 00:11:51129 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
[email protected]363f5272013-04-23 17:21:42130 chromeos::ScopedTestCrosSettings test_cros_settings_;
[email protected]6f96cbcb2011-11-04 02:26:07131#endif
132
[email protected]3a305db2011-04-12 13:40:53133 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23134 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35135 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35136
137 content::TestBrowserThreadBundle thread_bundle_;
[email protected]363f5272013-04-23 17:21:42138
139#if defined(OS_CHROMEOS)
140 chromeos::ScopedTestUserManager test_user_manager_;
141#endif
initial.commit09911bf2008-07-26 23:55:29142};
143
[email protected]844a1002011-04-19 11:37:21144TEST_F(ProfileManagerTest, GetProfile) {
[email protected]650b2d52013-02-10 03:41:45145 base::FilePath dest_path = temp_dir_.path();
[email protected]f7011fcb2009-01-28 21:54:32146 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29147
[email protected]881cb0b62011-12-13 20:39:54148 ProfileManager* profile_manager = g_browser_process->profile_manager();
149
initial.commit09911bf2008-07-26 23:55:29150 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36151 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21152 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29153
[email protected]844a1002011-04-19 11:37:21154 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54155 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29156}
[email protected]55474b572009-04-14 22:05:33157
[email protected]943aed5542010-06-11 15:57:08158TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45159 base::FilePath expected_default =
160 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54161 EXPECT_EQ(
162 expected_default.value(),
163 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08164}
165
166#if defined(OS_CHROMEOS)
167// This functionality only exists on Chrome OS.
168TEST_F(ProfileManagerTest, LoggedInProfileDir) {
169 CommandLine *cl = CommandLine::ForCurrentProcess();
[email protected]9c8b1f92013-09-19 14:39:59170 std::string profile_dir(chrome::kTestUserProfileDir);
[email protected]943aed5542010-06-11 15:57:08171
[email protected]931d1042013-04-05 17:50:44172 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir);
[email protected]943aed5542010-06-11 15:57:08173
[email protected]650b2d52013-02-10 03:41:45174 base::FilePath expected_default =
175 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54176 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08177 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54178 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08179
[email protected]eb5bb2e2013-04-15 20:09:19180 scoped_ptr<chromeos::MockUserManager> mock_user_manager;
181 mock_user_manager.reset(new chromeos::MockUserManager());
182 mock_user_manager->SetActiveUser("[email protected]");
183 chromeos::User* active_user = mock_user_manager->GetActiveUser();
[email protected]881cb0b62011-12-13 20:39:54184 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
[email protected]ad50def52011-10-19 23:17:07185 content::NotificationService::AllSources(),
[email protected]eb5bb2e2013-04-15 20:09:19186 content::Details<const chromeos::User>(active_user));
[email protected]650b2d52013-02-10 03:41:45187 base::FilePath expected_logged_in(profile_dir);
[email protected]943aed5542010-06-11 15:57:08188 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54189 profile_manager->GetInitialProfileDir().value());
[email protected]3a305db2011-04-12 13:40:53190 VLOG(1) << temp_dir_.path().Append(
[email protected]881cb0b62011-12-13 20:39:54191 profile_manager->GetInitialProfileDir()).value();
[email protected]943aed5542010-06-11 15:57:08192}
193
194#endif
195
[email protected]52d69b882010-06-17 17:35:51196TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
[email protected]650b2d52013-02-10 03:41:45197 base::FilePath dest_path1 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33198 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47199
[email protected]650b2d52013-02-10 03:41:45200 base::FilePath dest_path2 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33201 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
202
[email protected]881cb0b62011-12-13 20:39:54203 ProfileManager* profile_manager = g_browser_process->profile_manager();
204
[email protected]55474b572009-04-14 22:05:33205 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17206 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54207 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21208 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33209
[email protected]537c1082011-12-02 02:37:17210 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54211 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21212 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33213
214 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31215 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53216 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1,
217 Profile::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17218 profile1->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49219 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
[email protected]537c1082011-12-02 02:37:17220 profile2->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49221 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
[email protected]608e7e02013-07-24 12:23:31222 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53223 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2,
224 Profile::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55225
226 // Make sure any pending tasks run before we destroy the profiles.
[email protected]37ac95b2013-07-23 23:39:35227 base::RunLoop().RunUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55228
[email protected]c494d082013-01-04 20:41:22229 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]6cad5bf2011-03-10 21:21:55230
[email protected]237e6d02010-11-08 21:45:42231 // Make sure history cleans up correctly.
[email protected]37ac95b2013-07-23 23:39:35232 base::RunLoop().RunUntilIdle();
[email protected]55474b572009-04-14 22:05:33233}
[email protected]844a1002011-04-19 11:37:21234
[email protected]e547ac82011-07-25 12:34:11235MATCHER(NotFail, "Profile creation failure status is not reported.") {
[email protected]b8ba8d42011-12-07 22:13:59236 return arg == Profile::CREATE_STATUS_CREATED ||
237 arg == Profile::CREATE_STATUS_INITIALIZED;
[email protected]e547ac82011-07-25 12:34:11238}
239
[email protected]844a1002011-04-19 11:37:21240// Tests asynchronous profile creation mechanism.
[email protected]fea25d82012-12-19 01:19:50241// Crashes: http://crbug.com/89421
[email protected]dd6f4b12011-07-15 15:55:18242TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) {
[email protected]844a1002011-04-19 11:37:21243 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11244 EXPECT_CALL(mock_observer, OnProfileCreated(
245 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21246
[email protected]1c62b2f2013-06-28 00:15:00247 CreateProfileAsync(g_browser_process->profile_manager(),
248 "New Profile", &mock_observer);
[email protected]844a1002011-04-19 11:37:21249
[email protected]37ac95b2013-07-23 23:39:35250 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21251}
252
[email protected]e547ac82011-07-25 12:34:11253MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
[email protected]844a1002011-04-19 11:37:21254 if (!g_created_profile)
255 g_created_profile = arg;
[email protected]e547ac82011-07-25 12:34:11256 return arg != NULL && arg == g_created_profile;
[email protected]844a1002011-04-19 11:37:21257}
258
259TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
[email protected]844a1002011-04-19 11:37:21260 g_created_profile = NULL;
261
262 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11263 EXPECT_CALL(mock_observer1, OnProfileCreated(
264 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21265 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11266 EXPECT_CALL(mock_observer2, OnProfileCreated(
267 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21268 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11269 EXPECT_CALL(mock_observer3, OnProfileCreated(
270 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21271
[email protected]881cb0b62011-12-13 20:39:54272 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00273 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);
[email protected]844a1002011-04-19 11:37:21277
[email protected]37ac95b2013-07-23 23:39:35278 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21279}
280
281TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00282 const std::string profile_name1 = "New Profile 1";
283 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21284
285 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11286 EXPECT_CALL(mock_observer, OnProfileCreated(
287 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21288
[email protected]881cb0b62011-12-13 20:39:54289 ProfileManager* profile_manager = g_browser_process->profile_manager();
290
[email protected]1c62b2f2013-06-28 00:15:00291 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
292 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
[email protected]844a1002011-04-19 11:37:21293
[email protected]37ac95b2013-07-23 23:39:35294 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21295}
[email protected]279170832011-10-12 23:38:03296
[email protected]4251165a2013-07-17 04:33:40297TEST_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);
302}
303
[email protected]279170832011-10-12 23:38:03304TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54305 ProfileManager* profile_manager = g_browser_process->profile_manager();
306 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
[email protected]714bf1d2012-11-30 01:35:27307 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]dbb9aa42013-12-23 20:08:21308 base::Value::CreateBooleanValue(true));
[email protected]714bf1d2012-11-30 01:35:27309
310 // Setting a pref which is not applicable to a system (i.e., Android in this
311 // case) does not necessarily create it. Don't bother continuing with the
312 // test if this pref doesn't exist because it will not load the profiles if
313 // it cannot verify that the pref for background mode is enabled.
314 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
315 return;
[email protected]279170832011-10-12 23:38:03316
317 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
318 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40319 ASCIIToUTF16("name_1"), base::string16(), 0,
320 std::string());
[email protected]279170832011-10-12 23:38:03321 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40322 ASCIIToUTF16("name_2"), base::string16(), 0,
323 std::string());
[email protected]279170832011-10-12 23:38:03324 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
[email protected]a04db822013-12-11 19:14:40325 ASCIIToUTF16("name_3"), base::string16(), 0,
326 std::string());
[email protected]279170832011-10-12 23:38:03327 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
328 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
329 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
330
[email protected]881cb0b62011-12-13 20:39:54331 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03332
[email protected]881cb0b62011-12-13 20:39:54333 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
334}
335
[email protected]714bf1d2012-11-30 01:35:27336TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
337 ProfileManager* profile_manager = g_browser_process->profile_manager();
338 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
339 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]dbb9aa42013-12-23 20:08:21340 base::Value::CreateBooleanValue(false));
[email protected]714bf1d2012-11-30 01:35:27341
342 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
343 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40344 ASCIIToUTF16("name_1"), base::string16(), 0,
345 std::string());
[email protected]714bf1d2012-11-30 01:35:27346 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40347 ASCIIToUTF16("name_2"), base::string16(), 0,
348 std::string());
[email protected]714bf1d2012-11-30 01:35:27349 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
350 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
351 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
352
353 profile_manager->AutoloadProfiles();
354
355 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
356}
357
[email protected]881cb0b62011-12-13 20:39:54358TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
[email protected]650b2d52013-02-10 03:41:45359 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54360 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
361
362 ProfileManager* profile_manager = g_browser_process->profile_manager();
363
364 Profile* profile;
365
366 // Successfully create the profile
367 profile = profile_manager->GetProfile(dest_path);
368 ASSERT_TRUE(profile);
369
370 // Check that the profile name is non empty
371 std::string profile_name =
372 profile->GetPrefs()->GetString(prefs::kProfileName);
373 EXPECT_FALSE(profile_name.empty());
374
375 // Check that the profile avatar index is valid
376 size_t avatar_index =
377 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
378 EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex(
379 avatar_index));
380}
381
382// Tests that a new profile's entry in the profile info cache is setup with the
383// same values that are in the profile prefs.
384TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
[email protected]650b2d52013-02-10 03:41:45385 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54386 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
387
388 ProfileManager* profile_manager = g_browser_process->profile_manager();
389 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
390
391 // Successfully create the profile
392 Profile* profile = profile_manager->GetProfile(dest_path);
393 ASSERT_TRUE(profile);
394
395 std::string profile_name =
396 profile->GetPrefs()->GetString(prefs::kProfileName);
397 size_t avatar_index =
398 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
399
400 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
401
402 // Check if the profile prefs are the same as the cache prefs
403 EXPECT_EQ(profile_name,
[email protected]6778fed2013-12-24 20:09:37404 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
[email protected]881cb0b62011-12-13 20:39:54405 EXPECT_EQ(avatar_index,
406 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
[email protected]279170832011-10-12 23:38:03407}
[email protected]52edf812012-01-12 11:48:46408
[email protected]13720532013-05-30 12:30:59409TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
410 ProfileManager* profile_manager = g_browser_process->profile_manager();
411 ASSERT_TRUE(profile_manager);
412
413 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
414 ASSERT_TRUE(profile);
415 EXPECT_FALSE(profile->IsOffTheRecord());
416 PrefService* prefs = profile->GetPrefs();
417 EXPECT_EQ(IncognitoModePrefs::ENABLED,
418 IncognitoModePrefs::GetAvailability(prefs));
419
420 // Attach an incognito Profile to the TestingProfile.
421 ASSERT_FALSE(profile->GetOffTheRecordProfile());
[email protected]9819fd02013-08-22 10:49:39422 TestingProfile::Builder builder;
423 builder.SetIncognito();
424 scoped_ptr<TestingProfile> incognito_profile = builder.Build();
[email protected]13720532013-05-30 12:30:59425 EXPECT_TRUE(incognito_profile->IsOffTheRecord());
426 TestingProfile* testing_profile = static_cast<TestingProfile*>(profile);
[email protected]9819fd02013-08-22 10:49:39427 testing_profile->SetOffTheRecordProfile(incognito_profile.PassAs<Profile>());
[email protected]13720532013-05-30 12:30:59428 ASSERT_TRUE(profile->GetOffTheRecordProfile());
429
430 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
431 EXPECT_FALSE(
432 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
433
434 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
435 // incognito mode is forced.
436 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
437 EXPECT_TRUE(
438 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
439}
440
[email protected]c849fab2012-03-29 16:51:41441#if !defined(OS_ANDROID)
442// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46443TEST_F(ProfileManagerTest, LastOpenedProfiles) {
[email protected]650b2d52013-02-10 03:41:45444 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46445 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
446
[email protected]650b2d52013-02-10 03:41:45447 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46448 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
449
450 ProfileManager* profile_manager = g_browser_process->profile_manager();
451
452 // Successfully create the profiles.
453 TestingProfile* profile1 =
454 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
455 ASSERT_TRUE(profile1);
456
457 TestingProfile* profile2 =
458 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
459 ASSERT_TRUE(profile2);
460
461 std::vector<Profile*> last_opened_profiles =
462 profile_manager->GetLastOpenedProfiles();
463 ASSERT_EQ(0U, last_opened_profiles.size());
464
465 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34466 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29467 scoped_ptr<Browser> browser1a(
[email protected]c5f3e1842013-02-20 17:10:41468 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46469
470 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
471 ASSERT_EQ(1U, last_opened_profiles.size());
472 EXPECT_EQ(profile1, last_opened_profiles[0]);
473
474 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34475 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29476 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41477 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46478
479 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
480 ASSERT_EQ(2U, last_opened_profiles.size());
481 EXPECT_EQ(profile1, last_opened_profiles[0]);
482 EXPECT_EQ(profile2, last_opened_profiles[1]);
483
484 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29485 scoped_ptr<Browser> browser1b(
[email protected]c5f3e1842013-02-20 17:10:41486 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46487 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
488 ASSERT_EQ(2U, last_opened_profiles.size());
489 EXPECT_EQ(profile1, last_opened_profiles[0]);
490 EXPECT_EQ(profile2, last_opened_profiles[1]);
491
492 // Close the browsers.
493 browser1a.reset();
494 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
495 ASSERT_EQ(2U, last_opened_profiles.size());
496 EXPECT_EQ(profile1, last_opened_profiles[0]);
497 EXPECT_EQ(profile2, last_opened_profiles[1]);
498
499 browser1b.reset();
500 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
501 ASSERT_EQ(1U, last_opened_profiles.size());
502 EXPECT_EQ(profile2, last_opened_profiles[0]);
503
504 browser2.reset();
505 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
506 ASSERT_EQ(0U, last_opened_profiles.size());
507}
508
509TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
[email protected]650b2d52013-02-10 03:41:45510 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46511 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
512
[email protected]650b2d52013-02-10 03:41:45513 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46514 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
515
516 ProfileManager* profile_manager = g_browser_process->profile_manager();
517
518 // Successfully create the profiles.
519 TestingProfile* profile1 =
520 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
521 ASSERT_TRUE(profile1);
522
523 TestingProfile* profile2 =
524 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
525 ASSERT_TRUE(profile2);
526
527 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34528 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29529 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41530 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46531
532 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34533 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29534 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41535 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46536
537 std::vector<Profile*> last_opened_profiles =
538 profile_manager->GetLastOpenedProfiles();
539 ASSERT_EQ(2U, last_opened_profiles.size());
540 EXPECT_EQ(profile1, last_opened_profiles[0]);
541 EXPECT_EQ(profile2, last_opened_profiles[1]);
542
543 // Simulate a shutdown.
544 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30545 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:46546 content::NotificationService::AllSources(),
547 content::NotificationService::NoDetails());
548
549 // Even if the browsers are destructed during shutdown, the profiles stay
550 // open.
551 browser1.reset();
552 browser2.reset();
553
554 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
555 ASSERT_EQ(2U, last_opened_profiles.size());
556 EXPECT_EQ(profile1, last_opened_profiles[0]);
557 EXPECT_EQ(profile2, last_opened_profiles[1]);
558}
[email protected]41276342012-02-06 10:49:31559
560TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
[email protected]650b2d52013-02-10 03:41:45561 base::FilePath dest_path1 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31562 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]650b2d52013-02-10 03:41:45563 base::FilePath dest_path2 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31564 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
565
566 ProfileManager* profile_manager = g_browser_process->profile_manager();
567
568 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:06569 TestingProfile* profile1 =
570 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:31571 ASSERT_TRUE(profile1);
572
[email protected]4dffabe2012-05-19 14:37:06573 // incognito profiles should not be managed by the profile manager but by the
574 // original profile.
[email protected]9819fd02013-08-22 10:49:39575 TestingProfile::Builder builder;
576 builder.SetIncognito();
577 scoped_ptr<TestingProfile> profile2 = builder.Build();
578 profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
[email protected]41276342012-02-06 10:49:31579
580 std::vector<Profile*> last_opened_profiles =
581 profile_manager->GetLastOpenedProfiles();
582 ASSERT_EQ(0U, last_opened_profiles.size());
583
584 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34585 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29586 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41587 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:31588
589 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
590 ASSERT_EQ(1U, last_opened_profiles.size());
591 EXPECT_EQ(profile1, last_opened_profiles[0]);
592
593 // And for profile2.
[email protected]9819fd02013-08-22 10:49:39594 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
595 chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29596 scoped_ptr<Browser> browser2a(
[email protected]c5f3e1842013-02-20 17:10:41597 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31598
599 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
600 ASSERT_EQ(1U, last_opened_profiles.size());
601 EXPECT_EQ(profile1, last_opened_profiles[0]);
602
603 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29604 scoped_ptr<Browser> browser2b(
[email protected]c5f3e1842013-02-20 17:10:41605 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31606 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
607 ASSERT_EQ(1U, last_opened_profiles.size());
608 EXPECT_EQ(profile1, last_opened_profiles[0]);
609
610 // Close the browsers.
611 browser2a.reset();
612 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
613 ASSERT_EQ(1U, last_opened_profiles.size());
614 EXPECT_EQ(profile1, last_opened_profiles[0]);
615
616 browser2b.reset();
617 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
618 ASSERT_EQ(1U, last_opened_profiles.size());
619 EXPECT_EQ(profile1, last_opened_profiles[0]);
620
621 browser1.reset();
622 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
623 ASSERT_EQ(0U, last_opened_profiles.size());
624}
[email protected]c849fab2012-03-29 16:51:41625#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:00626
627#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
628// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:48629TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
630 base::FilePath dest_path = temp_dir_.path();
631 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
632
633 ProfileManager* profile_manager = g_browser_process->profile_manager();
634
635 TestingProfile* profile =
636 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
637 ASSERT_TRUE(profile);
638 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
639
640 // Here the last used profile is still the "Default" profile.
641 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
642 EXPECT_NE(profile, last_used_profile);
643
[email protected]79fd6b72013-10-22 16:39:27644 // Create a browser for the profile.
[email protected]8d9243a2013-10-21 17:11:48645 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
646 scoped_ptr<Browser> browser(
647 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
648 last_used_profile = profile_manager->GetLastUsedProfile();
649 EXPECT_NE(profile, last_used_profile);
650
651 // Close the browser.
652 browser.reset();
653 last_used_profile = profile_manager->GetLastUsedProfile();
654 EXPECT_NE(profile, last_used_profile);
655}
656
657TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
658 base::FilePath dest_path1 = temp_dir_.path();
659 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
660
661 base::FilePath dest_path2 = temp_dir_.path();
[email protected]79fd6b72013-10-22 16:39:27662 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
663
664 base::FilePath dest_path3 = temp_dir_.path();
665 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:48666
667 ProfileManager* profile_manager = g_browser_process->profile_manager();
668
669 // Successfully create the profiles.
670 TestingProfile* normal_profile =
671 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
672 ASSERT_TRUE(normal_profile);
673
674 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:27675 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:48676 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:27677 ASSERT_TRUE(ephemeral_profile1);
678 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
679 true);
680
681 // Add second ephemeral profile but don't mark it as such yet.
682 TestingProfile* ephemeral_profile2 =
683 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
684 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:48685
686 // Create a browser for profile1.
687 Browser::CreateParams profile1_params(normal_profile,
688 chrome::GetActiveDesktop());
689 scoped_ptr<Browser> browser1(
690 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
691
[email protected]79fd6b72013-10-22 16:39:27692 // Create browsers for the ephemeral profile.
693 Browser::CreateParams profile2_params(ephemeral_profile1,
[email protected]8d9243a2013-10-21 17:11:48694 chrome::GetActiveDesktop());
695 scoped_ptr<Browser> browser2(
696 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
697
[email protected]79fd6b72013-10-22 16:39:27698 Browser::CreateParams profile3_params(ephemeral_profile2,
699 chrome::GetActiveDesktop());
700 scoped_ptr<Browser> browser3(
701 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
702
[email protected]8d9243a2013-10-21 17:11:48703 std::vector<Profile*> last_opened_profiles =
704 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:27705 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:48706 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:27707 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
708
709 // Mark the second profile ephemeral.
710 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
711 true);
[email protected]8d9243a2013-10-21 17:11:48712
713 // Simulate a shutdown.
714 content::NotificationService::current()->Notify(
715 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
716 content::NotificationService::AllSources(),
717 content::NotificationService::NoDetails());
718 browser1.reset();
719 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:27720 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:48721
722 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
723 ASSERT_EQ(1U, last_opened_profiles.size());
724 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
725}
726
[email protected]1c62b2f2013-06-28 00:15:00727TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
728 ProfileManager* profile_manager = g_browser_process->profile_manager();
729 ASSERT_TRUE(profile_manager);
730
731 // Create and load two profiles.
732 const std::string profile_name1 = "New Profile 1";
733 const std::string profile_name2 = "New Profile 2";
734 base::FilePath dest_path1 =
735 temp_dir_.path().AppendASCII(profile_name1);
736 base::FilePath dest_path2 =
737 temp_dir_.path().AppendASCII(profile_name2);
738
739 MockObserver mock_observer;
740 EXPECT_CALL(mock_observer, OnProfileCreated(
741 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
742
743 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
744 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35745 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00746
747 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
748 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
749
750 // Set the active profile.
751 PrefService* local_state = g_browser_process->local_state();
752 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
753
754 // Delete the active profile.
755 profile_manager->ScheduleProfileForDeletion(dest_path1,
756 ProfileManager::CreateCallback());
757 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35758 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00759
760 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
761 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
762}
763#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
764
765#if defined(OS_MACOSX)
766// These tests are for a Mac-only code path that assumes the browser
767// process isn't killed when all browser windows are closed.
768TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
769 ProfileManager* profile_manager = g_browser_process->profile_manager();
770 ASSERT_TRUE(profile_manager);
771
772 // Create and load one profile, and just create a second profile.
773 const std::string profile_name1 = "New Profile 1";
774 const std::string profile_name2 = "New Profile 2";
775 base::FilePath dest_path1 =
776 temp_dir_.path().AppendASCII(profile_name1);
777 base::FilePath dest_path2 =
778 temp_dir_.path().AppendASCII(profile_name2);
779
780 MockObserver mock_observer;
781 EXPECT_CALL(mock_observer, OnProfileCreated(
782 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
783 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35784 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00785
786 // Track the profile, but don't load it.
787 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
788 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a04db822013-12-11 19:14:40789 base::string16(), 0, std::string());
[email protected]37ac95b2013-07-23 23:39:35790 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00791
792 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
793 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
794
795 // Set the active profile.
796 PrefService* local_state = g_browser_process->local_state();
797 local_state->SetString(prefs::kProfileLastUsed,
798 dest_path1.BaseName().MaybeAsASCII());
799
800 // Delete the active profile. This should switch and load the unloaded
801 // profile.
802 profile_manager->ScheduleProfileForDeletion(dest_path1,
803 ProfileManager::CreateCallback());
804
805 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35806 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00807
808 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
809 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
810}
811
812// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
813// by simulating a scenario in which the profile that is being loaded as
814// the next active profile has also been marked for deletion, so the
815// ProfileManager needs to recursively select a different next profile.
816TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
817 ProfileManager* profile_manager = g_browser_process->profile_manager();
818 ASSERT_TRUE(profile_manager);
819
820 // Create and load one profile, and create two more profiles.
821 const std::string profile_name1 = "New Profile 1";
822 const std::string profile_name2 = "New Profile 2";
823 const std::string profile_name3 = "New Profile 3";
824 base::FilePath dest_path1 =
825 temp_dir_.path().AppendASCII(profile_name1);
826 base::FilePath dest_path2 =
827 temp_dir_.path().AppendASCII(profile_name2);
828 base::FilePath dest_path3 =
829 temp_dir_.path().AppendASCII(profile_name3);
830
831 MockObserver mock_observer;
832 EXPECT_CALL(mock_observer, OnProfileCreated(
833 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
834 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35835 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00836
837 // Create the other profiles, but don't load them. Assign a fake avatar icon
838 // to ensure that profiles in the info cache are sorted by the profile name,
839 // and not randomly by the avatar name.
840 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
841 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a6e01b42013-08-05 14:13:13842 ASCIIToUTF16(profile_name2), 1, std::string());
[email protected]1c62b2f2013-06-28 00:15:00843 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
[email protected]a6e01b42013-08-05 14:13:13844 ASCIIToUTF16(profile_name3), 2, std::string());
[email protected]1c62b2f2013-06-28 00:15:00845
[email protected]37ac95b2013-07-23 23:39:35846 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00847
848 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
849 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
850
851 // Set the active profile.
852 PrefService* local_state = g_browser_process->local_state();
853 local_state->SetString(prefs::kProfileLastUsed,
854 dest_path1.BaseName().MaybeAsASCII());
855
856 // Delete the active profile, Profile1.
857 // This will post a CreateProfileAsync message, that tries to load Profile2,
858 // which checks that the profile is not being deleted, and then calls back
859 // FinishDeletingProfile for Profile1.
860 // Try to break this flow by setting the active profile to Profile2 in the
861 // middle (so after the first posted message), and trying to delete Profile2,
862 // so that the ProfileManager has to look for a different profile to load.
863 profile_manager->ScheduleProfileForDeletion(dest_path1,
864 ProfileManager::CreateCallback());
865 local_state->SetString(prefs::kProfileLastUsed,
866 dest_path2.BaseName().MaybeAsASCII());
867 profile_manager->ScheduleProfileForDeletion(dest_path2,
868 ProfileManager::CreateCallback());
869 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35870 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00871
872 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
873 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
874}
875#endif // !defined(OS_MACOSX)