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

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

Issue 14923004: [Mac] AppController needs to update its "last profile" pointer when the active profile is deleted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: derp. no test on CrOS either. Created 7 years, 5 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 <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"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h" 17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "chrome/browser/extensions/event_router_forwarder.h"
19 #include "chrome/browser/history/history_service.h" 18 #include "chrome/browser/history/history_service.h"
20 #include "chrome/browser/history/history_service_factory.h" 19 #include "chrome/browser/history/history_service_factory.h"
21 #include "chrome/browser/io_thread.h" 20 #include "chrome/browser/io_thread.h"
22 #include "chrome/browser/prefs/browser_prefs.h" 21 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/browser/prefs/incognito_mode_prefs.h" 22 #include "chrome/browser/prefs/incognito_mode_prefs.h"
24 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_info_cache.h" 24 #include "chrome/browser/profiles/profile_info_cache.h"
26 #include "chrome/browser/profiles/profile_manager.h" 25 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/ui/browser.h" 26 #include "chrome/browser/ui/browser.h"
28 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 class ProfileManagerTest : public testing::Test { 89 class ProfileManagerTest : public testing::Test {
91 protected: 90 protected:
92 class MockObserver { 91 class MockObserver {
93 public: 92 public:
94 MOCK_METHOD2(OnProfileCreated, 93 MOCK_METHOD2(OnProfileCreated,
95 void(Profile* profile, Profile::CreateStatus status)); 94 void(Profile* profile, Profile::CreateStatus status));
96 }; 95 };
97 96
98 ProfileManagerTest() 97 ProfileManagerTest()
99 : local_state_(TestingBrowserProcess::GetGlobal()), 98 : local_state_(TestingBrowserProcess::GetGlobal()),
100 extension_event_router_forwarder_(new extensions::EventRouterForwarder),
101 ui_thread_(BrowserThread::UI, &message_loop_), 99 ui_thread_(BrowserThread::UI, &message_loop_),
102 db_thread_(BrowserThread::DB, &message_loop_), 100 db_thread_(BrowserThread::DB, &message_loop_),
103 file_thread_(BrowserThread::FILE, &message_loop_) { 101 file_thread_(BrowserThread::FILE, &message_loop_) {
104 } 102 }
105 103
106 virtual void SetUp() { 104 virtual void SetUp() {
107 // Create a new temporary directory, and store the path 105 // Create a new temporary directory, and store the path
108 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
109 TestingBrowserProcess::GetGlobal()->SetProfileManager( 107 TestingBrowserProcess::GetGlobal()->SetProfileManager(
110 new testing::ProfileManager(temp_dir_.path())); 108 new testing::ProfileManager(temp_dir_.path()));
111 109
112 #if defined(OS_CHROMEOS) 110 #if defined(OS_CHROMEOS)
113 CommandLine* cl = CommandLine::ForCurrentProcess(); 111 CommandLine* cl = CommandLine::ForCurrentProcess();
114 cl->AppendSwitch(switches::kTestType); 112 cl->AppendSwitch(switches::kTestType);
115 #endif 113 #endif
116 } 114 }
117 115
118 virtual void TearDown() { 116 virtual void TearDown() {
119 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); 117 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
120 message_loop_.RunUntilIdle(); 118 message_loop_.RunUntilIdle();
121 } 119 }
122 120
121 // Helper function to create a profile with |name| for a profile |manager|.
122 void CreateProfileAsync(ProfileManager* manager,
123 const std::string& name,
124 MockObserver* mock_observer) {
125 manager->CreateProfileAsync(
126 temp_dir_.path().AppendASCII(name),
127 base::Bind(&MockObserver::OnProfileCreated,
128 base::Unretained(mock_observer)),
129 UTF8ToUTF16(name),
130 string16(),
131 false);
132 }
133
123 #if defined(OS_CHROMEOS) 134 #if defined(OS_CHROMEOS)
124 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 135 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
125 chromeos::ScopedTestCrosSettings test_cros_settings_; 136 chromeos::ScopedTestCrosSettings test_cros_settings_;
126 #endif 137 #endif
127 138
128 // The path to temporary directory used to contain the test operations. 139 // The path to temporary directory used to contain the test operations.
129 base::ScopedTempDir temp_dir_; 140 base::ScopedTempDir temp_dir_;
130 ScopedTestingLocalState local_state_; 141 ScopedTestingLocalState local_state_;
131 scoped_refptr<extensions::EventRouterForwarder>
132 extension_event_router_forwarder_;
133
134 base::MessageLoopForUI message_loop_; 142 base::MessageLoopForUI message_loop_;
135 content::TestBrowserThread ui_thread_; 143 content::TestBrowserThread ui_thread_;
136 content::TestBrowserThread db_thread_; 144 content::TestBrowserThread db_thread_;
137 content::TestBrowserThread file_thread_; 145 content::TestBrowserThread file_thread_;
138 146
139 #if defined(OS_CHROMEOS) 147 #if defined(OS_CHROMEOS)
140 chromeos::ScopedTestUserManager test_user_manager_; 148 chromeos::ScopedTestUserManager test_user_manager_;
141 #endif 149 #endif
142 }; 150 };
143 151
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 } 241 }
234 242
235 MATCHER(NotFail, "Profile creation failure status is not reported.") { 243 MATCHER(NotFail, "Profile creation failure status is not reported.") {
236 return arg == Profile::CREATE_STATUS_CREATED || 244 return arg == Profile::CREATE_STATUS_CREATED ||
237 arg == Profile::CREATE_STATUS_INITIALIZED; 245 arg == Profile::CREATE_STATUS_INITIALIZED;
238 } 246 }
239 247
240 // Tests asynchronous profile creation mechanism. 248 // Tests asynchronous profile creation mechanism.
241 // Crashes: http://crbug.com/89421 249 // Crashes: http://crbug.com/89421
242 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { 250 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) {
243 base::FilePath dest_path =
244 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile"));
245
246 MockObserver mock_observer; 251 MockObserver mock_observer;
247 EXPECT_CALL(mock_observer, OnProfileCreated( 252 EXPECT_CALL(mock_observer, OnProfileCreated(
248 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); 253 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
249 254
250 g_browser_process->profile_manager()->CreateProfileAsync(dest_path, 255 CreateProfileAsync(g_browser_process->profile_manager(),
251 base::Bind(&MockObserver::OnProfileCreated, 256 "New Profile", &mock_observer);
252 base::Unretained(&mock_observer)),
253 string16(), string16(), false);
254 257
255 message_loop_.RunUntilIdle(); 258 message_loop_.RunUntilIdle();
256 } 259 }
257 260
258 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { 261 MATCHER(SameNotNull, "The same non-NULL value for all calls.") {
259 if (!g_created_profile) 262 if (!g_created_profile)
260 g_created_profile = arg; 263 g_created_profile = arg;
261 return arg != NULL && arg == g_created_profile; 264 return arg != NULL && arg == g_created_profile;
262 } 265 }
263 266
264 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { 267 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) {
265 base::FilePath dest_path =
266 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile"));
267
268 g_created_profile = NULL; 268 g_created_profile = NULL;
269 269
270 MockObserver mock_observer1; 270 MockObserver mock_observer1;
271 EXPECT_CALL(mock_observer1, OnProfileCreated( 271 EXPECT_CALL(mock_observer1, OnProfileCreated(
272 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 272 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
273 MockObserver mock_observer2; 273 MockObserver mock_observer2;
274 EXPECT_CALL(mock_observer2, OnProfileCreated( 274 EXPECT_CALL(mock_observer2, OnProfileCreated(
275 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 275 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
276 MockObserver mock_observer3; 276 MockObserver mock_observer3;
277 EXPECT_CALL(mock_observer3, OnProfileCreated( 277 EXPECT_CALL(mock_observer3, OnProfileCreated(
278 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); 278 SameNotNull(), NotFail())).Times(testing::AtLeast(1));
279 279
280 ProfileManager* profile_manager = g_browser_process->profile_manager(); 280 ProfileManager* profile_manager = g_browser_process->profile_manager();
281 281 const std::string profile_name = "New Profile";
282 profile_manager->CreateProfileAsync(dest_path, 282 CreateProfileAsync(profile_manager, profile_name, &mock_observer1);
283 base::Bind(&MockObserver::OnProfileCreated, 283 CreateProfileAsync(profile_manager, profile_name, &mock_observer2);
284 base::Unretained(&mock_observer1)), 284 CreateProfileAsync(profile_manager, profile_name, &mock_observer3);
285 string16(), string16(), false);
286 profile_manager->CreateProfileAsync(dest_path,
287 base::Bind(&MockObserver::OnProfileCreated,
288 base::Unretained(&mock_observer2)),
289 string16(), string16(), false);
290 profile_manager->CreateProfileAsync(dest_path,
291 base::Bind(&MockObserver::OnProfileCreated,
292 base::Unretained(&mock_observer3)),
293 string16(), string16(), false);
294 285
295 message_loop_.RunUntilIdle(); 286 message_loop_.RunUntilIdle();
296 } 287 }
297 288
298 TEST_F(ProfileManagerTest, CreateProfilesAsync) { 289 TEST_F(ProfileManagerTest, CreateProfilesAsync) {
299 base::FilePath dest_path1 = 290 const std::string profile_name1 = "New Profile 1";
300 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); 291 const std::string profile_name2 = "New Profile 2";
301 base::FilePath dest_path2 =
302 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2"));
303 292
304 MockObserver mock_observer; 293 MockObserver mock_observer;
305 EXPECT_CALL(mock_observer, OnProfileCreated( 294 EXPECT_CALL(mock_observer, OnProfileCreated(
306 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 295 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
307 296
308 ProfileManager* profile_manager = g_browser_process->profile_manager(); 297 ProfileManager* profile_manager = g_browser_process->profile_manager();
309 298
310 profile_manager->CreateProfileAsync(dest_path1, 299 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
311 base::Bind(&MockObserver::OnProfileCreated, 300 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
312 base::Unretained(&mock_observer)),
313 string16(), string16(), false);
314 profile_manager->CreateProfileAsync(dest_path2,
315 base::Bind(&MockObserver::OnProfileCreated,
316 base::Unretained(&mock_observer)),
317 string16(), string16(), false);
318 301
319 message_loop_.RunUntilIdle(); 302 message_loop_.RunUntilIdle();
320 } 303 }
321 304
322 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { 305 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
323 ProfileManager* profile_manager = g_browser_process->profile_manager(); 306 ProfileManager* profile_manager = g_browser_process->profile_manager();
324 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 307 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
325 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, 308 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
326 Value::CreateBooleanValue(true)); 309 Value::CreateBooleanValue(true));
327 310
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 browser2b.reset(); 610 browser2b.reset();
628 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 611 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
629 ASSERT_EQ(1U, last_opened_profiles.size()); 612 ASSERT_EQ(1U, last_opened_profiles.size());
630 EXPECT_EQ(profile1, last_opened_profiles[0]); 613 EXPECT_EQ(profile1, last_opened_profiles[0]);
631 614
632 browser1.reset(); 615 browser1.reset();
633 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 616 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
634 ASSERT_EQ(0U, last_opened_profiles.size()); 617 ASSERT_EQ(0U, last_opened_profiles.size());
635 } 618 }
636 #endif // !defined(OS_ANDROID) 619 #endif // !defined(OS_ANDROID)
620
621 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
622 // There's no Browser object on Android and there's no multi-profiles on Chrome.
623 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
624 ProfileManager* profile_manager = g_browser_process->profile_manager();
625 ASSERT_TRUE(profile_manager);
626
627 // Create and load two profiles.
628 const std::string profile_name1 = "New Profile 1";
629 const std::string profile_name2 = "New Profile 2";
630 base::FilePath dest_path1 =
631 temp_dir_.path().AppendASCII(profile_name1);
632 base::FilePath dest_path2 =
633 temp_dir_.path().AppendASCII(profile_name2);
634
635 MockObserver mock_observer;
636 EXPECT_CALL(mock_observer, OnProfileCreated(
637 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
638
639 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
640 CreateProfileAsync(profile_manager, profile_name2, &mock_observer);
641 message_loop_.RunUntilIdle();
642
643 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
644 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
645
646 // Set the active profile.
647 PrefService* local_state = g_browser_process->local_state();
648 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
649
650 // Delete the active profile.
651 profile_manager->ScheduleProfileForDeletion(dest_path1,
652 ProfileManager::CreateCallback());
653 // Spin the message loop so that all the callbacks can finish running.
654 message_loop_.RunUntilIdle();
655
656 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
657 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
658 }
659 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
660
661 #if defined(OS_MACOSX)
662 // These tests are for a Mac-only code path that assumes the browser
663 // process isn't killed when all browser windows are closed.
664 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
665 ProfileManager* profile_manager = g_browser_process->profile_manager();
666 ASSERT_TRUE(profile_manager);
667
668 // Create and load one profile, and just create a second profile.
669 const std::string profile_name1 = "New Profile 1";
670 const std::string profile_name2 = "New Profile 2";
671 base::FilePath dest_path1 =
672 temp_dir_.path().AppendASCII(profile_name1);
673 base::FilePath dest_path2 =
674 temp_dir_.path().AppendASCII(profile_name2);
675
676 MockObserver mock_observer;
677 EXPECT_CALL(mock_observer, OnProfileCreated(
678 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
679 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
680 message_loop_.RunUntilIdle();
681
682 // Track the profile, but don't load it.
683 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
684 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
685 string16(), 0, false);
686 message_loop_.RunUntilIdle();
687
688 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
689 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
690
691 // Set the active profile.
692 PrefService* local_state = g_browser_process->local_state();
693 local_state->SetString(prefs::kProfileLastUsed,
694 dest_path1.BaseName().MaybeAsASCII());
695
696 // Delete the active profile. This should switch and load the unloaded
697 // profile.
698 profile_manager->ScheduleProfileForDeletion(dest_path1,
699 ProfileManager::CreateCallback());
700
701 // Spin the message loop so that all the callbacks can finish running.
702 message_loop_.RunUntilIdle();
703
704 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
705 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
706 }
707
708 // This tests the recursive call in ProfileManager::OnNewActiveProfileLoaded
709 // by simulating a scenario in which the profile that is being loaded as
710 // the next active profile has also been marked for deletion, so the
711 // ProfileManager needs to recursively select a different next profile.
712 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
713 ProfileManager* profile_manager = g_browser_process->profile_manager();
714 ASSERT_TRUE(profile_manager);
715
716 // Create and load one profile, and create two more profiles.
717 const std::string profile_name1 = "New Profile 1";
718 const std::string profile_name2 = "New Profile 2";
719 const std::string profile_name3 = "New Profile 3";
720 base::FilePath dest_path1 =
721 temp_dir_.path().AppendASCII(profile_name1);
722 base::FilePath dest_path2 =
723 temp_dir_.path().AppendASCII(profile_name2);
724 base::FilePath dest_path3 =
725 temp_dir_.path().AppendASCII(profile_name3);
726
727 MockObserver mock_observer;
728 EXPECT_CALL(mock_observer, OnProfileCreated(
729 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
730 CreateProfileAsync(profile_manager, profile_name1, &mock_observer);
731 message_loop_.RunUntilIdle();
732
733 // Create the other profiles, but don't load them. Assign a fake avatar icon
734 // to ensure that profiles in the info cache are sorted by the profile name,
735 // and not randomly by the avatar name.
736 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
737 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
738 ASCIIToUTF16(profile_name2), 1, false);
739 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
740 ASCIIToUTF16(profile_name3), 2, false);
741
742 message_loop_.RunUntilIdle();
743
744 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
745 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
746
747 // Set the active profile.
748 PrefService* local_state = g_browser_process->local_state();
749 local_state->SetString(prefs::kProfileLastUsed,
750 dest_path1.BaseName().MaybeAsASCII());
751
752 // Delete the active profile, Profile1.
753 // This will post a CreateProfileAsync message, that tries to load Profile2,
754 // which checks that the profile is not being deleted, and then calls back
755 // FinishDeletingProfile for Profile1.
756 // Try to break this flow by setting the active profile to Profile2 in the
757 // middle (so after the first posted message), and trying to delete Profile2,
758 // so that the ProfileManager has to look for a different profile to load.
759 profile_manager->ScheduleProfileForDeletion(dest_path1,
760 ProfileManager::CreateCallback());
761 local_state->SetString(prefs::kProfileLastUsed,
762 dest_path2.BaseName().MaybeAsASCII());
763 profile_manager->ScheduleProfileForDeletion(dest_path2,
764 ProfileManager::CreateCallback());
765 // Spin the message loop so that all the callbacks can finish running.
766 message_loop_.RunUntilIdle();
767
768 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
769 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
770 }
771 #endif // !defined(OS_MACOSX)
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