Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 base::SystemMonitor::AllocateSystemIOPorts(); | 58 base::SystemMonitor::AllocateSystemIOPorts(); |
| 59 #endif | 59 #endif |
| 60 system_monitor_dummy_.reset(new base::SystemMonitor); | 60 system_monitor_dummy_.reset(new base::SystemMonitor); |
| 61 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( | 61 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( |
| 62 &io_thread_); | 62 &io_thread_); |
| 63 } | 63 } |
| 64 | 64 |
| 65 virtual void SetUp() { | 65 virtual void SetUp() { |
| 66 // Create a new temporary directory, and store the path | 66 // Create a new temporary directory, and store the path |
| 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 68 profile_manager_.reset(new ProfileManagerWithoutInit(temp_dir_.path())); | 68 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| 69 new ProfileManagerWithoutInit(temp_dir_.path())); | |
|
jwd
2011/12/02 02:35:43
This change, and the other related changes, will c
| |
| 69 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
| 70 // Force the ProfileInfoCache to be created immediately, so we can | 71 // Force the ProfileInfoCache to be created immediately, so we can |
| 71 // remove the shortcut manager for testing. | 72 // remove the shortcut manager for testing. |
| 72 profile_manager_->GetProfileInfoCache(); | 73 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 73 profile_manager_->RemoveProfileShortcutManagerForTesting(); | 74 profile_manager->GetProfileInfoCache(); |
| 75 profile_manager->RemoveProfileShortcutManagerForTesting(); | |
| 74 #endif | 76 #endif |
| 75 } | 77 } |
| 76 | 78 |
| 77 virtual void TearDown() { | 79 virtual void TearDown() { |
| 78 profile_manager_.reset(); | 80 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| 81 NULL); | |
| 79 message_loop_.RunAllPending(); | 82 message_loop_.RunAllPending(); |
| 80 } | 83 } |
| 81 | 84 |
| 82 class MockObserver : public ProfileManagerObserver { | 85 class MockObserver : public ProfileManagerObserver { |
| 83 public: | 86 public: |
| 84 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); | 87 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 #if defined(OS_CHROMEOS) | 90 #if defined(OS_CHROMEOS) |
| 88 // Do not change order of stub_cros_enabler_, which needs to be constructed | 91 // Do not change order of stub_cros_enabler_, which needs to be constructed |
| 89 // before io_thread_ which requires CrosLibrary to be initialized to construct | 92 // before io_thread_ which requires CrosLibrary to be initialized to construct |
| 90 // its data member pref_proxy_config_tracker_ on ChromeOS. | 93 // its data member pref_proxy_config_tracker_ on ChromeOS. |
| 91 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 94 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 92 #endif | 95 #endif |
| 93 | 96 |
| 94 // The path to temporary directory used to contain the test operations. | 97 // The path to temporary directory used to contain the test operations. |
| 95 ScopedTempDir temp_dir_; | 98 ScopedTempDir temp_dir_; |
| 96 ScopedTestingLocalState local_state_; | 99 ScopedTestingLocalState local_state_; |
| 97 scoped_refptr<ExtensionEventRouterForwarder> | 100 scoped_refptr<ExtensionEventRouterForwarder> |
| 98 extension_event_router_forwarder_; | 101 extension_event_router_forwarder_; |
| 99 | 102 |
| 100 MessageLoopForUI message_loop_; | 103 MessageLoopForUI message_loop_; |
| 101 content::TestBrowserThread ui_thread_; | 104 content::TestBrowserThread ui_thread_; |
| 102 content::TestBrowserThread db_thread_; | 105 content::TestBrowserThread db_thread_; |
| 103 content::TestBrowserThread file_thread_; | 106 content::TestBrowserThread file_thread_; |
| 104 IOThread io_thread_; | 107 IOThread io_thread_; |
| 105 | 108 |
| 106 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; | 109 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; |
| 107 | |
| 108 // Also will test profile deletion. | |
| 109 scoped_ptr<ProfileManager> profile_manager_; | |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 TEST_F(ProfileManagerTest, GetProfile) { | 112 TEST_F(ProfileManagerTest, GetProfile) { |
| 113 FilePath dest_path = temp_dir_.path(); | 113 FilePath dest_path = temp_dir_.path(); |
| 114 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | 114 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 115 | 115 |
| 116 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 117 | |
| 116 Profile* profile; | 118 Profile* profile; |
| 117 | 119 |
| 118 // Successfully create a profile. | 120 // Successfully create a profile. |
| 119 profile = profile_manager_->GetProfile(dest_path); | 121 profile = profile_manager->GetProfile(dest_path); |
| 120 EXPECT_TRUE(profile); | 122 EXPECT_TRUE(profile); |
| 121 | 123 |
| 122 // The profile already exists when we call GetProfile. Just load it. | 124 // The profile already exists when we call GetProfile. Just load it. |
| 123 EXPECT_EQ(profile, profile_manager_->GetProfile(dest_path)); | 125 EXPECT_EQ(profile, profile_manager->GetProfile(dest_path)); |
| 124 } | 126 } |
| 125 | 127 |
| 126 TEST_F(ProfileManagerTest, DefaultProfileDir) { | 128 TEST_F(ProfileManagerTest, DefaultProfileDir) { |
| 127 CommandLine *cl = CommandLine::ForCurrentProcess(); | 129 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 128 std::string profile_dir("my_user"); | 130 std::string profile_dir("my_user"); |
| 129 | 131 |
| 130 cl->AppendSwitch(switches::kTestType); | 132 cl->AppendSwitch(switches::kTestType); |
| 131 | 133 |
| 132 FilePath expected_default = | 134 FilePath expected_default = |
| 133 FilePath().AppendASCII(chrome::kInitialProfile); | 135 FilePath().AppendASCII(chrome::kInitialProfile); |
| 134 EXPECT_EQ(expected_default.value(), | 136 EXPECT_EQ( |
| 135 profile_manager_->GetInitialProfileDir().value()); | 137 expected_default.value(), |
| 138 g_browser_process->profile_manager()->GetInitialProfileDir().value()); | |
| 136 } | 139 } |
| 137 | 140 |
| 138 #if defined(OS_CHROMEOS) | 141 #if defined(OS_CHROMEOS) |
| 139 // This functionality only exists on Chrome OS. | 142 // This functionality only exists on Chrome OS. |
| 140 TEST_F(ProfileManagerTest, LoggedInProfileDir) { | 143 TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 141 CommandLine *cl = CommandLine::ForCurrentProcess(); | 144 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 142 std::string profile_dir("my_user"); | 145 std::string profile_dir("my_user"); |
| 143 | 146 |
| 144 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); | 147 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); |
| 145 cl->AppendSwitch(switches::kTestType); | 148 cl->AppendSwitch(switches::kTestType); |
| 146 | 149 |
| 147 FilePath expected_default = | 150 FilePath expected_default = |
| 148 FilePath().AppendASCII(chrome::kInitialProfile); | 151 FilePath().AppendASCII(chrome::kInitialProfile); |
| 152 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 149 EXPECT_EQ(expected_default.value(), | 153 EXPECT_EQ(expected_default.value(), |
| 150 profile_manager_->GetInitialProfileDir().value()); | 154 profile_manager->GetInitialProfileDir().value()); |
| 151 | 155 |
| 152 profile_manager_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 156 profile_manager->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 153 content::NotificationService::AllSources(), | 157 content::NotificationService::AllSources(), |
| 154 content::NotificationService::NoDetails()); | 158 content::NotificationService::NoDetails()); |
| 155 FilePath expected_logged_in(profile_dir); | 159 FilePath expected_logged_in(profile_dir); |
| 156 EXPECT_EQ(expected_logged_in.value(), | 160 EXPECT_EQ(expected_logged_in.value(), |
| 157 profile_manager_->GetInitialProfileDir().value()); | 161 profile_manager->GetInitialProfileDir().value()); |
| 158 VLOG(1) << temp_dir_.path().Append( | 162 VLOG(1) << temp_dir_.path().Append( |
| 159 profile_manager_->GetInitialProfileDir()).value(); | 163 profile_manager->GetInitialProfileDir()).value(); |
| 160 } | 164 } |
| 161 | 165 |
| 162 #endif | 166 #endif |
| 163 | 167 |
| 164 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 168 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
| 165 FilePath dest_path1 = temp_dir_.path(); | 169 FilePath dest_path1 = temp_dir_.path(); |
| 166 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 170 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 167 | 171 |
| 168 FilePath dest_path2 = temp_dir_.path(); | 172 FilePath dest_path2 = temp_dir_.path(); |
| 169 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 173 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 170 | 174 |
| 175 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 171 Profile* profile1; | 176 Profile* profile1; |
| 172 Profile* profile2; | 177 Profile* profile2; |
| 173 | 178 |
| 174 // Successfully create the profiles. | 179 // Successfully create the profiles. |
| 175 profile1 = profile_manager_->GetProfile(dest_path1); | 180 profile1 = profile_manager->GetProfile(dest_path1); |
| 176 ASSERT_TRUE(profile1); | 181 ASSERT_TRUE(profile1); |
| 177 | 182 |
| 178 profile2 = profile_manager_->GetProfile(dest_path2); | 183 profile2 = profile_manager->GetProfile(dest_path2); |
| 179 ASSERT_TRUE(profile2); | 184 ASSERT_TRUE(profile2); |
| 180 | 185 |
| 181 // Force lazy-init of some profile services to simulate use. | 186 // Force lazy-init of some profile services to simulate use. |
| 182 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 187 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 183 EXPECT_TRUE(profile1->GetBookmarkModel()); | 188 EXPECT_TRUE(profile1->GetBookmarkModel()); |
| 184 EXPECT_TRUE(profile2->GetBookmarkModel()); | 189 EXPECT_TRUE(profile2->GetBookmarkModel()); |
| 185 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 190 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 186 | 191 |
| 187 // Make sure any pending tasks run before we destroy the profiles. | 192 // Make sure any pending tasks run before we destroy the profiles. |
| 188 message_loop_.RunAllPending(); | 193 message_loop_.RunAllPending(); |
| 189 | 194 |
| 190 profile_manager_.reset(); | 195 static_cast<TestingBrowserProcess*>(g_browser_process)->SetProfileManager( |
| 196 NULL); | |
| 191 | 197 |
| 192 // Make sure history cleans up correctly. | 198 // Make sure history cleans up correctly. |
| 193 message_loop_.RunAllPending(); | 199 message_loop_.RunAllPending(); |
| 194 } | 200 } |
| 195 | 201 |
| 196 MATCHER(NotFail, "Profile creation failure status is not reported.") { | 202 MATCHER(NotFail, "Profile creation failure status is not reported.") { |
| 197 return arg == ProfileManagerObserver::STATUS_CREATED || | 203 return arg == ProfileManagerObserver::STATUS_CREATED || |
| 198 arg == ProfileManagerObserver::STATUS_INITIALIZED; | 204 arg == ProfileManagerObserver::STATUS_INITIALIZED; |
| 199 } | 205 } |
| 200 | 206 |
| 201 // Tests asynchronous profile creation mechanism. | 207 // Tests asynchronous profile creation mechanism. |
| 202 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { | 208 TEST_F(ProfileManagerTest, DISABLED_CreateProfileAsync) { |
| 203 FilePath dest_path = | 209 FilePath dest_path = |
| 204 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); | 210 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); |
| 205 | 211 |
| 206 MockObserver mock_observer; | 212 MockObserver mock_observer; |
| 207 EXPECT_CALL(mock_observer, OnProfileCreated( | 213 EXPECT_CALL(mock_observer, OnProfileCreated( |
| 208 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); | 214 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 209 | 215 |
| 210 profile_manager_->CreateProfileAsync(dest_path, &mock_observer); | 216 g_browser_process->profile_manager()->CreateProfileAsync(dest_path, |
| 217 &mock_observer); | |
| 211 | 218 |
| 212 message_loop_.RunAllPending(); | 219 message_loop_.RunAllPending(); |
| 213 } | 220 } |
| 214 | 221 |
| 215 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { | 222 MATCHER(SameNotNull, "The same non-NULL value for all calls.") { |
| 216 if (!g_created_profile) | 223 if (!g_created_profile) |
| 217 g_created_profile = arg; | 224 g_created_profile = arg; |
| 218 return arg != NULL && arg == g_created_profile; | 225 return arg != NULL && arg == g_created_profile; |
| 219 } | 226 } |
| 220 | 227 |
| 221 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { | 228 TEST_F(ProfileManagerTest, CreateProfileAsyncMultipleRequests) { |
| 222 FilePath dest_path = | 229 FilePath dest_path = |
| 223 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); | 230 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile")); |
| 224 | 231 |
| 225 g_created_profile = NULL; | 232 g_created_profile = NULL; |
| 226 | 233 |
| 227 MockObserver mock_observer1; | 234 MockObserver mock_observer1; |
| 228 EXPECT_CALL(mock_observer1, OnProfileCreated( | 235 EXPECT_CALL(mock_observer1, OnProfileCreated( |
| 229 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 236 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 230 MockObserver mock_observer2; | 237 MockObserver mock_observer2; |
| 231 EXPECT_CALL(mock_observer2, OnProfileCreated( | 238 EXPECT_CALL(mock_observer2, OnProfileCreated( |
| 232 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 239 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 233 MockObserver mock_observer3; | 240 MockObserver mock_observer3; |
| 234 EXPECT_CALL(mock_observer3, OnProfileCreated( | 241 EXPECT_CALL(mock_observer3, OnProfileCreated( |
| 235 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); | 242 SameNotNull(), NotFail())).Times(testing::AtLeast(1)); |
| 236 | 243 |
| 237 profile_manager_->CreateProfileAsync(dest_path, &mock_observer1); | 244 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 238 profile_manager_->CreateProfileAsync(dest_path, &mock_observer2); | 245 |
| 239 profile_manager_->CreateProfileAsync(dest_path, &mock_observer3); | 246 profile_manager->CreateProfileAsync(dest_path, &mock_observer1); |
| 247 profile_manager->CreateProfileAsync(dest_path, &mock_observer2); | |
| 248 profile_manager->CreateProfileAsync(dest_path, &mock_observer3); | |
| 240 | 249 |
| 241 message_loop_.RunAllPending(); | 250 message_loop_.RunAllPending(); |
| 242 } | 251 } |
| 243 | 252 |
| 244 TEST_F(ProfileManagerTest, CreateProfilesAsync) { | 253 TEST_F(ProfileManagerTest, CreateProfilesAsync) { |
| 245 FilePath dest_path1 = | 254 FilePath dest_path1 = |
| 246 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); | 255 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 1")); |
| 247 FilePath dest_path2 = | 256 FilePath dest_path2 = |
| 248 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); | 257 temp_dir_.path().Append(FILE_PATH_LITERAL("New Profile 2")); |
| 249 | 258 |
| 250 MockObserver mock_observer; | 259 MockObserver mock_observer; |
| 251 EXPECT_CALL(mock_observer, OnProfileCreated( | 260 EXPECT_CALL(mock_observer, OnProfileCreated( |
| 252 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); | 261 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); |
| 253 | 262 |
| 254 profile_manager_->CreateProfileAsync(dest_path1, &mock_observer); | 263 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 255 profile_manager_->CreateProfileAsync(dest_path2, &mock_observer); | 264 |
| 265 profile_manager->CreateProfileAsync(dest_path1, &mock_observer); | |
| 266 profile_manager->CreateProfileAsync(dest_path2, &mock_observer); | |
| 256 | 267 |
| 257 message_loop_.RunAllPending(); | 268 message_loop_.RunAllPending(); |
| 258 } | 269 } |
| 259 | 270 |
| 260 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { | 271 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { |
| 261 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 272 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 273 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); | |
| 262 | 274 |
| 263 EXPECT_EQ(0u, cache.GetNumberOfProfiles()); | 275 EXPECT_EQ(0u, cache.GetNumberOfProfiles()); |
| 264 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), | 276 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), |
| 265 ASCIIToUTF16("name_1"), string16(), 0); | 277 ASCIIToUTF16("name_1"), string16(), 0); |
| 266 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), | 278 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), |
| 267 ASCIIToUTF16("name_2"), string16(), 0); | 279 ASCIIToUTF16("name_2"), string16(), 0); |
| 268 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), | 280 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
| 269 ASCIIToUTF16("name_3"), string16(), 0); | 281 ASCIIToUTF16("name_3"), string16(), 0); |
| 270 cache.SetBackgroundStatusOfProfileAtIndex(0, true); | 282 cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 271 cache.SetBackgroundStatusOfProfileAtIndex(2, true); | 283 cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 272 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); | 284 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 273 | 285 |
| 274 profile_manager_->AutoloadProfiles(); | 286 profile_manager->AutoloadProfiles(); |
| 275 | 287 |
| 276 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); | 288 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); |
| 277 } | 289 } |
| 290 | |
| 291 TEST_F(ProfileManagerTest, InitProfileUserPrefs) { | |
| 292 FilePath dest_path = temp_dir_.path(); | |
| 293 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | |
| 294 | |
| 295 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 296 | |
| 297 Profile* profile; | |
| 298 | |
| 299 // Successfully create the profile | |
| 300 profile = profile_manager->GetProfile(dest_path); | |
| 301 ASSERT_TRUE(profile); | |
| 302 | |
| 303 // Check that the profile name is non empty | |
| 304 std::string profile_name = | |
| 305 profile->GetPrefs()->GetString(prefs::kProfileName); | |
| 306 EXPECT_FALSE(profile_name.empty()); | |
| 307 | |
| 308 // Check that the profile avatar index is valid | |
| 309 size_t avatar_index = profile->GetPrefs()->GetInteger( | |
| 310 prefs::kProfileAvatarIndex); | |
| 311 EXPECT_TRUE(profile_manager->GetProfileInfoCache().IsDefaultAvatarIconIndex( | |
| 312 avatar_index)); | |
| 313 | |
| 314 } | |
| OLD | NEW |