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"
35 Profile
* CreateTestingProfile(const std::string
& profile_name
) {
36 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
37 size_t starting_number_of_profiles
= profile_manager
->GetNumberOfProfiles();
40 PathService::Get(chrome::DIR_USER_DATA
, &path
);
41 path
= path
.AppendASCII(profile_name
);
42 if (!base::PathExists(path
) && !base::CreateDirectory(path
))
43 NOTREACHED() << "Could not create directory at " << path
.MaybeAsASCII();
46 Profile::CreateProfile(path
, NULL
, Profile::CREATE_MODE_SYNCHRONOUS
);
47 profile_manager
->RegisterTestingProfile(profile
, true, false);
48 EXPECT_EQ(starting_number_of_profiles
+ 1,
49 profile_manager
->GetNumberOfProfiles());
53 // Set up the profiles to enable Lock. Takes as parameter a profile that will be
54 // signed in, and also creates a supervised user (necessary for lock).
55 void SetupProfilesForLock(Profile
* signed_in
) {
56 const char* signed_in_email
= "me@google.com";
57 Profile
* supervised
= CreateTestingProfile("supervised");
58 ProfileInfoCache
* cache
= &g_browser_process
->profile_manager()->
59 GetProfileInfoCache();
60 cache
->SetAuthInfoOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
61 signed_in
->GetPath()), "12345", base::UTF8ToUTF16(signed_in_email
));
62 signed_in
->GetPrefs()->
63 SetString(prefs::kGoogleServicesHostedDomain
, "google.com");
64 cache
->SetSupervisedUserIdOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
65 supervised
->GetPath()), signed_in_email
);
67 EXPECT_TRUE(profiles::IsLockAvailable(signed_in
));
70 views::View
* FindWebView(views::View
* view
) {
71 std::queue
<views::View
*> queue
;
73 while (!queue
.empty()) {
74 views::View
* current
= queue
.front();
76 if (current
->GetClassName() == views::WebView::kViewClassName
)
79 for (int i
= 0; i
< current
->child_count(); ++i
)
80 queue
.push(current
->child_at(i
));
87 class ProfileChooserViewExtensionsTest
: public ExtensionBrowserTest
{
89 ProfileChooserViewExtensionsTest() {}
90 ~ProfileChooserViewExtensionsTest() override
{}
93 void SetUp() override
{
94 ExtensionBrowserTest::SetUp();
95 DCHECK(switches::IsNewAvatarMenu());
96 DCHECK(switches::IsNewProfileManagement());
99 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
100 ExtensionBrowserTest::SetUpCommandLine(command_line
);
101 switches::EnableNewProfileManagementForTesting(command_line
);
104 void OpenProfileChooserView(Browser
* browser
){
105 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(browser
);
106 NewAvatarButton
* button
= browser_view
->frame()->GetNewAvatarMenuButton();
108 NOTREACHED() << "NewAvatarButton not found.";
109 if (browser_view
->frame()->GetAvatarMenuButton())
110 NOTREACHED() << "Old Avatar Menu Button found.";
112 ProfileChooserView::close_on_deactivate_for_testing_
= false;
114 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
115 ui::EventTimeForNow(), 0, 0);
116 button
->NotifyClick(e
);
117 base::MessageLoop::current()->RunUntilIdle();
118 EXPECT_TRUE(ProfileChooserView::IsShowing());
120 // Create this observer before lock is pressed to avoid a race condition.
121 window_close_observer_
.reset(new content::WindowedNotificationObserver(
122 chrome::NOTIFICATION_BROWSER_CLOSED
,
123 content::Source
<Browser
>(browser
)));
126 AvatarMenu
* GetProfileChooserViewAvatarMenu() {
127 return ProfileChooserView::profile_bubble_
->avatar_menu_
.get();
130 void ClickProfileChooserViewLockButton() {
131 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
132 ui::EventTimeForNow(), 0, 0);
133 ProfileChooserView::profile_bubble_
->ButtonPressed(
134 ProfileChooserView::profile_bubble_
->lock_button_
, e
);
137 // Access the registry that has been prepared with at least one extension.
138 extensions::ExtensionRegistry
* GetPreparedRegistry(Profile
* signed_in
) {
139 extensions::ExtensionRegistry
* registry
=
140 extensions::ExtensionRegistry::Get(signed_in
);
141 const size_t initial_num_extensions
= registry
->enabled_extensions().size();
142 const extensions::Extension
* ext
= LoadExtension(
143 test_data_dir_
.AppendASCII("app"));
145 EXPECT_EQ(initial_num_extensions
+ 1,
146 registry
->enabled_extensions().size());
147 EXPECT_EQ(0U, registry
->blocked_extensions().size());
151 void WaitForUserManager() {
152 // If the User Manager hasn't shown yet, wait for it to show up.
153 // TODO(mlerman): As per crbug.com/450221, we should somehow observe when
154 // the UserManager is created and wait for that event.
155 if (!UserManager::IsShowing())
156 base::MessageLoop::current()->RunUntilIdle();
157 EXPECT_TRUE(UserManager::IsShowing());
160 content::WindowedNotificationObserver
* window_close_observer() {
161 return window_close_observer_
.get();
164 ProfileChooserView
* current_profile_bubble() {
165 return ProfileChooserView::profile_bubble_
;
168 views::View
* signin_current_profile_link() {
169 return ProfileChooserView::profile_bubble_
->signin_current_profile_link_
;
172 void ShowSigninView() {
173 DCHECK(current_profile_bubble());
174 DCHECK(current_profile_bubble()->avatar_menu_
);
175 current_profile_bubble()->ShowView(
176 profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN
,
177 current_profile_bubble()->avatar_menu_
.get());
178 base::MessageLoop::current()->RunUntilIdle();
182 scoped_ptr
<content::WindowedNotificationObserver
> window_close_observer_
;
184 DISALLOW_COPY_AND_ASSIGN(ProfileChooserViewExtensionsTest
);
187 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, SigninButtonHasFocus
) {
188 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
189 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
191 EXPECT_TRUE(signin_current_profile_link()->HasFocus());
194 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, ContentAreaHasFocus
) {
195 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
197 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
201 ASSERT_TRUE(current_profile_bubble());
202 views::View
* web_view
= FindWebView(current_profile_bubble());
203 ASSERT_TRUE(web_view
);
204 EXPECT_TRUE(web_view
->HasFocus());
207 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, ViewProfileUMA
) {
208 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
210 base::HistogramTester histograms
;
211 Profile
* profile
= browser()->profile();
212 profile
->GetPrefs()->SetInteger(prefs::kProfileAvatarTutorialShown
, 0);
214 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
216 histograms
.ExpectUniqueSample("Profile.NewAvatarMenu.Upgrade",
217 ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_VIEW
, 1);
220 // Flaky: http://crbug.com/450221
221 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
222 // UserManager to be showing.
223 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, DISABLED_LockProfile
) {
224 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
226 SetupProfilesForLock(browser()->profile());
227 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
229 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
230 AvatarMenu
* menu
= GetProfileChooserViewAvatarMenu();
231 EXPECT_FALSE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
233 ClickProfileChooserViewLockButton();
234 EXPECT_TRUE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
236 window_close_observer()->Wait();
237 EXPECT_TRUE(BrowserList::GetInstance(chrome::GetActiveDesktop())->empty());
239 WaitForUserManager();
240 // We need to hide the User Manager or else the process can't die.
244 // Flaky: http://crbug.com/450221
245 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
246 // UserManager to be showing.
247 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
248 DISABLED_LockProfileBlockExtensions
) {
249 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
250 // Make sure we have at least one enabled extension.
251 extensions::ExtensionRegistry
* registry
=
252 GetPreparedRegistry(browser()->profile());
253 SetupProfilesForLock(browser()->profile());
255 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
256 ClickProfileChooserViewLockButton();
257 window_close_observer()->Wait();
259 WaitForUserManager();
260 // Assert that the ExtensionService is blocked.
261 ASSERT_EQ(1U, registry
->blocked_extensions().size());
263 // We need to hide the User Manager or else the process can't die.
267 // Flaky: http://crbug.com/450221
268 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
269 // UserManager to be showing.
270 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
271 DISABLED_LockProfileNoBlockOtherProfileExtensions
) {
272 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
273 // Make sure we have at least one enabled extension.
274 extensions::ExtensionRegistry
* registry
=
275 GetPreparedRegistry(browser()->profile());
276 const size_t total_enabled_extensions
= registry
->enabled_extensions().size();
278 // Create a different profile and then lock it.
279 Profile
*signed_in
= CreateTestingProfile("signed_in");
280 SetupProfilesForLock(signed_in
);
281 extensions::ExtensionSystem::Get(signed_in
)->InitForRegularProfile(true);
282 Browser
* browser_to_lock
= CreateBrowser(signed_in
);
283 EXPECT_EQ(2U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
285 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser_to_lock
));
286 ClickProfileChooserViewLockButton();
287 window_close_observer()->Wait();
288 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
290 WaitForUserManager();
291 // Assert that the first profile's extensions are not blocked.
292 ASSERT_EQ(total_enabled_extensions
, registry
->enabled_extensions().size());
293 ASSERT_EQ(0U, registry
->blocked_extensions().size());
295 // We need to hide the User Manager or else the process can't die.