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