Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / message_center / views / notification_view_unittest.cc
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.
4
5 #include "ui/message_center/views/notification_view.h"
6
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"
16
17 namespace message_center {
18
19 /* Test fixture ***************************************************************/
20
21 class NotificationViewTest : public testing::Test,
22                              public MessageCenterController {
23  public:
24   NotificationViewTest();
25   virtual ~NotificationViewTest();
26
27   virtual void SetUp() OVERRIDE;
28   virtual void TearDown() OVERRIDE;
29
30   NotificationView* notification_view() { return notification_view_.get(); }
31   Notification* notification() { return notification_.get(); }
32   RichNotificationData* data() { return data_.get(); }
33
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;
44
45  protected:
46   const gfx::Image CreateTestImage(int width, int height) {
47     return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height));
48   }
49
50   const SkBitmap CreateBitmap(int width, int height) {
51     SkBitmap bitmap;
52     bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
53     bitmap.allocPixels();
54     bitmap.eraseRGB(0, 255, 0);
55     return bitmap;
56   }
57
58  private:
59   scoped_ptr<RichNotificationData> data_;
60   scoped_ptr<Notification> notification_;
61   scoped_ptr<NotificationView> notification_view_;
62
63   DISALLOW_COPY_AND_ASSIGN(NotificationViewTest);
64 };
65
66 NotificationViewTest::NotificationViewTest() {
67 }
68
69 NotificationViewTest::~NotificationViewTest() {
70 }
71
72 void NotificationViewTest::SetUp() {
73   // Create a dummy notification.
74   SkBitmap bitmap;
75   data_.reset(new RichNotificationData());
76   notification_.reset(
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"),
84                        *data_,
85                        NULL));
86   notification_->set_small_image(CreateTestImage(16, 16));
87   notification_->set_image(CreateTestImage(320, 240));
88
89   // Then create a new NotificationView with that single notification.
90   notification_view_.reset(
91       NotificationView::Create(this, *notification_, true));
92 }
93
94 void NotificationViewTest::TearDown() {
95   notification_view_.reset();
96 }
97
98 void NotificationViewTest::ClickOnNotification(
99     const std::string& notification_id) {
100   // For this test, this method should not be invoked.
101   NOTREACHED();
102 }
103
104 void NotificationViewTest::RemoveNotification(
105     const std::string& notification_id,
106     bool by_user) {
107   // For this test, this method should not be invoked.
108   NOTREACHED();
109 }
110
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.
115   NOTREACHED();
116   return scoped_ptr<ui::MenuModel>();
117 }
118
119 bool NotificationViewTest::HasClickedListener(
120     const std::string& notification_id) {
121   return true;
122 }
123
124 void NotificationViewTest::ClickOnNotificationButton(
125     const std::string& notification_id,
126     int button_index) {
127   // For this test, this method should not be invoked.
128   NOTREACHED();
129 }
130
131 /* Unit tests *****************************************************************/
132
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_);
138
139   notification()->set_image(gfx::Image());
140   notification()->set_title(base::ASCIIToUTF16(""));
141   notification()->set_message(base::ASCIIToUTF16(""));
142   notification()->set_icon(gfx::Image());
143
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_);
150 }
151
152 TEST_F(NotificationViewTest, TestLineLimits) {
153   notification()->set_image(CreateTestImage(0, 0));
154   notification()->set_context_message(base::ASCIIToUTF16(""));
155   notification_view()->CreateOrUpdateViews(*notification());
156
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));
160
161   notification()->set_image(CreateTestImage(2, 2));
162   notification_view()->CreateOrUpdateViews(*notification());
163
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));
167
168   notification()->set_context_message(base::UTF8ToUTF16("foo"));
169   notification_view()->CreateOrUpdateViews(*notification());
170
171   EXPECT_TRUE(notification_view()->context_message_view_ != NULL);
172
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));
176 }
177
178 }  // namespace message_center