[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 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | d99bcaa | 2010-12-05 20:15:56 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 9 | #include "base/command_line.h" |
miguelg | 1b49554 | 2016-03-18 17:57:46 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 12 | #include "base/files/scoped_temp_dir.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 13 | #include "base/macros.h" |
vabr | 8684c9a | 2017-03-29 13:14:57 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 0b08bf2 | 2017-08-15 20:10:06 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 20 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 21 | #include "chrome/browser/browser_process.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 22 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 23 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 24 | #include "chrome/browser/io_thread.h" |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 25 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 26 | #include "chrome/browser/prefs/incognito_mode_prefs.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 27 | #include "chrome/browser/profiles/profile.h" |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 29 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 30 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 31 | #include "chrome/browser/profiles/profiles_state.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 32 | #include "chrome/browser/ui/browser.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 33 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | #include "chrome/common/chrome_paths.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 35 | #include "chrome/common/chrome_switches.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 36 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 37 | #include "chrome/grit/generated_resources.h" |
[email protected] | 7688968a | 2013-02-12 21:45:13 | [diff] [blame] | 38 | #include "chrome/test/base/scoped_testing_local_state.h" |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 39 | #include "chrome/test/base/test_browser_window.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 40 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 41 | #include "chrome/test/base/testing_profile.h" |
sdefresne | 506dd52 | 2015-03-12 18:49:12 | [diff] [blame] | 42 | #include "components/history/core/browser/history_service.h" |
[email protected] | 29896ee | 2014-06-17 17:20:53 | [diff] [blame] | 43 | #include "components/signin/core/common/profile_management_switches.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 44 | #include "content/public/browser/notification_service.h" |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 45 | #include "content/public/common/content_switches.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 46 | #include "content/public/test/test_browser_thread_bundle.h" |
Gabriel Charette | faf43219 | 2017-08-17 22:00:41 | [diff] [blame] | 47 | #include "content/public/test/test_utils.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 48 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 50 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 52 | #if defined(OS_CHROMEOS) |
merkulova | 793f302 | 2015-02-04 10:18:30 | [diff] [blame] | 53 | #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 54 | #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 55 | #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
xdai | d05f51c | 2015-03-23 17:21:33 | [diff] [blame] | 56 | #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 57 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 58 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 59 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 60 | #include "chromeos/chromeos_switches.h" |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 61 | #include "components/user_manager/user_manager.h" |
alemate | 3c1c088 | 2016-10-27 19:05:12 | [diff] [blame] | 62 | #include "components/user_manager/user_names.h" |
Andrew Wilson | c6c2c37e | 2017-09-01 14:44:27 | [diff] [blame] | 63 | #include "extensions/common/features/feature_session_type.h" |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 64 | #endif // defined(OS_CHROMEOS) |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 65 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 66 | using base::ASCIIToUTF16; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 67 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 68 | namespace { |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 69 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 70 | // This global variable is used to check that value returned to different |
| 71 | // observers is the same. |
| 72 | Profile* g_created_profile; |
| 73 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 74 | class UnittestProfileManager : public ::ProfileManagerWithoutInit { |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 75 | public: |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 76 | explicit UnittestProfileManager(const base::FilePath& user_data_dir) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 77 | : ::ProfileManagerWithoutInit(user_data_dir) {} |
| 78 | |
| 79 | protected: |
dcheng | 1fd71638 | 2014-10-22 20:14:41 | [diff] [blame] | 80 | Profile* CreateProfileHelper(const base::FilePath& file_path) override { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 81 | if (!base::PathExists(file_path)) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 82 | if (!base::CreateDirectory(file_path)) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | return new TestingProfile(file_path, NULL); |
| 86 | } |
| 87 | |
dcheng | 1fd71638 | 2014-10-22 20:14:41 | [diff] [blame] | 88 | Profile* CreateProfileAsyncHelper(const base::FilePath& path, |
| 89 | Delegate* delegate) override { |
Gabriel Charette | 0b08bf2 | 2017-08-15 20:10:06 | [diff] [blame] | 90 | // ThreadTaskRunnerHandle::Get() is TestingProfile's "async" IOTaskRunner |
| 91 | // (ref. TestingProfile::GetIOTaskRunner()). |
| 92 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 93 | FROM_HERE, |
tzik | 6e4834a | 2017-04-20 15:42:58 | [diff] [blame] | 94 | base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 95 | |
| 96 | return new TestingProfile(path, this); |
| 97 | } |
| 98 | }; |
| 99 | |
miguelg | 1b49554 | 2016-03-18 17:57:46 | [diff] [blame] | 100 | void ExpectNullProfile(base::Closure closure, Profile* profile) { |
| 101 | EXPECT_EQ(nullptr, profile); |
| 102 | closure.Run(); |
| 103 | } |
| 104 | |
| 105 | void ExpectProfileWithName(const std::string& profile_name, |
| 106 | bool incognito, |
| 107 | base::Closure closure, |
| 108 | Profile* profile) { |
| 109 | EXPECT_NE(nullptr, profile); |
| 110 | EXPECT_EQ(incognito, profile->IsOffTheRecord()); |
| 111 | if (incognito) |
| 112 | profile = profile->GetOriginalProfile(); |
| 113 | |
| 114 | // Create a profile on the fly so the the same comparison |
| 115 | // can be used in Windows and other platforms. |
| 116 | EXPECT_EQ(base::FilePath().AppendASCII(profile_name), |
| 117 | profile->GetPath().BaseName()); |
| 118 | closure.Run(); |
| 119 | } |
| 120 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 121 | } // namespace |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 122 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 123 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 124 | protected: |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 125 | class MockObserver { |
| 126 | public: |
| 127 | MOCK_METHOD2(OnProfileCreated, |
| 128 | void(Profile* profile, Profile::CreateStatus status)); |
| 129 | }; |
| 130 | |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 131 | ProfileManagerTest() |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 132 | : local_state_(TestingBrowserProcess::GetGlobal()) { |
[email protected] | cb0e4f1 | 2009-12-03 00:09:09 | [diff] [blame] | 133 | } |
| 134 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 135 | void SetUp() override { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 136 | // Create a new temporary directory, and store the path |
| 137 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 138 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 139 | new UnittestProfileManager(temp_dir_.GetPath())); |
[email protected] | f89b50f | 2012-08-16 21:36:50 | [diff] [blame] | 140 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 141 | #if defined(OS_CHROMEOS) |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 142 | base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 143 | cl->AppendSwitch(switches::kTestType); |
xdai | d05f51c | 2015-03-23 17:21:33 | [diff] [blame] | 144 | chromeos::WallpaperManager::Initialize(); |
Andrew Wilson | c6c2c37e | 2017-09-01 14:44:27 | [diff] [blame] | 145 | |
| 146 | // Have to manually reset the session type in between test runs because |
| 147 | // some tests log in users. |
| 148 | ASSERT_EQ(extensions::FeatureSessionType::INITIAL, |
| 149 | extensions::GetCurrentFeatureSessionType()); |
| 150 | session_type_ = extensions::ScopedCurrentFeatureSessionType( |
| 151 | extensions::GetCurrentFeatureSessionType()); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 152 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | } |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 154 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 155 | void TearDown() override { |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 156 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 157 | content::RunAllTasksUntilIdle(); |
xdai | d05f51c | 2015-03-23 17:21:33 | [diff] [blame] | 158 | #if defined(OS_CHROMEOS) |
Andrew Wilson | c6c2c37e | 2017-09-01 14:44:27 | [diff] [blame] | 159 | session_type_.reset(); |
xdai | d05f51c | 2015-03-23 17:21:33 | [diff] [blame] | 160 | chromeos::WallpaperManager::Shutdown(); |
| 161 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 163 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 164 | // Helper function to create a profile with |name| for a profile |manager|. |
| 165 | void CreateProfileAsync(ProfileManager* manager, |
| 166 | const std::string& name, |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 167 | bool is_supervised, |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 168 | MockObserver* mock_observer) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 169 | manager->CreateProfileAsync(temp_dir_.GetPath().AppendASCII(name), |
| 170 | base::Bind(&MockObserver::OnProfileCreated, |
| 171 | base::Unretained(mock_observer)), |
| 172 | base::UTF8ToUTF16(name), |
| 173 | profiles::GetDefaultAvatarIconUrl(0), |
| 174 | is_supervised ? "Dummy ID" : std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 175 | } |
| 176 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 177 | // Helper function to add a profile with |profile_name| to |profile_manager|'s |
| 178 | // ProfileAttributesStorage, and return the profile created. |
| 179 | Profile* AddProfileToStorage(ProfileManager* profile_manager, |
| 180 | const std::string& path_suffix, |
| 181 | const base::string16& profile_name) { |
| 182 | ProfileAttributesStorage& storage = |
| 183 | profile_manager->GetProfileAttributesStorage(); |
| 184 | size_t num_profiles = storage.GetNumberOfProfiles(); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 185 | base::FilePath path = temp_dir_.GetPath().AppendASCII(path_suffix); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 186 | storage.AddProfile(path, profile_name, std::string(), base::string16(), 0, |
| 187 | std::string()); |
| 188 | EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 189 | return profile_manager->GetProfile(path); |
| 190 | } |
| 191 | |
palar | 47cf44bd | 2017-03-03 15:36:11 | [diff] [blame] | 192 | // Helper function to set profile ephemeral at prefs and attributes storage. |
| 193 | void SetProfileEphemeral(Profile* profile) { |
| 194 | profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); |
| 195 | |
| 196 | // Update IsEphemeral in attributes storage, normally it happened via |
| 197 | // kForceEphemeralProfiles pref change event routed to |
| 198 | // ProfileImpl::UpdateIsEphemeralInStorage(). |
| 199 | ProfileAttributesEntry* entry; |
| 200 | ProfileAttributesStorage& storage = |
| 201 | g_browser_process->profile_manager()->GetProfileAttributesStorage(); |
| 202 | EXPECT_TRUE( |
| 203 | storage.GetProfileAttributesWithPath(profile->GetPath(), &entry)); |
| 204 | entry->SetIsEphemeral(true); |
| 205 | } |
| 206 | |
Gabriel Charette | cc8362b | 2017-09-20 21:59:40 | [diff] [blame] | 207 | content::TestBrowserThreadBundle thread_bundle_; |
| 208 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 209 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 210 | // Helper function to register an user with id |user_id| and create profile |
| 211 | // with a correct path. |
| 212 | void RegisterUser(const std::string& user_id) { |
| 213 | chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get(); |
| 214 | const std::string user_id_hash = |
| 215 | profile_helper->GetUserIdHashByUserIdForTesting(user_id); |
| 216 | user_manager::UserManager::Get()->UserLoggedIn( |
alemate | 3ffbde6f | 2015-11-03 02:02:55 | [diff] [blame] | 217 | AccountId::FromUserEmail(user_id), user_id_hash, false); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 218 | g_browser_process->profile_manager()->GetProfile( |
| 219 | profile_helper->GetProfilePathByUserIdHash(user_id_hash)); |
| 220 | } |
| 221 | |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 222 | chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 223 | chromeos::ScopedTestCrosSettings test_cros_settings_; |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 224 | #endif |
| 225 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 226 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 227 | base::ScopedTempDir temp_dir_; |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 228 | ScopedTestingLocalState local_state_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 229 | |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 230 | |
| 231 | #if defined(OS_CHROMEOS) |
| 232 | chromeos::ScopedTestUserManager test_user_manager_; |
Andrew Wilson | c6c2c37e | 2017-09-01 14:44:27 | [diff] [blame] | 233 | std::unique_ptr<base::AutoReset<extensions::FeatureSessionType>> |
| 234 | session_type_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 235 | #endif |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 236 | |
| 237 | DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | }; |
| 239 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 240 | TEST_F(ProfileManagerTest, GetProfile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 241 | base::FilePath dest_path = temp_dir_.GetPath(); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 242 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 244 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 245 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | // Successfully create a profile. |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 247 | Profile* profile = profile_manager->GetProfile(dest_path); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 248 | EXPECT_TRUE(profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 249 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 250 | // The profile already exists when we call GetProfile. Just load it. |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 251 | EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | } |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 253 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 254 | TEST_F(ProfileManagerTest, DefaultProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 255 | base::FilePath expected_default = |
| 256 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 257 | EXPECT_EQ( |
| 258 | expected_default.value(), |
| 259 | g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 260 | } |
| 261 | |
skuhne | 24bb2443 | 2014-09-08 19:31:47 | [diff] [blame] | 262 | MATCHER(NotFail, "Profile creation failure status is not reported.") { |
| 263 | return arg == Profile::CREATE_STATUS_CREATED || |
| 264 | arg == Profile::CREATE_STATUS_INITIALIZED; |
| 265 | } |
| 266 | |
| 267 | MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
| 268 | if (!g_created_profile) |
| 269 | g_created_profile = arg; |
| 270 | return arg != NULL && arg == g_created_profile; |
| 271 | } |
| 272 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 273 | #if defined(OS_CHROMEOS) |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 274 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 275 | // This functionality only exists on Chrome OS. |
| 276 | TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 277 | base::FilePath expected_default = |
| 278 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 279 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 280 | EXPECT_EQ(expected_default.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 281 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 282 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 283 | const char kTestUserName[] = "[email protected]"; |
alemate | 3ffbde6f | 2015-11-03 02:02:55 | [diff] [blame] | 284 | const AccountId test_account_id(AccountId::FromUserEmail(kTestUserName)); |
merkulova | 793f302 | 2015-02-04 10:18:30 | [diff] [blame] | 285 | chromeos::FakeChromeUserManager* user_manager = |
| 286 | new chromeos::FakeChromeUserManager(); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 287 | chromeos::ScopedUserManagerEnabler enabler(user_manager); |
| 288 | |
alemate | 3ffbde6f | 2015-11-03 02:02:55 | [diff] [blame] | 289 | const user_manager::User* active_user = |
| 290 | user_manager->AddUser(test_account_id); |
| 291 | user_manager->LoginUser(test_account_id); |
| 292 | user_manager->SwitchActiveUser(test_account_id); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 293 | |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 294 | profile_manager->Observe( |
| 295 | chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 296 | content::NotificationService::AllSources(), |
| 297 | content::Details<const user_manager::User>(active_user)); |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 298 | base::FilePath expected_logged_in( |
| 299 | chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash())); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 300 | EXPECT_EQ(expected_logged_in.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 301 | profile_manager->GetInitialProfileDir().value()); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 302 | VLOG(1) << temp_dir_.GetPath() |
| 303 | .Append(profile_manager->GetInitialProfileDir()) |
| 304 | .value(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 305 | } |
| 306 | |
xiyuan | 41d32a3 | 2017-06-12 20:06:13 | [diff] [blame] | 307 | // Test Get[ActiveUser|PrimaryUser|LastUsed]Profile does not load user profile. |
| 308 | TEST_F(ProfileManagerTest, UserProfileLoading) { |
| 309 | using chromeos::ProfileHelper; |
| 310 | |
| 311 | Profile* const signin_profile = ProfileHelper::GetSigninProfile(); |
| 312 | |
| 313 | // Get[Active|Primary|LastUsed]Profile return the sign-in profile before login |
| 314 | // happens. IsSameProfile() is used to properly test against TestProfile whose |
| 315 | // OTR version uses a different temp path. |
| 316 | EXPECT_TRUE( |
| 317 | ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile)); |
| 318 | EXPECT_TRUE( |
| 319 | ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile)); |
| 320 | EXPECT_TRUE( |
| 321 | ProfileManager::GetLastUsedProfile()->IsSameProfile(signin_profile)); |
| 322 | |
| 323 | // User signs in but user profile loading has not started. |
| 324 | const std::string user_id = "[email protected]"; |
| 325 | const std::string user_id_hash = |
| 326 | ProfileHelper::Get()->GetUserIdHashByUserIdForTesting(user_id); |
| 327 | user_manager::UserManager::Get()->UserLoggedIn( |
| 328 | AccountId::FromUserEmail(user_id), user_id_hash, false); |
| 329 | |
| 330 | // Sign-in profile should be returned at this stage. Otherwise, login code |
| 331 | // ends up in an invalid state. Strange things as in http://crbug.com/728683 |
| 332 | // and http://crbug.com/718734 happens. |
| 333 | EXPECT_TRUE( |
| 334 | ProfileManager::GetActiveUserProfile()->IsSameProfile(signin_profile)); |
| 335 | EXPECT_TRUE( |
| 336 | ProfileManager::GetPrimaryUserProfile()->IsSameProfile(signin_profile)); |
xiyuan | 8c6a411 | 2017-06-19 21:03:49 | [diff] [blame] | 337 | |
| 338 | // GetLastUsedProfile() after login but before a user profile is loaded is |
| 339 | // fatal. |
| 340 | EXPECT_DEATH_IF_SUPPORTED(ProfileManager::GetLastUsedProfile(), ".*"); |
xiyuan | 41d32a3 | 2017-06-12 20:06:13 | [diff] [blame] | 341 | |
| 342 | // Simulate UserSessionManager loads the profile. |
| 343 | Profile* const user_profile = |
| 344 | g_browser_process->profile_manager()->GetProfile( |
| 345 | ProfileHelper::Get()->GetProfilePathByUserIdHash(user_id_hash)); |
| 346 | ASSERT_FALSE(user_profile->IsSameProfile(signin_profile)); |
| 347 | |
| 348 | // User profile is returned thereafter. |
| 349 | EXPECT_TRUE( |
| 350 | ProfileManager::GetActiveUserProfile()->IsSameProfile(user_profile)); |
| 351 | EXPECT_TRUE( |
| 352 | ProfileManager::GetPrimaryUserProfile()->IsSameProfile(user_profile)); |
| 353 | EXPECT_TRUE( |
| 354 | ProfileManager::GetLastUsedProfile()->IsSameProfile(user_profile)); |
| 355 | } |
| 356 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 357 | #endif |
| 358 | |
[email protected] | 52d69b88 | 2010-06-17 17:35:51 | [diff] [blame] | 359 | TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 360 | base::FilePath dest_path1 = temp_dir_.GetPath(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 361 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 362 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 363 | base::FilePath dest_path2 = temp_dir_.GetPath(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 364 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 365 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 366 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 367 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 368 | // Successfully create the profiles. |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 369 | TestingProfile* profile1 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 370 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 371 | ASSERT_TRUE(profile1); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 372 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 373 | TestingProfile* profile2 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 374 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 375 | ASSERT_TRUE(profile2); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 376 | |
| 377 | // Force lazy-init of some profile services to simulate use. |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 378 | ASSERT_TRUE(profile1->CreateHistoryService(true, false)); |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 379 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 380 | profile1, ServiceAccessType::EXPLICIT_ACCESS)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 381 | profile1->CreateBookmarkModel(true); |
pke | 3e0d5c9 | 2016-08-08 09:07:30 | [diff] [blame] | 382 | EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile1)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 383 | profile2->CreateBookmarkModel(true); |
pke | 3e0d5c9 | 2016-08-08 09:07:30 | [diff] [blame] | 384 | EXPECT_TRUE(BookmarkModelFactory::GetForBrowserContext(profile2)); |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 385 | ASSERT_TRUE(profile2->CreateHistoryService(true, false)); |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 386 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 387 | profile2, ServiceAccessType::EXPLICIT_ACCESS)); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 388 | |
| 389 | // Make sure any pending tasks run before we destroy the profiles. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 390 | content::RunAllTasksUntilIdle(); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 391 | |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 392 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 393 | |
[email protected] | 237e6d0 | 2010-11-08 21:45:42 | [diff] [blame] | 394 | // Make sure history cleans up correctly. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 395 | content::RunAllTasksUntilIdle(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 396 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 397 | |
miguelg | 1b49554 | 2016-03-18 17:57:46 | [diff] [blame] | 398 | TEST_F(ProfileManagerTest, LoadNonExistingProfile) { |
| 399 | const std::string profile_name = "NonExistingProfile"; |
| 400 | base::RunLoop run_loop_1; |
| 401 | base::RunLoop run_loop_2; |
| 402 | |
| 403 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 404 | profile_manager->LoadProfile( |
| 405 | profile_name, false /* incognito */, |
| 406 | base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure())); |
| 407 | run_loop_1.Run(); |
| 408 | |
| 409 | profile_manager->LoadProfile( |
| 410 | profile_name, true /* incognito */, |
| 411 | base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure())); |
| 412 | run_loop_2.Run(); |
| 413 | } |
| 414 | |
| 415 | TEST_F(ProfileManagerTest, LoadExistingProfile) { |
| 416 | const std::string profile_name = "MyProfile"; |
| 417 | const std::string other_name = "SomeOtherProfile"; |
| 418 | MockObserver mock_observer1; |
| 419 | EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail())) |
| 420 | .Times(testing::AtLeast(1)); |
| 421 | |
| 422 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 423 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1); |
| 424 | |
| 425 | // Make sure a real profile is created before continuing. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 426 | content::RunAllTasksUntilIdle(); |
miguelg | 1b49554 | 2016-03-18 17:57:46 | [diff] [blame] | 427 | |
| 428 | base::RunLoop load_profile; |
| 429 | bool incognito = false; |
| 430 | profile_manager->LoadProfile( |
| 431 | profile_name, incognito, |
| 432 | base::Bind(&ExpectProfileWithName, profile_name, incognito, |
| 433 | load_profile.QuitClosure())); |
| 434 | load_profile.Run(); |
| 435 | |
| 436 | base::RunLoop load_profile_incognito; |
| 437 | incognito = true; |
| 438 | profile_manager->LoadProfile( |
| 439 | profile_name, incognito, |
| 440 | base::Bind(&ExpectProfileWithName, profile_name, incognito, |
| 441 | load_profile_incognito.QuitClosure())); |
| 442 | load_profile_incognito.Run(); |
| 443 | |
| 444 | // Loading some other non existing profile should still return null. |
| 445 | base::RunLoop load_other_profile; |
| 446 | profile_manager->LoadProfile( |
| 447 | other_name, false, |
| 448 | base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure())); |
| 449 | load_other_profile.Run(); |
| 450 | } |
| 451 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 452 | TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 453 | g_created_profile = NULL; |
| 454 | |
| 455 | MockObserver mock_observer1; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 456 | EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 457 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 458 | MockObserver mock_observer2; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 459 | EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 460 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 461 | MockObserver mock_observer3; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 462 | EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 463 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 464 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 465 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 466 | const std::string profile_name = "New Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 467 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1); |
| 468 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer2); |
| 469 | CreateProfileAsync(profile_manager, profile_name, false, &mock_observer3); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 470 | |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 471 | content::RunAllTasksUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 475 | const std::string profile_name1 = "New Profile 1"; |
| 476 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 477 | |
| 478 | MockObserver mock_observer; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 479 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 480 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 481 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 482 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 483 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 484 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 485 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 486 | |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 487 | content::RunAllTasksUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 488 | } |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 489 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 490 | TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) { |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 491 | std::string name = "0 Supervised Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 492 | |
| 493 | MockObserver mock_observer; |
| 494 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 495 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 496 | |
| 497 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 498 | ProfileAttributesStorage& storage = |
| 499 | profile_manager->GetProfileAttributesStorage(); |
| 500 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 501 | |
| 502 | CreateProfileAsync(profile_manager, name, true, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 503 | content::RunAllTasksUntilIdle(); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 504 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 505 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 506 | // Supervised profiles should start out omitted from the profile list. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 507 | EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 508 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 509 | name = "1 Regular Profile"; |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 510 | CreateProfileAsync(profile_manager, name, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 511 | content::RunAllTasksUntilIdle(); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 512 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 513 | EXPECT_EQ(2u, storage.GetNumberOfProfiles()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 514 | // Non-supervised profiles should be included in the profile list. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 515 | EXPECT_FALSE(storage.GetAllProfilesAttributesSortedByName()[1u]->IsOmitted()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 516 | } |
| 517 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 518 | TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) { |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 519 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 520 | ProfileAttributesStorage& storage = |
| 521 | profile_manager->GetProfileAttributesStorage(); |
| 522 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 523 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 524 | const base::FilePath supervised_path = |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 525 | temp_dir_.GetPath().AppendASCII("Supervised"); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 526 | TestingProfile* supervised_profile = |
| 527 | new TestingProfile(supervised_path, NULL); |
| 528 | supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID"); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 529 | |
| 530 | // RegisterTestingProfile adds the profile to the cache and takes ownership. |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 531 | profile_manager->RegisterTestingProfile(supervised_profile, true, false); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 532 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
| 533 | EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 534 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 535 | const base::FilePath nonsupervised_path = |
| 536 | temp_dir_.GetPath().AppendASCII("Non-Supervised"); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 537 | TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path, |
| 538 | NULL); |
| 539 | profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 540 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 541 | EXPECT_EQ(2u, storage.GetNumberOfProfiles()); |
| 542 | ProfileAttributesEntry* entry; |
| 543 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry)); |
| 544 | EXPECT_TRUE(entry->IsOmitted()); |
| 545 | |
| 546 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry)); |
| 547 | EXPECT_FALSE(entry->IsOmitted()); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 548 | } |
| 549 | |
[email protected] | 4251165a | 2013-07-17 04:33:40 | [diff] [blame] | 550 | TEST_F(ProfileManagerTest, GetGuestProfilePath) { |
| 551 | base::FilePath guest_path = ProfileManager::GetGuestProfilePath(); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 552 | base::FilePath expected_path = temp_dir_.GetPath(); |
[email protected] | 4251165a | 2013-07-17 04:33:40 | [diff] [blame] | 553 | expected_path = expected_path.Append(chrome::kGuestProfileDir); |
| 554 | EXPECT_EQ(expected_path, guest_path); |
| 555 | } |
| 556 | |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 557 | TEST_F(ProfileManagerTest, GetSystemProfilePath) { |
| 558 | base::FilePath system_profile_path = ProfileManager::GetSystemProfilePath(); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 559 | base::FilePath expected_path = temp_dir_.GetPath(); |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 560 | expected_path = expected_path.Append(chrome::kSystemProfileDir); |
| 561 | EXPECT_EQ(expected_path, system_profile_path); |
| 562 | } |
| 563 | |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 564 | class UnittestGuestProfileManager : public UnittestProfileManager { |
| 565 | public: |
| 566 | explicit UnittestGuestProfileManager(const base::FilePath& user_data_dir) |
| 567 | : UnittestProfileManager(user_data_dir) {} |
| 568 | |
| 569 | protected: |
dcheng | 1fd71638 | 2014-10-22 20:14:41 | [diff] [blame] | 570 | Profile* CreateProfileHelper(const base::FilePath& file_path) override { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 571 | TestingProfile::Builder builder; |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 572 | builder.SetGuestSession(); |
[email protected] | d22f7c93 | 2014-02-20 22:48:58 | [diff] [blame] | 573 | builder.SetPath(file_path); |
| 574 | TestingProfile* testing_profile = builder.Build().release(); |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 575 | return testing_profile; |
| 576 | } |
| 577 | }; |
| 578 | |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 579 | class ProfileManagerGuestTest : public ProfileManagerTest { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 580 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 581 | void SetUp() override { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 582 | // Create a new temporary directory, and store the path |
| 583 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 584 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 585 | new UnittestGuestProfileManager(temp_dir_.GetPath())); |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 586 | |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 587 | #if defined(OS_CHROMEOS) |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 588 | base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 589 | // This switch is needed to skip non-test specific behavior in |
| 590 | // ProfileManager (accessing DBusThreadManager). |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 591 | cl->AppendSwitch(switches::kTestType); |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 592 | |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 593 | cl->AppendSwitch(chromeos::switches::kGuestSession); |
| 594 | cl->AppendSwitch(::switches::kIncognito); |
| 595 | |
xdai | d05f51c | 2015-03-23 17:21:33 | [diff] [blame] | 596 | chromeos::WallpaperManager::Initialize(); |
Andrew Wilson | c6c2c37e | 2017-09-01 14:44:27 | [diff] [blame] | 597 | // Have to manually reset the session type in between test runs because |
| 598 | // RegisterUser() changes it. |
| 599 | ASSERT_EQ(extensions::FeatureSessionType::INITIAL, |
| 600 | extensions::GetCurrentFeatureSessionType()); |
| 601 | session_type_ = extensions::ScopedCurrentFeatureSessionType( |
| 602 | extensions::GetCurrentFeatureSessionType()); |
| 603 | |
alemate | 3c1c088 | 2016-10-27 19:05:12 | [diff] [blame] | 604 | RegisterUser(user_manager::kGuestUserName); |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 605 | #endif |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 606 | } |
| 607 | }; |
| 608 | |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 609 | TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { |
[email protected] | 2a92f218 | 2014-03-25 00:47:59 | [diff] [blame] | 610 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 611 | ASSERT_TRUE(profile_manager); |
| 612 | |
| 613 | Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 614 | ASSERT_TRUE(profile); |
| 615 | EXPECT_TRUE(profile->IsOffTheRecord()); |
| 616 | } |
| 617 | |
| 618 | #if defined(OS_CHROMEOS) |
[email protected] | 9b6e10c | 2014-05-20 19:12:28 | [diff] [blame] | 619 | TEST_F(ProfileManagerGuestTest, GuestProfileIngonito) { |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 620 | Profile* primary_profile = ProfileManager::GetPrimaryUserProfile(); |
| 621 | EXPECT_TRUE(primary_profile->IsOffTheRecord()); |
| 622 | |
| 623 | Profile* active_profile = ProfileManager::GetActiveUserProfile(); |
| 624 | EXPECT_TRUE(active_profile->IsOffTheRecord()); |
| 625 | |
| 626 | EXPECT_TRUE(active_profile->IsSameProfile(primary_profile)); |
[email protected] | d22f7c93 | 2014-02-20 22:48:58 | [diff] [blame] | 627 | |
| 628 | Profile* last_used_profile = ProfileManager::GetLastUsedProfile(); |
| 629 | EXPECT_TRUE(last_used_profile->IsOffTheRecord()); |
| 630 | |
| 631 | EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile)); |
[email protected] | 0ff6198e | 2014-01-08 22:45:00 | [diff] [blame] | 632 | } |
| 633 | #endif |
| 634 | |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 635 | TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 636 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 637 | ProfileAttributesStorage& storage = |
| 638 | profile_manager->GetProfileAttributesStorage(); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 639 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
vabr | 8684c9a | 2017-03-29 13:14:57 | [diff] [blame] | 640 | base::MakeUnique<base::Value>(true)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 641 | |
| 642 | // Setting a pref which is not applicable to a system (i.e., Android in this |
| 643 | // case) does not necessarily create it. Don't bother continuing with the |
| 644 | // test if this pref doesn't exist because it will not load the profiles if |
| 645 | // it cannot verify that the pref for background mode is enabled. |
| 646 | if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled)) |
| 647 | return; |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 648 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 649 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
| 650 | |
| 651 | storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"), |
| 652 | ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string()); |
| 653 | storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"), |
| 654 | ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string()); |
| 655 | storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"), |
| 656 | ASCIIToUTF16("name_3"), "34567", base::string16(), 0, std::string()); |
| 657 | |
| 658 | EXPECT_EQ(3u, storage.GetNumberOfProfiles()); |
| 659 | |
| 660 | std::vector<ProfileAttributesEntry*> entries = |
| 661 | storage.GetAllProfilesAttributes(); |
| 662 | entries[0u]->SetBackgroundStatus(true); |
| 663 | entries[2u]->SetBackgroundStatus(true); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 664 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 665 | profile_manager->AutoloadProfiles(); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 666 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 667 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 668 | } |
| 669 | |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 670 | TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) { |
| 671 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 672 | ProfileAttributesStorage& storage = |
| 673 | profile_manager->GetProfileAttributesStorage(); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 674 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
vabr | 8684c9a | 2017-03-29 13:14:57 | [diff] [blame] | 675 | base::MakeUnique<base::Value>(false)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 676 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 677 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
| 678 | |
| 679 | storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"), |
| 680 | ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string()); |
| 681 | storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"), |
| 682 | ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string()); |
| 683 | |
| 684 | EXPECT_EQ(2u, storage.GetNumberOfProfiles()); |
| 685 | |
| 686 | std::vector<ProfileAttributesEntry*> entries = |
| 687 | storage.GetAllProfilesAttributes(); |
| 688 | entries[0u]->SetBackgroundStatus(false); |
| 689 | entries[1u]->SetBackgroundStatus(true); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 690 | |
| 691 | profile_manager->AutoloadProfiles(); |
| 692 | |
| 693 | EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size()); |
| 694 | } |
| 695 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 696 | TEST_F(ProfileManagerTest, InitProfileUserPrefs) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 697 | base::FilePath dest_path = temp_dir_.GetPath(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 698 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 699 | |
| 700 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 701 | |
| 702 | Profile* profile; |
| 703 | |
| 704 | // Successfully create the profile |
| 705 | profile = profile_manager->GetProfile(dest_path); |
| 706 | ASSERT_TRUE(profile); |
| 707 | |
| 708 | // Check that the profile name is non empty |
| 709 | std::string profile_name = |
| 710 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 711 | EXPECT_FALSE(profile_name.empty()); |
| 712 | |
| 713 | // Check that the profile avatar index is valid |
| 714 | size_t avatar_index = |
| 715 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 716 | EXPECT_TRUE(profiles::IsDefaultAvatarIconIndex( |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 717 | avatar_index)); |
| 718 | } |
| 719 | |
| 720 | // Tests that a new profile's entry in the profile info cache is setup with the |
| 721 | // same values that are in the profile prefs. |
| 722 | TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 723 | base::FilePath dest_path = temp_dir_.GetPath(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 724 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 725 | |
| 726 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 727 | |
| 728 | // Successfully create the profile |
| 729 | Profile* profile = profile_manager->GetProfile(dest_path); |
| 730 | ASSERT_TRUE(profile); |
| 731 | |
| 732 | std::string profile_name = |
| 733 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 734 | size_t avatar_index = |
| 735 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
| 736 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 737 | ProfileAttributesEntry* entry; |
| 738 | ASSERT_TRUE(profile_manager->GetProfileAttributesStorage(). |
| 739 | GetProfileAttributesWithPath(dest_path, &entry)); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 740 | |
| 741 | // Check if the profile prefs are the same as the cache prefs |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 742 | EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName())); |
| 743 | EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 744 | } |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 745 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 746 | TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { |
| 747 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 748 | ASSERT_TRUE(profile_manager); |
| 749 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 750 | #if defined(OS_CHROMEOS) |
xiyuan | 41d32a3 | 2017-06-12 20:06:13 | [diff] [blame] | 751 | // On CrOS, profile returned by GetLastUsedProfile is a sign-in profile that |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 752 | // is forced to be incognito. That's why we need to create at least one user |
| 753 | // to get a regular profile. |
| 754 | RegisterUser("[email protected]"); |
| 755 | #endif |
| 756 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 757 | Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 758 | ASSERT_TRUE(profile); |
| 759 | EXPECT_FALSE(profile->IsOffTheRecord()); |
| 760 | PrefService* prefs = profile->GetPrefs(); |
| 761 | EXPECT_EQ(IncognitoModePrefs::ENABLED, |
| 762 | IncognitoModePrefs::GetAvailability(prefs)); |
| 763 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 764 | ASSERT_TRUE(profile->GetOffTheRecordProfile()); |
| 765 | |
| 766 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); |
| 767 | EXPECT_FALSE( |
| 768 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 769 | |
| 770 | // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when |
| 771 | // incognito mode is forced. |
| 772 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); |
| 773 | EXPECT_TRUE( |
| 774 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 775 | } |
| 776 | |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 777 | #if !defined(OS_ANDROID) |
| 778 | // There's no Browser object on Android. |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 779 | TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 780 | base::FilePath dest_path1 = temp_dir_.GetPath(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 781 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 782 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 783 | base::FilePath dest_path2 = temp_dir_.GetPath(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 784 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 785 | |
| 786 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 787 | |
| 788 | // Successfully create the profiles. |
| 789 | TestingProfile* profile1 = |
| 790 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 791 | ASSERT_TRUE(profile1); |
| 792 | |
| 793 | TestingProfile* profile2 = |
| 794 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 795 | ASSERT_TRUE(profile2); |
| 796 | |
| 797 | std::vector<Profile*> last_opened_profiles = |
| 798 | profile_manager->GetLastOpenedProfiles(); |
| 799 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 800 | |
| 801 | // Create a browser for profile1. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 802 | Browser::CreateParams profile1_params(profile1, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 803 | std::unique_ptr<Browser> browser1a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 804 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 805 | |
| 806 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 807 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 808 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 809 | |
| 810 | // And for profile2. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 811 | Browser::CreateParams profile2_params(profile2, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 812 | std::unique_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 813 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 814 | |
| 815 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 816 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 817 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 818 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 819 | |
| 820 | // Adding more browsers doesn't change anything. |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 821 | std::unique_ptr<Browser> browser1b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 822 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 823 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 824 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 825 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 826 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 827 | |
| 828 | // Close the browsers. |
| 829 | browser1a.reset(); |
| 830 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 831 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 832 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 833 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 834 | |
| 835 | browser1b.reset(); |
| 836 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 837 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 838 | EXPECT_EQ(profile2, last_opened_profiles[0]); |
| 839 | |
| 840 | browser2.reset(); |
| 841 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 842 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 843 | } |
| 844 | |
| 845 | TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 846 | base::FilePath dest_path1 = temp_dir_.GetPath(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 847 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 848 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 849 | base::FilePath dest_path2 = temp_dir_.GetPath(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 850 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 851 | |
| 852 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 853 | |
| 854 | // Successfully create the profiles. |
| 855 | TestingProfile* profile1 = |
| 856 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 857 | ASSERT_TRUE(profile1); |
| 858 | |
| 859 | TestingProfile* profile2 = |
| 860 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 861 | ASSERT_TRUE(profile2); |
| 862 | |
| 863 | // Create a browser for profile1. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 864 | Browser::CreateParams profile1_params(profile1, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 865 | std::unique_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 866 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 867 | |
| 868 | // And for profile2. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 869 | Browser::CreateParams profile2_params(profile2, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 870 | std::unique_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 871 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 872 | |
| 873 | std::vector<Profile*> last_opened_profiles = |
| 874 | profile_manager->GetLastOpenedProfiles(); |
| 875 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 876 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 877 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 878 | |
| 879 | // Simulate a shutdown. |
| 880 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 881 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 882 | content::NotificationService::AllSources(), |
| 883 | content::NotificationService::NoDetails()); |
| 884 | |
| 885 | // Even if the browsers are destructed during shutdown, the profiles stay |
| 886 | // open. |
| 887 | browser1.reset(); |
| 888 | browser2.reset(); |
| 889 | |
| 890 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 891 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 892 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 893 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 894 | } |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 895 | |
| 896 | TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 897 | base::FilePath dest_path1 = temp_dir_.GetPath(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 898 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 899 | base::FilePath dest_path2 = temp_dir_.GetPath(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 900 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 901 | |
| 902 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 903 | |
| 904 | // Successfully create the profiles. |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 905 | TestingProfile* profile1 = |
| 906 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 907 | ASSERT_TRUE(profile1); |
| 908 | |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 909 | std::vector<Profile*> last_opened_profiles = |
| 910 | profile_manager->GetLastOpenedProfiles(); |
| 911 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 912 | |
| 913 | // Create a browser for profile1. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 914 | Browser::CreateParams profile1_params(profile1, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 915 | std::unique_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 916 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 917 | |
| 918 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 919 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 920 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 921 | |
| 922 | // And for profile2. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 923 | Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(), |
| 924 | true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 925 | std::unique_ptr<Browser> browser2a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 926 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 927 | |
| 928 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 929 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 930 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 931 | |
| 932 | // Adding more browsers doesn't change anything. |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 933 | std::unique_ptr<Browser> browser2b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 934 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 935 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 936 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 937 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 938 | |
| 939 | // Close the browsers. |
| 940 | browser2a.reset(); |
| 941 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 942 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 943 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 944 | |
| 945 | browser2b.reset(); |
| 946 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 947 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 948 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 949 | |
| 950 | browser1.reset(); |
| 951 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 952 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 953 | } |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 954 | #endif // !defined(OS_ANDROID) |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 955 | |
| 956 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 957 | // 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] | 958 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 959 | base::FilePath dest_path = temp_dir_.GetPath(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 960 | dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile")); |
| 961 | |
| 962 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 963 | |
| 964 | TestingProfile* profile = |
| 965 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); |
| 966 | ASSERT_TRUE(profile); |
palar | 47cf44bd | 2017-03-03 15:36:11 | [diff] [blame] | 967 | SetProfileEphemeral(profile); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 968 | |
| 969 | // Here the last used profile is still the "Default" profile. |
| 970 | Profile* last_used_profile = profile_manager->GetLastUsedProfile(); |
| 971 | EXPECT_NE(profile, last_used_profile); |
| 972 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 973 | // Create a browser for the profile. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 974 | Browser::CreateParams profile_params(profile, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 975 | std::unique_ptr<Browser> browser( |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 976 | chrome::CreateBrowserWithTestWindowForParams(&profile_params)); |
| 977 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 978 | EXPECT_NE(profile, last_used_profile); |
| 979 | |
| 980 | // Close the browser. |
| 981 | browser.reset(); |
| 982 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 983 | EXPECT_NE(profile, last_used_profile); |
| 984 | } |
| 985 | |
| 986 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 987 | base::FilePath dest_path1 = temp_dir_.GetPath(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 988 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile")); |
| 989 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 990 | base::FilePath dest_path2 = temp_dir_.GetPath(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 991 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1")); |
| 992 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 993 | base::FilePath dest_path3 = temp_dir_.GetPath(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 994 | dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2")); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 995 | |
| 996 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 997 | |
| 998 | // Successfully create the profiles. |
| 999 | TestingProfile* normal_profile = |
| 1000 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 1001 | ASSERT_TRUE(normal_profile); |
| 1002 | |
| 1003 | // Add one ephemeral profile which should not end up in this list. |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1004 | TestingProfile* ephemeral_profile1 = |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1005 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1006 | ASSERT_TRUE(ephemeral_profile1); |
palar | 47cf44bd | 2017-03-03 15:36:11 | [diff] [blame] | 1007 | SetProfileEphemeral(ephemeral_profile1); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1008 | |
| 1009 | // Add second ephemeral profile but don't mark it as such yet. |
| 1010 | TestingProfile* ephemeral_profile2 = |
| 1011 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3)); |
| 1012 | ASSERT_TRUE(ephemeral_profile2); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1013 | |
| 1014 | // Create a browser for profile1. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 1015 | Browser::CreateParams profile1_params(normal_profile, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 1016 | std::unique_ptr<Browser> browser1( |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1017 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
| 1018 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1019 | // Create browsers for the ephemeral profile. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 1020 | Browser::CreateParams profile2_params(ephemeral_profile1, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 1021 | std::unique_ptr<Browser> browser2( |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1022 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
| 1023 | |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 1024 | Browser::CreateParams profile3_params(ephemeral_profile2, true); |
dcheng | 6e25ed3 | 2016-04-08 03:30:03 | [diff] [blame] | 1025 | std::unique_ptr<Browser> browser3( |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1026 | chrome::CreateBrowserWithTestWindowForParams(&profile3_params)); |
| 1027 | |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1028 | std::vector<Profile*> last_opened_profiles = |
| 1029 | profile_manager->GetLastOpenedProfiles(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1030 | ASSERT_EQ(2U, last_opened_profiles.size()); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1031 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1032 | EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]); |
| 1033 | |
| 1034 | // Mark the second profile ephemeral. |
palar | 47cf44bd | 2017-03-03 15:36:11 | [diff] [blame] | 1035 | SetProfileEphemeral(ephemeral_profile2); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1036 | |
| 1037 | // Simulate a shutdown. |
| 1038 | content::NotificationService::current()->Notify( |
| 1039 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 1040 | content::NotificationService::AllSources(), |
| 1041 | content::NotificationService::NoDetails()); |
| 1042 | browser1.reset(); |
| 1043 | browser2.reset(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 1044 | browser3.reset(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 1045 | |
| 1046 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 1047 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 1048 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
| 1049 | } |
| 1050 | |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1051 | TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) { |
| 1052 | // Create two profiles, one of them ephemeral. |
| 1053 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1054 | ProfileAttributesStorage& storage = |
| 1055 | profile_manager->GetProfileAttributesStorage(); |
| 1056 | ASSERT_EQ(0u, storage.GetNumberOfProfiles()); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1057 | |
| 1058 | const std::string profile_name1 = "Homer"; |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1059 | base::FilePath path1 = |
| 1060 | profile_manager->user_data_dir().AppendASCII(profile_name1); |
| 1061 | storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(), |
| 1062 | base::UTF8ToUTF16(profile_name1), 0, std::string()); |
| 1063 | storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1064 | ASSERT_TRUE(base::CreateDirectory(path1)); |
| 1065 | |
| 1066 | const std::string profile_name2 = "Marge"; |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1067 | base::FilePath path2 = |
| 1068 | profile_manager->user_data_dir().AppendASCII(profile_name2); |
| 1069 | storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(), |
| 1070 | base::UTF8ToUTF16(profile_name2), 0, std::string()); |
| 1071 | ASSERT_EQ(2u, storage.GetNumberOfProfiles()); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1072 | ASSERT_TRUE(base::CreateDirectory(path2)); |
| 1073 | |
| 1074 | // Set the active profile. |
| 1075 | PrefService* local_state = g_browser_process->local_state(); |
| 1076 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 1077 | |
| 1078 | profile_manager->CleanUpEphemeralProfiles(); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1079 | content::RunAllTasksUntilIdle(); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1080 | |
| 1081 | // The ephemeral profile should be deleted, and the last used profile set to |
| 1082 | // the other one. |
| 1083 | EXPECT_FALSE(base::DirectoryExists(path1)); |
| 1084 | EXPECT_TRUE(base::DirectoryExists(path2)); |
| 1085 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1086 | ASSERT_EQ(1u, storage.GetNumberOfProfiles()); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1087 | |
| 1088 | // Mark the remaining profile ephemeral and clean up. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1089 | storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1090 | profile_manager->CleanUpEphemeralProfiles(); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1091 | content::RunAllTasksUntilIdle(); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1092 | |
| 1093 | // The profile should be deleted, and the last used profile set to a new one. |
| 1094 | EXPECT_FALSE(base::DirectoryExists(path2)); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1095 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
bauerb | 8b02218 | 2015-05-13 09:18:39 | [diff] [blame] | 1096 | EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed)); |
| 1097 | } |
| 1098 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1099 | TEST_F(ProfileManagerTest, ActiveProfileDeleted) { |
| 1100 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1101 | ASSERT_TRUE(profile_manager); |
| 1102 | |
| 1103 | // Create and load two profiles. |
| 1104 | const std::string profile_name1 = "New Profile 1"; |
| 1105 | const std::string profile_name2 = "New Profile 2"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1106 | base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1); |
| 1107 | base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1108 | |
| 1109 | MockObserver mock_observer; |
| 1110 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1111 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
| 1112 | |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 1113 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
| 1114 | CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1115 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1116 | |
| 1117 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1118 | EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage(). |
| 1119 | GetNumberOfProfiles()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1120 | |
| 1121 | // Set the active profile. |
| 1122 | PrefService* local_state = g_browser_process->local_state(); |
| 1123 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 1124 | |
| 1125 | // Delete the active profile. |
| 1126 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1127 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1128 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1129 | |
| 1130 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1131 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 1132 | } |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1133 | |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1134 | TEST_F(ProfileManagerTest, LastProfileDeleted) { |
| 1135 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1136 | ASSERT_TRUE(profile_manager); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1137 | ProfileAttributesStorage& storage = |
| 1138 | profile_manager->GetProfileAttributesStorage(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1139 | |
| 1140 | // Create and load a profile. |
| 1141 | const std::string profile_name1 = "New Profile 1"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1142 | base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1143 | |
| 1144 | MockObserver mock_observer; |
| 1145 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1146 | testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 1147 | |
| 1148 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1149 | content::RunAllTasksUntilIdle(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1150 | |
| 1151 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1152 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1153 | |
| 1154 | // Set it as the active profile. |
| 1155 | PrefService* local_state = g_browser_process->local_state(); |
| 1156 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 1157 | |
| 1158 | // Delete the active profile. |
| 1159 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1160 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1161 | content::RunAllTasksUntilIdle(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1162 | |
| 1163 | // A new profile should have been created |
| 1164 | const std::string profile_name2 = "Profile 1"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1165 | base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1166 | |
| 1167 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1168 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1169 | EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath()); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) { |
| 1173 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1174 | ASSERT_TRUE(profile_manager); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1175 | ProfileAttributesStorage& storage = |
| 1176 | profile_manager->GetProfileAttributesStorage(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1177 | |
| 1178 | // Create and load a profile. |
| 1179 | const std::string profile_name1 = "New Profile 1"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1180 | base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1181 | |
| 1182 | MockObserver mock_observer; |
| 1183 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1184 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 1185 | |
| 1186 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1187 | content::RunAllTasksUntilIdle(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1188 | |
| 1189 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1190 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1191 | |
| 1192 | // Create the profile and register it. |
| 1193 | const std::string guest_profile_name = |
| 1194 | ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII(); |
| 1195 | |
| 1196 | TestingProfile::Builder builder; |
| 1197 | builder.SetGuestSession(); |
| 1198 | builder.SetPath(ProfileManager::GetGuestProfilePath()); |
| 1199 | TestingProfile* guest_profile = builder.Build().release(); |
| 1200 | guest_profile->set_profile_name(guest_profile_name); |
| 1201 | // Registering the profile passes ownership to the ProfileManager. |
| 1202 | profile_manager->RegisterTestingProfile(guest_profile, false, false); |
| 1203 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1204 | // The Guest profile does not get added to the ProfileAttributesStorage. |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1205 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1206 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1207 | |
| 1208 | // Set the Guest profile as the active profile. |
| 1209 | PrefService* local_state = g_browser_process->local_state(); |
| 1210 | local_state->SetString(prefs::kProfileLastUsed, guest_profile_name); |
| 1211 | |
| 1212 | // Delete the other profile. |
| 1213 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1214 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1215 | content::RunAllTasksUntilIdle(); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1216 | |
| 1217 | // A new profile should have been created. |
| 1218 | const std::string profile_name2 = "Profile 1"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1219 | base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1220 | |
| 1221 | EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1222 | EXPECT_EQ(1u, storage.GetNumberOfProfiles()); |
| 1223 | EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath()); |
[email protected] | 602b5a29f | 2014-07-18 15:39:33 | [diff] [blame] | 1224 | } |
| 1225 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1226 | TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) { |
| 1227 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1228 | return; |
| 1229 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1230 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1231 | ProfileAttributesStorage& storage = |
| 1232 | profile_manager->GetProfileAttributesStorage(); |
| 1233 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1234 | |
| 1235 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1236 | const base::string16 default_profile_name = |
| 1237 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1238 | const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); |
| 1239 | Profile* profile1 = AddProfileToStorage(profile_manager, |
| 1240 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1241 | EXPECT_EQ(default_profile_name, |
| 1242 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1243 | |
| 1244 | // Multiple profiles means displaying the actual profile names. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1245 | const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); |
| 1246 | Profile* profile2 = AddProfileToStorage(profile_manager, |
| 1247 | "path_2", profile_name2); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1248 | EXPECT_EQ(profile_name1, |
| 1249 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1250 | EXPECT_EQ(profile_name2, |
| 1251 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1252 | |
| 1253 | // Deleting a profile means returning to the default name. |
| 1254 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1255 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1256 | content::RunAllTasksUntilIdle(); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1257 | EXPECT_EQ(default_profile_name, |
| 1258 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) { |
| 1262 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1263 | return; |
| 1264 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1265 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1266 | ProfileAttributesStorage& storage = |
| 1267 | profile_manager->GetProfileAttributesStorage(); |
| 1268 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1269 | |
| 1270 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1271 | const base::string16 default_profile_name = |
| 1272 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1273 | const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); |
| 1274 | Profile* profile1 = AddProfileToStorage(profile_manager, |
| 1275 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1276 | EXPECT_EQ(default_profile_name, |
| 1277 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1278 | ASSERT_EQ(1u, storage.GetNumberOfProfiles()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1279 | |
| 1280 | // We should display custom names for local profiles. |
| 1281 | const base::string16 custom_profile_name = ASCIIToUTF16("Batman"); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1282 | ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u]; |
| 1283 | entry->SetName(custom_profile_name); |
| 1284 | entry->SetIsUsingDefaultName(false); |
| 1285 | EXPECT_EQ(custom_profile_name, entry->GetName()); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1286 | EXPECT_EQ(custom_profile_name, |
| 1287 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1288 | |
| 1289 | // Multiple profiles means displaying the actual profile names. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1290 | const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); |
| 1291 | Profile* profile2 = AddProfileToStorage(profile_manager, |
| 1292 | "path_2", profile_name2); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1293 | EXPECT_EQ(custom_profile_name, |
| 1294 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1295 | EXPECT_EQ(profile_name2, |
| 1296 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1297 | |
| 1298 | // Deleting a profile means returning to the original, custom name. |
| 1299 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1300 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1301 | content::RunAllTasksUntilIdle(); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1302 | EXPECT_EQ(custom_profile_name, |
| 1303 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) { |
| 1307 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1308 | return; |
| 1309 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1310 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1311 | ProfileAttributesStorage& storage = |
| 1312 | profile_manager->GetProfileAttributesStorage(); |
| 1313 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1314 | |
| 1315 | // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. |
| 1316 | const base::string16 default_profile_name = |
| 1317 | l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1318 | const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); |
| 1319 | Profile* profile1 = AddProfileToStorage(profile_manager, |
| 1320 | "path_1", profile_name1); |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1321 | EXPECT_EQ(default_profile_name, |
| 1322 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1323 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1324 | ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u]; |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1325 | // For a signed in profile with a default name we still display |
| 1326 | // IDS_SINGLE_PROFILE_DISPLAY_NAME. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1327 | entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]")); |
| 1328 | EXPECT_EQ(profile_name1, entry->GetName()); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1329 | EXPECT_EQ(default_profile_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1330 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1331 | |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1332 | // For a signed in profile with a non-default Gaia given name we display the |
| 1333 | // Gaia given name. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1334 | entry->SetAuthInfo("12345", ASCIIToUTF16("[email protected]")); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1335 | const base::string16 gaia_given_name(ASCIIToUTF16("given name")); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1336 | entry->SetGAIAGivenName(gaia_given_name); |
| 1337 | EXPECT_EQ(gaia_given_name, entry->GetName()); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1338 | EXPECT_EQ(gaia_given_name, |
| 1339 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1340 | |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1341 | // Multiple profiles means displaying the actual profile names. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1342 | const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); |
| 1343 | Profile* profile2 = AddProfileToStorage(profile_manager, |
| 1344 | "path_2", profile_name2); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1345 | EXPECT_EQ(gaia_given_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1346 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1347 | EXPECT_EQ(profile_name2, |
| 1348 | profiles::GetAvatarNameForProfile(profile2->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1349 | |
| 1350 | // Deleting a profile means returning to the original, actual profile name. |
| 1351 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 1352 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1353 | content::RunAllTasksUntilIdle(); |
[email protected] | 78186a4 | 2014-08-22 22:07:30 | [diff] [blame] | 1354 | EXPECT_EQ(gaia_given_name, |
[email protected] | 119809d | 2014-06-27 22:12:44 | [diff] [blame] | 1355 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
[email protected] | 3f13053 | 2014-02-26 20:39:24 | [diff] [blame] | 1356 | } |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1357 | |
| 1358 | TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) { |
| 1359 | if (!profiles::IsMultipleProfilesEnabled()) |
| 1360 | return; |
| 1361 | |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1362 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1363 | ProfileAttributesStorage& storage = |
| 1364 | profile_manager->GetProfileAttributesStorage(); |
| 1365 | EXPECT_EQ(0u, storage.GetNumberOfProfiles()); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1366 | |
| 1367 | // Create two signed in profiles, with both new and legacy default names, and |
| 1368 | // a profile with a custom name. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1369 | Profile* profile1 = AddProfileToStorage(profile_manager, "path_1", |
| 1370 | ASCIIToUTF16("Person 1")); |
| 1371 | Profile* profile2 = AddProfileToStorage(profile_manager, "path_2", |
| 1372 | ASCIIToUTF16("Default Profile")); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1373 | const base::string16 profile_name3(ASCIIToUTF16("Batman")); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1374 | Profile* profile3 = AddProfileToStorage(profile_manager, "path_3", |
| 1375 | profile_name3); |
| 1376 | EXPECT_EQ(3u, storage.GetNumberOfProfiles()); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1377 | |
| 1378 | // Sign in all profiles, and make sure they do not have a Gaia name set. |
| 1379 | const base::string16 email1(ASCIIToUTF16("[email protected]")); |
| 1380 | const base::string16 email2(ASCIIToUTF16("[email protected]")); |
| 1381 | const base::string16 email3(ASCIIToUTF16("[email protected]")); |
| 1382 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1383 | ProfileAttributesEntry* entry; |
| 1384 | |
| 1385 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(), |
| 1386 | &entry)); |
| 1387 | entry->SetAuthInfo("12345", email1); |
| 1388 | entry->SetGAIAGivenName(base::string16()); |
| 1389 | entry->SetGAIAName(base::string16()); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1390 | |
| 1391 | // This may resort the cache, so be extra cautious to use the right profile. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1392 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(), |
| 1393 | &entry)); |
| 1394 | entry->SetAuthInfo("23456", email2); |
| 1395 | entry->SetGAIAGivenName(base::string16()); |
| 1396 | entry->SetGAIAName(base::string16()); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1397 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1398 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(), |
| 1399 | &entry)); |
| 1400 | entry->SetAuthInfo("34567", email3); |
| 1401 | entry->SetGAIAGivenName(base::string16()); |
| 1402 | entry->SetGAIAName(base::string16()); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1403 | |
| 1404 | // The profiles with default names should display the email address. |
| 1405 | EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1406 | EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath())); |
| 1407 | |
| 1408 | // The profile with the custom name should display that. |
| 1409 | EXPECT_EQ(profile_name3, |
| 1410 | profiles::GetAvatarNameForProfile(profile3->GetPath())); |
| 1411 | |
| 1412 | // Adding a Gaia name to a profile that previously had a default name should |
| 1413 | // start displaying it. |
| 1414 | const base::string16 gaia_given_name(ASCIIToUTF16("Robin")); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1415 | ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(), |
| 1416 | &entry)); |
| 1417 | entry->SetGAIAGivenName(gaia_given_name); |
noms | c929946 | 2014-09-16 22:55:28 | [diff] [blame] | 1418 | EXPECT_EQ(gaia_given_name, |
| 1419 | profiles::GetAvatarNameForProfile(profile1->GetPath())); |
| 1420 | } |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1421 | #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 1422 | |
| 1423 | #if defined(OS_MACOSX) |
| 1424 | // These tests are for a Mac-only code path that assumes the browser |
| 1425 | // process isn't killed when all browser windows are closed. |
| 1426 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { |
| 1427 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1428 | ASSERT_TRUE(profile_manager); |
| 1429 | |
| 1430 | // Create and load one profile, and just create a second profile. |
| 1431 | const std::string profile_name1 = "New Profile 1"; |
| 1432 | const std::string profile_name2 = "New Profile 2"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1433 | base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1); |
| 1434 | base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1435 | |
| 1436 | MockObserver mock_observer; |
| 1437 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1438 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 1439 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1440 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1441 | |
| 1442 | // Track the profile, but don't load it. |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1443 | ProfileAttributesStorage& storage = |
| 1444 | profile_manager->GetProfileAttributesStorage(); |
| 1445 | storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456", |
| 1446 | base::string16(), 0, std::string()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1447 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1448 | |
| 1449 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1450 | EXPECT_EQ(2u, storage.GetNumberOfProfiles()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1451 | |
| 1452 | // Set the active profile. |
| 1453 | PrefService* local_state = g_browser_process->local_state(); |
| 1454 | local_state->SetString(prefs::kProfileLastUsed, |
| 1455 | dest_path1.BaseName().MaybeAsASCII()); |
| 1456 | |
| 1457 | // Delete the active profile. This should switch and load the unloaded |
| 1458 | // profile. |
| 1459 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1460 | ProfileManager::CreateCallback()); |
| 1461 | |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1462 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1463 | |
| 1464 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1465 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 1466 | } |
| 1467 | |
| 1468 | // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded |
| 1469 | // by simulating a scenario in which the profile that is being loaded as |
| 1470 | // the next active profile has also been marked for deletion, so the |
| 1471 | // ProfileManager needs to recursively select a different next profile. |
| 1472 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { |
| 1473 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1474 | ASSERT_TRUE(profile_manager); |
| 1475 | |
| 1476 | // Create and load one profile, and create two more profiles. |
| 1477 | const std::string profile_name1 = "New Profile 1"; |
| 1478 | const std::string profile_name2 = "New Profile 2"; |
| 1479 | const std::string profile_name3 = "New Profile 3"; |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 1480 | base::FilePath dest_path1 = temp_dir_.GetPath().AppendASCII(profile_name1); |
| 1481 | base::FilePath dest_path2 = temp_dir_.GetPath().AppendASCII(profile_name2); |
| 1482 | base::FilePath dest_path3 = temp_dir_.GetPath().AppendASCII(profile_name3); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1483 | |
| 1484 | MockObserver mock_observer; |
| 1485 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 1486 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 1487 | CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1488 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1489 | |
| 1490 | // Create the other profiles, but don't load them. Assign a fake avatar icon |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1491 | // to ensure that profiles in the profile attributes storage are sorted by the |
| 1492 | // profile name, and not randomly by the avatar name. |
| 1493 | ProfileAttributesStorage& storage = |
| 1494 | profile_manager->GetProfileAttributesStorage(); |
| 1495 | storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456", |
| 1496 | ASCIIToUTF16(profile_name2), 1, std::string()); |
| 1497 | storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567", |
| 1498 | ASCIIToUTF16(profile_name3), 2, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1499 | |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1500 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1501 | |
| 1502 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 1503 | EXPECT_EQ(3u, storage.GetNumberOfProfiles()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1504 | |
| 1505 | // Set the active profile. |
| 1506 | PrefService* local_state = g_browser_process->local_state(); |
| 1507 | local_state->SetString(prefs::kProfileLastUsed, |
| 1508 | dest_path1.BaseName().MaybeAsASCII()); |
| 1509 | |
| 1510 | // Delete the active profile, Profile1. |
| 1511 | // This will post a CreateProfileAsync message, that tries to load Profile2, |
| 1512 | // which checks that the profile is not being deleted, and then calls back |
| 1513 | // FinishDeletingProfile for Profile1. |
| 1514 | // Try to break this flow by setting the active profile to Profile2 in the |
| 1515 | // middle (so after the first posted message), and trying to delete Profile2, |
| 1516 | // so that the ProfileManager has to look for a different profile to load. |
| 1517 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 1518 | ProfileManager::CreateCallback()); |
| 1519 | local_state->SetString(prefs::kProfileLastUsed, |
| 1520 | dest_path2.BaseName().MaybeAsASCII()); |
| 1521 | profile_manager->ScheduleProfileForDeletion(dest_path2, |
| 1522 | ProfileManager::CreateCallback()); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame^] | 1523 | content::RunAllTasksUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 1524 | |
| 1525 | EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1526 | EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
| 1527 | } |
| 1528 | #endif // !defined(OS_MACOSX) |