blob: 44f2b9786e04705acd34c16824c6bdb322ce1ec1 [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>
Daniel Hosseinian8f0b7482019-06-07 18:21:048#include <utility>
[email protected]d99bcaa2010-12-05 20:15:569
Sebastien Marchandf1349f52019-01-25 03:16:4110#include "base/bind.h"
David Roger51797642018-03-20 13:29:1611#include "base/bind_helpers.h"
[email protected]943aed5542010-06-11 15:57:0812#include "base/command_line.h"
miguelg1b495542016-03-18 17:57:4613#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0414#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2315#include "base/files/scoped_temp_dir.h"
avib896c712015-12-26 02:10:4316#include "base/macros.h"
vabr8684c9a2017-03-29 13:14:5717#include "base/memory/ptr_util.h"
[email protected]37ac95b2013-07-23 23:39:3518#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0819#include "base/strings/utf_string_conversions.h"
Gabriel Charette0b08bf22017-08-15 20:10:0620#include "base/threading/thread_task_runner_handle.h"
[email protected]29d70252011-04-28 02:16:5821#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3222#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4923#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3524#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0325#include "chrome/browser/chrome_notification_types.h"
[email protected]9718a722012-06-19 20:10:5326#include "chrome/browser/history/history_service_factory.h"
[email protected]81ad7f4a2011-03-16 01:33:2927#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5928#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3329#include "chrome/browser/profiles/profile.h"
[email protected]c3e559772014-04-09 04:02:5430#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]279170832011-10-12 23:38:0331#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3332#include "chrome/browser/profiles/profile_manager.h"
[email protected]3f130532014-02-26 20:39:2433#include "chrome/browser/profiles/profiles_state.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]583844c2011-08-27 00:38:3540#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1741#include "chrome/test/base/testing_profile.h"
Alexander Alekseev3f09a8f2018-05-03 02:52:1042#include "components/account_id/account_id.h"
sdefresne506dd522015-03-12 18:49:1243#include "components/history/core/browser/history_service.h"
Colin Blundellac55c9972018-11-07 15:30:3344#include "components/signin/core/browser/account_consistency_method.h"
[email protected]ad50def52011-10-19 23:17:0745#include "content/public/browser/notification_service.h"
[email protected]f9357a442014-05-15 18:44:0746#include "content/public/common/content_switches.h"
[email protected]37ac95b2013-07-23 23:39:3547#include "content/public/test/test_browser_thread_bundle.h"
Gabriel Charettefaf432192017-08-17 22:00:4148#include "content/public/test/test_utils.h"
[email protected]844a1002011-04-19 11:37:2149#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2950#include "testing/gtest/include/gtest/gtest.h"
[email protected]3f130532014-02-26 20:39:2451#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2952
Collin Baker8a217552019-05-29 19:47:5153#if !defined(OS_ANDROID)
54#include "chrome/browser/ui/browser.h"
55#include "chrome/test/base/test_browser_window.h"
56#endif
57
[email protected]775caec2011-09-01 17:14:3258#if defined(OS_CHROMEOS)
merkulova793f3022015-02-04 10:18:3059#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
[email protected]4d390782014-08-15 09:22:5860#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
dzhioev0298d722014-08-26 13:18:5261#include "chrome/browser/chromeos/profiles/profile_helper.h"
A Olsenc63621c2018-08-13 15:32:1762#include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
Wenzhao Zang8cf7ebd2017-12-22 02:37:1963#include "chrome/browser/ui/ash/test_wallpaper_controller.h"
64#include "chrome/browser/ui/ash/wallpaper_controller_client.h"
Steven Bennetts38e9bde22019-01-03 17:31:1065#include "chromeos/constants/chromeos_switches.h"
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:4766#include "components/arc/arc_prefs.h"
Yusuke Satod41a2c92019-03-26 00:55:1467#include "components/arc/session/arc_supervision_transition.h"
Xiyuan Xiadfe3a9f2017-11-13 21:46:2668#include "components/user_manager/scoped_user_manager.h"
[email protected]4d390782014-08-15 09:22:5869#include "components/user_manager/user_manager.h"
alemate3c1c0882016-10-27 19:05:1270#include "components/user_manager/user_names.h"
Andrew Wilsonc6c2c37e2017-09-01 14:44:2771#include "extensions/common/features/feature_session_type.h"
dzhioev0298d722014-08-26 13:18:5272#endif // defined(OS_CHROMEOS)
[email protected]775caec2011-09-01 17:14:3273
Jochen Eisingerd84bb42a2018-07-23 22:39:0374#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
75#include "chrome/browser/supervised_user/supervised_user_constants.h"
76#endif
77
[email protected]6778fed2013-12-24 20:09:3778using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3979
[email protected]844a1002011-04-19 11:37:2180namespace {
[email protected]f4e706252012-01-10 17:11:3681
[email protected]844a1002011-04-19 11:37:2182// This global variable is used to check that value returned to different
83// observers is the same.
Lei Zhangbd15e1c92018-07-03 08:53:4884Profile* g_created_profile = nullptr;
[email protected]844a1002011-04-19 11:37:2185
Lei Zhangbd15e1c92018-07-03 08:53:4886class UnittestProfileManager : public ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1787 public:
[email protected]9819fd02013-08-22 10:49:3988 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
Lei Zhangbd15e1c92018-07-03 08:53:4889 : ProfileManagerWithoutInit(user_data_dir) {}
90 ~UnittestProfileManager() override = default;
[email protected]537c1082011-12-02 02:37:1791
92 protected:
dcheng1fd716382014-10-22 20:14:4193 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]7567484142013-07-11 17:36:0794 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5495 if (!base::CreateDirectory(file_path))
Lei Zhangbd15e1c92018-07-03 08:53:4896 return nullptr;
[email protected]537c1082011-12-02 02:37:1797 }
Lei Zhangbd15e1c92018-07-03 08:53:4898 return new TestingProfile(file_path, nullptr);
[email protected]537c1082011-12-02 02:37:1799 }
100
Daniel Hosseinian8f0b7482019-06-07 18:21:04101 std::unique_ptr<Profile> CreateProfileAsyncHelper(
102 const base::FilePath& path,
103 Delegate* delegate) override {
Gabriel Charette0b08bf22017-08-15 20:10:06104 // ThreadTaskRunnerHandle::Get() is TestingProfile's "async" IOTaskRunner
105 // (ref. TestingProfile::GetIOTaskRunner()).
106 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE,
tzik6e4834a2017-04-20 15:42:58108 base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:17109
Daniel Hosseinian8f0b7482019-06-07 18:21:04110 return std::make_unique<TestingProfile>(path, this);
[email protected]537c1082011-12-02 02:37:17111 }
112};
113
miguelg1b495542016-03-18 17:57:46114void ExpectNullProfile(base::Closure closure, Profile* profile) {
115 EXPECT_EQ(nullptr, profile);
116 closure.Run();
117}
118
119void ExpectProfileWithName(const std::string& profile_name,
120 bool incognito,
121 base::Closure closure,
122 Profile* profile) {
123 EXPECT_NE(nullptr, profile);
124 EXPECT_EQ(incognito, profile->IsOffTheRecord());
125 if (incognito)
126 profile = profile->GetOriginalProfile();
127
128 // Create a profile on the fly so the the same comparison
129 // can be used in Windows and other platforms.
130 EXPECT_EQ(base::FilePath().AppendASCII(profile_name),
131 profile->GetPath().BaseName());
132 closure.Run();
133}
134
[email protected]9819fd02013-08-22 10:49:39135} // namespace
[email protected]537c1082011-12-02 02:37:17136
[email protected]583844c2011-08-27 00:38:35137class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:47138 protected:
[email protected]f4e706252012-01-10 17:11:36139 class MockObserver {
140 public:
141 MOCK_METHOD2(OnProfileCreated,
142 void(Profile* profile, Profile::CreateStatus status));
143 };
144
[email protected]6cad5bf2011-03-10 21:21:55145 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:35146 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:09147 }
148
dchenge1bc7982014-10-30 00:32:40149 void SetUp() override {
[email protected]3a305db2011-04-12 13:40:53150 // Create a new temporary directory, and store the path
151 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22152 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22153 new UnittestProfileManager(temp_dir_.GetPath()));
[email protected]f89b50f2012-08-16 21:36:50154
[email protected]537c1082011-12-02 02:37:17155#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43156 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
dzhioev0298d722014-08-26 13:18:52157 cl->AppendSwitch(switches::kTestType);
Wenzhao Zang8cf7ebd2017-12-22 02:37:19158 wallpaper_controller_client_ =
159 std::make_unique<WallpaperControllerClient>();
Mike Wassermana5352da2019-05-30 17:26:08160 wallpaper_controller_client_->InitForTesting(&test_wallpaper_controller_);
Andrew Wilsonc6c2c37e2017-09-01 14:44:27161
162 // Have to manually reset the session type in between test runs because
163 // some tests log in users.
164 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
165 extensions::GetCurrentFeatureSessionType());
166 session_type_ = extensions::ScopedCurrentFeatureSessionType(
167 extensions::GetCurrentFeatureSessionType());
[email protected]537c1082011-12-02 02:37:17168#endif
initial.commit09911bf2008-07-26 23:55:29169 }
[email protected]81ad7f4a2011-03-16 01:33:29170
dchenge1bc7982014-10-30 00:32:40171 void TearDown() override {
Lei Zhangbd15e1c92018-07-03 08:53:48172 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
Gabriel Charette01507a22017-09-27 21:30:08173 content::RunAllTasksUntilIdle();
xdaid05f51c2015-03-23 17:21:33174#if defined(OS_CHROMEOS)
Andrew Wilsonc6c2c37e2017-09-01 14:44:27175 session_type_.reset();
Wenzhao Zang8cf7ebd2017-12-22 02:37:19176 wallpaper_controller_client_.reset();
xdaid05f51c2015-03-23 17:21:33177#endif
initial.commit09911bf2008-07-26 23:55:29178 }
[email protected]f0a51fb52009-03-05 12:46:38179
[email protected]1c62b2f2013-06-28 00:15:00180 // Helper function to create a profile with |name| for a profile |manager|.
181 void CreateProfileAsync(ProfileManager* manager,
182 const std::string& name,
[email protected]1c62b2f2013-06-28 00:15:00183 MockObserver* mock_observer) {
vabr8023d872016-09-15 08:12:22184 manager->CreateProfileAsync(temp_dir_.GetPath().AppendASCII(name),
185 base::Bind(&MockObserver::OnProfileCreated,
186 base::Unretained(mock_observer)),
187 base::UTF8ToUTF16(name),
Marc Treib92127162018-08-27 14:20:03188 profiles::GetDefaultAvatarIconUrl(0));
[email protected]1c62b2f2013-06-28 00:15:00189 }
190
lwchkg498e92492016-04-23 11:04:12191 // Helper function to add a profile with |profile_name| to |profile_manager|'s
192 // ProfileAttributesStorage, and return the profile created.
193 Profile* AddProfileToStorage(ProfileManager* profile_manager,
194 const std::string& path_suffix,
195 const base::string16& profile_name) {
196 ProfileAttributesStorage& storage =
197 profile_manager->GetProfileAttributesStorage();
198 size_t num_profiles = storage.GetNumberOfProfiles();
vabr8023d872016-09-15 08:12:22199 base::FilePath path = temp_dir_.GetPath().AppendASCII(path_suffix);
lwchkg498e92492016-04-23 11:04:12200 storage.AddProfile(path, profile_name, std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58201 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12202 EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:24203 return profile_manager->GetProfile(path);
204 }
205
palar47cf44bd2017-03-03 15:36:11206 // Helper function to set profile ephemeral at prefs and attributes storage.
207 void SetProfileEphemeral(Profile* profile) {
208 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
209
210 // Update IsEphemeral in attributes storage, normally it happened via
211 // kForceEphemeralProfiles pref change event routed to
212 // ProfileImpl::UpdateIsEphemeralInStorage().
213 ProfileAttributesEntry* entry;
214 ProfileAttributesStorage& storage =
215 g_browser_process->profile_manager()->GetProfileAttributesStorage();
216 EXPECT_TRUE(
217 storage.GetProfileAttributesWithPath(profile->GetPath(), &entry));
218 entry->SetIsEphemeral(true);
219 }
220
[email protected]6f96cbcb2011-11-04 02:26:07221#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52222 // Helper function to register an user with id |user_id| and create profile
223 // with a correct path.
Alexander Alekseev17513602017-12-05 03:16:36224 void RegisterUser(const AccountId& account_id) {
dzhioev0298d722014-08-26 13:18:52225 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
226 const std::string user_id_hash =
Alexander Alekseev17513602017-12-05 03:16:36227 profile_helper->GetUserIdHashByUserIdForTesting(
228 account_id.GetUserEmail());
229 user_manager::UserManager::Get()->UserLoggedIn(account_id, user_id_hash,
Alexander Alekseev2a5efd62017-12-06 07:27:28230 false /* browser_restart */,
231 false /* is_child */);
dzhioev0298d722014-08-26 13:18:52232 g_browser_process->profile_manager()->GetProfile(
233 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
234 }
235
A Olsenc63621c2018-08-13 15:32:17236 chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
[email protected]6f96cbcb2011-11-04 02:26:07237#endif
238
Lei Zhangbd15e1c92018-07-03 08:53:48239 content::TestBrowserThreadBundle thread_bundle_;
240
[email protected]3a305db2011-04-12 13:40:53241 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23242 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35243 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35244
[email protected]363f5272013-04-23 17:21:42245#if defined(OS_CHROMEOS)
246 chromeos::ScopedTestUserManager test_user_manager_;
Andrew Wilsonc6c2c37e2017-09-01 14:44:27247 std::unique_ptr<base::AutoReset<extensions::FeatureSessionType>>
248 session_type_;
Wenzhao Zang8cf7ebd2017-12-22 02:37:19249 std::unique_ptr<WallpaperControllerClient> wallpaper_controller_client_;
250 TestWallpaperController test_wallpaper_controller_;
[email protected]363f5272013-04-23 17:21:42251#endif
dzhioev0298d722014-08-26 13:18:52252
253 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
initial.commit09911bf2008-07-26 23:55:29254};
255
[email protected]844a1002011-04-19 11:37:21256TEST_F(ProfileManagerTest, GetProfile) {
vabr8023d872016-09-15 08:12:22257 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]f7011fcb2009-01-28 21:54:32258 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29259
[email protected]881cb0b62011-12-13 20:39:54260 ProfileManager* profile_manager = g_browser_process->profile_manager();
261
initial.commit09911bf2008-07-26 23:55:29262 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36263 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21264 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29265
[email protected]844a1002011-04-19 11:37:21266 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54267 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29268}
[email protected]55474b572009-04-14 22:05:33269
[email protected]943aed5542010-06-11 15:57:08270TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45271 base::FilePath expected_default =
272 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54273 EXPECT_EQ(
274 expected_default.value(),
275 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08276}
277
skuhne24bb24432014-09-08 19:31:47278MATCHER(NotFail, "Profile creation failure status is not reported.") {
279 return arg == Profile::CREATE_STATUS_CREATED ||
280 arg == Profile::CREATE_STATUS_INITIALIZED;
281}
282
283MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
284 if (!g_created_profile)
285 g_created_profile = arg;
Lei Zhangbd15e1c92018-07-03 08:53:48286 return arg && arg == g_created_profile;
skuhne24bb24432014-09-08 19:31:47287}
288
[email protected]943aed5542010-06-11 15:57:08289#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52290
[email protected]943aed5542010-06-11 15:57:08291// This functionality only exists on Chrome OS.
292TEST_F(ProfileManagerTest, LoggedInProfileDir) {
[email protected]650b2d52013-02-10 03:41:45293 base::FilePath expected_default =
294 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54295 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08296 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54297 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08298
Alexander Alekseev17513602017-12-05 03:16:36299 constexpr char kTestUserName[] = "[email protected]";
300 constexpr char kTestUserGaiaId[] = "0123456789";
301 const AccountId test_account_id(
302 AccountId::FromUserEmailGaiaId(kTestUserName, kTestUserGaiaId));
merkulova793f3022015-02-04 10:18:30303 chromeos::FakeChromeUserManager* user_manager =
304 new chromeos::FakeChromeUserManager();
Xiyuan Xiadfe3a9f2017-11-13 21:46:26305 user_manager::ScopedUserManager enabler(base::WrapUnique(user_manager));
dzhioev0298d722014-08-26 13:18:52306
alemate3ffbde6f2015-11-03 02:02:55307 const user_manager::User* active_user =
308 user_manager->AddUser(test_account_id);
309 user_manager->LoginUser(test_account_id);
310 user_manager->SwitchActiveUser(test_account_id);
dzhioev0298d722014-08-26 13:18:52311
[email protected]2fda9972014-07-23 14:51:59312 profile_manager->Observe(
313 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
314 content::NotificationService::AllSources(),
315 content::Details<const user_manager::User>(active_user));
dzhioev0298d722014-08-26 13:18:52316 base::FilePath expected_logged_in(
317 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
[email protected]943aed5542010-06-11 15:57:08318 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54319 profile_manager->GetInitialProfileDir().value());
vabr8023d872016-09-15 08:12:22320 VLOG(1) << temp_dir_.GetPath()
321 .Append(profile_manager->GetInitialProfileDir())
322 .value();
[email protected]943aed5542010-06-11 15:57:08323}
324
xiyuan41d32a32017-06-12 20:06:13325// Test Get[ActiveUser|PrimaryUser|LastUsed]Profile does not load user profile.
326TEST_F(ProfileManagerTest, UserProfileLoading) {
327 using chromeos::ProfileHelper;
328
329 Profile* const signin_profile = ProfileHelper::GetSigninProfile();
330
331 // Get[Active|Primary|LastUsed]Profile return the sign-in profile before login
332 // happens. IsSameProfile() is used to properly test against TestProfile whose
333 // OTR version uses a different temp path.
334 EXPECT_TRUE(
335 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
336 EXPECT_TRUE(
337 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
338 EXPECT_TRUE(
339 ProfileManager::GetLastUsedProfile()->IsSameProfile(signin_profile));
340
341 // User signs in but user profile loading has not started.
342 const std::string user_id = "[email protected]";
Alexander Alekseev17513602017-12-05 03:16:36343 const std::string gaia_id = "0123456789";
xiyuan41d32a32017-06-12 20:06:13344 const std::string user_id_hash =
345 ProfileHelper::Get()->GetUserIdHashByUserIdForTesting(user_id);
346 user_manager::UserManager::Get()->UserLoggedIn(
Alexander Alekseev2a5efd62017-12-06 07:27:28347 AccountId::FromUserEmailGaiaId(user_id, gaia_id), user_id_hash,
348 false /* browser_restart */, false /* is_child */);
xiyuan41d32a32017-06-12 20:06:13349
350 // Sign-in profile should be returned at this stage. Otherwise, login code
351 // ends up in an invalid state. Strange things as in http://crbug.com/728683
352 // and http://crbug.com/718734 happens.
353 EXPECT_TRUE(
354 ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile));
355 EXPECT_TRUE(
356 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile));
xiyuan8c6a4112017-06-19 21:03:49357
358 // GetLastUsedProfile() after login but before a user profile is loaded is
359 // fatal.
360 EXPECT_DEATH_IF_SUPPORTED(ProfileManager::GetLastUsedProfile(), ".*");
xiyuan41d32a32017-06-12 20:06:13361
362 // Simulate UserSessionManager loads the profile.
363 Profile* const user_profile =
364 g_browser_process->profile_manager()->GetProfile(
365 ProfileHelper::Get()->GetProfilePathByUserIdHash(user_id_hash));
366 ASSERT_FALSE(user_profile->IsSameProfile(signin_profile));
367
368 // User profile is returned thereafter.
369 EXPECT_TRUE(
370 ProfileManager::GetActiveUserProfile()->IsSameProfile(user_profile));
371 EXPECT_TRUE(
372 ProfileManager::GetPrimaryUserProfile()->IsSameProfile(user_profile));
373 EXPECT_TRUE(
374 ProfileManager::GetLastUsedProfile()->IsSameProfile(user_profile));
375}
376
Lei Zhangbd15e1c92018-07-03 08:53:48377#endif // defined(OS_CHROMEOS)
[email protected]943aed5542010-06-11 15:57:08378
Scott Little4087e1c2017-11-28 19:56:59379// Data race on Linux bots. http://crbug.com/789214
380#if defined(OS_LINUX)
381#define MAYBE_CreateAndUseTwoProfiles DISABLED_CreateAndUseTwoProfiles
382#else
383#define MAYBE_CreateAndUseTwoProfiles CreateAndUseTwoProfiles
384#endif
385
386TEST_F(ProfileManagerTest, MAYBE_CreateAndUseTwoProfiles) {
vabr8023d872016-09-15 08:12:22387 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33388 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47389
vabr8023d872016-09-15 08:12:22390 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]55474b572009-04-14 22:05:33391 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
392
[email protected]881cb0b62011-12-13 20:39:54393 ProfileManager* profile_manager = g_browser_process->profile_manager();
394
[email protected]55474b572009-04-14 22:05:33395 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17396 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54397 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21398 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33399
[email protected]537c1082011-12-02 02:37:17400 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54401 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21402 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33403
404 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31405 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04406 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
407 profile1, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17408 profile1->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30409 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile1));
[email protected]537c1082011-12-02 02:37:17410 profile2->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30411 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile2));
[email protected]608e7e02013-07-24 12:23:31412 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04413 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
414 profile2, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55415
416 // Make sure any pending tasks run before we destroy the profiles.
Gabriel Charette01507a22017-09-27 21:30:08417 content::RunAllTasksUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55418
Lei Zhangbd15e1c92018-07-03 08:53:48419 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
[email protected]6cad5bf2011-03-10 21:21:55420
[email protected]237e6d02010-11-08 21:45:42421 // Make sure history cleans up correctly.
Gabriel Charette01507a22017-09-27 21:30:08422 content::RunAllTasksUntilIdle();
[email protected]55474b572009-04-14 22:05:33423}
[email protected]844a1002011-04-19 11:37:21424
miguelg1b495542016-03-18 17:57:46425TEST_F(ProfileManagerTest, LoadNonExistingProfile) {
426 const std::string profile_name = "NonExistingProfile";
427 base::RunLoop run_loop_1;
428 base::RunLoop run_loop_2;
429
430 ProfileManager* profile_manager = g_browser_process->profile_manager();
431 profile_manager->LoadProfile(
432 profile_name, false /* incognito */,
433 base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure()));
434 run_loop_1.Run();
435
436 profile_manager->LoadProfile(
437 profile_name, true /* incognito */,
438 base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure()));
439 run_loop_2.Run();
440}
441
442TEST_F(ProfileManagerTest, LoadExistingProfile) {
443 const std::string profile_name = "MyProfile";
444 const std::string other_name = "SomeOtherProfile";
445 MockObserver mock_observer1;
446 EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail()))
447 .Times(testing::AtLeast(1));
448
449 ProfileManager* profile_manager = g_browser_process->profile_manager();
Marc Treib92127162018-08-27 14:20:03450 CreateProfileAsync(profile_manager, profile_name, &mock_observer1);
miguelg1b495542016-03-18 17:57:46451
452 // Make sure a real profile is created before continuing.
Gabriel Charette01507a22017-09-27 21:30:08453 content::RunAllTasksUntilIdle();
miguelg1b495542016-03-18 17:57:46454
455 base::RunLoop load_profile;
456 bool incognito = false;
457 profile_manager->LoadProfile(
458 profile_name, incognito,
459 base::Bind(&ExpectProfileWithName, profile_name, incognito,
460 load_profile.QuitClosure()));
461 load_profile.Run();
462
463 base::RunLoop load_profile_incognito;
464 incognito = true;
465 profile_manager->LoadProfile(
466 profile_name, incognito,
467 base::Bind(&ExpectProfileWithName, profile_name, incognito,
468 load_profile_incognito.QuitClosure()));
469 load_profile_incognito.Run();
470
471 // Loading some other non existing profile should still return null.
472 base::RunLoop load_other_profile;
473 profile_manager->LoadProfile(
474 other_name, false,
475 base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure()));
476 load_other_profile.Run();
477}
478
[email protected]844a1002011-04-19 11:37:21479TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
Lei Zhangbd15e1c92018-07-03 08:53:48480 g_created_profile = nullptr;
[email protected]844a1002011-04-19 11:37:21481
482 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11483 EXPECT_CALL(mock_observer1, OnProfileCreated(
484 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21485 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11486 EXPECT_CALL(mock_observer2, OnProfileCreated(
487 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21488 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11489 EXPECT_CALL(mock_observer3, OnProfileCreated(
490 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21491
[email protected]881cb0b62011-12-13 20:39:54492 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00493 const std::string profile_name = "New Profile";
Marc Treib92127162018-08-27 14:20:03494 CreateProfileAsync(profile_manager, profile_name, &mock_observer1);
495 CreateProfileAsync(profile_manager, profile_name, &mock_observer2);
496 CreateProfileAsync(profile_manager, profile_name, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21497
Gabriel Charette01507a22017-09-27 21:30:08498 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21499}
500
501TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00502 const std::string profile_name1 = "New Profile 1";
503 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21504
505 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11506 EXPECT_CALL(mock_observer, OnProfileCreated(
507 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21508
[email protected]881cb0b62011-12-13 20:39:54509 ProfileManager* profile_manager = g_browser_process->profile_manager();
510
Marc Treib92127162018-08-27 14:20:03511 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
512 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
[email protected]844a1002011-04-19 11:37:21513
Gabriel Charette01507a22017-09-27 21:30:08514 content::RunAllTasksUntilIdle();
[email protected]844a1002011-04-19 11:37:21515}
[email protected]279170832011-10-12 23:38:03516
lwchkg498e92492016-04-23 11:04:12517TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) {
[email protected]5ddfade2014-02-03 10:24:53518 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12519 ProfileAttributesStorage& storage =
520 profile_manager->GetProfileAttributesStorage();
521 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53522
Jochen Eisingerd84bb42a2018-07-23 22:39:03523#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05524 const base::FilePath supervised_path =
vabr8023d872016-09-15 08:12:22525 temp_dir_.GetPath().AppendASCII("Supervised");
Daniel Hosseinian8f0b7482019-06-07 18:21:04526 auto supervised_profile =
527 std::make_unique<TestingProfile>(supervised_path, nullptr);
Jochen Eisingerd84bb42a2018-07-23 22:39:03528 supervised_profile->GetPrefs()->SetString(
529 prefs::kSupervisedUserId, supervised_users::kChildAccountSUID);
[email protected]5ddfade2014-02-03 10:24:53530
531 // RegisterTestingProfile adds the profile to the cache and takes ownership.
Daniel Hosseinian8f0b7482019-06-07 18:21:04532 profile_manager->RegisterTestingProfile(std::move(supervised_profile), true,
533 false);
Lei Zhangbd15e1c92018-07-03 08:53:48534 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
535 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0]->IsOmitted());
Jochen Eisingerd84bb42a2018-07-23 22:39:03536#endif
[email protected]5ddfade2014-02-03 10:24:53537
vabr8023d872016-09-15 08:12:22538 const base::FilePath nonsupervised_path =
539 temp_dir_.GetPath().AppendASCII("Non-Supervised");
Daniel Hosseinian8f0b7482019-06-07 18:21:04540 auto nonsupervised_profile =
541 std::make_unique<TestingProfile>(nonsupervised_path, nullptr);
542 profile_manager->RegisterTestingProfile(std::move(nonsupervised_profile),
543 true, false);
[email protected]5ddfade2014-02-03 10:24:53544
Jochen Eisingerd84bb42a2018-07-23 22:39:03545#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
lwchkg498e92492016-04-23 11:04:12546 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
Jochen Eisingerd84bb42a2018-07-23 22:39:03547#else
548 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
549#endif
lwchkg498e92492016-04-23 11:04:12550 ProfileAttributesEntry* entry;
Jochen Eisingerd84bb42a2018-07-23 22:39:03551#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
lwchkg498e92492016-04-23 11:04:12552 ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry));
553 EXPECT_TRUE(entry->IsOmitted());
Jochen Eisingerd84bb42a2018-07-23 22:39:03554#endif
lwchkg498e92492016-04-23 11:04:12555
556 ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry));
557 EXPECT_FALSE(entry->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53558}
559
[email protected]4251165a2013-07-17 04:33:40560TEST_F(ProfileManagerTest, GetGuestProfilePath) {
561 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
vabr8023d872016-09-15 08:12:22562 base::FilePath expected_path = temp_dir_.GetPath();
[email protected]4251165a2013-07-17 04:33:40563 expected_path = expected_path.Append(chrome::kGuestProfileDir);
564 EXPECT_EQ(expected_path, guest_path);
565}
566
mlermanb8df6e82015-01-23 19:55:34567TEST_F(ProfileManagerTest, GetSystemProfilePath) {
568 base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath();
vabr8023d872016-09-15 08:12:22569 base::FilePath expected_path = temp_dir_.GetPath();
mlermanb8df6e82015-01-23 19:55:34570 expected_path = expected_path.Append(chrome::kSystemProfileDir);
571 EXPECT_EQ(expected_path, system_profile_path);
572}
573
[email protected]0ff6198e2014-01-08 22:45:00574class UnittestGuestProfileManager : public UnittestProfileManager {
575 public:
576 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
577 : UnittestProfileManager(user_data_dir) {}
578
579 protected:
dcheng1fd716382014-10-22 20:14:41580 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]0ff6198e2014-01-08 22:45:00581 TestingProfile::Builder builder;
[email protected]0ff6198e2014-01-08 22:45:00582 builder.SetGuestSession();
[email protected]d22f7c932014-02-20 22:48:58583 builder.SetPath(file_path);
584 TestingProfile* testing_profile = builder.Build().release();
[email protected]0ff6198e2014-01-08 22:45:00585 return testing_profile;
586 }
587};
588
[email protected]9b6e10c2014-05-20 19:12:28589class ProfileManagerGuestTest : public ProfileManagerTest {
[email protected]0ff6198e2014-01-08 22:45:00590 protected:
dchenge1bc7982014-10-30 00:32:40591 void SetUp() override {
[email protected]0ff6198e2014-01-08 22:45:00592 // Create a new temporary directory, and store the path
593 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
594 TestingBrowserProcess::GetGlobal()->SetProfileManager(
vabr8023d872016-09-15 08:12:22595 new UnittestGuestProfileManager(temp_dir_.GetPath()));
[email protected]0ff6198e2014-01-08 22:45:00596
[email protected]2a92f2182014-03-25 00:47:59597#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43598 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
[email protected]9b6e10c2014-05-20 19:12:28599 // This switch is needed to skip non-test specific behavior in
600 // ProfileManager (accessing DBusThreadManager).
[email protected]0ff6198e2014-01-08 22:45:00601 cl->AppendSwitch(switches::kTestType);
[email protected]9b6e10c2014-05-20 19:12:28602
[email protected]0ff6198e2014-01-08 22:45:00603 cl->AppendSwitch(chromeos::switches::kGuestSession);
604 cl->AppendSwitch(::switches::kIncognito);
605
Wenzhao Zang8cf7ebd2017-12-22 02:37:19606 wallpaper_controller_client_ =
607 std::make_unique<WallpaperControllerClient>();
Mike Wassermana5352da2019-05-30 17:26:08608 wallpaper_controller_client_->InitForTesting(&test_wallpaper_controller_);
Wenzhao Zang8cf7ebd2017-12-22 02:37:19609
Andrew Wilsonc6c2c37e2017-09-01 14:44:27610 // Have to manually reset the session type in between test runs because
611 // RegisterUser() changes it.
612 ASSERT_EQ(extensions::FeatureSessionType::INITIAL,
613 extensions::GetCurrentFeatureSessionType());
614 session_type_ = extensions::ScopedCurrentFeatureSessionType(
615 extensions::GetCurrentFeatureSessionType());
616
Alexander Alekseev17513602017-12-05 03:16:36617 RegisterUser(GetFakeUserManager()->GetGuestAccountId());
[email protected]2a92f2182014-03-25 00:47:59618#endif
[email protected]0ff6198e2014-01-08 22:45:00619 }
Alexander Alekseev17513602017-12-05 03:16:36620
621 private:
622#if defined(OS_CHROMEOS)
623 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
624 return static_cast<chromeos::FakeChromeUserManager*>(
625 user_manager::UserManager::Get());
626 }
627#endif
[email protected]0ff6198e2014-01-08 22:45:00628};
629
[email protected]9b6e10c2014-05-20 19:12:28630TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
[email protected]2a92f2182014-03-25 00:47:59631 ProfileManager* profile_manager = g_browser_process->profile_manager();
632 ASSERT_TRUE(profile_manager);
633
634 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
635 ASSERT_TRUE(profile);
636 EXPECT_TRUE(profile->IsOffTheRecord());
637}
638
639#if defined(OS_CHROMEOS)
[email protected]9b6e10c2014-05-20 19:12:28640TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
[email protected]0ff6198e2014-01-08 22:45:00641 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
642 EXPECT_TRUE(primary_profile->IsOffTheRecord());
643
644 Profile* active_profile = ProfileManager::GetActiveUserProfile();
645 EXPECT_TRUE(active_profile->IsOffTheRecord());
646
647 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
[email protected]d22f7c932014-02-20 22:48:58648
649 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
650 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
651
652 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
[email protected]0ff6198e2014-01-08 22:45:00653}
654#endif
655
[email protected]279170832011-10-12 23:38:03656TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54657 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12658 ProfileAttributesStorage& storage =
659 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27660 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
Jeremy Romanec48d7a2018-03-01 17:35:09661 std::make_unique<base::Value>(true));
[email protected]714bf1d2012-11-30 01:35:27662
663 // Setting a pref which is not applicable to a system (i.e., Android in this
664 // case) does not necessarily create it. Don't bother continuing with the
665 // test if this pref doesn't exist because it will not load the profiles if
666 // it cannot verify that the pref for background mode is enabled.
667 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
668 return;
[email protected]279170832011-10-12 23:38:03669
lwchkg498e92492016-04-23 11:04:12670 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
671
672 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
Roman Sorokin8ead4d72018-04-16 15:58:58673 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
674 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12675 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
Roman Sorokin8ead4d72018-04-16 15:58:58676 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
677 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12678 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"),
Roman Sorokin8ead4d72018-04-16 15:58:58679 ASCIIToUTF16("name_3"), "34567", base::string16(), 0,
680 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12681
Lei Zhangbd15e1c92018-07-03 08:53:48682 ASSERT_EQ(3u, storage.GetNumberOfProfiles());
lwchkg498e92492016-04-23 11:04:12683
684 std::vector<ProfileAttributesEntry*> entries =
685 storage.GetAllProfilesAttributes();
Lei Zhangbd15e1c92018-07-03 08:53:48686 entries[0]->SetBackgroundStatus(true);
687 entries[2]->SetBackgroundStatus(true);
[email protected]279170832011-10-12 23:38:03688
[email protected]881cb0b62011-12-13 20:39:54689 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03690
[email protected]881cb0b62011-12-13 20:39:54691 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
692}
693
[email protected]714bf1d2012-11-30 01:35:27694TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
695 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12696 ProfileAttributesStorage& storage =
697 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27698 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
Jeremy Romanec48d7a2018-03-01 17:35:09699 std::make_unique<base::Value>(false));
[email protected]714bf1d2012-11-30 01:35:27700
lwchkg498e92492016-04-23 11:04:12701 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
702
703 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
Roman Sorokin8ead4d72018-04-16 15:58:58704 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
705 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12706 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
Roman Sorokin8ead4d72018-04-16 15:58:58707 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
708 std::string(), EmptyAccountId());
lwchkg498e92492016-04-23 11:04:12709
Lei Zhangbd15e1c92018-07-03 08:53:48710 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
lwchkg498e92492016-04-23 11:04:12711
712 std::vector<ProfileAttributesEntry*> entries =
713 storage.GetAllProfilesAttributes();
Lei Zhangbd15e1c92018-07-03 08:53:48714 entries[0]->SetBackgroundStatus(false);
715 entries[1]->SetBackgroundStatus(true);
[email protected]714bf1d2012-11-30 01:35:27716
717 profile_manager->AutoloadProfiles();
718
719 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
720}
721
[email protected]881cb0b62011-12-13 20:39:54722TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
vabr8023d872016-09-15 08:12:22723 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54724 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
725
726 ProfileManager* profile_manager = g_browser_process->profile_manager();
727
728 Profile* profile;
729
730 // Successfully create the profile
731 profile = profile_manager->GetProfile(dest_path);
732 ASSERT_TRUE(profile);
733
734 // Check that the profile name is non empty
735 std::string profile_name =
736 profile->GetPrefs()->GetString(prefs::kProfileName);
737 EXPECT_FALSE(profile_name.empty());
738
739 // Check that the profile avatar index is valid
740 size_t avatar_index =
741 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
[email protected]c3e559772014-04-09 04:02:54742 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
[email protected]881cb0b62011-12-13 20:39:54743 avatar_index));
744}
745
746// Tests that a new profile's entry in the profile info cache is setup with the
747// same values that are in the profile prefs.
748TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
vabr8023d872016-09-15 08:12:22749 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]881cb0b62011-12-13 20:39:54750 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
751
752 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]881cb0b62011-12-13 20:39:54753
754 // Successfully create the profile
755 Profile* profile = profile_manager->GetProfile(dest_path);
756 ASSERT_TRUE(profile);
757
758 std::string profile_name =
759 profile->GetPrefs()->GetString(prefs::kProfileName);
760 size_t avatar_index =
761 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
762
lwchkg498e92492016-04-23 11:04:12763 ProfileAttributesEntry* entry;
764 ASSERT_TRUE(profile_manager->GetProfileAttributesStorage().
765 GetProfileAttributesWithPath(dest_path, &entry));
[email protected]881cb0b62011-12-13 20:39:54766
767 // Check if the profile prefs are the same as the cache prefs
lwchkg498e92492016-04-23 11:04:12768 EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName()));
769 EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex());
[email protected]279170832011-10-12 23:38:03770}
[email protected]52edf812012-01-12 11:48:46771
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47772#if defined(OS_CHROMEOS)
773TEST_F(ProfileManagerTest, InitProfileForChildOnFirstSignIn) {
774 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
775 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
776
777 const std::string user_email = "[email protected]";
778 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
779 const std::string user_id_hash =
780 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
781 const base::FilePath dest_path =
782 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
783
784 TestingProfile::Builder builder;
785 builder.SetPath(dest_path);
786 builder.OverrideIsNewProfile(true);
787 std::unique_ptr<Profile> profile = builder.Build();
788
789 user_manager->UserLoggedIn(account_id, user_id_hash,
790 false /* browser_restart */, true /* is_child */);
791 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
792
793 EXPECT_EQ(
794 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
795 static_cast<int>(arc::ArcSupervisionTransition::NO_TRANSITION));
796 EXPECT_EQ(profile->GetPrefs()->GetString(prefs::kSupervisedUserId),
797 supervised_users::kChildAccountSUID);
798}
799
800TEST_F(ProfileManagerTest, InitProfileForRegularToChildTransition) {
801 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
802 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
803
804 const std::string user_email = "[email protected]";
805 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
806 const std::string user_id_hash =
807 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
808 const base::FilePath dest_path =
809 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
810
811 TestingProfile::Builder builder;
812 builder.SetPath(dest_path);
813 builder.OverrideIsNewProfile(false);
814 std::unique_ptr<Profile> profile = builder.Build();
Patricia Hongo890c47d2018-08-14 16:26:33815 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, true);
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47816
817 user_manager->UserLoggedIn(account_id, user_id_hash,
818 false /* browser_restart */, true /* is_child */);
819 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
820
821 EXPECT_EQ(
822 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
823 static_cast<int>(arc::ArcSupervisionTransition::REGULAR_TO_CHILD));
824 EXPECT_EQ(profile->GetPrefs()->GetString(prefs::kSupervisedUserId),
825 supervised_users::kChildAccountSUID);
826}
827
828TEST_F(ProfileManagerTest, InitProfileForChildToRegularTransition) {
829 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
830 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
831
832 const std::string user_email = "[email protected]";
833 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
834 const std::string user_id_hash =
835 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
836 const base::FilePath dest_path =
837 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
838
839 TestingProfile::Builder builder;
840 builder.SetPath(dest_path);
841 builder.OverrideIsNewProfile(false);
842 builder.SetSupervisedUserId(supervised_users::kChildAccountSUID);
843 std::unique_ptr<Profile> profile = builder.Build();
Patricia Hongo890c47d2018-08-14 16:26:33844 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, true);
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47845
846 user_manager->UserLoggedIn(account_id, user_id_hash,
847 false /* browser_restart */, false /* is_child */);
848 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
849
850 EXPECT_EQ(
851 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
852 static_cast<int>(arc::ArcSupervisionTransition::CHILD_TO_REGULAR));
853 EXPECT_TRUE(profile->GetPrefs()->GetString(prefs::kSupervisedUserId).empty());
854}
855
Patricia Hongo890c47d2018-08-14 16:26:33856TEST_F(ProfileManagerTest,
857 InitProfileForChildToRegularTransitionArcNotSignedIn) {
858 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
859 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
860
861 const std::string user_email = "[email protected]";
862 const AccountId account_id = AccountId::FromUserEmailGaiaId(user_email, "1");
863 const std::string user_id_hash =
864 profile_helper->GetUserIdHashByUserIdForTesting(user_email);
865 const base::FilePath dest_path =
866 profile_helper->GetProfilePathByUserIdHash(user_id_hash);
867
868 TestingProfile::Builder builder;
869 builder.SetPath(dest_path);
870 builder.OverrideIsNewProfile(false);
871 builder.SetSupervisedUserId(supervised_users::kChildAccountSUID);
872 std::unique_ptr<Profile> profile = builder.Build();
873 profile->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, false);
874
875 user_manager->UserLoggedIn(account_id, user_id_hash,
876 false /* browser_restart */, false /* is_child */);
877 g_browser_process->profile_manager()->InitProfileUserPrefs(profile.get());
878
879 EXPECT_EQ(
880 profile->GetPrefs()->GetInteger(arc::prefs::kArcSupervisionTransition),
881 static_cast<int>(arc::ArcSupervisionTransition::NO_TRANSITION));
882 EXPECT_TRUE(profile->GetPrefs()->GetString(prefs::kSupervisedUserId).empty());
883}
884
Bruno Kim Medeiros Cesarc259cc52018-08-03 23:49:47885#endif
886
[email protected]13720532013-05-30 12:30:59887TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
888 ProfileManager* profile_manager = g_browser_process->profile_manager();
889 ASSERT_TRUE(profile_manager);
890
dzhioev0298d722014-08-26 13:18:52891#if defined(OS_CHROMEOS)
xiyuan41d32a32017-06-12 20:06:13892 // On CrOS, profile returned by GetLastUsedProfile is a sign-in profile that
dzhioev0298d722014-08-26 13:18:52893 // is forced to be incognito. That's why we need to create at least one user
894 // to get a regular profile.
Alexander Alekseev17513602017-12-05 03:16:36895 RegisterUser(
896 AccountId::FromUserEmailGaiaId("[email protected]", "1234567890"));
dzhioev0298d722014-08-26 13:18:52897#endif
898
[email protected]13720532013-05-30 12:30:59899 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
900 ASSERT_TRUE(profile);
901 EXPECT_FALSE(profile->IsOffTheRecord());
902 PrefService* prefs = profile->GetPrefs();
Ian Vollick381389a62019-06-26 16:49:40903 EXPECT_EQ(IncognitoModePrefs::kDefaultAvailability,
[email protected]13720532013-05-30 12:30:59904 IncognitoModePrefs::GetAvailability(prefs));
905
[email protected]13720532013-05-30 12:30:59906 ASSERT_TRUE(profile->GetOffTheRecordProfile());
907
908 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
909 EXPECT_FALSE(
910 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
911
912 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
913 // incognito mode is forced.
914 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
915 EXPECT_TRUE(
916 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
917}
918
[email protected]c849fab2012-03-29 16:51:41919#if !defined(OS_ANDROID)
920// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46921TEST_F(ProfileManagerTest, LastOpenedProfiles) {
vabr8023d872016-09-15 08:12:22922 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46923 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
924
vabr8023d872016-09-15 08:12:22925 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46926 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
927
928 ProfileManager* profile_manager = g_browser_process->profile_manager();
929
930 // Successfully create the profiles.
931 TestingProfile* profile1 =
932 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
933 ASSERT_TRUE(profile1);
934
935 TestingProfile* profile2 =
936 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
937 ASSERT_TRUE(profile2);
938
939 std::vector<Profile*> last_opened_profiles =
940 profile_manager->GetLastOpenedProfiles();
941 ASSERT_EQ(0U, last_opened_profiles.size());
942
943 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:35944 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:03945 std::unique_ptr<Browser> browser1a(
cm.sanchi8b7ec882017-11-16 07:22:05946 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46947
948 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
949 ASSERT_EQ(1U, last_opened_profiles.size());
950 EXPECT_EQ(profile1, last_opened_profiles[0]);
951
952 // And for profile2.
erg7b01d692017-02-22 21:57:35953 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:03954 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:05955 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46956
957 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
958 ASSERT_EQ(2U, last_opened_profiles.size());
959 EXPECT_EQ(profile1, last_opened_profiles[0]);
960 EXPECT_EQ(profile2, last_opened_profiles[1]);
961
962 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03963 std::unique_ptr<Browser> browser1b(
cm.sanchi8b7ec882017-11-16 07:22:05964 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46965 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
966 ASSERT_EQ(2U, last_opened_profiles.size());
967 EXPECT_EQ(profile1, last_opened_profiles[0]);
968 EXPECT_EQ(profile2, last_opened_profiles[1]);
969
970 // Close the browsers.
971 browser1a.reset();
972 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
973 ASSERT_EQ(2U, last_opened_profiles.size());
974 EXPECT_EQ(profile1, last_opened_profiles[0]);
975 EXPECT_EQ(profile2, last_opened_profiles[1]);
976
977 browser1b.reset();
978 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
979 ASSERT_EQ(1U, last_opened_profiles.size());
980 EXPECT_EQ(profile2, last_opened_profiles[0]);
981
982 browser2.reset();
983 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
984 ASSERT_EQ(0U, last_opened_profiles.size());
985}
986
987TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
vabr8023d872016-09-15 08:12:22988 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46989 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
990
vabr8023d872016-09-15 08:12:22991 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]52edf812012-01-12 11:48:46992 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
993
994 ProfileManager* profile_manager = g_browser_process->profile_manager();
995
996 // Successfully create the profiles.
997 TestingProfile* profile1 =
998 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
999 ASSERT_TRUE(profile1);
1000
1001 TestingProfile* profile2 =
1002 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
1003 ASSERT_TRUE(profile2);
1004
1005 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351006 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:031007 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051008 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:461009
1010 // And for profile2.
erg7b01d692017-02-22 21:57:351011 Browser::CreateParams profile2_params(profile2, true);
dcheng6e25ed32016-04-08 03:30:031012 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:051013 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:461014
1015 std::vector<Profile*> last_opened_profiles =
1016 profile_manager->GetLastOpenedProfiles();
1017 ASSERT_EQ(2U, last_opened_profiles.size());
1018 EXPECT_EQ(profile1, last_opened_profiles[0]);
1019 EXPECT_EQ(profile2, last_opened_profiles[1]);
1020
1021 // Simulate a shutdown.
1022 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:301023 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:461024 content::NotificationService::AllSources(),
1025 content::NotificationService::NoDetails());
1026
1027 // Even if the browsers are destructed during shutdown, the profiles stay
1028 // open.
1029 browser1.reset();
1030 browser2.reset();
1031
1032 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1033 ASSERT_EQ(2U, last_opened_profiles.size());
1034 EXPECT_EQ(profile1, last_opened_profiles[0]);
1035 EXPECT_EQ(profile2, last_opened_profiles[1]);
1036}
[email protected]41276342012-02-06 10:49:311037
1038TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
vabr8023d872016-09-15 08:12:221039 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:311040 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
vabr8023d872016-09-15 08:12:221041 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]41276342012-02-06 10:49:311042 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
1043
1044 ProfileManager* profile_manager = g_browser_process->profile_manager();
1045
1046 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:061047 TestingProfile* profile1 =
1048 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:311049 ASSERT_TRUE(profile1);
1050
[email protected]41276342012-02-06 10:49:311051 std::vector<Profile*> last_opened_profiles =
1052 profile_manager->GetLastOpenedProfiles();
1053 ASSERT_EQ(0U, last_opened_profiles.size());
1054
1055 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351056 Browser::CreateParams profile1_params(profile1, true);
dcheng6e25ed32016-04-08 03:30:031057 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051058 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:311059
1060 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1061 ASSERT_EQ(1U, last_opened_profiles.size());
1062 EXPECT_EQ(profile1, last_opened_profiles[0]);
1063
1064 // And for profile2.
erg7b01d692017-02-22 21:57:351065 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
1066 true);
dcheng6e25ed32016-04-08 03:30:031067 std::unique_ptr<Browser> browser2a(
cm.sanchi8b7ec882017-11-16 07:22:051068 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:311069
1070 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1071 ASSERT_EQ(1U, last_opened_profiles.size());
1072 EXPECT_EQ(profile1, last_opened_profiles[0]);
1073
1074 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:031075 std::unique_ptr<Browser> browser2b(
cm.sanchi8b7ec882017-11-16 07:22:051076 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:311077 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1078 ASSERT_EQ(1U, last_opened_profiles.size());
1079 EXPECT_EQ(profile1, last_opened_profiles[0]);
1080
1081 // Close the browsers.
1082 browser2a.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 browser2b.reset();
1088 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1089 ASSERT_EQ(1U, last_opened_profiles.size());
1090 EXPECT_EQ(profile1, last_opened_profiles[0]);
1091
1092 browser1.reset();
1093 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1094 ASSERT_EQ(0U, last_opened_profiles.size());
1095}
[email protected]c849fab2012-03-29 16:51:411096#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:001097
1098#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1099// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:481100TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
vabr8023d872016-09-15 08:12:221101 base::FilePath dest_path = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:481102 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
1103
1104 ProfileManager* profile_manager = g_browser_process->profile_manager();
1105
1106 TestingProfile* profile =
1107 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
1108 ASSERT_TRUE(profile);
palar47cf44bd2017-03-03 15:36:111109 SetProfileEphemeral(profile);
[email protected]8d9243a2013-10-21 17:11:481110
1111 // Here the last used profile is still the "Default" profile.
1112 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
1113 EXPECT_NE(profile, last_used_profile);
1114
[email protected]79fd6b72013-10-22 16:39:271115 // Create a browser for the profile.
erg7b01d692017-02-22 21:57:351116 Browser::CreateParams profile_params(profile, true);
dcheng6e25ed32016-04-08 03:30:031117 std::unique_ptr<Browser> browser(
cm.sanchi8b7ec882017-11-16 07:22:051118 CreateBrowserWithTestWindowForParams(&profile_params));
[email protected]8d9243a2013-10-21 17:11:481119 last_used_profile = profile_manager->GetLastUsedProfile();
1120 EXPECT_NE(profile, last_used_profile);
1121
1122 // Close the browser.
1123 browser.reset();
1124 last_used_profile = profile_manager->GetLastUsedProfile();
1125 EXPECT_NE(profile, last_used_profile);
1126}
1127
1128TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
vabr8023d872016-09-15 08:12:221129 base::FilePath dest_path1 = temp_dir_.GetPath();
[email protected]8d9243a2013-10-21 17:11:481130 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
1131
vabr8023d872016-09-15 08:12:221132 base::FilePath dest_path2 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:271133 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
1134
vabr8023d872016-09-15 08:12:221135 base::FilePath dest_path3 = temp_dir_.GetPath();
[email protected]79fd6b72013-10-22 16:39:271136 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:481137
1138 ProfileManager* profile_manager = g_browser_process->profile_manager();
1139
1140 // Successfully create the profiles.
1141 TestingProfile* normal_profile =
1142 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
1143 ASSERT_TRUE(normal_profile);
1144
1145 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:271146 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:481147 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:271148 ASSERT_TRUE(ephemeral_profile1);
palar47cf44bd2017-03-03 15:36:111149 SetProfileEphemeral(ephemeral_profile1);
[email protected]79fd6b72013-10-22 16:39:271150
1151 // Add second ephemeral profile but don't mark it as such yet.
1152 TestingProfile* ephemeral_profile2 =
1153 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
1154 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481155
1156 // Create a browser for profile1.
erg7b01d692017-02-22 21:57:351157 Browser::CreateParams profile1_params(normal_profile, true);
dcheng6e25ed32016-04-08 03:30:031158 std::unique_ptr<Browser> browser1(
cm.sanchi8b7ec882017-11-16 07:22:051159 CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]8d9243a2013-10-21 17:11:481160
[email protected]79fd6b72013-10-22 16:39:271161 // Create browsers for the ephemeral profile.
erg7b01d692017-02-22 21:57:351162 Browser::CreateParams profile2_params(ephemeral_profile1, true);
dcheng6e25ed32016-04-08 03:30:031163 std::unique_ptr<Browser> browser2(
cm.sanchi8b7ec882017-11-16 07:22:051164 CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]8d9243a2013-10-21 17:11:481165
erg7b01d692017-02-22 21:57:351166 Browser::CreateParams profile3_params(ephemeral_profile2, true);
dcheng6e25ed32016-04-08 03:30:031167 std::unique_ptr<Browser> browser3(
cm.sanchi8b7ec882017-11-16 07:22:051168 CreateBrowserWithTestWindowForParams(&profile3_params));
[email protected]79fd6b72013-10-22 16:39:271169
[email protected]8d9243a2013-10-21 17:11:481170 std::vector<Profile*> last_opened_profiles =
1171 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:271172 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:481173 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:271174 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
1175
1176 // Mark the second profile ephemeral.
palar47cf44bd2017-03-03 15:36:111177 SetProfileEphemeral(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:481178
1179 // Simulate a shutdown.
1180 content::NotificationService::current()->Notify(
1181 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
1182 content::NotificationService::AllSources(),
1183 content::NotificationService::NoDetails());
1184 browser1.reset();
1185 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:271186 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:481187
1188 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
1189 ASSERT_EQ(1U, last_opened_profiles.size());
1190 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
1191}
1192
bauerb8b022182015-05-13 09:18:391193TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
1194 // Create two profiles, one of them ephemeral.
1195 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121196 ProfileAttributesStorage& storage =
1197 profile_manager->GetProfileAttributesStorage();
1198 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391199
1200 const std::string profile_name1 = "Homer";
lwchkg498e92492016-04-23 11:04:121201 base::FilePath path1 =
1202 profile_manager->user_data_dir().AppendASCII(profile_name1);
1203 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581204 base::UTF8ToUTF16(profile_name1), 0, std::string(),
1205 EmptyAccountId());
Lei Zhangbd15e1c92018-07-03 08:53:481206 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391207 ASSERT_TRUE(base::CreateDirectory(path1));
1208
1209 const std::string profile_name2 = "Marge";
lwchkg498e92492016-04-23 11:04:121210 base::FilePath path2 =
1211 profile_manager->user_data_dir().AppendASCII(profile_name2);
1212 storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581213 base::UTF8ToUTF16(profile_name2), 0, std::string(),
1214 EmptyAccountId());
lwchkg498e92492016-04-23 11:04:121215 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391216 ASSERT_TRUE(base::CreateDirectory(path2));
1217
1218 // Set the active profile.
1219 PrefService* local_state = g_browser_process->local_state();
1220 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1221
1222 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081223 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391224
1225 // The ephemeral profile should be deleted, and the last used profile set to
1226 // the other one.
1227 EXPECT_FALSE(base::DirectoryExists(path1));
1228 EXPECT_TRUE(base::DirectoryExists(path2));
1229 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121230 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391231
1232 // Mark the remaining profile ephemeral and clean up.
Lei Zhangbd15e1c92018-07-03 08:53:481233 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391234 profile_manager->CleanUpEphemeralProfiles();
Gabriel Charette01507a22017-09-27 21:30:081235 content::RunAllTasksUntilIdle();
bauerb8b022182015-05-13 09:18:391236
1237 // The profile should be deleted, and the last used profile set to a new one.
1238 EXPECT_FALSE(base::DirectoryExists(path2));
lwchkg498e92492016-04-23 11:04:121239 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391240 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1241}
1242
Owen Minb0528082018-03-22 16:15:471243TEST_F(ProfileManagerTest, CleanUpEphemeralProfilesWithGuestLastUsedProfile) {
1244 ProfileManager* profile_manager = g_browser_process->profile_manager();
1245 ProfileAttributesStorage& storage =
1246 profile_manager->GetProfileAttributesStorage();
1247 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
1248
1249 const std::string profile_name1 = "Homer";
1250 base::FilePath path1 =
1251 profile_manager->user_data_dir().AppendASCII(profile_name1);
1252 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:581253 base::UTF8ToUTF16(profile_name1), 0, std::string(),
1254 EmptyAccountId());
Lei Zhangbd15e1c92018-07-03 08:53:481255 storage.GetAllProfilesAttributes()[0]->SetIsEphemeral(true);
Owen Minb0528082018-03-22 16:15:471256 ASSERT_TRUE(base::CreateDirectory(path1));
1257 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1258
1259 // Set the active profile.
1260 PrefService* local_state = g_browser_process->local_state();
1261 local_state->SetString(prefs::kProfileLastUsed, std::string("Guest Profile"));
1262
1263 profile_manager->CleanUpEphemeralProfiles();
1264 content::RunAllTasksUntilIdle();
1265
1266 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
1267 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1268}
1269
[email protected]1c62b2f2013-06-28 00:15:001270TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
1271 ProfileManager* profile_manager = g_browser_process->profile_manager();
1272 ASSERT_TRUE(profile_manager);
1273
1274 // Create and load two profiles.
1275 const std::string profile_name1 = "New Profile 1";
1276 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221277 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1278 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001279
1280 MockObserver mock_observer;
1281 EXPECT_CALL(mock_observer, OnProfileCreated(
1282 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
1283
Marc Treib92127162018-08-27 14:20:031284 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
1285 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081286 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001287
1288 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121289 EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage().
1290 GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001291
1292 // Set the active profile.
1293 PrefService* local_state = g_browser_process->local_state();
1294 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1295
1296 // Delete the active profile.
David Roger51797642018-03-20 13:29:161297 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081298 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001299
1300 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1301 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1302}
[email protected]3f130532014-02-26 20:39:241303
[email protected]602b5a29f2014-07-18 15:39:331304TEST_F(ProfileManagerTest, LastProfileDeleted) {
1305 ProfileManager* profile_manager = g_browser_process->profile_manager();
1306 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121307 ProfileAttributesStorage& storage =
1308 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331309
1310 // Create and load a profile.
1311 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221312 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331313
1314 MockObserver mock_observer;
1315 EXPECT_CALL(mock_observer, OnProfileCreated(
1316 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
1317
Marc Treib92127162018-08-27 14:20:031318 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081319 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331320
1321 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121322 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331323
1324 // Set it as the active profile.
1325 PrefService* local_state = g_browser_process->local_state();
1326 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1327
1328 // Delete the active profile.
David Roger51797642018-03-20 13:29:161329 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081330 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331331
1332 // A new profile should have been created
1333 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221334 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331335
1336 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1337 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
Lei Zhangbd15e1c92018-07-03 08:53:481338 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1339 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331340}
1341
1342TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1343 ProfileManager* profile_manager = g_browser_process->profile_manager();
1344 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121345 ProfileAttributesStorage& storage =
1346 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331347
1348 // Create and load a profile.
1349 const std::string profile_name1 = "New Profile 1";
vabr8023d872016-09-15 08:12:221350 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
[email protected]602b5a29f2014-07-18 15:39:331351
1352 MockObserver mock_observer;
1353 EXPECT_CALL(mock_observer, OnProfileCreated(
1354 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1355
Marc Treib92127162018-08-27 14:20:031356 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081357 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331358
1359 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121360 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331361
1362 // Create the profile and register it.
1363 const std::string guest_profile_name =
1364 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1365
1366 TestingProfile::Builder builder;
1367 builder.SetGuestSession();
1368 builder.SetPath(ProfileManager::GetGuestProfilePath());
Daniel Hosseinian8f0b7482019-06-07 18:21:041369 std::unique_ptr<TestingProfile> guest_profile = builder.Build();
[email protected]602b5a29f2014-07-18 15:39:331370 guest_profile->set_profile_name(guest_profile_name);
Daniel Hosseinian8f0b7482019-06-07 18:21:041371 profile_manager->RegisterTestingProfile(std::move(guest_profile), false,
1372 false);
[email protected]602b5a29f2014-07-18 15:39:331373
lwchkg498e92492016-04-23 11:04:121374 // The Guest profile does not get added to the ProfileAttributesStorage.
[email protected]602b5a29f2014-07-18 15:39:331375 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121376 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331377
1378 // Set the Guest profile as the active profile.
1379 PrefService* local_state = g_browser_process->local_state();
1380 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1381
1382 // Delete the other profile.
David Roger51797642018-03-20 13:29:161383 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081384 content::RunAllTasksUntilIdle();
[email protected]602b5a29f2014-07-18 15:39:331385
1386 // A new profile should have been created.
1387 const std::string profile_name2 = "Profile 1";
vabr8023d872016-09-15 08:12:221388 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]602b5a29f2014-07-18 15:39:331389
1390 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
Lei Zhangbd15e1c92018-07-03 08:53:481391 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1392 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331393}
1394
[email protected]3f130532014-02-26 20:39:241395TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1396 if (!profiles::IsMultipleProfilesEnabled())
1397 return;
1398
[email protected]3f130532014-02-26 20:39:241399 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121400 ProfileAttributesStorage& storage =
1401 profile_manager->GetProfileAttributesStorage();
1402 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241403
1404 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1405 const base::string16 default_profile_name =
1406 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121407 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1408 Profile* profile1 = AddProfileToStorage(profile_manager,
1409 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441410 EXPECT_EQ(default_profile_name,
1411 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241412
1413 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121414 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1415 Profile* profile2 = AddProfileToStorage(profile_manager,
1416 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441417 EXPECT_EQ(profile_name1,
1418 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1419 EXPECT_EQ(profile_name2,
1420 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241421
1422 // Deleting a profile means returning to the default name.
1423 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161424 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081425 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441426 EXPECT_EQ(default_profile_name,
1427 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241428}
1429
1430TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1431 if (!profiles::IsMultipleProfilesEnabled())
1432 return;
1433
[email protected]3f130532014-02-26 20:39:241434 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121435 ProfileAttributesStorage& storage =
1436 profile_manager->GetProfileAttributesStorage();
1437 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241438
1439 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1440 const base::string16 default_profile_name =
1441 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121442 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1443 Profile* profile1 = AddProfileToStorage(profile_manager,
1444 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441445 EXPECT_EQ(default_profile_name,
1446 profiles::GetAvatarNameForProfile(profile1->GetPath()));
lwchkg498e92492016-04-23 11:04:121447 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241448
1449 // We should display custom names for local profiles.
1450 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
Lei Zhangbd15e1c92018-07-03 08:53:481451 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0];
lwchkg498e92492016-04-23 11:04:121452 entry->SetName(custom_profile_name);
1453 entry->SetIsUsingDefaultName(false);
1454 EXPECT_EQ(custom_profile_name, entry->GetName());
[email protected]119809d2014-06-27 22:12:441455 EXPECT_EQ(custom_profile_name,
1456 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241457
1458 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121459 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1460 Profile* profile2 = AddProfileToStorage(profile_manager,
1461 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441462 EXPECT_EQ(custom_profile_name,
1463 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1464 EXPECT_EQ(profile_name2,
1465 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241466
1467 // Deleting a profile means returning to the original, custom name.
1468 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161469 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081470 content::RunAllTasksUntilIdle();
[email protected]119809d2014-06-27 22:12:441471 EXPECT_EQ(custom_profile_name,
1472 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241473}
1474
1475TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1476 if (!profiles::IsMultipleProfilesEnabled())
1477 return;
1478
[email protected]3f130532014-02-26 20:39:241479 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121480 ProfileAttributesStorage& storage =
1481 profile_manager->GetProfileAttributesStorage();
1482 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241483
1484 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1485 const base::string16 default_profile_name =
1486 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121487 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1488 Profile* profile1 = AddProfileToStorage(profile_manager,
1489 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441490 EXPECT_EQ(default_profile_name,
1491 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241492
Lei Zhangbd15e1c92018-07-03 08:53:481493 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1494 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0];
[email protected]78186a42014-08-22 22:07:301495 // For a signed in profile with a default name we still display
1496 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
lwchkg498e92492016-04-23 11:04:121497 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
1498 EXPECT_EQ(profile_name1, entry->GetName());
[email protected]78186a42014-08-22 22:07:301499 EXPECT_EQ(default_profile_name,
[email protected]119809d2014-06-27 22:12:441500 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241501
[email protected]78186a42014-08-22 22:07:301502 // For a signed in profile with a non-default Gaia given name we display the
1503 // Gaia given name.
lwchkg498e92492016-04-23 11:04:121504 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
[email protected]78186a42014-08-22 22:07:301505 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
lwchkg498e92492016-04-23 11:04:121506 entry->SetGAIAGivenName(gaia_given_name);
1507 EXPECT_EQ(gaia_given_name, entry->GetName());
[email protected]78186a42014-08-22 22:07:301508 EXPECT_EQ(gaia_given_name,
1509 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1510
[email protected]3f130532014-02-26 20:39:241511 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121512 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1513 Profile* profile2 = AddProfileToStorage(profile_manager,
1514 "path_2", profile_name2);
[email protected]78186a42014-08-22 22:07:301515 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441516 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1517 EXPECT_EQ(profile_name2,
1518 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241519
1520 // Deleting a profile means returning to the original, actual profile name.
1521 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
David Roger51797642018-03-20 13:29:161522 base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081523 content::RunAllTasksUntilIdle();
[email protected]78186a42014-08-22 22:07:301524 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441525 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241526}
nomsc9299462014-09-16 22:55:281527
1528TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1529 if (!profiles::IsMultipleProfilesEnabled())
1530 return;
1531
nomsc9299462014-09-16 22:55:281532 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121533 ProfileAttributesStorage& storage =
1534 profile_manager->GetProfileAttributesStorage();
1535 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281536
1537 // Create two signed in profiles, with both new and legacy default names, and
1538 // a profile with a custom name.
lwchkg498e92492016-04-23 11:04:121539 Profile* profile1 = AddProfileToStorage(profile_manager, "path_1",
1540 ASCIIToUTF16("Person 1"));
1541 Profile* profile2 = AddProfileToStorage(profile_manager, "path_2",
1542 ASCIIToUTF16("Default Profile"));
nomsc9299462014-09-16 22:55:281543 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
lwchkg498e92492016-04-23 11:04:121544 Profile* profile3 = AddProfileToStorage(profile_manager, "path_3",
1545 profile_name3);
1546 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281547
1548 // Sign in all profiles, and make sure they do not have a Gaia name set.
1549 const base::string16 email1(ASCIIToUTF16("[email protected]"));
1550 const base::string16 email2(ASCIIToUTF16("[email protected]"));
1551 const base::string16 email3(ASCIIToUTF16("[email protected]"));
1552
lwchkg498e92492016-04-23 11:04:121553 ProfileAttributesEntry* entry;
1554
1555 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1556 &entry));
1557 entry->SetAuthInfo("12345", email1);
1558 entry->SetGAIAGivenName(base::string16());
1559 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281560
1561 // This may resort the cache, so be extra cautious to use the right profile.
lwchkg498e92492016-04-23 11:04:121562 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(),
1563 &entry));
1564 entry->SetAuthInfo("23456", email2);
1565 entry->SetGAIAGivenName(base::string16());
1566 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281567
lwchkg498e92492016-04-23 11:04:121568 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(),
1569 &entry));
1570 entry->SetAuthInfo("34567", email3);
1571 entry->SetGAIAGivenName(base::string16());
1572 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281573
1574 // The profiles with default names should display the email address.
1575 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1576 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1577
1578 // The profile with the custom name should display that.
1579 EXPECT_EQ(profile_name3,
1580 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1581
1582 // Adding a Gaia name to a profile that previously had a default name should
1583 // start displaying it.
1584 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
lwchkg498e92492016-04-23 11:04:121585 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1586 &entry));
1587 entry->SetGAIAGivenName(gaia_given_name);
nomsc9299462014-09-16 22:55:281588 EXPECT_EQ(gaia_given_name,
1589 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1590}
[email protected]1c62b2f2013-06-28 00:15:001591#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1592
1593#if defined(OS_MACOSX)
1594// These tests are for a Mac-only code path that assumes the browser
1595// process isn't killed when all browser windows are closed.
1596TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1597 ProfileManager* profile_manager = g_browser_process->profile_manager();
1598 ASSERT_TRUE(profile_manager);
1599
1600 // Create and load one profile, and just create a second profile.
1601 const std::string profile_name1 = "New Profile 1";
1602 const std::string profile_name2 = "New Profile 2";
vabr8023d872016-09-15 08:12:221603 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1604 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001605
1606 MockObserver mock_observer;
1607 EXPECT_CALL(mock_observer, OnProfileCreated(
1608 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
Marc Treib92127162018-08-27 14:20:031609 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081610 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001611
1612 // Track the profile, but don't load it.
lwchkg498e92492016-04-23 11:04:121613 ProfileAttributesStorage& storage =
1614 profile_manager->GetProfileAttributesStorage();
1615 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
Roman Sorokin8ead4d72018-04-16 15:58:581616 base::string16(), 0, std::string(), EmptyAccountId());
Gabriel Charette01507a22017-09-27 21:30:081617 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001618
1619 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121620 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001621
1622 // Set the active profile.
1623 PrefService* local_state = g_browser_process->local_state();
1624 local_state->SetString(prefs::kProfileLastUsed,
1625 dest_path1.BaseName().MaybeAsASCII());
1626
1627 // Delete the active profile. This should switch and load the unloaded
1628 // profile.
David Roger51797642018-03-20 13:29:161629 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
[email protected]1c62b2f2013-06-28 00:15:001630
Gabriel Charette01507a22017-09-27 21:30:081631 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001632
1633 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1634 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1635}
1636
1637// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1638// by simulating a scenario in which the profile that is being loaded as
1639// the next active profile has also been marked for deletion, so the
1640// ProfileManager needs to recursively select a different next profile.
1641TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1642 ProfileManager* profile_manager = g_browser_process->profile_manager();
1643 ASSERT_TRUE(profile_manager);
1644
1645 // Create and load one profile, and create two more profiles.
1646 const std::string profile_name1 = "New Profile 1";
1647 const std::string profile_name2 = "New Profile 2";
1648 const std::string profile_name3 = "New Profile 3";
vabr8023d872016-09-15 08:12:221649 base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1);
1650 base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2);
1651 base::FilePath dest_path3 = temp_dir_.GetPath().AppendASCII(profile_name3);
[email protected]1c62b2f2013-06-28 00:15:001652
1653 MockObserver mock_observer;
1654 EXPECT_CALL(mock_observer, OnProfileCreated(
1655 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
Marc Treib92127162018-08-27 14:20:031656 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
Gabriel Charette01507a22017-09-27 21:30:081657 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001658
1659 // Create the other profiles, but don't load them. Assign a fake avatar icon
lwchkg498e92492016-04-23 11:04:121660 // to ensure that profiles in the profile attributes storage are sorted by the
1661 // profile name, and not randomly by the avatar name.
1662 ProfileAttributesStorage& storage =
1663 profile_manager->GetProfileAttributesStorage();
1664 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
Roman Sorokin8ead4d72018-04-16 15:58:581665 ASCIIToUTF16(profile_name2), 1, std::string(),
1666 EmptyAccountId());
lwchkg498e92492016-04-23 11:04:121667 storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567",
Roman Sorokin8ead4d72018-04-16 15:58:581668 ASCIIToUTF16(profile_name3), 2, std::string(),
1669 EmptyAccountId());
[email protected]1c62b2f2013-06-28 00:15:001670
Gabriel Charette01507a22017-09-27 21:30:081671 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001672
1673 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121674 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001675
1676 // Set the active profile.
1677 PrefService* local_state = g_browser_process->local_state();
1678 local_state->SetString(prefs::kProfileLastUsed,
1679 dest_path1.BaseName().MaybeAsASCII());
1680
1681 // Delete the active profile, Profile1.
1682 // This will post a CreateProfileAsync message, that tries to load Profile2,
1683 // which checks that the profile is not being deleted, and then calls back
1684 // FinishDeletingProfile for Profile1.
1685 // Try to break this flow by setting the active profile to Profile2 in the
1686 // middle (so after the first posted message), and trying to delete Profile2,
1687 // so that the ProfileManager has to look for a different profile to load.
David Roger51797642018-03-20 13:29:161688 profile_manager->ScheduleProfileForDeletion(dest_path1, base::DoNothing());
[email protected]1c62b2f2013-06-28 00:15:001689 local_state->SetString(prefs::kProfileLastUsed,
1690 dest_path2.BaseName().MaybeAsASCII());
David Roger51797642018-03-20 13:29:161691 profile_manager->ScheduleProfileForDeletion(dest_path2, base::DoNothing());
Gabriel Charette01507a22017-09-27 21:30:081692 content::RunAllTasksUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001693
1694 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1695 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1696}
Lei Zhangbd15e1c92018-07-03 08:53:481697#endif // defined(OS_MACOSX)