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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 18 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 Delegate* delegate) override { | 87 Delegate* delegate) override { |
87 // This is safe while all file operations are done on the FILE thread. | 88 // This is safe while all file operations are done on the FILE thread. |
88 BrowserThread::PostTask( | 89 BrowserThread::PostTask( |
89 BrowserThread::FILE, FROM_HERE, | 90 BrowserThread::FILE, FROM_HERE, |
90 base::Bind(base::IgnoreResult(&base::CreateDirectory), path)); | 91 base::Bind(base::IgnoreResult(&base::CreateDirectory), path)); |
91 | 92 |
92 return new TestingProfile(path, this); | 93 return new TestingProfile(path, this); |
93 } | 94 } |
94 }; | 95 }; |
95 | 96 |
| 97 void ExpectNullProfile(base::Closure closure, Profile* profile) { |
| 98 EXPECT_EQ(nullptr, profile); |
| 99 closure.Run(); |
| 100 } |
| 101 |
| 102 void ExpectProfileWithName(const std::string& profile_name, |
| 103 bool incognito, |
| 104 base::Closure closure, |
| 105 Profile* profile) { |
| 106 EXPECT_NE(nullptr, profile); |
| 107 EXPECT_EQ(incognito, profile->IsOffTheRecord()); |
| 108 if (incognito) |
| 109 profile = profile->GetOriginalProfile(); |
| 110 |
| 111 // Create a profile on the fly so the the same comparison |
| 112 // can be used in Windows and other platforms. |
| 113 EXPECT_EQ(base::FilePath().AppendASCII(profile_name), |
| 114 profile->GetPath().BaseName()); |
| 115 closure.Run(); |
| 116 } |
| 117 |
96 } // namespace | 118 } // namespace |
97 | 119 |
98 class ProfileManagerTest : public testing::Test { | 120 class ProfileManagerTest : public testing::Test { |
99 protected: | 121 protected: |
100 class MockObserver { | 122 class MockObserver { |
101 public: | 123 public: |
102 MOCK_METHOD2(OnProfileCreated, | 124 MOCK_METHOD2(OnProfileCreated, |
103 void(Profile* profile, Profile::CreateStatus status)); | 125 void(Profile* profile, Profile::CreateStatus status)); |
104 }; | 126 }; |
105 | 127 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 308 |
287 // Make sure any pending tasks run before we destroy the profiles. | 309 // Make sure any pending tasks run before we destroy the profiles. |
288 base::RunLoop().RunUntilIdle(); | 310 base::RunLoop().RunUntilIdle(); |
289 | 311 |
290 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); | 312 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
291 | 313 |
292 // Make sure history cleans up correctly. | 314 // Make sure history cleans up correctly. |
293 base::RunLoop().RunUntilIdle(); | 315 base::RunLoop().RunUntilIdle(); |
294 } | 316 } |
295 | 317 |
| 318 TEST_F(ProfileManagerTest, LoadNonExistingProfile) { |
| 319 const std::string profile_name = "NonExistingProfile"; |
| 320 base::RunLoop run_loop_1; |
| 321 base::RunLoop run_loop_2; |
| 322 |
| 323 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 324 profile_manager->LoadProfile( |
| 325 profile_name, false /* incognito */, |
| 326 base::Bind(&ExpectNullProfile, run_loop_1.QuitClosure())); |
| 327 run_loop_1.Run(); |
| 328 |
| 329 profile_manager->LoadProfile( |
| 330 profile_name, true /* incognito */, |
| 331 base::Bind(&ExpectNullProfile, run_loop_2.QuitClosure())); |
| 332 run_loop_2.Run(); |
| 333 } |
| 334 |
| 335 TEST_F(ProfileManagerTest, LoadExistingProfile) { |
| 336 const std::string profile_name = "MyProfile"; |
| 337 const std::string other_name = "SomeOtherProfile"; |
| 338 MockObserver mock_observer1; |
| 339 EXPECT_CALL(mock_observer1, OnProfileCreated(SameNotNull(), NotFail())) |
| 340 .Times(testing::AtLeast(1)); |
| 341 |
| 342 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 343 CreateProfileAsync(profile_manager, profile_name, false, &mock_observer1); |
| 344 |
| 345 // Make sure a real profile is created before continuing. |
| 346 base::RunLoop().RunUntilIdle(); |
| 347 |
| 348 base::RunLoop load_profile; |
| 349 bool incognito = false; |
| 350 profile_manager->LoadProfile( |
| 351 profile_name, incognito, |
| 352 base::Bind(&ExpectProfileWithName, profile_name, incognito, |
| 353 load_profile.QuitClosure())); |
| 354 load_profile.Run(); |
| 355 |
| 356 base::RunLoop load_profile_incognito; |
| 357 incognito = true; |
| 358 profile_manager->LoadProfile( |
| 359 profile_name, incognito, |
| 360 base::Bind(&ExpectProfileWithName, profile_name, incognito, |
| 361 load_profile_incognito.QuitClosure())); |
| 362 load_profile_incognito.Run(); |
| 363 |
| 364 // Loading some other non existing profile should still return null. |
| 365 base::RunLoop load_other_profile; |
| 366 profile_manager->LoadProfile( |
| 367 other_name, false, |
| 368 base::Bind(&ExpectNullProfile, load_other_profile.QuitClosure())); |
| 369 load_other_profile.Run(); |
| 370 } |
| 371 |
296 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { | 372 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
297 g_created_profile = NULL; | 373 g_created_profile = NULL; |
298 | 374 |
299 MockObserver mock_observer1; | 375 MockObserver mock_observer1; |
300 EXPECT_CALL(mock_observer1, OnProfileCreated( | 376 EXPECT_CALL(mock_observer1, OnProfileCreated( |
301 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 377 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
302 MockObserver mock_observer2; | 378 MockObserver mock_observer2; |
303 EXPECT_CALL(mock_observer2, OnProfileCreated( | 379 EXPECT_CALL(mock_observer2, OnProfileCreated( |
304 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 380 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
305 MockObserver mock_observer3; | 381 MockObserver mock_observer3; |
(...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1339 dest_path2.BaseName().MaybeAsASCII()); | 1415 dest_path2.BaseName().MaybeAsASCII()); |
1340 profile_manager->ScheduleProfileForDeletion(dest_path2, | 1416 profile_manager->ScheduleProfileForDeletion(dest_path2, |
1341 ProfileManager::CreateCallback()); | 1417 ProfileManager::CreateCallback()); |
1342 // Spin the message loop so that all the callbacks can finish running. | 1418 // Spin the message loop so that all the callbacks can finish running. |
1343 base::RunLoop().RunUntilIdle(); | 1419 base::RunLoop().RunUntilIdle(); |
1344 | 1420 |
1345 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); | 1421 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); |
1346 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); | 1422 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); |
1347 } | 1423 } |
1348 #endif // !defined(OS_MACOSX) | 1424 #endif // !defined(OS_MACOSX) |
OLD | NEW |