blob: b98362821d89a734de3e2556fffc60a92cd89a18 [file] [log] [blame]
[email protected]52edf812012-01-12 11:48:461// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d99bcaa2010-12-05 20:15:565#include <string>
6
[email protected]943aed5542010-06-11 15:57:087#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
[email protected]37ac95b2013-07-23 23:39:3511#include "base/run_loop.h"
[email protected]e309f312013-06-07 21:50:0812#include "base/strings/utf_string_conversions.h"
[email protected]29d70252011-04-28 02:16:5813#include "base/values.h"
[email protected]775caec2011-09-01 17:14:3214#include "build/build_config.h"
[email protected]5114d182012-07-30 23:09:4915#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]583844c2011-08-27 00:38:3516#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0317#include "chrome/browser/chrome_notification_types.h"
[email protected]363f5272013-04-23 17:21:4218#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]6a2c09f2013-01-25 04:50:0719#include "chrome/browser/history/history_service.h"
[email protected]9718a722012-06-19 20:10:5320#include "chrome/browser/history/history_service_factory.h"
[email protected]63e26822011-07-16 19:07:3521#include "chrome/browser/io_thread.h"
[email protected]81ad7f4a2011-03-16 01:33:2922#include "chrome/browser/prefs/browser_prefs.h"
[email protected]13720532013-05-30 12:30:5923#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3324#include "chrome/browser/profiles/profile.h"
[email protected]c3e559772014-04-09 04:02:5425#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]279170832011-10-12 23:38:0326#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3327#include "chrome/browser/profiles/profile_manager.h"
[email protected]3f130532014-02-26 20:39:2428#include "chrome/browser/profiles/profiles_state.h"
[email protected]d8748142012-05-16 21:13:4329#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0830#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0832#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5833#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1834#include "chrome/grit/generated_resources.h"
[email protected]7688968a2013-02-12 21:45:1335#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2936#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3537#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1738#include "chrome/test/base/testing_profile.h"
[email protected]29896ee2014-06-17 17:20:5339#include "components/signin/core/common/profile_management_switches.h"
[email protected]ad50def52011-10-19 23:17:0740#include "content/public/browser/notification_service.h"
[email protected]f9357a442014-05-15 18:44:0741#include "content/public/common/content_switches.h"
[email protected]37ac95b2013-07-23 23:39:3542#include "content/public/test/test_browser_thread_bundle.h"
[email protected]844a1002011-04-19 11:37:2143#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2944#include "testing/gtest/include/gtest/gtest.h"
[email protected]3f130532014-02-26 20:39:2445#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2946
[email protected]775caec2011-09-01 17:14:3247#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:5248#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
[email protected]4d390782014-08-15 09:22:5849#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
dzhioev0298d722014-08-26 13:18:5250#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
51#include "chrome/browser/chromeos/profiles/profile_helper.h"
[email protected]363f5272013-04-23 17:21:4252#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]e4854dc2013-04-24 00:11:5153#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]931d1042013-04-05 17:50:4454#include "chromeos/chromeos_switches.h"
[email protected]4c40e062014-07-09 21:22:5155#include "chromeos/login/user_names.h"
[email protected]4d390782014-08-15 09:22:5856#include "components/user_manager/user_manager.h"
dzhioev0298d722014-08-26 13:18:5257#endif // defined(OS_CHROMEOS)
[email protected]775caec2011-09-01 17:14:3258
[email protected]6778fed2013-12-24 20:09:3759using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3960using content::BrowserThread;
61
[email protected]844a1002011-04-19 11:37:2162namespace {
[email protected]f4e706252012-01-10 17:11:3663
[email protected]844a1002011-04-19 11:37:2164// This global variable is used to check that value returned to different
65// observers is the same.
66Profile* g_created_profile;
67
[email protected]9819fd02013-08-22 10:49:3968class UnittestProfileManager : public ::ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1769 public:
[email protected]9819fd02013-08-22 10:49:3970 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
[email protected]537c1082011-12-02 02:37:1771 : ::ProfileManagerWithoutInit(user_data_dir) {}
72
73 protected:
[email protected]650b2d52013-02-10 03:41:4574 virtual Profile* CreateProfileHelper(
75 const base::FilePath& file_path) OVERRIDE {
[email protected]7567484142013-07-11 17:36:0776 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5477 if (!base::CreateDirectory(file_path))
[email protected]537c1082011-12-02 02:37:1778 return NULL;
79 }
80 return new TestingProfile(file_path, NULL);
81 }
82
[email protected]650b2d52013-02-10 03:41:4583 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:1784 Delegate* delegate) OVERRIDE {
85 // This is safe while all file operations are done on the FILE thread.
86 BrowserThread::PostTask(
87 BrowserThread::FILE, FROM_HERE,
[email protected]426d1c92013-12-03 20:08:5488 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:1789
90 return new TestingProfile(path, this);
91 }
92};
93
[email protected]9819fd02013-08-22 10:49:3994} // namespace
[email protected]537c1082011-12-02 02:37:1795
[email protected]583844c2011-08-27 00:38:3596class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:4797 protected:
[email protected]f4e706252012-01-10 17:11:3698 class MockObserver {
99 public:
100 MOCK_METHOD2(OnProfileCreated,
101 void(Profile* profile, Profile::CreateStatus status));
102 };
103
[email protected]6cad5bf2011-03-10 21:21:55104 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:35105 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:09106 }
107
initial.commit09911bf2008-07-26 23:55:29108 virtual void SetUp() {
[email protected]3a305db2011-04-12 13:40:53109 // Create a new temporary directory, and store the path
110 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22111 TestingBrowserProcess::GetGlobal()->SetProfileManager(
[email protected]9819fd02013-08-22 10:49:39112 new UnittestProfileManager(temp_dir_.path()));
[email protected]f89b50f2012-08-16 21:36:50113
[email protected]537c1082011-12-02 02:37:17114#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52115 CommandLine* cl = CommandLine::ForCurrentProcess();
116 cl->AppendSwitch(switches::kTestType);
[email protected]537c1082011-12-02 02:37:17117#endif
initial.commit09911bf2008-07-26 23:55:29118 }
[email protected]81ad7f4a2011-03-16 01:33:29119
initial.commit09911bf2008-07-26 23:55:29120 virtual void TearDown() {
[email protected]c494d082013-01-04 20:41:22121 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]37ac95b2013-07-23 23:39:35122 base::RunLoop().RunUntilIdle();
initial.commit09911bf2008-07-26 23:55:29123 }
[email protected]f0a51fb52009-03-05 12:46:38124
[email protected]1c62b2f2013-06-28 00:15:00125 // Helper function to create a profile with |name| for a profile |manager|.
126 void CreateProfileAsync(ProfileManager* manager,
127 const std::string& name,
[email protected]d20d0432014-06-12 17:14:05128 bool is_supervised,
[email protected]1c62b2f2013-06-28 00:15:00129 MockObserver* mock_observer) {
130 manager->CreateProfileAsync(
131 temp_dir_.path().AppendASCII(name),
132 base::Bind(&MockObserver::OnProfileCreated,
133 base::Unretained(mock_observer)),
[email protected]6778fed2013-12-24 20:09:37134 base::UTF8ToUTF16(name),
[email protected]c3e559772014-04-09 04:02:54135 base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(0)),
[email protected]d20d0432014-06-12 17:14:05136 is_supervised ? "Dummy ID" : std::string());
[email protected]1c62b2f2013-06-28 00:15:00137 }
138
[email protected]3f130532014-02-26 20:39:24139 // Helper function to add a profile with |profile_name| to
140 // |profile_manager|'s ProfileInfoCache, and return the profile created.
141 Profile* AddProfileToCache(ProfileManager* profile_manager,
142 const std::string& path_suffix,
143 const base::string16& profile_name) {
144 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
145 size_t num_profiles = cache.GetNumberOfProfiles();
146 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
147 cache.AddProfileToCache(path, profile_name,
148 base::string16(), 0, std::string());
149 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
150 return profile_manager->GetProfile(path);
151 }
152
[email protected]6f96cbcb2011-11-04 02:26:07153#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52154 // Helper function to register an user with id |user_id| and create profile
155 // with a correct path.
156 void RegisterUser(const std::string& user_id) {
157 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
158 const std::string user_id_hash =
159 profile_helper->GetUserIdHashByUserIdForTesting(user_id);
160 user_manager::UserManager::Get()->UserLoggedIn(
161 user_id, user_id_hash, false);
162 g_browser_process->profile_manager()->GetProfile(
163 profile_helper->GetProfilePathByUserIdHash(user_id_hash));
164 }
165
[email protected]e4854dc2013-04-24 00:11:51166 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
[email protected]363f5272013-04-23 17:21:42167 chromeos::ScopedTestCrosSettings test_cros_settings_;
[email protected]6f96cbcb2011-11-04 02:26:07168#endif
169
[email protected]3a305db2011-04-12 13:40:53170 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23171 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35172 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35173
174 content::TestBrowserThreadBundle thread_bundle_;
[email protected]363f5272013-04-23 17:21:42175
176#if defined(OS_CHROMEOS)
177 chromeos::ScopedTestUserManager test_user_manager_;
178#endif
dzhioev0298d722014-08-26 13:18:52179
180 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest);
initial.commit09911bf2008-07-26 23:55:29181};
182
[email protected]844a1002011-04-19 11:37:21183TEST_F(ProfileManagerTest, GetProfile) {
[email protected]650b2d52013-02-10 03:41:45184 base::FilePath dest_path = temp_dir_.path();
[email protected]f7011fcb2009-01-28 21:54:32185 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29186
[email protected]881cb0b62011-12-13 20:39:54187 ProfileManager* profile_manager = g_browser_process->profile_manager();
188
initial.commit09911bf2008-07-26 23:55:29189 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36190 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21191 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29192
[email protected]844a1002011-04-19 11:37:21193 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54194 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29195}
[email protected]55474b572009-04-14 22:05:33196
[email protected]943aed5542010-06-11 15:57:08197TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45198 base::FilePath expected_default =
199 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54200 EXPECT_EQ(
201 expected_default.value(),
202 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08203}
204
205#if defined(OS_CHROMEOS)
dzhioev0298d722014-08-26 13:18:52206
[email protected]943aed5542010-06-11 15:57:08207// This functionality only exists on Chrome OS.
208TEST_F(ProfileManagerTest, LoggedInProfileDir) {
[email protected]650b2d52013-02-10 03:41:45209 base::FilePath expected_default =
210 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54211 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08212 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54213 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08214
dzhioev0298d722014-08-26 13:18:52215 const char kTestUserName[] = "[email protected]";
216 chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
217 chromeos::ScopedUserManagerEnabler enabler(user_manager);
218
219 const user_manager::User* active_user = user_manager->AddUser(kTestUserName);
220 user_manager->LoginUser(kTestUserName);
221 user_manager->SwitchActiveUser(kTestUserName);
222
[email protected]2fda9972014-07-23 14:51:59223 profile_manager->Observe(
224 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
225 content::NotificationService::AllSources(),
226 content::Details<const user_manager::User>(active_user));
dzhioev0298d722014-08-26 13:18:52227 base::FilePath expected_logged_in(
228 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash()));
[email protected]943aed5542010-06-11 15:57:08229 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54230 profile_manager->GetInitialProfileDir().value());
[email protected]3a305db2011-04-12 13:40:53231 VLOG(1) << temp_dir_.path().Append(
[email protected]881cb0b62011-12-13 20:39:54232 profile_manager->GetInitialProfileDir()).value();
[email protected]943aed5542010-06-11 15:57:08233}
234
235#endif
236
[email protected]52d69b882010-06-17 17:35:51237TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
[email protected]650b2d52013-02-10 03:41:45238 base::FilePath dest_path1 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33239 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47240
[email protected]650b2d52013-02-10 03:41:45241 base::FilePath dest_path2 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33242 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
243
[email protected]881cb0b62011-12-13 20:39:54244 ProfileManager* profile_manager = g_browser_process->profile_manager();
245
[email protected]55474b572009-04-14 22:05:33246 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17247 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54248 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21249 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33250
[email protected]537c1082011-12-02 02:37:17251 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54252 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21253 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33254
255 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31256 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53257 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1,
258 Profile::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17259 profile1->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49260 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
[email protected]537c1082011-12-02 02:37:17261 profile2->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49262 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
[email protected]608e7e02013-07-24 12:23:31263 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53264 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2,
265 Profile::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55266
267 // Make sure any pending tasks run before we destroy the profiles.
[email protected]37ac95b2013-07-23 23:39:35268 base::RunLoop().RunUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55269
[email protected]c494d082013-01-04 20:41:22270 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]6cad5bf2011-03-10 21:21:55271
[email protected]237e6d02010-11-08 21:45:42272 // Make sure history cleans up correctly.
[email protected]37ac95b2013-07-23 23:39:35273 base::RunLoop().RunUntilIdle();
[email protected]55474b572009-04-14 22:05:33274}
[email protected]844a1002011-04-19 11:37:21275
[email protected]e547ac82011-07-25 12:34:11276MATCHER(NotFail, "Profile creation failure status is not reported.") {
[email protected]b8ba8d42011-12-07 22:13:59277 return arg == Profile::CREATE_STATUS_CREATED ||
278 arg == Profile::CREATE_STATUS_INITIALIZED;
[email protected]e547ac82011-07-25 12:34:11279}
280
[email protected]e547ac82011-07-25 12:34:11281MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
[email protected]844a1002011-04-19 11:37:21282 if (!g_created_profile)
283 g_created_profile = arg;
[email protected]e547ac82011-07-25 12:34:11284 return arg != NULL && arg == g_created_profile;
[email protected]844a1002011-04-19 11:37:21285}
286
287TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
[email protected]844a1002011-04-19 11:37:21288 g_created_profile = NULL;
289
290 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11291 EXPECT_CALL(mock_observer1, OnProfileCreated(
292 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21293 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11294 EXPECT_CALL(mock_observer2, OnProfileCreated(
295 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21296 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11297 EXPECT_CALL(mock_observer3, OnProfileCreated(
298 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21299
[email protected]881cb0b62011-12-13 20:39:54300 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00301 const std::string profile_name = "New Profile";
[email protected]5ddfade2014-02-03 10:24:53302 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
303 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
304 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21305
[email protected]37ac95b2013-07-23 23:39:35306 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21307}
308
309TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00310 const std::string profile_name1 = "New Profile 1";
311 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21312
313 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11314 EXPECT_CALL(mock_observer, OnProfileCreated(
315 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21316
[email protected]881cb0b62011-12-13 20:39:54317 ProfileManager* profile_manager = g_browser_process->profile_manager();
318
[email protected]5ddfade2014-02-03 10:24:53319 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
320 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]844a1002011-04-19 11:37:21321
[email protected]37ac95b2013-07-23 23:39:35322 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21323}
[email protected]279170832011-10-12 23:38:03324
[email protected]5ddfade2014-02-03 10:24:53325TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
[email protected]d20d0432014-06-12 17:14:05326 std::string name = "0 Supervised Profile";
[email protected]5ddfade2014-02-03 10:24:53327
328 MockObserver mock_observer;
329 EXPECT_CALL(mock_observer, OnProfileCreated(
330 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
331
332 ProfileManager* profile_manager = g_browser_process->profile_manager();
333 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
334 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
335
336 CreateProfileAsync(profile_manager, name, true, &mock_observer);
337 base::RunLoop().RunUntilIdle();
338
339 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05340 // Supervised profiles should start out omitted from the profile list.
[email protected]5ddfade2014-02-03 10:24:53341 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
342
[email protected]d20d0432014-06-12 17:14:05343 name = "1 Regular Profile";
[email protected]5ddfade2014-02-03 10:24:53344 CreateProfileAsync(profile_manager, name, false, &mock_observer);
345 base::RunLoop().RunUntilIdle();
346
347 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05348 // Non-supervised profiles should be included in the profile list.
[email protected]5ddfade2014-02-03 10:24:53349 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
350}
351
352TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
353 ProfileManager* profile_manager = g_browser_process->profile_manager();
354 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
355 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
356
[email protected]d20d0432014-06-12 17:14:05357 const base::FilePath supervised_path =
358 temp_dir_.path().AppendASCII("Supervised");
359 TestingProfile* supervised_profile =
360 new TestingProfile(supervised_path, NULL);
361 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
[email protected]5ddfade2014-02-03 10:24:53362
363 // RegisterTestingProfile adds the profile to the cache and takes ownership.
[email protected]d20d0432014-06-12 17:14:05364 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
[email protected]5ddfade2014-02-03 10:24:53365 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
366 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
367
[email protected]d20d0432014-06-12 17:14:05368 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII(
369 "Non-Supervised");
370 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
371 NULL);
372 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
[email protected]5ddfade2014-02-03 10:24:53373
374 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
[email protected]d20d0432014-06-12 17:14:05375 size_t supervised_index = cache.GetIndexOfProfileWithPath(supervised_path);
376 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(supervised_index));
377 size_t nonsupervised_index =
378 cache.GetIndexOfProfileWithPath(nonsupervised_path);
379 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonsupervised_index));
[email protected]5ddfade2014-02-03 10:24:53380}
381
[email protected]4251165a2013-07-17 04:33:40382TEST_F(ProfileManagerTest, GetGuestProfilePath) {
383 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
384 base::FilePath expected_path = temp_dir_.path();
385 expected_path = expected_path.Append(chrome::kGuestProfileDir);
386 EXPECT_EQ(expected_path, guest_path);
387}
388
[email protected]0ff6198e2014-01-08 22:45:00389class UnittestGuestProfileManager : public UnittestProfileManager {
390 public:
391 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
392 : UnittestProfileManager(user_data_dir) {}
393
394 protected:
395 virtual Profile* CreateProfileHelper(
396 const base::FilePath& file_path) OVERRIDE {
[email protected]0ff6198e2014-01-08 22:45:00397 TestingProfile::Builder builder;
[email protected]0ff6198e2014-01-08 22:45:00398 builder.SetGuestSession();
[email protected]d22f7c932014-02-20 22:48:58399 builder.SetPath(file_path);
400 TestingProfile* testing_profile = builder.Build().release();
[email protected]0ff6198e2014-01-08 22:45:00401 return testing_profile;
402 }
403};
404
[email protected]9b6e10c2014-05-20 19:12:28405class ProfileManagerGuestTest : public ProfileManagerTest {
[email protected]0ff6198e2014-01-08 22:45:00406 protected:
407 virtual void SetUp() {
408 // Create a new temporary directory, and store the path
409 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
410 TestingBrowserProcess::GetGlobal()->SetProfileManager(
411 new UnittestGuestProfileManager(temp_dir_.path()));
412
[email protected]2a92f2182014-03-25 00:47:59413#if defined(OS_CHROMEOS)
[email protected]0ff6198e2014-01-08 22:45:00414 CommandLine* cl = CommandLine::ForCurrentProcess();
[email protected]9b6e10c2014-05-20 19:12:28415 // This switch is needed to skip non-test specific behavior in
416 // ProfileManager (accessing DBusThreadManager).
[email protected]0ff6198e2014-01-08 22:45:00417 cl->AppendSwitch(switches::kTestType);
[email protected]9b6e10c2014-05-20 19:12:28418
[email protected]0ff6198e2014-01-08 22:45:00419 cl->AppendSwitch(chromeos::switches::kGuestSession);
420 cl->AppendSwitch(::switches::kIncognito);
421
dzhioev0298d722014-08-26 13:18:52422 RegisterUser(chromeos::login::kGuestUserName);
[email protected]2a92f2182014-03-25 00:47:59423#endif
[email protected]0ff6198e2014-01-08 22:45:00424 }
425};
426
[email protected]9b6e10c2014-05-20 19:12:28427TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) {
[email protected]2a92f2182014-03-25 00:47:59428 ProfileManager* profile_manager = g_browser_process->profile_manager();
429 ASSERT_TRUE(profile_manager);
430
431 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
432 ASSERT_TRUE(profile);
433 EXPECT_TRUE(profile->IsOffTheRecord());
434}
435
436#if defined(OS_CHROMEOS)
[email protected]9b6e10c2014-05-20 19:12:28437TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
[email protected]0ff6198e2014-01-08 22:45:00438 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
439 EXPECT_TRUE(primary_profile->IsOffTheRecord());
440
441 Profile* active_profile = ProfileManager::GetActiveUserProfile();
442 EXPECT_TRUE(active_profile->IsOffTheRecord());
443
444 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
[email protected]d22f7c932014-02-20 22:48:58445
446 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
447 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
448
449 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
[email protected]0ff6198e2014-01-08 22:45:00450}
451#endif
452
[email protected]279170832011-10-12 23:38:03453TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54454 ProfileManager* profile_manager = g_browser_process->profile_manager();
455 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
[email protected]714bf1d2012-11-30 01:35:27456 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]012d1312014-07-17 06:37:40457 new base::FundamentalValue(true));
[email protected]714bf1d2012-11-30 01:35:27458
459 // Setting a pref which is not applicable to a system (i.e., Android in this
460 // case) does not necessarily create it. Don't bother continuing with the
461 // test if this pref doesn't exist because it will not load the profiles if
462 // it cannot verify that the pref for background mode is enabled.
463 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
464 return;
[email protected]279170832011-10-12 23:38:03465
466 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
467 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40468 ASCIIToUTF16("name_1"), base::string16(), 0,
469 std::string());
[email protected]279170832011-10-12 23:38:03470 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40471 ASCIIToUTF16("name_2"), base::string16(), 0,
472 std::string());
[email protected]279170832011-10-12 23:38:03473 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
[email protected]a04db822013-12-11 19:14:40474 ASCIIToUTF16("name_3"), base::string16(), 0,
475 std::string());
[email protected]279170832011-10-12 23:38:03476 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
477 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
478 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
479
[email protected]881cb0b62011-12-13 20:39:54480 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03481
[email protected]881cb0b62011-12-13 20:39:54482 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
483}
484
[email protected]714bf1d2012-11-30 01:35:27485TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
486 ProfileManager* profile_manager = g_browser_process->profile_manager();
487 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
488 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]012d1312014-07-17 06:37:40489 new base::FundamentalValue(false));
[email protected]714bf1d2012-11-30 01:35:27490
491 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
492 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40493 ASCIIToUTF16("name_1"), base::string16(), 0,
494 std::string());
[email protected]714bf1d2012-11-30 01:35:27495 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40496 ASCIIToUTF16("name_2"), base::string16(), 0,
497 std::string());
[email protected]714bf1d2012-11-30 01:35:27498 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
499 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
500 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
501
502 profile_manager->AutoloadProfiles();
503
504 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
505}
506
[email protected]881cb0b62011-12-13 20:39:54507TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
[email protected]650b2d52013-02-10 03:41:45508 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54509 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
510
511 ProfileManager* profile_manager = g_browser_process->profile_manager();
512
513 Profile* profile;
514
515 // Successfully create the profile
516 profile = profile_manager->GetProfile(dest_path);
517 ASSERT_TRUE(profile);
518
519 // Check that the profile name is non empty
520 std::string profile_name =
521 profile->GetPrefs()->GetString(prefs::kProfileName);
522 EXPECT_FALSE(profile_name.empty());
523
524 // Check that the profile avatar index is valid
525 size_t avatar_index =
526 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
[email protected]c3e559772014-04-09 04:02:54527 EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex(
[email protected]881cb0b62011-12-13 20:39:54528 avatar_index));
529}
530
531// Tests that a new profile's entry in the profile info cache is setup with the
532// same values that are in the profile prefs.
533TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
[email protected]650b2d52013-02-10 03:41:45534 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54535 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
536
537 ProfileManager* profile_manager = g_browser_process->profile_manager();
538 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
539
540 // Successfully create the profile
541 Profile* profile = profile_manager->GetProfile(dest_path);
542 ASSERT_TRUE(profile);
543
544 std::string profile_name =
545 profile->GetPrefs()->GetString(prefs::kProfileName);
546 size_t avatar_index =
547 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
548
549 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
550
551 // Check if the profile prefs are the same as the cache prefs
552 EXPECT_EQ(profile_name,
[email protected]6778fed2013-12-24 20:09:37553 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
[email protected]881cb0b62011-12-13 20:39:54554 EXPECT_EQ(avatar_index,
555 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
[email protected]279170832011-10-12 23:38:03556}
[email protected]52edf812012-01-12 11:48:46557
[email protected]13720532013-05-30 12:30:59558TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
559 ProfileManager* profile_manager = g_browser_process->profile_manager();
560 ASSERT_TRUE(profile_manager);
561
dzhioev0298d722014-08-26 13:18:52562#if defined(OS_CHROMEOS)
563 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that
564 // is forced to be incognito. That's why we need to create at least one user
565 // to get a regular profile.
566 RegisterUser("[email protected]");
567#endif
568
[email protected]13720532013-05-30 12:30:59569 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
570 ASSERT_TRUE(profile);
571 EXPECT_FALSE(profile->IsOffTheRecord());
572 PrefService* prefs = profile->GetPrefs();
573 EXPECT_EQ(IncognitoModePrefs::ENABLED,
574 IncognitoModePrefs::GetAvailability(prefs));
575
[email protected]13720532013-05-30 12:30:59576 ASSERT_TRUE(profile->GetOffTheRecordProfile());
577
578 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
579 EXPECT_FALSE(
580 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
581
582 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
583 // incognito mode is forced.
584 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
585 EXPECT_TRUE(
586 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
587}
588
[email protected]c849fab2012-03-29 16:51:41589#if !defined(OS_ANDROID)
590// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46591TEST_F(ProfileManagerTest, LastOpenedProfiles) {
[email protected]650b2d52013-02-10 03:41:45592 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46593 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
594
[email protected]650b2d52013-02-10 03:41:45595 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46596 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
597
598 ProfileManager* profile_manager = g_browser_process->profile_manager();
599
600 // Successfully create the profiles.
601 TestingProfile* profile1 =
602 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
603 ASSERT_TRUE(profile1);
604
605 TestingProfile* profile2 =
606 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
607 ASSERT_TRUE(profile2);
608
609 std::vector<Profile*> last_opened_profiles =
610 profile_manager->GetLastOpenedProfiles();
611 ASSERT_EQ(0U, last_opened_profiles.size());
612
613 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34614 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29615 scoped_ptr<Browser> browser1a(
[email protected]c5f3e1842013-02-20 17:10:41616 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46617
618 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
619 ASSERT_EQ(1U, last_opened_profiles.size());
620 EXPECT_EQ(profile1, last_opened_profiles[0]);
621
622 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34623 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29624 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41625 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46626
627 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
628 ASSERT_EQ(2U, last_opened_profiles.size());
629 EXPECT_EQ(profile1, last_opened_profiles[0]);
630 EXPECT_EQ(profile2, last_opened_profiles[1]);
631
632 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29633 scoped_ptr<Browser> browser1b(
[email protected]c5f3e1842013-02-20 17:10:41634 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46635 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
636 ASSERT_EQ(2U, last_opened_profiles.size());
637 EXPECT_EQ(profile1, last_opened_profiles[0]);
638 EXPECT_EQ(profile2, last_opened_profiles[1]);
639
640 // Close the browsers.
641 browser1a.reset();
642 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
643 ASSERT_EQ(2U, last_opened_profiles.size());
644 EXPECT_EQ(profile1, last_opened_profiles[0]);
645 EXPECT_EQ(profile2, last_opened_profiles[1]);
646
647 browser1b.reset();
648 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
649 ASSERT_EQ(1U, last_opened_profiles.size());
650 EXPECT_EQ(profile2, last_opened_profiles[0]);
651
652 browser2.reset();
653 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
654 ASSERT_EQ(0U, last_opened_profiles.size());
655}
656
657TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
[email protected]650b2d52013-02-10 03:41:45658 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46659 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
660
[email protected]650b2d52013-02-10 03:41:45661 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46662 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
663
664 ProfileManager* profile_manager = g_browser_process->profile_manager();
665
666 // Successfully create the profiles.
667 TestingProfile* profile1 =
668 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
669 ASSERT_TRUE(profile1);
670
671 TestingProfile* profile2 =
672 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
673 ASSERT_TRUE(profile2);
674
675 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34676 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29677 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41678 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46679
680 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34681 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29682 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41683 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46684
685 std::vector<Profile*> last_opened_profiles =
686 profile_manager->GetLastOpenedProfiles();
687 ASSERT_EQ(2U, last_opened_profiles.size());
688 EXPECT_EQ(profile1, last_opened_profiles[0]);
689 EXPECT_EQ(profile2, last_opened_profiles[1]);
690
691 // Simulate a shutdown.
692 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30693 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:46694 content::NotificationService::AllSources(),
695 content::NotificationService::NoDetails());
696
697 // Even if the browsers are destructed during shutdown, the profiles stay
698 // open.
699 browser1.reset();
700 browser2.reset();
701
702 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
703 ASSERT_EQ(2U, last_opened_profiles.size());
704 EXPECT_EQ(profile1, last_opened_profiles[0]);
705 EXPECT_EQ(profile2, last_opened_profiles[1]);
706}
[email protected]41276342012-02-06 10:49:31707
708TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
[email protected]650b2d52013-02-10 03:41:45709 base::FilePath dest_path1 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31710 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]650b2d52013-02-10 03:41:45711 base::FilePath dest_path2 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31712 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
713
714 ProfileManager* profile_manager = g_browser_process->profile_manager();
715
716 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:06717 TestingProfile* profile1 =
718 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:31719 ASSERT_TRUE(profile1);
720
[email protected]5ddfade2014-02-03 10:24:53721 // Incognito profiles should not be managed by the profile manager but by the
[email protected]4dffabe2012-05-19 14:37:06722 // original profile.
[email protected]9819fd02013-08-22 10:49:39723 TestingProfile::Builder builder;
724 builder.SetIncognito();
725 scoped_ptr<TestingProfile> profile2 = builder.Build();
726 profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
[email protected]41276342012-02-06 10:49:31727
728 std::vector<Profile*> last_opened_profiles =
729 profile_manager->GetLastOpenedProfiles();
730 ASSERT_EQ(0U, last_opened_profiles.size());
731
732 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34733 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29734 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41735 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:31736
737 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
738 ASSERT_EQ(1U, last_opened_profiles.size());
739 EXPECT_EQ(profile1, last_opened_profiles[0]);
740
741 // And for profile2.
[email protected]9819fd02013-08-22 10:49:39742 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
743 chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29744 scoped_ptr<Browser> browser2a(
[email protected]c5f3e1842013-02-20 17:10:41745 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31746
747 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
748 ASSERT_EQ(1U, last_opened_profiles.size());
749 EXPECT_EQ(profile1, last_opened_profiles[0]);
750
751 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29752 scoped_ptr<Browser> browser2b(
[email protected]c5f3e1842013-02-20 17:10:41753 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31754 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
755 ASSERT_EQ(1U, last_opened_profiles.size());
756 EXPECT_EQ(profile1, last_opened_profiles[0]);
757
758 // Close the browsers.
759 browser2a.reset();
760 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
761 ASSERT_EQ(1U, last_opened_profiles.size());
762 EXPECT_EQ(profile1, last_opened_profiles[0]);
763
764 browser2b.reset();
765 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
766 ASSERT_EQ(1U, last_opened_profiles.size());
767 EXPECT_EQ(profile1, last_opened_profiles[0]);
768
769 browser1.reset();
770 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
771 ASSERT_EQ(0U, last_opened_profiles.size());
772}
[email protected]c849fab2012-03-29 16:51:41773#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:00774
775#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
776// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:48777TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
778 base::FilePath dest_path = temp_dir_.path();
779 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
780
781 ProfileManager* profile_manager = g_browser_process->profile_manager();
782
783 TestingProfile* profile =
784 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
785 ASSERT_TRUE(profile);
786 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
787
788 // Here the last used profile is still the "Default" profile.
789 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
790 EXPECT_NE(profile, last_used_profile);
791
[email protected]79fd6b72013-10-22 16:39:27792 // Create a browser for the profile.
[email protected]8d9243a2013-10-21 17:11:48793 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
794 scoped_ptr<Browser> browser(
795 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
796 last_used_profile = profile_manager->GetLastUsedProfile();
797 EXPECT_NE(profile, last_used_profile);
798
799 // Close the browser.
800 browser.reset();
801 last_used_profile = profile_manager->GetLastUsedProfile();
802 EXPECT_NE(profile, last_used_profile);
803}
804
805TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
806 base::FilePath dest_path1 = temp_dir_.path();
807 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
808
809 base::FilePath dest_path2 = temp_dir_.path();
[email protected]79fd6b72013-10-22 16:39:27810 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
811
812 base::FilePath dest_path3 = temp_dir_.path();
813 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:48814
815 ProfileManager* profile_manager = g_browser_process->profile_manager();
816
817 // Successfully create the profiles.
818 TestingProfile* normal_profile =
819 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
820 ASSERT_TRUE(normal_profile);
821
822 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:27823 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:48824 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:27825 ASSERT_TRUE(ephemeral_profile1);
826 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
827 true);
828
829 // Add second ephemeral profile but don't mark it as such yet.
830 TestingProfile* ephemeral_profile2 =
831 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
832 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:48833
834 // Create a browser for profile1.
835 Browser::CreateParams profile1_params(normal_profile,
836 chrome::GetActiveDesktop());
837 scoped_ptr<Browser> browser1(
838 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
839
[email protected]79fd6b72013-10-22 16:39:27840 // Create browsers for the ephemeral profile.
841 Browser::CreateParams profile2_params(ephemeral_profile1,
[email protected]8d9243a2013-10-21 17:11:48842 chrome::GetActiveDesktop());
843 scoped_ptr<Browser> browser2(
844 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
845
[email protected]79fd6b72013-10-22 16:39:27846 Browser::CreateParams profile3_params(ephemeral_profile2,
847 chrome::GetActiveDesktop());
848 scoped_ptr<Browser> browser3(
849 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
850
[email protected]8d9243a2013-10-21 17:11:48851 std::vector<Profile*> last_opened_profiles =
852 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:27853 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:48854 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:27855 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
856
857 // Mark the second profile ephemeral.
858 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
859 true);
[email protected]8d9243a2013-10-21 17:11:48860
861 // Simulate a shutdown.
862 content::NotificationService::current()->Notify(
863 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
864 content::NotificationService::AllSources(),
865 content::NotificationService::NoDetails());
866 browser1.reset();
867 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:27868 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:48869
870 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
871 ASSERT_EQ(1U, last_opened_profiles.size());
872 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
873}
874
[email protected]1c62b2f2013-06-28 00:15:00875TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
876 ProfileManager* profile_manager = g_browser_process->profile_manager();
877 ASSERT_TRUE(profile_manager);
878
879 // Create and load two profiles.
880 const std::string profile_name1 = "New Profile 1";
881 const std::string profile_name2 = "New Profile 2";
[email protected]3f130532014-02-26 20:39:24882 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
883 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:00884
885 MockObserver mock_observer;
886 EXPECT_CALL(mock_observer, OnProfileCreated(
887 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
888
[email protected]5ddfade2014-02-03 10:24:53889 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
890 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35891 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00892
893 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
894 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
895
896 // Set the active profile.
897 PrefService* local_state = g_browser_process->local_state();
898 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
899
900 // Delete the active profile.
901 profile_manager->ScheduleProfileForDeletion(dest_path1,
902 ProfileManager::CreateCallback());
903 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35904 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00905
906 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
907 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
908}
[email protected]3f130532014-02-26 20:39:24909
[email protected]602b5a29f2014-07-18 15:39:33910TEST_F(ProfileManagerTest, LastProfileDeleted) {
911 ProfileManager* profile_manager = g_browser_process->profile_manager();
912 ASSERT_TRUE(profile_manager);
913
914 // Create and load a profile.
915 const std::string profile_name1 = "New Profile 1";
916 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
917
918 MockObserver mock_observer;
919 EXPECT_CALL(mock_observer, OnProfileCreated(
920 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
921
922 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
923 base::RunLoop().RunUntilIdle();
924
925 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
926 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
927
928 // Set it as the active profile.
929 PrefService* local_state = g_browser_process->local_state();
930 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
931
932 // Delete the active profile.
933 profile_manager->ScheduleProfileForDeletion(dest_path1,
934 ProfileManager::CreateCallback());
935 // Spin the message loop so that all the callbacks can finish running.
936 base::RunLoop().RunUntilIdle();
937
938 // A new profile should have been created
939 const std::string profile_name2 = "Profile 1";
940 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
941
942 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
943 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
944 EXPECT_EQ(dest_path2,
945 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
946}
947
948TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
949 ProfileManager* profile_manager = g_browser_process->profile_manager();
950 ASSERT_TRUE(profile_manager);
951
952 // Create and load a profile.
953 const std::string profile_name1 = "New Profile 1";
954 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
955
956 MockObserver mock_observer;
957 EXPECT_CALL(mock_observer, OnProfileCreated(
958 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
959
960 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
961 base::RunLoop().RunUntilIdle();
962
963 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
964 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
965
966 // Create the profile and register it.
967 const std::string guest_profile_name =
968 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
969
970 TestingProfile::Builder builder;
971 builder.SetGuestSession();
972 builder.SetPath(ProfileManager::GetGuestProfilePath());
973 TestingProfile* guest_profile = builder.Build().release();
974 guest_profile->set_profile_name(guest_profile_name);
975 // Registering the profile passes ownership to the ProfileManager.
976 profile_manager->RegisterTestingProfile(guest_profile, false, false);
977
978 // The Guest profile does not get added to the ProfileInfoCache.
979 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
980 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
981
982 // Set the Guest profile as the active profile.
983 PrefService* local_state = g_browser_process->local_state();
984 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
985
986 // Delete the other profile.
987 profile_manager->ScheduleProfileForDeletion(dest_path1,
988 ProfileManager::CreateCallback());
989 // Spin the message loop so that all the callbacks can finish running.
990 base::RunLoop().RunUntilIdle();
991
992 // A new profile should have been created.
993 const std::string profile_name2 = "Profile 1";
994 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
995
996 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
997 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
998 EXPECT_EQ(dest_path2,
999 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
1000}
1001
[email protected]3f130532014-02-26 20:39:241002TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1003 if (!profiles::IsMultipleProfilesEnabled())
1004 return;
1005
1006 // The command line is reset at the end of every test by the test suite.
[email protected]c9335412014-07-28 17:47:251007 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
[email protected]3f130532014-02-26 20:39:241008
1009 ProfileManager* profile_manager = g_browser_process->profile_manager();
1010 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1011 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1012
1013 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1014 const base::string16 default_profile_name =
1015 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1016 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1017 Profile* profile1 = AddProfileToCache(profile_manager,
1018 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441019 EXPECT_EQ(default_profile_name,
1020 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241021
1022 // Multiple profiles means displaying the actual profile names.
1023 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1024 Profile* profile2 = AddProfileToCache(profile_manager,
1025 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441026 EXPECT_EQ(profile_name1,
1027 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1028 EXPECT_EQ(profile_name2,
1029 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241030
1031 // Deleting a profile means returning to the default name.
1032 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1033 ProfileManager::CreateCallback());
1034 // Spin the message loop so that all the callbacks can finish running.
1035 base::RunLoop().RunUntilIdle();
[email protected]119809d2014-06-27 22:12:441036 EXPECT_EQ(default_profile_name,
1037 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241038}
1039
1040TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1041 if (!profiles::IsMultipleProfilesEnabled())
1042 return;
1043
1044 // The command line is reset at the end of every test by the test suite.
[email protected]c9335412014-07-28 17:47:251045 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
[email protected]3f130532014-02-26 20:39:241046
1047 ProfileManager* profile_manager = g_browser_process->profile_manager();
1048 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1049 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1050
1051 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1052 const base::string16 default_profile_name =
1053 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1054 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1055 Profile* profile1 = AddProfileToCache(profile_manager,
1056 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441057 EXPECT_EQ(default_profile_name,
1058 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241059
1060 // We should display custom names for local profiles.
1061 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
1062 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
[email protected]1cdf0932014-08-16 06:00:491063 cache.SetProfileIsUsingDefaultNameAtIndex(0, false);
[email protected]3f130532014-02-26 20:39:241064 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
[email protected]119809d2014-06-27 22:12:441065 EXPECT_EQ(custom_profile_name,
1066 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241067
1068 // Multiple profiles means displaying the actual profile names.
1069 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1070 Profile* profile2 = AddProfileToCache(profile_manager,
1071 "path_2", profile_name2);
[email protected]119809d2014-06-27 22:12:441072 EXPECT_EQ(custom_profile_name,
1073 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1074 EXPECT_EQ(profile_name2,
1075 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241076
1077 // Deleting a profile means returning to the original, custom name.
1078 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1079 ProfileManager::CreateCallback());
1080 // Spin the message loop so that all the callbacks can finish running.
1081 base::RunLoop().RunUntilIdle();
[email protected]119809d2014-06-27 22:12:441082 EXPECT_EQ(custom_profile_name,
1083 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241084}
1085
1086TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1087 if (!profiles::IsMultipleProfilesEnabled())
1088 return;
1089
[email protected]29896ee2014-06-17 17:20:531090 // The command line is reset at the end of every test by the test suite.
[email protected]c9335412014-07-28 17:47:251091 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
[email protected]3f130532014-02-26 20:39:241092
1093 ProfileManager* profile_manager = g_browser_process->profile_manager();
1094 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1095 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1096
1097 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1098 const base::string16 default_profile_name =
1099 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1100 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1101 Profile* profile1 = AddProfileToCache(profile_manager,
1102 "path_1", profile_name1);
[email protected]119809d2014-06-27 22:12:441103 EXPECT_EQ(default_profile_name,
1104 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241105
[email protected]78186a42014-08-22 22:07:301106 // For a signed in profile with a default name we still display
1107 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
[email protected]3f130532014-02-26 20:39:241108 cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("[email protected]"));
1109 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
[email protected]78186a42014-08-22 22:07:301110 EXPECT_EQ(default_profile_name,
[email protected]119809d2014-06-27 22:12:441111 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241112
[email protected]78186a42014-08-22 22:07:301113 // For a signed in profile with a non-default Gaia given name we display the
1114 // Gaia given name.
1115 cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("[email protected]"));
1116 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
1117 cache.SetGAIAGivenNameOfProfileAtIndex(0, gaia_given_name);
1118 EXPECT_EQ(gaia_given_name, cache.GetNameOfProfileAtIndex(0));
1119 EXPECT_EQ(gaia_given_name,
1120 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1121
[email protected]3f130532014-02-26 20:39:241122 // Multiple profiles means displaying the actual profile names.
1123 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1124 Profile* profile2 = AddProfileToCache(profile_manager,
1125 "path_2", profile_name2);
[email protected]78186a42014-08-22 22:07:301126 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441127 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1128 EXPECT_EQ(profile_name2,
1129 profiles::GetAvatarNameForProfile(profile2->GetPath()));
[email protected]3f130532014-02-26 20:39:241130
1131 // Deleting a profile means returning to the original, actual profile name.
1132 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1133 ProfileManager::CreateCallback());
1134 // Spin the message loop so that all the callbacks can finish running.
1135 base::RunLoop().RunUntilIdle();
[email protected]78186a42014-08-22 22:07:301136 EXPECT_EQ(gaia_given_name,
[email protected]119809d2014-06-27 22:12:441137 profiles::GetAvatarNameForProfile(profile1->GetPath()));
[email protected]3f130532014-02-26 20:39:241138}
[email protected]1c62b2f2013-06-28 00:15:001139#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1140
1141#if defined(OS_MACOSX)
1142// These tests are for a Mac-only code path that assumes the browser
1143// process isn't killed when all browser windows are closed.
1144TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1145 ProfileManager* profile_manager = g_browser_process->profile_manager();
1146 ASSERT_TRUE(profile_manager);
1147
1148 // Create and load one profile, and just create a second profile.
1149 const std::string profile_name1 = "New Profile 1";
1150 const std::string profile_name2 = "New Profile 2";
[email protected]3f130532014-02-26 20:39:241151 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1152 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
[email protected]1c62b2f2013-06-28 00:15:001153
1154 MockObserver mock_observer;
1155 EXPECT_CALL(mock_observer, OnProfileCreated(
1156 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531157 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:351158 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001159
1160 // Track the profile, but don't load it.
1161 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1162 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a04db822013-12-11 19:14:401163 base::string16(), 0, std::string());
[email protected]37ac95b2013-07-23 23:39:351164 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001165
1166 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1167 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
1168
1169 // Set the active profile.
1170 PrefService* local_state = g_browser_process->local_state();
1171 local_state->SetString(prefs::kProfileLastUsed,
1172 dest_path1.BaseName().MaybeAsASCII());
1173
1174 // Delete the active profile. This should switch and load the unloaded
1175 // profile.
1176 profile_manager->ScheduleProfileForDeletion(dest_path1,
1177 ProfileManager::CreateCallback());
1178
1179 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:351180 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001181
1182 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1183 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1184}
1185
1186// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
1187// by simulating a scenario in which the profile that is being loaded as
1188// the next active profile has also been marked for deletion, so the
1189// ProfileManager needs to recursively select a different next profile.
1190TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
1191 ProfileManager* profile_manager = g_browser_process->profile_manager();
1192 ASSERT_TRUE(profile_manager);
1193
1194 // Create and load one profile, and create two more profiles.
1195 const std::string profile_name1 = "New Profile 1";
1196 const std::string profile_name2 = "New Profile 2";
1197 const std::string profile_name3 = "New Profile 3";
[email protected]3f130532014-02-26 20:39:241198 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1199 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1200 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
[email protected]1c62b2f2013-06-28 00:15:001201
1202 MockObserver mock_observer;
1203 EXPECT_CALL(mock_observer, OnProfileCreated(
1204 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:531205 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:351206 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001207
1208 // Create the other profiles, but don't load them. Assign a fake avatar icon
1209 // to ensure that profiles in the info cache are sorted by the profile name,
1210 // and not randomly by the avatar name.
1211 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1212 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a6e01b42013-08-05 14:13:131213 ASCIIToUTF16(profile_name2), 1, std::string());
[email protected]1c62b2f2013-06-28 00:15:001214 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
[email protected]a6e01b42013-08-05 14:13:131215 ASCIIToUTF16(profile_name3), 2, std::string());
[email protected]1c62b2f2013-06-28 00:15:001216
[email protected]37ac95b2013-07-23 23:39:351217 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001218
1219 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1220 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1221
1222 // Set the active profile.
1223 PrefService* local_state = g_browser_process->local_state();
1224 local_state->SetString(prefs::kProfileLastUsed,
1225 dest_path1.BaseName().MaybeAsASCII());
1226
1227 // Delete the active profile, Profile1.
1228 // This will post a CreateProfileAsync message, that tries to load Profile2,
1229 // which checks that the profile is not being deleted, and then calls back
1230 // FinishDeletingProfile for Profile1.
1231 // Try to break this flow by setting the active profile to Profile2 in the
1232 // middle (so after the first posted message), and trying to delete Profile2,
1233 // so that the ProfileManager has to look for a different profile to load.
1234 profile_manager->ScheduleProfileForDeletion(dest_path1,
1235 ProfileManager::CreateCallback());
1236 local_state->SetString(prefs::kProfileLastUsed,
1237 dest_path2.BaseName().MaybeAsASCII());
1238 profile_manager->ScheduleProfileForDeletion(dest_path2,
1239 ProfileManager::CreateCallback());
1240 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:351241 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:001242
1243 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1244 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1245}
1246#endif // !defined(OS_MACOSX)