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