blob: a7a38a6b4dab2f6bfb453e1600d84d48b04f9a93 [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
David Roger51797642018-03-20 13:29:169#include "base/bind_helpers.h"
[email protected]943aed5542010-06-11 15:57:0810#include "base/command_line.h"
miguelg1b495542016-03-18 17:57:4611#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0412#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
avib896c712015-12-26 02:10:4314#include "base/macros.h"
vabr8684c9a2017-03-29 13:14:5715#include "base/memory/ptr_util.h"
[email protected]37ac95b2013-07-23 23:39:3516#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0817#include "base/strings/utf_string_conversions.h"
Gabriel Charette0b08bf22017-08-15 20:10:0618#include "base/threading/thread_task_runner_handle.h"
[email protected]29d70252011-04-28 02:16:5819#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3220#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4921#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3522#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0323#include "chrome/browser/chrome_notification_types.h"
[email protected]9718a722012-06-19 20:10:5324#include "chrome/browser/history/history_service_factory.h"
[email protected]63e26822011-07-16 19:07:3525#include "chrome/browser/io_thread.h"
[email protected]81ad7f4a2011-03-16 01:33:2926#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5927#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3328#include "chrome/browser/profiles/profile.h"
[email protected]c3e559772014-04-09 04:02:5429#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]279170832011-10-12 23:38:0330#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3331#include "chrome/browser/profiles/profile_manager.h"
[email protected]3f130532014-02-26 20:39:2432#include "chrome/browser/profiles/profiles_state.h"
[email protected]d8748142012-05-16 21:13:4333#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0834#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0836#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5837#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1838#include "chrome/grit/generated_resources.h"
[email protected]7688968a2013-02-12 21:45:1339#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2940#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3541#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1742#include "chrome/test/base/testing_profile.h"
Alexander Alekseev3f09a8f2018-05-03 02:52:1043#include "components/account_id/account_id.h"
sdefresne506dd522015-03-12 18:49:1244#include "components/history/core/browser/history_service.h"
David Rogera6c88122017-10-25 13:02:4645#include "components/signin/core/browser/profile_management_switches.h"
[email protected]ad50def52011-10-19 23:17:0746#include "content/public/browser/notification_service.h"
[email protected]f9357a442014-05-15 18:44:0747#include "content/public/common/content_switches.h"
[email protected]37ac95b2013-07-23 23:39:3548#include "content/public/test/test_browser_thread_bundle.h"
Gabriel Charettefaf432192017-08-17 22:00:4149#include "content/public/test/test_utils.h"
[email protected]844a1002011-04-19 11:37:2150#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2951#include "testing/gtest/include/gtest/gtest.h"
[email protected]3f130532014-02-26 20:39:2452#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2953
[email protected]775caec2011-09-01 17:14:3254#if defined(OS_CHROMEOS)
merkulova793f3022015-02-04 10:18:3055#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
[email protected]4d390782014-08-15 09:22:5856#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
dzhioev0298d722014-08-26 13:18:5257#include "chrome/browser/chromeos/profiles/profile_helper.h"
A Olsenc63621c2018-08-13 15:32:1758#include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
Wenzhao Zang8cf7ebd2017-12-22 02:37:1959#include "chrome/browser/ui/ash/test_wallpaper_controller.h"
60#include "chrome/browser/ui/ash/wallpaper_controller_client.h"
[email protected]931d1042013-04-05 17:50:4461#include "chromeos/chromeos_switches.h"
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:4762#include "components/arc/arc_prefs.h"
63#include "components/arc/arc_supervision_transition.h"
Xiyuan Xiadfe3a9f2017-11-13 21:46:2664#include "components/user_manager/scoped_user_manager.h"
[email protected]4d390782014-08-15 09:22:5865#include "components/user_manager/user_manager.h"
alemate3c1c0882016-10-27 19:05:1266#include "components/user_manager/user_names.h"
Andrew Wilsonc6c2c37e2017-09-01 14:44:2767#include "extensions/common/features/feature_session_type.h"
dzhioev0298d722014-08-26 13:18:5268#endif // defined(OS_CHROMEOS)
[email protected]775caec2011-09-01 17:14:3269
Jochen Eisingerd84bb42a2018-07-23 22:39:0370#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
71#include "chrome/browser/supervised_user/supervised_user_constants.h"
72#endif
73
[email protected]6778fed2013-12-24 20:09:3774using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3975
[email protected]844a1002011-04-19 11:37:2176namespace {
[email protected]f4e706252012-01-10 17:11:3677
[email protected]844a1002011-04-19 11:37:2178// This global variable is used to check that value returned to different
79// observers is the same.
Lei Zhangbd15e1c92018-07-03 08:53:4880Profile* g_created_profile = nullptr;
[email protected]844a1002011-04-19 11:37:2181
Lei Zhangbd15e1c92018-07-03 08:53:4882class UnittestProfileManager : public ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1783 public:
[email protected]9819fd02013-08-22 10:49:3984 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
Lei Zhangbd15e1c92018-07-03 08:53:4885 : ProfileManagerWithoutInit(user_data_dir) {}
86 ~UnittestProfileManager() override = default;
[email protected]537c1082011-12-02 02:37:1787
88 protected:
dcheng1fd716382014-10-22 20:14:4189 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]7567484142013-07-11 17:36:0790 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5491 if (!base::CreateDirectory(file_path))
Lei Zhangbd15e1c92018-07-03 08:53:4892 return nullptr;
[email protected]537c1082011-12-02 02:37:1793 }
Lei Zhangbd15e1c92018-07-03 08:53:4894 return new TestingProfile(file_path, nullptr);
[email protected]537c1082011-12-02 02:37:1795 }
96
dcheng1fd716382014-10-22 20:14:4197 Profile* CreateProfileAsyncHelper(const base::FilePath& path,
98 Delegate* delegate) override {
Gabriel Charette0b08bf22017-08-15 20:10:0699 // ThreadTaskRunnerHandle::Get() is TestingProfile's "async" IOTaskRunner
100 // (ref. TestingProfile::GetIOTaskRunner()).
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 FROM_HERE,
tzik6e4834a2017-04-20 15:42:58103 base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:17104
105 return new TestingProfile(path, this);
106 }
107};
108
miguelg1b495542016-03-18 17:57:46109void ExpectNullProfile(base::Closure closure, Profile* profile) {
110 EXPECT_EQ(nullptr, profile);
111 closure.Run();
112}
113
114void ExpectProfileWithName(const std::string& profile_name,
115 bool incognito,
116 base::Closure closure,
117 Profile* profile) {
118 EXPECT_NE(nullptr, profile);
119 EXPECT_EQ(incognito, profile->IsOffTheRecord());
120 if (incognito)
121 profile = profile->GetOriginalProfile();
122
123 // Create a profile on the fly so the the same comparison
124 // can be used in Windows and other platforms.
125 EXPECT_EQ(base::FilePath().AppendASCII(profile_name),
126 profile->GetPath().BaseName());
127 closure.Run();
128}
129
[email protected]9819fd02013-08-22 10:49:39130} // namespace
[email protected]537c1082011-12-02 02:37:17131
[email protected]583844c2011-08-27 00:38:35132class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:47133 protected:
[email protected]f4e706252012-01-10 17:11:36134 class MockObserver {
135 public:
136 MOCK_METHOD2(OnProfileCreated,
137 void(Profile* profile, Profile::CreateStatus status));
138 };
139
[email protected]6cad5bf2011-03-10 21:21:55140 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:35141 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:09142 }
143
dchenge1bc7982014-10-30 00:32:40144 void SetUp() override {
[email protected]3a305db2011-04-12 13:40:53145 // Create a new temporary directory, and store the path
146 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22147 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22148 new UnittestProfileManager(temp_dir_.GetPath()));
[email protected]f89b50f2012-08-16 21:36:50149
[email protected]537c1082011-12-02 02:37:17150#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43151 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
dzhioev0298d722014-08-26 13:18:52152 cl->AppendSwitch(switches::kTestType);
Wenzhao Zang8cf7ebd2017-12-22 02:37:19153 wallpaper_controller_client_ =
154 std::make_unique<WallpaperControllerClient>();
155 wallpaper_controller_client_->InitForTesting(
156 test_wallpaper_controller_.CreateInterfacePtr());
Andrew Wilsonc6c2c37e2017-09-01 14:44:27157
158 // Have to manually reset the session type in between test runs because
159 // some tests log in users.
160 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
161 extensions::GetCurrentFeatureSessionType());
162 session_type_ = extensions::ScopedCurrentFeatureSessionType(
163 extensions::GetCurrentFeatureSessionType());
[email protected]537c1082011-12-02 02:37:17164#endif
initial.commit09911bf2008-07-26 23:55:29165 }
[email protected]81ad7f4a2011-03-16 01:33:29166
dchenge1bc7982014-10-30 00:32:40167 void TearDown() override {
Lei Zhangbd15e1c92018-07-03 08:53:48168 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
Gabriel Charette01507a22017-09-27 21:30:08169 content::RunAllTasksUntilIdle();
xdaid05f51c2015-03-23 17:21:33170#if defined(OS_CHROMEOS)
Andrew Wilsonc6c2c37e2017-09-01 14:44:27171 session_type_.reset();
Wenzhao Zang8cf7ebd2017-12-22 02:37:19172 wallpaper_controller_client_.reset();
xdaid05f51c2015-03-23 17:21:33173#endif
initial.commit09911bf2008-07-26 23:55:29174 }
[email protected]f0a51fb52009-03-05 12:46:38175
[email protected]1c62b2f2013-06-28 00:15:00176 // Helper function to create a profile with |name| for a profile |manager|.
177 void CreateProfileAsync(ProfileManager* manager,
178 const std::string& name,
[email protected]1c62b2f2013-06-28 00:15:00179 MockObserver* mock_observer) {
vabr8023d872016-09-15 08:12:22180 manager->CreateProfileAsync(temp_dir_.GetPath().AppendASCII(name),
181 base::Bind(&MockObserver::OnProfileCreated,
182 base::Unretained(mock_observer)),
183 base::UTF8ToUTF16(name),
Marc Treib92127162018-08-27 14:20:03184 profiles::GetDefaultAvatarIconUrl(0));
[email protected]1c62b2f2013-06-28 00:15:00185 }
186
lwchkg498e92492016-04-23 11:04:12187 // Helper function to add a profile with |profile_name| to |profile_manager|'s
188 // ProfileAttributesStorage, and return the profile created.
189 Profile* AddProfileToStorage(ProfileManager* profile_manager,
190 const std::string& path_suffix,
191 const base::string16& profile_name) {
192 ProfileAttributesStorage& storage =
193 profile_manager->GetProfileAttributesStorage();
194 size_t num_profiles = storage.GetNumberOfProfiles();
vabr8023d872016-09-15 08:12:22195 base::FilePath path = temp_dir_.GetPath().AppendASCII(path_suffix);
lwchkg498e92492016-04-23 11:04:12196 storage.AddProfile(path, profile_name, std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58197 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12198 EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:24199 return profile_manager->GetProfile(path);
200 }
201
palar47cf44bd2017-03-03 15:36:11202 // Helper function to set profile ephemeral at prefs and attributes storage.
203 void SetProfileEphemeral(Profile* profile) {
204 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
205
206 // Update IsEphemeral in attributes storage, normally it happened via
207 // kForceEphemeralProfiles pref change event routed to
208 // ProfileImpl::UpdateIsEphemeralInStorage().
209 ProfileAttributesEntry* entry;
210 ProfileAttributesStorage& storage =
211 g_browser_process->profile_manager()->GetProfileAttributesStorage();
212 EXPECT_TRUE(
213 storage.GetProfileAttributesWithPath(profile->GetPath(), &entry));
214 entry->SetIsEphemeral(true);
215 }
216
[email protected]6f96cbcb2011-11-04 02:26:07217#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52218 // Helper function to register an user with id |user_id| and create profile
219 // with a correct path.
Alexander Alekseev17513602017-12-05 03:16:36220 void RegisterUser(const AccountId& account_id) {
dzhioev0298d722014-08-26 13:18:52221 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
222 const std::string user_id_hash =
Alexander Alekseev17513602017-12-05 03:16:36223 profile_helper->GetUserIdHashByUserIdForTesting(
224 account_id.GetUserEmail());
225 user_manager::UserManager::Get()->UserLoggedIn(account_id, user_id_hash,
Alexander Alekseev2a5efd62017-12-06 07:27:28226 false /* browser_restart */,
227 false /* is_child */);
dzhioev0298d722014-08-26 13:18:52228 g_browser_process->profile_manager()->GetProfile(
229 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
230 }
231
A Olsenc63621c2018-08-13 15:32:17232 chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
[email protected]6f96cbcb2011-11-04 02:26:07233#endif
234
Lei Zhangbd15e1c92018-07-03 08:53:48235 content::TestBrowserThreadBundle thread_bundle_;
236
[email protected]3a305db2011-04-12 13:40:53237 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23238 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35239 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35240
[email protected]363f5272013-04-23 17:21:42241#if defined(OS_CHROMEOS)
242 chromeos::ScopedTestUserManager test_user_manager_;
Andrew Wilsonc6c2c37e2017-09-01 14:44:27243 std::unique_ptr<base::AutoReset<extensions::FeatureSessionType>>
244 session_type_;
Wenzhao Zang8cf7ebd2017-12-22 02:37:19245 std::unique_ptr<WallpaperControllerClient> wallpaper_controller_client_;
246 TestWallpaperController test_wallpaper_controller_;
[email protected]363f5272013-04-23 17:21:42247#endif
dzhioev0298d722014-08-26 13:18:52248
249 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
initial.commit09911bf2008-07-26 23:55:29250};
251
[email protected]844a1002011-04-19 11:37:21252TEST_F(ProfileManagerTest, GetProfile) {
vabr8023d872016-09-15 08:12:22253 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]f7011fcb2009-01-28 21:54:32254 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29255
[email protected]881cb0b62011-12-13 20:39:54256 ProfileManager* profile_manager = g_browser_process->profile_manager();
257
initial.commit09911bf2008-07-26 23:55:29258 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36259 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21260 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29261
[email protected]844a1002011-04-19 11:37:21262 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54263 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29264}
[email protected]55474b572009-04-14 22:05:33265
[email protected]943aed5542010-06-11 15:57:08266TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45267 base::FilePath expected_default =
268 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54269 EXPECT_EQ(
270 expected_default.value(),
271 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08272}
273
skuhne24bb24432014-09-08 19:31:47274MATCHER(NotFail, "Profile creation failure status is not reported.") {
275 return arg == Profile::CREATE_STATUS_CREATED ||
276 arg == Profile::CREATE_STATUS_INITIALIZED;
277}
278
279MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
280 if (!g_created_profile)
281 g_created_profile = arg;
Lei Zhangbd15e1c92018-07-03 08:53:48282 return arg && arg == g_created_profile;
skuhne24bb24432014-09-08 19:31:47283}
284
[email protected]943aed5542010-06-11 15:57:08285#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52286
[email protected]943aed5542010-06-11 15:57:08287// This functionality only exists on Chrome OS.
288TEST_F(ProfileManagerTest, LoggedInProfileDir) {
[email protected]650b2d52013-02-10 03:41:45289 base::FilePath expected_default =
290 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54291 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08292 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54293 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08294
Alexander Alekseev17513602017-12-05 03:16:36295 constexpr char kTestUserName[] = "[email protected]";
296 constexpr char kTestUserGaiaId[] = "0123456789";
297 const AccountId test_account_id(
298 AccountId::FromUserEmailGaiaId(kTestUserName, kTestUserGaiaId));
merkulova793f3022015-02-04 10:18:30299 chromeos::FakeChromeUserManager* user_manager =
300 new chromeos::FakeChromeUserManager();
Xiyuan Xiadfe3a9f2017-11-13 21:46:26301 user_manager::ScopedUserManager enabler(base::WrapUnique(user_manager));
dzhioev0298d722014-08-26 13:18:52302
alemate3ffbde6f2015-11-03 02:02:55303 const user_manager::User* active_user =
304 user_manager->AddUser(test_account_id);
305 user_manager->LoginUser(test_account_id);
306 user_manager->SwitchActiveUser(test_account_id);
dzhioev0298d722014-08-26 13:18:52307
[email protected]2fda9972014-07-23 14:51:59308 profile_manager->Observe(
309 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
310 content::NotificationService::AllSources(),
311 content::Details<const user_manager::User>(active_user));
dzhioev0298d722014-08-26 13:18:52312 base::FilePath expected_logged_in(
313 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
[email protected]943aed5542010-06-11 15:57:08314 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54315 profile_manager->GetInitialProfileDir().value());
vabr8023d872016-09-15 08:12:22316 VLOG(1) << temp_dir_.GetPath()
317 .Append(profile_manager->GetInitialProfileDir())
318 .value();
[email protected]943aed5542010-06-11 15:57:08319}
320
xiyuan41d32a32017-06-12 20:06:13321// Test Get[ActiveUser|PrimaryUser|LastUsed]Profile does not load user profile.
322TEST_F(ProfileManagerTest, UserProfileLoading) {
323 using chromeos::ProfileHelper;
324
325 Profile* const signin_profile = ProfileHelper::GetSigninProfile();
326
327 // Get[Active|Primary|LastUsed]Profile return the sign-in profile before login
328 // happens. IsSameProfile() is used to properly test against TestProfile whose
329 // OTR version uses a different temp path.
330 EXPECT_TRUE(
331 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
332 EXPECT_TRUE(
333 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
334 EXPECT_TRUE(
335 ProfileManager::GetLastUsedProfile()->IsSameProfile(signin_profile));
336
337 // User signs in but user profile loading has not started.
338 const std::string user_id = "[email protected]";
Alexander Alekseev17513602017-12-05 03:16:36339 const std::string gaia_id = "0123456789";
xiyuan41d32a32017-06-12 20:06:13340 const std::string user_id_hash =
341 ProfileHelper::Get()->GetUserIdHashByUserIdForTesting(user_id);
342 user_manager::UserManager::Get()->UserLoggedIn(
Alexander Alekseev2a5efd62017-12-06 07:27:28343 AccountId::FromUserEmailGaiaId(user_id, gaia_id), user_id_hash,
344 false /* browser_restart */, false /* is_child */);
xiyuan41d32a32017-06-12 20:06:13345
346 // Sign-in profile should be returned at this stage. Otherwise, login code
347 // ends up in an invalid state. Strange things as in http://crbug.com/728683
348 // and http://crbug.com/718734 happens.
349 EXPECT_TRUE(
350 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
351 EXPECT_TRUE(
352 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
xiyuan8c6a4112017-06-19 21:03:49353
354 // GetLastUsedProfile() after login but before a user profile is loaded is
355 // fatal.
356 EXPECT_DEATH_IF_SUPPORTED(ProfileManager::GetLastUsedProfile(), ".*");
xiyuan41d32a32017-06-12 20:06:13357
358 // Simulate UserSessionManager loads the profile.
359 Profile* const user_profile =
360 g_browser_process->profile_manager()->GetProfile(
361 ProfileHelper::Get()->GetProfilePathByUserIdHash(user_id_hash));
362 ASSERT_FALSE(user_profile->IsSameProfile(signin_profile));
363
364 // User profile is returned thereafter.
365 EXPECT_TRUE(
366 ProfileManager::GetActiveUserProfile()->IsSameProfile(user_profile));
367 EXPECT_TRUE(
368 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(user_profile));
369 EXPECT_TRUE(
370 ProfileManager::GetLastUsedProfile()->IsSameProfile(user_profile));
371}
372
Lei Zhangbd15e1c92018-07-03 08:53:48373#endif // defined(OS_CHROMEOS)
[email protected]943aed5542010-06-11 15:57:08374
Scott Little4087e1c2017-11-28 19:56:59375// Data race on Linux bots. http://crbug.com/789214
376#if defined(OS_LINUX)
377#define MAYBE_CreateAndUseTwoProfiles DISABLED_CreateAndUseTwoProfiles
378#else
379#define MAYBE_CreateAndUseTwoProfiles CreateAndUseTwoProfiles
380#endif
381
382TEST_F(ProfileManagerTest, MAYBE_CreateAndUseTwoProfiles) {
vabr8023d872016-09-15 08:12:22383 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33384 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47385
vabr8023d872016-09-15 08:12:22386 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33387 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
388
[email protected]881cb0b62011-12-13 20:39:54389 ProfileManager* profile_manager = g_browser_process->profile_manager();
390
[email protected]55474b572009-04-14 22:05:33391 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17392 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54393 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21394 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33395
[email protected]537c1082011-12-02 02:37:17396 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54397 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21398 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33399
400 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31401 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04402 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
403 profile1, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17404 profile1->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30405 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile1));
[email protected]537c1082011-12-02 02:37:17406 profile2->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30407 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile2));
[email protected]608e7e02013-07-24 12:23:31408 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04409 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
410 profile2, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55411
412 // Make sure any pending tasks run before we destroy the profiles.
Gabriel Charette01507a22017-09-27 21:30:08413 content::RunAllTasksUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55414
Lei Zhangbd15e1c92018-07-03 08:53:48415 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
[email protected]6cad5bf2011-03-10 21:21:55416
[email protected]237e6d02010-11-08 21:45:42417 // Make sure history cleans up correctly.
Gabriel Charette01507a22017-09-27 21:30:08418 content::RunAllTasksUntilIdle();
[email protected]55474b572009-04-14 22:05:33419}
[email protected]844a1002011-04-19 11:37:21420
miguelg1b495542016-03-18 17:57:46421TEST_F(ProfileManagerTest, LoadNonExistingProfile) {
422 const std::string profile_name = "NonExistingProfile";
423 base::RunLoop run_loop_1;
424 base::RunLoop run_loop_2;
425
426 ProfileManager* profile_manager = g_browser_process->profile_manager();
427 profile_manager->LoadProfile(
428 profile_name, false /* incognito */,
429 base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure()));
430 run_loop_1.Run();
431
432 profile_manager->LoadProfile(
433 profile_name, true /* incognito */,
434 base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure()));
435 run_loop_2.Run();
436}
437
438TEST_F(ProfileManagerTest, LoadExistingProfile) {
439 const std::string profile_name = "MyProfile";
440 const std::string other_name = "SomeOtherProfile";
441 MockObserver mock_observer1;
442 EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail()))
443 .Times(testing::AtLeast(1));
444
445 ProfileManager* profile_manager = g_browser_process->profile_manager();
Marc Treib92127162018-08-27 14:20:03446 CreateProfileAsync(profile_manager, profile_name, &mock_observer1);
miguelg1b495542016-03-18 17:57:46447
448 // Make sure a real profile is created before continuing.
Gabriel Charette01507a22017-09-27 21:30:08449 content::RunAllTasksUntilIdle();
miguelg1b495542016-03-18 17:57:46450
451 base::RunLoop load_profile;
452 bool incognito = false;
453 profile_manager->LoadProfile(
454 profile_name, incognito,
455 base::Bind(&ExpectProfileWithName, profile_name, incognito,
456 load_profile.QuitClosure()));
457 load_profile.Run();
458
459 base::RunLoop load_profile_incognito;
460 incognito = true;
461 profile_manager->LoadProfile(
462 profile_name, incognito,
463 base::Bind(&ExpectProfileWithName, profile_name, incognito,
464 load_profile_incognito.QuitClosure()));
465 load_profile_incognito.Run();
466
467 // Loading some other non existing profile should still return null.
468 base::RunLoop load_other_profile;
469 profile_manager->LoadProfile(
470 other_name, false,
471 base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure()));
472 load_other_profile.Run();
473}
474
[email protected]844a1002011-04-19 11:37:21475TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
Lei Zhangbd15e1c92018-07-03 08:53:48476 g_created_profile = nullptr;
[email protected]844a1002011-04-19 11:37:21477
478 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11479 EXPECT_CALL(mock_observer1, OnProfileCreated(
480 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21481 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11482 EXPECT_CALL(mock_observer2, OnProfileCreated(
483 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21484 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11485 EXPECT_CALL(mock_observer3, OnProfileCreated(
486 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21487
[email protected]881cb0b62011-12-13 20:39:54488 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00489 const std::string profile_name = "New Profile";
Marc Treib92127162018-08-27 14:20:03490 CreateProfileAsync(profile_manager, profile_name, &mock_observer1);
491 CreateProfileAsync(profile_manager, profile_name, &mock_observer2);
492 CreateProfileAsync(profile_manager, profile_name, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21493
Gabriel Charette01507a22017-09-27 21:30:08494 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21495}
496
497TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00498 const std::string profile_name1 = "New Profile 1";
499 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21500
501 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11502 EXPECT_CALL(mock_observer, OnProfileCreated(
503 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21504
[email protected]881cb0b62011-12-13 20:39:54505 ProfileManager* profile_manager = g_browser_process->profile_manager();
506
Marc Treib92127162018-08-27 14:20:03507 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
508 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
[email protected]844a1002011-04-19 11:37:21509
Gabriel Charette01507a22017-09-27 21:30:08510 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21511}
[email protected]279170832011-10-12 23:38:03512
lwchkg498e92492016-04-23 11:04:12513TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) {
[email protected]5ddfade2014-02-03 10:24:53514 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12515 ProfileAttributesStorage& storage =
516 profile_manager->GetProfileAttributesStorage();
517 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53518
Jochen Eisingerd84bb42a2018-07-23 22:39:03519#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05520 const base::FilePath supervised_path =
vabr8023d872016-09-15 08:12:22521 temp_dir_.GetPath().AppendASCII("Supervised");
[email protected]d20d0432014-06-12 17:14:05522 TestingProfile* supervised_profile =
Lei Zhangbd15e1c92018-07-03 08:53:48523 new TestingProfile(supervised_path, nullptr);
Jochen Eisingerd84bb42a2018-07-23 22:39:03524 supervised_profile->GetPrefs()->SetString(
525 prefs::kSupervisedUserId, supervised_users::kChildAccountSUID);
[email protected]5ddfade2014-02-03 10:24:53526
527 // RegisterTestingProfile adds the profile to the cache and takes ownership.
[email protected]d20d0432014-06-12 17:14:05528 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
Lei Zhangbd15e1c92018-07-03 08:53:48529 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
530 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0]->IsOmitted());
Jochen Eisingerd84bb42a2018-07-23 22:39:03531#endif
[email protected]5ddfade2014-02-03 10:24:53532
vabr8023d872016-09-15 08:12:22533 const base::FilePath nonsupervised_path =
534 temp_dir_.GetPath().AppendASCII("Non-Supervised");
Lei Zhangbd15e1c92018-07-03 08:53:48535 TestingProfile* nonsupervised_profile =
536 new TestingProfile(nonsupervised_path, nullptr);
[email protected]d20d0432014-06-12 17:14:05537 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
[email protected]5ddfade2014-02-03 10:24:53538
Jochen Eisingerd84bb42a2018-07-23 22:39:03539#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
lwchkg498e92492016-04-23 11:04:12540 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
Jochen Eisingerd84bb42a2018-07-23 22:39:03541#else
542 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
543#endif
lwchkg498e92492016-04-23 11:04:12544 ProfileAttributesEntry* entry;
Jochen Eisingerd84bb42a2018-07-23 22:39:03545#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
lwchkg498e92492016-04-23 11:04:12546 ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry));
547 EXPECT_TRUE(entry->IsOmitted());
Jochen Eisingerd84bb42a2018-07-23 22:39:03548#endif
lwchkg498e92492016-04-23 11:04:12549
550 ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry));
551 EXPECT_FALSE(entry->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53552}
553
[email protected]4251165a2013-07-17 04:33:40554TEST_F(ProfileManagerTest, GetGuestProfilePath) {
555 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
vabr8023d872016-09-15 08:12:22556 base::FilePath expected_path = temp_dir_.GetPath();
[email protected]4251165a2013-07-17 04:33:40557 expected_path = expected_path.Append(chrome::kGuestProfileDir);
558 EXPECT_EQ(expected_path, guest_path);
559}
560
mlermanb8df6e82015-01-23 19:55:34561TEST_F(ProfileManagerTest, GetSystemProfilePath) {
562 base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath();
vabr8023d872016-09-15 08:12:22563 base::FilePath expected_path = temp_dir_.GetPath();
mlermanb8df6e82015-01-23 19:55:34564 expected_path = expected_path.Append(chrome::kSystemProfileDir);
565 EXPECT_EQ(expected_path, system_profile_path);
566}
567
[email protected]0ff6198e2014-01-08 22:45:00568class UnittestGuestProfileManager : public UnittestProfileManager {
569 public:
570 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
571 : UnittestProfileManager(user_data_dir) {}
572
573 protected:
dcheng1fd716382014-10-22 20:14:41574 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]0ff6198e2014-01-08 22:45:00575 TestingProfile::Builder builder;
[email protected]0ff6198e2014-01-08 22:45:00576 builder.SetGuestSession();
[email protected]d22f7c932014-02-20 22:48:58577 builder.SetPath(file_path);
578 TestingProfile* testing_profile = builder.Build().release();
[email protected]0ff6198e2014-01-08 22:45:00579 return testing_profile;
580 }
581};
582
[email protected]9b6e10c2014-05-20 19:12:28583class ProfileManagerGuestTest : public ProfileManagerTest {
[email protected]0ff6198e2014-01-08 22:45:00584 protected:
dchenge1bc7982014-10-30 00:32:40585 void SetUp() override {
[email protected]0ff6198e2014-01-08 22:45:00586 // Create a new temporary directory, and store the path
587 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
588 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22589 new UnittestGuestProfileManager(temp_dir_.GetPath()));
[email protected]0ff6198e2014-01-08 22:45:00590
[email protected]2a92f2182014-03-25 00:47:59591#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43592 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
[email protected]9b6e10c2014-05-20 19:12:28593 // This switch is needed to skip non-test specific behavior in
594 // ProfileManager (accessing DBusThreadManager).
[email protected]0ff6198e2014-01-08 22:45:00595 cl->AppendSwitch(switches::kTestType);
[email protected]9b6e10c2014-05-20 19:12:28596
[email protected]0ff6198e2014-01-08 22:45:00597 cl->AppendSwitch(chromeos::switches::kGuestSession);
598 cl->AppendSwitch(::switches::kIncognito);
599
Wenzhao Zang8cf7ebd2017-12-22 02:37:19600 wallpaper_controller_client_ =
601 std::make_unique<WallpaperControllerClient>();
602 wallpaper_controller_client_->InitForTesting(
603 test_wallpaper_controller_.CreateInterfacePtr());
604
Andrew Wilsonc6c2c37e2017-09-01 14:44:27605 // Have to manually reset the session type in between test runs because
606 // RegisterUser() changes it.
607 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
608 extensions::GetCurrentFeatureSessionType());
609 session_type_ = extensions::ScopedCurrentFeatureSessionType(
610 extensions::GetCurrentFeatureSessionType());
611
Alexander Alekseev17513602017-12-05 03:16:36612 RegisterUser(GetFakeUserManager()->GetGuestAccountId());
[email protected]2a92f2182014-03-25 00:47:59613#endif
[email protected]0ff6198e2014-01-08 22:45:00614 }
Alexander Alekseev17513602017-12-05 03:16:36615
616 private:
617#if defined(OS_CHROMEOS)
618 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
619 return static_cast<chromeos::FakeChromeUserManager*>(
620 user_manager::UserManager::Get());
621 }
622#endif
[email protected]0ff6198e2014-01-08 22:45:00623};
624
[email protected]9b6e10c2014-05-20 19:12:28625TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
[email protected]2a92f2182014-03-25 00:47:59626 ProfileManager* profile_manager = g_browser_process->profile_manager();
627 ASSERT_TRUE(profile_manager);
628
629 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
630 ASSERT_TRUE(profile);
631 EXPECT_TRUE(profile->IsOffTheRecord());
632}
633
634#if defined(OS_CHROMEOS)
[email protected]9b6e10c2014-05-20 19:12:28635TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
[email protected]0ff6198e2014-01-08 22:45:00636 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
637 EXPECT_TRUE(primary_profile->IsOffTheRecord());
638
639 Profile* active_profile = ProfileManager::GetActiveUserProfile();
640 EXPECT_TRUE(active_profile->IsOffTheRecord());
641
642 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
[email protected]d22f7c932014-02-20 22:48:58643
644 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
645 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
646
647 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
[email protected]0ff6198e2014-01-08 22:45:00648}
649#endif
650
[email protected]279170832011-10-12 23:38:03651TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54652 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12653 ProfileAttributesStorage& storage =
654 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27655 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
Jeremy Romanec48d7a2018-03-01 17:35:09656 std::make_unique<base::Value>(true));
[email protected]714bf1d2012-11-30 01:35:27657
658 // Setting a pref which is not applicable to a system (i.e., Android in this
659 // case) does not necessarily create it. Don't bother continuing with the
660 // test if this pref doesn't exist because it will not load the profiles if
661 // it cannot verify that the pref for background mode is enabled.
662 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
663 return;
[email protected]279170832011-10-12 23:38:03664
lwchkg498e92492016-04-23 11:04:12665 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
666
667 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
Roman Sorokin8ead4d72018-04-16 15:58:58668 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
669 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12670 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
Roman Sorokin8ead4d72018-04-16 15:58:58671 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
672 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12673 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"),
Roman Sorokin8ead4d72018-04-16 15:58:58674 ASCIIToUTF16("name_3"), "34567", base::string16(), 0,
675 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12676
Lei Zhangbd15e1c92018-07-03 08:53:48677 ASSERT_EQ(3u, storage.GetNumberOfProfiles());
lwchkg498e92492016-04-23 11:04:12678
679 std::vector<ProfileAttributesEntry*> entries =
680 storage.GetAllProfilesAttributes();
Lei Zhangbd15e1c92018-07-03 08:53:48681 entries[0]->SetBackgroundStatus(true);
682 entries[2]->SetBackgroundStatus(true);
[email protected]279170832011-10-12 23:38:03683
[email protected]881cb0b62011-12-13 20:39:54684 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03685
[email protected]881cb0b62011-12-13 20:39:54686 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
687}
688
[email protected]714bf1d2012-11-30 01:35:27689TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
690 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12691 ProfileAttributesStorage& storage =
692 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27693 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
Jeremy Romanec48d7a2018-03-01 17:35:09694 std::make_unique<base::Value>(false));
[email protected]714bf1d2012-11-30 01:35:27695
lwchkg498e92492016-04-23 11:04:12696 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
697
698 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
Roman Sorokin8ead4d72018-04-16 15:58:58699 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
700 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12701 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
Roman Sorokin8ead4d72018-04-16 15:58:58702 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
703 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12704
Lei Zhangbd15e1c92018-07-03 08:53:48705 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
lwchkg498e92492016-04-23 11:04:12706
707 std::vector<ProfileAttributesEntry*> entries =
708 storage.GetAllProfilesAttributes();
Lei Zhangbd15e1c92018-07-03 08:53:48709 entries[0]->SetBackgroundStatus(false);
710 entries[1]->SetBackgroundStatus(true);
[email protected]714bf1d2012-11-30 01:35:27711
712 profile_manager->AutoloadProfiles();
713
714 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
715}
716
[email protected]881cb0b62011-12-13 20:39:54717TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
vabr8023d872016-09-15 08:12:22718 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54719 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
720
721 ProfileManager* profile_manager = g_browser_process->profile_manager();
722
723 Profile* profile;
724
725 // Successfully create the profile
726 profile = profile_manager->GetProfile(dest_path);
727 ASSERT_TRUE(profile);
728
729 // Check that the profile name is non empty
730 std::string profile_name =
731 profile->GetPrefs()->GetString(prefs::kProfileName);
732 EXPECT_FALSE(profile_name.empty());
733
734 // Check that the profile avatar index is valid
735 size_t avatar_index =
736 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
[email protected]c3e559772014-04-09 04:02:54737 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
[email protected]881cb0b62011-12-13 20:39:54738 avatar_index));
739}
740
741// Tests that a new profile's entry in the profile info cache is setup with the
742// same values that are in the profile prefs.
743TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
vabr8023d872016-09-15 08:12:22744 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54745 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
746
747 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]881cb0b62011-12-13 20:39:54748
749 // Successfully create the profile
750 Profile* profile = profile_manager->GetProfile(dest_path);
751 ASSERT_TRUE(profile);
752
753 std::string profile_name =
754 profile->GetPrefs()->GetString(prefs::kProfileName);
755 size_t avatar_index =
756 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
757
lwchkg498e92492016-04-23 11:04:12758 ProfileAttributesEntry* entry;
759 ASSERT_TRUE(profile_manager->GetProfileAttributesStorage().
760 GetProfileAttributesWithPath(dest_path, &entry));
[email protected]881cb0b62011-12-13 20:39:54761
762 // Check if the profile prefs are the same as the cache prefs
lwchkg498e92492016-04-23 11:04:12763 EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName()));
764 EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex());
[email protected]279170832011-10-12 23:38:03765}
[email protected]52edf812012-01-12 11:48:46766
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47767#if defined(OS_CHROMEOS)
768TEST_F(ProfileManagerTest, InitProfileForChildOnFirstSignIn) {
769 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
770 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
771
772 const std::string user_email = "[email protected]";
773 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
774 const std::string user_id_hash =
775 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
776 const base::FilePath dest_path =
777 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
778
779 TestingProfile::Builder builder;
780 builder.SetPath(dest_path);
781 builder.OverrideIsNewProfile(true);
782 std::unique_ptr<Profile> profile = builder.Build();
783
784 user_manager->UserLoggedIn(account_id, user_id_hash,
785 false /* browser_restart */, true /* is_child */);
786 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
787
788 EXPECT_EQ(
789 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
790 static_cast<int>(arc::ArcSupervisionTransition::NO_TRANSITION));
791 EXPECT_EQ(profile->GetPrefs()->GetString(prefs::kSupervisedUserId),
792 supervised_users::kChildAccountSUID);
793}
794
795TEST_F(ProfileManagerTest, InitProfileForRegularToChildTransition) {
796 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
797 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
798
799 const std::string user_email = "[email protected]";
800 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
801 const std::string user_id_hash =
802 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
803 const base::FilePath dest_path =
804 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
805
806 TestingProfile::Builder builder;
807 builder.SetPath(dest_path);
808 builder.OverrideIsNewProfile(false);
809 std::unique_ptr<Profile> profile = builder.Build();
Patricia Hongo890c47d2018-08-14 16:26:33810 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, true);
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47811
812 user_manager->UserLoggedIn(account_id, user_id_hash,
813 false /* browser_restart */, true /* is_child */);
814 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
815
816 EXPECT_EQ(
817 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
818 static_cast<int>(arc::ArcSupervisionTransition::REGULAR_TO_CHILD));
819 EXPECT_EQ(profile->GetPrefs()->GetString(prefs::kSupervisedUserId),
820 supervised_users::kChildAccountSUID);
821}
822
823TEST_F(ProfileManagerTest, InitProfileForChildToRegularTransition) {
824 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
825 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
826
827 const std::string user_email = "[email protected]";
828 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
829 const std::string user_id_hash =
830 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
831 const base::FilePath dest_path =
832 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
833
834 TestingProfile::Builder builder;
835 builder.SetPath(dest_path);
836 builder.OverrideIsNewProfile(false);
837 builder.SetSupervisedUserId(supervised_users::kChildAccountSUID);
838 std::unique_ptr<Profile> profile = builder.Build();
Patricia Hongo890c47d2018-08-14 16:26:33839 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, true);
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47840
841 user_manager->UserLoggedIn(account_id, user_id_hash,
842 false /* browser_restart */, false /* is_child */);
843 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
844
845 EXPECT_EQ(
846 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
847 static_cast<int>(arc::ArcSupervisionTransition::CHILD_TO_REGULAR));
848 EXPECT_TRUE(profile->GetPrefs()->GetString(prefs::kSupervisedUserId).empty());
849}
850
Patricia Hongo890c47d2018-08-14 16:26:33851TEST_F(ProfileManagerTest,
852 InitProfileForChildToRegularTransitionArcNotSignedIn) {
853 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
854 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
855
856 const std::string user_email = "[email protected]";
857 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
858 const std::string user_id_hash =
859 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
860 const base::FilePath dest_path =
861 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
862
863 TestingProfile::Builder builder;
864 builder.SetPath(dest_path);
865 builder.OverrideIsNewProfile(false);
866 builder.SetSupervisedUserId(supervised_users::kChildAccountSUID);
867 std::unique_ptr<Profile> profile = builder.Build();
868 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, false);
869
870 user_manager->UserLoggedIn(account_id, user_id_hash,
871 false /* browser_restart */, false /* is_child */);
872 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
873
874 EXPECT_EQ(
875 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
876 static_cast<int>(arc::ArcSupervisionTransition::NO_TRANSITION));
877 EXPECT_TRUE(profile->GetPrefs()->GetString(prefs::kSupervisedUserId).empty());
878}
879
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47880#endif
881
[email protected]13720532013-05-30 12:30:59882TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
883 ProfileManager* profile_manager = g_browser_process->profile_manager();
884 ASSERT_TRUE(profile_manager);
885
dzhioev0298d722014-08-26 13:18:52886#if defined(OS_CHROMEOS)
xiyuan41d32a32017-06-12 20:06:13887 // On CrOS, profile returned by GetLastUsedProfile is a sign-in profile that
dzhioev0298d722014-08-26 13:18:52888 // is forced to be incognito. That's why we need to create at least one user
889 // to get a regular profile.
Alexander Alekseev17513602017-12-05 03:16:36890 RegisterUser(
891 AccountId::FromUserEmailGaiaId("[email protected]", "1234567890"));
dzhioev0298d722014-08-26 13:18:52892#endif
893
[email protected]13720532013-05-30 12:30:59894 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
895 ASSERT_TRUE(profile);
896 EXPECT_FALSE(profile->IsOffTheRecord());
897 PrefService* prefs = profile->GetPrefs();
898 EXPECT_EQ(IncognitoModePrefs::ENABLED,
899 IncognitoModePrefs::GetAvailability(prefs));
900
[email protected]13720532013-05-30 12:30:59901 ASSERT_TRUE(profile->GetOffTheRecordProfile());
902
903 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
904 EXPECT_FALSE(
905 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
906
907 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
908 // incognito mode is forced.
909 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
910 EXPECT_TRUE(
911 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
912}
913
[email protected]c849fab2012-03-29 16:51:41914#if !defined(OS_ANDROID)
915// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46916TEST_F(ProfileManagerTest, LastOpenedProfiles) {
vabr8023d872016-09-15 08:12:22917 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46918 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
919
vabr8023d872016-09-15 08:12:22920 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46921 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
922
923 ProfileManager* profile_manager = g_browser_process->profile_manager();
924
925 // Successfully create the profiles.
926 TestingProfile* profile1 =
927 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
928 ASSERT_TRUE(profile1);
929
930 TestingProfile* profile2 =
931 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
932 ASSERT_TRUE(profile2);
933
934 std::vector<Profile*> last_opened_profiles =
935 profile_manager->GetLastOpenedProfiles();
936 ASSERT_EQ(0U, last_opened_profiles.size());
937
938 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:35939 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:03940 std::unique_ptr<Browser> browser1a(
cm.sanchi8b7ec882017-11-16 07:22:05941 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46942
943 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
944 ASSERT_EQ(1U, last_opened_profiles.size());
945 EXPECT_EQ(profile1, last_opened_profiles[0]);
946
947 // And for profile2.
erg7b01d692017-02-22 21:57:35948 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:03949 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:05950 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46951
952 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
953 ASSERT_EQ(2U, last_opened_profiles.size());
954 EXPECT_EQ(profile1, last_opened_profiles[0]);
955 EXPECT_EQ(profile2, last_opened_profiles[1]);
956
957 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03958 std::unique_ptr<Browser> browser1b(
cm.sanchi8b7ec882017-11-16 07:22:05959 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46960 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
961 ASSERT_EQ(2U, last_opened_profiles.size());
962 EXPECT_EQ(profile1, last_opened_profiles[0]);
963 EXPECT_EQ(profile2, last_opened_profiles[1]);
964
965 // Close the browsers.
966 browser1a.reset();
967 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
968 ASSERT_EQ(2U, last_opened_profiles.size());
969 EXPECT_EQ(profile1, last_opened_profiles[0]);
970 EXPECT_EQ(profile2, last_opened_profiles[1]);
971
972 browser1b.reset();
973 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
974 ASSERT_EQ(1U, last_opened_profiles.size());
975 EXPECT_EQ(profile2, last_opened_profiles[0]);
976
977 browser2.reset();
978 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
979 ASSERT_EQ(0U, last_opened_profiles.size());
980}
981
982TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
vabr8023d872016-09-15 08:12:22983 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46984 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
985
vabr8023d872016-09-15 08:12:22986 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46987 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
988
989 ProfileManager* profile_manager = g_browser_process->profile_manager();
990
991 // Successfully create the profiles.
992 TestingProfile* profile1 =
993 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
994 ASSERT_TRUE(profile1);
995
996 TestingProfile* profile2 =
997 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
998 ASSERT_TRUE(profile2);
999
1000 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351001 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:031002 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051003 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:461004
1005 // And for profile2.
erg7b01d692017-02-22 21:57:351006 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:031007 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:051008 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:461009
1010 std::vector<Profile*> last_opened_profiles =
1011 profile_manager->GetLastOpenedProfiles();
1012 ASSERT_EQ(2U, last_opened_profiles.size());
1013 EXPECT_EQ(profile1, last_opened_profiles[0]);
1014 EXPECT_EQ(profile2, last_opened_profiles[1]);
1015
1016 // Simulate a shutdown.
1017 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:301018 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:461019 content::NotificationService::AllSources(),
1020 content::NotificationService::NoDetails());
1021
1022 // Even if the browsers are destructed during shutdown, the profiles stay
1023 // open.
1024 browser1.reset();
1025 browser2.reset();
1026
1027 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1028 ASSERT_EQ(2U, last_opened_profiles.size());
1029 EXPECT_EQ(profile1, last_opened_profiles[0]);
1030 EXPECT_EQ(profile2, last_opened_profiles[1]);
1031}
[email protected]41276342012-02-06 10:49:311032
1033TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
vabr8023d872016-09-15 08:12:221034 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:311035 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
vabr8023d872016-09-15 08:12:221036 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:311037 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
1038
1039 ProfileManager* profile_manager = g_browser_process->profile_manager();
1040
1041 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:061042 TestingProfile* profile1 =
1043 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:311044 ASSERT_TRUE(profile1);
1045
[email protected]41276342012-02-06 10:49:311046 std::vector<Profile*> last_opened_profiles =
1047 profile_manager->GetLastOpenedProfiles();
1048 ASSERT_EQ(0U, last_opened_profiles.size());
1049
1050 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351051 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:031052 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051053 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:311054
1055 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1056 ASSERT_EQ(1U, last_opened_profiles.size());
1057 EXPECT_EQ(profile1, last_opened_profiles[0]);
1058
1059 // And for profile2.
erg7b01d692017-02-22 21:57:351060 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
1061 true);
dcheng6e25ed32016-04-08 03:30:031062 std::unique_ptr<Browser> browser2a(
cm.sanchi8b7ec882017-11-16 07:22:051063 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:311064
1065 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1066 ASSERT_EQ(1U, last_opened_profiles.size());
1067 EXPECT_EQ(profile1, last_opened_profiles[0]);
1068
1069 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:031070 std::unique_ptr<Browser> browser2b(
cm.sanchi8b7ec882017-11-16 07:22:051071 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:311072 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1073 ASSERT_EQ(1U, last_opened_profiles.size());
1074 EXPECT_EQ(profile1, last_opened_profiles[0]);
1075
1076 // Close the browsers.
1077 browser2a.reset();
1078 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1079 ASSERT_EQ(1U, last_opened_profiles.size());
1080 EXPECT_EQ(profile1, last_opened_profiles[0]);
1081
1082 browser2b.reset();
1083 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1084 ASSERT_EQ(1U, last_opened_profiles.size());
1085 EXPECT_EQ(profile1, last_opened_profiles[0]);
1086
1087 browser1.reset();
1088 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1089 ASSERT_EQ(0U, last_opened_profiles.size());
1090}
[email protected]c849fab2012-03-29 16:51:411091#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:001092
1093#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1094// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:481095TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
vabr8023d872016-09-15 08:12:221096 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:481097 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
1098
1099 ProfileManager* profile_manager = g_browser_process->profile_manager();
1100
1101 TestingProfile* profile =
1102 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
1103 ASSERT_TRUE(profile);
palar47cf44bd2017-03-03 15:36:111104 SetProfileEphemeral(profile);
[email protected]8d9243a2013-10-21 17:11:481105
1106 // Here the last used profile is still the "Default" profile.
1107 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
1108 EXPECT_NE(profile, last_used_profile);
1109
[email protected]79fd6b72013-10-22 16:39:271110 // Create a browser for the profile.
erg7b01d692017-02-22 21:57:351111 Browser::CreateParams profile_params(profile, true);
dcheng6e25ed32016-04-08 03:30:031112 std::unique_ptr<Browser> browser(
cm.sanchi8b7ec882017-11-16 07:22:051113 CreateBrowserWithTestWindowForParams(&profile_params));
[email protected]8d9243a2013-10-21 17:11:481114 last_used_profile = profile_manager->GetLastUsedProfile();
1115 EXPECT_NE(profile, last_used_profile);
1116
1117 // Close the browser.
1118 browser.reset();
1119 last_used_profile = profile_manager->GetLastUsedProfile();
1120 EXPECT_NE(profile, last_used_profile);
1121}
1122
1123TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
vabr8023d872016-09-15 08:12:221124 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:481125 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
1126
vabr8023d872016-09-15 08:12:221127 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:271128 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
1129
vabr8023d872016-09-15 08:12:221130 base::FilePath dest_path3 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:271131 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:481132
1133 ProfileManager* profile_manager = g_browser_process->profile_manager();
1134
1135 // Successfully create the profiles.
1136 TestingProfile* normal_profile =
1137 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
1138 ASSERT_TRUE(normal_profile);
1139
1140 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:271141 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:481142 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:271143 ASSERT_TRUE(ephemeral_profile1);
palar47cf44bd2017-03-03 15:36:111144 SetProfileEphemeral(ephemeral_profile1);
[email protected]79fd6b72013-10-22 16:39:271145
1146 // Add second ephemeral profile but don't mark it as such yet.
1147 TestingProfile* ephemeral_profile2 =
1148 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
1149 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481150
1151 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351152 Browser::CreateParams profile1_params(normal_profile, true);
dcheng6e25ed32016-04-08 03:30:031153 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051154 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]8d9243a2013-10-21 17:11:481155
[email protected]79fd6b72013-10-22 16:39:271156 // Create browsers for the ephemeral profile.
erg7b01d692017-02-22 21:57:351157 Browser::CreateParams profile2_params(ephemeral_profile1, true);
dcheng6e25ed32016-04-08 03:30:031158 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:051159 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]8d9243a2013-10-21 17:11:481160
erg7b01d692017-02-22 21:57:351161 Browser::CreateParams profile3_params(ephemeral_profile2, true);
dcheng6e25ed32016-04-08 03:30:031162 std::unique_ptr<Browser> browser3(
cm.sanchi8b7ec882017-11-16 07:22:051163 CreateBrowserWithTestWindowForParams(&profile3_params));
[email protected]79fd6b72013-10-22 16:39:271164
[email protected]8d9243a2013-10-21 17:11:481165 std::vector<Profile*> last_opened_profiles =
1166 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:271167 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:481168 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:271169 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
1170
1171 // Mark the second profile ephemeral.
palar47cf44bd2017-03-03 15:36:111172 SetProfileEphemeral(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481173
1174 // Simulate a shutdown.
1175 content::NotificationService::current()->Notify(
1176 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
1177 content::NotificationService::AllSources(),
1178 content::NotificationService::NoDetails());
1179 browser1.reset();
1180 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:271181 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:481182
1183 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1184 ASSERT_EQ(1U, last_opened_profiles.size());
1185 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
1186}
1187
bauerb8b022182015-05-13 09:18:391188TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
1189 // Create two profiles, one of them ephemeral.
1190 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121191 ProfileAttributesStorage& storage =
1192 profile_manager->GetProfileAttributesStorage();
1193 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391194
1195 const std::string profile_name1 = "Homer";
lwchkg498e92492016-04-23 11:04:121196 base::FilePath path1 =
1197 profile_manager->user_data_dir().AppendASCII(profile_name1);
1198 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581199 base::UTF8ToUTF16(profile_name1), 0, std::string(),
1200 EmptyAccountId());
Lei Zhangbd15e1c92018-07-03 08:53:481201 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391202 ASSERT_TRUE(base::CreateDirectory(path1));
1203
1204 const std::string profile_name2 = "Marge";
lwchkg498e92492016-04-23 11:04:121205 base::FilePath path2 =
1206 profile_manager->user_data_dir().AppendASCII(profile_name2);
1207 storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581208 base::UTF8ToUTF16(profile_name2), 0, std::string(),
1209 EmptyAccountId());
lwchkg498e92492016-04-23 11:04:121210 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391211 ASSERT_TRUE(base::CreateDirectory(path2));
1212
1213 // Set the active profile.
1214 PrefService* local_state = g_browser_process->local_state();
1215 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1216
1217 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081218 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391219
1220 // The ephemeral profile should be deleted, and the last used profile set to
1221 // the other one.
1222 EXPECT_FALSE(base::DirectoryExists(path1));
1223 EXPECT_TRUE(base::DirectoryExists(path2));
1224 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121225 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391226
1227 // Mark the remaining profile ephemeral and clean up.
Lei Zhangbd15e1c92018-07-03 08:53:481228 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391229 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081230 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391231
1232 // The profile should be deleted, and the last used profile set to a new one.
1233 EXPECT_FALSE(base::DirectoryExists(path2));
lwchkg498e92492016-04-23 11:04:121234 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391235 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1236}
1237
Owen Minb0528082018-03-22 16:15:471238TEST_F(ProfileManagerTest, CleanUpEphemeralProfilesWithGuestLastUsedProfile) {
1239 ProfileManager* profile_manager = g_browser_process->profile_manager();
1240 ProfileAttributesStorage& storage =
1241 profile_manager->GetProfileAttributesStorage();
1242 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
1243
1244 const std::string profile_name1 = "Homer";
1245 base::FilePath path1 =
1246 profile_manager->user_data_dir().AppendASCII(profile_name1);
1247 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581248 base::UTF8ToUTF16(profile_name1), 0, std::string(),
1249 EmptyAccountId());
Lei Zhangbd15e1c92018-07-03 08:53:481250 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
Owen Minb0528082018-03-22 16:15:471251 ASSERT_TRUE(base::CreateDirectory(path1));
1252 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1253
1254 // Set the active profile.
1255 PrefService* local_state = g_browser_process->local_state();
1256 local_state->SetString(prefs::kProfileLastUsed, std::string("Guest Profile"));
1257
1258 profile_manager->CleanUpEphemeralProfiles();
1259 content::RunAllTasksUntilIdle();
1260
1261 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
1262 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1263}
1264
[email protected]1c62b2f2013-06-28 00:15:001265TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
1266 ProfileManager* profile_manager = g_browser_process->profile_manager();
1267 ASSERT_TRUE(profile_manager);
1268
1269 // Create and load two profiles.
1270 const std::string profile_name1 = "New Profile 1";
1271 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221272 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1273 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001274
1275 MockObserver mock_observer;
1276 EXPECT_CALL(mock_observer, OnProfileCreated(
1277 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
1278
Marc Treib92127162018-08-27 14:20:031279 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
1280 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081281 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001282
1283 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121284 EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage().
1285 GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001286
1287 // Set the active profile.
1288 PrefService* local_state = g_browser_process->local_state();
1289 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1290
1291 // Delete the active profile.
David Roger51797642018-03-20 13:29:161292 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081293 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001294
1295 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1296 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1297}
[email protected]3f130532014-02-26 20:39:241298
[email protected]602b5a29f2014-07-18 15:39:331299TEST_F(ProfileManagerTest, LastProfileDeleted) {
1300 ProfileManager* profile_manager = g_browser_process->profile_manager();
1301 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121302 ProfileAttributesStorage& storage =
1303 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331304
1305 // Create and load a profile.
1306 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221307 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331308
1309 MockObserver mock_observer;
1310 EXPECT_CALL(mock_observer, OnProfileCreated(
1311 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
1312
Marc Treib92127162018-08-27 14:20:031313 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081314 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331315
1316 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121317 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331318
1319 // Set it as the active profile.
1320 PrefService* local_state = g_browser_process->local_state();
1321 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1322
1323 // Delete the active profile.
David Roger51797642018-03-20 13:29:161324 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081325 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331326
1327 // A new profile should have been created
1328 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221329 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331330
1331 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1332 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
Lei Zhangbd15e1c92018-07-03 08:53:481333 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1334 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331335}
1336
1337TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1338 ProfileManager* profile_manager = g_browser_process->profile_manager();
1339 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121340 ProfileAttributesStorage& storage =
1341 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331342
1343 // Create and load a profile.
1344 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221345 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331346
1347 MockObserver mock_observer;
1348 EXPECT_CALL(mock_observer, OnProfileCreated(
1349 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1350
Marc Treib92127162018-08-27 14:20:031351 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081352 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331353
1354 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121355 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331356
1357 // Create the profile and register it.
1358 const std::string guest_profile_name =
1359 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1360
1361 TestingProfile::Builder builder;
1362 builder.SetGuestSession();
1363 builder.SetPath(ProfileManager::GetGuestProfilePath());
1364 TestingProfile* guest_profile = builder.Build().release();
1365 guest_profile->set_profile_name(guest_profile_name);
1366 // Registering the profile passes ownership to the ProfileManager.
1367 profile_manager->RegisterTestingProfile(guest_profile, false, false);
1368
lwchkg498e92492016-04-23 11:04:121369 // The Guest profile does not get added to the ProfileAttributesStorage.
[email protected]602b5a29f2014-07-18 15:39:331370 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121371 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331372
1373 // Set the Guest profile as the active profile.
1374 PrefService* local_state = g_browser_process->local_state();
1375 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1376
1377 // Delete the other profile.
David Roger51797642018-03-20 13:29:161378 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081379 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331380
1381 // A new profile should have been created.
1382 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221383 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331384
1385 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
Lei Zhangbd15e1c92018-07-03 08:53:481386 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1387 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331388}
1389
[email protected]3f130532014-02-26 20:39:241390TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1391 if (!profiles::IsMultipleProfilesEnabled())
1392 return;
1393
[email protected]3f130532014-02-26 20:39:241394 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121395 ProfileAttributesStorage& storage =
1396 profile_manager->GetProfileAttributesStorage();
1397 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241398
1399 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1400 const base::string16 default_profile_name =
1401 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121402 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1403 Profile* profile1 = AddProfileToStorage(profile_manager,
1404 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441405 EXPECT_EQ(default_profile_name,
1406 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241407
1408 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121409 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1410 Profile* profile2 = AddProfileToStorage(profile_manager,
1411 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441412 EXPECT_EQ(profile_name1,
1413 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1414 EXPECT_EQ(profile_name2,
1415 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241416
1417 // Deleting a profile means returning to the default name.
1418 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161419 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081420 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441421 EXPECT_EQ(default_profile_name,
1422 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241423}
1424
1425TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1426 if (!profiles::IsMultipleProfilesEnabled())
1427 return;
1428
[email protected]3f130532014-02-26 20:39:241429 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121430 ProfileAttributesStorage& storage =
1431 profile_manager->GetProfileAttributesStorage();
1432 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241433
1434 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1435 const base::string16 default_profile_name =
1436 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121437 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1438 Profile* profile1 = AddProfileToStorage(profile_manager,
1439 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441440 EXPECT_EQ(default_profile_name,
1441 profiles::GetAvatarNameForProfile(profile1->GetPath()));
lwchkg498e92492016-04-23 11:04:121442 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241443
1444 // We should display custom names for local profiles.
1445 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
Lei Zhangbd15e1c92018-07-03 08:53:481446 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0];
lwchkg498e92492016-04-23 11:04:121447 entry->SetName(custom_profile_name);
1448 entry->SetIsUsingDefaultName(false);
1449 EXPECT_EQ(custom_profile_name, entry->GetName());
[email protected]119809d2014-06-27 22:12:441450 EXPECT_EQ(custom_profile_name,
1451 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241452
1453 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121454 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1455 Profile* profile2 = AddProfileToStorage(profile_manager,
1456 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441457 EXPECT_EQ(custom_profile_name,
1458 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1459 EXPECT_EQ(profile_name2,
1460 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241461
1462 // Deleting a profile means returning to the original, custom name.
1463 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161464 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081465 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441466 EXPECT_EQ(custom_profile_name,
1467 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241468}
1469
1470TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1471 if (!profiles::IsMultipleProfilesEnabled())
1472 return;
1473
[email protected]3f130532014-02-26 20:39:241474 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121475 ProfileAttributesStorage& storage =
1476 profile_manager->GetProfileAttributesStorage();
1477 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241478
1479 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1480 const base::string16 default_profile_name =
1481 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121482 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1483 Profile* profile1 = AddProfileToStorage(profile_manager,
1484 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441485 EXPECT_EQ(default_profile_name,
1486 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241487
Lei Zhangbd15e1c92018-07-03 08:53:481488 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1489 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0];
[email protected]78186a42014-08-22 22:07:301490 // For a signed in profile with a default name we still display
1491 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
lwchkg498e92492016-04-23 11:04:121492 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
1493 EXPECT_EQ(profile_name1, entry->GetName());
[email protected]78186a42014-08-22 22:07:301494 EXPECT_EQ(default_profile_name,
[email protected]119809d2014-06-27 22:12:441495 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241496
[email protected]78186a42014-08-22 22:07:301497 // For a signed in profile with a non-default Gaia given name we display the
1498 // Gaia given name.
lwchkg498e92492016-04-23 11:04:121499 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
[email protected]78186a42014-08-22 22:07:301500 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
lwchkg498e92492016-04-23 11:04:121501 entry->SetGAIAGivenName(gaia_given_name);
1502 EXPECT_EQ(gaia_given_name, entry->GetName());
[email protected]78186a42014-08-22 22:07:301503 EXPECT_EQ(gaia_given_name,
1504 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1505
[email protected]3f130532014-02-26 20:39:241506 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121507 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1508 Profile* profile2 = AddProfileToStorage(profile_manager,
1509 "path_2", profile_name2);
[email protected]78186a42014-08-22 22:07:301510 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441511 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1512 EXPECT_EQ(profile_name2,
1513 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241514
1515 // Deleting a profile means returning to the original, actual profile name.
1516 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161517 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081518 content::RunAllTasksUntilIdle();
[email protected]78186a42014-08-22 22:07:301519 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441520 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241521}
nomsc9299462014-09-16 22:55:281522
1523TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1524 if (!profiles::IsMultipleProfilesEnabled())
1525 return;
1526
nomsc9299462014-09-16 22:55:281527 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121528 ProfileAttributesStorage& storage =
1529 profile_manager->GetProfileAttributesStorage();
1530 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281531
1532 // Create two signed in profiles, with both new and legacy default names, and
1533 // a profile with a custom name.
lwchkg498e92492016-04-23 11:04:121534 Profile* profile1 = AddProfileToStorage(profile_manager, "path_1",
1535 ASCIIToUTF16("Person 1"));
1536 Profile* profile2 = AddProfileToStorage(profile_manager, "path_2",
1537 ASCIIToUTF16("Default Profile"));
nomsc9299462014-09-16 22:55:281538 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
lwchkg498e92492016-04-23 11:04:121539 Profile* profile3 = AddProfileToStorage(profile_manager, "path_3",
1540 profile_name3);
1541 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281542
1543 // Sign in all profiles, and make sure they do not have a Gaia name set.
1544 const base::string16 email1(ASCIIToUTF16("[email protected]"));
1545 const base::string16 email2(ASCIIToUTF16("[email protected]"));
1546 const base::string16 email3(ASCIIToUTF16("[email protected]"));
1547
lwchkg498e92492016-04-23 11:04:121548 ProfileAttributesEntry* entry;
1549
1550 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1551 &entry));
1552 entry->SetAuthInfo("12345", email1);
1553 entry->SetGAIAGivenName(base::string16());
1554 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281555
1556 // This may resort the cache, so be extra cautious to use the right profile.
lwchkg498e92492016-04-23 11:04:121557 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(),
1558 &entry));
1559 entry->SetAuthInfo("23456", email2);
1560 entry->SetGAIAGivenName(base::string16());
1561 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281562
lwchkg498e92492016-04-23 11:04:121563 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(),
1564 &entry));
1565 entry->SetAuthInfo("34567", email3);
1566 entry->SetGAIAGivenName(base::string16());
1567 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281568
1569 // The profiles with default names should display the email address.
1570 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1571 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1572
1573 // The profile with the custom name should display that.
1574 EXPECT_EQ(profile_name3,
1575 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1576
1577 // Adding a Gaia name to a profile that previously had a default name should
1578 // start displaying it.
1579 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
lwchkg498e92492016-04-23 11:04:121580 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1581 &entry));
1582 entry->SetGAIAGivenName(gaia_given_name);
nomsc9299462014-09-16 22:55:281583 EXPECT_EQ(gaia_given_name,
1584 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1585}
[email protected]1c62b2f2013-06-28 00:15:001586#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1587
1588#if defined(OS_MACOSX)
1589// These tests are for a Mac-only code path that assumes the browser
1590// process isn't killed when all browser windows are closed.
1591TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1592 ProfileManager* profile_manager = g_browser_process->profile_manager();
1593 ASSERT_TRUE(profile_manager);
1594
1595 // Create and load one profile, and just create a second profile.
1596 const std::string profile_name1 = "New Profile 1";
1597 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221598 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1599 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001600
1601 MockObserver mock_observer;
1602 EXPECT_CALL(mock_observer, OnProfileCreated(
1603 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
Marc Treib92127162018-08-27 14:20:031604 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081605 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001606
1607 // Track the profile, but don't load it.
lwchkg498e92492016-04-23 11:04:121608 ProfileAttributesStorage& storage =
1609 profile_manager->GetProfileAttributesStorage();
1610 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
Roman Sorokin8ead4d72018-04-16 15:58:581611 base::string16(), 0, std::string(), EmptyAccountId());
Gabriel Charette01507a22017-09-27 21:30:081612 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001613
1614 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121615 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001616
1617 // Set the active profile.
1618 PrefService* local_state = g_browser_process->local_state();
1619 local_state->SetString(prefs::kProfileLastUsed,
1620 dest_path1.BaseName().MaybeAsASCII());
1621
1622 // Delete the active profile. This should switch and load the unloaded
1623 // profile.
David Roger51797642018-03-20 13:29:161624 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
[email protected]1c62b2f2013-06-28 00:15:001625
Gabriel Charette01507a22017-09-27 21:30:081626 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001627
1628 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1629 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1630}
1631
1632// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1633// by simulating a scenario in which the profile that is being loaded as
1634// the next active profile has also been marked for deletion, so the
1635// ProfileManager needs to recursively select a different next profile.
1636TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1637 ProfileManager* profile_manager = g_browser_process->profile_manager();
1638 ASSERT_TRUE(profile_manager);
1639
1640 // Create and load one profile, and create two more profiles.
1641 const std::string profile_name1 = "New Profile 1";
1642 const std::string profile_name2 = "New Profile 2";
1643 const std::string profile_name3 = "New Profile 3";
vabr8023d872016-09-15 08:12:221644 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1645 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
1646 base::FilePath dest_path3 = temp_dir_.GetPath().AppendASCII(profile_name3);
[email protected]1c62b2f2013-06-28 00:15:001647
1648 MockObserver mock_observer;
1649 EXPECT_CALL(mock_observer, OnProfileCreated(
1650 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
Marc Treib92127162018-08-27 14:20:031651 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081652 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001653
1654 // Create the other profiles, but don't load them. Assign a fake avatar icon
lwchkg498e92492016-04-23 11:04:121655 // to ensure that profiles in the profile attributes storage are sorted by the
1656 // profile name, and not randomly by the avatar name.
1657 ProfileAttributesStorage& storage =
1658 profile_manager->GetProfileAttributesStorage();
1659 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
Roman Sorokin8ead4d72018-04-16 15:58:581660 ASCIIToUTF16(profile_name2), 1, std::string(),
1661 EmptyAccountId());
lwchkg498e92492016-04-23 11:04:121662 storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567",
Roman Sorokin8ead4d72018-04-16 15:58:581663 ASCIIToUTF16(profile_name3), 2, std::string(),
1664 EmptyAccountId());
[email protected]1c62b2f2013-06-28 00:15:001665
Gabriel Charette01507a22017-09-27 21:30:081666 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001667
1668 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121669 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001670
1671 // Set the active profile.
1672 PrefService* local_state = g_browser_process->local_state();
1673 local_state->SetString(prefs::kProfileLastUsed,
1674 dest_path1.BaseName().MaybeAsASCII());
1675
1676 // Delete the active profile, Profile1.
1677 // This will post a CreateProfileAsync message, that tries to load Profile2,
1678 // which checks that the profile is not being deleted, and then calls back
1679 // FinishDeletingProfile for Profile1.
1680 // Try to break this flow by setting the active profile to Profile2 in the
1681 // middle (so after the first posted message), and trying to delete Profile2,
1682 // so that the ProfileManager has to look for a different profile to load.
David Roger51797642018-03-20 13:29:161683 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
[email protected]1c62b2f2013-06-28 00:15:001684 local_state->SetString(prefs::kProfileLastUsed,
1685 dest_path2.BaseName().MaybeAsASCII());
David Roger51797642018-03-20 13:29:161686 profile_manager->ScheduleProfileForDeletion(dest_path2, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081687 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001688
1689 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1690 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1691}
Lei Zhangbd15e1c92018-07-03 08:53:481692#endif // defined(OS_MACOSX)