[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" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 15 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 17 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 19 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 20 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 21 | #include "chrome/browser/io_thread.h" |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 22 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 23 | #include "chrome/browser/prefs/incognito_mode_prefs.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 25 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 26 | #include "chrome/browser/profiles/profile_manager.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] | 7688968a | 2013-02-12 21:45:13 | [diff] [blame] | 32 | #include "chrome/test/base/scoped_testing_local_state.h" |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 33 | #include "chrome/test/base/test_browser_window.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 34 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 35 | #include "chrome/test/base/testing_profile.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 36 | #include "content/public/browser/notification_service.h" |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 37 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 38 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "testing/gtest/include/gtest/gtest.h" |
| 40 | |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 41 | #if defined(OS_CHROMEOS) |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 42 | #include "chrome/browser/chromeos/login/mock_user_manager.h" |
[email protected] | 9a68d3a | 2013-04-22 16:26:54 | [diff] [blame] | 43 | #include "chrome/browser/chromeos/login/user_manager.h" |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 44 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 45 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 46 | #include "chromeos/chromeos_switches.h" |
[email protected] | 775caec | 2011-09-01 17:14:32 | [diff] [blame] | 47 | #endif |
| 48 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame^] | 49 | using base::ASCIIToUTF16; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 50 | using content::BrowserThread; |
| 51 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 52 | namespace { |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 53 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 54 | // This global variable is used to check that value returned to different |
| 55 | // observers is the same. |
| 56 | Profile* g_created_profile; |
| 57 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 58 | class UnittestProfileManager : public ::ProfileManagerWithoutInit { |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 59 | public: |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 60 | explicit UnittestProfileManager(const base::FilePath& user_data_dir) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 61 | : ::ProfileManagerWithoutInit(user_data_dir) {} |
| 62 | |
| 63 | protected: |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 64 | virtual Profile* CreateProfileHelper( |
| 65 | const base::FilePath& file_path) OVERRIDE { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 66 | if (!base::PathExists(file_path)) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 67 | if (!base::CreateDirectory(file_path)) |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | return new TestingProfile(file_path, NULL); |
| 71 | } |
| 72 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 73 | virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 74 | Delegate* delegate) OVERRIDE { |
| 75 | // This is safe while all file operations are done on the FILE thread. |
| 76 | BrowserThread::PostTask( |
| 77 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 78 | base::Bind(base::IgnoreResult(&base::CreateDirectory), path)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 79 | |
| 80 | return new TestingProfile(path, this); |
| 81 | } |
| 82 | }; |
| 83 | |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 84 | } // namespace |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 85 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 86 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 87 | protected: |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 88 | class MockObserver { |
| 89 | public: |
| 90 | MOCK_METHOD2(OnProfileCreated, |
| 91 | void(Profile* profile, Profile::CreateStatus status)); |
| 92 | }; |
| 93 | |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 94 | ProfileManagerTest() |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 95 | : local_state_(TestingBrowserProcess::GetGlobal()) { |
[email protected] | cb0e4f1 | 2009-12-03 00:09:09 | [diff] [blame] | 96 | } |
| 97 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | virtual void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 99 | // Create a new temporary directory, and store the path |
| 100 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 101 | TestingBrowserProcess::GetGlobal()->SetProfileManager( |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 102 | new UnittestProfileManager(temp_dir_.path())); |
[email protected] | f89b50f | 2012-08-16 21:36:50 | [diff] [blame] | 103 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 104 | #if defined(OS_CHROMEOS) |
[email protected] | 19506d54 | 2013-10-15 23:11:06 | [diff] [blame] | 105 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 106 | cl->AppendSwitch(switches::kTestType); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 107 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | } |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame] | 109 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | virtual void TearDown() { |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 111 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 112 | base::RunLoop().RunUntilIdle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 114 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 115 | // Helper function to create a profile with |name| for a profile |manager|. |
| 116 | void CreateProfileAsync(ProfileManager* manager, |
| 117 | const std::string& name, |
| 118 | MockObserver* mock_observer) { |
| 119 | manager->CreateProfileAsync( |
| 120 | temp_dir_.path().AppendASCII(name), |
| 121 | base::Bind(&MockObserver::OnProfileCreated, |
| 122 | base::Unretained(mock_observer)), |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame^] | 123 | base::UTF8ToUTF16(name), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 124 | base::string16(), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 125 | std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 128 | #if defined(OS_CHROMEOS) |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 129 | chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 130 | chromeos::ScopedTestCrosSettings test_cros_settings_; |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 131 | #endif |
| 132 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 133 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 134 | base::ScopedTempDir temp_dir_; |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 135 | ScopedTestingLocalState local_state_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 136 | |
| 137 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 138 | |
| 139 | #if defined(OS_CHROMEOS) |
| 140 | chromeos::ScopedTestUserManager test_user_manager_; |
| 141 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | }; |
| 143 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 144 | TEST_F(ProfileManagerTest, GetProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 145 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 146 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 148 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 149 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | // Successfully create a profile. |
[email protected] | f4e70625 | 2012-01-10 17:11:36 | [diff] [blame] | 151 | Profile* profile = profile_manager->GetProfile(dest_path); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 152 | EXPECT_TRUE(profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 154 | // The profile already exists when we call GetProfile. Just load it. |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 155 | EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 157 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 158 | TEST_F(ProfileManagerTest, DefaultProfileDir) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 159 | base::FilePath expected_default = |
| 160 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 161 | EXPECT_EQ( |
| 162 | expected_default.value(), |
| 163 | g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | #if defined(OS_CHROMEOS) |
| 167 | // This functionality only exists on Chrome OS. |
| 168 | TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 169 | CommandLine *cl = CommandLine::ForCurrentProcess(); |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 170 | std::string profile_dir(chrome::kTestUserProfileDir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 171 | |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 172 | cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 173 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 174 | base::FilePath expected_default = |
| 175 | base::FilePath().AppendASCII(chrome::kInitialProfile); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 176 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 177 | EXPECT_EQ(expected_default.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 178 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 179 | |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 180 | scoped_ptr<chromeos::MockUserManager> mock_user_manager; |
| 181 | mock_user_manager.reset(new chromeos::MockUserManager()); |
| 182 | mock_user_manager->SetActiveUser("[email protected]"); |
| 183 | chromeos::User* active_user = mock_user_manager->GetActiveUser(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 184 | profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 185 | content::NotificationService::AllSources(), |
[email protected] | eb5bb2e | 2013-04-15 20:09:19 | [diff] [blame] | 186 | content::Details<const chromeos::User>(active_user)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 187 | base::FilePath expected_logged_in(profile_dir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 188 | EXPECT_EQ(expected_logged_in.value(), |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 189 | profile_manager->GetInitialProfileDir().value()); |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 190 | VLOG(1) << temp_dir_.path().Append( |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 191 | profile_manager->GetInitialProfileDir()).value(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
[email protected] | 52d69b88 | 2010-06-17 17:35:51 | [diff] [blame] | 196 | TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 197 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 198 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 199 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 200 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 201 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 202 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 203 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 204 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 205 | // Successfully create the profiles. |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 206 | TestingProfile* profile1 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 207 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 208 | ASSERT_TRUE(profile1); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 209 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 210 | TestingProfile* profile2 = |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 211 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 212 | ASSERT_TRUE(profile2); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 213 | |
| 214 | // Force lazy-init of some profile services to simulate use. |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 215 | ASSERT_TRUE(profile1->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 216 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile1, |
| 217 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 218 | profile1->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 219 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile1)); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 220 | profile2->CreateBookmarkModel(true); |
[email protected] | 5114d18 | 2012-07-30 23:09:49 | [diff] [blame] | 221 | EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile2)); |
[email protected] | 608e7e0 | 2013-07-24 12:23:31 | [diff] [blame] | 222 | ASSERT_TRUE(profile2->CreateHistoryService(true, false)); |
[email protected] | 9718a72 | 2012-06-19 20:10:53 | [diff] [blame] | 223 | EXPECT_TRUE(HistoryServiceFactory::GetForProfile(profile2, |
| 224 | Profile::EXPLICIT_ACCESS)); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 225 | |
| 226 | // Make sure any pending tasks run before we destroy the profiles. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 227 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 228 | |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 229 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 230 | |
[email protected] | 237e6d0 | 2010-11-08 21:45:42 | [diff] [blame] | 231 | // Make sure history cleans up correctly. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 232 | base::RunLoop().RunUntilIdle(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 233 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 234 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 235 | MATCHER(NotFail, "Profile creation failure status is not reported.") { |
[email protected] | b8ba8d4 | 2011-12-07 22:13:59 | [diff] [blame] | 236 | return arg == Profile::CREATE_STATUS_CREATED || |
| 237 | arg == Profile::CREATE_STATUS_INITIALIZED; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 240 | // Tests asynchronous profile creation mechanism. |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 241 | // Crashes: http://crbug.com/89421 |
[email protected] | dd6f4b1 | 2011-07-15 15:55:18 | [diff] [blame] | 242 | TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 243 | MockObserver mock_observer; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 244 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 245 | testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 246 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 247 | CreateProfileAsync(g_browser_process->profile_manager(), |
| 248 | "New Profile", &mock_observer); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 249 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 250 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 253 | MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 254 | if (!g_created_profile) |
| 255 | g_created_profile = arg; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 256 | return arg != NULL && arg == g_created_profile; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 260 | g_created_profile = NULL; |
| 261 | |
| 262 | MockObserver mock_observer1; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 263 | EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 264 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 265 | MockObserver mock_observer2; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 266 | EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 267 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 268 | MockObserver mock_observer3; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 269 | EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 270 | SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 271 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 272 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 273 | const std::string profile_name = "New Profile"; |
| 274 | CreateProfileAsync(profile_manager, profile_name, &mock_observer1); |
| 275 | CreateProfileAsync(profile_manager, profile_name, &mock_observer2); |
| 276 | CreateProfileAsync(profile_manager, profile_name, &mock_observer3); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 277 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 278 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 282 | const std::string profile_name1 = "New Profile 1"; |
| 283 | const std::string profile_name2 = "New Profile 2"; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 284 | |
| 285 | MockObserver mock_observer; |
[email protected] | e547ac8 | 2011-07-25 12:34:11 | [diff] [blame] | 286 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 287 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 288 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 289 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 290 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 291 | CreateProfileAsync(profile_manager, profile_name1, &mock_observer); |
| 292 | CreateProfileAsync(profile_manager, profile_name2, &mock_observer); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 293 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 294 | base::RunLoop().RunUntilIdle(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 295 | } |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 296 | |
[email protected] | 4251165a | 2013-07-17 04:33:40 | [diff] [blame] | 297 | TEST_F(ProfileManagerTest, GetGuestProfilePath) { |
| 298 | base::FilePath guest_path = ProfileManager::GetGuestProfilePath(); |
| 299 | base::FilePath expected_path = temp_dir_.path(); |
| 300 | expected_path = expected_path.Append(chrome::kGuestProfileDir); |
| 301 | EXPECT_EQ(expected_path, guest_path); |
| 302 | } |
| 303 | |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 304 | TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 305 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 306 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 307 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
[email protected] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 308 | base::Value::CreateBooleanValue(true)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 309 | |
| 310 | // Setting a pref which is not applicable to a system (i.e., Android in this |
| 311 | // case) does not necessarily create it. Don't bother continuing with the |
| 312 | // test if this pref doesn't exist because it will not load the profiles if |
| 313 | // it cannot verify that the pref for background mode is enabled. |
| 314 | if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled)) |
| 315 | return; |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 316 | |
| 317 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 318 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 319 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 320 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 321 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 322 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 323 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 324 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 325 | ASCIIToUTF16("name_3"), base::string16(), 0, |
| 326 | std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 327 | cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 328 | cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 329 | EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 330 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 331 | profile_manager->AutoloadProfiles(); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 332 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 333 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 334 | } |
| 335 | |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 336 | TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) { |
| 337 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 338 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 339 | local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, |
[email protected] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 340 | base::Value::CreateBooleanValue(false)); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 341 | |
| 342 | EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 343 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 344 | ASCIIToUTF16("name_1"), base::string16(), 0, |
| 345 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 346 | cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 347 | ASCIIToUTF16("name_2"), base::string16(), 0, |
| 348 | std::string()); |
[email protected] | 714bf1d | 2012-11-30 01:35:27 | [diff] [blame] | 349 | cache.SetBackgroundStatusOfProfileAtIndex(0, false); |
| 350 | cache.SetBackgroundStatusOfProfileAtIndex(1, true); |
| 351 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
| 352 | |
| 353 | profile_manager->AutoloadProfiles(); |
| 354 | |
| 355 | EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size()); |
| 356 | } |
| 357 | |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 358 | TEST_F(ProfileManagerTest, InitProfileUserPrefs) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 359 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 360 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 361 | |
| 362 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 363 | |
| 364 | Profile* profile; |
| 365 | |
| 366 | // Successfully create the profile |
| 367 | profile = profile_manager->GetProfile(dest_path); |
| 368 | ASSERT_TRUE(profile); |
| 369 | |
| 370 | // Check that the profile name is non empty |
| 371 | std::string profile_name = |
| 372 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 373 | EXPECT_FALSE(profile_name.empty()); |
| 374 | |
| 375 | // Check that the profile avatar index is valid |
| 376 | size_t avatar_index = |
| 377 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
| 378 | EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex( |
| 379 | avatar_index)); |
| 380 | } |
| 381 | |
| 382 | // Tests that a new profile's entry in the profile info cache is setup with the |
| 383 | // same values that are in the profile prefs. |
| 384 | TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 385 | base::FilePath dest_path = temp_dir_.path(); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 386 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 387 | |
| 388 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 389 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 390 | |
| 391 | // Successfully create the profile |
| 392 | Profile* profile = profile_manager->GetProfile(dest_path); |
| 393 | ASSERT_TRUE(profile); |
| 394 | |
| 395 | std::string profile_name = |
| 396 | profile->GetPrefs()->GetString(prefs::kProfileName); |
| 397 | size_t avatar_index = |
| 398 | profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); |
| 399 | |
| 400 | size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path); |
| 401 | |
| 402 | // Check if the profile prefs are the same as the cache prefs |
| 403 | EXPECT_EQ(profile_name, |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame^] | 404 | base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); |
[email protected] | 881cb0b6 | 2011-12-13 20:39:54 | [diff] [blame] | 405 | EXPECT_EQ(avatar_index, |
| 406 | cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 407 | } |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 408 | |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 409 | TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { |
| 410 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 411 | ASSERT_TRUE(profile_manager); |
| 412 | |
| 413 | Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 414 | ASSERT_TRUE(profile); |
| 415 | EXPECT_FALSE(profile->IsOffTheRecord()); |
| 416 | PrefService* prefs = profile->GetPrefs(); |
| 417 | EXPECT_EQ(IncognitoModePrefs::ENABLED, |
| 418 | IncognitoModePrefs::GetAvailability(prefs)); |
| 419 | |
| 420 | // Attach an incognito Profile to the TestingProfile. |
| 421 | ASSERT_FALSE(profile->GetOffTheRecordProfile()); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 422 | TestingProfile::Builder builder; |
| 423 | builder.SetIncognito(); |
| 424 | scoped_ptr<TestingProfile> incognito_profile = builder.Build(); |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 425 | EXPECT_TRUE(incognito_profile->IsOffTheRecord()); |
| 426 | TestingProfile* testing_profile = static_cast<TestingProfile*>(profile); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 427 | testing_profile->SetOffTheRecordProfile(incognito_profile.PassAs<Profile>()); |
[email protected] | 1372053 | 2013-05-30 12:30:59 | [diff] [blame] | 428 | ASSERT_TRUE(profile->GetOffTheRecordProfile()); |
| 429 | |
| 430 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); |
| 431 | EXPECT_FALSE( |
| 432 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 433 | |
| 434 | // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when |
| 435 | // incognito mode is forced. |
| 436 | IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); |
| 437 | EXPECT_TRUE( |
| 438 | profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
| 439 | } |
| 440 | |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 441 | #if !defined(OS_ANDROID) |
| 442 | // There's no Browser object on Android. |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 443 | TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 444 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 445 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 446 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 447 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 448 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 449 | |
| 450 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 451 | |
| 452 | // Successfully create the profiles. |
| 453 | TestingProfile* profile1 = |
| 454 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 455 | ASSERT_TRUE(profile1); |
| 456 | |
| 457 | TestingProfile* profile2 = |
| 458 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 459 | ASSERT_TRUE(profile2); |
| 460 | |
| 461 | std::vector<Profile*> last_opened_profiles = |
| 462 | profile_manager->GetLastOpenedProfiles(); |
| 463 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 464 | |
| 465 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 466 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 467 | scoped_ptr<Browser> browser1a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 468 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 469 | |
| 470 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 471 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 472 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 473 | |
| 474 | // And for profile2. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 475 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 476 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 477 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 478 | |
| 479 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 480 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 481 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 482 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 483 | |
| 484 | // Adding more browsers doesn't change anything. |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 485 | scoped_ptr<Browser> browser1b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 486 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 487 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 488 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 489 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 490 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 491 | |
| 492 | // Close the browsers. |
| 493 | browser1a.reset(); |
| 494 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 495 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 496 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 497 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 498 | |
| 499 | browser1b.reset(); |
| 500 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 501 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 502 | EXPECT_EQ(profile2, last_opened_profiles[0]); |
| 503 | |
| 504 | browser2.reset(); |
| 505 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 506 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 507 | } |
| 508 | |
| 509 | TEST_F(ProfileManagerTest, LastOpenedProfilesAtShutdown) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 510 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 511 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 512 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 513 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 514 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 515 | |
| 516 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 517 | |
| 518 | // Successfully create the profiles. |
| 519 | TestingProfile* profile1 = |
| 520 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 521 | ASSERT_TRUE(profile1); |
| 522 | |
| 523 | TestingProfile* profile2 = |
| 524 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
| 525 | ASSERT_TRUE(profile2); |
| 526 | |
| 527 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 528 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 529 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 530 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 531 | |
| 532 | // And for profile2. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 533 | Browser::CreateParams profile2_params(profile2, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 534 | scoped_ptr<Browser> browser2( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 535 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 536 | |
| 537 | std::vector<Profile*> last_opened_profiles = |
| 538 | profile_manager->GetLastOpenedProfiles(); |
| 539 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 540 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 541 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 542 | |
| 543 | // Simulate a shutdown. |
| 544 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 545 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | 52edf81 | 2012-01-12 11:48:46 | [diff] [blame] | 546 | content::NotificationService::AllSources(), |
| 547 | content::NotificationService::NoDetails()); |
| 548 | |
| 549 | // Even if the browsers are destructed during shutdown, the profiles stay |
| 550 | // open. |
| 551 | browser1.reset(); |
| 552 | browser2.reset(); |
| 553 | |
| 554 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 555 | ASSERT_EQ(2U, last_opened_profiles.size()); |
| 556 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 557 | EXPECT_EQ(profile2, last_opened_profiles[1]); |
| 558 | } |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 559 | |
| 560 | TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 561 | base::FilePath dest_path1 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 562 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 563 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 564 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 565 | |
| 566 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 567 | |
| 568 | // Successfully create the profiles. |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 569 | TestingProfile* profile1 = |
| 570 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 571 | ASSERT_TRUE(profile1); |
| 572 | |
[email protected] | 4dffabe | 2012-05-19 14:37:06 | [diff] [blame] | 573 | // incognito profiles should not be managed by the profile manager but by the |
| 574 | // original profile. |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 575 | TestingProfile::Builder builder; |
| 576 | builder.SetIncognito(); |
| 577 | scoped_ptr<TestingProfile> profile2 = builder.Build(); |
| 578 | profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>()); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 579 | |
| 580 | std::vector<Profile*> last_opened_profiles = |
| 581 | profile_manager->GetLastOpenedProfiles(); |
| 582 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 583 | |
| 584 | // Create a browser for profile1. |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 585 | Browser::CreateParams profile1_params(profile1, chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 586 | scoped_ptr<Browser> browser1( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 587 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 588 | |
| 589 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 590 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 591 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 592 | |
| 593 | // And for profile2. |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 594 | Browser::CreateParams profile2_params(profile1->GetOffTheRecordProfile(), |
| 595 | chrome::GetActiveDesktop()); |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 596 | scoped_ptr<Browser> browser2a( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 597 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 598 | |
| 599 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 600 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 601 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 602 | |
| 603 | // Adding more browsers doesn't change anything. |
[email protected] | a4fe6701 | 2012-07-25 20:14:29 | [diff] [blame] | 604 | scoped_ptr<Browser> browser2b( |
[email protected] | c5f3e184 | 2013-02-20 17:10:41 | [diff] [blame] | 605 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
[email protected] | 4127634 | 2012-02-06 10:49:31 | [diff] [blame] | 606 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 607 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 608 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 609 | |
| 610 | // Close the browsers. |
| 611 | browser2a.reset(); |
| 612 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 613 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 614 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 615 | |
| 616 | browser2b.reset(); |
| 617 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 618 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 619 | EXPECT_EQ(profile1, last_opened_profiles[0]); |
| 620 | |
| 621 | browser1.reset(); |
| 622 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 623 | ASSERT_EQ(0U, last_opened_profiles.size()); |
| 624 | } |
[email protected] | c849fab | 2012-03-29 16:51:41 | [diff] [blame] | 625 | #endif // !defined(OS_ANDROID) |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 626 | |
| 627 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 628 | // 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] | 629 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastProfile) { |
| 630 | base::FilePath dest_path = temp_dir_.path(); |
| 631 | dest_path = dest_path.Append(FILE_PATH_LITERAL("Ephemeral Profile")); |
| 632 | |
| 633 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 634 | |
| 635 | TestingProfile* profile = |
| 636 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path)); |
| 637 | ASSERT_TRUE(profile); |
| 638 | profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); |
| 639 | |
| 640 | // Here the last used profile is still the "Default" profile. |
| 641 | Profile* last_used_profile = profile_manager->GetLastUsedProfile(); |
| 642 | EXPECT_NE(profile, last_used_profile); |
| 643 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 644 | // Create a browser for the profile. |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 645 | Browser::CreateParams profile_params(profile, chrome::GetActiveDesktop()); |
| 646 | scoped_ptr<Browser> browser( |
| 647 | chrome::CreateBrowserWithTestWindowForParams(&profile_params)); |
| 648 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 649 | EXPECT_NE(profile, last_used_profile); |
| 650 | |
| 651 | // Close the browser. |
| 652 | browser.reset(); |
| 653 | last_used_profile = profile_manager->GetLastUsedProfile(); |
| 654 | EXPECT_NE(profile, last_used_profile); |
| 655 | } |
| 656 | |
| 657 | TEST_F(ProfileManagerTest, EphemeralProfilesDontEndUpAsLastOpenedAtShutdown) { |
| 658 | base::FilePath dest_path1 = temp_dir_.path(); |
| 659 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("Normal Profile")); |
| 660 | |
| 661 | base::FilePath dest_path2 = temp_dir_.path(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 662 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("Ephemeral Profile 1")); |
| 663 | |
| 664 | base::FilePath dest_path3 = temp_dir_.path(); |
| 665 | dest_path3 = dest_path3.Append(FILE_PATH_LITERAL("Ephemeral Profile 2")); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 666 | |
| 667 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 668 | |
| 669 | // Successfully create the profiles. |
| 670 | TestingProfile* normal_profile = |
| 671 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1)); |
| 672 | ASSERT_TRUE(normal_profile); |
| 673 | |
| 674 | // Add one ephemeral profile which should not end up in this list. |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 675 | TestingProfile* ephemeral_profile1 = |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 676 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path2)); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 677 | ASSERT_TRUE(ephemeral_profile1); |
| 678 | ephemeral_profile1->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, |
| 679 | true); |
| 680 | |
| 681 | // Add second ephemeral profile but don't mark it as such yet. |
| 682 | TestingProfile* ephemeral_profile2 = |
| 683 | static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path3)); |
| 684 | ASSERT_TRUE(ephemeral_profile2); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 685 | |
| 686 | // Create a browser for profile1. |
| 687 | Browser::CreateParams profile1_params(normal_profile, |
| 688 | chrome::GetActiveDesktop()); |
| 689 | scoped_ptr<Browser> browser1( |
| 690 | chrome::CreateBrowserWithTestWindowForParams(&profile1_params)); |
| 691 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 692 | // Create browsers for the ephemeral profile. |
| 693 | Browser::CreateParams profile2_params(ephemeral_profile1, |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 694 | chrome::GetActiveDesktop()); |
| 695 | scoped_ptr<Browser> browser2( |
| 696 | chrome::CreateBrowserWithTestWindowForParams(&profile2_params)); |
| 697 | |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 698 | Browser::CreateParams profile3_params(ephemeral_profile2, |
| 699 | chrome::GetActiveDesktop()); |
| 700 | scoped_ptr<Browser> browser3( |
| 701 | chrome::CreateBrowserWithTestWindowForParams(&profile3_params)); |
| 702 | |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 703 | std::vector<Profile*> last_opened_profiles = |
| 704 | profile_manager->GetLastOpenedProfiles(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 705 | ASSERT_EQ(2U, last_opened_profiles.size()); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 706 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 707 | EXPECT_EQ(ephemeral_profile2, last_opened_profiles[1]); |
| 708 | |
| 709 | // Mark the second profile ephemeral. |
| 710 | ephemeral_profile2->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, |
| 711 | true); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 712 | |
| 713 | // Simulate a shutdown. |
| 714 | content::NotificationService::current()->Notify( |
| 715 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 716 | content::NotificationService::AllSources(), |
| 717 | content::NotificationService::NoDetails()); |
| 718 | browser1.reset(); |
| 719 | browser2.reset(); |
[email protected] | 79fd6b7 | 2013-10-22 16:39:27 | [diff] [blame] | 720 | browser3.reset(); |
[email protected] | 8d9243a | 2013-10-21 17:11:48 | [diff] [blame] | 721 | |
| 722 | last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
| 723 | ASSERT_EQ(1U, last_opened_profiles.size()); |
| 724 | EXPECT_EQ(normal_profile, last_opened_profiles[0]); |
| 725 | } |
| 726 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 727 | TEST_F(ProfileManagerTest, ActiveProfileDeleted) { |
| 728 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 729 | ASSERT_TRUE(profile_manager); |
| 730 | |
| 731 | // Create and load two profiles. |
| 732 | const std::string profile_name1 = "New Profile 1"; |
| 733 | const std::string profile_name2 = "New Profile 2"; |
| 734 | base::FilePath dest_path1 = |
| 735 | temp_dir_.path().AppendASCII(profile_name1); |
| 736 | base::FilePath dest_path2 = |
| 737 | temp_dir_.path().AppendASCII(profile_name2); |
| 738 | |
| 739 | MockObserver mock_observer; |
| 740 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 741 | testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
| 742 | |
| 743 | CreateProfileAsync(profile_manager, profile_name1, &mock_observer); |
| 744 | CreateProfileAsync(profile_manager, profile_name2, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 745 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 746 | |
| 747 | EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 748 | EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles()); |
| 749 | |
| 750 | // Set the active profile. |
| 751 | PrefService* local_state = g_browser_process->local_state(); |
| 752 | local_state->SetString(prefs::kProfileLastUsed, profile_name1); |
| 753 | |
| 754 | // Delete the active profile. |
| 755 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 756 | ProfileManager::CreateCallback()); |
| 757 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 758 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 759 | |
| 760 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 761 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 762 | } |
| 763 | #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 764 | |
| 765 | #if defined(OS_MACOSX) |
| 766 | // These tests are for a Mac-only code path that assumes the browser |
| 767 | // process isn't killed when all browser windows are closed. |
| 768 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { |
| 769 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 770 | ASSERT_TRUE(profile_manager); |
| 771 | |
| 772 | // Create and load one profile, and just create a second profile. |
| 773 | const std::string profile_name1 = "New Profile 1"; |
| 774 | const std::string profile_name2 = "New Profile 2"; |
| 775 | base::FilePath dest_path1 = |
| 776 | temp_dir_.path().AppendASCII(profile_name1); |
| 777 | base::FilePath dest_path2 = |
| 778 | temp_dir_.path().AppendASCII(profile_name2); |
| 779 | |
| 780 | MockObserver mock_observer; |
| 781 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 782 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 783 | CreateProfileAsync(profile_manager, profile_name1, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 784 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 785 | |
| 786 | // Track the profile, but don't load it. |
| 787 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 788 | cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 789 | base::string16(), 0, std::string()); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 790 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 791 | |
| 792 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 793 | EXPECT_EQ(2u, cache.GetNumberOfProfiles()); |
| 794 | |
| 795 | // Set the active profile. |
| 796 | PrefService* local_state = g_browser_process->local_state(); |
| 797 | local_state->SetString(prefs::kProfileLastUsed, |
| 798 | dest_path1.BaseName().MaybeAsASCII()); |
| 799 | |
| 800 | // Delete the active profile. This should switch and load the unloaded |
| 801 | // profile. |
| 802 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 803 | ProfileManager::CreateCallback()); |
| 804 | |
| 805 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 806 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 807 | |
| 808 | EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); |
| 809 | EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); |
| 810 | } |
| 811 | |
| 812 | // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded |
| 813 | // by simulating a scenario in which the profile that is being loaded as |
| 814 | // the next active profile has also been marked for deletion, so the |
| 815 | // ProfileManager needs to recursively select a different next profile. |
| 816 | TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { |
| 817 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 818 | ASSERT_TRUE(profile_manager); |
| 819 | |
| 820 | // Create and load one profile, and create two more profiles. |
| 821 | const std::string profile_name1 = "New Profile 1"; |
| 822 | const std::string profile_name2 = "New Profile 2"; |
| 823 | const std::string profile_name3 = "New Profile 3"; |
| 824 | base::FilePath dest_path1 = |
| 825 | temp_dir_.path().AppendASCII(profile_name1); |
| 826 | base::FilePath dest_path2 = |
| 827 | temp_dir_.path().AppendASCII(profile_name2); |
| 828 | base::FilePath dest_path3 = |
| 829 | temp_dir_.path().AppendASCII(profile_name3); |
| 830 | |
| 831 | MockObserver mock_observer; |
| 832 | EXPECT_CALL(mock_observer, OnProfileCreated( |
| 833 | testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); |
| 834 | CreateProfileAsync(profile_manager, profile_name1, &mock_observer); |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 835 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 836 | |
| 837 | // Create the other profiles, but don't load them. Assign a fake avatar icon |
| 838 | // to ensure that profiles in the info cache are sorted by the profile name, |
| 839 | // and not randomly by the avatar name. |
| 840 | ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 841 | cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 842 | ASCIIToUTF16(profile_name2), 1, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 843 | cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3), |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 844 | ASCIIToUTF16(profile_name3), 2, std::string()); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 845 | |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 846 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 847 | |
| 848 | EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); |
| 849 | EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 850 | |
| 851 | // Set the active profile. |
| 852 | PrefService* local_state = g_browser_process->local_state(); |
| 853 | local_state->SetString(prefs::kProfileLastUsed, |
| 854 | dest_path1.BaseName().MaybeAsASCII()); |
| 855 | |
| 856 | // Delete the active profile, Profile1. |
| 857 | // This will post a CreateProfileAsync message, that tries to load Profile2, |
| 858 | // which checks that the profile is not being deleted, and then calls back |
| 859 | // FinishDeletingProfile for Profile1. |
| 860 | // Try to break this flow by setting the active profile to Profile2 in the |
| 861 | // middle (so after the first posted message), and trying to delete Profile2, |
| 862 | // so that the ProfileManager has to look for a different profile to load. |
| 863 | profile_manager->ScheduleProfileForDeletion(dest_path1, |
| 864 | ProfileManager::CreateCallback()); |
| 865 | local_state->SetString(prefs::kProfileLastUsed, |
| 866 | dest_path2.BaseName().MaybeAsASCII()); |
| 867 | profile_manager->ScheduleProfileForDeletion(dest_path2, |
| 868 | ProfileManager::CreateCallback()); |
| 869 | // Spin the message loop so that all the callbacks can finish running. |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 870 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 871 | |
| 872 | EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
| 873 | EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
| 874 | } |
| 875 | #endif // !defined(OS_MACOSX) |