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/utf_string_conversions.h"
6 #include "content/browser/notifications/notification_database_data.pb.h"
7 #include "content/browser/notifications/notification_database_data_conversions.h"
8 #include "content/public/browser/notification_database_data.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 const int64_t kNotificationId
= 42;
14 const int64_t kServiceWorkerRegistrationId
= 9001;
16 const char kOrigin
[] = "https://example.com/";
17 const char kNotificationTitle
[] = "My Notification";
18 const char kNotificationLang
[] = "nl";
19 const char kNotificationBody
[] = "Hello, world!";
20 const char kNotificationTag
[] = "my_tag";
21 const char kNotificationIconUrl
[] = "https://example.com/icon.png";
22 const unsigned char kNotificationData
[] = { 0xdf, 0xff, 0x0, 0x0, 0xff, 0xdf };
24 TEST(NotificationDatabaseDataTest
, SerializeAndDeserializeData
) {
25 std::vector
<char> developer_data(
26 kNotificationData
, kNotificationData
+ arraysize(kNotificationData
));
28 PlatformNotificationData notification_data
;
29 notification_data
.title
= base::ASCIIToUTF16(kNotificationTitle
);
30 notification_data
.direction
=
31 PlatformNotificationData::NotificationDirectionRightToLeft
;
32 notification_data
.lang
= kNotificationLang
;
33 notification_data
.body
= base::ASCIIToUTF16(kNotificationBody
);
34 notification_data
.tag
= kNotificationTag
;
35 notification_data
.icon
= GURL(kNotificationIconUrl
);
36 notification_data
.silent
= true;
37 notification_data
.data
= developer_data
;
39 NotificationDatabaseData database_data
;
40 database_data
.notification_id
= kNotificationId
;
41 database_data
.origin
= GURL(kOrigin
);
42 database_data
.service_worker_registration_id
= kServiceWorkerRegistrationId
;
43 database_data
.notification_data
= notification_data
;
45 std::string serialized_data
;
47 // Serialize the data in |notification_data| to the string |serialized_data|.
48 ASSERT_TRUE(SerializeNotificationDatabaseData(database_data
,
51 NotificationDatabaseData copied_data
;
53 // Deserialize the data in |serialized_data| to |copied_data|.
54 ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data
,
57 EXPECT_EQ(database_data
.notification_id
, copied_data
.notification_id
);
58 EXPECT_EQ(database_data
.origin
, copied_data
.origin
);
59 EXPECT_EQ(database_data
.service_worker_registration_id
,
60 copied_data
.service_worker_registration_id
);
62 const PlatformNotificationData
& copied_notification_data
=
63 copied_data
.notification_data
;
65 EXPECT_EQ(notification_data
.title
, copied_notification_data
.title
);
66 EXPECT_EQ(notification_data
.direction
, copied_notification_data
.direction
);
67 EXPECT_EQ(notification_data
.lang
, copied_notification_data
.lang
);
68 EXPECT_EQ(notification_data
.body
, copied_notification_data
.body
);
69 EXPECT_EQ(notification_data
.tag
, copied_notification_data
.tag
);
70 EXPECT_EQ(notification_data
.icon
, copied_notification_data
.icon
);
71 EXPECT_EQ(notification_data
.silent
, copied_notification_data
.silent
);
73 ASSERT_EQ(developer_data
.size(), copied_notification_data
.data
.size());
74 for (size_t i
= 0; i
< developer_data
.size(); ++i
)
75 EXPECT_EQ(developer_data
[i
], copied_notification_data
.data
[i
]);
78 } // namespace content