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
.require_interaction
= true;
47 notification_data
.data
= developer_data
;
48 for (size_t i
= 0; i
< kPlatformNotificationMaxActions
; i
++) {
49 PlatformNotificationAction notification_action
;
50 notification_action
.action
= base::SizeTToString(i
);
51 notification_action
.title
= base::SizeTToString16(i
);
52 notification_data
.actions
.push_back(notification_action
);
55 NotificationDatabaseData database_data
;
56 database_data
.notification_id
= kNotificationId
;
57 database_data
.origin
= GURL(kOrigin
);
58 database_data
.service_worker_registration_id
= kServiceWorkerRegistrationId
;
59 database_data
.notification_data
= notification_data
;
61 std::string serialized_data
;
63 // Serialize the data in |notification_data| to the string |serialized_data|.
64 ASSERT_TRUE(SerializeNotificationDatabaseData(database_data
,
67 NotificationDatabaseData copied_data
;
69 // Deserialize the data in |serialized_data| to |copied_data|.
70 ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data
,
73 EXPECT_EQ(database_data
.notification_id
, copied_data
.notification_id
);
74 EXPECT_EQ(database_data
.origin
, copied_data
.origin
);
75 EXPECT_EQ(database_data
.service_worker_registration_id
,
76 copied_data
.service_worker_registration_id
);
78 const PlatformNotificationData
& copied_notification_data
=
79 copied_data
.notification_data
;
81 EXPECT_EQ(notification_data
.title
, copied_notification_data
.title
);
82 EXPECT_EQ(notification_data
.direction
, copied_notification_data
.direction
);
83 EXPECT_EQ(notification_data
.lang
, copied_notification_data
.lang
);
84 EXPECT_EQ(notification_data
.body
, copied_notification_data
.body
);
85 EXPECT_EQ(notification_data
.tag
, copied_notification_data
.tag
);
86 EXPECT_EQ(notification_data
.icon
, copied_notification_data
.icon
);
88 EXPECT_THAT(copied_notification_data
.vibration_pattern
,
89 testing::ElementsAreArray(kNotificationVibrationPattern
));
91 EXPECT_EQ(notification_data
.silent
, copied_notification_data
.silent
);
92 EXPECT_EQ(notification_data
.require_interaction
,
93 copied_notification_data
.require_interaction
);
95 ASSERT_EQ(developer_data
.size(), copied_notification_data
.data
.size());
96 for (size_t i
= 0; i
< developer_data
.size(); ++i
)
97 EXPECT_EQ(developer_data
[i
], copied_notification_data
.data
[i
]);
99 ASSERT_EQ(notification_data
.actions
.size(),
100 copied_notification_data
.actions
.size());
101 for (size_t i
= 0; i
< notification_data
.actions
.size(); ++i
) {
102 EXPECT_EQ(notification_data
.actions
[i
].action
,
103 copied_notification_data
.actions
[i
].action
);
104 EXPECT_EQ(notification_data
.actions
[i
].title
,
105 copied_notification_data
.actions
[i
].title
);
109 TEST(NotificationDatabaseDataTest
, SerializeAndDeserializeDirections
) {
110 PlatformNotificationData::Direction directions
[] = {
111 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
,
112 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
,
113 PlatformNotificationData::DIRECTION_AUTO
116 for (size_t i
= 0; i
< arraysize(directions
); ++i
) {
117 PlatformNotificationData notification_data
;
118 notification_data
.direction
= directions
[i
];
120 NotificationDatabaseData database_data
;
121 database_data
.notification_data
= notification_data
;
123 std::string serialized_data
;
124 ASSERT_TRUE(SerializeNotificationDatabaseData(database_data
,
127 NotificationDatabaseData copied_data
;
128 ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data
,
131 EXPECT_EQ(directions
[i
], copied_data
.notification_data
.direction
);
135 } // namespace content