| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #include "chrome/test/base/testing_profile.h" | 38 #include "chrome/test/base/testing_profile.h" |
| 39 #include "components/signin/core/common/profile_management_switches.h" | 39 #include "components/signin/core/common/profile_management_switches.h" |
| 40 #include "content/public/browser/notification_service.h" | 40 #include "content/public/browser/notification_service.h" |
| 41 #include "content/public/common/content_switches.h" | 41 #include "content/public/common/content_switches.h" |
| 42 #include "content/public/test/test_browser_thread_bundle.h" | 42 #include "content/public/test/test_browser_thread_bundle.h" |
| 43 #include "testing/gmock/include/gmock/gmock.h" | 43 #include "testing/gmock/include/gmock/gmock.h" |
| 44 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
| 45 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
| 46 | 46 |
| 47 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
| 48 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" | 48 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
| 49 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | 49 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
| 50 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 51 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 50 #include "chrome/browser/chromeos/settings/cros_settings.h" | 52 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 51 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 53 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 52 #include "chromeos/chromeos_switches.h" | 54 #include "chromeos/chromeos_switches.h" |
| 53 #include "chromeos/login/user_names.h" | 55 #include "chromeos/login/user_names.h" |
| 54 #include "components/user_manager/user_manager.h" | 56 #include "components/user_manager/user_manager.h" |
| 55 #endif | 57 #endif // defined(OS_CHROMEOS) |
| 56 | 58 |
| 57 using base::ASCIIToUTF16; | 59 using base::ASCIIToUTF16; |
| 58 using content::BrowserThread; | 60 using content::BrowserThread; |
| 59 | 61 |
| 60 namespace { | 62 namespace { |
| 61 | 63 |
| 62 // This global variable is used to check that value returned to different | 64 // This global variable is used to check that value returned to different |
| 63 // observers is the same. | 65 // observers is the same. |
| 64 Profile* g_created_profile; | 66 Profile* g_created_profile; |
| 65 | 67 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 : local_state_(TestingBrowserProcess::GetGlobal()) { | 105 : local_state_(TestingBrowserProcess::GetGlobal()) { |
| 104 } | 106 } |
| 105 | 107 |
| 106 virtual void SetUp() { | 108 virtual void SetUp() { |
| 107 // Create a new temporary directory, and store the path | 109 // Create a new temporary directory, and store the path |
| 108 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 110 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 109 TestingBrowserProcess::GetGlobal()->SetProfileManager( | 111 TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 110 new UnittestProfileManager(temp_dir_.path())); | 112 new UnittestProfileManager(temp_dir_.path())); |
| 111 | 113 |
| 112 #if defined(OS_CHROMEOS) | 114 #if defined(OS_CHROMEOS) |
| 113 CommandLine* cl = CommandLine::ForCurrentProcess(); | 115 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 114 cl->AppendSwitch(switches::kTestType); | 116 cl->AppendSwitch(switches::kTestType); |
| 115 #endif | 117 #endif |
| 116 } | 118 } |
| 117 | 119 |
| 118 virtual void TearDown() { | 120 virtual void TearDown() { |
| 119 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); | 121 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
| 120 base::RunLoop().RunUntilIdle(); | 122 base::RunLoop().RunUntilIdle(); |
| 121 } | 123 } |
| 122 | 124 |
| 123 // Helper function to create a profile with |name| for a profile |manager|. | 125 // Helper function to create a profile with |name| for a profile |manager|. |
| 124 void CreateProfileAsync(ProfileManager* manager, | 126 void CreateProfileAsync(ProfileManager* manager, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 142 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | 144 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 143 size_t num_profiles = cache.GetNumberOfProfiles(); | 145 size_t num_profiles = cache.GetNumberOfProfiles(); |
| 144 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); | 146 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); |
| 145 cache.AddProfileToCache(path, profile_name, | 147 cache.AddProfileToCache(path, profile_name, |
| 146 base::string16(), 0, std::string()); | 148 base::string16(), 0, std::string()); |
| 147 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles()); | 149 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles()); |
| 148 return profile_manager->GetProfile(path); | 150 return profile_manager->GetProfile(path); |
| 149 } | 151 } |
| 150 | 152 |
| 151 #if defined(OS_CHROMEOS) | 153 #if defined(OS_CHROMEOS) |
| 154 // Helper function to register an user with id |user_id| and create profile |
| 155 // with a correct path. |
| 156 void RegisterUser(const std::string& user_id) { |
| 157 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get(); |
| 158 const std::string user_id_hash = |
| 159 profile_helper->GetUserIdHashByUserIdForTesting(user_id); |
| 160 user_manager::UserManager::Get()->UserLoggedIn( |
| 161 user_id, user_id_hash, false); |
| 162 g_browser_process->profile_manager()->GetProfile( |
| 163 profile_helper->GetProfilePathByUserIdHash(user_id_hash)); |
| 164 } |
| 165 |
| 152 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 166 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 153 chromeos::ScopedTestCrosSettings test_cros_settings_; | 167 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 154 #endif | 168 #endif |
| 155 | 169 |
| 156 // The path to temporary directory used to contain the test operations. | 170 // The path to temporary directory used to contain the test operations. |
| 157 base::ScopedTempDir temp_dir_; | 171 base::ScopedTempDir temp_dir_; |
| 158 ScopedTestingLocalState local_state_; | 172 ScopedTestingLocalState local_state_; |
| 159 | 173 |
| 160 content::TestBrowserThreadBundle thread_bundle_; | 174 content::TestBrowserThreadBundle thread_bundle_; |
| 161 | 175 |
| 162 #if defined(OS_CHROMEOS) | 176 #if defined(OS_CHROMEOS) |
| 163 chromeos::ScopedTestUserManager test_user_manager_; | 177 chromeos::ScopedTestUserManager test_user_manager_; |
| 164 #endif | 178 #endif |
| 179 |
| 180 DISALLOW_COPY_AND_ASSIGN(ProfileManagerTest); |
| 165 }; | 181 }; |
| 166 | 182 |
| 167 TEST_F(ProfileManagerTest, GetProfile) { | 183 TEST_F(ProfileManagerTest, GetProfile) { |
| 168 base::FilePath dest_path = temp_dir_.path(); | 184 base::FilePath dest_path = temp_dir_.path(); |
| 169 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | 185 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 170 | 186 |
| 171 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 187 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 172 | 188 |
| 173 // Successfully create a profile. | 189 // Successfully create a profile. |
| 174 Profile* profile = profile_manager->GetProfile(dest_path); | 190 Profile* profile = profile_manager->GetProfile(dest_path); |
| 175 EXPECT_TRUE(profile); | 191 EXPECT_TRUE(profile); |
| 176 | 192 |
| 177 // The profile already exists when we call GetProfile. Just load it. | 193 // The profile already exists when we call GetProfile. Just load it. |
| 178 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); | 194 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
| 179 } | 195 } |
| 180 | 196 |
| 181 TEST_F(ProfileManagerTest, DefaultProfileDir) { | 197 TEST_F(ProfileManagerTest, DefaultProfileDir) { |
| 182 base::FilePath expected_default = | 198 base::FilePath expected_default = |
| 183 base::FilePath().AppendASCII(chrome::kInitialProfile); | 199 base::FilePath().AppendASCII(chrome::kInitialProfile); |
| 184 EXPECT_EQ( | 200 EXPECT_EQ( |
| 185 expected_default.value(), | 201 expected_default.value(), |
| 186 g_browser_process->profile_manager()->GetInitialProfileDir().value()); | 202 g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
| 187 } | 203 } |
| 188 | 204 |
| 189 #if defined(OS_CHROMEOS) | 205 #if defined(OS_CHROMEOS) |
| 206 |
| 190 // This functionality only exists on Chrome OS. | 207 // This functionality only exists on Chrome OS. |
| 191 TEST_F(ProfileManagerTest, LoggedInProfileDir) { | 208 TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 192 CommandLine *cl = CommandLine::ForCurrentProcess(); | |
| 193 std::string profile_dir(chrome::kTestUserProfileDir); | |
| 194 | |
| 195 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir); | |
| 196 | |
| 197 base::FilePath expected_default = | 209 base::FilePath expected_default = |
| 198 base::FilePath().AppendASCII(chrome::kInitialProfile); | 210 base::FilePath().AppendASCII(chrome::kInitialProfile); |
| 199 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 211 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 200 EXPECT_EQ(expected_default.value(), | 212 EXPECT_EQ(expected_default.value(), |
| 201 profile_manager->GetInitialProfileDir().value()); | 213 profile_manager->GetInitialProfileDir().value()); |
| 202 | 214 |
| 203 scoped_ptr<chromeos::MockUserManager> mock_user_manager; | 215 const char kTestUserName[] = "test-user@example.com"; |
| 204 mock_user_manager.reset(new chromeos::MockUserManager()); | 216 chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager(); |
| 205 mock_user_manager->SetActiveUser("user@gmail.com"); | 217 chromeos::ScopedUserManagerEnabler enabler(user_manager); |
| 206 user_manager::User* active_user = mock_user_manager->GetActiveUser(); | 218 |
| 219 const user_manager::User* active_user = user_manager->AddUser(kTestUserName); |
| 220 user_manager->LoginUser(kTestUserName); |
| 221 user_manager->SwitchActiveUser(kTestUserName); |
| 222 |
| 207 profile_manager->Observe( | 223 profile_manager->Observe( |
| 208 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 224 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 209 content::NotificationService::AllSources(), | 225 content::NotificationService::AllSources(), |
| 210 content::Details<const user_manager::User>(active_user)); | 226 content::Details<const user_manager::User>(active_user)); |
| 211 base::FilePath expected_logged_in(profile_dir); | 227 base::FilePath expected_logged_in( |
| 228 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash())); |
| 212 EXPECT_EQ(expected_logged_in.value(), | 229 EXPECT_EQ(expected_logged_in.value(), |
| 213 profile_manager->GetInitialProfileDir().value()); | 230 profile_manager->GetInitialProfileDir().value()); |
| 214 VLOG(1) << temp_dir_.path().Append( | 231 VLOG(1) << temp_dir_.path().Append( |
| 215 profile_manager->GetInitialProfileDir()).value(); | 232 profile_manager->GetInitialProfileDir()).value(); |
| 216 } | 233 } |
| 217 | 234 |
| 218 #endif | 235 #endif |
| 219 | 236 |
| 220 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 237 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
| 221 base::FilePath dest_path1 = temp_dir_.path(); | 238 base::FilePath dest_path1 = temp_dir_.path(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 409 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 393 TestingBrowserProcess::GetGlobal()->SetProfileManager( | 410 TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 394 new UnittestGuestProfileManager(temp_dir_.path())); | 411 new UnittestGuestProfileManager(temp_dir_.path())); |
| 395 | 412 |
| 396 #if defined(OS_CHROMEOS) | 413 #if defined(OS_CHROMEOS) |
| 397 CommandLine* cl = CommandLine::ForCurrentProcess(); | 414 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 398 // This switch is needed to skip non-test specific behavior in | 415 // This switch is needed to skip non-test specific behavior in |
| 399 // ProfileManager (accessing DBusThreadManager). | 416 // ProfileManager (accessing DBusThreadManager). |
| 400 cl->AppendSwitch(switches::kTestType); | 417 cl->AppendSwitch(switches::kTestType); |
| 401 | 418 |
| 402 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, | |
| 403 std::string(chrome::kProfileDirPrefix) + | |
| 404 chromeos::login::kGuestUserName); | |
| 405 cl->AppendSwitch(chromeos::switches::kGuestSession); | 419 cl->AppendSwitch(chromeos::switches::kGuestSession); |
| 406 cl->AppendSwitch(::switches::kIncognito); | 420 cl->AppendSwitch(::switches::kIncognito); |
| 407 | 421 |
| 408 user_manager::UserManager::Get()->UserLoggedIn( | 422 RegisterUser(chromeos::login::kGuestUserName); |
| 409 chromeos::login::kGuestUserName, | |
| 410 chromeos::login::kGuestUserName, | |
| 411 false); | |
| 412 #endif | 423 #endif |
| 413 } | 424 } |
| 414 }; | 425 }; |
| 415 | 426 |
| 416 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { | 427 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { |
| 417 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 428 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 418 ASSERT_TRUE(profile_manager); | 429 ASSERT_TRUE(profile_manager); |
| 419 | 430 |
| 420 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); | 431 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 421 ASSERT_TRUE(profile); | 432 ASSERT_TRUE(profile); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 EXPECT_EQ(profile_name, | 552 EXPECT_EQ(profile_name, |
| 542 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); | 553 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); |
| 543 EXPECT_EQ(avatar_index, | 554 EXPECT_EQ(avatar_index, |
| 544 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); | 555 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); |
| 545 } | 556 } |
| 546 | 557 |
| 547 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { | 558 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { |
| 548 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 559 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 549 ASSERT_TRUE(profile_manager); | 560 ASSERT_TRUE(profile_manager); |
| 550 | 561 |
| 562 #if defined(OS_CHROMEOS) |
| 563 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that |
| 564 // is forced to be incognito. That's why we need to create at least one user |
| 565 // to get a regular profile. |
| 566 RegisterUser("test-user@example.com"); |
| 567 #endif |
| 568 |
| 551 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); | 569 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 552 ASSERT_TRUE(profile); | 570 ASSERT_TRUE(profile); |
| 553 EXPECT_FALSE(profile->IsOffTheRecord()); | 571 EXPECT_FALSE(profile->IsOffTheRecord()); |
| 554 PrefService* prefs = profile->GetPrefs(); | 572 PrefService* prefs = profile->GetPrefs(); |
| 555 EXPECT_EQ(IncognitoModePrefs::ENABLED, | 573 EXPECT_EQ(IncognitoModePrefs::ENABLED, |
| 556 IncognitoModePrefs::GetAvailability(prefs)); | 574 IncognitoModePrefs::GetAvailability(prefs)); |
| 557 | 575 |
| 558 ASSERT_TRUE(profile->GetOffTheRecordProfile()); | 576 ASSERT_TRUE(profile->GetOffTheRecordProfile()); |
| 559 | 577 |
| 560 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); | 578 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1219 dest_path2.BaseName().MaybeAsASCII()); | 1237 dest_path2.BaseName().MaybeAsASCII()); |
| 1220 profile_manager->ScheduleProfileForDeletion(dest_path2, | 1238 profile_manager->ScheduleProfileForDeletion(dest_path2, |
| 1221 ProfileManager::CreateCallback()); | 1239 ProfileManager::CreateCallback()); |
| 1222 // Spin the message loop so that all the callbacks can finish running. | 1240 // Spin the message loop so that all the callbacks can finish running. |
| 1223 base::RunLoop().RunUntilIdle(); | 1241 base::RunLoop().RunUntilIdle(); |
| 1224 | 1242 |
| 1225 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); | 1243 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1226 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); | 1244 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
| 1227 } | 1245 } |
| 1228 #endif // !defined(OS_MACOSX) | 1246 #endif // !defined(OS_MACOSX) |
| OLD | NEW |