Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/profiles/profile_manager_browsertest.cc

Issue 962143002: Reland r318288: Wait until a new profile has been created before deleting the active profile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: fix compile Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/profiles/profile_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/password_manager/password_store_factory.h" 9 #include "chrome/browser/password_manager/password_store_factory.h"
10 #include "chrome/browser/profiles/profile_info_cache.h" 10 #include "chrome/browser/profiles/profile_info_cache.h"
(...skipping 22 matching lines...) Expand all
33 #include "chromeos/chromeos_switches.h" 33 #include "chromeos/chromeos_switches.h"
34 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
35 #endif 35 #endif
36 36
37 namespace { 37 namespace {
38 38
39 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing; 39 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing;
40 40
41 // An observer that returns back to test code after a new profile is 41 // An observer that returns back to test code after a new profile is
42 // initialized. 42 // initialized.
43 void OnUnblockOnProfileCreation(Profile* profile, 43 void OnUnblockOnProfileCreation(base::RunLoop* run_loop,
44 Profile* profile,
44 Profile::CreateStatus status) { 45 Profile::CreateStatus status) {
45 if (status == Profile::CREATE_STATUS_INITIALIZED) 46 if (status == Profile::CREATE_STATUS_INITIALIZED)
46 base::MessageLoop::current()->Quit(); 47 run_loop->Quit();
47 } 48 }
48 49
49 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { 50 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) {
50 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); 51 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL);
51 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); 52 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL);
52 // No browser should have been created for this profile yet. 53 // No browser should have been created for this profile yet.
53 EXPECT_EQ(chrome::GetTotalBrowserCountForProfile(profile), 0U); 54 EXPECT_EQ(chrome::GetTotalBrowserCountForProfile(profile), 0U);
54 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); 55 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
55 if (status == Profile::CREATE_STATUS_INITIALIZED) 56 if (status == Profile::CREATE_STATUS_INITIALIZED)
56 base::MessageLoop::current()->Quit(); 57 base::MessageLoop::current()->Quit();
(...skipping 27 matching lines...) Expand all
84 } 85 }
85 86
86 private: 87 private:
87 std::string last_used_profile_name_; 88 std::string last_used_profile_name_;
88 89
89 DISALLOW_COPY_AND_ASSIGN(ProfileRemovalObserver); 90 DISALLOW_COPY_AND_ASSIGN(ProfileRemovalObserver);
90 }; 91 };
91 92
92 // The class serves to retrieve passwords from PasswordStore asynchronously. It 93 // The class serves to retrieve passwords from PasswordStore asynchronously. It
93 // used by ProfileManagerBrowserTest.DeletePasswords on some platforms. 94 // used by ProfileManagerBrowserTest.DeletePasswords on some platforms.
94 class PasswordStoreConsumerVerifier : 95 class PasswordStoreConsumerVerifier
95 public password_manager::PasswordStoreConsumer { 96 : public password_manager::PasswordStoreConsumer {
96 public: 97 public:
97 PasswordStoreConsumerVerifier() : called_(false) {}
98
99 void OnGetPasswordStoreResults( 98 void OnGetPasswordStoreResults(
100 ScopedVector<autofill::PasswordForm> results) override { 99 ScopedVector<autofill::PasswordForm> results) override {
101 EXPECT_FALSE(called_);
102 called_ = true;
103 password_entries_.swap(results); 100 password_entries_.swap(results);
101 run_loop_.Quit();
104 } 102 }
105 103
106 bool IsCalled() const { return called_; } 104 void Wait() {
105 run_loop_.Run();
106 }
107 107
108 const std::vector<autofill::PasswordForm*>& GetPasswords() const { 108 const std::vector<autofill::PasswordForm*>& GetPasswords() const {
109 return password_entries_.get(); 109 return password_entries_.get();
110 } 110 }
111
111 private: 112 private:
113 base::RunLoop run_loop_;
112 ScopedVector<autofill::PasswordForm> password_entries_; 114 ScopedVector<autofill::PasswordForm> password_entries_;
113 bool called_;
114 }; 115 };
115 116
116 static base::FilePath GetFirstNonSigninProfile(const ProfileInfoCache& cache) { 117 static base::FilePath GetFirstNonSigninProfile(const ProfileInfoCache& cache) {
117 #if defined(OS_CHROMEOS) 118 #if defined(OS_CHROMEOS)
118 const base::FilePath signin_path = 119 const base::FilePath signin_path =
119 chromeos::ProfileHelper::GetSigninProfileDir(); 120 chromeos::ProfileHelper::GetSigninProfileDir();
120 size_t i, profile_num = cache.GetNumberOfProfiles(); 121 size_t i, profile_num = cache.GetNumberOfProfiles();
121 for (i = 0; i != profile_num; ++i) { 122 for (i = 0; i != profile_num; ++i) {
122 base::FilePath profile_path = cache.GetPathOfProfileAtIndex(i); 123 base::FilePath profile_path = cache.GetPathOfProfileAtIndex(i);
123 if (profile_path != signin_path) 124 if (profile_path != signin_path)
(...skipping 29 matching lines...) Expand all
153 ProfileManager* profile_manager = g_browser_process->profile_manager(); 154 ProfileManager* profile_manager = g_browser_process->profile_manager();
154 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 155 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
155 ProfileRemovalObserver observer; 156 ProfileRemovalObserver observer;
156 157
157 // We should start out with 1 profile. 158 // We should start out with 1 profile.
158 ASSERT_EQ(cache.GetNumberOfProfiles(), 1U); 159 ASSERT_EQ(cache.GetNumberOfProfiles(), 1U);
159 160
160 // Delete singleton profile. 161 // Delete singleton profile.
161 base::FilePath singleton_profile_path = cache.GetPathOfProfileAtIndex(0); 162 base::FilePath singleton_profile_path = cache.GetPathOfProfileAtIndex(0);
162 EXPECT_FALSE(singleton_profile_path.empty()); 163 EXPECT_FALSE(singleton_profile_path.empty());
163 profile_manager->ScheduleProfileForDeletion(singleton_profile_path, 164 base::RunLoop run_loop;
164 ProfileManager::CreateCallback()); 165 profile_manager->ScheduleProfileForDeletion(
166 singleton_profile_path,
167 base::Bind(&OnUnblockOnProfileCreation, &run_loop));
165 168
166 // Spin things till profile is actually deleted. 169 // Spin things till profile is actually deleted.
Mike Lerman 2015/03/02 18:37:38 Replace Spin with Wait
Bernhard Bauer 2015/03/02 21:06:45 Done.
167 content::RunAllPendingInMessageLoop(); 170 run_loop.Run();
168 171
169 // Make sure a new profile was created automatically. 172 // Make sure a new profile was created automatically.
170 EXPECT_EQ(cache.GetNumberOfProfiles(), 1U); 173 EXPECT_EQ(cache.GetNumberOfProfiles(), 1U);
171 base::FilePath new_profile_path = cache.GetPathOfProfileAtIndex(0); 174 base::FilePath new_profile_path = cache.GetPathOfProfileAtIndex(0);
172 EXPECT_NE(new_profile_path, singleton_profile_path); 175 EXPECT_NE(new_profile_path.value(), singleton_profile_path.value());
173 176
174 // Make sure that last used profile preference is set correctly. 177 // Make sure that last used profile preference is set correctly.
175 Profile* last_used = ProfileManager::GetLastUsedProfile(); 178 Profile* last_used = ProfileManager::GetLastUsedProfile();
176 EXPECT_EQ(new_profile_path, last_used->GetPath()); 179 EXPECT_EQ(new_profile_path.value(), last_used->GetPath().value());
177 180
178 // Make sure the last used profile was set correctly before the notification 181 // Make sure the last used profile was set correctly before the notification
179 // was sent. 182 // was sent.
180 std::string last_used_profile_name = 183 std::string last_used_profile_name =
181 last_used->GetPath().BaseName().MaybeAsASCII(); 184 last_used->GetPath().BaseName().MaybeAsASCII();
182 EXPECT_EQ(last_used_profile_name, observer.last_used_profile_name()); 185 EXPECT_EQ(last_used_profile_name, observer.last_used_profile_name());
183 } 186 }
184 187
185 // Delete all profiles in a multi profile setup and make sure a new one is 188 // Delete all profiles in a multi profile setup and make sure a new one is
186 // created. 189 // created.
187 // Crashes/CHECKs. See crbug.com/104851 190 // Crashes/CHECKs. See crbug.com/104851
188 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) { 191 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) {
189 ProfileManager* profile_manager = g_browser_process->profile_manager(); 192 ProfileManager* profile_manager = g_browser_process->profile_manager();
190 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 193 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
191 194
192 // Create an additional profile. 195 // Create an additional profile.
193 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); 196 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
194 profile_manager->CreateProfileAsync(new_path, 197 base::RunLoop run_loop;
195 base::Bind(&OnUnblockOnProfileCreation), 198 profile_manager->CreateProfileAsync(
196 base::string16(), base::string16(), 199 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
197 std::string()); 200 base::string16(), base::string16(), std::string());
198 201
199 // Spin to allow profile creation to take place, loop is terminated 202 // Spin to allow profile creation to take place, loop is terminated
200 // by OnUnblockOnProfileCreation when the profile is created. 203 // by OnUnblockOnProfileCreation when the profile is created.
201 content::RunMessageLoop(); 204 run_loop.Run();
202 205
203 ASSERT_EQ(cache.GetNumberOfProfiles(), 2U); 206 ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
204 207
205 // Delete all profiles. 208 // Delete all profiles.
206 base::FilePath profile_path1 = cache.GetPathOfProfileAtIndex(0); 209 base::FilePath profile_path1 = cache.GetPathOfProfileAtIndex(0);
207 base::FilePath profile_path2 = cache.GetPathOfProfileAtIndex(1); 210 base::FilePath profile_path2 = cache.GetPathOfProfileAtIndex(1);
208 EXPECT_FALSE(profile_path1.empty()); 211 EXPECT_FALSE(profile_path1.empty());
209 EXPECT_FALSE(profile_path2.empty()); 212 EXPECT_FALSE(profile_path2.empty());
210 profile_manager->ScheduleProfileForDeletion(profile_path1, 213 profile_manager->ScheduleProfileForDeletion(profile_path1,
211 ProfileManager::CreateCallback()); 214 ProfileManager::CreateCallback());
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 302 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
300 size_t initial_profile_count = profile_manager->GetNumberOfProfiles(); 303 size_t initial_profile_count = profile_manager->GetNumberOfProfiles();
301 base::FilePath path_profile1 = GetFirstNonSigninProfile(cache); 304 base::FilePath path_profile1 = GetFirstNonSigninProfile(cache);
302 305
303 ASSERT_NE(0U, initial_profile_count); 306 ASSERT_NE(0U, initial_profile_count);
304 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); 307 EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
305 308
306 // Create an additional profile. 309 // Create an additional profile.
307 base::FilePath path_profile2 = 310 base::FilePath path_profile2 =
308 profile_manager->GenerateNextProfileDirectoryPath(); 311 profile_manager->GenerateNextProfileDirectoryPath();
309 profile_manager->CreateProfileAsync(path_profile2, 312 base::RunLoop run_loop;
310 base::Bind(&OnUnblockOnProfileCreation), 313 profile_manager->CreateProfileAsync(
311 base::string16(), base::string16(), 314 path_profile2, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
312 std::string()); 315 base::string16(), base::string16(), std::string());
313 316
314 // Spin to allow profile creation to take place, loop is terminated 317 // Spin to allow profile creation to take place, loop is terminated
Mike Lerman 2015/03/02 18:37:38 Replace "Spin" with "Wait", or somehow note that w
Bernhard Bauer 2015/03/02 21:06:45 Done. (I used "run the message loop", as "spin" is
315 // by OnUnblockOnProfileCreation when the profile is created. 318 // by OnUnblockOnProfileCreation when the profile is created.
316 content::RunMessageLoop(); 319 run_loop.Run();
317 320
318 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); 321 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop();
319 BrowserList* browser_list = BrowserList::GetInstance(desktop_type); 322 BrowserList* browser_list = BrowserList::GetInstance(desktop_type);
320 ASSERT_EQ(initial_profile_count + 1, cache.GetNumberOfProfiles()); 323 ASSERT_EQ(initial_profile_count + 1, cache.GetNumberOfProfiles());
321 EXPECT_EQ(1U, browser_list->size()); 324 EXPECT_EQ(1U, browser_list->size());
322 325
323 // Open a browser window for the first profile. 326 // Open a browser window for the first profile.
324 profiles::SwitchToProfile(path_profile1, desktop_type, false, 327 profiles::SwitchToProfile(path_profile1, desktop_type, false,
325 kOnProfileSwitchDoNothing, 328 kOnProfileSwitchDoNothing,
326 ProfileMetrics::SWITCH_PROFILE_ICON); 329 ProfileMetrics::SWITCH_PROFILE_ICON);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 form.blacklisted_by_user = false; 441 form.blacklisted_by_user = false;
439 442
440 scoped_refptr<password_manager::PasswordStore> password_store = 443 scoped_refptr<password_manager::PasswordStore> password_store =
441 PasswordStoreFactory::GetForProfile( 444 PasswordStoreFactory::GetForProfile(
442 profile, ServiceAccessType::EXPLICIT_ACCESS).get(); 445 profile, ServiceAccessType::EXPLICIT_ACCESS).get();
443 ASSERT_TRUE(password_store.get()); 446 ASSERT_TRUE(password_store.get());
444 447
445 password_store->AddLogin(form); 448 password_store->AddLogin(form);
446 PasswordStoreConsumerVerifier verify_add; 449 PasswordStoreConsumerVerifier verify_add;
447 password_store->GetAutofillableLogins(&verify_add); 450 password_store->GetAutofillableLogins(&verify_add);
451 verify_add.Wait();
452 EXPECT_EQ(1u, verify_add.GetPasswords().size());
448 453
449 ProfileManager* profile_manager = g_browser_process->profile_manager(); 454 ProfileManager* profile_manager = g_browser_process->profile_manager();
450 profile_manager->ScheduleProfileForDeletion(profile->GetPath(), 455 base::RunLoop run_loop;
451 ProfileManager::CreateCallback()); 456 profile_manager->ScheduleProfileForDeletion(
452 content::RunAllPendingInMessageLoop(); 457 profile->GetPath(), base::Bind(&OnUnblockOnProfileCreation, &run_loop));
458 run_loop.Run();
459
453 PasswordStoreConsumerVerifier verify_delete; 460 PasswordStoreConsumerVerifier verify_delete;
454 password_store->GetAutofillableLogins(&verify_delete); 461 password_store->GetAutofillableLogins(&verify_delete);
455 462 verify_delete.Wait();
456 // Run the password background thread.
457 base::RunLoop run_loop;
458 base::Closure task = base::Bind(
459 base::IgnoreResult(&content::BrowserThread::PostTask),
460 content::BrowserThread::UI,
461 FROM_HERE,
462 run_loop.QuitClosure());
463 EXPECT_TRUE(password_store->ScheduleTask(task));
464 run_loop.Run();
465
466 EXPECT_TRUE(verify_add.IsCalled());
467 EXPECT_EQ(1u, verify_add.GetPasswords().size());
468 EXPECT_TRUE(verify_delete.IsCalled());
469 EXPECT_EQ(0u, verify_delete.GetPasswords().size()); 463 EXPECT_EQ(0u, verify_delete.GetPasswords().size());
470 } 464 }
471 #endif // !defined(OS_WIN) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 465 #endif // !defined(OS_WIN) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698