[email protected] | ebbbb9f | 2011-03-09 13:16:14 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d99bcaa | 2010-12-05 20:15:56 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 7 | #include "base/command_line.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 9 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 11 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
| 13 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 14 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "chrome/common/chrome_paths.h" |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 16 | #include "chrome/common/chrome_switches.h" |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 17 | #include "chrome/test/testing_browser_process.h" |
| 18 | #include "chrome/test/testing_pref_service.h" |
[email protected] | 1bda9755 | 2011-03-01 20:11:52 | [diff] [blame] | 19 | #include "content/browser/browser_thread.h" |
[email protected] | ebbbb9f | 2011-03-09 13:16:14 | [diff] [blame] | 20 | #include "content/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 4ae61df | 2011-01-19 15:29:47 | [diff] [blame] | 22 | #include "ui/base/system_monitor/system_monitor.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 25 | protected: |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 26 | ProfileManagerTest() |
| 27 | : ui_thread_(BrowserThread::UI, &message_loop_), |
| 28 | file_thread_(BrowserThread::FILE, &message_loop_) { |
[email protected] | cb0e4f1 | 2009-12-03 00:09:09 | [diff] [blame] | 29 | } |
| 30 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | virtual void SetUp() { |
| 32 | // Name a subdirectory of the temp directory. |
| 33 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 34 | test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
| 36 | // Create a fresh, empty copy of this directory. |
| 37 | file_util::Delete(test_dir_, true); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 38 | file_util::CreateDirectory(test_dir_); |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 39 | |
| 40 | // Create a local_state PrefService. |
| 41 | browser::RegisterLocalState(&test_local_state_); |
| 42 | TestingBrowserProcess* testing_browser_process = |
| 43 | static_cast<TestingBrowserProcess*>(g_browser_process); |
| 44 | testing_browser_process->SetPrefService(&test_local_state_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | } |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 46 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | virtual void TearDown() { |
| 48 | // Clean up test directory |
| 49 | ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
| 50 | ASSERT_FALSE(file_util::PathExists(test_dir_)); |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 51 | |
| 52 | TestingBrowserProcess* testing_browser_process = |
| 53 | static_cast<TestingBrowserProcess*>(g_browser_process); |
| 54 | testing_browser_process->SetPrefService(NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 56 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 57 | MessageLoopForUI message_loop_; |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 58 | BrowserThread ui_thread_; |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 59 | BrowserThread file_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | |
| 61 | // the path to temporary directory used to contain the test operations |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 62 | FilePath test_dir_; |
[email protected] | 81ad7f4a | 2011-03-16 01:33:29 | [diff] [blame^] | 63 | |
| 64 | TestingPrefService test_local_state_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | }; |
| 66 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | TEST_F(ProfileManagerTest, CreateProfile) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 68 | FilePath source_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | PathService::Get(chrome::DIR_TEST_DATA, &source_path); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 70 | source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); |
| 71 | source_path = source_path.Append(FILE_PATH_LITERAL("sample")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 73 | FilePath dest_path = test_dir_; |
| 74 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
| 76 | scoped_ptr<Profile> profile; |
| 77 | |
| 78 | // Successfully create a profile. |
[email protected] | 9acc4860 | 2009-11-30 21:18:51 | [diff] [blame] | 79 | profile.reset(ProfileManager::CreateProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | ASSERT_TRUE(profile.get()); |
| 81 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | profile.reset(); |
| 83 | |
| 84 | #ifdef NDEBUG |
| 85 | // In Release mode, we always try to always return a profile. In debug, |
| 86 | // these cases would trigger DCHECKs. |
| 87 | |
| 88 | // The profile already exists when we call CreateProfile. Just load it. |
[email protected] | 9acc4860 | 2009-11-30 21:18:51 | [diff] [blame] | 89 | profile.reset(ProfileManager::CreateProfile(dest_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | ASSERT_TRUE(profile.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | #endif |
| 92 | } |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 93 | |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 94 | TEST_F(ProfileManagerTest, DefaultProfileDir) { |
| 95 | CommandLine *cl = CommandLine::ForCurrentProcess(); |
[email protected] | 4ae61df | 2011-01-19 15:29:47 | [diff] [blame] | 96 | ui::SystemMonitor dummy; |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 97 | ProfileManager profile_manager; |
| 98 | std::string profile_dir("my_user"); |
| 99 | |
| 100 | cl->AppendSwitch(switches::kTestType); |
| 101 | |
| 102 | FilePath expected_default = |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 103 | FilePath().AppendASCII(chrome::kNotSignedInProfile); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 104 | EXPECT_EQ(expected_default.value(), |
| 105 | profile_manager.GetCurrentProfileDir().value()); |
| 106 | } |
| 107 | |
| 108 | #if defined(OS_CHROMEOS) |
| 109 | // This functionality only exists on Chrome OS. |
| 110 | TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 111 | CommandLine *cl = CommandLine::ForCurrentProcess(); |
[email protected] | 4ae61df | 2011-01-19 15:29:47 | [diff] [blame] | 112 | ui::SystemMonitor dummy; |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 113 | ProfileManager profile_manager; |
| 114 | std::string profile_dir("my_user"); |
| 115 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 116 | cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 117 | cl->AppendSwitch(switches::kTestType); |
| 118 | |
| 119 | FilePath expected_default = |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 120 | FilePath().AppendASCII(chrome::kNotSignedInProfile); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 121 | EXPECT_EQ(expected_default.value(), |
| 122 | profile_manager.GetCurrentProfileDir().value()); |
| 123 | |
| 124 | profile_manager.Observe(NotificationType::LOGIN_USER_CHANGED, |
| 125 | NotificationService::AllSources(), |
| 126 | NotificationService::NoDetails()); |
| 127 | FilePath expected_logged_in(profile_dir); |
| 128 | EXPECT_EQ(expected_logged_in.value(), |
| 129 | profile_manager.GetCurrentProfileDir().value()); |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 130 | VLOG(1) << test_dir_.Append(profile_manager.GetCurrentProfileDir()).value(); |
[email protected] | 943aed554 | 2010-06-11 15:57:08 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #endif |
| 134 | |
[email protected] | 52d69b88 | 2010-06-17 17:35:51 | [diff] [blame] | 135 | TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 136 | FilePath source_path; |
| 137 | PathService::Get(chrome::DIR_TEST_DATA, &source_path); |
| 138 | source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); |
| 139 | source_path = source_path.Append(FILE_PATH_LITERAL("sample")); |
| 140 | |
| 141 | FilePath dest_path1 = test_dir_; |
| 142 | dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 143 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 144 | FilePath dest_path2 = test_dir_; |
| 145 | dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 146 | |
| 147 | scoped_ptr<Profile> profile1; |
| 148 | scoped_ptr<Profile> profile2; |
| 149 | |
| 150 | // Successfully create the profiles. |
[email protected] | 9acc4860 | 2009-11-30 21:18:51 | [diff] [blame] | 151 | profile1.reset(ProfileManager::CreateProfile(dest_path1)); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 152 | ASSERT_TRUE(profile1.get()); |
| 153 | |
[email protected] | 9acc4860 | 2009-11-30 21:18:51 | [diff] [blame] | 154 | profile2.reset(ProfileManager::CreateProfile(dest_path2)); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 155 | ASSERT_TRUE(profile2.get()); |
| 156 | |
| 157 | // Force lazy-init of some profile services to simulate use. |
| 158 | EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 159 | EXPECT_TRUE(profile1->GetBookmarkModel()); |
| 160 | EXPECT_TRUE(profile2->GetBookmarkModel()); |
| 161 | EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 162 | |
| 163 | // Make sure any pending tasks run before we destroy the profiles. |
| 164 | message_loop_.RunAllPending(); |
| 165 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 166 | profile1.reset(); |
| 167 | profile2.reset(); |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 168 | |
[email protected] | 237e6d0 | 2010-11-08 21:45:42 | [diff] [blame] | 169 | // Make sure history cleans up correctly. |
| 170 | message_loop_.RunAllPending(); |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 171 | } |