File: profile_list_desktop_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (363 lines) | stat: -rw-r--r-- 12,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/profiles/profile_list_desktop.h"

#include <string>

#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/profiles/avatar_menu_observer.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

using base::ASCIIToUTF16;

namespace {

class MockObserver : public AvatarMenuObserver {
 public:
  MockObserver() : count_(0) {}
  ~MockObserver() override {}

  void OnAvatarMenuChanged(AvatarMenu* avatar_menu) override { ++count_; }

  int change_count() const { return count_; }

 private:
  int count_;

  DISALLOW_COPY_AND_ASSIGN(MockObserver);
};

class ProfileListDesktopTest : public testing::Test {
 public:
  ProfileListDesktopTest()
      : manager_(TestingBrowserProcess::GetGlobal()) {
  }

  void SetUp() override {
    ASSERT_TRUE(manager_.SetUp());
#if defined(OS_CHROMEOS)
    // AvatarMenu and multiple profiles works after user logged in.
    manager_.SetLoggedIn(true);
#endif
  }

  AvatarMenu* GetAvatarMenu() {
    // Reset the MockObserver.
    mock_observer_.reset(new MockObserver());
    EXPECT_EQ(0, change_count());

    // Reset the model.
    avatar_menu_.reset(new AvatarMenu(
        manager()->profile_info_cache(),
        mock_observer_.get(),
        NULL));
    avatar_menu_->RebuildMenu();
    EXPECT_EQ(0, change_count());
    return avatar_menu_.get();
  }

  TestingProfileManager* manager() { return &manager_; }

  void AddOmittedProfile(std::string name) {
    ProfileInfoCache* cache = manager()->profile_info_cache();
    cache->AddProfileToCache(
        cache->GetUserDataDir().AppendASCII(name), ASCIIToUTF16(name),
        base::string16(), 0, "TEST_ID");
  }

  int change_count() const { return mock_observer_->change_count(); }

 private:
  TestingProfileManager manager_;
  scoped_ptr<MockObserver> mock_observer_;
  scoped_ptr<AvatarMenu> avatar_menu_;
  content::TestBrowserThreadBundle thread_bundle_;

  DISALLOW_COPY_AND_ASSIGN(ProfileListDesktopTest);
};

TEST_F(ProfileListDesktopTest, InitialCreation) {
  manager()->CreateTestingProfile("Test 1");
  manager()->CreateTestingProfile("Test 2");

  AvatarMenu* model = GetAvatarMenu();
  EXPECT_EQ(0, change_count());

  ASSERT_EQ(2U, model->GetNumberOfItems());

  const AvatarMenu::Item& item1 = model->GetItemAt(0);
  EXPECT_EQ(0U, item1.menu_index);
  EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name);

  const AvatarMenu::Item& item2 = model->GetItemAt(1);
  EXPECT_EQ(1U, item2.menu_index);
  EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name);
}

TEST_F(ProfileListDesktopTest, NoOmittedProfiles) {
  ProfileListDesktop profile_list(manager()->profile_info_cache());

  // Profiles are stored and listed alphabetically.
  manager()->CreateTestingProfile("1 included");
  manager()->CreateTestingProfile("2 included");
  manager()->CreateTestingProfile("3 included");
  manager()->CreateTestingProfile("4 included");

  profile_list.RebuildMenu();
  ASSERT_EQ(4u, profile_list.GetNumberOfItems());

  const AvatarMenu::Item& item1 = profile_list.GetItemAt(0);
  EXPECT_EQ(0u, item1.menu_index);
  EXPECT_EQ(0u, item1.profile_index);
  EXPECT_EQ(ASCIIToUTF16("1 included"), item1.name);

  const AvatarMenu::Item& item2 = profile_list.GetItemAt(1);
  EXPECT_EQ(1u, item2.menu_index);
  EXPECT_EQ(1u, item2.profile_index);
  EXPECT_EQ(ASCIIToUTF16("2 included"), item2.name);

  const AvatarMenu::Item& item3 = profile_list.GetItemAt(2);
  EXPECT_EQ(2u, item3.menu_index);
  EXPECT_EQ(2u, item3.profile_index);
  EXPECT_EQ(ASCIIToUTF16("3 included"), item3.name);

  const AvatarMenu::Item& item4 = profile_list.GetItemAt(3);
  EXPECT_EQ(3u, item4.menu_index);
  EXPECT_EQ(3u, item4.profile_index);
  EXPECT_EQ(ASCIIToUTF16("4 included"), item4.name);

  EXPECT_EQ(0u, profile_list.MenuIndexFromProfileIndex(0));
  EXPECT_EQ(1u, profile_list.MenuIndexFromProfileIndex(1));
  EXPECT_EQ(2u, profile_list.MenuIndexFromProfileIndex(2));
  EXPECT_EQ(3u, profile_list.MenuIndexFromProfileIndex(3));
}

