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