1 // Copyright 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.
7 #include "ash/ash_switches.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
13 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/profiles/avatar_menu.h"
17 #include "chrome/browser/profiles/avatar_menu_observer.h"
18 #include "chrome/browser/profiles/profile_info_cache.h"
19 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/testing_profile_manager.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using base::ASCIIToUTF16
;
31 class MockObserver
: public AvatarMenuObserver
{
33 MockObserver() : count_(0) {}
34 ~MockObserver() override
{}
36 void OnAvatarMenuChanged(AvatarMenu
* avatar_menu
) override
{ ++count_
; }
38 int change_count() const { return count_
; }
43 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
50 class ProfileListChromeOSTest
: public testing::Test
{
52 ProfileListChromeOSTest()
53 : manager_(TestingBrowserProcess::GetGlobal()) {
56 void SetUp() override
{
57 ASSERT_TRUE(manager_
.SetUp());
59 // AvatarMenu and multiple profiles works after user logged in.
60 manager_
.SetLoggedIn(true);
62 // Initialize the UserManager singleton to a fresh FakeChromeUserManager
64 user_manager_enabler_
.reset(
65 new ScopedUserManagerEnabler(new FakeChromeUserManager
));
68 FakeChromeUserManager
* GetFakeChromeUserManager() {
69 return static_cast<FakeChromeUserManager
*>(
70 user_manager::UserManager::Get());
73 void AddProfile(base::string16 name
, bool log_in
) {
74 std::string email_string
= base::UTF16ToASCII(name
) + "@example.com";
76 // Add a user to the fake user manager.
77 GetFakeChromeUserManager()->AddUser(email_string
);
79 GetFakeChromeUserManager()->LoginUser(email_string
);
81 // Create a profile for the user.
82 manager()->CreateTestingProfile(email_string
);
85 AvatarMenu
* GetAvatarMenu() {
86 // Reset the MockObserver.
87 mock_observer_
.reset(new MockObserver());
88 EXPECT_EQ(0, change_count());
91 avatar_menu_
.reset(new AvatarMenu(
92 manager()->profile_info_cache(),
95 avatar_menu_
->RebuildMenu();
96 EXPECT_EQ(0, change_count());
97 return avatar_menu_
.get();
100 void ActiveUserChanged(const base::string16
& name
) {
101 std::string email_string
= base::UTF16ToASCII(name
) + "@example.com";
102 GetFakeChromeUserManager()->SwitchActiveUser(email_string
);
105 TestingProfileManager
* manager() { return &manager_
; }
107 int change_count() const { return mock_observer_
->change_count(); }
110 content::TestBrowserThreadBundle thread_bundle_
;
111 TestingProfileManager manager_
;
112 scoped_ptr
<MockObserver
> mock_observer_
;
113 scoped_ptr
<ScopedUserManagerEnabler
> user_manager_enabler_
;
114 scoped_ptr
<AvatarMenu
> avatar_menu_
;
115 ChromeShellDelegate chrome_shell_delegate_
;
117 DISALLOW_COPY_AND_ASSIGN(ProfileListChromeOSTest
);
120 TEST_F(ProfileListChromeOSTest
, InitialCreation
) {
121 base::string16
name1(ASCIIToUTF16("p1"));
123 AddProfile(name1
, true);
125 AvatarMenu
* menu
= GetAvatarMenu();
127 ASSERT_EQ(1U, menu
->GetNumberOfItems());
129 const AvatarMenu::Item
& item1
= menu
->GetItemAt(0);
130 EXPECT_EQ(0U, item1
.menu_index
);
131 EXPECT_EQ(name1
, item1
.name
);
134 TEST_F(ProfileListChromeOSTest
, ShowLoggedInUsers
) {
135 base::string16
name1(ASCIIToUTF16("p1"));
136 base::string16
name2(ASCIIToUTF16("p2"));
137 base::string16
name3(ASCIIToUTF16("p3"));
138 base::string16
name4(ASCIIToUTF16("p4"));
140 AddProfile(name1
, true);
141 AddProfile(name2
, false);
142 AddProfile(name3
, true);
143 AddProfile(name4
, false);
145 AvatarMenu
* menu
= GetAvatarMenu();
147 ASSERT_EQ(2U, menu
->GetNumberOfItems());
149 const AvatarMenu::Item
& item1
= menu
->GetItemAt(0);
150 EXPECT_EQ(0U, item1
.menu_index
);
151 EXPECT_EQ(name1
, item1
.name
);
153 const AvatarMenu::Item
& item3
= menu
->GetItemAt(1);
154 EXPECT_EQ(1U, item3
.menu_index
);
155 EXPECT_EQ(name3
, item3
.name
);
158 TEST_F(ProfileListChromeOSTest
, DontShowSupervisedUsers
) {
159 base::string16
name1(ASCIIToUTF16("p1"));
160 base::string16
supervised_name(ASCIIToUTF16("p2@example.com"));
162 AddProfile(name1
, true);
164 // Add a managed user profile.
165 ProfileInfoCache
* cache
= manager()->profile_info_cache();
166 manager()->profile_info_cache()->AddProfileToCache(
167 cache
->GetUserDataDir().AppendASCII("p2"), supervised_name
, std::string(),
168 base::string16(), 0, "TEST_ID");
170 GetFakeChromeUserManager()->AddUser(base::UTF16ToASCII(supervised_name
));
172 AvatarMenu
* menu
= GetAvatarMenu();
173 ASSERT_EQ(1U, menu
->GetNumberOfItems());
175 const AvatarMenu::Item
& item1
= menu
->GetItemAt(0);
176 EXPECT_EQ(0U, item1
.menu_index
);
177 EXPECT_EQ(name1
, item1
.name
);
180 TEST_F(ProfileListChromeOSTest
, ShowAddProfileLink
) {
181 base::string16
name1(ASCIIToUTF16("p1.com"));
182 base::string16
name2(ASCIIToUTF16("p2.com"));
184 AddProfile(name1
, true);
185 AddProfile(name2
, false);
187 AvatarMenu
* menu
= GetAvatarMenu();
189 ASSERT_EQ(1U, menu
->GetNumberOfItems());
190 EXPECT_TRUE(menu
->ShouldShowAddNewProfileLink());
193 TEST_F(ProfileListChromeOSTest
, DontShowAddProfileLink
) {
194 base::string16
name1(ASCIIToUTF16("p1.com"));
195 base::string16
name2(ASCIIToUTF16("p2.com"));
197 AddProfile(name1
, true);
198 AddProfile(name2
, true);
200 AvatarMenu
* menu
= GetAvatarMenu();
202 ASSERT_EQ(2U, menu
->GetNumberOfItems());
203 EXPECT_FALSE(menu
->ShouldShowAddNewProfileLink());
206 TEST_F(ProfileListChromeOSTest
, ActiveItem
) {
207 base::string16
name1(ASCIIToUTF16("p1.com"));
208 base::string16
name2(ASCIIToUTF16("p2.com"));
210 AddProfile(name1
, true);
211 AddProfile(name2
, true);
213 ActiveUserChanged(name1
);
215 AvatarMenu
* menu
= GetAvatarMenu();
217 ASSERT_EQ(2U, menu
->GetNumberOfItems());
218 // TODO(jeremy): Expand test to verify active profile index other than 0
220 ASSERT_EQ(0U, menu
->GetActiveProfileIndex());
223 TEST_F(ProfileListChromeOSTest
, ModifyingNameResortsCorrectly
) {
224 base::string16
name1(ASCIIToUTF16("Alpha"));
225 base::string16
name2(ASCIIToUTF16("Beta"));
226 base::string16
newname1(ASCIIToUTF16("Gamma"));
228 AddProfile(name1
, true);
229 AddProfile(name2
, true);
231 AvatarMenu
* menu
= GetAvatarMenu();
232 EXPECT_EQ(0, change_count());
233 ASSERT_EQ(2U, menu
->GetNumberOfItems());
235 const AvatarMenu::Item
& item1
= menu
->GetItemAt(0);
236 EXPECT_EQ(0U, item1
.menu_index
);
237 EXPECT_EQ(name1
, item1
.name
);
239 const AvatarMenu::Item
& item2
= menu
->GetItemAt(1);
240 EXPECT_EQ(1U, item2
.menu_index
);
241 EXPECT_EQ(name2
, item2
.name
);
243 // Change name of the first profile, to trigger resorting of the profiles:
244 // now the first menu item should be named "beta", and the second be "gamma".
245 GetFakeChromeUserManager()->SaveUserDisplayName(
246 base::UTF16ToASCII(name1
) + "@example.com", newname1
);
247 manager()->profile_info_cache()->SetNameOfProfileAtIndex(0, newname1
);
248 EXPECT_EQ(1, change_count());
250 const AvatarMenu::Item
& item1next
= menu
->GetItemAt(0);
251 EXPECT_EQ(0U, item1next
.menu_index
);
252 EXPECT_EQ(name2
, item1next
.name
);
254 const AvatarMenu::Item
& item2next
= menu
->GetItemAt(1);
255 EXPECT_EQ(1U, item2next
.menu_index
);
256 EXPECT_EQ(newname1
, item2next
.name
);
259 TEST_F(ProfileListChromeOSTest
, ChangeOnNotify
) {
260 base::string16
name1(ASCIIToUTF16("p1.com"));
261 base::string16
name2(ASCIIToUTF16("p2.com"));
263 AddProfile(name1
, true);
264 AddProfile(name2
, true);
266 AvatarMenu
* menu
= GetAvatarMenu();
267 EXPECT_EQ(2U, menu
->GetNumberOfItems());
268 EXPECT_EQ(0, change_count());
270 base::string16
name3(ASCIIToUTF16("p3.com"));
271 AddProfile(name3
, true);
273 // Three changes happened via the call to CreateTestingProfile: adding the
274 // profile to the cache, setting the user name (which rebuilt the list of
275 // profiles after the name change), and changing the avatar.
276 EXPECT_EQ(change_count(), 3);
277 ASSERT_EQ(3U, menu
->GetNumberOfItems());
279 const AvatarMenu::Item
& item1
= menu
->GetItemAt(0);
280 EXPECT_EQ(0U, item1
.menu_index
);
281 EXPECT_EQ(name1
, item1
.name
);
283 const AvatarMenu::Item
& item2
= menu
->GetItemAt(1);
284 EXPECT_EQ(1U, item2
.menu_index
);
285 EXPECT_EQ(name2
, item2
.name
);
287 const AvatarMenu::Item
& item3
= menu
->GetItemAt(2);
288 EXPECT_EQ(2U, item3
.menu_index
);
289 EXPECT_EQ(name3
, item3
.name
);
292 TEST_F(ProfileListChromeOSTest
, DontShowAvatarMenu
) {
293 // If in the new M-32 UX mode the icon gets shown, the menu will not.
294 base::string16
name1(ASCIIToUTF16("p1"));
295 base::string16
name2(ASCIIToUTF16("p2"));
297 AddProfile(name1
, true);
299 // Should only show avatar menu with multiple users.
300 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
302 AddProfile(name2
, false);
304 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
307 } // namespace chromeos