TEST_F(ProfileListDesktopTest, WithOmittedProfiles) {
  ProfileListDesktop profile_list(manager()->profile_info_cache());

  // Profiles are stored and listed alphabetically.
  AddOmittedProfile("0 omitted");
  manager()->CreateTestingProfile("1 included");
  AddOmittedProfile("2 omitted");
  manager()->CreateTestingProfile("3 included");
  manager()->CreateTestingProfile("4 included");
  AddOmittedProfile("5 omitted");
  manager()->CreateTestingProfile("6 included");
  AddOmittedProfile("7 omitted");

  profile_list.RebuildMenu();
  ASSERT_EQ(4u, profile_list.GetNumberOfItems());

  const AvatarMenu::Item& item1 = profile_list.GetItemAt(0);
  EXPECT_EQ(0u, item1.menu_index);
  EXPECT_EQ(1u, item1.profile_index);
  EXPECT_EQ(ASCIIToUTF16("1 included"), item1.name);

  const AvatarMenu::Item& item2 = profile_list.GetItemAt(1);
  EXPECT_EQ(1u, item2.menu_index);
  EXPECT_EQ(3u, item2.profile_index);
  EXPECT_EQ(ASCIIToUTF16("3 included"), item2.name);

  const AvatarMenu::Item& item3 = profile_list.GetItemAt(2);
  EXPECT_EQ(2u, item3.menu_index);
  EXPECT_EQ(4u, item3.profile_index);
  EXPECT_EQ(ASCIIToUTF16("4 included"), item3.name);

  const AvatarMenu::Item& item4 = profile_list.GetItemAt(3);
  EXPECT_EQ(3u, item4.menu_index);
  EXPECT_EQ(6u, item4.profile_index);
  EXPECT_EQ(ASCIIToUTF16("6 included"), item4.name);

  EXPECT_EQ(0u, profile_list.MenuIndexFromProfileIndex(1));
  EXPECT_EQ(1u, profile_list.MenuIndexFromProfileIndex(3));
  EXPECT_EQ(2u, profile_list.MenuIndexFromProfileIndex(4));
  EXPECT_EQ(3u, profile_list.MenuIndexFromProfileIndex(6));
}

TEST_F(ProfileListDesktopTest, ActiveItem) {
  manager()->CreateTestingProfile("Test 1");
  manager()->CreateTestingProfile("Test 2");

  AvatarMenu* model = GetAvatarMenu();
  ASSERT_EQ(2U, model->GetNumberOfItems());
  // TODO(jeremy): Expand test to verify active profile index other than 0
  // crbug.com/100871
  ASSERT_EQ(0U, model->GetActiveProfileIndex());
}

TEST_F(ProfileListDesktopTest, ModifyingNameResortsCorrectly) {
  std::string name1("Alpha");
  std::string name2("Beta");
  std::string newname1("Gamma");

  manager()->CreateTestingProfile(name1);
  manager()->CreateTestingProfile(name2);

  AvatarMenu* model = GetAvatarMenu();
  EXPECT_EQ(0, change_count());

  ASSERT_EQ(2U, model->GetNumberOfItems());

  const AvatarMenu::Item& item1 = model->GetItemAt(0);
  EXPECT_EQ(0U, item1.menu_index);
  EXPECT_EQ(ASCIIToUTF16(name1), item1.name);

  const AvatarMenu::Item& item2 = model->GetItemAt(1);
  EXPECT_EQ(1U, item2.menu_index);
  EXPECT_EQ(ASCIIToUTF16(name2), item2.name);

  // Change name of the first profile, to trigger resorting of the profiles:
  // now the first model should be named "beta", and the second be "gamma".
  manager()->profile_info_cache()->SetNameOfProfileAtIndex(0,
        ASCIIToUTF16(newname1));
  const AvatarMenu::Item& item1next = model->GetItemAt(0);
  EXPECT_GT(change_count(), 1);
  EXPECT_EQ(0U, item1next.menu_index);
  EXPECT_EQ(ASCIIToUTF16(name2), item1next.name);

  const AvatarMenu::Item& item2next = model->GetItemAt(1);
  EXPECT_EQ(1U, item2next.menu_index);
  EXPECT_EQ(ASCIIToUTF16(newname1), item2next.name);
}

