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