| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 <map> |
| 6 #include <memory> |
| 7 #include <utility> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/message_center/fake_message_center.h" |
| 14 #include "ui/message_center/notification.h" |
| 15 #include "ui/message_center/notification_list.h" |
| 16 #include "ui/message_center/views/message_center_controller.h" |
| 17 #include "ui/message_center/views/message_list_view.h" |
| 18 #include "ui/message_center/views/notification_view.h" |
| 19 #include "ui/views/test/views_test_base.h" |
| 20 |
| 21 namespace message_center { |
| 22 |
| 23 static const char* kNotificationId1 = "notification id 1"; |
| 24 |
| 25 namespace { |
| 26 |
| 27 /* Types **********************************************************************/ |
| 28 |
| 29 enum CallType { GET_PREFERRED_SIZE, GET_HEIGHT_FOR_WIDTH, LAYOUT }; |
| 30 |
| 31 /* Instrumented/Mock NotificationView subclass ********************************/ |
| 32 |
| 33 class MockNotificationView : public NotificationView { |
| 34 public: |
| 35 class Test { |
| 36 public: |
| 37 virtual void RegisterCall(CallType type) = 0; |
| 38 }; |
| 39 |
| 40 MockNotificationView(MessageCenterController* controller, |
| 41 const Notification& notification, |
| 42 Test* test); |
| 43 ~MockNotificationView() override; |
| 44 |
| 45 gfx::Size GetPreferredSize() const override; |
| 46 int GetHeightForWidth(int w) const override; |
| 47 void Layout() override; |
| 48 |
| 49 private: |
| 50 Test* test_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(MockNotificationView); |
| 53 }; |
| 54 |
| 55 MockNotificationView::MockNotificationView(MessageCenterController* controller, |
| 56 const Notification& notification, |
| 57 Test* test) |
| 58 : NotificationView(controller, notification), test_(test) {} |
| 59 |
| 60 MockNotificationView::~MockNotificationView() {} |
| 61 |
| 62 gfx::Size MockNotificationView::GetPreferredSize() const { |
| 63 test_->RegisterCall(GET_PREFERRED_SIZE); |
| 64 DCHECK(child_count() > 0); |
| 65 return NotificationView::GetPreferredSize(); |
| 66 } |
| 67 |
| 68 int MockNotificationView::GetHeightForWidth(int width) const { |
| 69 test_->RegisterCall(GET_HEIGHT_FOR_WIDTH); |
| 70 DCHECK(child_count() > 0); |
| 71 return NotificationView::GetHeightForWidth(width); |
| 72 } |
| 73 |
| 74 void MockNotificationView::Layout() { |
| 75 test_->RegisterCall(LAYOUT); |
| 76 DCHECK(child_count() > 0); |
| 77 NotificationView::Layout(); |
| 78 } |
| 79 |
| 80 } // anonymous namespace |
| 81 |
| 82 /* Test fixture ***************************************************************/ |
| 83 |
| 84 class MessageListViewTest : public views::ViewsTestBase, |
| 85 public MockNotificationView::Test, |
| 86 public MessageListView::Observer, |
| 87 public MessageCenterController { |
| 88 public: |
| 89 MessageListViewTest() {} |
| 90 |
| 91 ~MessageListViewTest() override {} |
| 92 |
| 93 void SetUp() override { |
| 94 views::ViewsTestBase::SetUp(); |
| 95 |
| 96 message_list_view_.reset(new MessageListView(false /* top_down */)); |
| 97 message_list_view_->AddObserver(this); |
| 98 message_list_view_->set_owned_by_client(); |
| 99 |
| 100 widget_.reset(new views::Widget()); |
| 101 views::Widget::InitParams params = |
| 102 CreateParams(views::Widget::InitParams::TYPE_POPUP); |
| 103 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 104 params.bounds = gfx::Rect(50, 50, 650, 650); |
| 105 widget_->Init(params); |
| 106 views::View* root = widget_->GetRootView(); |
| 107 root->AddChildView(message_list_view_.get()); |
| 108 widget_->Show(); |
| 109 widget_->Activate(); |
| 110 } |
| 111 |
| 112 void TearDown() override { |
| 113 widget_->CloseNow(); |
| 114 widget_.reset(); |
| 115 |
| 116 message_list_view_->RemoveObserver(this); |
| 117 message_list_view_.reset(); |
| 118 |
| 119 views::ViewsTestBase::TearDown(); |
| 120 } |
| 121 |
| 122 protected: |
| 123 MessageListView* message_list_view() const { |
| 124 return message_list_view_.get(); |
| 125 } |
| 126 |
| 127 MockNotificationView* CreateNotificationView( |
| 128 const Notification& notification) { |
| 129 return new MockNotificationView(this, notification, this); |
| 130 } |
| 131 |
| 132 private: |
| 133 // MockNotificationView::Test override |
| 134 void RegisterCall(CallType type) override {} |
| 135 |
| 136 // MessageListView::Observer override |
| 137 void OnAllNotificationsCleared() override {} |
| 138 |
| 139 // MessageCenterController override: |
| 140 void ClickOnNotification(const std::string& notification_id) override {} |
| 141 void RemoveNotification(const std::string& notification_id, |
| 142 bool by_user) override {} |
| 143 std::unique_ptr<ui::MenuModel> CreateMenuModel( |
| 144 const NotifierId& notifier_id, |
| 145 const base::string16& display_source) override { |
| 146 NOTREACHED(); |
| 147 return nullptr; |
| 148 } |
| 149 bool HasClickedListener(const std::string& notification_id) override { |
| 150 return false; |
| 151 } |
| 152 void ClickOnNotificationButton(const std::string& notification_id, |
| 153 int button_index) override {} |
| 154 void ClickOnSettingsButton(const std::string& notification_id) override {} |
| 155 |
| 156 // Widget to host a MessageListView. |
| 157 std::unique_ptr<views::Widget> widget_; |
| 158 // MessageListView to be tested. |
| 159 std::unique_ptr<MessageListView> message_list_view_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(MessageListViewTest); |
| 162 }; |
| 163 |
| 164 /* Unit tests *****************************************************************/ |
| 165 |
| 166 TEST_F(MessageListViewTest, AddNotification) { |
| 167 // Create a dummy notification. |
| 168 auto notification_view = CreateNotificationView( |
| 169 Notification(NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1), |
| 170 base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message1"), |
| 171 gfx::Image(), base::UTF8ToUTF16("display source"), GURL(), |
| 172 NotifierId(NotifierId::APPLICATION, "extension_id"), |
| 173 message_center::RichNotificationData(), nullptr)); |
| 174 |
| 175 EXPECT_EQ(0, message_list_view()->child_count()); |
| 176 EXPECT_FALSE(message_list_view()->Contains(notification_view)); |
| 177 |
| 178 // Add a notification. |
| 179 message_list_view()->AddNotificationAt(notification_view, 0); |
| 180 |
| 181 EXPECT_EQ(1, message_list_view()->child_count()); |
| 182 EXPECT_TRUE(message_list_view()->Contains(notification_view)); |
| 183 } |
| 184 |
| 185 } // namespace |
| OLD | NEW |