TEST_F(ProfileListDesktopTest, ChangeOnNotify) {
  manager()->CreateTestingProfile("Test 1");
  manager()->CreateTestingProfile("Test 2");

  AvatarMenu* model = GetAvatarMenu();
  EXPECT_EQ(0, change_count());
  EXPECT_EQ(2U, model->GetNumberOfItems());

  manager()->CreateTestingProfile("Test 3");

  // Four changes happened via the call to CreateTestingProfile: adding the
  // profile to the cache, setting the user name, rebuilding the list of
  // profiles after the name change, and changing the avatar.
  // On Windows, an extra change happens to set the shortcut name for the
  // profile.
  // TODO(michaelpg): Determine why five changes happen on ChromeOS and
  // investigate other platforms.
  EXPECT_GE(change_count(), 4);
  ASSERT_EQ(3U, model->GetNumberOfItems());

  const AvatarMenu::Item& item1 = model->GetItemAt(0);
  EXPECT_EQ(0U, item1.menu_index);
  EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name);

  const AvatarMenu::Item& item2 = model->GetItemAt(1);
  EXPECT_EQ(1U, item2.menu_index);
  EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name);

  const AvatarMenu::Item& item3 = model->GetItemAt(2);
  EXPECT_EQ(2U, item3.menu_index);
  EXPECT_EQ(ASCIIToUTF16("Test 3"), item3.name);
}

TEST_F(ProfileListDesktopTest, ShowAvatarMenuInTrial) {
  // If multiprofile mode is not enabled, the trial will not be enabled, so it
  // isn't tested.
  if (!profiles::IsMultipleProfilesEnabled())
    return;

  base::FieldTrialList field_trial_list_(NULL);
  base::FieldTrialList::CreateFieldTrial("ShowProfileSwitcher", "AlwaysShow");

#if defined(OS_CHROMEOS)
  EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
#else
  EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
#endif
}

TEST_F(ProfileListDesktopTest, DontShowOldAvatarMenuForSingleProfile) {
  switches::DisableNewAvatarMenuForTesting(
      base::CommandLine::ForCurrentProcess());

  manager()->CreateTestingProfile("Test 1");

  EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());

  // If multiprofile mode is enabled, there are no other cases when we wouldn't
  // show the menu.
  if (profiles::IsMultipleProfilesEnabled())
    return;

  manager()->CreateTestingProfile("Test 2");

  EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
}

TEST_F(ProfileListDesktopTest, AlwaysShowNewAvatarMenu) {
  // If multiprofile mode is not enabled then the menu is never shown.
  if (!profiles::IsMultipleProfilesEnabled())
    return;

  switches::EnableNewAvatarMenuForTesting(
      base::CommandLine::ForCurrentProcess());

  manager()->CreateTestingProfile("Test 1");

  EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
}

TEST_F(ProfileListDesktopTest, ShowAvatarMenu) {
  // If multiprofile mode is not enabled then the menu is never shown.
  if (!profiles::IsMultipleProfilesEnabled())
    return;

  manager()->CreateTestingProfile("Test 1");
  manager()->CreateTestingProfile("Test 2");

#if defined(OS_CHROMEOS)
  EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu());
#else
  EXPECT_TRUE(AvatarMenu::ShouldShowAvatarMenu());
#endif
}

TEST_F(ProfileListDesktopTest, SyncState) {
  // If multiprofile mode is not enabled then the menu is never shown.
  if (!profiles::IsMultipleProfilesEnabled())
    return;

  manager()->CreateTestingProfile("Test 1");

  // Add a managed user profile.
  ProfileInfoCache* cache = manager()->profile_info_cache();
  base::FilePath path = cache->GetUserDataDir().AppendASCII("p2");
  cache->AddProfileToCache(path, ASCIIToUTF16("Test 2"), base::string16(), 0,
                           "TEST_ID");
  cache->SetIsOmittedProfileAtIndex(cache->GetIndexOfProfileWithPath(path),
                                    false);

  AvatarMenu* model = GetAvatarMenu();
  model->RebuildMenu();
  EXPECT_EQ(2U, model->GetNumberOfItems());

  // Now check that the sync_state of a supervised user shows the supervised
  // user avatar label instead.
  base::string16 supervised_user_label =
      l10n_util::GetStringUTF16(IDS_SUPERVISED_USER_AVATAR_LABEL);
  const AvatarMenu::Item& item1 = model->GetItemAt(0);
  EXPECT_NE(item1.sync_state, supervised_user_label);

  const AvatarMenu::Item& item2 = model->GetItemAt(1);
  EXPECT_EQ(item2.sync_state, supervised_user_label);
}

}  // namespace