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

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

Issue 2793913002: Added profile deletion browsertests. (Closed)
Patch Set: Review fixes. Created 3 years, 8 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 | « no previous file | chrome/browser/ui/webui/profile_helper_browsertest.cc » ('j') | 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "chrome/browser/browsing_data/browsing_data_remover_impl.h"
15 #include "chrome/browser/lifetime/keep_alive_types.h"
16 #include "chrome/browser/lifetime/scoped_keep_alive.h"
12 #include "chrome/browser/password_manager/password_store_factory.h" 17 #include "chrome/browser/password_manager/password_store_factory.h"
13 #include "chrome/browser/profiles/profile_attributes_entry.h" 18 #include "chrome/browser/profiles/profile_attributes_entry.h"
14 #include "chrome/browser/profiles/profile_attributes_storage.h" 19 #include "chrome/browser/profiles/profile_attributes_storage.h"
15 #include "chrome/browser/profiles/profile_manager.h" 20 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profile_window.h" 21 #include "chrome/browser/profiles/profile_window.h"
17 #include "chrome/browser/profiles/profiles_state.h" 22 #include "chrome/browser/profiles/profiles_state.h"
18 #include "chrome/browser/ui/browser_finder.h" 23 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_list.h" 24 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_window.h" 25 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
(...skipping 30 matching lines...) Expand all
52 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { 57 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) {
53 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); 58 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL);
54 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); 59 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL);
55 // No browser should have been created for this profile yet. 60 // No browser should have been created for this profile yet.
56 EXPECT_EQ(chrome::GetBrowserCount(profile), 0U); 61 EXPECT_EQ(chrome::GetBrowserCount(profile), 0U);
57 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); 62 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
58 if (status == Profile::CREATE_STATUS_INITIALIZED) 63 if (status == Profile::CREATE_STATUS_INITIALIZED)
59 base::MessageLoop::current()->QuitWhenIdle(); 64 base::MessageLoop::current()->QuitWhenIdle();
60 } 65 }
61 66
67 // An observer returns back to test code after one or more profiles was deleted.
68 // It has ScopedKeepAlive object to prevent browser shutdown started in case
Mike Lerman 2017/04/03 16:56:49 nit: this sentence needs better grammar
69 // browser become windowless.
70 class MultipleProfileDeletionObserver
71 : public BrowsingDataRemoverImpl::CompletionInhibitor {
72 public:
73 MultipleProfileDeletionObserver(size_t expect_profile_creation_count,
74 size_t expected_profile_data_removed_count)
75 : profiles_created_count_(0),
76 profiles_data_removed_count_(0),
77 expected_profile_creation_count_(expect_profile_creation_count),
78 expected_profile_data_removed_count_(
79 expected_profile_data_removed_count) {
80 size_t expected_events_count =
81 expected_profile_creation_count_ + expected_profile_data_removed_count_;
82 EXPECT_GT(expected_events_count, 0u);
83 BrowsingDataRemoverImpl::set_completion_inhibitor_for_testing(this);
84 }
85 ~MultipleProfileDeletionObserver() {
86 BrowsingDataRemoverImpl::set_completion_inhibitor_for_testing(nullptr);
87 }
88 ProfileManager::CreateCallback OnProfileCreatedClosure() {
89 return base::Bind(&MultipleProfileDeletionObserver::OnProfileCreated,
90 base::Unretained(this));
91 }
92 void Wait() {
93 keep_alive_ = base::MakeUnique<ScopedKeepAlive>(
94 KeepAliveOrigin::PROFILE_HELPER, KeepAliveRestartOption::DISABLED);
95 loop_.Run();
96 }
97
98 private:
99 void OnProfileCreated(Profile* profile, Profile::CreateStatus status) {
100 EXPECT_EQ(Profile::CREATE_STATUS_INITIALIZED, status);
101 profiles_created_count_++;
102 MaybeQuit();
103 }
104
105 // TODO(https://crbug.com/704601): remove this code when bug is fixed.
106 void OnBrowsingDataRemoverWouldComplete(
107 BrowsingDataRemoverImpl* remover,
108 const base::Closure& continue_to_completion) override {
109 continue_to_completion.Run();
110 profiles_data_removed_count_++;
111 MaybeQuit();
112 }
113
114 void MaybeQuit() {
115 DLOG(INFO) << profiles_created_count_ << " of expected "
116 << expected_profile_creation_count_ << " profiles created, "
117 << profiles_data_removed_count_ << " of expected "
118 << expected_profile_data_removed_count_
119 << " profile data removed.";
120 if (profiles_created_count_ < expected_profile_creation_count_ ||
121 profiles_data_removed_count_ < expected_profile_data_removed_count_)
122 return;
123
124 EXPECT_EQ(expected_profile_creation_count_, profiles_created_count_);
125 EXPECT_EQ(expected_profile_data_removed_count_,
126 profiles_data_removed_count_);
127
128 keep_alive_.reset();
129 loop_.Quit();
130 }
131
132 base::RunLoop loop_;
133 std::unique_ptr<ScopedKeepAlive> keep_alive_;
134 size_t profiles_created_count_;
135 size_t profiles_data_removed_count_;
136 size_t expected_profile_creation_count_;
137 size_t expected_profile_data_removed_count_;
138 };
139
62 void EphemeralProfileCreationComplete(Profile* profile, 140 void EphemeralProfileCreationComplete(Profile* profile,
63 Profile::CreateStatus status) { 141 Profile::CreateStatus status) {
64 if (status == Profile::CREATE_STATUS_INITIALIZED) 142 if (status == Profile::CREATE_STATUS_INITIALIZED)
65 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); 143 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
66 ProfileCreationComplete(profile, status); 144 ProfileCreationComplete(profile, status);
67 } 145 }
68 146
69 class ProfileRemovalObserver : public ProfileAttributesStorage::Observer { 147 class ProfileRemovalObserver : public ProfileAttributesStorage::Observer {
70 public: 148 public:
71 ProfileRemovalObserver() { 149 ProfileRemovalObserver() {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 class ProfileManagerBrowserTest : public InProcessBrowserTest { 223 class ProfileManagerBrowserTest : public InProcessBrowserTest {
146 protected: 224 protected:
147 void SetUpCommandLine(base::CommandLine* command_line) override { 225 void SetUpCommandLine(base::CommandLine* command_line) override {
148 #if defined(OS_CHROMEOS) 226 #if defined(OS_CHROMEOS)
149 command_line->AppendSwitch( 227 command_line->AppendSwitch(
150 chromeos::switches::kIgnoreUserProfileMappingForTests); 228 chromeos::switches::kIgnoreUserProfileMappingForTests);
151 #endif 229 #endif
152 } 230 }
153 }; 231 };
154 232
155 #if defined(OS_MACOSX) 233 // Android does not support multi-profiles, and CrOS multi-profiles
234 // implementation is too different for these tests.
235 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
156 236
157 // Delete single profile and make sure a new one is created. 237 // Delete single profile and make sure a new one is created.
158 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteSingletonProfile) { 238 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteSingletonProfile) {
159 ProfileManager* profile_manager = g_browser_process->profile_manager(); 239 ProfileManager* profile_manager = g_browser_process->profile_manager();
160 ProfileAttributesStorage& storage = 240 ProfileAttributesStorage& storage =
161 profile_manager->GetProfileAttributesStorage(); 241 profile_manager->GetProfileAttributesStorage();
162 ProfileRemovalObserver observer; 242 ProfileRemovalObserver observer;
163 243
164 // We should start out with 1 profile. 244 // We should start out with 1 profile.
165 ASSERT_EQ(1u, storage.GetNumberOfProfiles()); 245 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
166 246
167 // Delete singleton profile. 247 // Delete singleton profile.
168 base::FilePath singleton_profile_path = 248 base::FilePath singleton_profile_path =
169 storage.GetAllProfilesAttributes().front()->GetPath(); 249 storage.GetAllProfilesAttributes().front()->GetPath();
170 EXPECT_FALSE(singleton_profile_path.empty()); 250 EXPECT_FALSE(singleton_profile_path.empty());
171 base::RunLoop run_loop; 251 MultipleProfileDeletionObserver profile_deletion_observer(1u, 1u);
172 profile_manager->ScheduleProfileForDeletion( 252 profile_manager->ScheduleProfileForDeletion(
173 singleton_profile_path, 253 singleton_profile_path,
174 base::Bind(&OnUnblockOnProfileCreation, &run_loop)); 254 profile_deletion_observer.OnProfileCreatedClosure());
175 255
176 // Run the message loop until the profile is actually deleted (as indicated 256 // Run the message loop until the profile is actually deleted (as indicated
177 // by the callback above being called). 257 // by the callback above being called).
178 run_loop.Run(); 258 profile_deletion_observer.Wait();
179 259
180 // Make sure a new profile was created automatically. 260 // Make sure a new profile was created automatically.
181 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 261 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
182 base::FilePath new_profile_path = 262 base::FilePath new_profile_path =
183 storage.GetAllProfilesAttributes().front()->GetPath(); 263 storage.GetAllProfilesAttributes().front()->GetPath();
184 EXPECT_NE(new_profile_path.value(), singleton_profile_path.value()); 264 EXPECT_NE(new_profile_path.value(), singleton_profile_path.value());
185 265
186 // Make sure that last used profile preference is set correctly. 266 // Make sure that last used profile preference is set correctly.
187 Profile* last_used = ProfileManager::GetLastUsedProfile(); 267 Profile* last_used = ProfileManager::GetLastUsedProfile();
188 EXPECT_EQ(new_profile_path.value(), last_used->GetPath().value()); 268 EXPECT_EQ(new_profile_path.value(), last_used->GetPath().value());
189 269
190 // Make sure the last used profile was set correctly before the notification 270 // Make sure the last used profile was set correctly before the notification
191 // was sent. 271 // was sent.
192 std::string last_used_profile_name = 272 std::string last_used_profile_name =
193 last_used->GetPath().BaseName().MaybeAsASCII(); 273 last_used->GetPath().BaseName().MaybeAsASCII();
194 EXPECT_EQ(last_used_profile_name, observer.last_used_profile_name()); 274 EXPECT_EQ(last_used_profile_name, observer.last_used_profile_name());
195 } 275 }
196 276
277 // Delete inactive profile in a multi profile setup and make sure current
278 // browser is not affected.
279 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteInactiveProfile) {
280 ProfileManager* profile_manager = g_browser_process->profile_manager();
281 ProfileAttributesStorage& storage =
282 profile_manager->GetProfileAttributesStorage();
283 base::FilePath current_profile_path = browser()->profile()->GetPath();
284
285 // Create an additional profile.
286 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
287 base::RunLoop run_loop;
288 profile_manager->CreateProfileAsync(
289 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
290 base::string16(), std::string(), std::string());
291 run_loop.Run();
292
293 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
294
295 // Delete inactive profile.
296 MultipleProfileDeletionObserver profile_deletion_observer(0u, 1u);
297 profile_manager->ScheduleProfileForDeletion(
298 new_path, profile_deletion_observer.OnProfileCreatedClosure());
299 profile_deletion_observer.Wait();
300
301 // Make sure there only preexisted profile left.
302 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
303 EXPECT_EQ(current_profile_path,
304 storage.GetAllProfilesAttributes().front()->GetPath());
305
306 // Make sure that last used profile preference is set correctly.
307 Profile* last_used = ProfileManager::GetLastUsedProfile();
308 EXPECT_EQ(current_profile_path, last_used->GetPath());
309 }
310
311 // Delete current profile in a multi profile setup and make sure an existing one
312 // is loaded.
313 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteCurrentProfile) {
314 ProfileManager* profile_manager = g_browser_process->profile_manager();
315 ProfileAttributesStorage& storage =
316 profile_manager->GetProfileAttributesStorage();
317
318 // Create an additional profile.
319 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
320 base::RunLoop run_loop;
321 profile_manager->CreateProfileAsync(
322 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
323 base::string16(), std::string(), std::string());
324 run_loop.Run();
325
326 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
327
328 // Delete current profile.
329 MultipleProfileDeletionObserver profile_deletion_observer(1u, 1u);
330 profile_manager->ScheduleProfileForDeletion(
331 browser()->profile()->GetPath(),
332 profile_deletion_observer.OnProfileCreatedClosure());
333 profile_deletion_observer.Wait();
334
335 // Make sure a profile created earlier become the only profile.
336 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
337 EXPECT_EQ(new_path, storage.GetAllProfilesAttributes().front()->GetPath());
338
339 // Make sure that last used profile preference is set correctly.
340 Profile* last_used = ProfileManager::GetLastUsedProfile();
341 EXPECT_EQ(new_path, last_used->GetPath());
342 }
343
197 // Delete all profiles in a multi profile setup and make sure a new one is 344 // Delete all profiles in a multi profile setup and make sure a new one is
198 // created. 345 // created.
199 // Crashes/CHECKs. See crbug.com/104851 346 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteAllProfiles) {
200 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) {
201 ProfileManager* profile_manager = g_browser_process->profile_manager(); 347 ProfileManager* profile_manager = g_browser_process->profile_manager();
202 ProfileAttributesStorage& storage = 348 ProfileAttributesStorage& storage =
203 profile_manager->GetProfileAttributesStorage(); 349 profile_manager->GetProfileAttributesStorage();
204 350
205 // Create an additional profile. 351 // Create an additional profile.
206 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); 352 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
207 base::RunLoop run_loop; 353 base::RunLoop run_loop;
208 profile_manager->CreateProfileAsync( 354 profile_manager->CreateProfileAsync(
209 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop), 355 new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
210 base::string16(), std::string(), std::string()); 356 base::string16(), std::string(), std::string());
211 357
212 // Run the message loop to allow profile creation to take place; the loop is 358 // Run the message loop to allow profile creation to take place; the loop is
213 // terminated by OnUnblockOnProfileCreation when the profile is created. 359 // terminated by OnUnblockOnProfileCreation when the profile is created.
214 run_loop.Run(); 360 run_loop.Run();
215 361
216 ASSERT_EQ(2u, storage.GetNumberOfProfiles()); 362 ASSERT_EQ(2u, storage.GetNumberOfProfiles());
217 363
218 // Delete all profiles. 364 // Delete all profiles.
365 MultipleProfileDeletionObserver profile_deletion_observer(2u, 2u);
219 std::vector<ProfileAttributesEntry*> entries = 366 std::vector<ProfileAttributesEntry*> entries =
220 storage.GetAllProfilesAttributes(); 367 storage.GetAllProfilesAttributes();
221 std::vector<base::FilePath> old_profile_paths; 368 std::vector<base::FilePath> old_profile_paths;
222 for (ProfileAttributesEntry* entry : entries) { 369 for (ProfileAttributesEntry* entry : entries) {
223 base::FilePath profile_path = entry->GetPath(); 370 base::FilePath profile_path = entry->GetPath();
224 EXPECT_FALSE(profile_path.empty()); 371 EXPECT_FALSE(profile_path.empty());
225 profile_manager->ScheduleProfileForDeletion( 372 profile_manager->ScheduleProfileForDeletion(
226 profile_path, ProfileManager::CreateCallback()); 373 profile_path, profile_deletion_observer.OnProfileCreatedClosure());
227 old_profile_paths.push_back(profile_path); 374 old_profile_paths.push_back(profile_path);
228 } 375 }
229 376 profile_deletion_observer.Wait();
230 // Spin things so deletion can take place.
231 content::RunAllPendingInMessageLoop();
232 377
233 // Make sure a new profile was created automatically. 378 // Make sure a new profile was created automatically.
234 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 379 EXPECT_EQ(1u, storage.GetNumberOfProfiles());
235 base::FilePath new_profile_path = 380 base::FilePath new_profile_path =
236 storage.GetAllProfilesAttributes().front()->GetPath(); 381 storage.GetAllProfilesAttributes().front()->GetPath();
237 for (const base::FilePath& old_profile_path : old_profile_paths) 382 for (const base::FilePath& old_profile_path : old_profile_paths)
238 EXPECT_NE(old_profile_path, new_profile_path); 383 EXPECT_NE(old_profile_path, new_profile_path);
239 384
240 // Make sure that last used profile preference is set correctly. 385 // Make sure that last used profile preference is set correctly.
241 Profile* last_used = ProfileManager::GetLastUsedProfile(); 386 Profile* last_used = ProfileManager::GetLastUsedProfile();
242 EXPECT_EQ(new_profile_path, last_used->GetPath()); 387 EXPECT_EQ(new_profile_path, last_used->GetPath());
243 } 388 }
244 #endif // OS_MACOSX 389 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
245 390
246 #if defined(OS_CHROMEOS) 391 #if defined(OS_CHROMEOS)
247 392
248 class ProfileManagerCrOSBrowserTest : public ProfileManagerBrowserTest { 393 class ProfileManagerCrOSBrowserTest : public ProfileManagerBrowserTest {
249 protected: 394 protected:
250 void SetUpCommandLine(base::CommandLine* command_line) override { 395 void SetUpCommandLine(base::CommandLine* command_line) override {
251 // Use a user hash other than the default chrome::kTestUserProfileDir 396 // Use a user hash other than the default chrome::kTestUserProfileDir
252 // so that the prefix case is tested. 397 // so that the prefix case is tested.
253 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, 398 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile,
254 "test-user-hash"); 399 "test-user-hash");
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 645
501 EXPECT_FALSE(profile->HasOffTheRecordProfile()); 646 EXPECT_FALSE(profile->HasOffTheRecordProfile());
502 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile)); 647 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile));
503 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles()); 648 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles());
504 // After destroying the incognito profile incognito preferences should be 649 // After destroying the incognito profile incognito preferences should be
505 // cleared so the default save path should be taken from the main profile. 650 // cleared so the default save path should be taken from the main profile.
506 EXPECT_FALSE(profile->GetOffTheRecordPrefs() 651 EXPECT_FALSE(profile->GetOffTheRecordPrefs()
507 ->GetFilePath(prefs::kSaveFileDefaultDirectory) 652 ->GetFilePath(prefs::kSaveFileDefaultDirectory)
508 .empty()); 653 .empty());
509 } 654 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/webui/profile_helper_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698