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