OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/avatar_menu_model.h" | 5 #include "chrome/browser/profiles/avatar_menu_model.h" |
6 | 6 |
7 #include "base/string16.h" | 7 #include "base/string16.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/profiles/avatar_menu_model_observer.h" | 9 #include "chrome/browser/profiles/avatar_menu_model_observer.h" |
| 10 #include "chrome/browser/profiles/fake_profile_info_interface.h" |
10 #include "chrome/browser/profiles/profile_info_interface.h" | 11 #include "chrome/browser/profiles/profile_info_interface.h" |
11 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
12 #include "chrome/test/base/testing_browser_process_test.h" | 13 #include "chrome/test/base/testing_browser_process_test.h" |
13 #include "content/common/notification_service.h" | 14 #include "content/common/notification_service.h" |
14 #include "grit/theme_resources.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "ui/base/resource/resource_bundle.h" | |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 class FakeProfileInfo : public ProfileInfoInterface { | |
21 public: | |
22 FakeProfileInfo() {} | |
23 virtual ~FakeProfileInfo() {} | |
24 | |
25 std::vector<AvatarMenuModel::Item*>* mock_profiles() { | |
26 return &profiles_; | |
27 } | |
28 | |
29 virtual size_t GetNumberOfProfiles() const OVERRIDE { | |
30 return profiles_.size(); | |
31 } | |
32 | |
33 virtual size_t GetIndexOfProfileWithPath( | |
34 const FilePath& profile_path) const OVERRIDE { | |
35 return std::string::npos; | |
36 } | |
37 | |
38 virtual string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE { | |
39 return profiles_[index]->name; | |
40 } | |
41 | |
42 virtual FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE { | |
43 return FilePath(); | |
44 } | |
45 | |
46 virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( | |
47 size_t index) const OVERRIDE { | |
48 return profiles_[index]->icon; | |
49 } | |
50 | |
51 private: | |
52 std::vector<AvatarMenuModel::Item*> profiles_; | |
53 }; | |
54 | |
55 class MockObserver : public AvatarMenuModelObserver { | 19 class MockObserver : public AvatarMenuModelObserver { |
56 public: | 20 public: |
57 MockObserver() : count_(0) {} | 21 MockObserver() : count_(0) {} |
58 virtual ~MockObserver() {} | 22 virtual ~MockObserver() {} |
59 | 23 |
60 virtual void OnAvatarMenuModelChanged(AvatarMenuModel* avatar_menu_model) { | 24 virtual void OnAvatarMenuModelChanged( |
| 25 AvatarMenuModel* avatar_menu_model) OVERRIDE{ |
61 ++count_; | 26 ++count_; |
62 } | 27 } |
63 | 28 |
64 int change_count() { return count_; } | 29 int change_count() { return count_; } |
65 | 30 |
66 private: | 31 private: |
67 int count_; | 32 int count_; |
68 }; | 33 }; |
69 | 34 |
70 class AvatarMenuModelTest : public TestingBrowserProcessTest { | 35 class AvatarMenuModelTest : public TestingBrowserProcessTest { |
71 public: | 36 public: |
72 FakeProfileInfo* cache() { | 37 FakeProfileInfo* cache() { |
73 return &cache_; | 38 return &cache_; |
74 } | 39 } |
75 | 40 |
76 Browser* browser() { | 41 Browser* browser() { |
77 return NULL; | 42 return NULL; |
78 } | 43 } |
79 | 44 |
80 const gfx::Image& GetTestImage() { | 45 const gfx::Image& GetTestImage() { |
81 return ResourceBundle::GetSharedInstance().GetImageNamed( | 46 return FakeProfileInfo::GetTestImage(); |
82 IDR_PROFILE_AVATAR_0); | |
83 } | 47 } |
84 | 48 |
85 private: | 49 private: |
86 FakeProfileInfo cache_; | 50 FakeProfileInfo cache_; |
87 }; | 51 }; |
88 | 52 |
89 TEST_F(AvatarMenuModelTest, InitialCreation) { | 53 TEST_F(AvatarMenuModelTest, InitialCreation) { |
90 std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); | 54 std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); |
91 | 55 |
92 AvatarMenuModel::Item profile1(0, GetTestImage()); | 56 AvatarMenuModel::Item profile1(0, GetTestImage()); |
93 profile1.name = ASCIIToUTF16("Test 1"); | 57 profile1.name = ASCIIToUTF16("Test 1"); |
94 profiles->push_back(&profile1); | 58 profiles->push_back(&profile1); |
95 | 59 |
96 AvatarMenuModel::Item profile2(1, GetTestImage()); | 60 AvatarMenuModel::Item profile2(1, GetTestImage()); |
97 profile2.name = ASCIIToUTF16("Test 2"); | 61 profile2.name = ASCIIToUTF16("Test 2"); |
98 profiles->push_back(&profile2); | 62 profiles->push_back(&profile2); |
99 | 63 |
100 MockObserver observer; | 64 MockObserver observer; |
101 EXPECT_EQ(0, observer.change_count()); | 65 EXPECT_EQ(0, observer.change_count()); |
102 | 66 |
103 AvatarMenuModel model(cache(), &observer, browser()); | 67 AvatarMenuModel model(cache(), &observer, browser()); |
104 EXPECT_EQ(1, observer.change_count()); | 68 EXPECT_EQ(0, observer.change_count()); |
105 | 69 |
106 ASSERT_EQ(2U, model.GetNumberOfItems()); | 70 ASSERT_EQ(2U, model.GetNumberOfItems()); |
107 | 71 |
108 const AvatarMenuModel::Item& item1 = model.GetItemAt(0); | 72 const AvatarMenuModel::Item& item1 = model.GetItemAt(0); |
109 EXPECT_EQ(0U, item1.model_index); | 73 EXPECT_EQ(0U, item1.model_index); |
110 EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); | 74 EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); |
111 | 75 |
112 const AvatarMenuModel::Item& item2 = model.GetItemAt(1); | 76 const AvatarMenuModel::Item& item2 = model.GetItemAt(1); |
113 EXPECT_EQ(1U, item2.model_index); | 77 EXPECT_EQ(1U, item2.model_index); |
114 EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name); | 78 EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name); |
115 } | 79 } |
116 | 80 |
117 | 81 |
118 TEST_F(AvatarMenuModelTest, ChangeOnNotify) { | 82 TEST_F(AvatarMenuModelTest, ChangeOnNotify) { |
119 std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); | 83 std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); |
120 | 84 |
121 AvatarMenuModel::Item profile1(0, GetTestImage()); | 85 AvatarMenuModel::Item profile1(0, GetTestImage()); |
122 profile1.name = ASCIIToUTF16("Test 1"); | 86 profile1.name = ASCIIToUTF16("Test 1"); |
123 profiles->push_back(&profile1); | 87 profiles->push_back(&profile1); |
124 | 88 |
125 AvatarMenuModel::Item profile2(1, GetTestImage()); | 89 AvatarMenuModel::Item profile2(1, GetTestImage()); |
126 profile2.name = ASCIIToUTF16("Test 2"); | 90 profile2.name = ASCIIToUTF16("Test 2"); |
127 profiles->push_back(&profile2); | 91 profiles->push_back(&profile2); |
128 | 92 |
129 MockObserver observer; | 93 MockObserver observer; |
130 EXPECT_EQ(0, observer.change_count()); | 94 EXPECT_EQ(0, observer.change_count()); |
131 | 95 |
132 AvatarMenuModel model(cache(), &observer, browser()); | 96 AvatarMenuModel model(cache(), &observer, browser()); |
133 EXPECT_EQ(1, observer.change_count()); | 97 EXPECT_EQ(0, observer.change_count()); |
134 EXPECT_EQ(2U, model.GetNumberOfItems()); | 98 EXPECT_EQ(2U, model.GetNumberOfItems()); |
135 | 99 |
136 AvatarMenuModel::Item profile3(2, GetTestImage()); | 100 AvatarMenuModel::Item profile3(2, GetTestImage()); |
137 profile3.name = ASCIIToUTF16("Test 3"); | 101 profile3.name = ASCIIToUTF16("Test 3"); |
138 profiles->insert(profiles->begin() + 1, &profile3); | 102 profiles->insert(profiles->begin() + 1, &profile3); |
139 | 103 |
140 NotificationService::current()->Notify( | 104 NotificationService::current()->Notify( |
141 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 105 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
142 NotificationService::AllSources(), | 106 NotificationService::AllSources(), |
143 NotificationService::NoDetails()); | 107 NotificationService::NoDetails()); |
144 EXPECT_EQ(2, observer.change_count()); | 108 EXPECT_EQ(1, observer.change_count()); |
145 ASSERT_EQ(3U, model.GetNumberOfItems()); | 109 ASSERT_EQ(3U, model.GetNumberOfItems()); |
146 | 110 |
147 const AvatarMenuModel::Item& item1 = model.GetItemAt(0); | 111 const AvatarMenuModel::Item& item1 = model.GetItemAt(0); |
148 EXPECT_EQ(0U, item1.model_index); | 112 EXPECT_EQ(0U, item1.model_index); |
149 EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); | 113 EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); |
150 | 114 |
151 const AvatarMenuModel::Item& item2 = model.GetItemAt(1); | 115 const AvatarMenuModel::Item& item2 = model.GetItemAt(1); |
152 EXPECT_EQ(1U, item2.model_index); | 116 EXPECT_EQ(1U, item2.model_index); |
153 EXPECT_EQ(ASCIIToUTF16("Test 3"), item2.name); | 117 EXPECT_EQ(ASCIIToUTF16("Test 3"), item2.name); |
154 | 118 |
155 const AvatarMenuModel::Item& item3 = model.GetItemAt(2); | 119 const AvatarMenuModel::Item& item3 = model.GetItemAt(2); |
156 EXPECT_EQ(2U, item3.model_index); | 120 EXPECT_EQ(2U, item3.model_index); |
157 EXPECT_EQ(ASCIIToUTF16("Test 2"), item3.name); | 121 EXPECT_EQ(ASCIIToUTF16("Test 2"), item3.name); |
158 } | 122 } |
159 | 123 |
160 } // namespace | 124 } // namespace |
OLD | NEW |