1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_conversion_helper.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_test_util.h"
#include "chrome/common/extensions/api/notifications.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_types.h"
#include "url/gurl.h"
class NotificationConversionHelperTest : public testing::Test {
public:
NotificationConversionHelperTest() {}
void SetUp() override {}
void TearDown() override {}
protected:
scoped_ptr<Notification> CreateNotification(
message_center::NotificationType type) {
message_center::RichNotificationData optional_fields;
optional_fields.priority = 1;
optional_fields.context_message =
base::UTF8ToUTF16("I am a context message.");
optional_fields.timestamp = base::Time::FromDoubleT(12345678.9);
optional_fields.buttons.push_back(
message_center::ButtonInfo(base::UTF8ToUTF16("Button 1")));
optional_fields.buttons.push_back(
message_center::ButtonInfo(base::UTF8ToUTF16("Button 2")));
optional_fields.clickable = false;
if (type == message_center::NOTIFICATION_TYPE_IMAGE)
optional_fields.image = gfx::Image();
if (type == message_center::NOTIFICATION_TYPE_MULTIPLE) {
optional_fields.items.push_back(message_center::NotificationItem(
base::UTF8ToUTF16("Item 1 Title"),
base::UTF8ToUTF16("Item 1 Message")));
optional_fields.items.push_back(message_center::NotificationItem(
base::UTF8ToUTF16("Item 2 Title"),
base::UTF8ToUTF16("Item 2 Message")));
}
if (type == message_center::NOTIFICATION_TYPE_PROGRESS)
optional_fields.progress = 50;
NotificationDelegate* delegate(new MockNotificationDelegate("id1"));
SkBitmap bitmap;
bitmap.allocN32Pixels(1, 1);
bitmap.eraseColor(SkColorSetRGB(1, 2, 3));
gfx::Image icon = gfx::Image::CreateFrom1xBitmap(bitmap);
scoped_ptr<Notification> notification(new Notification(
type, base::UTF8ToUTF16("Title"),
base::UTF8ToUTF16("This is a message."), icon,
message_center::NotifierId(message_center::NotifierId::APPLICATION,
"Notifier 1"),
base::UTF8ToUTF16("Notifier's Name"), GURL(), "id1", optional_fields,
delegate));
return notification;
}
private:
DISALLOW_COPY_AND_ASSIGN(NotificationConversionHelperTest);
};
TEST_F(NotificationConversionHelperTest, NotificationToNotificationOptions) {
// Create a notification of image type
scoped_ptr<Notification> notification1 =
CreateNotification(message_center::NOTIFICATION_TYPE_IMAGE);
scoped_ptr<extensions::api::notifications::NotificationOptions> options1(
new extensions::api::notifications::NotificationOptions());
NotificationConversionHelper::NotificationToNotificationOptions(
*(notification1), options1.get());
EXPECT_EQ(options1->type,
extensions::api::notifications::TEMPLATE_TYPE_IMAGE);
EXPECT_EQ(*(options1->title), "Title");
EXPECT_EQ(*(options1->message), "This is a message.");
EXPECT_EQ(*(options1->priority), 1);
EXPECT_EQ(*(options1->context_message), "I am a context message.");
EXPECT_FALSE(*(options1->is_clickable));
EXPECT_EQ(*(options1->event_time), 12345678.9);
EXPECT_EQ(options1->buttons->at(0)->title, "Button 1");
EXPECT_EQ(options1->buttons->at(1)->title, "Button 2");
EXPECT_EQ(options1->icon_bitmap->width, 1);
EXPECT_EQ(options1->icon_bitmap->height, 1);
// Create a notification of progress type
scoped_ptr<Notification> notification2 =
CreateNotification(message_center::NOTIFICATION_TYPE_PROGRESS);
scoped_ptr<extensions::api::notifications::NotificationOptions> options2(
new extensions::api::notifications::NotificationOptions());
NotificationConversionHelper::NotificationToNotificationOptions(
*(notification2), options2.get());
EXPECT_EQ(options2->type,
extensions::api::notifications::TEMPLATE_TYPE_PROGRESS);
EXPECT_EQ(*(options2->progress), 50);
// Create a notification of multiple type
scoped_ptr<Notification> notification3 =
CreateNotification(message_center::NOTIFICATION_TYPE_MULTIPLE);
scoped_ptr<extensions::api::notifications::NotificationOptions> options3(
new extensions::api::notifications::NotificationOptions());
NotificationConversionHelper::NotificationToNotificationOptions(
*(notification3), options3.get());
EXPECT_EQ(options3->type, extensions::api::notifications::TEMPLATE_TYPE_LIST);
EXPECT_EQ(options3->items->at(0)->title, "Item 1 Title");
EXPECT_EQ(options3->items->at(0)->message, "Item 1 Message");
EXPECT_EQ(options3->items->at(1)->title, "Item 2 Title");
EXPECT_EQ(options3->items->at(1)->message, "Item 2 Message");
}
|