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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/profiles/profile_info_cache.h" | 9 #include "chrome/browser/profiles/profile_info_cache.h" |
10 #include "chrome/browser/profiles/profile_info_cache_observer.h" | 10 #include "chrome/browser/profiles/profile_info_cache_observer.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
12 #include "chrome/browser/profiles/profile_window.h" | 12 #include "chrome/browser/profiles/profile_window.h" |
13 #include "chrome/browser/profiles/profiles_state.h" | 13 #include "chrome/browser/profiles/profiles_state.h" |
14 #include "chrome/browser/ui/browser_finder.h" | 14 #include "chrome/browser/ui/browser_finder.h" |
15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 16 #include "chrome/browser/ui/browser_window.h" |
16 #include "chrome/browser/ui/host_desktop.h" | 17 #include "chrome/browser/ui/host_desktop.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
19 #include "chrome/test/base/test_switches.h" | 20 #include "chrome/test/base/test_switches.h" |
20 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
21 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // An observer that returns back to test code after a new profile is | 26 // An observer that returns back to test code after a new profile is |
26 // initialized. | 27 // initialized. |
27 void OnUnblockOnProfileCreation(Profile* profile, | 28 void OnUnblockOnProfileCreation(Profile* profile, |
28 Profile::CreateStatus status) { | 29 Profile::CreateStatus status) { |
29 if (status == Profile::CREATE_STATUS_INITIALIZED) | 30 if (status == Profile::CREATE_STATUS_INITIALIZED) |
30 base::MessageLoop::current()->Quit(); | 31 base::MessageLoop::current()->Quit(); |
31 } | 32 } |
32 | 33 |
33 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { | 34 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) { |
34 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); | 35 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL); |
35 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); | 36 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL); |
36 // No browser should have been created for this profile yet. | 37 // No browser should have been created for this profile yet. |
37 EXPECT_EQ(chrome::GetTotalBrowserCountForProfile(profile), 0U); | 38 EXPECT_EQ(chrome::GetTotalBrowserCountForProfile(profile), 0U); |
38 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); | 39 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U); |
39 if (status == Profile::CREATE_STATUS_INITIALIZED) | 40 if (status == Profile::CREATE_STATUS_INITIALIZED) |
40 base::MessageLoop::current()->Quit(); | 41 base::MessageLoop::current()->Quit(); |
41 } | 42 } |
42 | 43 |
| 44 void EphemeralProfileCreationComplete(Profile* profile, |
| 45 Profile::CreateStatus status) { |
| 46 if (status == Profile::CREATE_STATUS_INITIALIZED) |
| 47 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); |
| 48 ProfileCreationComplete(profile, status); |
| 49 } |
| 50 |
43 class ProfileRemovalObserver : public ProfileInfoCacheObserver { | 51 class ProfileRemovalObserver : public ProfileInfoCacheObserver { |
44 public: | 52 public: |
45 ProfileRemovalObserver() { | 53 ProfileRemovalObserver() { |
46 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver( | 54 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver( |
47 this); | 55 this); |
48 } | 56 } |
49 | 57 |
50 virtual ~ProfileRemovalObserver() { | 58 virtual ~ProfileRemovalObserver() { |
51 g_browser_process->profile_manager()->GetProfileInfoCache().RemoveObserver( | 59 g_browser_process->profile_manager()->GetProfileInfoCache().RemoveObserver( |
52 this); | 60 this); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); | 241 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); |
234 | 242 |
235 // Switch to the first profile without opening a new window. | 243 // Switch to the first profile without opening a new window. |
236 profiles::SwitchToProfile(path_profile1, desktop_type, false); | 244 profiles::SwitchToProfile(path_profile1, desktop_type, false); |
237 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U); | 245 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U); |
238 EXPECT_EQ(2U, browser_list->size()); | 246 EXPECT_EQ(2U, browser_list->size()); |
239 | 247 |
240 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath()); | 248 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath()); |
241 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); | 249 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); |
242 } | 250 } |
| 251 |
| 252 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, EphemeralProfile) { |
| 253 #if defined(OS_WIN) && defined(USE_ASH) |
| 254 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
| 255 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) |
| 256 return; |
| 257 #endif |
| 258 |
| 259 // If multiprofile mode is not enabled, you can't switch between profiles. |
| 260 if (!profiles::IsMultipleProfilesEnabled()) |
| 261 return; |
| 262 |
| 263 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 264 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); |
| 265 base::FilePath path_profile1 = cache.GetPathOfProfileAtIndex(0); |
| 266 |
| 267 ASSERT_EQ(1U, profile_manager->GetNumberOfProfiles()); |
| 268 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); |
| 269 |
| 270 // Create an ephemeral profile. |
| 271 base::FilePath path_profile2 = |
| 272 profile_manager->GenerateNextProfileDirectoryPath(); |
| 273 profile_manager->CreateProfileAsync( |
| 274 path_profile2, |
| 275 base::Bind(&EphemeralProfileCreationComplete), |
| 276 string16(), string16(), std::string()); |
| 277 |
| 278 // Spin to allow profile creation to take place. |
| 279 content::RunMessageLoop(); |
| 280 |
| 281 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); |
| 282 BrowserList* browser_list = BrowserList::GetInstance(desktop_type); |
| 283 ASSERT_EQ(2U, cache.GetNumberOfProfiles()); |
| 284 EXPECT_EQ(1U, browser_list->size()); |
| 285 |
| 286 // Open a browser window for the second profile. |
| 287 profiles::SwitchToProfile(path_profile2, desktop_type, false); |
| 288 EXPECT_EQ(2U, chrome::GetTotalBrowserCount()); |
| 289 EXPECT_EQ(2U, browser_list->size()); |
| 290 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); |
| 291 |
| 292 // Create a second window for the ephemeral profile. |
| 293 profiles::SwitchToProfile(path_profile2, desktop_type, true); |
| 294 EXPECT_EQ(3U, chrome::GetTotalBrowserCount()); |
| 295 EXPECT_EQ(3U, browser_list->size()); |
| 296 |
| 297 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath()); |
| 298 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath()); |
| 299 EXPECT_EQ(path_profile2, browser_list->get(2)->profile()->GetPath()); |
| 300 |
| 301 // Closing the first window of the ephemeral profile should not delete it. |
| 302 browser_list->get(2)->window()->Close(); |
| 303 content::RunAllPendingInMessageLoop(); |
| 304 EXPECT_EQ(2U, browser_list->size()); |
| 305 ASSERT_EQ(2U, cache.GetNumberOfProfiles()); |
| 306 |
| 307 // The second should though. |
| 308 browser_list->get(1)->window()->Close(); |
| 309 content::RunAllPendingInMessageLoop(); |
| 310 content::RunAllPendingInMessageLoop(); |
| 311 EXPECT_EQ(1U, browser_list->size()); |
| 312 ASSERT_EQ(1U, cache.GetNumberOfProfiles()); |
| 313 |
| 314 // Closing the last window should not reduce the number of profiles. |
| 315 browser_list->get(0)->window()->Close(); |
| 316 content::RunAllPendingInMessageLoop(); |
| 317 EXPECT_EQ(0U, browser_list->size()); |
| 318 ASSERT_EQ(1U, cache.GetNumberOfProfiles()); |
| 319 } |
OLD | NEW |