8f3c0b07e7c18257322a857af7f0b53301981229
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_list_desktop_browsertest.cc
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.
4
5 #include "base/command_line.h"
6 #include "base/path_service.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/avatar_menu.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/profiles/profile_window.h"
11 #include "chrome/browser/profiles/profiles_state.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/test_switches.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "content/public/test/test_utils.h"
20
21 namespace {
22
23 // An observer that returns back to test code after a new profile is
24 // initialized.
25 void OnUnblockOnProfileCreation(Profile* profile,
26                                 Profile::CreateStatus status) {
27   if (status == Profile::CREATE_STATUS_INITIALIZED)
28     base::MessageLoop::current()->Quit();
29 }
30
31 }  // namespace
32
33 class ProfileListDesktopBrowserTest : public InProcessBrowserTest {
34  public:
35   ProfileListDesktopBrowserTest() {}
36
37   AvatarMenu* GetAvatarMenu(ProfileInfoCache* cache) {
38     // Reset the menu.
39     avatar_menu_.reset(new AvatarMenu(
40         cache,
41         NULL,
42         browser()));
43     return avatar_menu_.get();
44   }
45
46  private:
47   scoped_ptr<AvatarMenu> avatar_menu_;
48
49   DISALLOW_COPY_AND_ASSIGN(ProfileListDesktopBrowserTest);
50 };
51
52 #if defined(OS_WIN)
53 // SignOut is flaky. So far only observed on Windows. crbug.com/357329.
54 #define MAYBE_SignOut DISABLED_SignOut
55 #elif defined(OS_CHROMEOS)
56 // This test doesn't make sense for Chrome OS since it has a different
57 // multi-profiles menu in the system tray instead.
58 #define MAYBE_SignOut DISABLED_SignOut
59 #else
60 #define MAYBE_SignOut SignOut
61 #endif
62 IN_PROC_BROWSER_TEST_F(ProfileListDesktopBrowserTest, MAYBE_SignOut) {
63   if (!profiles::IsMultipleProfilesEnabled())
64     return;
65
66   ProfileManager* profile_manager = g_browser_process->profile_manager();
67   Profile* current_profile = browser()->profile();
68   ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
69   size_t index = cache.GetIndexOfProfileWithPath(current_profile->GetPath());
70
71   AvatarMenu* menu = GetAvatarMenu(&cache);
72   menu->RebuildMenu();
73
74   BrowserList* browser_list =
75       BrowserList::GetInstance(chrome::GetActiveDesktop());
76   EXPECT_EQ(1U, browser_list->size());
77   content::WindowedNotificationObserver window_close_observer(
78       chrome::NOTIFICATION_BROWSER_CLOSED,
79       content::Source<Browser>(browser()));
80
81   EXPECT_FALSE(cache.ProfileIsSigninRequiredAtIndex(index));
82   profiles::LockProfile(current_profile);
83   window_close_observer.Wait();  // rely on test time-out for failure indication
84
85   EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(index));
86   EXPECT_EQ(0U, browser_list->size());
87
88   // Signing out brings up the User Manager which we should close before exit.
89   chrome::HideUserManager();
90 }
91
92 #if defined(OS_CHROMEOS)
93 // This test doesn't make sense for Chrome OS since it has a different
94 // multi-profiles menu in the system tray instead.
95 #define MAYBE_SwitchToProfile DISABLED_SwitchToProfile
96 #else
97 #define MAYBE_SwitchToProfile SwitchToProfile
98 #endif
99 IN_PROC_BROWSER_TEST_F(ProfileListDesktopBrowserTest, MAYBE_SwitchToProfile) {
100 #if defined(OS_WIN) && defined(USE_ASH)
101   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
102   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
103     return;
104 #endif
105
106   if (!profiles::IsMultipleProfilesEnabled())
107     return;
108
109   ProfileManager* profile_manager = g_browser_process->profile_manager();
110   Profile* current_profile = browser()->profile();
111   ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
112   base::FilePath path_profile1 = current_profile->GetPath();
113   base::FilePath user_dir = cache.GetUserDataDir();
114
115   // Create an additional profile.
116   base::FilePath path_profile2 = user_dir.Append(
117       FILE_PATH_LITERAL("New Profile 2"));
118   profile_manager->CreateProfileAsync(path_profile2,
119                                       base::Bind(&OnUnblockOnProfileCreation),
120                                       base::string16(), base::string16(),
121                                       std::string());
122
123   // Spin to allow profile creation to take place, loop is terminated
124   // by OnUnblockOnProfileCreation when the profile is created.
125   content::RunMessageLoop();
126   ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
127
128   AvatarMenu* menu = GetAvatarMenu(&cache);
129   menu->RebuildMenu();
130   BrowserList* browser_list =
131       BrowserList::GetInstance(chrome::GetActiveDesktop());
132   EXPECT_EQ(1U, browser_list->size());
133   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
134
135   // Open a browser window for the first profile.
136   menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1),
137                         false, ProfileMetrics::SWITCH_PROFILE_ICON);
138   EXPECT_EQ(1U, browser_list->size());
139   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
140
141   // Open a browser window for the second profile.
142   menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile2),
143                         false, ProfileMetrics::SWITCH_PROFILE_ICON);
144   EXPECT_EQ(2U, browser_list->size());
145
146   // Switch to the first profile without opening a new window.
147   menu->SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1),
148                         false, ProfileMetrics::SWITCH_PROFILE_ICON);
149   EXPECT_EQ(2U, browser_list->size());
150   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
151   EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
152 }