1 // Copyright (c) 2012 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/profiles/profile_list_desktop.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/prefs/pref_service_syncable.h"
14 #include "chrome/browser/profiles/avatar_menu_observer.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profiles_state.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "grit/generated_resources.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/l10n/l10n_util.h"
23 using base::ASCIIToUTF16
;
27 class MockObserver
: public AvatarMenuObserver
{
29 MockObserver() : count_(0) {}
30 virtual ~MockObserver() {}
32 virtual void OnAvatarMenuChanged(
33 AvatarMenu
* avatar_menu
) OVERRIDE
{
37 int change_count() const { return count_
; }
42 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
45 class ProfileListDesktopTest
: public testing::Test
{
47 ProfileListDesktopTest()
48 : manager_(TestingBrowserProcess::GetGlobal()) {
51 virtual void SetUp() {
52 ASSERT_TRUE(manager_
.SetUp());
53 #if defined(OS_CHROMEOS)
54 // AvatarMenu and multiple profiles works after user logged in.
55 manager_
.SetLoggedIn(true);
59 AvatarMenu
* GetAvatarMenu() {
60 // Reset the MockObserver.
61 mock_observer_
.reset(new MockObserver());
62 EXPECT_EQ(0, change_count());
65 avatar_menu_
.reset(new AvatarMenu(
66 manager()->profile_info_cache(),
69 avatar_menu_
->RebuildMenu();
70 EXPECT_EQ(0, change_count());
71 return avatar_menu_
.get();
74 TestingProfileManager
* manager() { return &manager_
; }
76 int change_count() const { return mock_observer_
->change_count(); }
79 TestingProfileManager manager_
;
80 scoped_ptr
<MockObserver
> mock_observer_
;
81 scoped_ptr
<AvatarMenu
> avatar_menu_
;
83 DISALLOW_COPY_AND_ASSIGN(ProfileListDesktopTest
);
86 TEST_F(ProfileListDesktopTest
, InitialCreation
) {
87 base::string16
name1(ASCIIToUTF16("Test 1"));
88 base::string16
name2(ASCIIToUTF16("Test 2"));
90 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
91 name1
, 0, std::string(),
92 TestingProfile::TestingFactories());
93 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
94 name2
, 0, std::string(),
95 TestingProfile::TestingFactories());
97 AvatarMenu
* model
= GetAvatarMenu();
98 EXPECT_EQ(0, change_count());
100 ASSERT_EQ(2U, model
->GetNumberOfItems());
102 const AvatarMenu::Item
& item1
= model
->GetItemAt(0);
103 EXPECT_EQ(0U, item1
.menu_index
);
104 EXPECT_EQ(name1
, item1
.name
);
106 const AvatarMenu::Item
& item2
= model
->GetItemAt(1);
107 EXPECT_EQ(1U, item2
.menu_index
);
108 EXPECT_EQ(name2
, item2
.name
);
111 TEST_F(ProfileListDesktopTest
, ActiveItem
) {
112 base::string16
name1(ASCIIToUTF16("Test 1"));
113 base::string16
name2(ASCIIToUTF16("Test 2"));
115 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
116 name1
, 0, std::string(),
117 TestingProfile::TestingFactories());
118 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
119 name2
, 0, std::string(),
120 TestingProfile::TestingFactories());
122 AvatarMenu
* model
= GetAvatarMenu();
123 ASSERT_EQ(2U, model
->GetNumberOfItems());
124 // TODO(jeremy): Expand test to verify active profile index other than 0
126 ASSERT_EQ(0U, model
->GetActiveProfileIndex());
129 TEST_F(ProfileListDesktopTest
, ModifyingNameResortsCorrectly
) {
130 base::string16
name1(ASCIIToUTF16("Alpha"));
131 base::string16
name2(ASCIIToUTF16("Beta"));
132 base::string16
newname1(ASCIIToUTF16("Gamma"));
134 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
135 name1
, 0, std::string(),
136 TestingProfile::TestingFactories());
137 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
138 name2
, 0, std::string(),
139 TestingProfile::TestingFactories());
141 AvatarMenu
* model
= GetAvatarMenu();
142 EXPECT_EQ(0, change_count());
144 ASSERT_EQ(2U, model
->GetNumberOfItems());
146 const AvatarMenu::Item
& item1
= model
->GetItemAt(0);
147 EXPECT_EQ(0U, item1
.menu_index
);
148 EXPECT_EQ(name1
, item1
.name
);
150 const AvatarMenu::Item
& item2
= model
->GetItemAt(1);
151 EXPECT_EQ(1U, item2
.menu_index
);
152 EXPECT_EQ(name2
, item2
.name
);
154 // Change name of the first profile, to trigger resorting of the profiles:
155 // now the first model should be named "beta", and the second be "gamma".
156 manager()->profile_info_cache()->SetNameOfProfileAtIndex(0, newname1
);
157 const AvatarMenu::Item
& item1next
= model
->GetItemAt(0);
158 EXPECT_GT(change_count(), 1);
159 EXPECT_EQ(0U, item1next
.menu_index
);
160 EXPECT_EQ(name2
, item1next
.name
);
162 const AvatarMenu::Item
& item2next
= model
->GetItemAt(1);
163 EXPECT_EQ(1U, item2next
.menu_index
);
164 EXPECT_EQ(newname1
, item2next
.name
);
167 TEST_F(ProfileListDesktopTest
, ChangeOnNotify
) {
168 base::string16
name1(ASCIIToUTF16("Test 1"));
169 base::string16
name2(ASCIIToUTF16("Test 2"));
171 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
172 name1
, 0, std::string(),
173 TestingProfile::TestingFactories());
174 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
175 name2
, 0, std::string(),
176 TestingProfile::TestingFactories());
178 AvatarMenu
* model
= GetAvatarMenu();
179 EXPECT_EQ(0, change_count());
180 EXPECT_EQ(2U, model
->GetNumberOfItems());
182 base::string16
name3(ASCIIToUTF16("Test 3"));
183 manager()->CreateTestingProfile("p3", scoped_ptr
<PrefServiceSyncable
>(),
184 name3
, 0, std::string(),
185 TestingProfile::TestingFactories());
187 // Four changes happened via the call to CreateTestingProfile: adding the
188 // profile to the cache, setting the user name, rebuilding the list of
189 // profiles after the name change, and changing the avatar.
190 // On Windows, an extra change happens to set the shortcut name for the
192 // TODO(michaelpg): Determine why five changes happen on ChromeOS and
193 // investigate other platforms.
194 EXPECT_GE(change_count(), 4);
195 ASSERT_EQ(3U, model
->GetNumberOfItems());
197 const AvatarMenu::Item
& item1
= model
->GetItemAt(0);
198 EXPECT_EQ(0U, item1
.menu_index
);
199 EXPECT_EQ(name1
, item1
.name
);
201 const AvatarMenu::Item
& item2
= model
->GetItemAt(1);
202 EXPECT_EQ(1U, item2
.menu_index
);
203 EXPECT_EQ(name2
, item2
.name
);
205 const AvatarMenu::Item
& item3
= model
->GetItemAt(2);
206 EXPECT_EQ(2U, item3
.menu_index
);
207 EXPECT_EQ(name3
, item3
.name
);
210 TEST_F(ProfileListDesktopTest
, ShowAvatarMenuInTrial
) {
211 // If multiprofile mode is not enabled, the trial will not be enabled, so it
213 if (!profiles::IsMultipleProfilesEnabled())
216 base::FieldTrialList
field_trial_list_(NULL
);
217 base::FieldTrialList::CreateFieldTrial("ShowProfileSwitcher", "AlwaysShow");
219 #if defined(OS_CHROMEOS)
220 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
222 EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
226 TEST_F(ProfileListDesktopTest
, DontShowAvatarMenu
) {
227 base::string16
name1(ASCIIToUTF16("Test 1"));
228 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
229 name1
, 0, std::string(),
230 TestingProfile::TestingFactories());
232 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
234 // If multiprofile mode is enabled, there are no other cases when we wouldn't
236 if (profiles::IsMultipleProfilesEnabled())
239 base::string16
name2(ASCIIToUTF16("Test 2"));
240 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
241 name2
, 0, std::string(),
242 TestingProfile::TestingFactories());
244 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
247 TEST_F(ProfileListDesktopTest
, ShowAvatarMenu
) {
248 // If multiprofile mode is not enabled then the menu is never shown.
249 if (!profiles::IsMultipleProfilesEnabled())
252 base::string16
name1(ASCIIToUTF16("Test 1"));
253 base::string16
name2(ASCIIToUTF16("Test 2"));
255 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
256 name1
, 0, std::string(),
257 TestingProfile::TestingFactories());
258 manager()->CreateTestingProfile("p2", scoped_ptr
<PrefServiceSyncable
>(),
259 name2
, 0, std::string(),
260 TestingProfile::TestingFactories());
262 #if defined(OS_CHROMEOS)
263 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
265 EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
269 TEST_F(ProfileListDesktopTest
, SyncState
) {
270 // If multiprofile mode is not enabled then the menu is never shown.
271 if (!profiles::IsMultipleProfilesEnabled())
274 manager()->CreateTestingProfile("p1", scoped_ptr
<PrefServiceSyncable
>(),
275 ASCIIToUTF16("Test 1"), 0, std::string(),
276 TestingProfile::TestingFactories());
278 // Add a managed user profile.
279 ProfileInfoCache
* cache
= manager()->profile_info_cache();
280 manager()->profile_info_cache()->AddProfileToCache(
281 cache
->GetUserDataDir().AppendASCII("p2"), ASCIIToUTF16("Test 2"),
282 base::string16(), 0, "TEST_ID");
284 AvatarMenu
* model
= GetAvatarMenu();
285 model
->RebuildMenu();
286 EXPECT_EQ(2U, model
->GetNumberOfItems());
288 // Now check that the sync_state of a managed user shows the managed user
289 // avatar label instead.
290 base::string16 managed_user_label
=
291 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL
);
292 const AvatarMenu::Item
& item1
= model
->GetItemAt(0);
293 EXPECT_NE(item1
.sync_state
, managed_user_label
);
295 const AvatarMenu::Item
& item2
= model
->GetItemAt(1);
296 EXPECT_EQ(item2
.sync_state
, managed_user_label
);