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/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 #include "chrome/browser/chromeos/login/user_manager.h" | 44 #include "chrome/browser/chromeos/login/user_manager.h" |
45 #include "chrome/browser/chromeos/settings/cros_settings.h" | 45 #include "chrome/browser/chromeos/settings/cros_settings.h" |
46 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 46 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
47 #include "chromeos/chromeos_switches.h" | 47 #include "chromeos/chromeos_switches.h" |
48 #endif | 48 #endif |
49 | 49 |
50 using content::BrowserThread; | 50 using content::BrowserThread; |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 void CheckLastUsedProfile(const base::FilePath& profile_path, | |
55 const std::string& profile_name) { | |
56 EXPECT_EQ( | |
57 g_browser_process->profile_manager()->GetLastUsedProfile()->GetPath(), | |
58 profile_path); | |
59 EXPECT_EQ( | |
60 g_browser_process->local_state()->GetString(prefs::kProfileLastUsed), | |
61 profile_name); | |
62 } | |
63 | |
64 // This class listens to the ProfileInfoCache for profile deletions | |
65 // and then runs a callback, which checks that the correct | |
66 // next active profile is set. | |
67 class ProfileRemovalObserver : public ProfileInfoCacheObserver { | |
68 public: | |
69 ProfileRemovalObserver( | |
70 ProfileManager* profile_manager, | |
71 base::Callback<void(void)> cb) | |
72 : profile_manager_(profile_manager), | |
73 callback_(cb) { | |
74 profile_manager_->GetProfileInfoCache().AddObserver(this); | |
75 } | |
76 | |
77 virtual ~ProfileRemovalObserver() { | |
78 profile_manager_->GetProfileInfoCache().RemoveObserver(this); | |
79 } | |
80 private: | |
81 // ProfileInfoCacheObserver implementation: | |
82 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE {} | |
83 virtual void OnProfileWasRemoved(const base::FilePath& profile_path, | |
84 const string16& profile_name) OVERRIDE { | |
85 callback_.Run(); | |
86 } | |
87 virtual void OnProfileWillBeRemoved( | |
88 const base::FilePath& profile_path) OVERRIDE {} | |
89 virtual void OnProfileNameChanged( | |
90 const base::FilePath& profile_path, const string16& old_profile_name) | |
91 OVERRIDE {} | |
92 virtual void OnProfileAvatarChanged( | |
93 const base::FilePath& profile_path) OVERRIDE {} | |
94 | |
95 ProfileManager* profile_manager_; | |
96 base::Callback<void(void)> callback_; | |
97 DISALLOW_COPY_AND_ASSIGN(ProfileRemovalObserver); | |
98 }; | |
99 | |
54 // This global variable is used to check that value returned to different | 100 // This global variable is used to check that value returned to different |
55 // observers is the same. | 101 // observers is the same. |
56 Profile* g_created_profile; | 102 Profile* g_created_profile; |
57 | 103 |
58 } // namespace | 104 } // namespace |
59 | 105 |
60 namespace testing { | 106 namespace testing { |
61 | 107 |
62 class ProfileManager : public ::ProfileManagerWithoutInit { | 108 class ProfileManager : public ::ProfileManagerWithoutInit { |
63 public: | 109 public: |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 EXPECT_FALSE( | 489 EXPECT_FALSE( |
444 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); | 490 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
445 | 491 |
446 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when | 492 // GetLastUsedProfileAllowedByPolicy() returns the incognito Profile when |
447 // incognito mode is forced. | 493 // incognito mode is forced. |
448 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); | 494 IncognitoModePrefs::SetAvailability(prefs, IncognitoModePrefs::FORCED); |
449 EXPECT_TRUE( | 495 EXPECT_TRUE( |
450 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); | 496 profile_manager->GetLastUsedProfileAllowedByPolicy()->IsOffTheRecord()); |
451 } | 497 } |
452 | 498 |
499 TEST_F(ProfileManagerTest, ActiveProfileDeleted) { | |
500 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
501 ASSERT_TRUE(profile_manager); | |
502 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | |
Alexei Svitkine (slow)
2013/06/12 21:42:23
Either make it const or a pointer.
Style guide di
noms (inactive)
2013/06/13 20:56:25
Done.
| |
503 | |
504 // Create and load two profiles. | |
505 base::FilePath dest_path1 = | |
506 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | |
507 base::FilePath dest_path2 = | |
508 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | |
509 | |
510 MockObserver mock_observer; | |
511 EXPECT_CALL(mock_observer, OnProfileCreated( | |
512 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); | |
513 | |
514 profile_manager->CreateProfileAsync(dest_path1, | |
515 base::Bind(&MockObserver::OnProfileCreated, | |
516 base::Unretained(&mock_observer)), | |
517 string16(), string16(), false); | |
Alexei Svitkine (slow)
2013/06/12 21:42:23
Can you refactor this block into a helper function
noms (inactive)
2013/06/13 20:56:25
Done.
| |
518 profile_manager->CreateProfileAsync(dest_path2, | |
519 base::Bind(&MockObserver::OnProfileCreated, | |
520 base::Unretained(&mock_observer)), | |
521 string16(), string16(), false); | |
522 | |
523 message_loop_.RunUntilIdle(); | |
524 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); | |
525 EXPECT_EQ(2u, cache.GetNumberOfProfiles()); | |
526 | |
527 // Set the active profile. | |
528 PrefService* local_state = g_browser_process->local_state(); | |
529 local_state->SetString(prefs::kProfileLastUsed, | |
530 dest_path1.BaseName().MaybeAsASCII()); | |
531 | |
532 ProfileRemovalObserver observer( | |
533 profile_manager, | |
534 base::Bind(&CheckLastUsedProfile, dest_path2, "New Profile 2")); | |
Alexei Svitkine (slow)
2013/06/12 21:42:23
Do you need this class?
Can't you just do the cal
noms (inactive)
2013/06/13 20:56:25
Done.
| |
535 | |
536 // Delete the active profile. The observer will check the next active profile | |
537 // is set correctly. | |
538 profile_manager->ScheduleProfileForDeletion(dest_path1, | |
539 ProfileManager::CreateCallback()); | |
540 // Spin the message loop so that all the callbacks can finish running. | |
541 message_loop_.RunUntilIdle(); | |
542 } | |
543 | |
544 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { | |
545 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
546 ASSERT_TRUE(profile_manager); | |
547 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | |
548 | |
549 // Create and load one profile, and just create a second profile. | |
550 base::FilePath dest_path1 = | |
551 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | |
552 base::FilePath dest_path2 = | |
553 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | |
554 | |
555 MockObserver mock_observer; | |
556 EXPECT_CALL(mock_observer, OnProfileCreated( | |
557 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); | |
558 | |
559 profile_manager->CreateProfileAsync(dest_path1, | |
560 base::Bind(&MockObserver::OnProfileCreated, | |
561 base::Unretained(&mock_observer)), | |
562 string16(), string16(), false); | |
563 | |
564 // Track the profile, but don't load it. | |
565 cache.AddProfileToCache(dest_path2, ASCIIToUTF16("New Profile 2"), | |
566 string16(), 0, false); | |
567 | |
568 message_loop_.RunUntilIdle(); | |
569 | |
570 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); | |
571 EXPECT_EQ(2u, cache.GetNumberOfProfiles()); | |
572 | |
573 // Set the active profile. | |
574 PrefService* local_state = g_browser_process->local_state(); | |
575 local_state->SetString(prefs::kProfileLastUsed, | |
576 dest_path1.BaseName().MaybeAsASCII()); | |
577 | |
578 ProfileRemovalObserver observer( | |
579 profile_manager, | |
580 base::Bind(&CheckLastUsedProfile, dest_path2, "New Profile 2")); | |
581 | |
582 // Delete the active profile. This should switch and load the unloaded | |
583 // profile. The observer will check the next active profile | |
584 // is set correctly. | |
585 profile_manager->ScheduleProfileForDeletion(dest_path1, | |
586 ProfileManager::CreateCallback()); | |
587 | |
588 // Spin the message loop so that all the callbacks can finish running. | |
589 message_loop_.RunUntilIdle(); | |
590 } | |
Alexei Svitkine (slow)
2013/06/12 21:42:23
Can you also add a test that exercises the "re-try
| |
591 | |
453 #if !defined(OS_ANDROID) | 592 #if !defined(OS_ANDROID) |
454 // There's no Browser object on Android. | 593 // There's no Browser object on Android. |
455 TEST_F(ProfileManagerTest, LastOpenedProfiles) { | 594 TEST_F(ProfileManagerTest, LastOpenedProfiles) { |
456 base::FilePath dest_path1 = temp_dir_.path(); | 595 base::FilePath dest_path1 = temp_dir_.path(); |
457 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 596 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
458 | 597 |
459 base::FilePath dest_path2 = temp_dir_.path(); | 598 base::FilePath dest_path2 = temp_dir_.path(); |
460 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 599 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
461 | 600 |
462 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 601 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
633 browser2b.reset(); | 772 browser2b.reset(); |
634 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 773 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
635 ASSERT_EQ(1U, last_opened_profiles.size()); | 774 ASSERT_EQ(1U, last_opened_profiles.size()); |
636 EXPECT_EQ(profile1, last_opened_profiles[0]); | 775 EXPECT_EQ(profile1, last_opened_profiles[0]); |
637 | 776 |
638 browser1.reset(); | 777 browser1.reset(); |
639 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); | 778 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); |
640 ASSERT_EQ(0U, last_opened_profiles.size()); | 779 ASSERT_EQ(0U, last_opened_profiles.size()); |
641 } | 780 } |
642 #endif // !defined(OS_ANDROID) | 781 #endif // !defined(OS_ANDROID) |
OLD | NEW |