blob: a6509987e4ce01c115ec781d61c6f0f93f54d16e [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"
[email protected]537c1082011-12-02 02:37:178#include "base/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]279170832011-10-12 23:38:0325#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]8ecad5e2010-12-02 21:18:3326#include "chrome/browser/profiles/profile_manager.h"
[email protected]d8748142012-05-16 21:13:4327#include "chrome/browser/ui/browser.h"
[email protected]943aed5542010-06-11 15:57:0828#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0830#include "chrome/common/chrome_switches.h"
[email protected]29d70252011-04-28 02:16:5831#include "chrome/common/pref_names.h"
[email protected]7688968a2013-02-12 21:45:1332#include "chrome/test/base/scoped_testing_local_state.h"
[email protected]a4fe67012012-07-25 20:14:2933#include "chrome/test/base/test_browser_window.h"
[email protected]583844c2011-08-27 00:38:3534#include "chrome/test/base/testing_browser_process.h"
[email protected]537c1082011-12-02 02:37:1735#include "chrome/test/base/testing_profile.h"
[email protected]ad50def52011-10-19 23:17:0736#include "content/public/browser/notification_service.h"
[email protected]37ac95b2013-07-23 23:39:3537#include "content/public/test/test_browser_thread_bundle.h"
[email protected]844a1002011-04-19 11:37:2138#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2939#include "testing/gtest/include/gtest/gtest.h"
40
[email protected]775caec2011-09-01 17:14:3241#if defined(OS_CHROMEOS)
[email protected]eb5bb2e2013-04-15 20:09:1942#include "chrome/browser/chromeos/login/mock_user_manager.h"
[email protected]9a68d3a2013-04-22 16:26:5443#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]363f5272013-04-23 17:21:4244#include "chrome/browser/chromeos/settings/cros_settings.h"
[email protected]e4854dc2013-04-24 00:11:5145#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]931d1042013-04-05 17:50:4446#include "chromeos/chromeos_switches.h"
[email protected]775caec2011-09-01 17:14:3247#endif
48
[email protected]6778fed2013-12-24 20:09:3749using base::ASCIIToUTF16;
[email protected]631bb742011-11-02 11:29:3950using content::BrowserThread;
51
[email protected]844a1002011-04-19 11:37:2152namespace {
[email protected]f4e706252012-01-10 17:11:3653
[email protected]844a1002011-04-19 11:37:2154// This global variable is used to check that value returned to different
55// observers is the same.
56Profile* g_created_profile;
57
[email protected]9819fd02013-08-22 10:49:3958class UnittestProfileManager : public ::ProfileManagerWithoutInit {
[email protected]537c1082011-12-02 02:37:1759 public:
[email protected]9819fd02013-08-22 10:49:3960 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
[email protected]537c1082011-12-02 02:37:1761 : ::ProfileManagerWithoutInit(user_data_dir) {}
62
63 protected:
[email protected]650b2d52013-02-10 03:41:4564 virtual Profile* CreateProfileHelper(
65 const base::FilePath& file_path) OVERRIDE {
[email protected]7567484142013-07-11 17:36:0766 if (!base::PathExists(file_path)) {
[email protected]426d1c92013-12-03 20:08:5467 if (!base::CreateDirectory(file_path))
[email protected]537c1082011-12-02 02:37:1768 return NULL;
69 }
70 return new TestingProfile(file_path, NULL);
71 }
72
[email protected]650b2d52013-02-10 03:41:4573 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:1774 Delegate* delegate) OVERRIDE {
75 // This is safe while all file operations are done on the FILE thread.
76 BrowserThread::PostTask(
77 BrowserThread::FILE, FROM_HERE,
[email protected]426d1c92013-12-03 20:08:5478 base::Bind(base::IgnoreResult(&base::CreateDirectory), path));
[email protected]537c1082011-12-02 02:37:1779
80 return new TestingProfile(path, this);
81 }
82};
83
[email protected]9819fd02013-08-22 10:49:3984} // namespace
[email protected]537c1082011-12-02 02:37:1785
[email protected]583844c2011-08-27 00:38:3586class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:4787 protected:
[email protected]f4e706252012-01-10 17:11:3688 class MockObserver {
89 public:
90 MOCK_METHOD2(OnProfileCreated,
91 void(Profile* profile, Profile::CreateStatus status));
92 };
93
[email protected]6cad5bf2011-03-10 21:21:5594 ProfileManagerTest()
[email protected]37ac95b2013-07-23 23:39:3595 : local_state_(TestingBrowserProcess::GetGlobal()) {
[email protected]cb0e4f12009-12-03 00:09:0996 }
97
initial.commit09911bf2008-07-26 23:55:2998 virtual void SetUp() {
[email protected]3a305db2011-04-12 13:40:5399 // Create a new temporary directory, and store the path
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]c494d082013-01-04 20:41:22101 TestingBrowserProcess::GetGlobal()->SetProfileManager(
[email protected]9819fd02013-08-22 10:49:39102 new UnittestProfileManager(temp_dir_.path()));
[email protected]f89b50f2012-08-16 21:36:50103
[email protected]537c1082011-12-02 02:37:17104#if defined(OS_CHROMEOS)
[email protected]19506d542013-10-15 23:11:06105 CommandLine* cl = CommandLine::ForCurrentProcess();
106 cl->AppendSwitch(switches::kTestType);
[email protected]537c1082011-12-02 02:37:17107#endif
initial.commit09911bf2008-07-26 23:55:29108 }
[email protected]81ad7f4a2011-03-16 01:33:29109
initial.commit09911bf2008-07-26 23:55:29110 virtual void TearDown() {
[email protected]c494d082013-01-04 20:41:22111 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]37ac95b2013-07-23 23:39:35112 base::RunLoop().RunUntilIdle();
initial.commit09911bf2008-07-26 23:55:29113 }
[email protected]f0a51fb52009-03-05 12:46:38114
[email protected]1c62b2f2013-06-28 00:15:00115 // Helper function to create a profile with |name| for a profile |manager|.
116 void CreateProfileAsync(ProfileManager* manager,
117 const std::string& name,
[email protected]5ddfade2014-02-03 10:24:53118 bool is_managed,
[email protected]1c62b2f2013-06-28 00:15:00119 MockObserver* mock_observer) {
120 manager->CreateProfileAsync(
121 temp_dir_.path().AppendASCII(name),
122 base::Bind(&MockObserver::OnProfileCreated,
123 base::Unretained(mock_observer)),
[email protected]6778fed2013-12-24 20:09:37124 base::UTF8ToUTF16(name),
[email protected]5ddfade2014-02-03 10:24:53125 base::UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(0)),
126 is_managed ? "Dummy ID" : std::string());
[email protected]1c62b2f2013-06-28 00:15:00127 }
128
[email protected]6f96cbcb2011-11-04 02:26:07129#if defined(OS_CHROMEOS)
[email protected]e4854dc2013-04-24 00:11:51130 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
[email protected]363f5272013-04-23 17:21:42131 chromeos::ScopedTestCrosSettings test_cros_settings_;
[email protected]6f96cbcb2011-11-04 02:26:07132#endif
133
[email protected]3a305db2011-04-12 13:40:53134 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:23135 base::ScopedTempDir temp_dir_;
[email protected]63e26822011-07-16 19:07:35136 ScopedTestingLocalState local_state_;
[email protected]37ac95b2013-07-23 23:39:35137
138 content::TestBrowserThreadBundle thread_bundle_;
[email protected]363f5272013-04-23 17:21:42139
140#if defined(OS_CHROMEOS)
141 chromeos::ScopedTestUserManager test_user_manager_;
142#endif
initial.commit09911bf2008-07-26 23:55:29143};
144
[email protected]844a1002011-04-19 11:37:21145TEST_F(ProfileManagerTest, GetProfile) {
[email protected]650b2d52013-02-10 03:41:45146 base::FilePath dest_path = temp_dir_.path();
[email protected]f7011fcb2009-01-28 21:54:32147 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:29148
[email protected]881cb0b62011-12-13 20:39:54149 ProfileManager* profile_manager = g_browser_process->profile_manager();
150
initial.commit09911bf2008-07-26 23:55:29151 // Successfully create a profile.
[email protected]f4e706252012-01-10 17:11:36152 Profile* profile = profile_manager->GetProfile(dest_path);
[email protected]844a1002011-04-19 11:37:21153 EXPECT_TRUE(profile);
initial.commit09911bf2008-07-26 23:55:29154
[email protected]844a1002011-04-19 11:37:21155 // The profile already exists when we call GetProfile. Just load it.
[email protected]881cb0b62011-12-13 20:39:54156 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:29157}
[email protected]55474b572009-04-14 22:05:33158
[email protected]943aed5542010-06-11 15:57:08159TEST_F(ProfileManagerTest, DefaultProfileDir) {
[email protected]650b2d52013-02-10 03:41:45160 base::FilePath expected_default =
161 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54162 EXPECT_EQ(
163 expected_default.value(),
164 g_browser_process->profile_manager()->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08165}
166
167#if defined(OS_CHROMEOS)
168// This functionality only exists on Chrome OS.
169TEST_F(ProfileManagerTest, LoggedInProfileDir) {
170 CommandLine *cl = CommandLine::ForCurrentProcess();
[email protected]9c8b1f92013-09-19 14:39:59171 std::string profile_dir(chrome::kTestUserProfileDir);
[email protected]943aed5542010-06-11 15:57:08172
[email protected]931d1042013-04-05 17:50:44173 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir);
[email protected]943aed5542010-06-11 15:57:08174
[email protected]650b2d52013-02-10 03:41:45175 base::FilePath expected_default =
176 base::FilePath().AppendASCII(chrome::kInitialProfile);
[email protected]881cb0b62011-12-13 20:39:54177 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]943aed5542010-06-11 15:57:08178 EXPECT_EQ(expected_default.value(),
[email protected]881cb0b62011-12-13 20:39:54179 profile_manager->GetInitialProfileDir().value());
[email protected]943aed5542010-06-11 15:57:08180
[email protected]eb5bb2e2013-04-15 20:09:19181 scoped_ptr<chromeos::MockUserManager> mock_user_manager;
182 mock_user_manager.reset(new chromeos::MockUserManager());
183 mock_user_manager->SetActiveUser("[email protected]");
184 chromeos::User* active_user = mock_user_manager->GetActiveUser();
[email protected]881cb0b62011-12-13 20:39:54185 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
[email protected]ad50def52011-10-19 23:17:07186 content::NotificationService::AllSources(),
[email protected]eb5bb2e2013-04-15 20:09:19187 content::Details<const chromeos::User>(active_user));
[email protected]650b2d52013-02-10 03:41:45188 base::FilePath expected_logged_in(profile_dir);
[email protected]943aed5542010-06-11 15:57:08189 EXPECT_EQ(expected_logged_in.value(),
[email protected]881cb0b62011-12-13 20:39:54190 profile_manager->GetInitialProfileDir().value());
[email protected]3a305db2011-04-12 13:40:53191 VLOG(1) << temp_dir_.path().Append(
[email protected]881cb0b62011-12-13 20:39:54192 profile_manager->GetInitialProfileDir()).value();
[email protected]943aed5542010-06-11 15:57:08193}
194
195#endif
196
[email protected]52d69b882010-06-17 17:35:51197TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
[email protected]650b2d52013-02-10 03:41:45198 base::FilePath dest_path1 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33199 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47200
[email protected]650b2d52013-02-10 03:41:45201 base::FilePath dest_path2 = temp_dir_.path();
[email protected]55474b572009-04-14 22:05:33202 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
203
[email protected]881cb0b62011-12-13 20:39:54204 ProfileManager* profile_manager = g_browser_process->profile_manager();
205
[email protected]55474b572009-04-14 22:05:33206 // Successfully create the profiles.
[email protected]537c1082011-12-02 02:37:17207 TestingProfile* profile1 =
[email protected]881cb0b62011-12-13 20:39:54208 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]844a1002011-04-19 11:37:21209 ASSERT_TRUE(profile1);
[email protected]55474b572009-04-14 22:05:33210
[email protected]537c1082011-12-02 02:37:17211 TestingProfile* profile2 =
[email protected]881cb0b62011-12-13 20:39:54212 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]844a1002011-04-19 11:37:21213 ASSERT_TRUE(profile2);
[email protected]55474b572009-04-14 22:05:33214
215 // Force lazy-init of some profile services to simulate use.
[email protected]608e7e02013-07-24 12:23:31216 ASSERT_TRUE(profile1->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53217 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1,
218 Profile::EXPLICIT_ACCESS));
[email protected]537c1082011-12-02 02:37:17219 profile1->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49220 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1));
[email protected]537c1082011-12-02 02:37:17221 profile2->CreateBookmarkModel(true);
[email protected]5114d182012-07-30 23:09:49222 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2));
[email protected]608e7e02013-07-24 12:23:31223 ASSERT_TRUE(profile2->CreateHistoryService(true, false));
[email protected]9718a722012-06-19 20:10:53224 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2,
225 Profile::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55226
227 // Make sure any pending tasks run before we destroy the profiles.
[email protected]37ac95b2013-07-23 23:39:35228 base::RunLoop().RunUntilIdle();
[email protected]6cad5bf2011-03-10 21:21:55229
[email protected]c494d082013-01-04 20:41:22230 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]6cad5bf2011-03-10 21:21:55231
[email protected]237e6d02010-11-08 21:45:42232 // Make sure history cleans up correctly.
[email protected]37ac95b2013-07-23 23:39:35233 base::RunLoop().RunUntilIdle();
[email protected]55474b572009-04-14 22:05:33234}
[email protected]844a1002011-04-19 11:37:21235
[email protected]e547ac82011-07-25 12:34:11236MATCHER(NotFail, "Profile creation failure status is not reported.") {
[email protected]b8ba8d42011-12-07 22:13:59237 return arg == Profile::CREATE_STATUS_CREATED ||
238 arg == Profile::CREATE_STATUS_INITIALIZED;
[email protected]e547ac82011-07-25 12:34:11239}
240
[email protected]e547ac82011-07-25 12:34:11241MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
[email protected]844a1002011-04-19 11:37:21242 if (!g_created_profile)
243 g_created_profile = arg;
[email protected]e547ac82011-07-25 12:34:11244 return arg != NULL && arg == g_created_profile;
[email protected]844a1002011-04-19 11:37:21245}
246
247TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
[email protected]844a1002011-04-19 11:37:21248 g_created_profile = NULL;
249
250 MockObserver mock_observer1;
[email protected]e547ac82011-07-25 12:34:11251 EXPECT_CALL(mock_observer1, OnProfileCreated(
252 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21253 MockObserver mock_observer2;
[email protected]e547ac82011-07-25 12:34:11254 EXPECT_CALL(mock_observer2, OnProfileCreated(
255 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21256 MockObserver mock_observer3;
[email protected]e547ac82011-07-25 12:34:11257 EXPECT_CALL(mock_observer3, OnProfileCreated(
258 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
[email protected]844a1002011-04-19 11:37:21259
[email protected]881cb0b62011-12-13 20:39:54260 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]1c62b2f2013-06-28 00:15:00261 const std::string profile_name = "New Profile";
[email protected]5ddfade2014-02-03 10:24:53262 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1);
263 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2);
264 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3);
[email protected]844a1002011-04-19 11:37:21265
[email protected]37ac95b2013-07-23 23:39:35266 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21267}
268
269TEST_F(ProfileManagerTest, CreateProfilesAsync) {
[email protected]1c62b2f2013-06-28 00:15:00270 const std::string profile_name1 = "New Profile 1";
271 const std::string profile_name2 = "New Profile 2";
[email protected]844a1002011-04-19 11:37:21272
273 MockObserver mock_observer;
[email protected]e547ac82011-07-25 12:34:11274 EXPECT_CALL(mock_observer, OnProfileCreated(
275 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
[email protected]844a1002011-04-19 11:37:21276
[email protected]881cb0b62011-12-13 20:39:54277 ProfileManager* profile_manager = g_browser_process->profile_manager();
278
[email protected]5ddfade2014-02-03 10:24:53279 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
280 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]844a1002011-04-19 11:37:21281
[email protected]37ac95b2013-07-23 23:39:35282 base::RunLoop().RunUntilIdle();
[email protected]844a1002011-04-19 11:37:21283}
[email protected]279170832011-10-12 23:38:03284
[email protected]5ddfade2014-02-03 10:24:53285TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
286 std::string name = "Managed Profile";
287
288 MockObserver mock_observer;
289 EXPECT_CALL(mock_observer, OnProfileCreated(
290 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
291
292 ProfileManager* profile_manager = g_browser_process->profile_manager();
293 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
294 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
295
296 CreateProfileAsync(profile_manager, name, true, &mock_observer);
297 base::RunLoop().RunUntilIdle();
298
299 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
300 // Managed profiles should start out omitted from the profile list.
301 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
302
303 name = "Regular Profile";
304 CreateProfileAsync(profile_manager, name, false, &mock_observer);
305 base::RunLoop().RunUntilIdle();
306
307 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
308 // Non-managed profiles should be included in the profile list.
309 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
310}
311
312TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
313 ProfileManager* profile_manager = g_browser_process->profile_manager();
314 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
315 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
316
317 const base::FilePath managed_path = temp_dir_.path().AppendASCII("Managed");
318 TestingProfile* managed_profile = new TestingProfile(managed_path, NULL);
319 managed_profile->GetPrefs()->SetString(prefs::kManagedUserId, "An ID");
320
321 // RegisterTestingProfile adds the profile to the cache and takes ownership.
322 profile_manager->RegisterTestingProfile(managed_profile, true, false);
323 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
324 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
325
326 const base::FilePath nonmanaged_path = temp_dir_.path().AppendASCII(
327 "Non-Managed");
328 TestingProfile* nonmanaged_profile = new TestingProfile(nonmanaged_path,
329 NULL);
330 profile_manager->RegisterTestingProfile(nonmanaged_profile, true, false);
331
332 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
333 size_t managed_index = cache.GetIndexOfProfileWithPath(managed_path);
334 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(managed_index));
335 size_t nonmanaged_index = cache.GetIndexOfProfileWithPath(nonmanaged_path);
336 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonmanaged_index));
337}
338
[email protected]4251165a2013-07-17 04:33:40339TEST_F(ProfileManagerTest, GetGuestProfilePath) {
340 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
341 base::FilePath expected_path = temp_dir_.path();
342 expected_path = expected_path.Append(chrome::kGuestProfileDir);
343 EXPECT_EQ(expected_path, guest_path);
344}
345
[email protected]0ff6198e2014-01-08 22:45:00346#if defined(OS_CHROMEOS)
347class UnittestGuestProfileManager : public UnittestProfileManager {
348 public:
349 explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir)
350 : UnittestProfileManager(user_data_dir) {}
351
352 protected:
353 virtual Profile* CreateProfileHelper(
354 const base::FilePath& file_path) OVERRIDE {
355 TestingProfile* testing_profile = new TestingProfile(file_path, NULL);
356
357 TestingProfile::Builder builder;
358 builder.SetIncognito();
359 builder.SetGuestSession();
360 builder.SetPath(ProfileManager::GetGuestProfilePath());
361 testing_profile->SetOffTheRecordProfile(builder.Build().PassAs<Profile>());
362
363 return testing_profile;
364 }
365};
366
367class ProfileManagerGuestTest : public ProfileManagerTest {
368 protected:
369 virtual void SetUp() {
370 // Create a new temporary directory, and store the path
371 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
372 TestingBrowserProcess::GetGlobal()->SetProfileManager(
373 new UnittestGuestProfileManager(temp_dir_.path()));
374
375 CommandLine* cl = CommandLine::ForCurrentProcess();
376 cl->AppendSwitch(switches::kTestType);
377 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
378 cl->AppendSwitch(chromeos::switches::kGuestSession);
379 cl->AppendSwitch(::switches::kIncognito);
380
381 chromeos::UserManager::Get()->UserLoggedIn(
382 chromeos::UserManager::kGuestUserName,
383 chromeos::UserManager::kGuestUserName,
384 false);
385 }
386};
387
388TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) {
389 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
390 EXPECT_TRUE(primary_profile->IsOffTheRecord());
391
392 Profile* active_profile = ProfileManager::GetActiveUserProfile();
393 EXPECT_TRUE(active_profile->IsOffTheRecord());
394
395 EXPECT_TRUE(active_profile->IsSameProfile(primary_profile));
396}
397#endif
398
[email protected]279170832011-10-12 23:38:03399TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
[email protected]881cb0b62011-12-13 20:39:54400 ProfileManager* profile_manager = g_browser_process->profile_manager();
401 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
[email protected]714bf1d2012-11-30 01:35:27402 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]dbb9aa42013-12-23 20:08:21403 base::Value::CreateBooleanValue(true));
[email protected]714bf1d2012-11-30 01:35:27404
405 // Setting a pref which is not applicable to a system (i.e., Android in this
406 // case) does not necessarily create it. Don't bother continuing with the
407 // test if this pref doesn't exist because it will not load the profiles if
408 // it cannot verify that the pref for background mode is enabled.
409 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
410 return;
[email protected]279170832011-10-12 23:38:03411
412 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
413 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40414 ASCIIToUTF16("name_1"), base::string16(), 0,
415 std::string());
[email protected]279170832011-10-12 23:38:03416 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40417 ASCIIToUTF16("name_2"), base::string16(), 0,
418 std::string());
[email protected]279170832011-10-12 23:38:03419 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
[email protected]a04db822013-12-11 19:14:40420 ASCIIToUTF16("name_3"), base::string16(), 0,
421 std::string());
[email protected]279170832011-10-12 23:38:03422 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
423 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
424 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
425
[email protected]881cb0b62011-12-13 20:39:54426 profile_manager->AutoloadProfiles();
[email protected]279170832011-10-12 23:38:03427
[email protected]881cb0b62011-12-13 20:39:54428 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
429}
430
[email protected]714bf1d2012-11-30 01:35:27431TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
432 ProfileManager* profile_manager = g_browser_process->profile_manager();
433 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
434 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
[email protected]dbb9aa42013-12-23 20:08:21435 base::Value::CreateBooleanValue(false));
[email protected]714bf1d2012-11-30 01:35:27436
437 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
438 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
[email protected]a04db822013-12-11 19:14:40439 ASCIIToUTF16("name_1"), base::string16(), 0,
440 std::string());
[email protected]714bf1d2012-11-30 01:35:27441 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
[email protected]a04db822013-12-11 19:14:40442 ASCIIToUTF16("name_2"), base::string16(), 0,
443 std::string());
[email protected]714bf1d2012-11-30 01:35:27444 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
445 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
446 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
447
448 profile_manager->AutoloadProfiles();
449
450 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
451}
452
[email protected]881cb0b62011-12-13 20:39:54453TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
[email protected]650b2d52013-02-10 03:41:45454 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54455 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
456
457 ProfileManager* profile_manager = g_browser_process->profile_manager();
458
459 Profile* profile;
460
461 // Successfully create the profile
462 profile = profile_manager->GetProfile(dest_path);
463 ASSERT_TRUE(profile);
464
465 // Check that the profile name is non empty
466 std::string profile_name =
467 profile->GetPrefs()->GetString(prefs::kProfileName);
468 EXPECT_FALSE(profile_name.empty());
469
470 // Check that the profile avatar index is valid
471 size_t avatar_index =
472 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
473 EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex(
474 avatar_index));
475}
476
477// Tests that a new profile's entry in the profile info cache is setup with the
478// same values that are in the profile prefs.
479TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
[email protected]650b2d52013-02-10 03:41:45480 base::FilePath dest_path = temp_dir_.path();
[email protected]881cb0b62011-12-13 20:39:54481 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
482
483 ProfileManager* profile_manager = g_browser_process->profile_manager();
484 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
485
486 // Successfully create the profile
487 Profile* profile = profile_manager->GetProfile(dest_path);
488 ASSERT_TRUE(profile);
489
490 std::string profile_name =
491 profile->GetPrefs()->GetString(prefs::kProfileName);
492 size_t avatar_index =
493 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
494
495 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
496
497 // Check if the profile prefs are the same as the cache prefs
498 EXPECT_EQ(profile_name,
[email protected]6778fed2013-12-24 20:09:37499 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
[email protected]881cb0b62011-12-13 20:39:54500 EXPECT_EQ(avatar_index,
501 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
[email protected]279170832011-10-12 23:38:03502}
[email protected]52edf812012-01-12 11:48:46503
[email protected]13720532013-05-30 12:30:59504TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
505 ProfileManager* profile_manager = g_browser_process->profile_manager();
506 ASSERT_TRUE(profile_manager);
507
508 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy();
509 ASSERT_TRUE(profile);
510 EXPECT_FALSE(profile->IsOffTheRecord());
511 PrefService* prefs = profile->GetPrefs();
512 EXPECT_EQ(IncognitoModePrefs::ENABLED,
513 IncognitoModePrefs::GetAvailability(prefs));
514
515 // Attach an incognito Profile to the TestingProfile.
516 ASSERT_FALSE(profile->GetOffTheRecordProfile());
[email protected]9819fd02013-08-22 10:49:39517 TestingProfile::Builder builder;
518 builder.SetIncognito();
519 scoped_ptr<TestingProfile> incognito_profile = builder.Build();
[email protected]13720532013-05-30 12:30:59520 EXPECT_TRUE(incognito_profile->IsOffTheRecord());
521 TestingProfile* testing_profile = static_cast<TestingProfile*>(profile);
[email protected]9819fd02013-08-22 10:49:39522 testing_profile->SetOffTheRecordProfile(incognito_profile.PassAs<Profile>());
[email protected]13720532013-05-30 12:30:59523 ASSERT_TRUE(profile->GetOffTheRecordProfile());
524
525 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED);
526 EXPECT_FALSE(
527 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
528
529 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when
530 // incognito mode is forced.
531 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED);
532 EXPECT_TRUE(
533 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord());
534}
535
[email protected]c849fab2012-03-29 16:51:41536#if !defined(OS_ANDROID)
537// There's no Browser object on Android.
[email protected]52edf812012-01-12 11:48:46538TEST_F(ProfileManagerTest, LastOpenedProfiles) {
[email protected]650b2d52013-02-10 03:41:45539 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46540 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
541
[email protected]650b2d52013-02-10 03:41:45542 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46543 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
544
545 ProfileManager* profile_manager = g_browser_process->profile_manager();
546
547 // Successfully create the profiles.
548 TestingProfile* profile1 =
549 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
550 ASSERT_TRUE(profile1);
551
552 TestingProfile* profile2 =
553 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
554 ASSERT_TRUE(profile2);
555
556 std::vector<Profile*> last_opened_profiles =
557 profile_manager->GetLastOpenedProfiles();
558 ASSERT_EQ(0U, last_opened_profiles.size());
559
560 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34561 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29562 scoped_ptr<Browser> browser1a(
[email protected]c5f3e1842013-02-20 17:10:41563 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46564
565 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
566 ASSERT_EQ(1U, last_opened_profiles.size());
567 EXPECT_EQ(profile1, last_opened_profiles[0]);
568
569 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34570 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29571 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41572 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46573
574 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
575 ASSERT_EQ(2U, last_opened_profiles.size());
576 EXPECT_EQ(profile1, last_opened_profiles[0]);
577 EXPECT_EQ(profile2, last_opened_profiles[1]);
578
579 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29580 scoped_ptr<Browser> browser1b(
[email protected]c5f3e1842013-02-20 17:10:41581 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46582 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
583 ASSERT_EQ(2U, last_opened_profiles.size());
584 EXPECT_EQ(profile1, last_opened_profiles[0]);
585 EXPECT_EQ(profile2, last_opened_profiles[1]);
586
587 // Close the browsers.
588 browser1a.reset();
589 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
590 ASSERT_EQ(2U, last_opened_profiles.size());
591 EXPECT_EQ(profile1, last_opened_profiles[0]);
592 EXPECT_EQ(profile2, last_opened_profiles[1]);
593
594 browser1b.reset();
595 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
596 ASSERT_EQ(1U, last_opened_profiles.size());
597 EXPECT_EQ(profile2, last_opened_profiles[0]);
598
599 browser2.reset();
600 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
601 ASSERT_EQ(0U, last_opened_profiles.size());
602}
603
604TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) {
[email protected]650b2d52013-02-10 03:41:45605 base::FilePath dest_path1 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46606 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
607
[email protected]650b2d52013-02-10 03:41:45608 base::FilePath dest_path2 = temp_dir_.path();
[email protected]52edf812012-01-12 11:48:46609 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
610
611 ProfileManager* profile_manager = g_browser_process->profile_manager();
612
613 // Successfully create the profiles.
614 TestingProfile* profile1 =
615 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
616 ASSERT_TRUE(profile1);
617
618 TestingProfile* profile2 =
619 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
620 ASSERT_TRUE(profile2);
621
622 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34623 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29624 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41625 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]52edf812012-01-12 11:48:46626
627 // And for profile2.
[email protected]ed2fa722013-06-25 20:37:34628 Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29629 scoped_ptr<Browser> browser2(
[email protected]c5f3e1842013-02-20 17:10:41630 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]52edf812012-01-12 11:48:46631
632 std::vector<Profile*> last_opened_profiles =
633 profile_manager->GetLastOpenedProfiles();
634 ASSERT_EQ(2U, last_opened_profiles.size());
635 EXPECT_EQ(profile1, last_opened_profiles[0]);
636 EXPECT_EQ(profile2, last_opened_profiles[1]);
637
638 // Simulate a shutdown.
639 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30640 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]52edf812012-01-12 11:48:46641 content::NotificationService::AllSources(),
642 content::NotificationService::NoDetails());
643
644 // Even if the browsers are destructed during shutdown, the profiles stay
645 // open.
646 browser1.reset();
647 browser2.reset();
648
649 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
650 ASSERT_EQ(2U, last_opened_profiles.size());
651 EXPECT_EQ(profile1, last_opened_profiles[0]);
652 EXPECT_EQ(profile2, last_opened_profiles[1]);
653}
[email protected]41276342012-02-06 10:49:31654
655TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
[email protected]650b2d52013-02-10 03:41:45656 base::FilePath dest_path1 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31657 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]650b2d52013-02-10 03:41:45658 base::FilePath dest_path2 = temp_dir_.path();
[email protected]41276342012-02-06 10:49:31659 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
660
661 ProfileManager* profile_manager = g_browser_process->profile_manager();
662
663 // Successfully create the profiles.
[email protected]4dffabe2012-05-19 14:37:06664 TestingProfile* profile1 =
665 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
[email protected]41276342012-02-06 10:49:31666 ASSERT_TRUE(profile1);
667
[email protected]5ddfade2014-02-03 10:24:53668 // Incognito profiles should not be managed by the profile manager but by the
[email protected]4dffabe2012-05-19 14:37:06669 // original profile.
[email protected]9819fd02013-08-22 10:49:39670 TestingProfile::Builder builder;
671 builder.SetIncognito();
672 scoped_ptr<TestingProfile> profile2 = builder.Build();
673 profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
[email protected]41276342012-02-06 10:49:31674
675 std::vector<Profile*> last_opened_profiles =
676 profile_manager->GetLastOpenedProfiles();
677 ASSERT_EQ(0U, last_opened_profiles.size());
678
679 // Create a browser for profile1.
[email protected]ed2fa722013-06-25 20:37:34680 Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29681 scoped_ptr<Browser> browser1(
[email protected]c5f3e1842013-02-20 17:10:41682 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
[email protected]41276342012-02-06 10:49:31683
684 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
685 ASSERT_EQ(1U, last_opened_profiles.size());
686 EXPECT_EQ(profile1, last_opened_profiles[0]);
687
688 // And for profile2.
[email protected]9819fd02013-08-22 10:49:39689 Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(),
690 chrome::GetActiveDesktop());
[email protected]a4fe67012012-07-25 20:14:29691 scoped_ptr<Browser> browser2a(
[email protected]c5f3e1842013-02-20 17:10:41692 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31693
694 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
695 ASSERT_EQ(1U, last_opened_profiles.size());
696 EXPECT_EQ(profile1, last_opened_profiles[0]);
697
698 // Adding more browsers doesn't change anything.
[email protected]a4fe67012012-07-25 20:14:29699 scoped_ptr<Browser> browser2b(
[email protected]c5f3e1842013-02-20 17:10:41700 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
[email protected]41276342012-02-06 10:49:31701 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
702 ASSERT_EQ(1U, last_opened_profiles.size());
703 EXPECT_EQ(profile1, last_opened_profiles[0]);
704
705 // Close the browsers.
706 browser2a.reset();
707 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
708 ASSERT_EQ(1U, last_opened_profiles.size());
709 EXPECT_EQ(profile1, last_opened_profiles[0]);
710
711 browser2b.reset();
712 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
713 ASSERT_EQ(1U, last_opened_profiles.size());
714 EXPECT_EQ(profile1, last_opened_profiles[0]);
715
716 browser1.reset();
717 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
718 ASSERT_EQ(0U, last_opened_profiles.size());
719}
[email protected]c849fab2012-03-29 16:51:41720#endif // !defined(OS_ANDROID)
[email protected]1c62b2f2013-06-28 00:15:00721
722#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
723// There's no Browser object on Android and there's no multi-profiles on Chrome.
[email protected]8d9243a2013-10-21 17:11:48724TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) {
725 base::FilePath dest_path = temp_dir_.path();
726 dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile"));
727
728 ProfileManager* profile_manager = g_browser_process->profile_manager();
729
730 TestingProfile* profile =
731 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path));
732 ASSERT_TRUE(profile);
733 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
734
735 // Here the last used profile is still the "Default" profile.
736 Profile* last_used_profile = profile_manager->GetLastUsedProfile();
737 EXPECT_NE(profile, last_used_profile);
738
[email protected]79fd6b72013-10-22 16:39:27739 // Create a browser for the profile.
[email protected]8d9243a2013-10-21 17:11:48740 Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop());
741 scoped_ptr<Browser> browser(
742 chrome::CreateBrowserWithTestWindowForParams(&profile_params));
743 last_used_profile = profile_manager->GetLastUsedProfile();
744 EXPECT_NE(profile, last_used_profile);
745
746 // Close the browser.
747 browser.reset();
748 last_used_profile = profile_manager->GetLastUsedProfile();
749 EXPECT_NE(profile, last_used_profile);
750}
751
752TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) {
753 base::FilePath dest_path1 = temp_dir_.path();
754 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile"));
755
756 base::FilePath dest_path2 = temp_dir_.path();
[email protected]79fd6b72013-10-22 16:39:27757 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1"));
758
759 base::FilePath dest_path3 = temp_dir_.path();
760 dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2"));
[email protected]8d9243a2013-10-21 17:11:48761
762 ProfileManager* profile_manager = g_browser_process->profile_manager();
763
764 // Successfully create the profiles.
765 TestingProfile* normal_profile =
766 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
767 ASSERT_TRUE(normal_profile);
768
769 // Add one ephemeral profile which should not end up in this list.
[email protected]79fd6b72013-10-22 16:39:27770 TestingProfile* ephemeral_profile1 =
[email protected]8d9243a2013-10-21 17:11:48771 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2));
[email protected]79fd6b72013-10-22 16:39:27772 ASSERT_TRUE(ephemeral_profile1);
773 ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
774 true);
775
776 // Add second ephemeral profile but don't mark it as such yet.
777 TestingProfile* ephemeral_profile2 =
778 static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3));
779 ASSERT_TRUE(ephemeral_profile2);
[email protected]8d9243a2013-10-21 17:11:48780
781 // Create a browser for profile1.
782 Browser::CreateParams profile1_params(normal_profile,
783 chrome::GetActiveDesktop());
784 scoped_ptr<Browser> browser1(
785 chrome::CreateBrowserWithTestWindowForParams(&profile1_params));
786
[email protected]79fd6b72013-10-22 16:39:27787 // Create browsers for the ephemeral profile.
788 Browser::CreateParams profile2_params(ephemeral_profile1,
[email protected]8d9243a2013-10-21 17:11:48789 chrome::GetActiveDesktop());
790 scoped_ptr<Browser> browser2(
791 chrome::CreateBrowserWithTestWindowForParams(&profile2_params));
792
[email protected]79fd6b72013-10-22 16:39:27793 Browser::CreateParams profile3_params(ephemeral_profile2,
794 chrome::GetActiveDesktop());
795 scoped_ptr<Browser> browser3(
796 chrome::CreateBrowserWithTestWindowForParams(&profile3_params));
797
[email protected]8d9243a2013-10-21 17:11:48798 std::vector<Profile*> last_opened_profiles =
799 profile_manager->GetLastOpenedProfiles();
[email protected]79fd6b72013-10-22 16:39:27800 ASSERT_EQ(2U, last_opened_profiles.size());
[email protected]8d9243a2013-10-21 17:11:48801 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
[email protected]79fd6b72013-10-22 16:39:27802 EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]);
803
804 // Mark the second profile ephemeral.
805 ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles,
806 true);
[email protected]8d9243a2013-10-21 17:11:48807
808 // Simulate a shutdown.
809 content::NotificationService::current()->Notify(
810 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
811 content::NotificationService::AllSources(),
812 content::NotificationService::NoDetails());
813 browser1.reset();
814 browser2.reset();
[email protected]79fd6b72013-10-22 16:39:27815 browser3.reset();
[email protected]8d9243a2013-10-21 17:11:48816
817 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
818 ASSERT_EQ(1U, last_opened_profiles.size());
819 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
820}
821
[email protected]1c62b2f2013-06-28 00:15:00822TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
823 ProfileManager* profile_manager = g_browser_process->profile_manager();
824 ASSERT_TRUE(profile_manager);
825
826 // Create and load two profiles.
827 const std::string profile_name1 = "New Profile 1";
828 const std::string profile_name2 = "New Profile 2";
829 base::FilePath dest_path1 =
830 temp_dir_.path().AppendASCII(profile_name1);
831 base::FilePath dest_path2 =
832 temp_dir_.path().AppendASCII(profile_name2);
833
834 MockObserver mock_observer;
835 EXPECT_CALL(mock_observer, OnProfileCreated(
836 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
837
[email protected]5ddfade2014-02-03 10:24:53838 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
839 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35840 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00841
842 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
843 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
844
845 // Set the active profile.
846 PrefService* local_state = g_browser_process->local_state();
847 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
848
849 // Delete the active profile.
850 profile_manager->ScheduleProfileForDeletion(dest_path1,
851 ProfileManager::CreateCallback());
852 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35853 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00854
855 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
856 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
857}
858#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
859
860#if defined(OS_MACOSX)
861// These tests are for a Mac-only code path that assumes the browser
862// process isn't killed when all browser windows are closed.
863TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
864 ProfileManager* profile_manager = g_browser_process->profile_manager();
865 ASSERT_TRUE(profile_manager);
866
867 // Create and load one profile, and just create a second profile.
868 const std::string profile_name1 = "New Profile 1";
869 const std::string profile_name2 = "New Profile 2";
870 base::FilePath dest_path1 =
871 temp_dir_.path().AppendASCII(profile_name1);
872 base::FilePath dest_path2 =
873 temp_dir_.path().AppendASCII(profile_name2);
874
875 MockObserver mock_observer;
876 EXPECT_CALL(mock_observer, OnProfileCreated(
877 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:53878 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35879 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00880
881 // Track the profile, but don't load it.
882 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
883 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a04db822013-12-11 19:14:40884 base::string16(), 0, std::string());
[email protected]37ac95b2013-07-23 23:39:35885 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00886
887 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
888 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
889
890 // Set the active profile.
891 PrefService* local_state = g_browser_process->local_state();
892 local_state->SetString(prefs::kProfileLastUsed,
893 dest_path1.BaseName().MaybeAsASCII());
894
895 // Delete the active profile. This should switch and load the unloaded
896 // profile.
897 profile_manager->ScheduleProfileForDeletion(dest_path1,
898 ProfileManager::CreateCallback());
899
900 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35901 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00902
903 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
904 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
905}
906
907// This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
908// by simulating a scenario in which the profile that is being loaded as
909// the next active profile has also been marked for deletion, so the
910// ProfileManager needs to recursively select a different next profile.
911TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
912 ProfileManager* profile_manager = g_browser_process->profile_manager();
913 ASSERT_TRUE(profile_manager);
914
915 // Create and load one profile, and create two more profiles.
916 const std::string profile_name1 = "New Profile 1";
917 const std::string profile_name2 = "New Profile 2";
918 const std::string profile_name3 = "New Profile 3";
919 base::FilePath dest_path1 =
920 temp_dir_.path().AppendASCII(profile_name1);
921 base::FilePath dest_path2 =
922 temp_dir_.path().AppendASCII(profile_name2);
923 base::FilePath dest_path3 =
924 temp_dir_.path().AppendASCII(profile_name3);
925
926 MockObserver mock_observer;
927 EXPECT_CALL(mock_observer, OnProfileCreated(
928 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
[email protected]5ddfade2014-02-03 10:24:53929 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
[email protected]37ac95b2013-07-23 23:39:35930 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00931
932 // Create the other profiles, but don't load them. Assign a fake avatar icon
933 // to ensure that profiles in the info cache are sorted by the profile name,
934 // and not randomly by the avatar name.
935 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
936 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
[email protected]a6e01b42013-08-05 14:13:13937 ASCIIToUTF16(profile_name2), 1, std::string());
[email protected]1c62b2f2013-06-28 00:15:00938 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
[email protected]a6e01b42013-08-05 14:13:13939 ASCIIToUTF16(profile_name3), 2, std::string());
[email protected]1c62b2f2013-06-28 00:15:00940
[email protected]37ac95b2013-07-23 23:39:35941 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00942
943 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
944 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
945
946 // Set the active profile.
947 PrefService* local_state = g_browser_process->local_state();
948 local_state->SetString(prefs::kProfileLastUsed,
949 dest_path1.BaseName().MaybeAsASCII());
950
951 // Delete the active profile, Profile1.
952 // This will post a CreateProfileAsync message, that tries to load Profile2,
953 // which checks that the profile is not being deleted, and then calls back
954 // FinishDeletingProfile for Profile1.
955 // Try to break this flow by setting the active profile to Profile2 in the
956 // middle (so after the first posted message), and trying to delete Profile2,
957 // so that the ProfileManager has to look for a different profile to load.
958 profile_manager->ScheduleProfileForDeletion(dest_path1,
959 ProfileManager::CreateCallback());
960 local_state->SetString(prefs::kProfileLastUsed,
961 dest_path2.BaseName().MaybeAsASCII());
962 profile_manager->ScheduleProfileForDeletion(dest_path2,
963 ProfileManager::CreateCallback());
964 // Spin the message loop so that all the callbacks can finish running.
[email protected]37ac95b2013-07-23 23:39:35965 base::RunLoop().RunUntilIdle();
[email protected]1c62b2f2013-06-28 00:15:00966
967 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
968 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
969}
970#endif // !defined(OS_MACOSX)