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
|
// Copyright (c) 2011 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/avatar_menu_model.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/profiles/avatar_menu_model_observer.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MockObserver : public AvatarMenuModelObserver {
public:
MockObserver() : count_(0) {}
virtual ~MockObserver() {}
virtual void OnAvatarMenuModelChanged(
AvatarMenuModel* avatar_menu_model) OVERRIDE{
++count_;
}
int change_count() { return count_; }
private:
int count_;
};
class AvatarMenuModelTest : public testing::Test {
public:
AvatarMenuModelTest()
: manager_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
}
virtual void SetUp() {
ASSERT_TRUE(manager_.SetUp());
}
Browser* browser() { return NULL; }
TestingProfileManager* manager() { return &manager_; }
private:
TestingProfileManager manager_;
};
TEST_F(AvatarMenuModelTest, InitialCreation) {
string16 name1(ASCIIToUTF16("Test 1"));
string16 name2(ASCIIToUTF16("Test 2"));
manager()->CreateTestingProfile("p1", name1, 0);
manager()->CreateTestingProfile("p2", name2, 0);
MockObserver observer;
EXPECT_EQ(0, observer.change_count());
AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser());
EXPECT_EQ(0, observer.change_count());
ASSERT_EQ(2U, model.GetNumberOfItems());
const AvatarMenuModel::Item& item1 = model.GetItemAt(0);
EXPECT_EQ(0U, item1.model_index);
EXPECT_EQ(name1, item1.name);
const AvatarMenuModel::Item& item2 = model.GetItemAt(1);
EXPECT_EQ(1U, item2.model_index);
EXPECT_EQ(name2, item2.name);
}
TEST_F(AvatarMenuModelTest, ActiveItem) {
string16 name1(ASCIIToUTF16("Test 1"));
string16 name2(ASCIIToUTF16("Test 2"));
manager()->CreateTestingProfile("p1", name1, 0);
manager()->CreateTestingProfile("p2", name2, 0);
MockObserver observer;
AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser());
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(AvatarMenuModelTest, ModifyingNameResortsCorrectly) {
string16 name1(ASCIIToUTF16("Alpha"));
string16 name2(ASCIIToUTF16("Beta"));
string16 newname1(ASCIIToUTF16("Gamma"));
manager()->CreateTestingProfile("p1", name1, 0);
manager()->CreateTestingProfile("p2", name2, 0);
MockObserver observer;
AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser());
EXPECT_EQ(0, observer.change_count());
ASSERT_EQ(2U, model.GetNumberOfItems());
const AvatarMenuModel::Item& item1 = model.GetItemAt(0);
EXPECT_EQ(0U, item1.model_index);
EXPECT_EQ(name1, item1.name);
const AvatarMenuModel::Item& item2 = model.GetItemAt(1);
EXPECT_EQ(1U, item2.model_index);
EXPECT_EQ(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, newname1);
const AvatarMenuModel::Item& item1next = model.GetItemAt(0);
EXPECT_GT(observer.change_count(), 1);
EXPECT_EQ(0U, item1next.model_index);
EXPECT_EQ(name2, item1next.name);
const AvatarMenuModel::Item& item2next = model.GetItemAt(1);
EXPECT_EQ(1U, item2next.model_index);
EXPECT_EQ(newname1, item2next.name);
}
TEST_F(AvatarMenuModelTest, ChangeOnNotify) {
string16 name1(ASCIIToUTF16("Test 1"));
string16 name2(ASCIIToUTF16("Test 2"));
manager()->CreateTestingProfile("p1", name1, 0);
manager()->CreateTestingProfile("p2", name2, 0);
MockObserver observer;
EXPECT_EQ(0, observer.change_count());
AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser());
EXPECT_EQ(0, observer.change_count());
EXPECT_EQ(2U, model.GetNumberOfItems());
string16 name3(ASCIIToUTF16("Test 3"));
manager()->CreateTestingProfile("p3", name3, 0);
// 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.
EXPECT_EQ(4, observer.change_count());
ASSERT_EQ(3U, model.GetNumberOfItems());
const AvatarMenuModel::Item& item1 = model.GetItemAt(0);
EXPECT_EQ(0U, item1.model_index);
EXPECT_EQ(name1, item1.name);
const AvatarMenuModel::Item& item2 = model.GetItemAt(1);
EXPECT_EQ(1U, item2.model_index);
EXPECT_EQ(name2, item2.name);
const AvatarMenuModel::Item& item3 = model.GetItemAt(2);
EXPECT_EQ(2U, item3.model_index);
EXPECT_EQ(name3, item3.name);
}
} // namespace
|