[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d99bcaa | 2010-12-05 20:15:56 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 15 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 17 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 19 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 20 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 21 | #include "chrome/browser/io_thread.h" |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 22 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 23 | #include "chrome/browser/prefs/incognito_mode_prefs.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 25 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 26 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 27 | #include "chrome/browser/ui/browser.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 28 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | #include "chrome/common/chrome_paths.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 30 | #include "chrome/common/chrome_switches.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 31 | #include "chrome/common/pref_names.h" |
[email protected] | 7688968a | 2013-02-12 21:45:13 | [diff] [blame] | 32 | #include "chrome/test/base/scoped_testing_local_state.h" |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 33 | #include "chrome/test/base/test_browser_window.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 34 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 35 | #include "chrome/test/base/testing_profile.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 36 | #include "content/public/browser/notification_service.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 37 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 38 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "testing/gtest/include/gtest/gtest.h" |
| 40 | |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 41 | #if defined(OS_CHROMEOS) |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 42 | #include "chrome/browser/chromeos/login/mock_user_manager.h" |
[email protected] | 9a68d3a | 2013-04-22 16:26:54 | [diff] [blame] | 43 | #include "chrome/browser/chromeos/login/user_manager.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 44 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 45 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 46 | #include "chromeos/chromeos_switches.h" |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 47 | #endif |
| 48 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 49 | using base::ASCIIToUTF16; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 50 | using content::BrowserThread; |
| 51 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 52 | namespace { |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 53 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 54 | // This global variable is used to check that value returned to different |
| 55 | // observers is the same. |
| 56 | Profile* g_created_profile; |
| 57 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 58 | class UnittestProfileManager : public ::ProfileManagerWithoutInit { |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 59 | public: |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 60 | explicit UnittestProfileManager(const base::FilePath& user_data_dir) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 61 | : ::ProfileManagerWithoutInit(user_data_dir) {} |
| 62 | |
| 63 | protected: |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 64 | virtual Profile* CreateProfileHelper( |
| 65 | const base::FilePath& file_path) OVERRIDE { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 66 | if (!base::PathExists(file_path)) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 67 | if (!base::CreateDirectory(file_path)) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | return new TestingProfile(file_path, NULL); |
| 71 | } |
| 72 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 73 | virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 74 | 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] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 78 | base::Bind(base::IgnoreResult(&base::CreateDirectory), path)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 79 | |
| 80 | return new TestingProfile(path, this); |
| 81 | } |
| 82 | }; |
| 83 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 84 | } // namespace |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 85 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 86 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 87 | protected: |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 88 | class MockObserver { |
| 89 | public: |
| 90 | MOCK_METHOD2(OnProfileCreated, |
| 91 | void(Profile* profile, Profile::CreateStatus status)); |
| 92 | }; |
| 93 | |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 94 | ProfileManagerTest() |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 95 | : local_state_(TestingBrowserProcess::GetGlobal()) { |
[email protected] | cb0e4f1 | 2009-12-03 00:09:09 | [diff] [blame] | 96 | } |
| 97 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | virtual void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 99 | // Create a new temporary directory, and store the path |
| 100 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 101 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 102 | new UnittestProfileManager(temp_dir_.path())); |
[email protected] | f89b50f | 2012-08-16 21:36:50 | [diff] [blame] | 103 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 104 | #if defined(OS_CHROMEOS) |
[email protected] | 19506d54 | 2013-10-15 23:11:06 | [diff] [blame] | 105 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 106 | cl->AppendSwitch(switches::kTestType); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 107 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | } |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 109 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | virtual void TearDown() { |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 111 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 112 | base::RunLoop().RunUntilIdle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 114 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 115 | // Helper function to create a profile with |name| for a profile |manager|. |
| 116 | void CreateProfileAsync(ProfileManager* manager, |
| 117 | const std::string& name, |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 118 | bool is_managed, |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 119 | MockObserver* mock_observer) { |
| 120 | manager->CreateProfileAsync( |
| 121 | temp_dir_.path().AppendASCII(name), |
| 122 | base::Bind(&MockObserver::OnProfileCreated, |
| 123 | base::Unretained(mock_observer)), |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 124 | base::UTF8ToUTF16(name), |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 125 | base::UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(0)), |
| 126 | is_managed ? "Dummy ID" : std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 129 | #if defined(OS_CHROMEOS) |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 130 | chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 131 | chromeos::ScopedTestCrosSettings test_cros_settings_; |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 132 | #endif |
| 133 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 134 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 135 | base::ScopedTempDir temp_dir_; |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 136 | ScopedTestingLocalState local_state_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 137 | |
| 138 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 139 | |
| 140 | #if defined(OS_CHROMEOS) |
| 141 | chromeos::ScopedTestUserManager test_user_manager_; |
| 142 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | }; |
| 144 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 145 | TEST_F(ProfileManagerTest, GetProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 146 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 147 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 149 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 150 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | // Successfully create a profile. |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 152 | Profile* profile = profile_manager->GetProfile(dest_path); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 153 | EXPECT_TRUE(profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 155 | // The profile already exists when we call GetProfile. Just load it. |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 156 | EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | } |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 158 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 159 | TEST_F(ProfileManagerTest, DefaultProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 160 | base::FilePath expected_default = |
| 161 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 162 | EXPECT_EQ( |
| 163 | expected_default.value(), |
| 164 | g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | #if defined(OS_CHROMEOS) |
| 168 | // This functionality only exists on Chrome OS. |
| 169 | TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 170 | CommandLine *cl = CommandLine::ForCurrentProcess(); |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 171 | std::string profile_dir(chrome::kTestUserProfileDir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 172 | |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 173 | cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 174 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 175 | base::FilePath expected_default = |
| 176 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 177 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 178 | EXPECT_EQ(expected_default.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 179 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 180 | |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 181 | 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] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 185 | profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 186 | content::NotificationService::AllSources(), |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 187 | content::Details<const chromeos::User>(active_user)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 188 | base::FilePath expected_logged_in(profile_dir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 189 | EXPECT_EQ(expected_logged_in.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 190 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 191 | VLOG(1) << temp_dir_.path().Append( |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 192 | profile_manager->GetInitialProfileDir()).value(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | #endif |
| 196 | |
[email protected] | 52d69b88 | 2010-06-17 17:35:51 | [diff] [blame] | 197 | TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 198 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 199 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 200 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 201 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 202 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 203 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 204 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 205 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 206 | // Successfully create the profiles. |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 207 | TestingProfile* profile1 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 208 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 209 | ASSERT_TRUE(profile1); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 210 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 211 | TestingProfile* profile2 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 212 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 213 | ASSERT_TRUE(profile2); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 214 | |
| 215 | // Force lazy-init of some profile services to simulate use. |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 216 | ASSERT_TRUE(profile1->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 217 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1, |
| 218 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 219 | profile1->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 220 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 221 | profile2->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 222 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2)); |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 223 | ASSERT_TRUE(profile2->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 224 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2, |
| 225 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 226 | |
| 227 | // Make sure any pending tasks run before we destroy the profiles. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 228 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 229 | |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 230 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 231 | |
[email protected] | 237e6d0 | 2010-11-08 21:45:42 | [diff] [blame] | 232 | // Make sure history cleans up correctly. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 233 | base::RunLoop().RunUntilIdle(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 234 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 235 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 236 | MATCHER(NotFail, "Profile creation failure status is not reported.") { |
[email protected] | b8ba8d4 | 2011-12-07 22:13:59 | [diff] [blame] | 237 | return arg == Profile::CREATE_STATUS_CREATED || |
| 238 | arg == Profile::CREATE_STATUS_INITIALIZED; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 241 | MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 242 | if (!g_created_profile) |
| 243 | g_created_profile = arg; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 244 | return arg != NULL && arg == g_created_profile; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 248 | g_created_profile = NULL; |
| 249 | |
| 250 | MockObserver mock_observer1; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 251 | EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 252 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 253 | MockObserver mock_observer2; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 254 | EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 255 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 256 | MockObserver mock_observer3; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 257 | EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 258 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 259 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 260 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 261 | const std::string profile_name = "New Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 262 | 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] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 265 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 266 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 270 | const std::string profile_name1 = "New Profile 1"; |
| 271 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 272 | |
| 273 | MockObserver mock_observer; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 274 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 275 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 276 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 277 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 278 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 279 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 280 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 281 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 282 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 283 | } |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 284 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 285 | TEST_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 | |
| 312 | TEST_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] | 4251165a | 2013-07-17 04:33:40 | [diff] [blame] | 339 | TEST_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] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 346 | #if defined(OS_CHROMEOS) |
| 347 | class 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 | |
| 367 | class 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 | |
| 388 | TEST_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] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 399 | TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 400 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 401 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 402 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
[email protected] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 403 | base::Value::CreateBooleanValue(true)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 404 | |
| 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] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 411 | |
| 412 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 413 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 414 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 415 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 416 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 417 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 418 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 419 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 420 | ASCIIToUTF16("name_3"), base::string16(), 0, |
| 421 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 422 | cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 423 | cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 424 | EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 425 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 426 | profile_manager->AutoloadProfiles(); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 427 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 428 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 429 | } |
| 430 | |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 431 | TEST_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] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 435 | base::Value::CreateBooleanValue(false)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 436 | |
| 437 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 438 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 439 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 440 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 441 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 442 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 443 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 444 | 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] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 453 | TEST_F(ProfileManagerTest, InitProfileUserPrefs) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 454 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 455 | 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. |
| 479 | TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 480 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 481 | 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] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 499 | base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 500 | EXPECT_EQ(avatar_index, |
| 501 | cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 502 | } |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 503 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 504 | TEST_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] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 517 | TestingProfile::Builder builder; |
| 518 | builder.SetIncognito(); |
| 519 | scoped_ptr<TestingProfile> incognito_profile = builder.Build(); |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 520 | EXPECT_TRUE(incognito_profile->IsOffTheRecord()); |
| 521 | TestingProfile* testing_profile = static_cast<TestingProfile*>(profile); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 522 | testing_profile->SetOffTheRecordProfile(incognito_profile.PassAs<Profile>()); |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 523 | 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] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 536 | #if !defined(OS_ANDROID) |
| 537 | // There's no Browser object on Android. |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 538 | TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 539 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 540 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 541 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 542 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 543 | 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] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 561 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 562 | scoped_ptr<Browser> browser1a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 563 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 564 | |
| 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] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 570 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 571 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 572 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 573 | |
| 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] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 580 | scoped_ptr<Browser> browser1b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 581 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 582 | 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 | |
| 604 | TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 605 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 606 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 607 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 608 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 609 | 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] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 623 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 624 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 625 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 626 | |
| 627 | // And for profile2. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 628 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 629 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 630 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 631 | |
| 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] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 640 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 641 | 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] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 654 | |
| 655 | TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 656 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 657 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 658 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 659 | 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] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 664 | TestingProfile* profile1 = |
| 665 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 666 | ASSERT_TRUE(profile1); |
| 667 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 668 | // Incognito profiles should not be managed by the profile manager but by the |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 669 | // original profile. |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 670 | TestingProfile::Builder builder; |
| 671 | builder.SetIncognito(); |
| 672 | scoped_ptr<TestingProfile> profile2 = builder.Build(); |
| 673 | profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>()); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 674 | |
| 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] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 680 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 681 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 682 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 683 | |
| 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] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 689 | Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(), |
| 690 | chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 691 | scoped_ptr<Browser> browser2a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 692 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 693 | |
| 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] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 699 | scoped_ptr<Browser> browser2b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 700 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 701 | 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] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 720 | #endif // !defined(OS_ANDROID) |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 721 | |
| 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] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 724 | TEST_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] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 739 | // Create a browser for the profile. |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 740 | 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 | |
| 752 | TEST_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] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 757 | 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] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 761 | |
| 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] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 770 | TestingProfile* ephemeral_profile1 = |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 771 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 772 | 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] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 780 | |
| 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] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 787 | // Create browsers for the ephemeral profile. |
| 788 | Browser::CreateParams profile2_params(ephemeral_profile1, |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 789 | chrome::GetActiveDesktop()); |
| 790 | scoped_ptr<Browser> browser2( |
| 791 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
| 792 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 793 | Browser::CreateParams profile3_params(ephemeral_profile2, |
| 794 | chrome::GetActiveDesktop()); |
| 795 | scoped_ptr<Browser> browser3( |
| 796 | chrome::CreateBrowserWithTestWindowForParams(&profile3_params)); |
| 797 | |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 798 | std::vector<Profile*> last_opened_profiles = |
| 799 | profile_manager->GetLastOpenedProfiles(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 800 | ASSERT_EQ(2U, last_opened_profiles.size()); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 801 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 802 | 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] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 807 | |
| 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] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 815 | browser3.reset(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 816 | |
| 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] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 822 | TEST_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] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 838 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 839 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 840 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 841 | |
| 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] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 853 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 854 | |
| 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. |
| 863 | TEST_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] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 878 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 879 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 880 | |
| 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] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 884 | base::string16(), 0, std::string()); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 885 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 886 | |
| 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] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 901 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 902 | |
| 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. |
| 911 | TEST_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] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame^] | 929 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 930 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 931 | |
| 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] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 937 | ASCIIToUTF16(profile_name2), 1, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 938 | cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 939 | ASCIIToUTF16(profile_name3), 2, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 940 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 941 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 942 | |
| 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] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 965 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 966 | |
| 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) |