Chromium Code Reviews| 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 "components/signin/core/common/profile_management_switches.h" | 38 #include "components/signin/core/common/profile_management_switches.h" |
| 39 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 40 #include "content/public/common/content_switches.h" | 40 #include "content/public/common/content_switches.h" |
| 41 #include "content/public/test/test_browser_thread_bundle.h" | 41 #include "content/public/test/test_browser_thread_bundle.h" |
| 42 #include "grit/generated_resources.h" | 42 #include "grit/generated_resources.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/user_manager.h" | 49 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 50 #include "chrome/browser/chromeos/settings/cros_settings.h" | 50 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 51 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 51 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 52 #include "chromeos/chromeos_switches.h" | 52 #include "chromeos/chromeos_switches.h" |
| 53 #include "chromeos/login/user_names.h" | 53 #include "chromeos/login/user_names.h" |
| 54 #endif | 54 #endif |
| 55 | 55 |
| 56 using base::ASCIIToUTF16; | 56 using base::ASCIIToUTF16; |
| 57 using content::BrowserThread; | 57 using content::BrowserThread; |
| 58 | 58 |
| 59 namespace { | 59 namespace { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 : local_state_(TestingBrowserProcess::GetGlobal()) { | 102 : local_state_(TestingBrowserProcess::GetGlobal()) { |
| 103 } | 103 } |
| 104 | 104 |
| 105 virtual void SetUp() { | 105 virtual void SetUp() { |
| 106 // Create a new temporary directory, and store the path | 106 // Create a new temporary directory, and store the path |
| 107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 108 TestingBrowserProcess::GetGlobal()->SetProfileManager( | 108 TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 109 new UnittestProfileManager(temp_dir_.path())); | 109 new UnittestProfileManager(temp_dir_.path())); |
| 110 | 110 |
| 111 #if defined(OS_CHROMEOS) | 111 #if defined(OS_CHROMEOS) |
| 112 CommandLine* cl = CommandLine::ForCurrentProcess(); | 112 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 113 cl->AppendSwitch(switches::kTestType); | 113 cl->AppendSwitch(switches::kTestType); |
| 114 #endif | 114 #endif |
| 115 } | 115 } |
| 116 | 116 |
| 117 virtual void TearDown() { | 117 virtual void TearDown() { |
| 118 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); | 118 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
| 119 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Helper function to create a profile with |name| for a profile |manager|. | 122 // Helper function to create a profile with |name| for a profile |manager|. |
| 123 void CreateProfileAsync(ProfileManager* manager, | 123 void CreateProfileAsync(ProfileManager* manager, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 141 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | 141 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 142 size_t num_profiles = cache.GetNumberOfProfiles(); | 142 size_t num_profiles = cache.GetNumberOfProfiles(); |
| 143 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); | 143 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); |
| 144 cache.AddProfileToCache(path, profile_name, | 144 cache.AddProfileToCache(path, profile_name, |
| 145 base::string16(), 0, std::string()); | 145 base::string16(), 0, std::string()); |
| 146 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles()); | 146 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles()); |
| 147 return profile_manager->GetProfile(path); | 147 return profile_manager->GetProfile(path); |
| 148 } | 148 } |
| 149 | 149 |
| 150 #if defined(OS_CHROMEOS) | 150 #if defined(OS_CHROMEOS) |
| 151 // Helper function to register an user with id |user_id| and create profile | |
| 152 // for him with a correct path. | |
|
Mr4D (OOO till 08-26)
2014/08/15 14:27:56
remove "for him".
dzhioev (left Google)
2014/08/15 16:11:25
Done.
| |
| 153 void RegisterUser(const std::string& user_id) { | |
| 154 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get(); | |
| 155 const std::string user_id_hash = | |
| 156 profile_helper->GetUserIdHashByUserIdForTests(user_id); | |
| 157 chromeos::UserManager::Get()->UserLoggedIn( | |
| 158 user_id, | |
| 159 user_id_hash, | |
| 160 false); | |
| 161 g_browser_process->profile_manager()->GetProfile( | |
| 162 profile_helper->GetProfilePathByUserIdHash(user_id_hash)); | |
| 163 } | |
| 164 | |
| 151 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 165 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 152 chromeos::ScopedTestCrosSettings test_cros_settings_; | 166 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 153 #endif | 167 #endif |
| 154 | 168 |
| 155 // The path to temporary directory used to contain the test operations. | 169 // The path to temporary directory used to contain the test operations. |
| 156 base::ScopedTempDir temp_dir_; | 170 base::ScopedTempDir temp_dir_; |
| 157 ScopedTestingLocalState local_state_; | 171 ScopedTestingLocalState local_state_; |
| 158 | 172 |
| 159 content::TestBrowserThreadBundle thread_bundle_; | 173 content::TestBrowserThreadBundle thread_bundle_; |
| 160 | 174 |
| 161 #if defined(OS_CHROMEOS) | 175 #if defined(OS_CHROMEOS) |
| 162 chromeos::ScopedTestUserManager test_user_manager_; | 176 chromeos::ScopedTestUserManager test_user_manager_; |
| 163 #endif | 177 #endif |
|
Mr4D (OOO till 08-26)
2014/08/15 14:27:56
Could you add DISALLOW_COPY_AND_ASSIGN here?
dzhioev (left Google)
2014/08/15 16:11:25
Done.
| |
| 164 }; | 178 }; |
| 165 | 179 |
| 166 TEST_F(ProfileManagerTest, GetProfile) { | 180 TEST_F(ProfileManagerTest, GetProfile) { |
| 167 base::FilePath dest_path = temp_dir_.path(); | 181 base::FilePath dest_path = temp_dir_.path(); |
| 168 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | 182 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 169 | 183 |
| 170 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 184 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 171 | 185 |
| 172 // Successfully create a profile. | 186 // Successfully create a profile. |
| 173 Profile* profile = profile_manager->GetProfile(dest_path); | 187 Profile* profile = profile_manager->GetProfile(dest_path); |
| 174 EXPECT_TRUE(profile); | 188 EXPECT_TRUE(profile); |
| 175 | 189 |
| 176 // The profile already exists when we call GetProfile. Just load it. | 190 // The profile already exists when we call GetProfile. Just load it. |
| 177 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); | 191 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
| 178 } | 192 } |
| 179 | 193 |
| 180 TEST_F(ProfileManagerTest, DefaultProfileDir) { | 194 TEST_F(ProfileManagerTest, DefaultProfileDir) { |
| 181 base::FilePath expected_default = | 195 base::FilePath expected_default = |
| 182 base::FilePath().AppendASCII(chrome::kInitialProfile); | 196 base::FilePath().AppendASCII(chrome::kInitialProfile); |
| 183 EXPECT_EQ( | 197 EXPECT_EQ( |
| 184 expected_default.value(), | 198 expected_default.value(), |
| 185 g_browser_process->profile_manager()->GetInitialProfileDir().value()); | 199 g_browser_process->profile_manager()->GetInitialProfileDir().value()); |
| 186 } | 200 } |
| 187 | 201 |
| 188 #if defined(OS_CHROMEOS) | 202 #if defined(OS_CHROMEOS) |
| 203 | |
| 189 // This functionality only exists on Chrome OS. | 204 // This functionality only exists on Chrome OS. |
| 190 TEST_F(ProfileManagerTest, LoggedInProfileDir) { | 205 TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 191 CommandLine *cl = CommandLine::ForCurrentProcess(); | |
| 192 std::string profile_dir(chrome::kTestUserProfileDir); | |
| 193 | |
| 194 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, profile_dir); | |
| 195 | |
| 196 base::FilePath expected_default = | 206 base::FilePath expected_default = |
| 197 base::FilePath().AppendASCII(chrome::kInitialProfile); | 207 base::FilePath().AppendASCII(chrome::kInitialProfile); |
| 198 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 208 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 199 EXPECT_EQ(expected_default.value(), | 209 EXPECT_EQ(expected_default.value(), |
| 200 profile_manager->GetInitialProfileDir().value()); | 210 profile_manager->GetInitialProfileDir().value()); |
| 201 | 211 |
| 202 scoped_ptr<chromeos::MockUserManager> mock_user_manager; | 212 const char kTestUserName[] = "test-user@example.com"; |
| 203 mock_user_manager.reset(new chromeos::MockUserManager()); | 213 chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager(); |
| 204 mock_user_manager->SetActiveUser("user@gmail.com"); | 214 chromeos::ScopedUserManagerEnabler enabler(user_manager); |
| 205 user_manager::User* active_user = mock_user_manager->GetActiveUser(); | 215 |
| 216 const user_manager::User* active_user = user_manager->AddUser(kTestUserName); | |
| 217 user_manager->LoginUser(kTestUserName); | |
| 218 user_manager->SwitchActiveUser(kTestUserName); | |
| 219 | |
| 206 profile_manager->Observe( | 220 profile_manager->Observe( |
| 207 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 221 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 208 content::NotificationService::AllSources(), | 222 content::NotificationService::AllSources(), |
| 209 content::Details<const user_manager::User>(active_user)); | 223 content::Details<const user_manager::User>(active_user)); |
| 210 base::FilePath expected_logged_in(profile_dir); | 224 base::FilePath expected_logged_in( |
| 225 chromeos::ProfileHelper::GetUserProfileDir(active_user->username_hash())); | |
| 211 EXPECT_EQ(expected_logged_in.value(), | 226 EXPECT_EQ(expected_logged_in.value(), |
| 212 profile_manager->GetInitialProfileDir().value()); | 227 profile_manager->GetInitialProfileDir().value()); |
| 213 VLOG(1) << temp_dir_.path().Append( | 228 VLOG(1) << temp_dir_.path().Append( |
| 214 profile_manager->GetInitialProfileDir()).value(); | 229 profile_manager->GetInitialProfileDir()).value(); |
| 215 } | 230 } |
| 216 | 231 |
| 217 #endif | 232 #endif |
| 218 | 233 |
| 219 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 234 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
| 220 base::FilePath dest_path1 = temp_dir_.path(); | 235 base::FilePath dest_path1 = temp_dir_.path(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 406 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 392 TestingBrowserProcess::GetGlobal()->SetProfileManager( | 407 TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 393 new UnittestGuestProfileManager(temp_dir_.path())); | 408 new UnittestGuestProfileManager(temp_dir_.path())); |
| 394 | 409 |
| 395 #if defined(OS_CHROMEOS) | 410 #if defined(OS_CHROMEOS) |
| 396 CommandLine* cl = CommandLine::ForCurrentProcess(); | 411 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 397 // This switch is needed to skip non-test specific behavior in | 412 // This switch is needed to skip non-test specific behavior in |
| 398 // ProfileManager (accessing DBusThreadManager). | 413 // ProfileManager (accessing DBusThreadManager). |
| 399 cl->AppendSwitch(switches::kTestType); | 414 cl->AppendSwitch(switches::kTestType); |
| 400 | 415 |
| 401 cl->AppendSwitchASCII(chromeos::switches::kLoginProfile, | |
| 402 std::string(chrome::kProfileDirPrefix) + | |
| 403 chromeos::login::kGuestUserName); | |
| 404 cl->AppendSwitch(chromeos::switches::kGuestSession); | 416 cl->AppendSwitch(chromeos::switches::kGuestSession); |
| 405 cl->AppendSwitch(::switches::kIncognito); | 417 cl->AppendSwitch(::switches::kIncognito); |
| 406 | 418 |
| 407 chromeos::UserManager::Get()->UserLoggedIn(chromeos::login::kGuestUserName, | 419 RegisterUser(chromeos::login::kGuestUserName); |
| 408 chromeos::login::kGuestUserName, | |
| 409 false); | |
| 410 #endif | 420 #endif |
| 411 } | 421 } |
| 412 }; | 422 }; |
| 413 | 423 |
| 414 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { | 424 TEST_F(ProfileManagerGuestTest, GetLastUsedProfileAllowedByPolicy) { |
| 415 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 425 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 416 ASSERT_TRUE(profile_manager); | 426 ASSERT_TRUE(profile_manager); |
| 417 | 427 |
| 418 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); | 428 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 419 ASSERT_TRUE(profile); | 429 ASSERT_TRUE(profile); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 EXPECT_EQ(profile_name, | 549 EXPECT_EQ(profile_name, |
| 540 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); | 550 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index))); |
| 541 EXPECT_EQ(avatar_index, | 551 EXPECT_EQ(avatar_index, |
| 542 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); | 552 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index)); |
| 543 } | 553 } |
| 544 | 554 |
| 545 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { | 555 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { |
| 546 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 556 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 547 ASSERT_TRUE(profile_manager); | 557 ASSERT_TRUE(profile_manager); |
| 548 | 558 |
| 559 #if defined(OS_CHROMEOS) | |
| 560 // On CrOS, profile returned by GetLastUsedProfile is an singin profile that | |
| 561 // is forced to be incognito. That's why we need to create at least one user | |
| 562 // to get a regular profile. | |
| 563 RegisterUser("test-user@example.com"); | |
| 564 #endif | |
| 565 | |
| 549 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); | 566 Profile* profile = profile_manager->GetLastUsedProfileAllowedByPolicy(); |
| 550 ASSERT_TRUE(profile); | 567 ASSERT_TRUE(profile); |
| 551 EXPECT_FALSE(profile->IsOffTheRecord()); | 568 EXPECT_FALSE(profile->IsOffTheRecord()); |
| 552 PrefService* prefs = profile->GetPrefs(); | 569 PrefService* prefs = profile->GetPrefs(); |
| 553 EXPECT_EQ(IncognitoModePrefs::ENABLED, | 570 EXPECT_EQ(IncognitoModePrefs::ENABLED, |
| 554 IncognitoModePrefs::GetAvailability(prefs)); | 571 IncognitoModePrefs::GetAvailability(prefs)); |
| 555 | 572 |
| 556 ASSERT_TRUE(profile->GetOffTheRecordProfile()); | 573 ASSERT_TRUE(profile->GetOffTheRecordProfile()); |
| 557 | 574 |
| 558 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); | 575 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::DISABLED); |
| (...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1206 dest_path2.BaseName().MaybeAsASCII()); | 1223 dest_path2.BaseName().MaybeAsASCII()); |
| 1207 profile_manager->ScheduleProfileForDeletion(dest_path2, | 1224 profile_manager->ScheduleProfileForDeletion(dest_path2, |
| 1208 ProfileManager::CreateCallback()); | 1225 ProfileManager::CreateCallback()); |
| 1209 // Spin the message loop so that all the callbacks can finish running. | 1226 // Spin the message loop so that all the callbacks can finish running. |
| 1210 base::RunLoop().RunUntilIdle(); | 1227 base::RunLoop().RunUntilIdle(); |
| 1211 | 1228 |
| 1212 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); | 1229 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
| 1213 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); | 1230 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
| 1214 } | 1231 } |
| 1215 #endif // !defined(OS_MACOSX) | 1232 #endif // !defined(OS_MACOSX) |
| OLD | NEW |