1 // Copyright 2015 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 "base/strings/string_number_conversions.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "content/browser/notifications/notification_database_data.pb.h"
8 #include "content/browser/notifications/notification_database_data_conversions.h"
9 #include "content/common/notification_constants.h"
10 #include "content/public/browser/notification_database_data.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 const int64_t kNotificationId
= 42;
17 const int64_t kServiceWorkerRegistrationId
= 9001;
19 const char kOrigin
[] = "https://example.com/";
20 const char kNotificationTitle
[] = "My Notification";
21 const char kNotificationLang
[] = "nl";
22 const char kNotificationBody
[] = "Hello, world!";
23 const char kNotificationTag
[] = "my_tag";
24 const char kNotificationIconUrl
[] = "https://example.com/icon.png";
25 const int kNotificationVibrationPattern
[] = { 100, 200, 300 };
26 const unsigned char kNotificationData
[] = { 0xdf, 0xff, 0x0, 0x0, 0xff, 0xdf };
28 TEST(NotificationDatabaseDataTest
, SerializeAndDeserializeData
) {
29 std::vector
<int> vibration_pattern(
30 kNotificationVibrationPattern
,
31 kNotificationVibrationPattern
+ arraysize(kNotificationVibrationPattern
));
33 std::vector
<char> developer_data(
34 kNotificationData
, kNotificationData
+ arraysize(kNotificationData
));
36 PlatformNotificationData notification_data
;
37 notification_data
.title
= base::ASCIIToUTF16(kNotificationTitle
);
38 notification_data
.direction
=
39 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
;
40 notification_data
.lang
= kNotificationLang
;
41 notification_data
.body
= base::ASCIIToUTF16(kNotificationBody
);
42 notification_data
.tag
= kNotificationTag
;
43 notification_data
.icon
= GURL(kNotificationIconUrl
);
44 notification_data
.vibration_pattern
= vibration_pattern
;
45 notification_data
.silent
= true;
46 notification_data
.data
= developer_data
;
47 for (size_t i
= 0; i
< kPlatformNotificationMaxActions
; i
++) {
48 PlatformNotificationAction notification_action
;
49 notification_action
.action
= base::SizeTToString(i
);
50 notification_action
.title
= base::SizeTToString16(i
);
51 notification_data
.actions
.push_back(notification_action
);
54 NotificationDatabaseData database_data
;
55 database_data
.notification_id
= kNotificationId
;
56 database_data
.origin
= GURL(kOrigin
);
57 database_data
.service_worker_registration_id
= kServiceWorkerRegistrationId
;
58 database_data
.notification_data
= notification_data
;
60 std::string serialized_data
;
62 // Serialize the data in |notification_data| to the string |serialized_data|.
63 ASSERT_TRUE(SerializeNotificationDatabaseData(database_data
,
66 NotificationDatabaseData copied_data
;
68 // Deserialize the data in |serialized_data| to |copied_data|.
69 ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data
,
72 EXPECT_EQ(database_data
.notification_id
, copied_data
.notification_id
);
73 EXPECT_EQ(database_data
.origin
, copied_data
.origin
);
74 EXPECT_EQ(database_data
.service_worker_registration_id
,
75 copied_data
.service_worker_registration_id
);
77 const PlatformNotificationData
& copied_notification_data
=
78 copied_data
.notification_data
;
80 EXPECT_EQ(notification_data
.title
, copied_notification_data
.title
);
81 EXPECT_EQ(notification_data
.direction
, copied_notification_data
.direction
);
82 EXPECT_EQ(notification_data
.lang
, copied_notification_data
.lang
);
83 EXPECT_EQ(notification_data
.body
, copied_notification_data
.body
);
84 EXPECT_EQ(notification_data
.tag
, copied_notification_data
.tag
);
85 EXPECT_EQ(notification_data
.icon
, copied_notification_data
.icon
);
87 EXPECT_THAT(copied_notification_data
.vibration_pattern
,
88 testing::ElementsAreArray(kNotificationVibrationPattern
));
90 EXPECT_EQ(notification_data
.silent
, copied_notification_data
.silent
);
92 ASSERT_EQ(developer_data
.size(), copied_notification_data
.data
.size());
93 for (size_t i
= 0; i
< developer_data
.size(); ++i
)
94 EXPECT_EQ(developer_data
[i
], copied_notification_data
.data
[i
]);
96 ASSERT_EQ(notification_data
.actions
.size(),
97 copied_notification_data
.actions
.size());
98 for (size_t i
= 0; i
< notification_data
.actions
.size(); ++i
) {
99 EXPECT_EQ(notification_data
.actions
[i
].action
,
100 copied_notification_data
.actions
[i
].action
);
101 EXPECT_EQ(notification_data
.actions
[i
].title
,
102 copied_notification_data
.actions
[i
].title
);
106 TEST(NotificationDatabaseDataTest
, SerializeAndDeserializeDirections
) {
107 PlatformNotificationData::Direction directions
[] = {
108 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
,
109 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
,
110 PlatformNotificationData::DIRECTION_AUTO
113 for (size_t i
= 0; i
< arraysize(directions
); ++i
) {
114 PlatformNotificationData notification_data
;
115 notification_data
.direction
= directions
[i
];
117 NotificationDatabaseData database_data
;
118 database_data
.notification_data
= notification_data
;
120 std::string serialized_data
;
121 ASSERT_TRUE(SerializeNotificationDatabaseData(database_data
,
124 NotificationDatabaseData copied_data
;
125 ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data
,
128 EXPECT_EQ(directions
[i
], copied_data
.notification_data
.direction
);
132 } // namespace content