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 "content/child/notifications/notification_data_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/common/platform_notification_data.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
15 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h"
19 const char kNotificationTitle
[] = "My Notification";
20 const char kNotificationLang
[] = "nl";
21 const char kNotificationBody
[] = "Hello, world!";
22 const char kNotificationTag
[] = "my_tag";
23 const char kNotificationIconUrl
[] = "https://example.com/icon.png";
24 const int kNotificationVibrationPattern
[] = { 100, 200, 300 };
25 const unsigned char kNotificationData
[] = { 0xdf, 0xff, 0x0, 0x0, 0xff, 0xdf };
26 const char kAction1Name
[] = "btn1";
27 const char kAction1Title
[] = "Button 1";
28 const char kAction2Name
[] = "btn2";
29 const char kAction2Title
[] = "Button 2";
31 TEST(NotificationDataConversionsTest
, ToPlatformNotificationData
) {
32 std::vector
<int> vibration_pattern(
33 kNotificationVibrationPattern
,
34 kNotificationVibrationPattern
+ arraysize(kNotificationVibrationPattern
));
36 std::vector
<char> developer_data(
37 kNotificationData
, kNotificationData
+ arraysize(kNotificationData
));
39 blink::WebVector
<blink::WebNotificationAction
> web_actions(
40 static_cast<size_t>(2));
41 web_actions
[0].action
= blink::WebString::fromUTF8(kAction1Name
);
42 web_actions
[0].title
= blink::WebString::fromUTF8(kAction1Title
);
43 web_actions
[1].action
= blink::WebString::fromUTF8(kAction2Name
);
44 web_actions
[1].title
= blink::WebString::fromUTF8(kAction2Title
);
46 blink::WebNotificationData
web_data(
47 blink::WebString::fromUTF8(kNotificationTitle
),
48 blink::WebNotificationData::DirectionLeftToRight
,
49 blink::WebString::fromUTF8(kNotificationLang
),
50 blink::WebString::fromUTF8(kNotificationBody
),
51 blink::WebString::fromUTF8(kNotificationTag
),
52 blink::WebURL(GURL(kNotificationIconUrl
)),
53 blink::WebVector
<int>(vibration_pattern
),
55 blink::WebVector
<char>(developer_data
),
58 PlatformNotificationData platform_data
= ToPlatformNotificationData(web_data
);
59 EXPECT_EQ(base::ASCIIToUTF16(kNotificationTitle
), platform_data
.title
);
60 EXPECT_EQ(PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
,
61 platform_data
.direction
);
62 EXPECT_EQ(kNotificationLang
, platform_data
.lang
);
63 EXPECT_EQ(base::ASCIIToUTF16(kNotificationBody
), platform_data
.body
);
64 EXPECT_EQ(kNotificationTag
, platform_data
.tag
);
65 EXPECT_EQ(kNotificationIconUrl
, platform_data
.icon
.spec());
66 EXPECT_TRUE(platform_data
.silent
);
68 EXPECT_THAT(platform_data
.vibration_pattern
,
69 testing::ElementsAreArray(kNotificationVibrationPattern
));
71 ASSERT_EQ(developer_data
.size(), platform_data
.data
.size());
72 for (size_t i
= 0; i
< developer_data
.size(); ++i
)
73 EXPECT_EQ(developer_data
[i
], platform_data
.data
[i
]);
74 ASSERT_EQ(web_actions
.size(), platform_data
.actions
.size());
75 EXPECT_EQ(kAction1Name
, platform_data
.actions
[0].action
);
76 EXPECT_EQ(base::ASCIIToUTF16(kAction1Title
), platform_data
.actions
[0].title
);
77 EXPECT_EQ(kAction2Name
, platform_data
.actions
[1].action
);
78 EXPECT_EQ(base::ASCIIToUTF16(kAction2Title
), platform_data
.actions
[1].title
);
81 TEST(NotificationDataConversionsTest
, ToWebNotificationData
) {
82 std::vector
<int> vibration_pattern(
83 kNotificationVibrationPattern
,
84 kNotificationVibrationPattern
+ arraysize(kNotificationVibrationPattern
));
86 std::vector
<char> developer_data(
87 kNotificationData
, kNotificationData
+ arraysize(kNotificationData
));
89 PlatformNotificationData platform_data
;
90 platform_data
.title
= base::ASCIIToUTF16(kNotificationTitle
);
91 platform_data
.direction
=
92 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
;
93 platform_data
.lang
= kNotificationLang
;
94 platform_data
.body
= base::ASCIIToUTF16(kNotificationBody
);
95 platform_data
.tag
= kNotificationTag
;
96 platform_data
.icon
= GURL(kNotificationIconUrl
);
97 platform_data
.vibration_pattern
= vibration_pattern
;
98 platform_data
.silent
= true;
99 platform_data
.data
= developer_data
;
100 platform_data
.actions
.resize(2);
101 platform_data
.actions
[0].action
= kAction1Name
;
102 platform_data
.actions
[0].title
= base::ASCIIToUTF16(kAction1Title
);
103 platform_data
.actions
[1].action
= kAction2Name
;
104 platform_data
.actions
[1].title
= base::ASCIIToUTF16(kAction2Title
);
106 blink::WebNotificationData web_data
= ToWebNotificationData(platform_data
);
107 EXPECT_EQ(kNotificationTitle
, web_data
.title
);
108 EXPECT_EQ(blink::WebNotificationData::DirectionLeftToRight
,
110 EXPECT_EQ(kNotificationLang
, web_data
.lang
);
111 EXPECT_EQ(kNotificationBody
, web_data
.body
);
112 EXPECT_EQ(kNotificationTag
, web_data
.tag
);
113 EXPECT_EQ(kNotificationIconUrl
, web_data
.icon
.string());
115 ASSERT_EQ(vibration_pattern
.size(), web_data
.vibrate
.size());
116 for (size_t i
= 0; i
< vibration_pattern
.size(); ++i
)
117 EXPECT_EQ(vibration_pattern
[i
], web_data
.vibrate
[i
]);
119 EXPECT_TRUE(web_data
.silent
);
121 ASSERT_EQ(developer_data
.size(), web_data
.data
.size());
122 for (size_t i
= 0; i
< developer_data
.size(); ++i
)
123 EXPECT_EQ(developer_data
[i
], web_data
.data
[i
]);
125 ASSERT_EQ(platform_data
.actions
.size(), web_data
.actions
.size());
126 EXPECT_EQ(kAction1Name
, web_data
.actions
[0].action
);
127 EXPECT_EQ(kAction1Title
, web_data
.actions
[0].title
);
128 EXPECT_EQ(kAction2Name
, web_data
.actions
[1].action
);
129 EXPECT_EQ(kAction2Title
, web_data
.actions
[1].title
);
132 TEST(NotificationDataConversionsTest
, NotificationDataDirectionality
) {
133 std::map
<blink::WebNotificationData::Direction
,
134 PlatformNotificationData::Direction
> mappings
;
136 mappings
[blink::WebNotificationData::DirectionLeftToRight
] =
137 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
;
138 mappings
[blink::WebNotificationData::DirectionRightToLeft
] =
139 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
;
140 mappings
[blink::WebNotificationData::DirectionAuto
] =
141 PlatformNotificationData::DIRECTION_AUTO
;
143 for (const auto& pair
: mappings
) {
145 blink::WebNotificationData web_data
;
146 web_data
.direction
= pair
.first
;
148 PlatformNotificationData platform_data
=
149 ToPlatformNotificationData(web_data
);
150 EXPECT_EQ(pair
.second
, platform_data
.direction
);
153 PlatformNotificationData platform_data
;
154 platform_data
.direction
= pair
.second
;
156 blink::WebNotificationData web_data
=
157 ToWebNotificationData(platform_data
);
158 EXPECT_EQ(pair
.first
, web_data
.direction
);
163 } // namespace content