blob: 29375a7ec8a982fe9fa7da6014f4c92c19aa3a93 [file] [log] [blame]
[email protected]52edf812012-01-12 11:48:461// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
avib896c712015-12-26 02:10:435#include <stddef.h>
6
[email protected]d99bcaa2010-12-05 20:15:567#include <string>
8
[email protected]943aed5542010-06-11 15:57:089#include "base/command_line.h"
miguelg1b495542016-03-18 17:57:4610#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0411#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
avib896c712015-12-26 02:10:4313#include "base/macros.h"
[email protected]37ac95b2013-07-23 23:39:3514#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0815#include "base/strings/utf_string_conversions.h"
[email protected]29d70252011-04-28 02:16:5816#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3217#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4918#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3519#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0320#include "chrome/browser/chrome_notification_types.h"
[email protected]363f5272013-04-23 17:21:4221#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]9718a722012-06-19 20:10:5322#include "chrome/browser/history/history_service_factory.h"
[email protected]63e26822011-07-16 19:07:3523#include "chrome/browser/io_thread.h"
[email protected]81ad7f4a2011-03-16 01:33:2924#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5925#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3326#include "chrome/browser/profiles/profile.h"
[email protected]c3e559772014-04-09 04:02:5427#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]279170832011-10-12 23:38:0328#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3329#include "chrome/browser/profiles/profile_manager.h"
[email protected]3f130532014-02-26 20:39:2430#include "chrome/browser/profiles/profiles_state.h"
[email protected]d8748142012-05-16 21:13:4331#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0832#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0834#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5835#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1836#include "chrome/grit/generated_resources.h"
[email protected]7688968a2013-02-12 21:45:1337#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2938#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3539#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1740#include "chrome/test/base/testing_profile.h"
sdefresne506dd522015-03-12 18:49:1241#include "components/history/core/browser/history_service.h"
[email protected]29896ee2014-06-17 17:20:5342#include "components/signin/core/common/profile_management_switches.h"
[email protected]ad50def52011-10-19 23:17:0743#include "content/public/browser/notification_service.h"
[email protected]f9357a442014-05-15 18:44:0744#include "content/public/common/content_switches.h"
[email protected]37ac95b2013-07-23 23:39:3545#include "content/public/test/test_browser_thread_bundle.h"
[email protected]844a1002011-04-19 11:37:2146#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2947#include "testing/gtest/include/gtest/gtest.h"
[email protected]3f130532014-02-26 20:39:2448#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2949
[email protected]775caec2011-09-01 17:14:3250#if defined(OS_CHROMEOS)
merkulova793f3022015-02-04 10:18:3051#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
[email protected]4d390782014-08-15 09:22:5852#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
dzhioev0298d722014-08-26 13:18:5253#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
xdaid05f51c2015-03-23 17:21:3354#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
dzhioev0298d722014-08-26 13:18:5255#include "chrome/browser/chromeos/profiles/profile_helper.h"
[email protected]363f5272013-04-23 17:21:4256#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]e4854dc2013-04-24 00:11:5157#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]931d1042013-04-05 17:50:4458#include "chromeos/chromeos_switches.h"
[email protected]4c40e062014-07-09 21:22:5159#include "chromeos/login/user_names.h"
[email protected]4d390782014-08-15 09:22:5860#include "components/user_manager/user_manager.h"
dzhioev0298d722014-08-26 13:18:5261#endif // defined(OS_CHROMEOS)
[email protected]775caec2011-09-01 17:14:3262
[email protected]6778fed2013-12-24 20:09:3763using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3964using content::BrowserThread;
65
[email protected]844a1002011-04-19 11:37:2166namespace {
[email protected]f4e706252012-01-10 17:11:3667
[email protected]844a1002011-04-19 11:37:2168// This global variable is used to check that value returned to different
69// observers is the same.
70Profile* g_created_profile;
71
[email protected]9819fd02013-08-22 10:49:3972class UnittestProfileManager : public ::ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1773 public:
[email protected]9819fd02013-08-22 10:49:3974 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
[email protected]537c1082011-12-02 02:37:1775 : ::ProfileManagerWithoutInit(user_data_dir) {}
76
77 protected:
dcheng1fd716382014-10-22 20:14:4178 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]7567484142013-07-11 17:36:0779 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5480 if (!base::CreateDirectory(file_path))
[email protected]537c1082011-12-02 02:37:1781 return NULL;
82 }
83 return new TestingProfile(file_path, NULL);
84 }
85
dcheng1fd716382014-10-22 20:14:4186 Profile* CreateProfileAsyncHelper(const base::FilePath& path,
87 Delegate* delegate) override {
[email protected]537c1082011-12-02 02:37:1788 // This is safe while all file operations are done on the FILE thread.
89 BrowserThread::PostTask(
90 BrowserThread::FILE, FROM_HERE,
[email protected]426d1c92013-12-03 20:08:5491 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:1792
93 return new TestingProfile(path, this);
94 }
95};
96
miguelg1b495542016-03-18 17:57:4697void ExpectNullProfile(base::Closure closure, Profile* profile) {
98 EXPECT_EQ(nullptr, profile);
99 closure.Run();
100}
101
102void ExpectProfileWithName(const std::string& profile_name,
103 bool incognito,
104 base::Closure closure,
105 Profile* profile) {
106 EXPECT_NE(nullptr, profile);
107 EXPECT_EQ(incognito, profile->IsOffTheRecord());
108 if (incognito)
109 profile = profile->GetOriginalProfile();
110
111 // Create a profile on the fly so the the same comparison
112 // can be used in Windows and other platforms.
113 EXPECT_EQ(base::FilePath().AppendASCII(profile_name),
114 profile->GetPath().BaseName());
115 closure.Run();
116}
117
[email protected]9819fd02013-08-22 10:49:39118} // namespace
[email protected]537c1082011-12-02 02:37:17119
[email protected]583844c2011-08-27 00:38:35120class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:47121 protected:
[email protected]f4e706252012-01-10 17:11:36122 class MockObserver {
123 public:
124 MOCK_METHOD2(OnProfileCreated,
125 void(Profile* profile, Profile::CreateStatus status));
126 };
127
[email protected]6cad5bf2011-03-10 21:21:55128 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:35129 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:09130 }
131
dchenge1bc7982014-10-30 00:32:40132 void SetUp() override {
[email protected]3a305db2011-04-12 13:40:53133 // Create a new temporary directory, and store the path
134 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22135 TestingBrowserProcess::GetGlobal()->SetProfileManager(
[email protected]9819fd02013-08-22 10:49:39136 new UnittestProfileManager(temp_dir_.path()));
[email protected]f89b50f2012-08-16 21:36:50137
[email protected]537c1082011-12-02 02:37:17138#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43139 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
dzhioev0298d722014-08-26 13:18:52140 cl->AppendSwitch(switches::kTestType);
xdaid05f51c2015-03-23 17:21:33141 chromeos::WallpaperManager::Initialize();
[email protected]537c1082011-12-02 02:37:17142#endif
initial.commit09911bf2008-07-26 23:55:29143 }
[email protected]81ad7f4a2011-03-16 01:33:29144
dchenge1bc7982014-10-30 00:32:40145 void TearDown() override {
[email protected]c494d082013-01-04 20:41:22146 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]37ac95b2013-07-23 23:39:35147 base::RunLoop().RunUntilIdle();
xdaid05f51c2015-03-23 17:21:33148#if defined(OS_CHROMEOS)
149 chromeos::WallpaperManager::Shutdown();
150#endif
initial.commit09911bf2008-07-26 23:55:29151 }
[email protected]f0a51fb52009-03-05 12:46:38152
[email protected]1c62b2f2013-06-28 00:15:00153 // Helper function to create a profile with |name| for a profile |manager|.
154 void CreateProfileAsync(ProfileManager* manager,
155 const std::string& name,
[email protected]d20d0432014-06-12 17:14:05156 bool is_supervised,
[email protected]1c62b2f2013-06-28 00:15:00157 MockObserver* mock_observer) {
158 manager->CreateProfileAsync(
159 temp_dir_.path().AppendASCII(name),
160 base::Bind(&MockObserver::OnProfileCreated,
161 base::Unretained(mock_observer)),
[email protected]6778fed2013-12-24 20:09:37162 base::UTF8ToUTF16(name),
lwchkg1f62d242015-10-29 00:50:43163 profiles::GetDefaultAvatarIconUrl(0),
[email protected]d20d0432014-06-12 17:14:05164 is_supervised ? "Dummy ID" : std::string());
[email protected]1c62b2f2013-06-28 00:15:00165 }
166
lwchkg498e92492016-04-23 11:04:12167 // Helper function to add a profile with |profile_name| to |profile_manager|'s
168 // ProfileAttributesStorage, and return the profile created.
169 Profile* AddProfileToStorage(ProfileManager* profile_manager,
170 const std::string& path_suffix,
171 const base::string16& profile_name) {
172 ProfileAttributesStorage& storage =
173 profile_manager->GetProfileAttributesStorage();
174 size_t num_profiles = storage.GetNumberOfProfiles();
[email protected]3f130532014-02-26 20:39:24175 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
lwchkg498e92492016-04-23 11:04:12176 storage.AddProfile(path, profile_name, std::string(), base::string16(), 0,
177 std::string());
178 EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:24179 return profile_manager->GetProfile(path);
180 }
181
[email protected]6f96cbcb2011-11-04 02:26:07182#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52183 // Helper function to register an user with id |user_id| and create profile
184 // with a correct path.
185 void RegisterUser(const std::string& user_id) {
186 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
187 const std::string user_id_hash =
188 profile_helper->GetUserIdHashByUserIdForTesting(user_id);
189 user_manager::UserManager::Get()->UserLoggedIn(
alemate3ffbde6f2015-11-03 02:02:55190 AccountId::FromUserEmail(user_id), user_id_hash, false);
dzhioev0298d722014-08-26 13:18:52191 g_browser_process->profile_manager()->GetProfile(
192 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
193 }
194
[email protected]e4854dc2013-04-24 00:11:51195 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
[email protected]363f5272013-04-23 17:21:42196 chromeos::ScopedTestCrosSettings test_cros_settings_;
[email protected]6f96cbcb2011-11-04 02:26:07197#endif
198
[email protected]3a305db2011-04-12 13:40:53199 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23200 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35201 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35202
203 content::TestBrowserThreadBundle thread_bundle_;
[email protected]363f5272013-04-23 17:21:42204
205#if defined(OS_CHROMEOS)
206 chromeos::ScopedTestUserManager test_user_manager_;
207#endif
dzhioev0298d722014-08-26 13:18:52208
209 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
initial.commit09911bf2008-07-26 23:55:29210};
211
[email protected]844a1002011-04-19 11:37:21212TEST_F(ProfileManagerTest, GetProfile) {
[email protected]650b2d52013-02-10 03:41:45213 base::FilePath dest_path = temp_dir_.path();
[email protected]f7011fcb2009-01-28 21:54:32214 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29215
[email protected]881cb0b62011-12-13 20:39:54216 ProfileManager* profile_manager = g_browser_process->profile_manager();
217
initial.commit09911bf2008-07-26 23:55:29218 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36219 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21220 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29221
[email protected]844a1002011-04-19 11:37:21222 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54223 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29224}
[email protected]55474b572009-04-14 22:05:33225
[email protected]943aed5542010-06-11 15:57:08226TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45227 base::FilePath expected_default =
228 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54229 EXPECT_EQ(
230 expected_default.value(),
231 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08232}
233
skuhne24bb24432014-09-08 19:31:47234MATCHER(NotFail, "Profile creation failure status is not reported.") {
235 return arg == Profile::CREATE_STATUS_CREATED ||
236 arg == Profile::CREATE_STATUS_INITIALIZED;
237}
238
239MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
240 if (!g_created_profile)
241 g_created_profile = arg;
242 return arg != NULL && arg == g_created_profile;
243}
244
[email protected]943aed5542010-06-11 15:57:08245#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52246
[email protected]943aed5542010-06-11 15:57:08247// This functionality only exists on Chrome OS.
248TEST_F(ProfileManagerTest, LoggedInProfileDir) {
[email protected]650b2d52013-02-10 03:41:45249 base::FilePath expected_default =
250 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54251 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08252 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54253 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08254
dzhioev0298d722014-08-26 13:18:52255 const char kTestUserName[] = "[email protected]";
alemate3ffbde6f2015-11-03 02:02:55256 const AccountId test_account_id(AccountId::FromUserEmail(kTestUserName));
merkulova793f3022015-02-04 10:18:30257 chromeos::FakeChromeUserManager* user_manager =
258 new chromeos::FakeChromeUserManager();
dzhioev0298d722014-08-26 13:18:52259 chromeos::ScopedUserManagerEnabler enabler(user_manager);
260
alemate3ffbde6f2015-11-03 02:02:55261 const user_manager::User* active_user =
262 user_manager->AddUser(test_account_id);
263 user_manager->LoginUser(test_account_id);
264 user_manager->SwitchActiveUser(test_account_id);
dzhioev0298d722014-08-26 13:18:52265
[email protected]2fda9972014-07-23 14:51:59266 profile_manager->Observe(
267 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
268 content::NotificationService::AllSources(),
269 content::Details<const user_manager::User>(active_user));
dzhioev0298d722014-08-26 13:18:52270 base::FilePath expected_logged_in(
271 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
[email protected]943aed5542010-06-11 15:57:08272 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54273 profile_manager->GetInitialProfileDir().value());
[email protected]3a305db2011-04-12 13:40:53274 VLOG(1) << temp_dir_.path().Append(
[email protected]881cb0b62011-12-13 20:39:54275 profile_manager->GetInitialProfileDir()).value();
[email protected]943aed5542010-06-11 15:57:08276}
277
278#endif
279
[email protected]52d69b882010-06-17 17:35:51280TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
[email protected]650b2d52013-02-10 03:41:45281 base::FilePath dest_path1 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33282 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47283
[email protected]650b2d52013-02-10 03:41:45284 base::FilePath dest_path2 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33285 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
286
[email protected]881cb0b62011-12-13 20:39:54287 ProfileManager* profile_manager = g_browser_process->profile_manager();
288
[email protected]55474b572009-04-14 22:05:33289 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17290 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54291 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21292 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33293
[email protected]537c1082011-12-02 02:37:17294 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54295 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21296 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33297
298 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31299 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04300 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
301 profile1, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17302 profile1->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30303 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile1));
[email protected]537c1082011-12-02 02:37:17304 profile2->CreateBookmarkModel(true);
pke3e0d5c92016-08-08 09:07:30305 EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile2));
[email protected]608e7e02013-07-24 12:23:31306 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
sdefresnee9ea3c22015-01-10 10:10:04307 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
308 profile2, ServiceAccessType::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55309
310 // Make sure any pending tasks run before we destroy the profiles.
[email protected]37ac95b2013-07-23 23:39:35311 base::RunLoop().RunUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55312
[email protected]c494d082013-01-04 20:41:22313 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]6cad5bf2011-03-10 21:21:55314
[email protected]237e6d02010-11-08 21:45:42315 // Make sure history cleans up correctly.
[email protected]37ac95b2013-07-23 23:39:35316 base::RunLoop().RunUntilIdle();
[email protected]55474b572009-04-14 22:05:33317}
[email protected]844a1002011-04-19 11:37:21318
miguelg1b495542016-03-18 17:57:46319TEST_F(ProfileManagerTest, LoadNonExistingProfile) {
320 const std::string profile_name = "NonExistingProfile";
321 base::RunLoop run_loop_1;
322 base::RunLoop run_loop_2;
323
324 ProfileManager* profile_manager = g_browser_process->profile_manager();
325 profile_manager->LoadProfile(
326 profile_name, false /* incognito */,
327 base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure()));
328 run_loop_1.Run();
329
330 profile_manager->LoadProfile(
331 profile_name, true /* incognito */,
332 base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure()));
333 run_loop_2.Run();
334}
335
336TEST_F(ProfileManagerTest, LoadExistingProfile) {
337 const std::string profile_name = "MyProfile";
338 const std::string other_name = "SomeOtherProfile";
339 MockObserver mock_observer1;
340 EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail()))
341 .Times(testing::AtLeast(1));
342
343 ProfileManager* profile_manager = g_browser_process->profile_manager();
344 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
345
346 // Make sure a real profile is created before continuing.
347 base::RunLoop().RunUntilIdle();
348
349 base::RunLoop load_profile;
350 bool incognito = false;
351 profile_manager->LoadProfile(
352 profile_name, incognito,
353 base::Bind(&ExpectProfileWithName, profile_name, incognito,
354 load_profile.QuitClosure()));
355 load_profile.Run();
356
357 base::RunLoop load_profile_incognito;
358 incognito = true;
359 profile_manager->LoadProfile(
360 profile_name, incognito,
361 base::Bind(&ExpectProfileWithName, profile_name, incognito,
362 load_profile_incognito.QuitClosure()));
363 load_profile_incognito.Run();
364
365 // Loading some other non existing profile should still return null.
366 base::RunLoop load_other_profile;
367 profile_manager->LoadProfile(
368 other_name, false,
369 base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure()));
370 load_other_profile.Run();
371}
372
[email protected]844a1002011-04-19 11:37:21373TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
[email protected]844a1002011-04-19 11:37:21374 g_created_profile = NULL;
375
376 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11377 EXPECT_CALL(mock_observer1, OnProfileCreated(
378 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21379 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11380 EXPECT_CALL(mock_observer2, OnProfileCreated(
381 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21382 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11383 EXPECT_CALL(mock_observer3, OnProfileCreated(
384 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21385
[email protected]881cb0b62011-12-13 20:39:54386 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00387 const std::string profile_name = "New Profile";
[email protected]5ddfade2014-02-03 10:24:53388 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
389 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
390 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21391
[email protected]37ac95b2013-07-23 23:39:35392 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21393}
394
395TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00396 const std::string profile_name1 = "New Profile 1";
397 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21398
399 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11400 EXPECT_CALL(mock_observer, OnProfileCreated(
401 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21402
[email protected]881cb0b62011-12-13 20:39:54403 ProfileManager* profile_manager = g_browser_process->profile_manager();
404
[email protected]5ddfade2014-02-03 10:24:53405 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
406 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]844a1002011-04-19 11:37:21407
[email protected]37ac95b2013-07-23 23:39:35408 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21409}
[email protected]279170832011-10-12 23:38:03410
[email protected]5ddfade2014-02-03 10:24:53411TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
[email protected]d20d0432014-06-12 17:14:05412 std::string name = "0 Supervised Profile";
[email protected]5ddfade2014-02-03 10:24:53413
414 MockObserver mock_observer;
415 EXPECT_CALL(mock_observer, OnProfileCreated(
416 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
417
418 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12419 ProfileAttributesStorage& storage =
420 profile_manager->GetProfileAttributesStorage();
421 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53422
423 CreateProfileAsync(profile_manager, name, true, &mock_observer);
424 base::RunLoop().RunUntilIdle();
425
lwchkg498e92492016-04-23 11:04:12426 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05427 // Supervised profiles should start out omitted from the profile list.
lwchkg498e92492016-04-23 11:04:12428 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53429
[email protected]d20d0432014-06-12 17:14:05430 name = "1 Regular Profile";
[email protected]5ddfade2014-02-03 10:24:53431 CreateProfileAsync(profile_manager, name, false, &mock_observer);
432 base::RunLoop().RunUntilIdle();
433
lwchkg498e92492016-04-23 11:04:12434 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05435 // Non-supervised profiles should be included in the profile list.
lwchkg498e92492016-04-23 11:04:12436 EXPECT_FALSE(storage.GetAllProfilesAttributesSortedByName()[1u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53437}
438
lwchkg498e92492016-04-23 11:04:12439TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) {
[email protected]5ddfade2014-02-03 10:24:53440 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12441 ProfileAttributesStorage& storage =
442 profile_manager->GetProfileAttributesStorage();
443 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]5ddfade2014-02-03 10:24:53444
[email protected]d20d0432014-06-12 17:14:05445 const base::FilePath supervised_path =
446 temp_dir_.path().AppendASCII("Supervised");
447 TestingProfile* supervised_profile =
448 new TestingProfile(supervised_path, NULL);
449 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
[email protected]5ddfade2014-02-03 10:24:53450
451 // RegisterTestingProfile adds the profile to the cache and takes ownership.
[email protected]d20d0432014-06-12 17:14:05452 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
lwchkg498e92492016-04-23 11:04:12453 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
454 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53455
[email protected]d20d0432014-06-12 17:14:05456 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII(
457 "Non-Supervised");
458 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
459 NULL);
460 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
[email protected]5ddfade2014-02-03 10:24:53461
lwchkg498e92492016-04-23 11:04:12462 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
463 ProfileAttributesEntry* entry;
464 ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry));
465 EXPECT_TRUE(entry->IsOmitted());
466
467 ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry));
468 EXPECT_FALSE(entry->IsOmitted());
[email protected]5ddfade2014-02-03 10:24:53469}
470
[email protected]4251165a2013-07-17 04:33:40471TEST_F(ProfileManagerTest, GetGuestProfilePath) {
472 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
473 base::FilePath expected_path = temp_dir_.path();
474 expected_path = expected_path.Append(chrome::kGuestProfileDir);
475 EXPECT_EQ(expected_path, guest_path);
476}
477
mlermanb8df6e82015-01-23 19:55:34478TEST_F(ProfileManagerTest, GetSystemProfilePath) {
479 base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath();
480 base::FilePath expected_path = temp_dir_.path();
481 expected_path = expected_path.Append(chrome::kSystemProfileDir);
482 EXPECT_EQ(expected_path, system_profile_path);
483}
484
[email protected]0ff6198e2014-01-08 22:45:00485class UnittestGuestProfileManager : public UnittestProfileManager {
486 public:
487 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
488 : UnittestProfileManager(user_data_dir) {}
489
490 protected:
dcheng1fd716382014-10-22 20:14:41491 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
[email protected]0ff6198e2014-01-08 22:45:00492 TestingProfile::Builder builder;
[email protected]0ff6198e2014-01-08 22:45:00493 builder.SetGuestSession();
[email protected]d22f7c932014-02-20 22:48:58494 builder.SetPath(file_path);
495 TestingProfile* testing_profile = builder.Build().release();
[email protected]0ff6198e2014-01-08 22:45:00496 return testing_profile;
497 }
498};
499
[email protected]9b6e10c2014-05-20 19:12:28500class ProfileManagerGuestTest : public ProfileManagerTest {
[email protected]0ff6198e2014-01-08 22:45:00501 protected:
dchenge1bc7982014-10-30 00:32:40502 void SetUp() override {
[email protected]0ff6198e2014-01-08 22:45:00503 // Create a new temporary directory, and store the path
504 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
505 TestingBrowserProcess::GetGlobal()->SetProfileManager(
506 new UnittestGuestProfileManager(temp_dir_.path()));
507
[email protected]2a92f2182014-03-25 00:47:59508#if defined(OS_CHROMEOS)
avi556c05022014-12-22 23:31:43509 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
[email protected]9b6e10c2014-05-20 19:12:28510 // This switch is needed to skip non-test specific behavior in
511 // ProfileManager (accessing DBusThreadManager).
[email protected]0ff6198e2014-01-08 22:45:00512 cl->AppendSwitch(switches::kTestType);
[email protected]9b6e10c2014-05-20 19:12:28513
[email protected]0ff6198e2014-01-08 22:45:00514 cl->AppendSwitch(chromeos::switches::kGuestSession);
515 cl->AppendSwitch(::switches::kIncognito);
516
xdaid05f51c2015-03-23 17:21:33517 chromeos::WallpaperManager::Initialize();
dzhioev0298d722014-08-26 13:18:52518 RegisterUser(chromeos::login::kGuestUserName);
[email protected]2a92f2182014-03-25 00:47:59519#endif
[email protected]0ff6198e2014-01-08 22:45:00520 }
521};
522
[email protected]9b6e10c2014-05-20 19:12:28523TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
[email protected]2a92f2182014-03-25 00:47:59524 ProfileManager* profile_manager = g_browser_process->profile_manager();
525 ASSERT_TRUE(profile_manager);
526
527 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
528 ASSERT_TRUE(profile);
529 EXPECT_TRUE(profile->IsOffTheRecord());
530}
531
532#if defined(OS_CHROMEOS)
[email protected]9b6e10c2014-05-20 19:12:28533TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
[email protected]0ff6198e2014-01-08 22:45:00534 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
535 EXPECT_TRUE(primary_profile->IsOffTheRecord());
536
537 Profile* active_profile = ProfileManager::GetActiveUserProfile();
538 EXPECT_TRUE(active_profile->IsOffTheRecord());
539
540 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
[email protected]d22f7c932014-02-20 22:48:58541
542 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
543 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
544
545 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
[email protected]0ff6198e2014-01-08 22:45:00546}
547#endif
548
[email protected]279170832011-10-12 23:38:03549TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54550 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12551 ProfileAttributesStorage& storage =
552 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27553 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]012d1312014-07-17 06:37:40554 new base::FundamentalValue(true));
[email protected]714bf1d2012-11-30 01:35:27555
556 // Setting a pref which is not applicable to a system (i.e., Android in this
557 // case) does not necessarily create it. Don't bother continuing with the
558 // test if this pref doesn't exist because it will not load the profiles if
559 // it cannot verify that the pref for background mode is enabled.
560 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
561 return;
[email protected]279170832011-10-12 23:38:03562
lwchkg498e92492016-04-23 11:04:12563 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
564
565 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
566 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string());
567 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
568 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string());
569 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"),
570 ASCIIToUTF16("name_3"), "34567", base::string16(), 0, std::string());
571
572 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
573
574 std::vector<ProfileAttributesEntry*> entries =
575 storage.GetAllProfilesAttributes();
576 entries[0u]->SetBackgroundStatus(true);
577 entries[2u]->SetBackgroundStatus(true);
[email protected]279170832011-10-12 23:38:03578
[email protected]881cb0b62011-12-13 20:39:54579 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03580
[email protected]881cb0b62011-12-13 20:39:54581 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
582}
583
[email protected]714bf1d2012-11-30 01:35:27584TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
585 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12586 ProfileAttributesStorage& storage =
587 profile_manager->GetProfileAttributesStorage();
[email protected]714bf1d2012-11-30 01:35:27588 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]012d1312014-07-17 06:37:40589 new base::FundamentalValue(false));
[email protected]714bf1d2012-11-30 01:35:27590
lwchkg498e92492016-04-23 11:04:12591 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
592
593 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"),
594 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string());
595 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"),
596 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string());
597
598 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
599
600 std::vector<ProfileAttributesEntry*> entries =
601 storage.GetAllProfilesAttributes();
602 entries[0u]->SetBackgroundStatus(false);
603 entries[1u]->SetBackgroundStatus(true);
[email protected]714bf1d2012-11-30 01:35:27604
605 profile_manager->AutoloadProfiles();
606
607 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
608}
609
[email protected]881cb0b62011-12-13 20:39:54610TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
[email protected]650b2d52013-02-10 03:41:45611 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54612 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
613
614 ProfileManager* profile_manager = g_browser_process->profile_manager();
615
616 Profile* profile;
617
618 // Successfully create the profile
619 profile = profile_manager->GetProfile(dest_path);
620 ASSERT_TRUE(profile);
621
622 // Check that the profile name is non empty
623 std::string profile_name =
624 profile->GetPrefs()->GetString(prefs::kProfileName);
625 EXPECT_FALSE(profile_name.empty());
626
627 // Check that the profile avatar index is valid
628 size_t avatar_index =
629 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
[email protected]c3e559772014-04-09 04:02:54630 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
[email protected]881cb0b62011-12-13 20:39:54631 avatar_index));
632}
633
634// Tests that a new profile's entry in the profile info cache is setup with the
635// same values that are in the profile prefs.
636TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
[email protected]650b2d52013-02-10 03:41:45637 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54638 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
639
640 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]881cb0b62011-12-13 20:39:54641
642 // Successfully create the profile
643 Profile* profile = profile_manager->GetProfile(dest_path);
644 ASSERT_TRUE(profile);
645
646 std::string profile_name =
647 profile->GetPrefs()->GetString(prefs::kProfileName);
648 size_t avatar_index =
649 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
650
lwchkg498e92492016-04-23 11:04:12651 ProfileAttributesEntry* entry;
652 ASSERT_TRUE(profile_manager->GetProfileAttributesStorage().
653 GetProfileAttributesWithPath(dest_path, &entry));
[email protected]881cb0b62011-12-13 20:39:54654
655 // Check if the profile prefs are the same as the cache prefs
lwchkg498e92492016-04-23 11:04:12656 EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName()));
657 EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex());
[email protected]279170832011-10-12 23:38:03658}
[email protected]52edf812012-01-12 11:48:46659
[email protected]13720532013-05-30 12:30:59660TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
661 ProfileManager* profile_manager = g_browser_process->profile_manager();
662 ASSERT_TRUE(profile_manager);
663
dzhioev0298d722014-08-26 13:18:52664#if defined(OS_CHROMEOS)
665 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that
666 // is forced to be incognito. That's why we need to create at least one user
667 // to get a regular profile.
668 RegisterUser("[email protected]");
669#endif
670
[email protected]13720532013-05-30 12:30:59671 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
672 ASSERT_TRUE(profile);
673 EXPECT_FALSE(profile->IsOffTheRecord());
674 PrefService* prefs = profile->GetPrefs();
675 EXPECT_EQ(IncognitoModePrefs::ENABLED,
676 IncognitoModePrefs::GetAvailability(prefs));
677
[email protected]13720532013-05-30 12:30:59678 ASSERT_TRUE(profile->GetOffTheRecordProfile());
679
680 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
681 EXPECT_FALSE(
682 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
683
684 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
685 // incognito mode is forced.
686 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
687 EXPECT_TRUE(
688 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
689}
690
[email protected]c849fab2012-03-29 16:51:41691#if !defined(OS_ANDROID)
692// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46693TEST_F(ProfileManagerTest, LastOpenedProfiles) {
[email protected]650b2d52013-02-10 03:41:45694 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46695 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
696
[email protected]650b2d52013-02-10 03:41:45697 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46698 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
699
700 ProfileManager* profile_manager = g_browser_process->profile_manager();
701
702 // Successfully create the profiles.
703 TestingProfile* profile1 =
704 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
705 ASSERT_TRUE(profile1);
706
707 TestingProfile* profile2 =
708 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
709 ASSERT_TRUE(profile2);
710
711 std::vector<Profile*> last_opened_profiles =
712 profile_manager->GetLastOpenedProfiles();
713 ASSERT_EQ(0U, last_opened_profiles.size());
714
715 // Create a browser for profile1.
scottmg851949002016-02-09 20:09:44716 Browser::CreateParams profile1_params(profile1);
dcheng6e25ed32016-04-08 03:30:03717 std::unique_ptr<Browser> browser1a(
[email protected]c5f3e1842013-02-20 17:10:41718 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46719
720 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
721 ASSERT_EQ(1U, last_opened_profiles.size());
722 EXPECT_EQ(profile1, last_opened_profiles[0]);
723
724 // And for profile2.
scottmg851949002016-02-09 20:09:44725 Browser::CreateParams profile2_params(profile2);
dcheng6e25ed32016-04-08 03:30:03726 std::unique_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41727 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46728
729 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
730 ASSERT_EQ(2U, last_opened_profiles.size());
731 EXPECT_EQ(profile1, last_opened_profiles[0]);
732 EXPECT_EQ(profile2, last_opened_profiles[1]);
733
734 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03735 std::unique_ptr<Browser> browser1b(
[email protected]c5f3e1842013-02-20 17:10:41736 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46737 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
738 ASSERT_EQ(2U, last_opened_profiles.size());
739 EXPECT_EQ(profile1, last_opened_profiles[0]);
740 EXPECT_EQ(profile2, last_opened_profiles[1]);
741
742 // Close the browsers.
743 browser1a.reset();
744 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
745 ASSERT_EQ(2U, last_opened_profiles.size());
746 EXPECT_EQ(profile1, last_opened_profiles[0]);
747 EXPECT_EQ(profile2, last_opened_profiles[1]);
748
749 browser1b.reset();
750 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
751 ASSERT_EQ(1U, last_opened_profiles.size());
752 EXPECT_EQ(profile2, last_opened_profiles[0]);
753
754 browser2.reset();
755 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
756 ASSERT_EQ(0U, last_opened_profiles.size());
757}
758
759TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
[email protected]650b2d52013-02-10 03:41:45760 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46761 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
762
[email protected]650b2d52013-02-10 03:41:45763 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46764 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
765
766 ProfileManager* profile_manager = g_browser_process->profile_manager();
767
768 // Successfully create the profiles.
769 TestingProfile* profile1 =
770 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
771 ASSERT_TRUE(profile1);
772
773 TestingProfile* profile2 =
774 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
775 ASSERT_TRUE(profile2);
776
777 // Create a browser for profile1.
scottmg851949002016-02-09 20:09:44778 Browser::CreateParams profile1_params(profile1);
dcheng6e25ed32016-04-08 03:30:03779 std::unique_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41780 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46781
782 // And for profile2.
scottmg851949002016-02-09 20:09:44783 Browser::CreateParams profile2_params(profile2);
dcheng6e25ed32016-04-08 03:30:03784 std::unique_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41785 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46786
787 std::vector<Profile*> last_opened_profiles =
788 profile_manager->GetLastOpenedProfiles();
789 ASSERT_EQ(2U, last_opened_profiles.size());
790 EXPECT_EQ(profile1, last_opened_profiles[0]);
791 EXPECT_EQ(profile2, last_opened_profiles[1]);
792
793 // Simulate a shutdown.
794 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30795 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:46796 content::NotificationService::AllSources(),
797 content::NotificationService::NoDetails());
798
799 // Even if the browsers are destructed during shutdown, the profiles stay
800 // open.
801 browser1.reset();
802 browser2.reset();
803
804 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
805 ASSERT_EQ(2U, last_opened_profiles.size());
806 EXPECT_EQ(profile1, last_opened_profiles[0]);
807 EXPECT_EQ(profile2, last_opened_profiles[1]);
808}
[email protected]41276342012-02-06 10:49:31809
810TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
[email protected]650b2d52013-02-10 03:41:45811 base::FilePath dest_path1 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31812 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]650b2d52013-02-10 03:41:45813 base::FilePath dest_path2 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31814 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
815
816 ProfileManager* profile_manager = g_browser_process->profile_manager();
817
818 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:06819 TestingProfile* profile1 =
820 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:31821 ASSERT_TRUE(profile1);
822
[email protected]41276342012-02-06 10:49:31823 std::vector<Profile*> last_opened_profiles =
824 profile_manager->GetLastOpenedProfiles();
825 ASSERT_EQ(0U, last_opened_profiles.size());
826
827 // Create a browser for profile1.
scottmg851949002016-02-09 20:09:44828 Browser::CreateParams profile1_params(profile1);
dcheng6e25ed32016-04-08 03:30:03829 std::unique_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41830 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:31831
832 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
833 ASSERT_EQ(1U, last_opened_profiles.size());
834 EXPECT_EQ(profile1, last_opened_profiles[0]);
835
836 // And for profile2.
scottmg851949002016-02-09 20:09:44837 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile());
dcheng6e25ed32016-04-08 03:30:03838 std::unique_ptr<Browser> browser2a(
[email protected]c5f3e1842013-02-20 17:10:41839 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31840
841 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
842 ASSERT_EQ(1U, last_opened_profiles.size());
843 EXPECT_EQ(profile1, last_opened_profiles[0]);
844
845 // Adding more browsers doesn't change anything.
dcheng6e25ed32016-04-08 03:30:03846 std::unique_ptr<Browser> browser2b(
[email protected]c5f3e1842013-02-20 17:10:41847 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31848 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
849 ASSERT_EQ(1U, last_opened_profiles.size());
850 EXPECT_EQ(profile1, last_opened_profiles[0]);
851
852 // Close the browsers.
853 browser2a.reset();
854 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
855 ASSERT_EQ(1U, last_opened_profiles.size());
856 EXPECT_EQ(profile1, last_opened_profiles[0]);
857
858 browser2b.reset();
859 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
860 ASSERT_EQ(1U, last_opened_profiles.size());
861 EXPECT_EQ(profile1, last_opened_profiles[0]);
862
863 browser1.reset();
864 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
865 ASSERT_EQ(0U, last_opened_profiles.size());
866}
[email protected]c849fab2012-03-29 16:51:41867#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:00868
869#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
870// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:48871TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
872 base::FilePath dest_path = temp_dir_.path();
873 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
874
875 ProfileManager* profile_manager = g_browser_process->profile_manager();
876
877 TestingProfile* profile =
878 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
879 ASSERT_TRUE(profile);
880 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
881
882 // Here the last used profile is still the "Default" profile.
883 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
884 EXPECT_NE(profile, last_used_profile);
885
[email protected]79fd6b72013-10-22 16:39:27886 // Create a browser for the profile.
scottmg851949002016-02-09 20:09:44887 Browser::CreateParams profile_params(profile);
dcheng6e25ed32016-04-08 03:30:03888 std::unique_ptr<Browser> browser(
[email protected]8d9243a2013-10-21 17:11:48889 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
890 last_used_profile = profile_manager->GetLastUsedProfile();
891 EXPECT_NE(profile, last_used_profile);
892
893 // Close the browser.
894 browser.reset();
895 last_used_profile = profile_manager->GetLastUsedProfile();
896 EXPECT_NE(profile, last_used_profile);
897}
898
899TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
900 base::FilePath dest_path1 = temp_dir_.path();
901 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
902
903 base::FilePath dest_path2 = temp_dir_.path();
[email protected]79fd6b72013-10-22 16:39:27904 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
905
906 base::FilePath dest_path3 = temp_dir_.path();
907 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:48908
909 ProfileManager* profile_manager = g_browser_process->profile_manager();
910
911 // Successfully create the profiles.
912 TestingProfile* normal_profile =
913 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
914 ASSERT_TRUE(normal_profile);
915
916 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:27917 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:48918 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:27919 ASSERT_TRUE(ephemeral_profile1);
920 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
921 true);
922
923 // Add second ephemeral profile but don't mark it as such yet.
924 TestingProfile* ephemeral_profile2 =
925 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
926 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:48927
928 // Create a browser for profile1.
scottmg851949002016-02-09 20:09:44929 Browser::CreateParams profile1_params(normal_profile);
dcheng6e25ed32016-04-08 03:30:03930 std::unique_ptr<Browser> browser1(
[email protected]8d9243a2013-10-21 17:11:48931 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
932
[email protected]79fd6b72013-10-22 16:39:27933 // Create browsers for the ephemeral profile.
scottmg851949002016-02-09 20:09:44934 Browser::CreateParams profile2_params(ephemeral_profile1);
dcheng6e25ed32016-04-08 03:30:03935 std::unique_ptr<Browser> browser2(
[email protected]8d9243a2013-10-21 17:11:48936 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
937
scottmg851949002016-02-09 20:09:44938 Browser::CreateParams profile3_params(ephemeral_profile2);
dcheng6e25ed32016-04-08 03:30:03939 std::unique_ptr<Browser> browser3(
[email protected]79fd6b72013-10-22 16:39:27940 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
941
[email protected]8d9243a2013-10-21 17:11:48942 std::vector<Profile*> last_opened_profiles =
943 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:27944 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:48945 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:27946 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
947
948 // Mark the second profile ephemeral.
949 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
950 true);
[email protected]8d9243a2013-10-21 17:11:48951
952 // Simulate a shutdown.
953 content::NotificationService::current()->Notify(
954 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
955 content::NotificationService::AllSources(),
956 content::NotificationService::NoDetails());
957 browser1.reset();
958 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:27959 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:48960
961 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
962 ASSERT_EQ(1U, last_opened_profiles.size());
963 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
964}
965
bauerb8b022182015-05-13 09:18:39966TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
967 // Create two profiles, one of them ephemeral.
968 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:12969 ProfileAttributesStorage& storage =
970 profile_manager->GetProfileAttributesStorage();
971 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:39972
973 const std::string profile_name1 = "Homer";
lwchkg498e92492016-04-23 11:04:12974 base::FilePath path1 =
975 profile_manager->user_data_dir().AppendASCII(profile_name1);
976 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(),
977 base::UTF8ToUTF16(profile_name1), 0, std::string());
978 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:39979 ASSERT_TRUE(base::CreateDirectory(path1));
980
981 const std::string profile_name2 = "Marge";
lwchkg498e92492016-04-23 11:04:12982 base::FilePath path2 =
983 profile_manager->user_data_dir().AppendASCII(profile_name2);
984 storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(),
985 base::UTF8ToUTF16(profile_name2), 0, std::string());
986 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:39987 ASSERT_TRUE(base::CreateDirectory(path2));
988
989 // Set the active profile.
990 PrefService* local_state = g_browser_process->local_state();
991 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
992
993 profile_manager->CleanUpEphemeralProfiles();
994 base::RunLoop().RunUntilIdle();
995
996 // The ephemeral profile should be deleted, and the last used profile set to
997 // the other one.
998 EXPECT_FALSE(base::DirectoryExists(path1));
999 EXPECT_TRUE(base::DirectoryExists(path2));
1000 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121001 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391002
1003 // Mark the remaining profile ephemeral and clean up.
lwchkg498e92492016-04-23 11:04:121004 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true);
bauerb8b022182015-05-13 09:18:391005 profile_manager->CleanUpEphemeralProfiles();
1006 base::RunLoop().RunUntilIdle();
1007
1008 // The profile should be deleted, and the last used profile set to a new one.
1009 EXPECT_FALSE(base::DirectoryExists(path2));
lwchkg498e92492016-04-23 11:04:121010 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
bauerb8b022182015-05-13 09:18:391011 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1012}
1013
[email protected]1c62b2f2013-06-28 00:15:001014TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
1015 ProfileManager* profile_manager = g_browser_process->profile_manager();
1016 ASSERT_TRUE(profile_manager);
1017
1018 // Create and load two profiles.
1019 const std::string profile_name1 = "New Profile 1";
1020 const std::string profile_name2 = "New Profile 2";
[email protected]3f130532014-02-26 20:39:241021 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1022 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001023
1024 MockObserver mock_observer;
1025 EXPECT_CALL(mock_observer, OnProfileCreated(
1026 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
1027
[email protected]5ddfade2014-02-03 10:24:531028 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1029 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:351030 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001031
1032 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121033 EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage().
1034 GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001035
1036 // Set the active profile.
1037 PrefService* local_state = g_browser_process->local_state();
1038 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1039
1040 // Delete the active profile.
1041 profile_manager->ScheduleProfileForDeletion(dest_path1,
1042 ProfileManager::CreateCallback());
1043 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:351044 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001045
1046 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1047 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1048}
[email protected]3f130532014-02-26 20:39:241049
[email protected]602b5a29f2014-07-18 15:39:331050TEST_F(ProfileManagerTest, LastProfileDeleted) {
1051 ProfileManager* profile_manager = g_browser_process->profile_manager();
1052 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121053 ProfileAttributesStorage& storage =
1054 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331055
1056 // Create and load a profile.
1057 const std::string profile_name1 = "New Profile 1";
1058 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1059
1060 MockObserver mock_observer;
1061 EXPECT_CALL(mock_observer, OnProfileCreated(
1062 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
1063
1064 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1065 base::RunLoop().RunUntilIdle();
1066
1067 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121068 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331069
1070 // Set it as the active profile.
1071 PrefService* local_state = g_browser_process->local_state();
1072 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1073
1074 // Delete the active profile.
1075 profile_manager->ScheduleProfileForDeletion(dest_path1,
1076 ProfileManager::CreateCallback());
1077 // Spin the message loop so that all the callbacks can finish running.
1078 base::RunLoop().RunUntilIdle();
1079
1080 // A new profile should have been created
1081 const std::string profile_name2 = "Profile 1";
1082 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1083
1084 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1085 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
lwchkg498e92492016-04-23 11:04:121086 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331087}
1088
1089TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1090 ProfileManager* profile_manager = g_browser_process->profile_manager();
1091 ASSERT_TRUE(profile_manager);
lwchkg498e92492016-04-23 11:04:121092 ProfileAttributesStorage& storage =
1093 profile_manager->GetProfileAttributesStorage();
[email protected]602b5a29f2014-07-18 15:39:331094
1095 // Create and load a profile.
1096 const std::string profile_name1 = "New Profile 1";
1097 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1098
1099 MockObserver mock_observer;
1100 EXPECT_CALL(mock_observer, OnProfileCreated(
1101 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1102
1103 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1104 base::RunLoop().RunUntilIdle();
1105
1106 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121107 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331108
1109 // Create the profile and register it.
1110 const std::string guest_profile_name =
1111 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1112
1113 TestingProfile::Builder builder;
1114 builder.SetGuestSession();
1115 builder.SetPath(ProfileManager::GetGuestProfilePath());
1116 TestingProfile* guest_profile = builder.Build().release();
1117 guest_profile->set_profile_name(guest_profile_name);
1118 // Registering the profile passes ownership to the ProfileManager.
1119 profile_manager->RegisterTestingProfile(guest_profile, false, false);
1120
lwchkg498e92492016-04-23 11:04:121121 // The Guest profile does not get added to the ProfileAttributesStorage.
[email protected]602b5a29f2014-07-18 15:39:331122 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121123 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]602b5a29f2014-07-18 15:39:331124
1125 // Set the Guest profile as the active profile.
1126 PrefService* local_state = g_browser_process->local_state();
1127 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1128
1129 // Delete the other profile.
1130 profile_manager->ScheduleProfileForDeletion(dest_path1,
1131 ProfileManager::CreateCallback());
1132 // Spin the message loop so that all the callbacks can finish running.
1133 base::RunLoop().RunUntilIdle();
1134
1135 // A new profile should have been created.
1136 const std::string profile_name2 = "Profile 1";
1137 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1138
1139 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121140 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
1141 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath());
[email protected]602b5a29f2014-07-18 15:39:331142}
1143
[email protected]3f130532014-02-26 20:39:241144TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1145 if (!profiles::IsMultipleProfilesEnabled())
1146 return;
1147
[email protected]3f130532014-02-26 20:39:241148 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121149 ProfileAttributesStorage& storage =
1150 profile_manager->GetProfileAttributesStorage();
1151 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241152
1153 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1154 const base::string16 default_profile_name =
1155 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121156 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1157 Profile* profile1 = AddProfileToStorage(profile_manager,
1158 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441159 EXPECT_EQ(default_profile_name,
1160 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241161
1162 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121163 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1164 Profile* profile2 = AddProfileToStorage(profile_manager,
1165 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441166 EXPECT_EQ(profile_name1,
1167 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1168 EXPECT_EQ(profile_name2,
1169 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241170
1171 // Deleting a profile means returning to the default name.
1172 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1173 ProfileManager::CreateCallback());
1174 // Spin the message loop so that all the callbacks can finish running.
1175 base::RunLoop().RunUntilIdle();
[email protected]119809d2014-06-27 22:12:441176 EXPECT_EQ(default_profile_name,
1177 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241178}
1179
1180TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1181 if (!profiles::IsMultipleProfilesEnabled())
1182 return;
1183
[email protected]3f130532014-02-26 20:39:241184 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121185 ProfileAttributesStorage& storage =
1186 profile_manager->GetProfileAttributesStorage();
1187 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241188
1189 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1190 const base::string16 default_profile_name =
1191 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121192 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1193 Profile* profile1 = AddProfileToStorage(profile_manager,
1194 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441195 EXPECT_EQ(default_profile_name,
1196 profiles::GetAvatarNameForProfile(profile1->GetPath()));
lwchkg498e92492016-04-23 11:04:121197 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241198
1199 // We should display custom names for local profiles.
1200 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
lwchkg498e92492016-04-23 11:04:121201 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u];
1202 entry->SetName(custom_profile_name);
1203 entry->SetIsUsingDefaultName(false);
1204 EXPECT_EQ(custom_profile_name, entry->GetName());
[email protected]119809d2014-06-27 22:12:441205 EXPECT_EQ(custom_profile_name,
1206 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241207
1208 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121209 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1210 Profile* profile2 = AddProfileToStorage(profile_manager,
1211 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441212 EXPECT_EQ(custom_profile_name,
1213 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1214 EXPECT_EQ(profile_name2,
1215 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241216
1217 // Deleting a profile means returning to the original, custom name.
1218 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1219 ProfileManager::CreateCallback());
1220 // Spin the message loop so that all the callbacks can finish running.
1221 base::RunLoop().RunUntilIdle();
[email protected]119809d2014-06-27 22:12:441222 EXPECT_EQ(custom_profile_name,
1223 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241224}
1225
1226TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1227 if (!profiles::IsMultipleProfilesEnabled())
1228 return;
1229
[email protected]3f130532014-02-26 20:39:241230 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121231 ProfileAttributesStorage& storage =
1232 profile_manager->GetProfileAttributesStorage();
1233 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
[email protected]3f130532014-02-26 20:39:241234
1235 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1236 const base::string16 default_profile_name =
1237 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
lwchkg498e92492016-04-23 11:04:121238 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u);
1239 Profile* profile1 = AddProfileToStorage(profile_manager,
1240 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441241 EXPECT_EQ(default_profile_name,
1242 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241243
lwchkg498e92492016-04-23 11:04:121244 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u];
[email protected]78186a42014-08-22 22:07:301245 // For a signed in profile with a default name we still display
1246 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
lwchkg498e92492016-04-23 11:04:121247 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
1248 EXPECT_EQ(profile_name1, entry->GetName());
[email protected]78186a42014-08-22 22:07:301249 EXPECT_EQ(default_profile_name,
[email protected]119809d2014-06-27 22:12:441250 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241251
[email protected]78186a42014-08-22 22:07:301252 // For a signed in profile with a non-default Gaia given name we display the
1253 // Gaia given name.
lwchkg498e92492016-04-23 11:04:121254 entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]"));
[email protected]78186a42014-08-22 22:07:301255 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
lwchkg498e92492016-04-23 11:04:121256 entry->SetGAIAGivenName(gaia_given_name);
1257 EXPECT_EQ(gaia_given_name, entry->GetName());
[email protected]78186a42014-08-22 22:07:301258 EXPECT_EQ(gaia_given_name,
1259 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1260
[email protected]3f130532014-02-26 20:39:241261 // Multiple profiles means displaying the actual profile names.
lwchkg498e92492016-04-23 11:04:121262 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u);
1263 Profile* profile2 = AddProfileToStorage(profile_manager,
1264 "path_2", profile_name2);
[email protected]78186a42014-08-22 22:07:301265 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441266 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1267 EXPECT_EQ(profile_name2,
1268 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241269
1270 // Deleting a profile means returning to the original, actual profile name.
1271 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1272 ProfileManager::CreateCallback());
1273 // Spin the message loop so that all the callbacks can finish running.
1274 base::RunLoop().RunUntilIdle();
[email protected]78186a42014-08-22 22:07:301275 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441276 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241277}
nomsc9299462014-09-16 22:55:281278
1279TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1280 if (!profiles::IsMultipleProfilesEnabled())
1281 return;
1282
nomsc9299462014-09-16 22:55:281283 ProfileManager* profile_manager = g_browser_process->profile_manager();
lwchkg498e92492016-04-23 11:04:121284 ProfileAttributesStorage& storage =
1285 profile_manager->GetProfileAttributesStorage();
1286 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281287
1288 // Create two signed in profiles, with both new and legacy default names, and
1289 // a profile with a custom name.
lwchkg498e92492016-04-23 11:04:121290 Profile* profile1 = AddProfileToStorage(profile_manager, "path_1",
1291 ASCIIToUTF16("Person 1"));
1292 Profile* profile2 = AddProfileToStorage(profile_manager, "path_2",
1293 ASCIIToUTF16("Default Profile"));
nomsc9299462014-09-16 22:55:281294 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
lwchkg498e92492016-04-23 11:04:121295 Profile* profile3 = AddProfileToStorage(profile_manager, "path_3",
1296 profile_name3);
1297 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
nomsc9299462014-09-16 22:55:281298
1299 // Sign in all profiles, and make sure they do not have a Gaia name set.
1300 const base::string16 email1(ASCIIToUTF16("[email protected]"));
1301 const base::string16 email2(ASCIIToUTF16("[email protected]"));
1302 const base::string16 email3(ASCIIToUTF16("[email protected]"));
1303
lwchkg498e92492016-04-23 11:04:121304 ProfileAttributesEntry* entry;
1305
1306 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1307 &entry));
1308 entry->SetAuthInfo("12345", email1);
1309 entry->SetGAIAGivenName(base::string16());
1310 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281311
1312 // This may resort the cache, so be extra cautious to use the right profile.
lwchkg498e92492016-04-23 11:04:121313 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(),
1314 &entry));
1315 entry->SetAuthInfo("23456", email2);
1316 entry->SetGAIAGivenName(base::string16());
1317 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281318
lwchkg498e92492016-04-23 11:04:121319 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(),
1320 &entry));
1321 entry->SetAuthInfo("34567", email3);
1322 entry->SetGAIAGivenName(base::string16());
1323 entry->SetGAIAName(base::string16());
nomsc9299462014-09-16 22:55:281324
1325 // The profiles with default names should display the email address.
1326 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1327 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1328
1329 // The profile with the custom name should display that.
1330 EXPECT_EQ(profile_name3,
1331 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1332
1333 // Adding a Gaia name to a profile that previously had a default name should
1334 // start displaying it.
1335 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
lwchkg498e92492016-04-23 11:04:121336 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(),
1337 &entry));
1338 entry->SetGAIAGivenName(gaia_given_name);
nomsc9299462014-09-16 22:55:281339 EXPECT_EQ(gaia_given_name,
1340 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1341}
[email protected]1c62b2f2013-06-28 00:15:001342#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1343
1344#if defined(OS_MACOSX)
1345// These tests are for a Mac-only code path that assumes the browser
1346// process isn't killed when all browser windows are closed.
1347TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1348 ProfileManager* profile_manager = g_browser_process->profile_manager();
1349 ASSERT_TRUE(profile_manager);
1350
1351 // Create and load one profile, and just create a second profile.
1352 const std::string profile_name1 = "New Profile 1";
1353 const std::string profile_name2 = "New Profile 2";
[email protected]3f130532014-02-26 20:39:241354 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1355 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001356
1357 MockObserver mock_observer;
1358 EXPECT_CALL(mock_observer, OnProfileCreated(
1359 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531360 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:351361 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001362
1363 // Track the profile, but don't load it.
lwchkg498e92492016-04-23 11:04:121364 ProfileAttributesStorage& storage =
1365 profile_manager->GetProfileAttributesStorage();
1366 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1367 base::string16(), 0, std::string());
[email protected]37ac95b2013-07-23 23:39:351368 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001369
1370 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121371 EXPECT_EQ(2u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001372
1373 // Set the active profile.
1374 PrefService* local_state = g_browser_process->local_state();
1375 local_state->SetString(prefs::kProfileLastUsed,
1376 dest_path1.BaseName().MaybeAsASCII());
1377
1378 // Delete the active profile. This should switch and load the unloaded
1379 // profile.
1380 profile_manager->ScheduleProfileForDeletion(dest_path1,
1381 ProfileManager::CreateCallback());
1382
1383 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:351384 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001385
1386 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1387 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1388}
1389
1390// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1391// by simulating a scenario in which the profile that is being loaded as
1392// the next active profile has also been marked for deletion, so the
1393// ProfileManager needs to recursively select a different next profile.
1394TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1395 ProfileManager* profile_manager = g_browser_process->profile_manager();
1396 ASSERT_TRUE(profile_manager);
1397
1398 // Create and load one profile, and create two more profiles.
1399 const std::string profile_name1 = "New Profile 1";
1400 const std::string profile_name2 = "New Profile 2";
1401 const std::string profile_name3 = "New Profile 3";
[email protected]3f130532014-02-26 20:39:241402 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1403 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1404 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
[email protected]1c62b2f2013-06-28 00:15:001405
1406 MockObserver mock_observer;
1407 EXPECT_CALL(mock_observer, OnProfileCreated(
1408 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531409 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:351410 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001411
1412 // Create the other profiles, but don't load them. Assign a fake avatar icon
lwchkg498e92492016-04-23 11:04:121413 // to ensure that profiles in the profile attributes storage are sorted by the
1414 // profile name, and not randomly by the avatar name.
1415 ProfileAttributesStorage& storage =
1416 profile_manager->GetProfileAttributesStorage();
1417 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1418 ASCIIToUTF16(profile_name2), 1, std::string());
1419 storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567",
1420 ASCIIToUTF16(profile_name3), 2, std::string());
[email protected]1c62b2f2013-06-28 00:15:001421
[email protected]37ac95b2013-07-23 23:39:351422 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001423
1424 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
lwchkg498e92492016-04-23 11:04:121425 EXPECT_EQ(3u, storage.GetNumberOfProfiles());
[email protected]1c62b2f2013-06-28 00:15:001426
1427 // Set the active profile.
1428 PrefService* local_state = g_browser_process->local_state();
1429 local_state->SetString(prefs::kProfileLastUsed,
1430 dest_path1.BaseName().MaybeAsASCII());
1431
1432 // Delete the active profile, Profile1.
1433 // This will post a CreateProfileAsync message, that tries to load Profile2,
1434 // which checks that the profile is not being deleted, and then calls back
1435 // FinishDeletingProfile for Profile1.
1436 // Try to break this flow by setting the active profile to Profile2 in the
1437 // middle (so after the first posted message), and trying to delete Profile2,
1438 // so that the ProfileManager has to look for a different profile to load.
1439 profile_manager->ScheduleProfileForDeletion(dest_path1,
1440 ProfileManager::CreateCallback());
1441 local_state->SetString(prefs::kProfileLastUsed,
1442 dest_path2.BaseName().MaybeAsASCII());
1443 profile_manager->ScheduleProfileForDeletion(dest_path2,
1444 ProfileManager::CreateCallback());
1445 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:351446 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001447
1448 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1449 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1450}
1451#endif // !defined(OS_MACOSX)