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"
32 // ChromeOS and mobile platforms don't have a ProfileChooserView.
33 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
37 Profile
* CreateTestingProfile(const std::string
& profile_name
) {
38 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
39 size_t starting_number_of_profiles
= profile_manager
->GetNumberOfProfiles();
42 PathService::Get(chrome::DIR_USER_DATA
, &path
);
43 path
= path
.AppendASCII(profile_name
);
44 if (!base::PathExists(path
) && !base::CreateDirectory(path
))
45 NOTREACHED() << "Could not create directory at " << path
.MaybeAsASCII();
48 Profile::CreateProfile(path
, NULL
, Profile::CREATE_MODE_SYNCHRONOUS
);
49 profile_manager
->RegisterTestingProfile(profile
, true, false);
50 EXPECT_EQ(starting_number_of_profiles
+ 1,
51 profile_manager
->GetNumberOfProfiles());
55 // Set up the profiles to enable Lock. Takes as parameter a profile that will be
56 // signed in, and also creates a supervised user (necessary for lock).
57 void SetupProfilesForLock(Profile
* signed_in
) {
58 const char* signed_in_email
= "me@google.com";
59 Profile
* supervised
= CreateTestingProfile("supervised");
60 ProfileInfoCache
* cache
= &g_browser_process
->profile_manager()->
61 GetProfileInfoCache();
62 cache
->SetUserNameOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
63 signed_in
->GetPath()), base::UTF8ToUTF16(signed_in_email
));
64 signed_in
->GetPrefs()->
65 SetString(prefs::kGoogleServicesHostedDomain
, "google.com");
66 cache
->SetSupervisedUserIdOfProfileAtIndex(cache
->GetIndexOfProfileWithPath(
67 supervised
->GetPath()), signed_in_email
);
69 EXPECT_TRUE(profiles::IsLockAvailable(signed_in
));
74 class ProfileChooserViewExtensionsTest
: public ExtensionBrowserTest
{
76 ProfileChooserViewExtensionsTest() {}
77 ~ProfileChooserViewExtensionsTest() override
{}
80 void SetUp() override
{
81 ExtensionBrowserTest::SetUp();
82 DCHECK(switches::IsNewAvatarMenu());
83 DCHECK(switches::IsNewProfileManagement());
86 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
87 ExtensionBrowserTest::SetUpCommandLine(command_line
);
88 switches::EnableNewProfileManagementForTesting(command_line
);
91 void OpenProfileChooserView(Browser
* browser
){
92 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(browser
);
93 NewAvatarButton
* button
= browser_view
->frame()->GetNewAvatarMenuButton();
95 NOTREACHED() << "NewAvatarButton not found.";
96 if (browser_view
->frame()->GetAvatarMenuButton())
97 NOTREACHED() << "Old Avatar Menu Button found.";
99 ProfileChooserView::close_on_deactivate_for_testing_
= false;
101 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
102 ui::EventTimeForNow(), 0, 0);
103 button
->NotifyClick(e
);
104 base::MessageLoop::current()->RunUntilIdle();
105 EXPECT_TRUE(ProfileChooserView::IsShowing());
107 // Create this observer before lock is pressed to avoid a race condition.
108 window_close_observer_
.reset(new content::WindowedNotificationObserver(
109 chrome::NOTIFICATION_BROWSER_CLOSED
,
110 content::Source
<Browser
>(browser
)));
113 AvatarMenu
* GetProfileChooserViewAvatarMenu() {
114 return ProfileChooserView::profile_bubble_
->avatar_menu_
.get();
117 void ClickProfileChooserViewLockButton() {
118 ui::MouseEvent
e(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
119 ui::EventTimeForNow(), 0, 0);
120 ProfileChooserView::profile_bubble_
->ButtonPressed(
121 ProfileChooserView::profile_bubble_
->lock_button_
, e
);
124 // Access the registry that has been prepared with at least one extension.
125 extensions::ExtensionRegistry
* GetPreparedRegistry(Profile
* signed_in
) {
126 extensions::ExtensionRegistry
* registry
=
127 extensions::ExtensionRegistry::Get(signed_in
);
128 const size_t initial_num_extensions
= registry
->enabled_extensions().size();
129 const extensions::Extension
* ext
= LoadExtension(
130 test_data_dir_
.AppendASCII("app"));
132 EXPECT_EQ(initial_num_extensions
+ 1,
133 registry
->enabled_extensions().size());
134 EXPECT_EQ(0U, registry
->blocked_extensions().size());
138 void WaitForUserManager() {
139 // If the User Manager hasn't shown yet, wait for it to show up.
140 // TODO(mlerman): As per crbug.com/450221, we should somehow observe when
141 // the UserManager is created and wait for that event.
142 if (!UserManager::IsShowing())
143 base::MessageLoop::current()->RunUntilIdle();
144 EXPECT_TRUE(UserManager::IsShowing());
147 content::WindowedNotificationObserver
* window_close_observer() {
148 return window_close_observer_
.get();
152 scoped_ptr
<content::WindowedNotificationObserver
> window_close_observer_
;
154 DISALLOW_COPY_AND_ASSIGN(ProfileChooserViewExtensionsTest
);
157 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, ViewProfileUMA
) {
158 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
160 base::HistogramTester histograms
;
161 Profile
* profile
= browser()->profile();
162 profile
->GetPrefs()->SetInteger(prefs::kProfileAvatarTutorialShown
, 0);
164 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
166 histograms
.ExpectUniqueSample("Profile.NewAvatarMenu.Upgrade",
167 ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_VIEW
, 1);
170 // Flaky: http://crbug.com/450221
171 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
172 // UserManager to be showing.
173 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
, DISABLED_LockProfile
) {
174 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
176 SetupProfilesForLock(browser()->profile());
177 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
179 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
180 AvatarMenu
* menu
= GetProfileChooserViewAvatarMenu();
181 EXPECT_FALSE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
183 ClickProfileChooserViewLockButton();
184 EXPECT_TRUE(menu
->GetItemAt(menu
->GetActiveProfileIndex()).signin_required
);
186 window_close_observer()->Wait();
187 EXPECT_TRUE(BrowserList::GetInstance(chrome::GetActiveDesktop())->empty());
189 WaitForUserManager();
190 // We need to hide the User Manager or else the process can't die.
194 // Flaky: http://crbug.com/450221
195 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
196 // UserManager to be showing.
197 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
198 DISABLED_LockProfileBlockExtensions
) {
199 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
200 // Make sure we have at least one enabled extension.
201 extensions::ExtensionRegistry
* registry
=
202 GetPreparedRegistry(browser()->profile());
203 SetupProfilesForLock(browser()->profile());
205 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser()));
206 ClickProfileChooserViewLockButton();
207 window_close_observer()->Wait();
209 WaitForUserManager();
210 // Assert that the ExtensionService is blocked.
211 ASSERT_EQ(1U, registry
->blocked_extensions().size());
213 // We need to hide the User Manager or else the process can't die.
217 // Flaky: http://crbug.com/450221
218 // WaitForUserManager()'s RunUntilIdle isn't always sufficient for the
219 // UserManager to be showing.
220 IN_PROC_BROWSER_TEST_F(ProfileChooserViewExtensionsTest
,
221 DISABLED_LockProfileNoBlockOtherProfileExtensions
) {
222 ASSERT_TRUE(profiles::IsMultipleProfilesEnabled());
223 // Make sure we have at least one enabled extension.
224 extensions::ExtensionRegistry
* registry
=
225 GetPreparedRegistry(browser()->profile());
226 const size_t total_enabled_extensions
= registry
->enabled_extensions().size();
228 // Create a different profile and then lock it.
229 Profile
*signed_in
= CreateTestingProfile("signed_in");
230 SetupProfilesForLock(signed_in
);
231 extensions::ExtensionSystem::Get(signed_in
)->InitForRegularProfile(true);
232 Browser
* browser_to_lock
= CreateBrowser(signed_in
);
233 EXPECT_EQ(2U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
235 ASSERT_NO_FATAL_FAILURE(OpenProfileChooserView(browser_to_lock
));
236 ClickProfileChooserViewLockButton();
237 window_close_observer()->Wait();
238 EXPECT_EQ(1U, BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
240 WaitForUserManager();
241 // Assert that the first profile's extensions are not blocked.
242 ASSERT_EQ(total_enabled_extensions
, registry
->enabled_extensions().size());
243 ASSERT_EQ(0U, registry
->blocked_extensions().size());
245 // We need to hide the User Manager or else the process can't die.