Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / child / notifications / notification_data_conversions_unittest.cc
blob93e247d197251aac895c58cc298b44cdddad726a
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"
7 #include <stdint.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"
17 namespace content {
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 blink::WebNotificationData web_data;
33 web_data.title = blink::WebString::fromUTF8(kNotificationTitle);
34 web_data.direction = blink::WebNotificationData::DirectionLeftToRight;
35 web_data.lang = blink::WebString::fromUTF8(kNotificationLang);
36 web_data.body = blink::WebString::fromUTF8(kNotificationBody);
37 web_data.tag = blink::WebString::fromUTF8(kNotificationTag);
38 web_data.icon = blink::WebURL(GURL(kNotificationIconUrl));
39 web_data.vibrate = blink::WebVector<int>(
40 kNotificationVibrationPattern, arraysize(kNotificationVibrationPattern));
41 web_data.silent = true;
42 web_data.requireInteraction = true;
43 web_data.data = blink::WebVector<char>(kNotificationData,
44 arraysize(kNotificationData));
46 web_data.actions = blink::WebVector<blink::WebNotificationAction>(
47 static_cast<size_t>(2));
48 web_data.actions[0].action = blink::WebString::fromUTF8(kAction1Name);
49 web_data.actions[0].title = blink::WebString::fromUTF8(kAction1Title);
50 web_data.actions[1].action = blink::WebString::fromUTF8(kAction2Name);
51 web_data.actions[1].title = blink::WebString::fromUTF8(kAction2Title);
53 PlatformNotificationData platform_data = ToPlatformNotificationData(web_data);
54 EXPECT_EQ(base::ASCIIToUTF16(kNotificationTitle), platform_data.title);
55 EXPECT_EQ(PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT,
56 platform_data.direction);
57 EXPECT_EQ(kNotificationLang, platform_data.lang);
58 EXPECT_EQ(base::ASCIIToUTF16(kNotificationBody), platform_data.body);
59 EXPECT_EQ(kNotificationTag, platform_data.tag);
60 EXPECT_EQ(kNotificationIconUrl, platform_data.icon.spec());
61 EXPECT_TRUE(platform_data.silent);
62 EXPECT_TRUE(platform_data.require_interaction);
64 EXPECT_THAT(platform_data.vibration_pattern,
65 testing::ElementsAreArray(kNotificationVibrationPattern));
67 ASSERT_EQ(web_data.data.size(), platform_data.data.size());
68 for (size_t i = 0; i < web_data.data.size(); ++i)
69 EXPECT_EQ(web_data.data[i], platform_data.data[i]);
70 ASSERT_EQ(web_data.actions.size(), platform_data.actions.size());
71 EXPECT_EQ(kAction1Name, platform_data.actions[0].action);
72 EXPECT_EQ(base::ASCIIToUTF16(kAction1Title), platform_data.actions[0].title);
73 EXPECT_EQ(kAction2Name, platform_data.actions[1].action);
74 EXPECT_EQ(base::ASCIIToUTF16(kAction2Title), platform_data.actions[1].title);
77 TEST(NotificationDataConversionsTest, ToWebNotificationData) {
78 std::vector<int> vibration_pattern(
79 kNotificationVibrationPattern,
80 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern));
82 std::vector<char> developer_data(
83 kNotificationData, kNotificationData + arraysize(kNotificationData));
85 PlatformNotificationData platform_data;
86 platform_data.title = base::ASCIIToUTF16(kNotificationTitle);
87 platform_data.direction =
88 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT;
89 platform_data.lang = kNotificationLang;
90 platform_data.body = base::ASCIIToUTF16(kNotificationBody);
91 platform_data.tag = kNotificationTag;
92 platform_data.icon = GURL(kNotificationIconUrl);
93 platform_data.vibration_pattern = vibration_pattern;
94 platform_data.silent = true;
95 platform_data.require_interaction = true;
96 platform_data.data = developer_data;
97 platform_data.actions.resize(2);
98 platform_data.actions[0].action = kAction1Name;
99 platform_data.actions[0].title = base::ASCIIToUTF16(kAction1Title);
100 platform_data.actions[1].action = kAction2Name;
101 platform_data.actions[1].title = base::ASCIIToUTF16(kAction2Title);
103 blink::WebNotificationData web_data = ToWebNotificationData(platform_data);
104 EXPECT_EQ(kNotificationTitle, web_data.title);
105 EXPECT_EQ(blink::WebNotificationData::DirectionLeftToRight,
106 web_data.direction);
107 EXPECT_EQ(kNotificationLang, web_data.lang);
108 EXPECT_EQ(kNotificationBody, web_data.body);
109 EXPECT_EQ(kNotificationTag, web_data.tag);
110 EXPECT_EQ(kNotificationIconUrl, web_data.icon.string());
112 ASSERT_EQ(vibration_pattern.size(), web_data.vibrate.size());
113 for (size_t i = 0; i < vibration_pattern.size(); ++i)
114 EXPECT_EQ(vibration_pattern[i], web_data.vibrate[i]);
116 EXPECT_TRUE(web_data.silent);
117 EXPECT_TRUE(web_data.requireInteraction);
119 ASSERT_EQ(developer_data.size(), web_data.data.size());
120 for (size_t i = 0; i < developer_data.size(); ++i)
121 EXPECT_EQ(developer_data[i], web_data.data[i]);
123 ASSERT_EQ(platform_data.actions.size(), web_data.actions.size());
124 EXPECT_EQ(kAction1Name, web_data.actions[0].action);
125 EXPECT_EQ(kAction1Title, web_data.actions[0].title);
126 EXPECT_EQ(kAction2Name, web_data.actions[1].action);
127 EXPECT_EQ(kAction2Title, web_data.actions[1].title);
130 TEST(NotificationDataConversionsTest, NotificationDataDirectionality) {
131 std::map<blink::WebNotificationData::Direction,
132 PlatformNotificationData::Direction> mappings;
134 mappings[blink::WebNotificationData::DirectionLeftToRight] =
135 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT;
136 mappings[blink::WebNotificationData::DirectionRightToLeft] =
137 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT;
138 mappings[blink::WebNotificationData::DirectionAuto] =
139 PlatformNotificationData::DIRECTION_AUTO;
141 for (const auto& pair : mappings) {
143 blink::WebNotificationData web_data;
144 web_data.direction = pair.first;
146 PlatformNotificationData platform_data =
147 ToPlatformNotificationData(web_data);
148 EXPECT_EQ(pair.second, platform_data.direction);
151 PlatformNotificationData platform_data;
152 platform_data.direction = pair.second;
154 blink::WebNotificationData web_data =
155 ToWebNotificationData(platform_data);
156 EXPECT_EQ(pair.first, web_data.direction);
161 } // namespace content