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