1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/profiles/avatar_menu.h"
8 #include "chrome/browser/profiles/profile_manager.h"
9 #include "chrome/browser/profiles/profile_window.h"
10 #include "chrome/browser/profiles/profiles_state.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_list.h"
13 #include "chrome/browser/ui/user_manager.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/test_switches.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "content/public/test/test_utils.h"
22 // An observer that returns back to test code after a new profile is
24 void OnUnblockOnProfileCreation(Profile* profile,
25 Profile::CreateStatus status) {
26 if (status == Profile::CREATE_STATUS_INITIALIZED)
27 base::MessageLoop::current()->Quit();
32 class ProfileListDesktopBrowserTest : public InProcessBrowserTest {
34 ProfileListDesktopBrowserTest() {}
36 scoped_ptr<AvatarMenu> CreateAvatarMenu(ProfileInfoCache* cache) {
37 return scoped_ptr<AvatarMenu>(new AvatarMenu(cache, NULL, browser()));
41 scoped_ptr<AvatarMenu> avatar_menu_;
43 DISALLOW_COPY_AND_ASSIGN(ProfileListDesktopBrowserTest);
47 // SignOut is flaky. So far only observed on Windows. crbug.com/357329.
48 #define MAYBE_SignOut DISABLED_SignOut
49 #elif defined(OS_CHROMEOS)
50 // This test doesn't make sense for Chrome OS since it has a different
51 // multi-profiles menu in the system tray instead.
52 #define MAYBE_SignOut DISABLED_SignOut
54 #define MAYBE_SignOut SignOut
56 IN_PROC_BROWSER_TEST_F(ProfileListDesktopBrowserTest, MAYBE_SignOut) {
57 if (!profiles::IsMultipleProfilesEnabled())
60 ProfileManager* profile_manager = g_browser_process->profile_manager();
61 Profile* current_profile = browser()->profile();
62 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
63 size_t index = cache.GetIndexOfProfileWithPath(current_profile->GetPath());
65 scoped_ptr<AvatarMenu> menu = CreateAvatarMenu(&cache);
68 BrowserList* browser_list =
69 BrowserList::GetInstance(chrome::GetActiveDesktop());
70 EXPECT_EQ(1U, browser_list->size());
71 content::WindowedNotificationObserver window_close_observer(
72 chrome::NOTIFICATION_BROWSER_CLOSED,
73 content::Source<Browser>(browser()));
75 EXPECT_FALSE(cache.ProfileIsSigninRequiredAtIndex(index));
76 profiles::LockProfile(current_profile);
77 window_close_observer.Wait(); // rely on test time-out for failure indication
79 EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(index));
80 EXPECT_EQ(0U, browser_list->size());
82 // Signing out brings up the User Manager which we should close before exit.
86 #if defined(OS_CHROMEOS)
87 // This test doesn't make sense for Chrome OS since it has a different
88 // multi-profiles menu in the system tray instead.
89 #define MAYBE_SwitchToProfile DISABLED_SwitchToProfile
91 #define MAYBE_SwitchToProfile SwitchToProfile
93 IN_PROC_BROWSER_TEST_F(ProfileListDesktopBrowserTest, MAYBE_SwitchToProfile) {
94 #if defined(OS_WIN) && defined(USE_ASH)
95 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
96 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
100 if (!profiles::IsMultipleProfilesEnabled())
103 ProfileManager* profile_manager = g_browser_process->profile_manager();
104 Profile* current_profile = browser()->profile();
105 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
106 base::FilePath path_profile1 = current_profile->GetPath();
107 base::FilePath user_dir = cache.GetUserDataDir();
109 // Create an additional profile.
110 base::FilePath path_profile2 = user_dir.Append(
111 FILE_PATH_LITERAL("New Profile 2"));
112 profile_manager->CreateProfileAsync(path_profile2,
113 base::Bind(&OnUnblockOnProfileCreation),
114 base::string16(), base::string16(),
117 // Spin to allow profile creation to take place, loop is terminated
118 // by OnUnblockOnProfileCreation when the profile is created.
119 content::RunMessageLoop();
120 ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
122 scoped_ptr<AvatarMenu> menu = CreateAvatarMenu(&cache);
124 BrowserList* browser_list =
125 BrowserList::GetInstance(chrome::GetActiveDesktop());
126 EXPECT_EQ(1U, browser_list->size());
127 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
129 // Open a browser window for the first profile.
130 menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1),
131 false, ProfileMetrics::SWITCH_PROFILE_ICON);
132 EXPECT_EQ(1U, browser_list->size());
133 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
135 // Open a browser window for the second profile.
136 menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile2),
137 false, ProfileMetrics::SWITCH_PROFILE_ICON);
138 EXPECT_EQ(2U, browser_list->size());
140 // Switch to the first profile without opening a new window.
141 menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1),
142 false, ProfileMetrics::SWITCH_PROFILE_ICON);
143 EXPECT_EQ(2U, browser_list->size());
144 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
145 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());