[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" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/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] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 25 | #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 26 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 27 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profiles_state.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 29 | #include "chrome/browser/ui/browser.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 30 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | #include "chrome/common/chrome_paths.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 32 | #include "chrome/common/chrome_switches.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 34 | #include "chrome/grit/generated_resources.h" |
[email protected] | 7688968a | 2013-02-12 21:45:13 | [diff] [blame] | 35 | #include "chrome/test/base/scoped_testing_local_state.h" |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 36 | #include "chrome/test/base/test_browser_window.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 37 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 38 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 29896ee | 2014-06-17 17:20:53 | [diff] [blame] | 39 | #include "components/signin/core/common/profile_management_switches.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 40 | #include "content/public/browser/notification_service.h" |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 41 | #include "content/public/common/content_switches.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 42 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 43 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 45 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 47 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 48 | #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 49 | #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 50 | #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 51 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 52 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 53 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 54 | #include "chromeos/chromeos_switches.h" |
[email protected] | 4c40e06 | 2014-07-09 21:22:51 | [diff] [blame] | 55 | #include "chromeos/login/user_names.h" |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 56 | #include "components/user_manager/user_manager.h" |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 57 | #endif // defined(OS_CHROMEOS) |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 58 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 59 | using base::ASCIIToUTF16; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 60 | using content::BrowserThread; |
| 61 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 62 | namespace { |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 63 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 64 | // This global variable is used to check that value returned to different |
| 65 | // observers is the same. |
| 66 | Profile* g_created_profile; |
| 67 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 68 | class UnittestProfileManager : public ::ProfileManagerWithoutInit { |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 69 | public: |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 70 | explicit UnittestProfileManager(const base::FilePath& user_data_dir) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 71 | : ::ProfileManagerWithoutInit(user_data_dir) {} |
| 72 | |
| 73 | protected: |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 74 | virtual Profile* CreateProfileHelper( |
| 75 | const base::FilePath& file_path) OVERRIDE { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 76 | if (!base::PathExists(file_path)) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 77 | if (!base::CreateDirectory(file_path)) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | return new TestingProfile(file_path, NULL); |
| 81 | } |
| 82 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 83 | virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 84 | Delegate* delegate) OVERRIDE { |
| 85 | // This is safe while all file operations are done on the FILE thread. |
| 86 | BrowserThread::PostTask( |
| 87 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 88 | base::Bind(base::IgnoreResult(&base::CreateDirectory), path)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 89 | |
| 90 | return new TestingProfile(path, this); |
| 91 | } |
| 92 | }; |
| 93 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 94 | } // namespace |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 95 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 96 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 97 | protected: |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 98 | class MockObserver { |
| 99 | public: |
| 100 | MOCK_METHOD2(OnProfileCreated, |
| 101 | void(Profile* profile, Profile::CreateStatus status)); |
| 102 | }; |
| 103 | |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 104 | ProfileManagerTest() |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 105 | : local_state_(TestingBrowserProcess::GetGlobal()) { |
[email protected] | cb0e4f1 | 2009-12-03 00:09:09 | [diff] [blame] | 106 | } |
| 107 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | virtual void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 109 | // Create a new temporary directory, and store the path |
| 110 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 111 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 112 | new UnittestProfileManager(temp_dir_.path())); |
[email protected] | f89b50f | 2012-08-16 21:36:50 | [diff] [blame] | 113 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 114 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 115 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 116 | cl->AppendSwitch(switches::kTestType); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 117 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | } |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 119 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | virtual void TearDown() { |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 121 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 122 | base::RunLoop().RunUntilIdle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 124 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 125 | // Helper function to create a profile with |name| for a profile |manager|. |
| 126 | void CreateProfileAsync(ProfileManager* manager, |
| 127 | const std::string& name, |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 128 | bool is_supervised, |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 129 | MockObserver* mock_observer) { |
| 130 | manager->CreateProfileAsync( |
| 131 | temp_dir_.path().AppendASCII(name), |
| 132 | base::Bind(&MockObserver::OnProfileCreated, |
| 133 | base::Unretained(mock_observer)), |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 134 | base::UTF8ToUTF16(name), |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 135 | base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(0)), |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 136 | is_supervised ? "Dummy ID" : std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 139 | // Helper function to add a profile with |profile_name| to |
| 140 | // |profile_manager|'s ProfileInfoCache, and return the profile created. |
| 141 | Profile* AddProfileToCache(ProfileManager* profile_manager, |
| 142 | const std::string& path_suffix, |
| 143 | const base::string16& profile_name) { |
| 144 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 145 | size_t num_profiles = cache.GetNumberOfProfiles(); |
| 146 | base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); |
| 147 | cache.AddProfileToCache(path, profile_name, |
| 148 | base::string16(), 0, std::string()); |
| 149 | EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles()); |
| 150 | return profile_manager->GetProfile(path); |
| 151 | } |
| 152 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 153 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 154 | // Helper function to register an user with id |user_id| and create profile |
| 155 | // with a correct path. |
| 156 | void RegisterUser(const std::string& user_id) { |
| 157 | chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get(); |
| 158 | const std::string user_id_hash = |
| 159 | profile_helper->GetUserIdHashByUserIdForTesting(user_id); |
| 160 | user_manager::UserManager::Get()->UserLoggedIn( |
| 161 | user_id, user_id_hash, false); |
| 162 | g_browser_process->profile_manager()->GetProfile( |
| 163 | profile_helper->GetProfilePathByUserIdHash(user_id_hash)); |
| 164 | } |
| 165 | |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 166 | chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 167 | chromeos::ScopedTestCrosSettings test_cros_settings_; |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 168 | #endif |
| 169 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 170 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 171 | base::ScopedTempDir temp_dir_; |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 172 | ScopedTestingLocalState local_state_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 173 | |
| 174 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 175 | |
| 176 | #if defined(OS_CHROMEOS) |
| 177 | chromeos::ScopedTestUserManager test_user_manager_; |
| 178 | #endif |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 179 | |
| 180 | DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | }; |
| 182 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 183 | TEST_F(ProfileManagerTest, GetProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 184 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 185 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 187 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 188 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | // Successfully create a profile. |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 190 | Profile* profile = profile_manager->GetProfile(dest_path); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 191 | EXPECT_TRUE(profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 193 | // The profile already exists when we call GetProfile. Just load it. |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 194 | EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | } |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 196 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 197 | TEST_F(ProfileManagerTest, DefaultProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 198 | base::FilePath expected_default = |
| 199 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 200 | EXPECT_EQ( |
| 201 | expected_default.value(), |
| 202 | g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 206 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 207 | // This functionality only exists on Chrome OS. |
| 208 | TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 209 | base::FilePath expected_default = |
| 210 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 211 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 212 | EXPECT_EQ(expected_default.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 213 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 214 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 215 | const char kTestUserName[] = "[email protected]"; |
| 216 | chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager(); |
| 217 | chromeos::ScopedUserManagerEnabler enabler(user_manager); |
| 218 | |
| 219 | const user_manager::User* active_user = user_manager->AddUser(kTestUserName); |
| 220 | user_manager->LoginUser(kTestUserName); |
| 221 | user_manager->SwitchActiveUser(kTestUserName); |
| 222 | |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 223 | profile_manager->Observe( |
| 224 | chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 225 | content::NotificationService::AllSources(), |
| 226 | content::Details<const user_manager::User>(active_user)); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 227 | base::FilePath expected_logged_in( |
| 228 | chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash())); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 229 | EXPECT_EQ(expected_logged_in.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 230 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 231 | VLOG(1) << temp_dir_.path().Append( |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 232 | profile_manager->GetInitialProfileDir()).value(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | #endif |
| 236 | |
[email protected] | 52d69b88 | 2010-06-17 17:35:51 | [diff] [blame] | 237 | TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 238 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 239 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 240 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 241 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 242 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 243 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 244 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 245 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 246 | // Successfully create the profiles. |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 247 | TestingProfile* profile1 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 248 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 249 | ASSERT_TRUE(profile1); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 250 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 251 | TestingProfile* profile2 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 252 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 253 | ASSERT_TRUE(profile2); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 254 | |
| 255 | // Force lazy-init of some profile services to simulate use. |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 256 | ASSERT_TRUE(profile1->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 257 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1, |
| 258 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 259 | profile1->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 260 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 261 | profile2->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 262 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2)); |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 263 | ASSERT_TRUE(profile2->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 264 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2, |
| 265 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 266 | |
| 267 | // Make sure any pending tasks run before we destroy the profiles. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 268 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 269 | |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 270 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 271 | |
[email protected] | 237e6d0 | 2010-11-08 21:45:42 | [diff] [blame] | 272 | // Make sure history cleans up correctly. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 273 | base::RunLoop().RunUntilIdle(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 274 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 275 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 276 | MATCHER(NotFail, "Profile creation failure status is not reported.") { |
[email protected] | b8ba8d4 | 2011-12-07 22:13:59 | [diff] [blame] | 277 | return arg == Profile::CREATE_STATUS_CREATED || |
| 278 | arg == Profile::CREATE_STATUS_INITIALIZED; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 281 | MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 282 | if (!g_created_profile) |
| 283 | g_created_profile = arg; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 284 | return arg != NULL && arg == g_created_profile; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 288 | g_created_profile = NULL; |
| 289 | |
| 290 | MockObserver mock_observer1; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 291 | EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 292 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 293 | MockObserver mock_observer2; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 294 | EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 295 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 296 | MockObserver mock_observer3; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 297 | EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 298 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 299 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 300 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 301 | const std::string profile_name = "New Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 302 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1); |
| 303 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2); |
| 304 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 305 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 306 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 310 | const std::string profile_name1 = "New Profile 1"; |
| 311 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 312 | |
| 313 | MockObserver mock_observer; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 314 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 315 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 316 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 317 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 318 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 319 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 320 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 321 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 322 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 323 | } |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 324 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 325 | TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) { |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 326 | std::string name = "0 Supervised Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 327 | |
| 328 | MockObserver mock_observer; |
| 329 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 330 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 331 | |
| 332 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 333 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 334 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 335 | |
| 336 | CreateProfileAsync(profile_manager, name, true, &mock_observer); |
| 337 | base::RunLoop().RunUntilIdle(); |
| 338 | |
| 339 | EXPECT_EQ(1u, cache.GetNumberOfProfiles()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 340 | // Supervised profiles should start out omitted from the profile list. |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 341 | EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0)); |
| 342 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 343 | name = "1 Regular Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 344 | CreateProfileAsync(profile_manager, name, false, &mock_observer); |
| 345 | base::RunLoop().RunUntilIdle(); |
| 346 | |
| 347 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 348 | // Non-supervised profiles should be included in the profile list. |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 349 | EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1)); |
| 350 | } |
| 351 | |
| 352 | TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) { |
| 353 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 354 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 355 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 356 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 357 | const base::FilePath supervised_path = |
| 358 | temp_dir_.path().AppendASCII("Supervised"); |
| 359 | TestingProfile* supervised_profile = |
| 360 | new TestingProfile(supervised_path, NULL); |
| 361 | supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID"); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 362 | |
| 363 | // RegisterTestingProfile adds the profile to the cache and takes ownership. |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 364 | profile_manager->RegisterTestingProfile(supervised_profile, true, false); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 365 | EXPECT_EQ(1u, cache.GetNumberOfProfiles()); |
| 366 | EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0)); |
| 367 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 368 | const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII( |
| 369 | "Non-Supervised"); |
| 370 | TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path, |
| 371 | NULL); |
| 372 | profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 373 | |
| 374 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 375 | size_t supervised_index = cache.GetIndexOfProfileWithPath(supervised_path); |
| 376 | EXPECT_TRUE(cache.IsOmittedProfileAtIndex(supervised_index)); |
| 377 | size_t nonsupervised_index = |
| 378 | cache.GetIndexOfProfileWithPath(nonsupervised_path); |
| 379 | EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonsupervised_index)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | 4251165a | 2013-07-17 04:33:40 | [diff] [blame] | 382 | TEST_F(ProfileManagerTest, GetGuestProfilePath) { |
| 383 | base::FilePath guest_path = ProfileManager::GetGuestProfilePath(); |
| 384 | base::FilePath expected_path = temp_dir_.path(); |
| 385 | expected_path = expected_path.Append(chrome::kGuestProfileDir); |
| 386 | EXPECT_EQ(expected_path, guest_path); |
| 387 | } |
| 388 | |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 389 | class UnittestGuestProfileManager : public UnittestProfileManager { |
| 390 | public: |
| 391 | explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir) |
| 392 | : UnittestProfileManager(user_data_dir) {} |
| 393 | |
| 394 | protected: |
| 395 | virtual Profile* CreateProfileHelper( |
| 396 | const base::FilePath& file_path) OVERRIDE { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 397 | TestingProfile::Builder builder; |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 398 | builder.SetGuestSession(); |
[email protected] | d22f7c93 | 2014-02-20 22:48:58 | [diff] [blame] | 399 | builder.SetPath(file_path); |
| 400 | TestingProfile* testing_profile = builder.Build().release(); |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 401 | return testing_profile; |
| 402 | } |
| 403 | }; |
| 404 | |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 405 | class ProfileManagerGuestTest : public ProfileManagerTest { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 406 | protected: |
| 407 | virtual void SetUp() { |
| 408 | // Create a new temporary directory, and store the path |
| 409 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 410 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 411 | new UnittestGuestProfileManager(temp_dir_.path())); |
| 412 | |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 413 | #if defined(OS_CHROMEOS) |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 414 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 415 | // This switch is needed to skip non-test specific behavior in |
| 416 | // ProfileManager (accessing DBusThreadManager). |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 417 | cl->AppendSwitch(switches::kTestType); |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 418 | |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 419 | cl->AppendSwitch(chromeos::switches::kGuestSession); |
| 420 | cl->AppendSwitch(::switches::kIncognito); |
| 421 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 422 | RegisterUser(chromeos::login::kGuestUserName); |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 423 | #endif |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 424 | } |
| 425 | }; |
| 426 | |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 427 | TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 428 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 429 | ASSERT_TRUE(profile_manager); |
| 430 | |
| 431 | Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 432 | ASSERT_TRUE(profile); |
| 433 | EXPECT_TRUE(profile->IsOffTheRecord()); |
| 434 | } |
| 435 | |
| 436 | #if defined(OS_CHROMEOS) |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 437 | TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 438 | Profile* primary_profile = ProfileManager::GetPrimaryUserProfile(); |
| 439 | EXPECT_TRUE(primary_profile->IsOffTheRecord()); |
| 440 | |
| 441 | Profile* active_profile = ProfileManager::GetActiveUserProfile(); |
| 442 | EXPECT_TRUE(active_profile->IsOffTheRecord()); |
| 443 | |
| 444 | EXPECT_TRUE(active_profile->IsSameProfile(primary_profile)); |
[email protected] | d22f7c93 | 2014-02-20 22:48:58 | [diff] [blame] | 445 | |
| 446 | Profile* last_used_profile = ProfileManager::GetLastUsedProfile(); |
| 447 | EXPECT_TRUE(last_used_profile->IsOffTheRecord()); |
| 448 | |
| 449 | EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile)); |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 450 | } |
| 451 | #endif |
| 452 | |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 453 | TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 454 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 455 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 456 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
[email protected] | 012d131 | 2014-07-17 06:37:40 | [diff] [blame] | 457 | new base::FundamentalValue(true)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 458 | |
| 459 | // Setting a pref which is not applicable to a system (i.e., Android in this |
| 460 | // case) does not necessarily create it. Don't bother continuing with the |
| 461 | // test if this pref doesn't exist because it will not load the profiles if |
| 462 | // it cannot verify that the pref for background mode is enabled. |
| 463 | if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled)) |
| 464 | return; |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 465 | |
| 466 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 467 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 468 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 469 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 470 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 471 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 472 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 473 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 474 | ASCIIToUTF16("name_3"), base::string16(), 0, |
| 475 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 476 | cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 477 | cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 478 | EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 479 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 480 | profile_manager->AutoloadProfiles(); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 481 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 482 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 483 | } |
| 484 | |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 485 | TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) { |
| 486 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 487 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 488 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
[email protected] | 012d131 | 2014-07-17 06:37:40 | [diff] [blame] | 489 | new base::FundamentalValue(false)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 490 | |
| 491 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 492 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 493 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 494 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 495 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 496 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 497 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 498 | cache.SetBackgroundStatusOfProfileAtIndex(0, false); |
| 499 | cache.SetBackgroundStatusOfProfileAtIndex(1, true); |
| 500 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
| 501 | |
| 502 | profile_manager->AutoloadProfiles(); |
| 503 | |
| 504 | EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size()); |
| 505 | } |
| 506 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 507 | TEST_F(ProfileManagerTest, InitProfileUserPrefs) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 508 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 509 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 510 | |
| 511 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 512 | |
| 513 | Profile* profile; |
| 514 | |
| 515 | // Successfully create the profile |
| 516 | profile = profile_manager->GetProfile(dest_path); |
| 517 | ASSERT_TRUE(profile); |
| 518 | |
| 519 | // Check that the profile name is non empty |
| 520 | std::string profile_name = |
| 521 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 522 | EXPECT_FALSE(profile_name.empty()); |
| 523 | |
| 524 | // Check that the profile avatar index is valid |
| 525 | size_t avatar_index = |
| 526 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 527 | EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex( |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 528 | avatar_index)); |
| 529 | } |
| 530 | |
| 531 | // Tests that a new profile's entry in the profile info cache is setup with the |
| 532 | // same values that are in the profile prefs. |
| 533 | TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 534 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 535 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 536 | |
| 537 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 538 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 539 | |
| 540 | // Successfully create the profile |
| 541 | Profile* profile = profile_manager->GetProfile(dest_path); |
| 542 | ASSERT_TRUE(profile); |
| 543 | |
| 544 | std::string profile_name = |
| 545 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 546 | size_t avatar_index = |
| 547 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
| 548 | |
| 549 | size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path); |
| 550 | |
| 551 | // Check if the profile prefs are the same as the cache prefs |
| 552 | EXPECT_EQ(profile_name, |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 553 | base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 554 | EXPECT_EQ(avatar_index, |
| 555 | cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 556 | } |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 557 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 558 | TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { |
| 559 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 560 | ASSERT_TRUE(profile_manager); |
| 561 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame^] | 562 | #if defined(OS_CHROMEOS) |
| 563 | // On CrOS, profile returned by GetLastUsedProfile is a singin profile that |
| 564 | // is forced to be incognito. That's why we need to create at least one user |
| 565 | // to get a regular profile. |
| 566 | RegisterUser("[email protected]"); |
| 567 | #endif |
| 568 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 569 | Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 570 | ASSERT_TRUE(profile); |
| 571 | EXPECT_FALSE(profile->IsOffTheRecord()); |
| 572 | PrefService* prefs = profile->GetPrefs(); |
| 573 | EXPECT_EQ(IncognitoModePrefs::ENABLED, |
| 574 | IncognitoModePrefs::GetAvailability(prefs)); |
| 575 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 576 | ASSERT_TRUE(profile->GetOffTheRecordProfile()); |
| 577 | |
| 578 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); |
| 579 | EXPECT_FALSE( |
| 580 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 581 | |
| 582 | // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when |
| 583 | // incognito mode is forced. |
| 584 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); |
| 585 | EXPECT_TRUE( |
| 586 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 587 | } |
| 588 | |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 589 | #if !defined(OS_ANDROID) |
| 590 | // There's no Browser object on Android. |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 591 | TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 592 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 593 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 594 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 595 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 596 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 597 | |
| 598 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 599 | |
| 600 | // Successfully create the profiles. |
| 601 | TestingProfile* profile1 = |
| 602 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 603 | ASSERT_TRUE(profile1); |
| 604 | |
| 605 | TestingProfile* profile2 = |
| 606 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 607 | ASSERT_TRUE(profile2); |
| 608 | |
| 609 | std::vector<Profile*> last_opened_profiles = |
| 610 | profile_manager->GetLastOpenedProfiles(); |
| 611 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 612 | |
| 613 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 614 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 615 | scoped_ptr<Browser> browser1a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 616 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 617 | |
| 618 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 619 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 620 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 621 | |
| 622 | // And for profile2. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 623 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 624 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 625 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 626 | |
| 627 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 628 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 629 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 630 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 631 | |
| 632 | // Adding more browsers doesn't change anything. |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 633 | scoped_ptr<Browser> browser1b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 634 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 635 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 636 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 637 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 638 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 639 | |
| 640 | // Close the browsers. |
| 641 | browser1a.reset(); |
| 642 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 643 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 644 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 645 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 646 | |
| 647 | browser1b.reset(); |
| 648 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 649 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 650 | EXPECT_EQ(profile2, last_opened_profiles[0]); |
| 651 | |
| 652 | browser2.reset(); |
| 653 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 654 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 655 | } |
| 656 | |
| 657 | TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 658 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 659 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 660 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 661 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 662 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 663 | |
| 664 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 665 | |
| 666 | // Successfully create the profiles. |
| 667 | TestingProfile* profile1 = |
| 668 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 669 | ASSERT_TRUE(profile1); |
| 670 | |
| 671 | TestingProfile* profile2 = |
| 672 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 673 | ASSERT_TRUE(profile2); |
| 674 | |
| 675 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 676 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 677 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 678 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 679 | |
| 680 | // And for profile2. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 681 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 682 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 683 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 684 | |
| 685 | std::vector<Profile*> last_opened_profiles = |
| 686 | profile_manager->GetLastOpenedProfiles(); |
| 687 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 688 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 689 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 690 | |
| 691 | // Simulate a shutdown. |
| 692 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 693 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 694 | content::NotificationService::AllSources(), |
| 695 | content::NotificationService::NoDetails()); |
| 696 | |
| 697 | // Even if the browsers are destructed during shutdown, the profiles stay |
| 698 | // open. |
| 699 | browser1.reset(); |
| 700 | browser2.reset(); |
| 701 | |
| 702 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 703 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 704 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 705 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 706 | } |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 707 | |
| 708 | TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 709 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 710 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 711 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 712 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 713 | |
| 714 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 715 | |
| 716 | // Successfully create the profiles. |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 717 | TestingProfile* profile1 = |
| 718 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 719 | ASSERT_TRUE(profile1); |
| 720 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 721 | // Incognito profiles should not be managed by the profile manager but by the |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 722 | // original profile. |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 723 | TestingProfile::Builder builder; |
| 724 | builder.SetIncognito(); |
| 725 | scoped_ptr<TestingProfile> profile2 = builder.Build(); |
| 726 | profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>()); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 727 | |
| 728 | std::vector<Profile*> last_opened_profiles = |
| 729 | profile_manager->GetLastOpenedProfiles(); |
| 730 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 731 | |
| 732 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 733 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 734 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 735 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 736 | |
| 737 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 738 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 739 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 740 | |
| 741 | // And for profile2. |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 742 | Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(), |
| 743 | chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 744 | scoped_ptr<Browser> browser2a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 745 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 746 | |
| 747 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 748 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 749 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 750 | |
| 751 | // Adding more browsers doesn't change anything. |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 752 | scoped_ptr<Browser> browser2b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 753 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 754 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 755 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 756 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 757 | |
| 758 | // Close the browsers. |
| 759 | browser2a.reset(); |
| 760 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 761 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 762 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 763 | |
| 764 | browser2b.reset(); |
| 765 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 766 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 767 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 768 | |
| 769 | browser1.reset(); |
| 770 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 771 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 772 | } |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 773 | #endif // !defined(OS_ANDROID) |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 774 | |
| 775 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 776 | // There's no Browser object on Android and there's no multi-profiles on Chrome. |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 777 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) { |
| 778 | base::FilePath dest_path = temp_dir_.path(); |
| 779 | dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile")); |
| 780 | |
| 781 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 782 | |
| 783 | TestingProfile* profile = |
| 784 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); |
| 785 | ASSERT_TRUE(profile); |
| 786 | profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); |
| 787 | |
| 788 | // Here the last used profile is still the "Default" profile. |
| 789 | Profile* last_used_profile = profile_manager->GetLastUsedProfile(); |
| 790 | EXPECT_NE(profile, last_used_profile); |
| 791 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 792 | // Create a browser for the profile. |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 793 | Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop()); |
| 794 | scoped_ptr<Browser> browser( |
| 795 | chrome::CreateBrowserWithTestWindowForParams(&profile_params)); |
| 796 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 797 | EXPECT_NE(profile, last_used_profile); |
| 798 | |
| 799 | // Close the browser. |
| 800 | browser.reset(); |
| 801 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 802 | EXPECT_NE(profile, last_used_profile); |
| 803 | } |
| 804 | |
| 805 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) { |
| 806 | base::FilePath dest_path1 = temp_dir_.path(); |
| 807 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile")); |
| 808 | |
| 809 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 810 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1")); |
| 811 | |
| 812 | base::FilePath dest_path3 = temp_dir_.path(); |
| 813 | dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2")); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 814 | |
| 815 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 816 | |
| 817 | // Successfully create the profiles. |
| 818 | TestingProfile* normal_profile = |
| 819 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 820 | ASSERT_TRUE(normal_profile); |
| 821 | |
| 822 | // Add one ephemeral profile which should not end up in this list. |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 823 | TestingProfile* ephemeral_profile1 = |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 824 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 825 | ASSERT_TRUE(ephemeral_profile1); |
| 826 | ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, |
| 827 | true); |
| 828 | |
| 829 | // Add second ephemeral profile but don't mark it as such yet. |
| 830 | TestingProfile* ephemeral_profile2 = |
| 831 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3)); |
| 832 | ASSERT_TRUE(ephemeral_profile2); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 833 | |
| 834 | // Create a browser for profile1. |
| 835 | Browser::CreateParams profile1_params(normal_profile, |
| 836 | chrome::GetActiveDesktop()); |
| 837 | scoped_ptr<Browser> browser1( |
| 838 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
| 839 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 840 | // Create browsers for the ephemeral profile. |
| 841 | Browser::CreateParams profile2_params(ephemeral_profile1, |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 842 | chrome::GetActiveDesktop()); |
| 843 | scoped_ptr<Browser> browser2( |
| 844 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
| 845 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 846 | Browser::CreateParams profile3_params(ephemeral_profile2, |
| 847 | chrome::GetActiveDesktop()); |
| 848 | scoped_ptr<Browser> browser3( |
| 849 | chrome::CreateBrowserWithTestWindowForParams(&profile3_params)); |
| 850 | |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 851 | std::vector<Profile*> last_opened_profiles = |
| 852 | profile_manager->GetLastOpenedProfiles(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 853 | ASSERT_EQ(2U, last_opened_profiles.size()); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 854 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 855 | EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]); |
| 856 | |
| 857 | // Mark the second profile ephemeral. |
| 858 | ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, |
| 859 | true); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 860 | |
| 861 | // Simulate a shutdown. |
| 862 | content::NotificationService::current()->Notify( |
| 863 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 864 | content::NotificationService::AllSources(), |
| 865 | content::NotificationService::NoDetails()); |
| 866 | browser1.reset(); |
| 867 | browser2.reset(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 868 | browser3.reset(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 869 | |
| 870 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 871 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 872 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
| 873 | } |
| 874 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 875 | TEST_F(ProfileManagerTest, ActiveProfileDeleted) { |
| 876 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 877 | ASSERT_TRUE(profile_manager); |
| 878 | |
| 879 | // Create and load two profiles. |
| 880 | const std::string profile_name1 = "New Profile 1"; |
| 881 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 882 | base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); |
| 883 | base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 884 | |
| 885 | MockObserver mock_observer; |
| 886 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 887 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
| 888 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 889 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 890 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 891 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 892 | |
| 893 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 894 | EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 895 | |
| 896 | // Set the active profile. |
| 897 | PrefService* local_state = g_browser_process->local_state(); |
| 898 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 899 | |
| 900 | // Delete the active profile. |
| 901 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 902 | ProfileManager::CreateCallback()); |
| 903 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 904 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 905 | |
| 906 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 907 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 908 | } |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 909 | |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 910 | TEST_F(ProfileManagerTest, LastProfileDeleted) { |
| 911 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 912 | ASSERT_TRUE(profile_manager); |
| 913 | |
| 914 | // Create and load a profile. |
| 915 | const std::string profile_name1 = "New Profile 1"; |
| 916 | base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); |
| 917 | |
| 918 | MockObserver mock_observer; |
| 919 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 920 | testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 921 | |
| 922 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 923 | base::RunLoop().RunUntilIdle(); |
| 924 | |
| 925 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 926 | EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 927 | |
| 928 | // Set it as the active profile. |
| 929 | PrefService* local_state = g_browser_process->local_state(); |
| 930 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 931 | |
| 932 | // Delete the active profile. |
| 933 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 934 | ProfileManager::CreateCallback()); |
| 935 | // Spin the message loop so that all the callbacks can finish running. |
| 936 | base::RunLoop().RunUntilIdle(); |
| 937 | |
| 938 | // A new profile should have been created |
| 939 | const std::string profile_name2 = "Profile 1"; |
| 940 | base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); |
| 941 | |
| 942 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 943 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 944 | EXPECT_EQ(dest_path2, |
| 945 | profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0)); |
| 946 | } |
| 947 | |
| 948 | TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) { |
| 949 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 950 | ASSERT_TRUE(profile_manager); |
| 951 | |
| 952 | // Create and load a profile. |
| 953 | const std::string profile_name1 = "New Profile 1"; |
| 954 | base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); |
| 955 | |
| 956 | MockObserver mock_observer; |
| 957 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 958 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 959 | |
| 960 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 961 | base::RunLoop().RunUntilIdle(); |
| 962 | |
| 963 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 964 | EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 965 | |
| 966 | // Create the profile and register it. |
| 967 | const std::string guest_profile_name = |
| 968 | ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII(); |
| 969 | |
| 970 | TestingProfile::Builder builder; |
| 971 | builder.SetGuestSession(); |
| 972 | builder.SetPath(ProfileManager::GetGuestProfilePath()); |
| 973 | TestingProfile* guest_profile = builder.Build().release(); |
| 974 | guest_profile->set_profile_name(guest_profile_name); |
| 975 | // Registering the profile passes ownership to the ProfileManager. |
| 976 | profile_manager->RegisterTestingProfile(guest_profile, false, false); |
| 977 | |
| 978 | // The Guest profile does not get added to the ProfileInfoCache. |
| 979 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 980 | EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 981 | |
| 982 | // Set the Guest profile as the active profile. |
| 983 | PrefService* local_state = g_browser_process->local_state(); |
| 984 | local_state->SetString(prefs::kProfileLastUsed, guest_profile_name); |
| 985 | |
| 986 | // Delete the other profile. |
| 987 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 988 | ProfileManager::CreateCallback()); |
| 989 | // Spin the message loop so that all the callbacks can finish running. |
| 990 | base::RunLoop().RunUntilIdle(); |
| 991 | |
| 992 | // A new profile should have been created. |
| 993 | const std::string profile_name2 = "Profile 1"; |
| 994 | base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); |
| 995 | |
| 996 | EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size()); |
| 997 | EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 998 | EXPECT_EQ(dest_path2, |
| 999 | profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0)); |
| 1000 | } |
| 1001 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1002 | TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) { |
| 1003 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1004 | return; |
| 1005 | |
| 1006 | // The command line is reset at the end of every test by the test suite. |
[email protected] | c933541 | 2014-07-28 17:47:25 | [diff] [blame] | 1007 | switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1008 | |
| 1009 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1010 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 1011 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 1012 | |
| 1013 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1014 | const base::string16 default_profile_name = |
| 1015 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
| 1016 | const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0); |
| 1017 | Profile* profile1 = AddProfileToCache(profile_manager, |
| 1018 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1019 | EXPECT_EQ(default_profile_name, |
| 1020 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1021 | |
| 1022 | // Multiple profiles means displaying the actual profile names. |
| 1023 | const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1); |
| 1024 | Profile* profile2 = AddProfileToCache(profile_manager, |
| 1025 | "path_2", profile_name2); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1026 | EXPECT_EQ(profile_name1, |
| 1027 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1028 | EXPECT_EQ(profile_name2, |
| 1029 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1030 | |
| 1031 | // Deleting a profile means returning to the default name. |
| 1032 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1033 | ProfileManager::CreateCallback()); |
| 1034 | // Spin the message loop so that all the callbacks can finish running. |
| 1035 | base::RunLoop().RunUntilIdle(); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1036 | EXPECT_EQ(default_profile_name, |
| 1037 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) { |
| 1041 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1042 | return; |
| 1043 | |
| 1044 | // The command line is reset at the end of every test by the test suite. |
[email protected] | c933541 | 2014-07-28 17:47:25 | [diff] [blame] | 1045 | switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1046 | |
| 1047 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1048 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 1049 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 1050 | |
| 1051 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1052 | const base::string16 default_profile_name = |
| 1053 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
| 1054 | const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0); |
| 1055 | Profile* profile1 = AddProfileToCache(profile_manager, |
| 1056 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1057 | EXPECT_EQ(default_profile_name, |
| 1058 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1059 | |
| 1060 | // We should display custom names for local profiles. |
| 1061 | const base::string16 custom_profile_name = ASCIIToUTF16("Batman"); |
| 1062 | cache.SetNameOfProfileAtIndex(0, custom_profile_name); |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 1063 | cache.SetProfileIsUsingDefaultNameAtIndex(0, false); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1064 | EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0)); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1065 | EXPECT_EQ(custom_profile_name, |
| 1066 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1067 | |
| 1068 | // Multiple profiles means displaying the actual profile names. |
| 1069 | const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1); |
| 1070 | Profile* profile2 = AddProfileToCache(profile_manager, |
| 1071 | "path_2", profile_name2); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1072 | EXPECT_EQ(custom_profile_name, |
| 1073 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1074 | EXPECT_EQ(profile_name2, |
| 1075 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1076 | |
| 1077 | // Deleting a profile means returning to the original, custom name. |
| 1078 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1079 | ProfileManager::CreateCallback()); |
| 1080 | // Spin the message loop so that all the callbacks can finish running. |
| 1081 | base::RunLoop().RunUntilIdle(); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1082 | EXPECT_EQ(custom_profile_name, |
| 1083 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) { |
| 1087 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1088 | return; |
| 1089 | |
[email protected] | 29896ee | 2014-06-17 17:20:53 | [diff] [blame] | 1090 | // The command line is reset at the end of every test by the test suite. |
[email protected] | c933541 | 2014-07-28 17:47:25 | [diff] [blame] | 1091 | switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1092 | |
| 1093 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1094 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 1095 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 1096 | |
| 1097 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1098 | const base::string16 default_profile_name = |
| 1099 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
| 1100 | const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0); |
| 1101 | Profile* profile1 = AddProfileToCache(profile_manager, |
| 1102 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1103 | EXPECT_EQ(default_profile_name, |
| 1104 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1105 | |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1106 | // For a signed in profile with a default name we still display |
| 1107 | // IDS_SINGLE_PROFILE_DISPLAY_NAME. |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1108 | cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("[email protected]")); |
| 1109 | EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0)); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1110 | EXPECT_EQ(default_profile_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1111 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1112 | |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1113 | // For a signed in profile with a non-default Gaia given name we display the |
| 1114 | // Gaia given name. |
| 1115 | cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("[email protected]")); |
| 1116 | const base::string16 gaia_given_name(ASCIIToUTF16("given name")); |
| 1117 | cache.SetGAIAGivenNameOfProfileAtIndex(0, gaia_given_name); |
| 1118 | EXPECT_EQ(gaia_given_name, cache.GetNameOfProfileAtIndex(0)); |
| 1119 | EXPECT_EQ(gaia_given_name, |
| 1120 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1121 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1122 | // Multiple profiles means displaying the actual profile names. |
| 1123 | const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1); |
| 1124 | Profile* profile2 = AddProfileToCache(profile_manager, |
| 1125 | "path_2", profile_name2); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1126 | EXPECT_EQ(gaia_given_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1127 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1128 | EXPECT_EQ(profile_name2, |
| 1129 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1130 | |
| 1131 | // Deleting a profile means returning to the original, actual profile name. |
| 1132 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1133 | ProfileManager::CreateCallback()); |
| 1134 | // Spin the message loop so that all the callbacks can finish running. |
| 1135 | base::RunLoop().RunUntilIdle(); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1136 | EXPECT_EQ(gaia_given_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1137 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1138 | } |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1139 | #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 1140 | |
| 1141 | #if defined(OS_MACOSX) |
| 1142 | // These tests are for a Mac-only code path that assumes the browser |
| 1143 | // process isn't killed when all browser windows are closed. |
| 1144 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { |
| 1145 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1146 | ASSERT_TRUE(profile_manager); |
| 1147 | |
| 1148 | // Create and load one profile, and just create a second profile. |
| 1149 | const std::string profile_name1 = "New Profile 1"; |
| 1150 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1151 | base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); |
| 1152 | base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1153 | |
| 1154 | MockObserver mock_observer; |
| 1155 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1156 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 1157 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1158 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1159 | |
| 1160 | // Track the profile, but don't load it. |
| 1161 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 1162 | cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 1163 | base::string16(), 0, std::string()); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1164 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1165 | |
| 1166 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 1167 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
| 1168 | |
| 1169 | // Set the active profile. |
| 1170 | PrefService* local_state = g_browser_process->local_state(); |
| 1171 | local_state->SetString(prefs::kProfileLastUsed, |
| 1172 | dest_path1.BaseName().MaybeAsASCII()); |
| 1173 | |
| 1174 | // Delete the active profile. This should switch and load the unloaded |
| 1175 | // profile. |
| 1176 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1177 | ProfileManager::CreateCallback()); |
| 1178 | |
| 1179 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1180 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1181 | |
| 1182 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1183 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 1184 | } |
| 1185 | |
| 1186 | // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded |
| 1187 | // by simulating a scenario in which the profile that is being loaded as |
| 1188 | // the next active profile has also been marked for deletion, so the |
| 1189 | // ProfileManager needs to recursively select a different next profile. |
| 1190 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { |
| 1191 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1192 | ASSERT_TRUE(profile_manager); |
| 1193 | |
| 1194 | // Create and load one profile, and create two more profiles. |
| 1195 | const std::string profile_name1 = "New Profile 1"; |
| 1196 | const std::string profile_name2 = "New Profile 2"; |
| 1197 | const std::string profile_name3 = "New Profile 3"; |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1198 | base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); |
| 1199 | base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); |
| 1200 | base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1201 | |
| 1202 | MockObserver mock_observer; |
| 1203 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1204 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 1205 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1206 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1207 | |
| 1208 | // Create the other profiles, but don't load them. Assign a fake avatar icon |
| 1209 | // to ensure that profiles in the info cache are sorted by the profile name, |
| 1210 | // and not randomly by the avatar name. |
| 1211 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 1212 | cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 1213 | ASCIIToUTF16(profile_name2), 1, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1214 | cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 1215 | ASCIIToUTF16(profile_name3), 2, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1216 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1217 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1218 | |
| 1219 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 1220 | EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 1221 | |
| 1222 | // Set the active profile. |
| 1223 | PrefService* local_state = g_browser_process->local_state(); |
| 1224 | local_state->SetString(prefs::kProfileLastUsed, |
| 1225 | dest_path1.BaseName().MaybeAsASCII()); |
| 1226 | |
| 1227 | // Delete the active profile, Profile1. |
| 1228 | // This will post a CreateProfileAsync message, that tries to load Profile2, |
| 1229 | // which checks that the profile is not being deleted, and then calls back |
| 1230 | // FinishDeletingProfile for Profile1. |
| 1231 | // Try to break this flow by setting the active profile to Profile2 in the |
| 1232 | // middle (so after the first posted message), and trying to delete Profile2, |
| 1233 | // so that the ProfileManager has to look for a different profile to load. |
| 1234 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1235 | ProfileManager::CreateCallback()); |
| 1236 | local_state->SetString(prefs::kProfileLastUsed, |
| 1237 | dest_path2.BaseName().MaybeAsASCII()); |
| 1238 | profile_manager->ScheduleProfileForDeletion(dest_path2, |
| 1239 | ProfileManager::CreateCallback()); |
| 1240 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 1241 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1242 | |
| 1243 | EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1244 | EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
| 1245 | } |
| 1246 | #endif // !defined(OS_MACOSX) |