1 // Copyright 2014 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 "chrome/browser/ui/views/profiles/profile_chooser_view.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/histogram_tester.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/extension_browsertest.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profile_metrics.h"
17 #include "chrome/browser/profiles/profiles_state.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/user_manager.h"
20 #include "chrome/browser/ui/views/frame/browser_view.h"
21 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
22 #include "chrome/browser/ui/views/profiles/new_avatar_button.h"
23 #include "chrome/browser/ui/views/profiles/user_manager_view.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "components/signin/core/common/profile_management_switches.h"
28 #include "content/public/test/test_utils.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "ui/events/event_utils.h"
31 #include "ui/views/controls/webview/webview.h"
33 // ChromeOS and mobile platforms don't have a ProfileChooserView.
34 #if defined(FRAME_AVATAR_BUTTON)
38 Profile
* CreateTestingProfile(const std::string
& profile_name
) {
39 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
40 size_t starting_number_of_profiles
= profile_manager
->GetNumberOfProfiles();
43 PathService::Get(chrome::DIR_USER_DATA
, &path
);
44 path
= path
.AppendASCII(profile_name
);
45 if (!base::PathExists(path
) && !base::CreateDirectory(path
))
46 NOTREACHED() << "Could not create directory at " << path
.MaybeAsASCII();
49 Profile::CreateProfile(path
, NULL
, Profile::CREATE_MODE_SYNCHRONOUS
);
50 profile_manager
->RegisterTestingProfile(profile
, true, false);
51 EXPECT_EQ(starting_number_of_profiles
+ 1,
52 profile_manager
->GetNumberOfProfiles());
56 // Set up the profiles to enable Lock. Takes as parameter a profile that will be
57 // signed in, and also creates a supervised user (necessary for lock).
58 void SetupProfilesForLock(Profile
* signed_in
) {
59 const char* signed_in_email
= "me@google.com";
60 Profile
* supervised
= CreateTestingProfile("supervised");
61 ProfileInfoCache
* cache
= &g_browser_process
->profile_manager()->
62 GetProfileInfoCache();
63 cache
->SetAuthInfoOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
64 signed_in
->GetPath()), "12345", base::UTF8ToUTF16(signed_in_email
));
65 signed_in
->GetPrefs()->
66 SetString(prefs::kGoogleServicesHostedDomain
, "google.com");
67 cache
->SetSupervisedUserIdOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
68 supervised
->GetPath()), signed_in_email
);
70 EXPECT_TRUE(profiles::IsLockAvailable(signed_in
));
73 views::View
* FindWebView(views::View
* view
) {
74 std::queue
<views::View
*> queue
;
76 while (!queue
.empty()) {
77 views::View
* current
= queue
.front();
79 if (current
->GetClassName() == views::WebView::kViewClassName
)
82 for (int i
= 0; i
< current
->child_count(); ++i
)
83 queue
.push(current
->child_at(i
));
90 class ProfileChooserViewExtensionsTest
: public ExtensionBrowserTest
{
92 ProfileChooserViewExtensionsTest() {}
93 ~ProfileChooserViewExtensionsTest() override
{}
96 void SetUp() override
{
97 ExtensionBrowserTest::SetUp();
98 DCHECK(switches::IsNewAvatarMenu());
99 DCHECK(switches::IsNewProfileManagement());
102 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
103 ExtensionBrowserTest::SetUpCommandLine(command_line
);
104 switches::EnableNewProfileManagementForTesting(command_line
);
107 void OpenProfileChooserView(Browser
* browser
){
108 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(browser
);
109 NewAvatarButton
* button
= browser_view
->frame()->GetNewAvatarMenuButton();
111 NOTREACHED() << "NewAvatarButton not found.";
112 if (browser_view
->frame()->GetAvatarMenuButton())
113 NOTREACHED() << "Old Avatar Menu Button found.";
115 ProfileChooserView::close_on_deactivate_for_testing_
= false;
117 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
118 ui::EventTimeForNow(), 0, 0);
119 button
->NotifyClick(e
);
120 base::MessageLoop::current()->RunUntilIdle();
121 EXPECT_TRUE(ProfileChooserView::IsShowing());
123 // Create this observer before lock is pressed to avoid a race condition.
124 window_close_observer_
.reset(new content::WindowedNotificationObserver(
125 chrome::NOTIFICATION_BROWSER_CLOSED
,
126 content::Source
<Browser
>(browser
)));
129 AvatarMenu
* GetProfileChooserViewAvatarMenu() {
130 return ProfileChooserView::profile_bubble_
->avatar_menu_
.get();
133 void ClickProfileChooserViewLockButton() {
134 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
135 ui::EventTimeForNow(), 0, 0);
136 ProfileChooserView::profile_bubble_
->ButtonPressed(
137 ProfileChooserView::profile_bubble_
->lock_button_
, e
);
140 // Access the registry that has been prepared with at least one extension.
141 extensions::ExtensionRegistry
* GetPreparedRegistry(Profile
* signed_in
) {
142 extensions::ExtensionRegistry
* registry
=
143 extensions::ExtensionRegistry::Get(signed_in
);
144 const size_t initial_num_extensions
= registry
->enabled_extensions().size();
145 const extensions::Extension
* ext
= LoadExtension(
146 test_data_dir_
.AppendASCII("app"));
148 EXPECT_EQ(initial_num_extensions
+ 1,
149 registry
->enabled_extensions().size());
150 EXPECT_EQ(0U, registry
->blocked_extensions().size());
154 void WaitForUserManager() {
155 // If the User Manager hasn't shown yet, wait for it to show up.
156 // TODO(mlerman): As per crbug.com/450221, we should somehow observe when
157 // the UserManager is created and wait for that event.
158 if (!UserManager::IsShowing())
159 base::MessageLoop::current()->RunUntilIdle();
160 EXPECT_TRUE(UserManager::IsShowing());
163 content::WindowedNotificationObserver
* window_close_observer() {
164 return window_close_observer_
.get();
167 ProfileChooserView
* current_profile_bubble() {
168 return ProfileChooserView::profile_bubble_
;
171 views::View
* signin_current_profile_link() {
172 return ProfileChooserView::profile_bubble_
->signin_current_profile_link_
;
175 void ShowSigninView() {
176 DCHECK(current_profile_bubble());
177 DCHECK(current_profile_bubble()->avatar_menu_
);
178 current_profile_bubble()->ShowView(
179 profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN
,
180 current_profile_bubble()->avatar_menu_
.get());
181 base::MessageLoop::current()->RunUntilIdle();
185 scoped_ptr
<content::WindowedNotificationObserver
> window_close_observer_
;
187 DISALLOW_COPY_AND_ASSIGN(ProfileChooserViewExtensionsTest
);
190 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, SigninButtonHasFocus
) {
191 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
192 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
194 EXPECT_TRUE(signin_current_profile_link()->HasFocus());
197 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, ContentAreaHasFocus
) {
198 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
200 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
204 ASSERT_TRUE(current_profile_bubble());
205 views::View
* web_view
= FindWebView(current_profile_bubble());
206 ASSERT_TRUE(web_view
);
207 EXPECT_TRUE(web_view
->HasFocus());
210 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, ViewProfileUMA
) {
211 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
213 base::HistogramTester histograms
;
214 Profile
* profile
= browser()->profile();
215 profile
->GetPrefs()->SetInteger(prefs::kProfileAvatarTutorialShown
, 0);
217 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
219 histograms
.ExpectUniqueSample("Profile.NewAvatarMenu.Upgrade",
220 ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_VIEW
, 1);
223 // Flaky: http://crbug.com/450221
224 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
225 // UserManager to be showing.
226 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, DISABLED_LockProfile
) {
227 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
229 SetupProfilesForLock(browser()->profile());
230 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
232 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
233 AvatarMenu
* menu
= GetProfileChooserViewAvatarMenu();
234 EXPECT_FALSE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
236 ClickProfileChooserViewLockButton();
237 EXPECT_TRUE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
239 window_close_observer()->Wait();
240 EXPECT_TRUE(BrowserList::GetInstance(chrome::GetActiveDesktop())->empty());
242 WaitForUserManager();
243 // We need to hide the User Manager or else the process can't die.
247 // Flaky: http://crbug.com/450221
248 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
249 // UserManager to be showing.
250 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
251 DISABLED_LockProfileBlockExtensions
) {
252 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
253 // Make sure we have at least one enabled extension.
254 extensions::ExtensionRegistry
* registry
=
255 GetPreparedRegistry(browser()->profile());
256 SetupProfilesForLock(browser()->profile());
258 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
259 ClickProfileChooserViewLockButton();
260 window_close_observer()->Wait();
262 WaitForUserManager();
263 // Assert that the ExtensionService is blocked.
264 ASSERT_EQ(1U, registry
->blocked_extensions().size());
266 // We need to hide the User Manager or else the process can't die.
270 // Flaky: http://crbug.com/450221
271 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
272 // UserManager to be showing.
273 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
274 DISABLED_LockProfileNoBlockOtherProfileExtensions
) {
275 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
276 // Make sure we have at least one enabled extension.
277 extensions::ExtensionRegistry
* registry
=
278 GetPreparedRegistry(browser()->profile());
279 const size_t total_enabled_extensions
= registry
->enabled_extensions().size();
281 // Create a different profile and then lock it.
282 Profile
*signed_in
= CreateTestingProfile("signed_in");
283 SetupProfilesForLock(signed_in
);
284 extensions::ExtensionSystem::Get(signed_in
)->InitForRegularProfile(true);
285 Browser
* browser_to_lock
= CreateBrowser(signed_in
);
286 EXPECT_EQ(2U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
288 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser_to_lock
));
289 ClickProfileChooserViewLockButton();
290 window_close_observer()->Wait();
291 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
293 WaitForUserManager();
294 // Assert that the first profile's extensions are not blocked.
295 ASSERT_EQ(total_enabled_extensions
, registry
->enabled_extensions().size());
296 ASSERT_EQ(0U, registry
->blocked_extensions().size());
298 // We need to hide the User Manager or else the process can't die.