blob: 3861733308d90b6b693d4bd06c1ad709443a7152 [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
avib896c712015-12-26 02:10:435#include <stddef.h>
6
[email protected]d99bcaa2010-12-05 20:15:567#include <string>
8
[email protected]943aed5542010-06-11 15:57:089#include "base/command_line.h"
miguelg1b495542016-03-18 17:57:4610#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0411#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
avib896c712015-12-26 02:10:4313#include "base/macros.h"
vabr8684c9a2017-03-29 13:14:5714#include "base/memory/ptr_util.h"
[email protected]37ac95b2013-07-23 23:39:3515#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0816#include "base/strings/utf_string_conversions.h"
Gabriel Charette0b08bf22017-08-15 20:10:0617#include "base/threading/thread_task_runner_handle.h"
[email protected]29d70252011-04-28 02:16:5818#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3219#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4920#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3521#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0322#include "chrome/browser/chrome_notification_types.h"
[email protected]9718a722012-06-19 20:10:5323#include "chrome/browser/history/history_service_factory.h"
[email protected]63e26822011-07-16 19:07:3524#include "chrome/browser/io_thread.h"
[email protected]81ad7f4a2011-03-16 01:33:2925#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5926#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3327#include "chrome/browser/profiles/profile.h"
[email protected]c3e559772014-04-09 04:02:5428#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]279170832011-10-12 23:38:0329#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3330#include "chrome/browser/profiles/profile_manager.h"
[email protected]3f130532014-02-26 20:39:2431#include "chrome/browser/profiles/profiles_state.h"
[email protected]d8748142012-05-16 21:13:4332#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0833#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0835#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5836#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1837#include "chrome/grit/generated_resources.h"
[email protected]7688968a2013-02-12 21:45:1338#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2939#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3540#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1741#include "chrome/test/base/testing_profile.h"
sdefresne506dd522015-03-12 18:49:1242#include "components/history/core/browser/history_service.h"
[email protected]29896ee2014-06-17 17:20:5343#include "components/signin/core/common/profile_management_switches.h"
[email protected]ad50def52011-10-19 23:17:0744#include "content/public/browser/notification_service.h"
[email protected]f9357a442014-05-15 18:44:0745#include "content/public/common/content_switches.h"
[email protected]37ac95b2013-07-23 23:39:3546#include "content/public/test/test_browser_thread_bundle.h"
Gabriel Charettefaf432192017-08-17 22:00:4147#include "content/public/test/test_utils.h"
[email protected]844a1002011-04-19 11:37:2148#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2949#include "testing/gtest/include/gtest/gtest.h"
[email protected]3f130532014-02-26 20:39:2450#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2951
[email protected]775caec2011-09-01 17:14:3252#if defined(OS_CHROMEOS)
merkulova793f3022015-02-04 10:18:3053#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
[email protected]4d390782014-08-15 09:22:5854#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
dzhioev0298d722014-08-26 13:18:5255#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
xdaid05f51c2015-03-23 17:21:3356#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
dzhioev0298d722014-08-26 13:18:5257#include "chrome/browser/chromeos/profiles/profile_helper.h"
[email protected]363f5272013-04-23 17:21:4258#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]e4854dc2013-04-24 00:11:5159#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]931d1042013-04-05 17:50:4460#include "chromeos/chromeos_switches.h"
[email protected]4d390782014-08-15 09:22:5861#include "components/user_manager/user_manager.h"
alemate3c1c0882016-10-27 19:05:1262#include "components/user_manager/user_names.h"
Andrew Wilsonc6c2c37e2017-09-01 14:44:2763#include "extensions/common/features/feature_session_type.h"
dzhioev0298d722014-08-26 13:18:5264#endif // defined(OS_CHROMEOS)
[email protected]775caec2011-09-01 17:14:3265
[email protected]6778fed2013-12-24 20:09:3766using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3967
[email protected]844a1002011-04-19 11:37:2168namespace {
[email protected]f4e706252012-01-10 17:11:3669
[email protected]844a1002011-04-19 11:37:2170// This global variable is used to check that value returned to different
71// observers is the same.
72Profile* g_created_profile;
73
[email protected]9819fd02013-08-22 10:49:3974class UnittestProfileManager : public ::ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1775 public:
[email protected]9819fd02013-08-22 10:49:3976 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
[email protected]537c1082011-12-02 02:37:1777 : ::ProfileManagerWithoutInit(user_data_dir) {}
78
79 protected:
dcheng1fd716382014-10-22 20:14:4180 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]7567484142013-07-11 17:36:0781 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5482 if (!base::CreateDirectory(file_path))
[email protected]537c1082011-12-02 02:37:1783 return NULL;
84 }
85 return new TestingProfile(file_path, NULL);
86 }
87
dcheng1fd716382014-10-22 20:14:4188 Profile* CreateProfileAsyncHelper(const base::FilePath& path,
89 Delegate* delegate) override {
Gabriel Charette0b08bf22017-08-15 20:10:0690 // ThreadTaskRunnerHandle::Get() is TestingProfile's "async" IOTaskRunner
91 // (ref. TestingProfile::GetIOTaskRunner()).
92 base::ThreadTaskRunnerHandle::Get()->PostTask(
93 FROM_HERE,
tzik6e4834a2017-04-20 15:42:5894 base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:1795
96 return new TestingProfile(path, this);
97 }
98};
99
miguelg1b495542016-03-18 17:57:46100void ExpectNullProfile(base::Closure closure, Profile* profile) {
101 EXPECT_EQ(nullptr, profile);
102 closure.Run();
103}
104
105void ExpectProfileWithName(const std::string& profile_name,
106 bool incognito,
107 base::Closure closure,
108 Profile* profile) {
109 EXPECT_NE(nullptr, profile);
110 EXPECT_EQ(incognito, profile->IsOffTheRecord());
111 if (incognito)
112 profile = profile->GetOriginalProfile();
113
114 // Create a profile on the fly so the the same comparison
115 // can be used in Windows and other platforms.
116 EXPECT_EQ(base::FilePath().AppendASCII(profile_name),
117 profile->GetPath().BaseName());
118 closure.Run();
119}
120
[email protected]9819fd02013-08-22 10:49:39121} // namespace
[email protected]537c1082011-12-02 02:37:17122
[email protected]583844c2011-08-27 00:38:35123class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:47124 protected:
[email protected]f4e706252012-01-10 17:11:36125 class MockObserver {
126 public:
127 MOCK_METHOD2(OnProfileCreated,
128 void(Profile* profile, Profile::CreateStatus status));
129 };
130
[email protected]6cad5bf2011-03-10 21:21:55131 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:35132 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:09133 }
134
dchenge1bc7982014-10-30 00:32:40135 void SetUp() override {
[email protected]3a305db2011-04-12 13:40:53136 // Create a new temporary directory, and store the path
137 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22138 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22139 new UnittestProfileManager(temp_dir_.GetPath()));
[email protected]f89b50f2012-08-16 21:36:50140
[email protected]537c1082011-12-02 02:37:17141#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43142 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
dzhioev0298d722014-08-26 13:18:52143 cl->AppendSwitch(switches::kTestType);
xdaid05f51c2015-03-23 17:21:33144 chromeos::WallpaperManager::Initialize();
Andrew Wilsonc6c2c37e2017-09-01 14:44:27145
146 // Have to manually reset the session type in between test runs because
147 // some tests log in users.
148 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
149 extensions::GetCurrentFeatureSessionType());
150 session_type_ = extensions::ScopedCurrentFeatureSessionType(
151 extensions::GetCurrentFeatureSessionType());
[email protected]537c1082011-12-02 02:37:17152#endif
initial.commit09911bf2008-07-26 23:55:29153 }
[email protected]81ad7f4a2011-03-16 01:33:29154
dchenge1bc7982014-10-30 00:32:40155 void TearDown() override {
[email protected]c494d082013-01-04 20:41:22156 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
Gabriel Charette01507a22017-09-27 21:30:08157 content::RunAllTasksUntilIdle();
xdaid05f51c2015-03-23 17:21:33158#if defined(OS_CHROMEOS)
Andrew Wilsonc6c2c37e2017-09-01 14:44:27159 session_type_.reset();
xdaid05f51c2015-03-23 17:21:33160 chromeos::WallpaperManager::Shutdown();
161#endif
initial.commit09911bf2008-07-26 23:55:29162 }
[email protected]f0a51fb52009-03-05 12:46:38163
[email protected]1c62b2f2013-06-28 00:15:00164 // Helper function to create a profile with |name| for a profile |manager|.
165 void CreateProfileAsync(ProfileManager* manager,
166 const std::string& name,
[email protected]d20d0432014-06-12 17:14:05167 bool is_supervised,
[email protected]1c62b2f2013-06-28 00:15:00168 MockObserver* mock_observer) {
vabr8023d872016-09-15 08:12:22169 manager->CreateProfileAsync(temp_dir_.GetPath().AppendASCII(name),
170 base::Bind(&MockObserver::OnProfileCreated,
171 base::Unretained(mock_observer)),
172 base::UTF8ToUTF16(name),
173 profiles::GetDefaultAvatarIconUrl(0),
174 is_supervised ? "Dummy ID" : std::string());
[email protected]1c62b2f2013-06-28 00:15:00175 }
176
lwchkg498e92492016-04-23 11:04:12177 // Helper function to add a profile with |profile_name| to |profile_manager|'s
178 // ProfileAttributesStorage, and return the profile created.
179 Profile* AddProfileToStorage(ProfileManager* profile_manager,
180 const std::string& path_suffix,
181 const base::string16& profile_name) {
182 ProfileAttributesStorage& storage =
183 profile_manager->GetProfileAttributesStorage();
184 size_t num_profiles = storage.GetNumberOfProfiles();
vabr8023d872016-09-15 08:12:22185 base::FilePath path = temp_dir_.GetPath().AppendASCII(path_suffix);
lwchkg498e92492016-04-23 11:04:12186 storage.AddProfile(path, profile_name, std::string(), base::string16(), 0,
187 std::string());
188 EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:24189 return profile_manager->GetProfile(path);
190 }
191
palar47cf44bd2017-03-03 15:36:11192 // Helper function to set profile ephemeral at prefs and attributes storage.
193 void SetProfileEphemeral(Profile* profile) {
194 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
195
196 // Update IsEphemeral in attributes storage, normally it happened via
197 // kForceEphemeralProfiles pref change event routed to
198 // ProfileImpl::UpdateIsEphemeralInStorage().
199 ProfileAttributesEntry* entry;
200 ProfileAttributesStorage& storage =
201 g_browser_process->profile_manager()->GetProfileAttributesStorage();
202 EXPECT_TRUE(
203 storage.GetProfileAttributesWithPath(profile->GetPath(), &entry));
204 entry->SetIsEphemeral(true);
205 }
206
Gabriel Charettecc8362b2017-09-20 21:59:40207 content::TestBrowserThreadBundle thread_bundle_;
208
[email protected]6f96cbcb2011-11-04 02:26:07209#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52210 // Helper function to register an user with id |user_id| and create profile
211 // with a correct path.
212 void RegisterUser(const std::string& user_id) {
213 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
214 const std::string user_id_hash =
215 profile_helper->GetUserIdHashByUserIdForTesting(user_id);
216 user_manager::UserManager::Get()->UserLoggedIn(
alemate3ffbde6f2015-11-03 02:02:55217 AccountId::FromUserEmail(user_id), user_id_hash, false);
dzhioev0298d722014-08-26 13:18:52218 g_browser_process->profile_manager()->GetProfile(
219 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
220 }
221
[email protected]e4854dc2013-04-24 00:11:51222 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
[email protected]363f5272013-04-23 17:21:42223 chromeos::ScopedTestCrosSettings test_cros_settings_;
[email protected]6f96cbcb2011-11-04 02:26:07224#endif
225
[email protected]3a305db2011-04-12 13:40:53226 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23227 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35228 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35229
[email protected]363f5272013-04-23 17:21:42230
231#if defined(OS_CHROMEOS)
232 chromeos::ScopedTestUserManager test_user_manager_;
Andrew Wilsonc6c2c37e2017-09-01 14:44:27233 std::unique_ptr<base::AutoReset<extensions::FeatureSessionType>>
234 session_type_;
[email protected]363f5272013-04-23 17:21:42235#endif
dzhioev0298d722014-08-26 13:18:52236
237 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
initial.commit09911bf2008-07-26 23:55:29238};
239
[email protected]844a1002011-04-19 11:37:21240TEST_F(ProfileManagerTest, GetProfile) {
vabr8023d872016-09-15 08:12:22241 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]f7011fcb2009-01-28 21:54:32242 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29243
[email protected]881cb0b62011-12-13 20:39:54244 ProfileManager* profile_manager = g_browser_process->profile_manager();
245
initial.commit09911bf2008-07-26 23:55:29246 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36247 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21248 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29249
[email protected]844a1002011-04-19 11:37:21250 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54251 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29252}
[email protected]55474b572009-04-14 22:05:33253
[email protected]943aed5542010-06-11 15:57:08254TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45255 base::FilePath expected_default =
256 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54257 EXPECT_EQ(
258 expected_default.value(),
259 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08260}
261
skuhne24bb24432014-09-08 19:31:47262MATCHER(NotFail, "Profile creation failure status is not reported.") {
263 return arg == Profile::CREATE_STATUS_CREATED ||
264 arg == Profile::CREATE_STATUS_INITIALIZED;
265}
266
267MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
268 if (!g_created_profile)
269 g_created_profile = arg;
270 return arg != NULL && arg == g_created_profile;
271}
272
[email protected]943aed5542010-06-11 15:57:08273#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52274
[email protected]943aed5542010-06-11 15:57:08275// This functionality only exists on Chrome OS.
276TEST_F(ProfileManagerTest, LoggedInProfileDir) {
[email protected]650b2d52013-02-10 03:41:45277 base::FilePath expected_default =
278 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54279 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08280 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54281 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08282
dzhioev0298d722014-08-26 13:18:52283 const char kTestUserName[] = "[email protected]";
alemate3ffbde6f2015-11-03 02:02:55284 const AccountId test_account_id(AccountId::FromUserEmail(kTestUserName));
merkulova793f3022015-02-04 10:18:30285 chromeos::FakeChromeUserManager* user_manager =
286 new chromeos::FakeChromeUserManager();
dzhioev0298d722014-08-26 13:18:52287 chromeos::ScopedUserManagerEnabler enabler(user_manager);
288
alemate3ffbde6f2015-11-03 02:02:55289 const user_manager::User* active_user =
290 user_manager->AddUser(test_account_id);
291 user_manager->LoginUser(test_account_id);
292 user_manager->SwitchActiveUser(test_account_id);
dzhioev0298d722014-08-26 13:18:52293
[email protected]2fda9972014-07-23 14:51:59294 profile_manager->Observe(
295 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
296 content::NotificationService::AllSources(),
297 content::Details<const user_manager::User>(active_user));
dzhioev0298d722014-08-26 13:18:52298 base::FilePath expected_logged_in(
299 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
[email protected]943aed5542010-06-11 15:57:08300 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54301 profile_manager->GetInitialProfileDir().value());
vabr8023d872016-09-15 08:12:22302 VLOG(1) << temp_dir_.GetPath()
303 .Append(profile_manager->GetInitialProfileDir())
304 .value();
[email protected]943aed5542010-06-11 15:57:08305}
306
xiyuan41d32a32017-06-12 20:06:13307// Test Get[ActiveUser|PrimaryUser|LastUsed]Profile does not load user profile.
308TEST_F(ProfileManagerTest, UserProfileLoading) {
309 using chromeos::ProfileHelper;
310
311 Profile* const signin_profile = ProfileHelper::GetSigninProfile();
312
313 // Get[Active|Primary|LastUsed]Profile return the sign-in profile before login
314 // happens. IsSameProfile() is used to properly test against TestProfile whose
315 // OTR version uses a different temp path.
316 EXPECT_TRUE(
317 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
318 EXPECT_TRUE(
319 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
320 EXPECT_TRUE(
321 ProfileManager::GetLastUsedProfile()->IsSameProfile(signin_profile));
322
323 // User signs in but user profile loading has not started.
324 const std::string user_id = "[email protected]";
325 const std::string user_id_hash =
326 ProfileHelper::Get()->GetUserIdHashByUserIdForTesting(user_id);
327 user_manager::UserManager::Get()->UserLoggedIn(
328 AccountId::FromUserEmail(user_id), user_id_hash, false);
329
330 // Sign-in profile should be returned at this stage. Otherwise, login code
331 // ends up in an invalid state. Strange things as in http://crbug.com/728683
332 // and http://crbug.com/718734 happens.
333 EXPECT_TRUE(
334 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
335 EXPECT_TRUE(
336 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
xiyuan8c6a4112017-06-19 21:03:49337
338 // GetLastUsedProfile() after login but before a user profile is loaded is
339 // fatal.
340 EXPECT_DEATH_IF_SUPPORTED(ProfileManager::GetLastUsedProfile(), ".*");
xiyuan41d32a32017-06-12 20:06:13341
342 // Simulate UserSessionManager loads the profile.
343 Profile* const user_profile =
344 g_browser_process->profile_manager()->GetProfile(
345 ProfileHelper::Get()->GetProfilePathByUserIdHash(user_id_hash));
346 ASSERT_FALSE(user_profile->IsSameProfile(signin_profile));
347
348 // User profile is returned thereafter.
349 EXPECT_TRUE(
350 ProfileManager::GetActiveUserProfile()->IsSameProfile(user_profile));
351 EXPECT_TRUE(
352 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(user_profile));
353 EXPECT_TRUE(
354 ProfileManager::GetLastUsedProfile()->IsSameProfile(user_profile));
355}
356
[email protected]943aed5542010-06-11 15:57:08357#endif
358
[email protected]52d69b882010-06-17 17:35:51359TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
vabr8023d872016-09-15 08:12:22360 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33361 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47362
vabr8023d872016-09-15 08:12:22363 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33364 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
365
[email protected]881cb0b62011-12-13 20:39:54366 ProfileManager* profile_manager = g_browser_process->profile_manager();
367
[email protected]55474b572009-04-14 22:05:33368 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17369 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54370 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21371 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33372
[email protected]537c1082011-12-02 02:37:17373 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54374 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21375 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33376
377 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31378 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04379 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
380 profile1, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17381 profile1->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30382 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile1));
[email protected]537c1082011-12-02 02:37:17383 profile2->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30384 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile2));
[email protected]608e7e02013-07-24 12:23:31385 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04386 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
387 profile2, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55388
389 // Make sure any pending tasks run before we destroy the profiles.
Gabriel Charette01507a22017-09-27 21:30:08390 content::RunAllTasksUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55391
[email protected]c494d082013-01-04 20:41:22392 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]6cad5bf2011-03-10 21:21:55393
[email protected]237e6d02010-11-08 21:45:42394 // Make sure history cleans up correctly.
Gabriel Charette01507a22017-09-27 21:30:08395 content::RunAllTasksUntilIdle();
[email protected]55474b572009-04-14 22:05:33396}
[email protected]844a1002011-04-19 11:37:21397
miguelg1b495542016-03-18 17:57:46398TEST_F(ProfileManagerTest, LoadNonExistingProfile) {
399 const std::string profile_name = "NonExistingProfile";
400 base::RunLoop run_loop_1;
401 base::RunLoop run_loop_2;
402
403 ProfileManager* profile_manager = g_browser_process->profile_manager();
404 profile_manager->LoadProfile(
405 profile_name, false /* incognito */,
406 base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure()));
407 run_loop_1.Run();
408
409 profile_manager->LoadProfile(
410 profile_name, true /* incognito */,
411 base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure()));
412 run_loop_2.Run();
413}
414
415TEST_F(ProfileManagerTest, LoadExistingProfile) {
416 const std::string profile_name = "MyProfile";
417 const std::string other_name = "SomeOtherProfile";
418 MockObserver mock_observer1;
419 EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail()))
420 .Times(testing::AtLeast(1));
421
422 ProfileManager* profile_manager = g_browser_process->profile_manager();
423 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
424
425 // Make sure a real profile is created before continuing.
Gabriel Charette01507a22017-09-27 21:30:08426 content::RunAllTasksUntilIdle();
miguelg1b495542016-03-18 17:57:46427
428 base::RunLoop load_profile;
429 bool incognito = false;
430 profile_manager->LoadProfile(
431 profile_name, incognito,
432 base::Bind(&ExpectProfileWithName, profile_name, incognito,
433 load_profile.QuitClosure()));
434 load_profile.Run();
435
436 base::RunLoop load_profile_incognito;
437 incognito = true;
438 profile_manager->LoadProfile(
439 profile_name, incognito,
440 base::Bind(&ExpectProfileWithName, profile_name, incognito,
441 load_profile_incognito.QuitClosure()));
442 load_profile_incognito.Run();
443
444 // Loading some other non existing profile should still return null.
445 base::RunLoop load_other_profile;
446 profile_manager->LoadProfile(
447 other_name, false,
448 base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure()));
449 load_other_profile.Run();
450}
451
[email protected]844a1002011-04-19 11:37:21452TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
[email protected]844a1002011-04-19 11:37:21453 g_created_profile = NULL;
454
455 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11456 EXPECT_CALL(mock_observer1, OnProfileCreated(
457 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21458 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11459 EXPECT_CALL(mock_observer2, OnProfileCreated(
460 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21461 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11462 EXPECT_CALL(mock_observer3, OnProfileCreated(
463 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21464
[email protected]881cb0b62011-12-13 20:39:54465 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00466 const std::string profile_name = "New Profile";
[email protected]5ddfade2014-02-03 10:24:53467 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
468 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
469 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21470
Gabriel Charette01507a22017-09-27 21:30:08471 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21472}
473
474TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00475 const std::string profile_name1 = "New Profile 1";
476 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21477
478 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11479 EXPECT_CALL(mock_observer, OnProfileCreated(
480 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21481
[email protected]881cb0b62011-12-13 20:39:54482 ProfileManager* profile_manager = g_browser_process->profile_manager();
483
[email protected]5ddfade2014-02-03 10:24:53484 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
485 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]844a1002011-04-19 11:37:21486
Gabriel Charette01507a22017-09-27 21:30:08487 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21488}
[email protected]279170832011-10-12 23:38:03489
[email protected]5ddfade2014-02-03 10:24:53490TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
[email protected]d20d0432014-06-12 17:14:05491 std::string name = "0 Supervised Profile";
[email protected]5ddfade2014-02-03 10:24:53492
493 MockObserver mock_observer;
494 EXPECT_CALL(mock_observer, OnProfileCreated(
495 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
496
497 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12498 ProfileAttributesStorage& storage =
499 profile_manager->GetProfileAttributesStorage();
500 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53501
502 CreateProfileAsync(profile_manager, name, true, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:08503 content::RunAllTasksUntilIdle();
[email protected]5ddfade2014-02-03 10:24:53504
lwchkg498e92492016-04-23 11:04:12505 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05506 // Supervised profiles should start out omitted from the profile list.
lwchkg498e92492016-04-23 11:04:12507 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53508
[email protected]d20d0432014-06-12 17:14:05509 name = "1 Regular Profile";
[email protected]5ddfade2014-02-03 10:24:53510 CreateProfileAsync(profile_manager, name, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:08511 content::RunAllTasksUntilIdle();
[email protected]5ddfade2014-02-03 10:24:53512
lwchkg498e92492016-04-23 11:04:12513 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05514 // Non-supervised profiles should be included in the profile list.
lwchkg498e92492016-04-23 11:04:12515 EXPECT_FALSE(storage.GetAllProfilesAttributesSortedByName()[1u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53516}
517
lwchkg498e92492016-04-23 11:04:12518TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) {
[email protected]5ddfade2014-02-03 10:24:53519 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12520 ProfileAttributesStorage& storage =
521 profile_manager->GetProfileAttributesStorage();
522 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53523
[email protected]d20d0432014-06-12 17:14:05524 const base::FilePath supervised_path =
vabr8023d872016-09-15 08:12:22525 temp_dir_.GetPath().AppendASCII("Supervised");
[email protected]d20d0432014-06-12 17:14:05526 TestingProfile* supervised_profile =
527 new TestingProfile(supervised_path, NULL);
528 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
[email protected]5ddfade2014-02-03 10:24:53529
530 // RegisterTestingProfile adds the profile to the cache and takes ownership.
[email protected]d20d0432014-06-12 17:14:05531 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
lwchkg498e92492016-04-23 11:04:12532 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
533 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53534
vabr8023d872016-09-15 08:12:22535 const base::FilePath nonsupervised_path =
536 temp_dir_.GetPath().AppendASCII("Non-Supervised");
[email protected]d20d0432014-06-12 17:14:05537 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
538 NULL);
539 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
[email protected]5ddfade2014-02-03 10:24:53540
lwchkg498e92492016-04-23 11:04:12541 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
542 ProfileAttributesEntry* entry;
543 ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry));
544 EXPECT_TRUE(entry->IsOmitted());
545
546 ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry));
547 EXPECT_FALSE(entry->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53548}
549
[email protected]4251165a2013-07-17 04:33:40550TEST_F(ProfileManagerTest, GetGuestProfilePath) {
551 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
vabr8023d872016-09-15 08:12:22552 base::FilePath expected_path = temp_dir_.GetPath();
[email protected]4251165a2013-07-17 04:33:40553 expected_path = expected_path.Append(chrome::kGuestProfileDir);
554 EXPECT_EQ(expected_path, guest_path);
555}
556
mlermanb8df6e82015-01-23 19:55:34557TEST_F(ProfileManagerTest, GetSystemProfilePath) {
558 base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath();
vabr8023d872016-09-15 08:12:22559 base::FilePath expected_path = temp_dir_.GetPath();
mlermanb8df6e82015-01-23 19:55:34560 expected_path = expected_path.Append(chrome::kSystemProfileDir);
561 EXPECT_EQ(expected_path, system_profile_path);
562}
563
[email protected]0ff6198e2014-01-08 22:45:00564class UnittestGuestProfileManager : public UnittestProfileManager {
565 public:
566 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
567 : UnittestProfileManager(user_data_dir) {}
568
569 protected:
dcheng1fd716382014-10-22 20:14:41570 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]0ff6198e2014-01-08 22:45:00571 TestingProfile::Builder builder;
[email protected]0ff6198e2014-01-08 22:45:00572 builder.SetGuestSession();
[email protected]d22f7c932014-02-20 22:48:58573 builder.SetPath(file_path);
574 TestingProfile* testing_profile = builder.Build().release();
[email protected]0ff6198e2014-01-08 22:45:00575 return testing_profile;
576 }
577};
578
[email protected]9b6e10c2014-05-20 19:12:28579class ProfileManagerGuestTest : public ProfileManagerTest {
[email protected]0ff6198e2014-01-08 22:45:00580 protected:
dchenge1bc7982014-10-30 00:32:40581 void SetUp() override {
[email protected]0ff6198e2014-01-08 22:45:00582 // Create a new temporary directory, and store the path
583 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
584 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22585 new UnittestGuestProfileManager(temp_dir_.GetPath()));
[email protected]0ff6198e2014-01-08 22:45:00586
[email protected]2a92f2182014-03-25 00:47:59587#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43588 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
[email protected]9b6e10c2014-05-20 19:12:28589 // This switch is needed to skip non-test specific behavior in
590 // ProfileManager (accessing DBusThreadManager).
[email protected]0ff6198e2014-01-08 22:45:00591 cl->AppendSwitch(switches::kTestType);
[email protected]9b6e10c2014-05-20 19:12:28592
[email protected]0ff6198e2014-01-08 22:45:00593 cl->AppendSwitch(chromeos::switches::kGuestSession);
594 cl->AppendSwitch(::switches::kIncognito);
595
xdaid05f51c2015-03-23 17:21:33596 chromeos::WallpaperManager::Initialize();
Andrew Wilsonc6c2c37e2017-09-01 14:44:27597 // Have to manually reset the session type in between test runs because
598 // RegisterUser() changes it.
599 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
600 extensions::GetCurrentFeatureSessionType());
601 session_type_ = extensions::ScopedCurrentFeatureSessionType(
602 extensions::GetCurrentFeatureSessionType());
603
alemate3c1c0882016-10-27 19:05:12604 RegisterUser(user_manager::kGuestUserName);
[email protected]2a92f2182014-03-25 00:47:59605#endif
[email protected]0ff6198e2014-01-08 22:45:00606 }
607};
608
[email protected]9b6e10c2014-05-20 19:12:28609TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
[email protected]2a92f2182014-03-25 00:47:59610 ProfileManager* profile_manager = g_browser_process->profile_manager();
611 ASSERT_TRUE(profile_manager);
612
613 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
614 ASSERT_TRUE(profile);
615 EXPECT_TRUE(profile->IsOffTheRecord());
616}
617
618#if defined(OS_CHROMEOS)
[email protected]9b6e10c2014-05-20 19:12:28619TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
[email protected]0ff6198e2014-01-08 22:45:00620 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
621 EXPECT_TRUE(primary_profile->IsOffTheRecord());
622
623 Profile* active_profile = ProfileManager::GetActiveUserProfile();
624 EXPECT_TRUE(active_profile->IsOffTheRecord());
625
626 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
[email protected]d22f7c932014-02-20 22:48:58627
628 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
629 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
630
631 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
[email protected]0ff6198e2014-01-08 22:45:00632}
633#endif
634
[email protected]279170832011-10-12 23:38:03635TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54636 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12637 ProfileAttributesStorage& storage =
638 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27639 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
vabr8684c9a2017-03-29 13:14:57640 base::MakeUnique<base::Value>(true));
[email protected]714bf1d2012-11-30 01:35:27641
642 // Setting a pref which is not applicable to a system (i.e., Android in this
643 // case) does not necessarily create it. Don't bother continuing with the
644 // test if this pref doesn't exist because it will not load the profiles if
645 // it cannot verify that the pref for background mode is enabled.
646 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
647 return;
[email protected]279170832011-10-12 23:38:03648
lwchkg498e92492016-04-23 11:04:12649 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
650
651 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
652 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string());
653 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
654 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string());
655 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"),
656 ASCIIToUTF16("name_3"), "34567", base::string16(), 0, std::string());
657
658 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
659
660 std::vector<ProfileAttributesEntry*> entries =
661 storage.GetAllProfilesAttributes();
662 entries[0u]->SetBackgroundStatus(true);
663 entries[2u]->SetBackgroundStatus(true);
[email protected]279170832011-10-12 23:38:03664
[email protected]881cb0b62011-12-13 20:39:54665 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03666
[email protected]881cb0b62011-12-13 20:39:54667 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
668}
669
[email protected]714bf1d2012-11-30 01:35:27670TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
671 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12672 ProfileAttributesStorage& storage =
673 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27674 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
vabr8684c9a2017-03-29 13:14:57675 base::MakeUnique<base::Value>(false));
[email protected]714bf1d2012-11-30 01:35:27676
lwchkg498e92492016-04-23 11:04:12677 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
678
679 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
680 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string());
681 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
682 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string());
683
684 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
685
686 std::vector<ProfileAttributesEntry*> entries =
687 storage.GetAllProfilesAttributes();
688 entries[0u]->SetBackgroundStatus(false);
689 entries[1u]->SetBackgroundStatus(true);
[email protected]714bf1d2012-11-30 01:35:27690
691 profile_manager->AutoloadProfiles();
692
693 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
694}
695
[email protected]881cb0b62011-12-13 20:39:54696TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
vabr8023d872016-09-15 08:12:22697 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54698 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
699
700 ProfileManager* profile_manager = g_browser_process->profile_manager();
701
702 Profile* profile;
703
704 // Successfully create the profile
705 profile = profile_manager->GetProfile(dest_path);
706 ASSERT_TRUE(profile);
707
708 // Check that the profile name is non empty
709 std::string profile_name =
710 profile->GetPrefs()->GetString(prefs::kProfileName);
711 EXPECT_FALSE(profile_name.empty());
712
713 // Check that the profile avatar index is valid
714 size_t avatar_index =
715 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
[email protected]c3e559772014-04-09 04:02:54716 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
[email protected]881cb0b62011-12-13 20:39:54717 avatar_index));
718}
719
720// Tests that a new profile's entry in the profile info cache is setup with the
721// same values that are in the profile prefs.
722TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
vabr8023d872016-09-15 08:12:22723 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54724 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
725
726 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]881cb0b62011-12-13 20:39:54727
728 // Successfully create the profile
729 Profile* profile = profile_manager->GetProfile(dest_path);
730 ASSERT_TRUE(profile);
731
732 std::string profile_name =
733 profile->GetPrefs()->GetString(prefs::kProfileName);
734 size_t avatar_index =
735 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
736
lwchkg498e92492016-04-23 11:04:12737 ProfileAttributesEntry* entry;
738 ASSERT_TRUE(profile_manager->GetProfileAttributesStorage().
739 GetProfileAttributesWithPath(dest_path, &entry));
[email protected]881cb0b62011-12-13 20:39:54740
741 // Check if the profile prefs are the same as the cache prefs
lwchkg498e92492016-04-23 11:04:12742 EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName()));
743 EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex());
[email protected]279170832011-10-12 23:38:03744}
[email protected]52edf812012-01-12 11:48:46745
[email protected]13720532013-05-30 12:30:59746TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
747 ProfileManager* profile_manager = g_browser_process->profile_manager();
748 ASSERT_TRUE(profile_manager);
749
dzhioev0298d722014-08-26 13:18:52750#if defined(OS_CHROMEOS)
xiyuan41d32a32017-06-12 20:06:13751 // On CrOS, profile returned by GetLastUsedProfile is a sign-in profile that
dzhioev0298d722014-08-26 13:18:52752 // is forced to be incognito. That's why we need to create at least one user
753 // to get a regular profile.
754 RegisterUser("[email protected]");
755#endif
756
[email protected]13720532013-05-30 12:30:59757 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
758 ASSERT_TRUE(profile);
759 EXPECT_FALSE(profile->IsOffTheRecord());
760 PrefService* prefs = profile->GetPrefs();
761 EXPECT_EQ(IncognitoModePrefs::ENABLED,
762 IncognitoModePrefs::GetAvailability(prefs));
763
[email protected]13720532013-05-30 12:30:59764 ASSERT_TRUE(profile->GetOffTheRecordProfile());
765
766 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
767 EXPECT_FALSE(
768 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
769
770 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
771 // incognito mode is forced.
772 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
773 EXPECT_TRUE(
774 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
775}
776
[email protected]c849fab2012-03-29 16:51:41777#if !defined(OS_ANDROID)
778// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46779TEST_F(ProfileManagerTest, LastOpenedProfiles) {
vabr8023d872016-09-15 08:12:22780 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46781 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
782
vabr8023d872016-09-15 08:12:22783 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46784 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
785
786 ProfileManager* profile_manager = g_browser_process->profile_manager();
787
788 // Successfully create the profiles.
789 TestingProfile* profile1 =
790 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
791 ASSERT_TRUE(profile1);
792
793 TestingProfile* profile2 =
794 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
795 ASSERT_TRUE(profile2);
796
797 std::vector<Profile*> last_opened_profiles =
798 profile_manager->GetLastOpenedProfiles();
799 ASSERT_EQ(0U, last_opened_profiles.size());
800
801 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:35802 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:03803 std::unique_ptr<Browser> browser1a(
[email protected]c5f3e1842013-02-20 17:10:41804 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46805
806 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
807 ASSERT_EQ(1U, last_opened_profiles.size());
808 EXPECT_EQ(profile1, last_opened_profiles[0]);
809
810 // And for profile2.
erg7b01d692017-02-22 21:57:35811 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:03812 std::unique_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41813 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46814
815 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
816 ASSERT_EQ(2U, last_opened_profiles.size());
817 EXPECT_EQ(profile1, last_opened_profiles[0]);
818 EXPECT_EQ(profile2, last_opened_profiles[1]);
819
820 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03821 std::unique_ptr<Browser> browser1b(
[email protected]c5f3e1842013-02-20 17:10:41822 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46823 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
824 ASSERT_EQ(2U, last_opened_profiles.size());
825 EXPECT_EQ(profile1, last_opened_profiles[0]);
826 EXPECT_EQ(profile2, last_opened_profiles[1]);
827
828 // Close the browsers.
829 browser1a.reset();
830 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
831 ASSERT_EQ(2U, last_opened_profiles.size());
832 EXPECT_EQ(profile1, last_opened_profiles[0]);
833 EXPECT_EQ(profile2, last_opened_profiles[1]);
834
835 browser1b.reset();
836 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
837 ASSERT_EQ(1U, last_opened_profiles.size());
838 EXPECT_EQ(profile2, last_opened_profiles[0]);
839
840 browser2.reset();
841 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
842 ASSERT_EQ(0U, last_opened_profiles.size());
843}
844
845TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
vabr8023d872016-09-15 08:12:22846 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46847 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
848
vabr8023d872016-09-15 08:12:22849 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46850 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
851
852 ProfileManager* profile_manager = g_browser_process->profile_manager();
853
854 // Successfully create the profiles.
855 TestingProfile* profile1 =
856 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
857 ASSERT_TRUE(profile1);
858
859 TestingProfile* profile2 =
860 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
861 ASSERT_TRUE(profile2);
862
863 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:35864 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:03865 std::unique_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41866 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46867
868 // And for profile2.
erg7b01d692017-02-22 21:57:35869 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:03870 std::unique_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41871 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46872
873 std::vector<Profile*> last_opened_profiles =
874 profile_manager->GetLastOpenedProfiles();
875 ASSERT_EQ(2U, last_opened_profiles.size());
876 EXPECT_EQ(profile1, last_opened_profiles[0]);
877 EXPECT_EQ(profile2, last_opened_profiles[1]);
878
879 // Simulate a shutdown.
880 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30881 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:46882 content::NotificationService::AllSources(),
883 content::NotificationService::NoDetails());
884
885 // Even if the browsers are destructed during shutdown, the profiles stay
886 // open.
887 browser1.reset();
888 browser2.reset();
889
890 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
891 ASSERT_EQ(2U, last_opened_profiles.size());
892 EXPECT_EQ(profile1, last_opened_profiles[0]);
893 EXPECT_EQ(profile2, last_opened_profiles[1]);
894}
[email protected]41276342012-02-06 10:49:31895
896TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
vabr8023d872016-09-15 08:12:22897 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:31898 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
vabr8023d872016-09-15 08:12:22899 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:31900 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
901
902 ProfileManager* profile_manager = g_browser_process->profile_manager();
903
904 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:06905 TestingProfile* profile1 =
906 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:31907 ASSERT_TRUE(profile1);
908
[email protected]41276342012-02-06 10:49:31909 std::vector<Profile*> last_opened_profiles =
910 profile_manager->GetLastOpenedProfiles();
911 ASSERT_EQ(0U, last_opened_profiles.size());
912
913 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:35914 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:03915 std::unique_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41916 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:31917
918 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
919 ASSERT_EQ(1U, last_opened_profiles.size());
920 EXPECT_EQ(profile1, last_opened_profiles[0]);
921
922 // And for profile2.
erg7b01d692017-02-22 21:57:35923 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
924 true);
dcheng6e25ed32016-04-08 03:30:03925 std::unique_ptr<Browser> browser2a(
[email protected]c5f3e1842013-02-20 17:10:41926 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31927
928 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
929 ASSERT_EQ(1U, last_opened_profiles.size());
930 EXPECT_EQ(profile1, last_opened_profiles[0]);
931
932 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03933 std::unique_ptr<Browser> browser2b(
[email protected]c5f3e1842013-02-20 17:10:41934 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31935 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
936 ASSERT_EQ(1U, last_opened_profiles.size());
937 EXPECT_EQ(profile1, last_opened_profiles[0]);
938
939 // Close the browsers.
940 browser2a.reset();
941 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
942 ASSERT_EQ(1U, last_opened_profiles.size());
943 EXPECT_EQ(profile1, last_opened_profiles[0]);
944
945 browser2b.reset();
946 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
947 ASSERT_EQ(1U, last_opened_profiles.size());
948 EXPECT_EQ(profile1, last_opened_profiles[0]);
949
950 browser1.reset();
951 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
952 ASSERT_EQ(0U, last_opened_profiles.size());
953}
[email protected]c849fab2012-03-29 16:51:41954#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:00955
956#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
957// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:48958TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
vabr8023d872016-09-15 08:12:22959 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:48960 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
961
962 ProfileManager* profile_manager = g_browser_process->profile_manager();
963
964 TestingProfile* profile =
965 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
966 ASSERT_TRUE(profile);
palar47cf44bd2017-03-03 15:36:11967 SetProfileEphemeral(profile);
[email protected]8d9243a2013-10-21 17:11:48968
969 // Here the last used profile is still the "Default" profile.
970 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
971 EXPECT_NE(profile, last_used_profile);
972
[email protected]79fd6b72013-10-22 16:39:27973 // Create a browser for the profile.
erg7b01d692017-02-22 21:57:35974 Browser::CreateParams profile_params(profile, true);
dcheng6e25ed32016-04-08 03:30:03975 std::unique_ptr<Browser> browser(
[email protected]8d9243a2013-10-21 17:11:48976 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
977 last_used_profile = profile_manager->GetLastUsedProfile();
978 EXPECT_NE(profile, last_used_profile);
979
980 // Close the browser.
981 browser.reset();
982 last_used_profile = profile_manager->GetLastUsedProfile();
983 EXPECT_NE(profile, last_used_profile);
984}
985
986TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
vabr8023d872016-09-15 08:12:22987 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:48988 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
989
vabr8023d872016-09-15 08:12:22990 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:27991 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
992
vabr8023d872016-09-15 08:12:22993 base::FilePath dest_path3 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:27994 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:48995
996 ProfileManager* profile_manager = g_browser_process->profile_manager();
997
998 // Successfully create the profiles.
999 TestingProfile* normal_profile =
1000 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
1001 ASSERT_TRUE(normal_profile);
1002
1003 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:271004 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:481005 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:271006 ASSERT_TRUE(ephemeral_profile1);
palar47cf44bd2017-03-03 15:36:111007 SetProfileEphemeral(ephemeral_profile1);
[email protected]79fd6b72013-10-22 16:39:271008
1009 // Add second ephemeral profile but don't mark it as such yet.
1010 TestingProfile* ephemeral_profile2 =
1011 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
1012 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481013
1014 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351015 Browser::CreateParams profile1_params(normal_profile, true);
dcheng6e25ed32016-04-08 03:30:031016 std::unique_ptr<Browser> browser1(
[email protected]8d9243a2013-10-21 17:11:481017 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
1018
[email protected]79fd6b72013-10-22 16:39:271019 // Create browsers for the ephemeral profile.
erg7b01d692017-02-22 21:57:351020 Browser::CreateParams profile2_params(ephemeral_profile1, true);
dcheng6e25ed32016-04-08 03:30:031021 std::unique_ptr<Browser> browser2(
[email protected]8d9243a2013-10-21 17:11:481022 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
1023
erg7b01d692017-02-22 21:57:351024 Browser::CreateParams profile3_params(ephemeral_profile2, true);
dcheng6e25ed32016-04-08 03:30:031025 std::unique_ptr<Browser> browser3(
[email protected]79fd6b72013-10-22 16:39:271026 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
1027
[email protected]8d9243a2013-10-21 17:11:481028 std::vector<Profile*> last_opened_profiles =
1029 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:271030 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:481031 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:271032 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
1033
1034 // Mark the second profile ephemeral.
palar47cf44bd2017-03-03 15:36:111035 SetProfileEphemeral(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481036
1037 // Simulate a shutdown.
1038 content::NotificationService::current()->Notify(
1039 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
1040 content::NotificationService::AllSources(),
1041 content::NotificationService::NoDetails());
1042 browser1.reset();
1043 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:271044 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:481045
1046 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1047 ASSERT_EQ(1U, last_opened_profiles.size());
1048 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
1049}
1050
bauerb8b022182015-05-13 09:18:391051TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
1052 // Create two profiles, one of them ephemeral.
1053 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121054 ProfileAttributesStorage& storage =
1055 profile_manager->GetProfileAttributesStorage();
1056 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391057
1058 const std::string profile_name1 = "Homer";
lwchkg498e92492016-04-23 11:04:121059 base::FilePath path1 =
1060 profile_manager->user_data_dir().AppendASCII(profile_name1);
1061 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
1062 base::UTF8ToUTF16(profile_name1), 0, std::string());
1063 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391064 ASSERT_TRUE(base::CreateDirectory(path1));
1065
1066 const std::string profile_name2 = "Marge";
lwchkg498e92492016-04-23 11:04:121067 base::FilePath path2 =
1068 profile_manager->user_data_dir().AppendASCII(profile_name2);
1069 storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(),
1070 base::UTF8ToUTF16(profile_name2), 0, std::string());
1071 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391072 ASSERT_TRUE(base::CreateDirectory(path2));
1073
1074 // Set the active profile.
1075 PrefService* local_state = g_browser_process->local_state();
1076 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1077
1078 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081079 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391080
1081 // The ephemeral profile should be deleted, and the last used profile set to
1082 // the other one.
1083 EXPECT_FALSE(base::DirectoryExists(path1));
1084 EXPECT_TRUE(base::DirectoryExists(path2));
1085 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121086 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391087
1088 // Mark the remaining profile ephemeral and clean up.
lwchkg498e92492016-04-23 11:04:121089 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391090 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081091 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391092
1093 // The profile should be deleted, and the last used profile set to a new one.
1094 EXPECT_FALSE(base::DirectoryExists(path2));
lwchkg498e92492016-04-23 11:04:121095 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391096 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1097}
1098
[email protected]1c62b2f2013-06-28 00:15:001099TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
1100 ProfileManager* profile_manager = g_browser_process->profile_manager();
1101 ASSERT_TRUE(profile_manager);
1102
1103 // Create and load two profiles.
1104 const std::string profile_name1 = "New Profile 1";
1105 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221106 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1107 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001108
1109 MockObserver mock_observer;
1110 EXPECT_CALL(mock_observer, OnProfileCreated(
1111 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
1112
[email protected]5ddfade2014-02-03 10:24:531113 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1114 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081115 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001116
1117 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121118 EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage().
1119 GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001120
1121 // Set the active profile.
1122 PrefService* local_state = g_browser_process->local_state();
1123 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1124
1125 // Delete the active profile.
1126 profile_manager->ScheduleProfileForDeletion(dest_path1,
1127 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081128 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001129
1130 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1131 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1132}
[email protected]3f130532014-02-26 20:39:241133
[email protected]602b5a29f2014-07-18 15:39:331134TEST_F(ProfileManagerTest, LastProfileDeleted) {
1135 ProfileManager* profile_manager = g_browser_process->profile_manager();
1136 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121137 ProfileAttributesStorage& storage =
1138 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331139
1140 // Create and load a profile.
1141 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221142 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331143
1144 MockObserver mock_observer;
1145 EXPECT_CALL(mock_observer, OnProfileCreated(
1146 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
1147
1148 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081149 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331150
1151 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121152 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331153
1154 // Set it as the active profile.
1155 PrefService* local_state = g_browser_process->local_state();
1156 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1157
1158 // Delete the active profile.
1159 profile_manager->ScheduleProfileForDeletion(dest_path1,
1160 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081161 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331162
1163 // A new profile should have been created
1164 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221165 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331166
1167 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1168 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121169 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331170}
1171
1172TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1173 ProfileManager* profile_manager = g_browser_process->profile_manager();
1174 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121175 ProfileAttributesStorage& storage =
1176 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331177
1178 // Create and load a profile.
1179 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221180 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331181
1182 MockObserver mock_observer;
1183 EXPECT_CALL(mock_observer, OnProfileCreated(
1184 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1185
1186 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081187 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331188
1189 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121190 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331191
1192 // Create the profile and register it.
1193 const std::string guest_profile_name =
1194 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1195
1196 TestingProfile::Builder builder;
1197 builder.SetGuestSession();
1198 builder.SetPath(ProfileManager::GetGuestProfilePath());
1199 TestingProfile* guest_profile = builder.Build().release();
1200 guest_profile->set_profile_name(guest_profile_name);
1201 // Registering the profile passes ownership to the ProfileManager.
1202 profile_manager->RegisterTestingProfile(guest_profile, false, false);
1203
lwchkg498e92492016-04-23 11:04:121204 // The Guest profile does not get added to the ProfileAttributesStorage.
[email protected]602b5a29f2014-07-18 15:39:331205 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121206 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331207
1208 // Set the Guest profile as the active profile.
1209 PrefService* local_state = g_browser_process->local_state();
1210 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1211
1212 // Delete the other profile.
1213 profile_manager->ScheduleProfileForDeletion(dest_path1,
1214 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081215 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331216
1217 // A new profile should have been created.
1218 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221219 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331220
1221 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121222 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
1223 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331224}
1225
[email protected]3f130532014-02-26 20:39:241226TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1227 if (!profiles::IsMultipleProfilesEnabled())
1228 return;
1229
[email protected]3f130532014-02-26 20:39:241230 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121231 ProfileAttributesStorage& storage =
1232 profile_manager->GetProfileAttributesStorage();
1233 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241234
1235 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1236 const base::string16 default_profile_name =
1237 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121238 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1239 Profile* profile1 = AddProfileToStorage(profile_manager,
1240 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441241 EXPECT_EQ(default_profile_name,
1242 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241243
1244 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121245 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1246 Profile* profile2 = AddProfileToStorage(profile_manager,
1247 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441248 EXPECT_EQ(profile_name1,
1249 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1250 EXPECT_EQ(profile_name2,
1251 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241252
1253 // Deleting a profile means returning to the default name.
1254 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1255 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081256 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441257 EXPECT_EQ(default_profile_name,
1258 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241259}
1260
1261TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1262 if (!profiles::IsMultipleProfilesEnabled())
1263 return;
1264
[email protected]3f130532014-02-26 20:39:241265 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121266 ProfileAttributesStorage& storage =
1267 profile_manager->GetProfileAttributesStorage();
1268 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241269
1270 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1271 const base::string16 default_profile_name =
1272 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121273 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1274 Profile* profile1 = AddProfileToStorage(profile_manager,
1275 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441276 EXPECT_EQ(default_profile_name,
1277 profiles::GetAvatarNameForProfile(profile1->GetPath()));
lwchkg498e92492016-04-23 11:04:121278 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241279
1280 // We should display custom names for local profiles.
1281 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
lwchkg498e92492016-04-23 11:04:121282 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u];
1283 entry->SetName(custom_profile_name);
1284 entry->SetIsUsingDefaultName(false);
1285 EXPECT_EQ(custom_profile_name, entry->GetName());
[email protected]119809d2014-06-27 22:12:441286 EXPECT_EQ(custom_profile_name,
1287 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241288
1289 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121290 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1291 Profile* profile2 = AddProfileToStorage(profile_manager,
1292 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441293 EXPECT_EQ(custom_profile_name,
1294 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1295 EXPECT_EQ(profile_name2,
1296 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241297
1298 // Deleting a profile means returning to the original, custom name.
1299 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1300 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081301 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441302 EXPECT_EQ(custom_profile_name,
1303 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241304}
1305
1306TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1307 if (!profiles::IsMultipleProfilesEnabled())
1308 return;
1309
[email protected]3f130532014-02-26 20:39:241310 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121311 ProfileAttributesStorage& storage =
1312 profile_manager->GetProfileAttributesStorage();
1313 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241314
1315 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1316 const base::string16 default_profile_name =
1317 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121318 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1319 Profile* profile1 = AddProfileToStorage(profile_manager,
1320 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441321 EXPECT_EQ(default_profile_name,
1322 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241323
lwchkg498e92492016-04-23 11:04:121324 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u];
[email protected]78186a42014-08-22 22:07:301325 // For a signed in profile with a default name we still display
1326 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
lwchkg498e92492016-04-23 11:04:121327 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
1328 EXPECT_EQ(profile_name1, entry->GetName());
[email protected]78186a42014-08-22 22:07:301329 EXPECT_EQ(default_profile_name,
[email protected]119809d2014-06-27 22:12:441330 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241331
[email protected]78186a42014-08-22 22:07:301332 // For a signed in profile with a non-default Gaia given name we display the
1333 // Gaia given name.
lwchkg498e92492016-04-23 11:04:121334 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
[email protected]78186a42014-08-22 22:07:301335 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
lwchkg498e92492016-04-23 11:04:121336 entry->SetGAIAGivenName(gaia_given_name);
1337 EXPECT_EQ(gaia_given_name, entry->GetName());
[email protected]78186a42014-08-22 22:07:301338 EXPECT_EQ(gaia_given_name,
1339 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1340
[email protected]3f130532014-02-26 20:39:241341 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121342 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1343 Profile* profile2 = AddProfileToStorage(profile_manager,
1344 "path_2", profile_name2);
[email protected]78186a42014-08-22 22:07:301345 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441346 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1347 EXPECT_EQ(profile_name2,
1348 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241349
1350 // Deleting a profile means returning to the original, actual profile name.
1351 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1352 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081353 content::RunAllTasksUntilIdle();
[email protected]78186a42014-08-22 22:07:301354 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441355 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241356}
nomsc9299462014-09-16 22:55:281357
1358TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1359 if (!profiles::IsMultipleProfilesEnabled())
1360 return;
1361
nomsc9299462014-09-16 22:55:281362 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121363 ProfileAttributesStorage& storage =
1364 profile_manager->GetProfileAttributesStorage();
1365 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281366
1367 // Create two signed in profiles, with both new and legacy default names, and
1368 // a profile with a custom name.
lwchkg498e92492016-04-23 11:04:121369 Profile* profile1 = AddProfileToStorage(profile_manager, "path_1",
1370 ASCIIToUTF16("Person 1"));
1371 Profile* profile2 = AddProfileToStorage(profile_manager, "path_2",
1372 ASCIIToUTF16("Default Profile"));
nomsc9299462014-09-16 22:55:281373 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
lwchkg498e92492016-04-23 11:04:121374 Profile* profile3 = AddProfileToStorage(profile_manager, "path_3",
1375 profile_name3);
1376 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281377
1378 // Sign in all profiles, and make sure they do not have a Gaia name set.
1379 const base::string16 email1(ASCIIToUTF16("[email protected]"));
1380 const base::string16 email2(ASCIIToUTF16("[email protected]"));
1381 const base::string16 email3(ASCIIToUTF16("[email protected]"));
1382
lwchkg498e92492016-04-23 11:04:121383 ProfileAttributesEntry* entry;
1384
1385 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1386 &entry));
1387 entry->SetAuthInfo("12345", email1);
1388 entry->SetGAIAGivenName(base::string16());
1389 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281390
1391 // This may resort the cache, so be extra cautious to use the right profile.
lwchkg498e92492016-04-23 11:04:121392 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(),
1393 &entry));
1394 entry->SetAuthInfo("23456", email2);
1395 entry->SetGAIAGivenName(base::string16());
1396 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281397
lwchkg498e92492016-04-23 11:04:121398 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(),
1399 &entry));
1400 entry->SetAuthInfo("34567", email3);
1401 entry->SetGAIAGivenName(base::string16());
1402 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281403
1404 // The profiles with default names should display the email address.
1405 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1406 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1407
1408 // The profile with the custom name should display that.
1409 EXPECT_EQ(profile_name3,
1410 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1411
1412 // Adding a Gaia name to a profile that previously had a default name should
1413 // start displaying it.
1414 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
lwchkg498e92492016-04-23 11:04:121415 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1416 &entry));
1417 entry->SetGAIAGivenName(gaia_given_name);
nomsc9299462014-09-16 22:55:281418 EXPECT_EQ(gaia_given_name,
1419 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1420}
[email protected]1c62b2f2013-06-28 00:15:001421#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1422
1423#if defined(OS_MACOSX)
1424// These tests are for a Mac-only code path that assumes the browser
1425// process isn't killed when all browser windows are closed.
1426TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1427 ProfileManager* profile_manager = g_browser_process->profile_manager();
1428 ASSERT_TRUE(profile_manager);
1429
1430 // Create and load one profile, and just create a second profile.
1431 const std::string profile_name1 = "New Profile 1";
1432 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221433 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1434 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001435
1436 MockObserver mock_observer;
1437 EXPECT_CALL(mock_observer, OnProfileCreated(
1438 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531439 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081440 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001441
1442 // Track the profile, but don't load it.
lwchkg498e92492016-04-23 11:04:121443 ProfileAttributesStorage& storage =
1444 profile_manager->GetProfileAttributesStorage();
1445 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1446 base::string16(), 0, std::string());
Gabriel Charette01507a22017-09-27 21:30:081447 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001448
1449 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121450 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001451
1452 // Set the active profile.
1453 PrefService* local_state = g_browser_process->local_state();
1454 local_state->SetString(prefs::kProfileLastUsed,
1455 dest_path1.BaseName().MaybeAsASCII());
1456
1457 // Delete the active profile. This should switch and load the unloaded
1458 // profile.
1459 profile_manager->ScheduleProfileForDeletion(dest_path1,
1460 ProfileManager::CreateCallback());
1461
Gabriel Charette01507a22017-09-27 21:30:081462 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001463
1464 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1465 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1466}
1467
1468// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1469// by simulating a scenario in which the profile that is being loaded as
1470// the next active profile has also been marked for deletion, so the
1471// ProfileManager needs to recursively select a different next profile.
1472TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1473 ProfileManager* profile_manager = g_browser_process->profile_manager();
1474 ASSERT_TRUE(profile_manager);
1475
1476 // Create and load one profile, and create two more profiles.
1477 const std::string profile_name1 = "New Profile 1";
1478 const std::string profile_name2 = "New Profile 2";
1479 const std::string profile_name3 = "New Profile 3";
vabr8023d872016-09-15 08:12:221480 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1481 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
1482 base::FilePath dest_path3 = temp_dir_.GetPath().AppendASCII(profile_name3);
[email protected]1c62b2f2013-06-28 00:15:001483
1484 MockObserver mock_observer;
1485 EXPECT_CALL(mock_observer, OnProfileCreated(
1486 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531487 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081488 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001489
1490 // Create the other profiles, but don't load them. Assign a fake avatar icon
lwchkg498e92492016-04-23 11:04:121491 // to ensure that profiles in the profile attributes storage are sorted by the
1492 // profile name, and not randomly by the avatar name.
1493 ProfileAttributesStorage& storage =
1494 profile_manager->GetProfileAttributesStorage();
1495 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1496 ASCIIToUTF16(profile_name2), 1, std::string());
1497 storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567",
1498 ASCIIToUTF16(profile_name3), 2, std::string());
[email protected]1c62b2f2013-06-28 00:15:001499
Gabriel Charette01507a22017-09-27 21:30:081500 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001501
1502 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121503 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001504
1505 // Set the active profile.
1506 PrefService* local_state = g_browser_process->local_state();
1507 local_state->SetString(prefs::kProfileLastUsed,
1508 dest_path1.BaseName().MaybeAsASCII());
1509
1510 // Delete the active profile, Profile1.
1511 // This will post a CreateProfileAsync message, that tries to load Profile2,
1512 // which checks that the profile is not being deleted, and then calls back
1513 // FinishDeletingProfile for Profile1.
1514 // Try to break this flow by setting the active profile to Profile2 in the
1515 // middle (so after the first posted message), and trying to delete Profile2,
1516 // so that the ProfileManager has to look for a different profile to load.
1517 profile_manager->ScheduleProfileForDeletion(dest_path1,
1518 ProfileManager::CreateCallback());
1519 local_state->SetString(prefs::kProfileLastUsed,
1520 dest_path2.BaseName().MaybeAsASCII());
1521 profile_manager->ScheduleProfileForDeletion(dest_path2,
1522 ProfileManager::CreateCallback());
Gabriel Charette01507a22017-09-27 21:30:081523 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001524
1525 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1526 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1527}
1528#endif // !defined(OS_MACOSX)