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 "chrome/browser/notifications/notification_conversion_helper.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/notifications/notification.h"
9 #include "chrome/browser/notifications/notification_test_util.h"
10 #include "chrome/common/extensions/api/notifications.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/message_center/notification.h"
14 #include "ui/message_center/notification_types.h"
17 class NotificationConversionHelperTest
: public testing::Test
{
19 NotificationConversionHelperTest() {}
21 virtual void SetUp() OVERRIDE
{}
23 virtual void TearDown() OVERRIDE
{}
26 scoped_ptr
<Notification
> CreateNotification(
27 message_center::NotificationType type
) {
28 message_center::RichNotificationData optional_fields
;
29 optional_fields
.priority
= 1;
30 optional_fields
.context_message
=
31 base::UTF8ToUTF16("I am a context message.");
32 optional_fields
.timestamp
= base::Time::FromDoubleT(12345678.9);
33 optional_fields
.buttons
.push_back(
34 message_center::ButtonInfo(base::UTF8ToUTF16("Button 1")));
35 optional_fields
.buttons
.push_back(
36 message_center::ButtonInfo(base::UTF8ToUTF16("Button 2")));
37 optional_fields
.clickable
= false;
39 if (type
== message_center::NOTIFICATION_TYPE_IMAGE
)
40 optional_fields
.image
= gfx::Image();
41 if (type
== message_center::NOTIFICATION_TYPE_MULTIPLE
) {
42 optional_fields
.items
.push_back(message_center::NotificationItem(
43 base::UTF8ToUTF16("Item 1 Title"),
44 base::UTF8ToUTF16("Item 1 Message")));
45 optional_fields
.items
.push_back(message_center::NotificationItem(
46 base::UTF8ToUTF16("Item 2 Title"),
47 base::UTF8ToUTF16("Item 2 Message")));
49 if (type
== message_center::NOTIFICATION_TYPE_PROGRESS
)
50 optional_fields
.progress
= 50;
52 NotificationDelegate
* delegate(new MockNotificationDelegate("id1"));
55 bitmap
.allocN32Pixels(1, 1);
56 bitmap
.eraseColor(SkColorSetRGB(1, 2, 3));
57 gfx::Image icon
= gfx::Image::CreateFrom1xBitmap(bitmap
);
59 scoped_ptr
<Notification
> notification(new Notification(
62 base::UTF8ToUTF16("Title"),
63 base::UTF8ToUTF16("This is a message."),
65 blink::WebTextDirectionDefault
,
66 message_center::NotifierId(message_center::NotifierId::APPLICATION
,
68 base::UTF8ToUTF16("Notifier's Name"),
69 base::UTF8ToUTF16("id1"),
73 return notification
.Pass();
77 DISALLOW_COPY_AND_ASSIGN(NotificationConversionHelperTest
);
80 TEST_F(NotificationConversionHelperTest
, NotificationToNotificationOptions
) {
81 // Create a notification of image type
82 scoped_ptr
<Notification
> notification1
=
83 CreateNotification(message_center::NOTIFICATION_TYPE_IMAGE
);
84 scoped_ptr
<extensions::api::notifications::NotificationOptions
> options1(
85 new extensions::api::notifications::NotificationOptions());
86 NotificationConversionHelper::NotificationToNotificationOptions(
87 *(notification1
), options1
.get());
89 EXPECT_EQ(options1
->type
,
90 extensions::api::notifications::TEMPLATE_TYPE_IMAGE
);
91 EXPECT_EQ(*(options1
->title
), "Title");
92 EXPECT_EQ(*(options1
->message
), "This is a message.");
93 EXPECT_EQ(*(options1
->priority
), 1);
94 EXPECT_EQ(*(options1
->context_message
), "I am a context message.");
95 EXPECT_FALSE(*(options1
->is_clickable
));
96 EXPECT_EQ(*(options1
->event_time
), 12345678.9);
97 EXPECT_EQ(options1
->buttons
->at(0)->title
, "Button 1");
98 EXPECT_EQ(options1
->buttons
->at(1)->title
, "Button 2");
100 EXPECT_EQ(options1
->icon_bitmap
->width
, 1);
101 EXPECT_EQ(options1
->icon_bitmap
->height
, 1);
103 // Create a notification of progress type
104 scoped_ptr
<Notification
> notification2
=
105 CreateNotification(message_center::NOTIFICATION_TYPE_PROGRESS
);
106 scoped_ptr
<extensions::api::notifications::NotificationOptions
> options2(
107 new extensions::api::notifications::NotificationOptions());
108 NotificationConversionHelper::NotificationToNotificationOptions(
109 *(notification2
), options2
.get());
110 EXPECT_EQ(options2
->type
,
111 extensions::api::notifications::TEMPLATE_TYPE_PROGRESS
);
112 EXPECT_EQ(*(options2
->progress
), 50);
114 // Create a notification of multiple type
115 scoped_ptr
<Notification
> notification3
=
116 CreateNotification(message_center::NOTIFICATION_TYPE_MULTIPLE
);
117 scoped_ptr
<extensions::api::notifications::NotificationOptions
> options3(
118 new extensions::api::notifications::NotificationOptions());
119 NotificationConversionHelper::NotificationToNotificationOptions(
120 *(notification3
), options3
.get());
121 EXPECT_EQ(options3
->type
, extensions::api::notifications::TEMPLATE_TYPE_LIST
);
122 EXPECT_EQ(options3
->items
->at(0)->title
, "Item 1 Title");
123 EXPECT_EQ(options3
->items
->at(0)->message
, "Item 1 Message");
124 EXPECT_EQ(options3
->items
->at(1)->title
, "Item 2 Title");
125 EXPECT_EQ(options3
->items
->at(1)->message
, "Item 2 Message");