1 // Copyright 2014 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.
5 #include "ui/message_center/views/notification_view.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/gfx/image/image.h"
12 #include "ui/message_center/notification.h"
13 #include "ui/message_center/notification_list.h"
14 #include "ui/message_center/notification_types.h"
15 #include "ui/message_center/views/message_center_controller.h"
17 namespace message_center {
19 /* Test fixture ***************************************************************/
21 class NotificationViewTest : public testing::Test,
22 public MessageCenterController {
24 NotificationViewTest();
25 virtual ~NotificationViewTest();
27 virtual void SetUp() OVERRIDE;
28 virtual void TearDown() OVERRIDE;
30 NotificationView* notification_view() { return notification_view_.get(); }
31 Notification* notification() { return notification_.get(); }
32 RichNotificationData* data() { return data_.get(); }
34 // Overridden from MessageCenterController:
35 virtual void ClickOnNotification(const std::string& notification_id) OVERRIDE;
36 virtual void RemoveNotification(const std::string& notification_id,
37 bool by_user) OVERRIDE;
38 virtual scoped_ptr<ui::MenuModel> CreateMenuModel(
39 const NotifierId& notifier_id,
40 const base::string16& display_source) OVERRIDE;
41 virtual bool HasClickedListener(const std::string& notification_id) OVERRIDE;
42 virtual void ClickOnNotificationButton(const std::string& notification_id,
43 int button_index) OVERRIDE;
46 const gfx::Image CreateTestImage(int width, int height) {
47 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height));
50 const SkBitmap CreateBitmap(int width, int height) {
52 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
54 bitmap.eraseRGB(0, 255, 0);
59 scoped_ptr<RichNotificationData> data_;
60 scoped_ptr<Notification> notification_;
61 scoped_ptr<NotificationView> notification_view_;
63 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest);
66 NotificationViewTest::NotificationViewTest() {
69 NotificationViewTest::~NotificationViewTest() {
72 void NotificationViewTest::SetUp() {
73 // Create a dummy notification.
75 data_.reset(new RichNotificationData());
77 new Notification(NOTIFICATION_TYPE_BASE_FORMAT,
78 std::string("notification id"),
79 base::UTF8ToUTF16("title"),
80 base::UTF8ToUTF16("message"),
81 CreateTestImage(80, 80),
82 base::UTF8ToUTF16("display source"),
83 NotifierId(NotifierId::APPLICATION, "extension_id"),
86 notification_->set_small_image(CreateTestImage(16, 16));
87 notification_->set_image(CreateTestImage(320, 240));
89 // Then create a new NotificationView with that single notification.
90 notification_view_.reset(
91 NotificationView::Create(this, *notification_, true));
94 void NotificationViewTest::TearDown() {
95 notification_view_.reset();
98 void NotificationViewTest::ClickOnNotification(
99 const std::string& notification_id) {
100 // For this test, this method should not be invoked.
104 void NotificationViewTest::RemoveNotification(
105 const std::string& notification_id,
107 // For this test, this method should not be invoked.
111 scoped_ptr<ui::MenuModel> NotificationViewTest::CreateMenuModel(
112 const NotifierId& notifier_id,
113 const base::string16& display_source) {
114 // For this test, this method should not be invoked.
116 return scoped_ptr<ui::MenuModel>();
119 bool NotificationViewTest::HasClickedListener(
120 const std::string& notification_id) {
124 void NotificationViewTest::ClickOnNotificationButton(
125 const std::string& notification_id,
127 // For this test, this method should not be invoked.
131 /* Unit tests *****************************************************************/
133 TEST_F(NotificationViewTest, CreateOrUpdateTest) {
134 EXPECT_TRUE(NULL != notification_view()->title_view_);
135 EXPECT_TRUE(NULL != notification_view()->message_view_);
136 EXPECT_TRUE(NULL != notification_view()->icon_view_);
137 EXPECT_TRUE(NULL != notification_view()->image_view_);
139 notification()->set_image(gfx::Image());
140 notification()->set_title(base::ASCIIToUTF16(""));
141 notification()->set_message(base::ASCIIToUTF16(""));
142 notification()->set_icon(gfx::Image());
144 notification_view()->CreateOrUpdateViews(*notification());
145 EXPECT_TRUE(NULL == notification_view()->title_view_);
146 EXPECT_TRUE(NULL == notification_view()->message_view_);
147 EXPECT_TRUE(NULL == notification_view()->image_view_);
148 // We still expect an icon view for all layouts.
149 EXPECT_TRUE(NULL != notification_view()->icon_view_);
152 TEST_F(NotificationViewTest, TestLineLimits) {
153 notification()->set_image(CreateTestImage(0, 0));
154 notification()->set_context_message(base::ASCIIToUTF16(""));
155 notification_view()->CreateOrUpdateViews(*notification());
157 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(0, 360));
158 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(1, 360));
159 EXPECT_EQ(3, notification_view()->GetMessageLineLimit(2, 360));
161 notification()->set_image(CreateTestImage(2, 2));
162 notification_view()->CreateOrUpdateViews(*notification());
164 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(0, 360));
165 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360));
166 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360));
168 notification()->set_context_message(base::UTF8ToUTF16("foo"));
169 notification_view()->CreateOrUpdateViews(*notification());
171 EXPECT_TRUE(notification_view()->context_message_view_ != NULL);
173 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360));
174 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360));
175 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360));
178 } // namespace message_center