Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_list_desktop_unittest.cc
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.
4
5 #include "chrome/browser/profiles/profile_list_desktop.h"
6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/prefs/pref_service_syncable.h"
15 #include "chrome/browser/profiles/avatar_menu_observer.h"
16 #include "chrome/browser/profiles/profile_info_cache.h"
17 #include "chrome/browser/profiles/profiles_state.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "components/signin/core/common/profile_management_switches.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/base/l10n/l10n_util.h"
25
26 using base::ASCIIToUTF16;
27
28 namespace {
29
30 class MockObserver : public AvatarMenuObserver {
31  public:
32   MockObserver() : count_(0) {}
33   ~MockObserver() override {}
34
35   void OnAvatarMenuChanged(AvatarMenu* avatar_menu) override { ++count_; }
36
37   int change_count() const { return count_; }
38
39  private:
40   int count_;
41
42   DISALLOW_COPY_AND_ASSIGN(MockObserver);
43 };
44
45 class ProfileListDesktopTest : public testing::Test {
46  public:
47   ProfileListDesktopTest()
48       : manager_(TestingBrowserProcess::GetGlobal()) {
49   }
50
51   void SetUp() override {
52     ASSERT_TRUE(manager_.SetUp());
53 #if defined(OS_CHROMEOS)
54     // AvatarMenu and multiple profiles works after user logged in.
55     manager_.SetLoggedIn(true);
56 #endif
57   }
58
59   AvatarMenu* GetAvatarMenu() {
60     // Reset the MockObserver.
61     mock_observer_.reset(new MockObserver());
62     EXPECT_EQ(0, change_count());
63
64     // Reset the model.
65     avatar_menu_.reset(new AvatarMenu(
66         manager()->profile_info_cache(),
67         mock_observer_.get(),
68         NULL));
69     avatar_menu_->RebuildMenu();
70     EXPECT_EQ(0, change_count());
71     return avatar_menu_.get();
72   }
73
74   TestingProfileManager* manager() { return &manager_; }
75
76   void AddOmittedProfile(std::string name) {
77     ProfileInfoCache* cache = manager()->profile_info_cache();
78     cache->AddProfileToCache(
79         cache->GetUserDataDir().AppendASCII(name), ASCIIToUTF16(name),
80         base::string16(), 0, "TEST_ID");
81   }
82
83   int change_count() const { return mock_observer_->change_count(); }
84
85  private:
86   TestingProfileManager manager_;
87   scoped_ptr<MockObserver> mock_observer_;
88   scoped_ptr<AvatarMenu> avatar_menu_;
89   content::TestBrowserThreadBundle thread_bundle_;
90
91   DISALLOW_COPY_AND_ASSIGN(ProfileListDesktopTest);
92 };
93
94 TEST_F(ProfileListDesktopTest, InitialCreation) {
95   manager()->CreateTestingProfile("Test 1");
96   manager()->CreateTestingProfile("Test 2");
97
98   AvatarMenu* model = GetAvatarMenu();
99   EXPECT_EQ(0, change_count());
100
101   ASSERT_EQ(2U, model->GetNumberOfItems());
102
103   const AvatarMenu::Item& item1 = model->GetItemAt(0);
104   EXPECT_EQ(0U, item1.menu_index);
105   EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name);
106
107   const AvatarMenu::Item& item2 = model->GetItemAt(1);
108   EXPECT_EQ(1U, item2.menu_index);
109   EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name);
110 }
111
112 TEST_F(ProfileListDesktopTest, NoOmittedProfiles) {
113   ProfileListDesktop profile_list(manager()->profile_info_cache());
114
115   // Profiles are stored and listed alphabetically.
116   manager()->CreateTestingProfile("1 included");
117   manager()->CreateTestingProfile("2 included");
118   manager()->CreateTestingProfile("3 included");
119   manager()->CreateTestingProfile("4 included");
120
121   profile_list.RebuildMenu();
122   ASSERT_EQ(4u, profile_list.GetNumberOfItems());
123
124   const AvatarMenu::Item& item1 = profile_list.GetItemAt(0);
125   EXPECT_EQ(0u, item1.menu_index);
126   EXPECT_EQ(0u, item1.profile_index);
127   EXPECT_EQ(ASCIIToUTF16("1 included"), item1.name);
128
129   const AvatarMenu::Item& item2 = profile_list.GetItemAt(1);
130   EXPECT_EQ(1u, item2.menu_index);
131   EXPECT_EQ(1u, item2.profile_index);
132   EXPECT_EQ(ASCIIToUTF16("2 included"), item2.name);
133
134   const AvatarMenu::Item& item3 = profile_list.GetItemAt(2);
135   EXPECT_EQ(2u, item3.menu_index);
136   EXPECT_EQ(2u, item3.profile_index);
137   EXPECT_EQ(ASCIIToUTF16("3 included"), item3.name);
138
139   const AvatarMenu::Item& item4 = profile_list.GetItemAt(3);
140   EXPECT_EQ(3u, item4.menu_index);
141   EXPECT_EQ(3u, item4.profile_index);
142   EXPECT_EQ(ASCIIToUTF16("4 included"), item4.name);
143
144   EXPECT_EQ(0u, profile_list.MenuIndexFromProfileIndex(0));
145   EXPECT_EQ(1u, profile_list.MenuIndexFromProfileIndex(1));
146   EXPECT_EQ(2u, profile_list.MenuIndexFromProfileIndex(2));
147   EXPECT_EQ(3u, profile_list.MenuIndexFromProfileIndex(3));
148 }
149
150 TEST_F(ProfileListDesktopTest, WithOmittedProfiles) {
151   ProfileListDesktop profile_list(manager()->profile_info_cache());
152
153   // Profiles are stored and listed alphabetically.
154   AddOmittedProfile("0 omitted");
155   manager()->CreateTestingProfile("1 included");
156   AddOmittedProfile("2 omitted");
157   manager()->CreateTestingProfile("3 included");
158   manager()->CreateTestingProfile("4 included");
159   AddOmittedProfile("5 omitted");
160   manager()->CreateTestingProfile("6 included");
161   AddOmittedProfile("7 omitted");
162
163   profile_list.RebuildMenu();
164   ASSERT_EQ(4u, profile_list.GetNumberOfItems());
165
166   const AvatarMenu::Item& item1 = profile_list.GetItemAt(0);
167   EXPECT_EQ(0u, item1.menu_index);
168   EXPECT_EQ(1u, item1.profile_index);
169   EXPECT_EQ(ASCIIToUTF16("1 included"), item1.name);
170
171   const AvatarMenu::Item& item2 = profile_list.GetItemAt(1);
172   EXPECT_EQ(1u, item2.menu_index);
173   EXPECT_EQ(3u, item2.profile_index);
174   EXPECT_EQ(ASCIIToUTF16("3 included"), item2.name);
175
176   const AvatarMenu::Item& item3 = profile_list.GetItemAt(2);
177   EXPECT_EQ(2u, item3.menu_index);
178   EXPECT_EQ(4u, item3.profile_index);
179   EXPECT_EQ(ASCIIToUTF16("4 included"), item3.name);
180
181   const AvatarMenu::Item& item4 = profile_list.GetItemAt(3);
182   EXPECT_EQ(3u, item4.menu_index);
183   EXPECT_EQ(6u, item4.profile_index);
184   EXPECT_EQ(ASCIIToUTF16("6 included"), item4.name);
185
186   EXPECT_EQ(0u, profile_list.MenuIndexFromProfileIndex(1));
187   EXPECT_EQ(1u, profile_list.MenuIndexFromProfileIndex(3));
188   EXPECT_EQ(2u, profile_list.MenuIndexFromProfileIndex(4));
189   EXPECT_EQ(3u, profile_list.MenuIndexFromProfileIndex(6));
190 }
191
192 TEST_F(ProfileListDesktopTest, ActiveItem) {
193   manager()->CreateTestingProfile("Test 1");
194   manager()->CreateTestingProfile("Test 2");
195
196   AvatarMenu* model = GetAvatarMenu();
197   ASSERT_EQ(2U, model->GetNumberOfItems());
198   // TODO(jeremy): Expand test to verify active profile index other than 0
199   // crbug.com/100871
200   ASSERT_EQ(0U, model->GetActiveProfileIndex());
201 }
202
203 TEST_F(ProfileListDesktopTest, ModifyingNameResortsCorrectly) {
204   std::string name1("Alpha");
205   std::string name2("Beta");
206   std::string newname1("Gamma");
207
208   manager()->CreateTestingProfile(name1);
209   manager()->CreateTestingProfile(name2);
210
211   AvatarMenu* model = GetAvatarMenu();
212   EXPECT_EQ(0, change_count());
213
214   ASSERT_EQ(2U, model->GetNumberOfItems());
215
216   const AvatarMenu::Item& item1 = model->GetItemAt(0);
217   EXPECT_EQ(0U, item1.menu_index);
218   EXPECT_EQ(ASCIIToUTF16(name1), item1.name);
219
220   const AvatarMenu::Item& item2 = model->GetItemAt(1);
221   EXPECT_EQ(1U, item2.menu_index);
222   EXPECT_EQ(ASCIIToUTF16(name2), item2.name);
223
224   // Change name of the first profile, to trigger resorting of the profiles:
225   // now the first model should be named "beta", and the second be "gamma".
226   manager()->profile_info_cache()->SetNameOfProfileAtIndex(0,
227         ASCIIToUTF16(newname1));
228   const AvatarMenu::Item& item1next = model->GetItemAt(0);
229   EXPECT_GT(change_count(), 1);
230   EXPECT_EQ(0U, item1next.menu_index);
231   EXPECT_EQ(ASCIIToUTF16(name2), item1next.name);
232
233   const AvatarMenu::Item& item2next = model->GetItemAt(1);
234   EXPECT_EQ(1U, item2next.menu_index);
235   EXPECT_EQ(ASCIIToUTF16(newname1), item2next.name);
236 }
237
238 TEST_F(ProfileListDesktopTest, ChangeOnNotify) {
239   manager()->CreateTestingProfile("Test 1");
240   manager()->CreateTestingProfile("Test 2");
241
242   AvatarMenu* model = GetAvatarMenu();
243   EXPECT_EQ(0, change_count());
244   EXPECT_EQ(2U, model->GetNumberOfItems());
245
246   manager()->CreateTestingProfile("Test 3");
247
248   // Four changes happened via the call to CreateTestingProfile: adding the
249   // profile to the cache, setting the user name, rebuilding the list of
250   // profiles after the name change, and changing the avatar.
251   // On Windows, an extra change happens to set the shortcut name for the
252   // profile.
253   // TODO(michaelpg): Determine why five changes happen on ChromeOS and
254   // investigate other platforms.
255   EXPECT_GE(change_count(), 4);
256   ASSERT_EQ(3U, model->GetNumberOfItems());
257
258   const AvatarMenu::Item& item1 = model->GetItemAt(0);
259   EXPECT_EQ(0U, item1.menu_index);
260   EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name);
261
262   const AvatarMenu::Item& item2 = model->GetItemAt(1);
263   EXPECT_EQ(1U, item2.menu_index);
264   EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name);
265
266   const AvatarMenu::Item& item3 = model->GetItemAt(2);
267   EXPECT_EQ(2U, item3.menu_index);
268   EXPECT_EQ(ASCIIToUTF16("Test 3"), item3.name);
269 }
270
271 TEST_F(ProfileListDesktopTest, ShowAvatarMenuInTrial) {
272   // If multiprofile mode is not enabled, the trial will not be enabled, so it
273   // isn't tested.
274   if (!profiles::IsMultipleProfilesEnabled())
275     return;
276
277   base::FieldTrialList field_trial_list_(NULL);
278   base::FieldTrialList::CreateFieldTrial("ShowProfileSwitcher", "AlwaysShow");
279
280 #if defined(OS_CHROMEOS)
281   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
282 #else
283   EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
284 #endif
285 }
286
287 TEST_F(ProfileListDesktopTest, DontShowOldAvatarMenuForSingleProfile) {
288   switches::DisableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
289
290   manager()->CreateTestingProfile("Test 1");
291
292   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
293
294   // If multiprofile mode is enabled, there are no other cases when we wouldn't
295   // show the menu.
296   if (profiles::IsMultipleProfilesEnabled())
297     return;
298
299   manager()->CreateTestingProfile("Test 2");
300
301   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
302 }
303
304 TEST_F(ProfileListDesktopTest, AlwaysShowNewAvatarMenu) {
305   // If multiprofile mode is not enabled then the menu is never shown.
306   if (!profiles::IsMultipleProfilesEnabled())
307     return;
308
309   switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
310
311   manager()->CreateTestingProfile("Test 1");
312
313   EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
314 }
315
316 TEST_F(ProfileListDesktopTest, ShowAvatarMenu) {
317   // If multiprofile mode is not enabled then the menu is never shown.
318   if (!profiles::IsMultipleProfilesEnabled())
319     return;
320
321   manager()->CreateTestingProfile("Test 1");
322   manager()->CreateTestingProfile("Test 2");
323
324 #if defined(OS_CHROMEOS)
325   EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
326 #else
327   EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
328 #endif
329 }
330
331 TEST_F(ProfileListDesktopTest, SyncState) {
332   // If multiprofile mode is not enabled then the menu is never shown.
333   if (!profiles::IsMultipleProfilesEnabled())
334     return;
335
336   manager()->CreateTestingProfile("Test 1");
337
338   // Add a managed user profile.
339   ProfileInfoCache* cache = manager()->profile_info_cache();
340   base::FilePath path = cache->GetUserDataDir().AppendASCII("p2");
341   cache->AddProfileToCache(path, ASCIIToUTF16("Test 2"), base::string16(), 0,
342                            "TEST_ID");
343   cache->SetIsOmittedProfileAtIndex(cache->GetIndexOfProfileWithPath(path),
344                                     false);
345
346   AvatarMenu* model = GetAvatarMenu();
347   model->RebuildMenu();
348   EXPECT_EQ(2U, model->GetNumberOfItems());
349
350   // Now check that the sync_state of a supervised user shows the supervised
351   // user avatar label instead.
352   base::string16 supervised_user_label =
353       l10n_util::GetStringUTF16(IDS_SUPERVISED_USER_AVATAR_LABEL);
354   const AvatarMenu::Item& item1 = model->GetItemAt(0);
355   EXPECT_NE(item1.sync_state, supervised_user_label);
356
357   const AvatarMenu::Item& item2 = model->GetItemAt(1);
358   EXPECT_EQ(item2.sync_state, supervised_user_label);
359 }
360
361 }  // namespace