- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / profiles / profile_list_chromeos_unittest.cc
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.
4
5 #include <string>
6
7 #include "ash/ash_switches.h"
8 #include "base/command_line.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/chromeos/login/fake_user_manager.h"
14 #include "chrome/browser/prefs/pref_service_syncable.h"
15 #include "chrome/browser/profiles/avatar_menu.h"
16 #include "chrome/browser/profiles/avatar_menu_observer.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile_manager.h"
23 #include "components/variations/entropy_provider.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 namespace {
28
29 // As defined in /chromeos/dbus/cryptohome_client.cc.
30 static const char kUserIdHashSuffix[] = "-hash";
31
32 class MockObserver : public AvatarMenuObserver {
33  public:
34   MockObserver() : count_(0) {}
35   virtual ~MockObserver() {}
36
37   virtual void OnAvatarMenuChanged(
38       AvatarMenu* avatar_menu) OVERRIDE {
39     ++count_;
40   }
41
42   int change_count() const { return count_; }
43
44  private:
45   int count_;
46
47   DISALLOW_COPY_AND_ASSIGN(MockObserver);
48 };
49
50 }  // namespace
51
52 namespace chromeos {
53
54 class ProfileListChromeOSTest : public testing::Test {
55  public:
56   ProfileListChromeOSTest()
57       : manager_(TestingBrowserProcess::GetGlobal()) {
58   }
59
60   virtual void SetUp() {
61     ASSERT_TRUE(manager_.SetUp());
62
63     // AvatarMenu and multiple profiles works after user logged in.
64     manager_.SetLoggedIn(true);
65
66     // We only instantiate UserMenuModel if multi-profile mode is enabled.
67     CommandLine* cl = CommandLine::ForCurrentProcess();
68     cl->AppendSwitch(switches::kMultiProfiles);
69
70     field_trial_list_.reset(new base::FieldTrialList(
71         new metrics::SHA1EntropyProvider("42")));
72     base::FieldTrialList::CreateTrialsFromString(
73         "ChromeOSUseMultiProfiles/Enable/",
74         base::FieldTrialList::ACTIVATE_TRIALS);
75
76     // Initialize the UserManager singleton to a fresh FakeUserManager instance.
77     user_manager_enabler_.reset(
78         new ScopedUserManagerEnabler(new FakeUserManager));
79   }
80
81   FakeUserManager* GetFakeUserManager() {
82     return static_cast<FakeUserManager*>(UserManager::Get());
83   }
84
85   void AddProfile(string16 name, bool log_in) {
86     std::string email_string = UTF16ToASCII(name) + "@example.com";
87
88     // Add a user to the fake user manager.
89     GetFakeUserManager()->AddUser(email_string);
90     if (log_in) {
91       GetFakeUserManager()->UserLoggedIn(
92           email_string,
93           email_string + kUserIdHashSuffix,
94           false);
95     }
96
97     // Create a profile for the user.
98     manager()->CreateTestingProfile(
99         chrome::kProfileDirPrefix + email_string + kUserIdHashSuffix,
100         scoped_ptr<PrefServiceSyncable>(),
101         ASCIIToUTF16(email_string), 0, std::string());
102   }
103
104   AvatarMenu* GetAvatarMenu() {
105     // Reset the MockObserver.
106     mock_observer_.reset(new MockObserver());
107     EXPECT_EQ(0, change_count());
108
109     // Reset the menu.
110     avatar_menu_.reset(new AvatarMenu(
111         manager()->profile_info_cache(),
112         mock_observer_.get(),
113         NULL));
114     avatar_menu_->RebuildMenu();
115     EXPECT_EQ(0, change_count());
116     return avatar_menu_.get();
117   }
118
119   TestingProfileManager* manager() { return &manager_; }
120
121   int change_count() const { return mock_observer_->change_count(); }
122
123  private:
124   TestingProfileManager manager_;
125   scoped_ptr<MockObserver> mock_observer_;
126   scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
127   scoped_ptr<AvatarMenu> avatar_menu_;
128   ChromeShellDelegate chrome_shell_delegate_;
129   scoped_ptr<base::FieldTrialList> field_trial_list_;
130
131   DISALLOW_COPY_AND_ASSIGN(ProfileListChromeOSTest);
132 };
133
134 TEST_F(ProfileListChromeOSTest, InitialCreation) {
135   string16 name1(ASCIIToUTF16("p1"));
136
137   AddProfile(name1, true);
138
139   AvatarMenu* menu = GetAvatarMenu();
140
141   ASSERT_EQ(1U, menu->GetNumberOfItems());
142
143   const AvatarMenu::Item& item1 = menu->GetItemAt(0);
144   EXPECT_EQ(0U, item1.menu_index);
145   EXPECT_EQ(name1, item1.name);
146 }
147
148 TEST_F(ProfileListChromeOSTest, ShowLoggedInUsers) {
149   string16 name1(ASCIIToUTF16("p1"));
150   string16 name2(ASCIIToUTF16("p2"));
151   string16 name3(ASCIIToUTF16("p3"));
152   string16 name4(ASCIIToUTF16("p4"));
153
154   AddProfile(name1, true);
155   AddProfile(name2, false);
156   AddProfile(name3, true);
157   AddProfile(name4, false);
158
159   AvatarMenu* menu = GetAvatarMenu();
160
161   ASSERT_EQ(2U, menu->GetNumberOfItems());
162
163   const AvatarMenu::Item& item1 = menu->GetItemAt(0);
164   EXPECT_EQ(0U, item1.menu_index);
165   EXPECT_EQ(name1, item1.name);
166
167   const AvatarMenu::Item& item3 = menu->GetItemAt(1);
168   EXPECT_EQ(1U, item3.menu_index);
169   EXPECT_EQ(name3, item3.name);
170 }
171
172 TEST_F(ProfileListChromeOSTest, DontShowManagedUsers) {
173   string16 name1(ASCIIToUTF16("p1"));
174   string16 managed_name(ASCIIToUTF16("p2@example.com"));
175
176   AddProfile(name1, true);
177
178   // Add a managed user profile.
179   ProfileInfoCache* cache = manager()->profile_info_cache();
180   manager()->profile_info_cache()->AddProfileToCache(
181       cache->GetUserDataDir().AppendASCII("p2"), managed_name,
182       string16(), 0, "TEST_ID");
183
184   GetFakeUserManager()->AddUser(UTF16ToASCII(managed_name));
185
186   AvatarMenu* menu = GetAvatarMenu();
187   ASSERT_EQ(1U, menu->GetNumberOfItems());
188
189   const AvatarMenu::Item& item1 = menu->GetItemAt(0);
190   EXPECT_EQ(0U, item1.menu_index);
191   EXPECT_EQ(name1, item1.name);
192 }
193
194 TEST_F(ProfileListChromeOSTest, ShowAddProfileLink) {
195   string16 name1(ASCIIToUTF16("p1.com"));
196   string16 name2(ASCIIToUTF16("p2.com"));
197
198   AddProfile(name1, true);
199   AddProfile(name2, false);
200
201   AvatarMenu* menu = GetAvatarMenu();
202
203   ASSERT_EQ(1U, menu->GetNumberOfItems());
204   EXPECT_TRUE(menu->ShouldShowAddNewProfileLink());
205 }
206
207 TEST_F(ProfileListChromeOSTest, DontShowAddProfileLink) {
208   string16 name1(ASCIIToUTF16("p1.com"));
209   string16 name2(ASCIIToUTF16("p2.com"));
210
211   AddProfile(name1, true);
212   AddProfile(name2, true);
213
214   AvatarMenu* menu = GetAvatarMenu();
215
216   ASSERT_EQ(2U, menu->GetNumberOfItems());
217   EXPECT_FALSE(menu->ShouldShowAddNewProfileLink());
218 }
219
220 TEST_F(ProfileListChromeOSTest, ActiveItem) {
221   string16 name1(ASCIIToUTF16("p1.com"));
222   string16 name2(ASCIIToUTF16("p2.com"));
223
224   AddProfile(name1, true);
225   AddProfile(name2, true);
226
227   AvatarMenu* menu = GetAvatarMenu();
228
229   ASSERT_EQ(2U, menu->GetNumberOfItems());
230   // TODO(jeremy): Expand test to verify active profile index other than 0
231   // crbug.com/100871
232   ASSERT_EQ(0U, menu->GetActiveProfileIndex());
233 }
234
235 TEST_F(ProfileListChromeOSTest, ModifyingNameResortsCorrectly) {
236   string16 name1(ASCIIToUTF16("Alpha"));
237   string16 name2(ASCIIToUTF16("Beta"));
238   string16 newname1(ASCIIToUTF16("Gamma"));
239
240   AddProfile(name1, true);
241   AddProfile(name2, true);
242
243   AvatarMenu* menu = GetAvatarMenu();
244
245   ASSERT_EQ(2U, menu->GetNumberOfItems());
246
247   const AvatarMenu::Item& item1 = menu->GetItemAt(0);
248   EXPECT_EQ(0U, item1.menu_index);
249   EXPECT_EQ(name1, item1.name);
250
251   const AvatarMenu::Item& item2 = menu->GetItemAt(1);
252   EXPECT_EQ(1U, item2.menu_index);
253   EXPECT_EQ(name2, item2.name);
254
255   // Change name of the first profile, to trigger resorting of the profiles:
256   // now the first menu item should be named "beta", and the second be "gamma".
257   GetFakeUserManager()->SaveUserDisplayName(
258       UTF16ToASCII(name1) + "@example.com", newname1);
259   manager()->profile_info_cache()->SetNameOfProfileAtIndex(0, newname1);
260
261   const AvatarMenu::Item& item1next = menu->GetItemAt(0);
262   EXPECT_GT(change_count(), 1);
263   EXPECT_EQ(0U, item1next.menu_index);
264   EXPECT_EQ(name2, item1next.name);
265
266   const AvatarMenu::Item& item2next = menu->GetItemAt(1);
267   EXPECT_EQ(1U, item2next.menu_index);
268   EXPECT_EQ(newname1, item2next.name);
269 }
270
271 TEST_F(ProfileListChromeOSTest, ChangeOnNotify) {
272   string16 name1(ASCIIToUTF16("p1.com"));
273   string16 name2(ASCIIToUTF16("p2.com"));
274
275   AddProfile(name1, true);
276   AddProfile(name2, true);
277
278   AvatarMenu* menu = GetAvatarMenu();
279   EXPECT_EQ(2U, menu->GetNumberOfItems());
280
281   string16 name3(ASCIIToUTF16("p3.com"));
282   AddProfile(name3, true);
283
284   // Four changes happened via the call to CreateTestingProfile: adding the
285   // profile to the cache, setting the user name, rebuilding the list of
286   // profiles after the name change, and changing the avatar.
287   // TODO(michaelpg): Determine why actual change number does not match comment.
288   EXPECT_GE(change_count(), 4);
289   ASSERT_EQ(3U, menu->GetNumberOfItems());
290
291   const AvatarMenu::Item& item1 = menu->GetItemAt(0);
292   EXPECT_EQ(0U, item1.menu_index);
293   EXPECT_EQ(name1, item1.name);
294
295   const AvatarMenu::Item& item2 = menu->GetItemAt(1);
296   EXPECT_EQ(1U, item2.menu_index);
297   EXPECT_EQ(name2, item2.name);
298
299   const AvatarMenu::Item& item3 = menu->GetItemAt(2);
300   EXPECT_EQ(2U, item3.menu_index);
301   EXPECT_EQ(name3, item3.name);
302 }
303
304 TEST_F(ProfileListChromeOSTest, DontShowAvatarMenu) {
305   // If in the new M-32 UX mode the icon gets shown, the menu will not.
306   string16 name1(ASCIIToUTF16("p1"));
307   string16 name2(ASCIIToUTF16("p2"));
308
309   AddProfile(name1, true);
310
311   // Should only show avatar menu with multiple users.
312   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
313
314   AddProfile(name2, false);
315
316   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
317 }
318
319 TEST_F(ProfileListChromeOSTest, ShowAvatarMenuInM31) {
320   // In M-31 mode, the menu will get shown.
321   CommandLine* cl = CommandLine::ForCurrentProcess();
322   cl->AppendSwitch(ash::switches::kAshEnableFullMultiProfileMode);
323
324   string16 name1(ASCIIToUTF16("p1"));
325   string16 name2(ASCIIToUTF16("p2"));
326
327   AddProfile(name1, true);
328
329   // Should only show avatar menu with multiple users.
330   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
331
332   AddProfile(name2, false);
333
334   EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
335 }
336
337 }  // namespace chromeos