| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "content/browser/notifications/notification_database_data.pb.h" | 12 #include "content/browser/notifications/notification_database_data.pb.h" |
| 13 #include "content/browser/notifications/notification_database_data_conversions.h
" | 13 #include "content/browser/notifications/notification_database_data_conversions.h
" |
| 14 #include "content/common/notification_constants.h" | 14 #include "content/common/notification_constants.h" |
| 15 #include "content/public/browser/notification_database_data.h" | 15 #include "content/public/browser/notification_database_data.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 const int64_t kNotificationId = 42; | 21 const int64_t kNotificationId = 42; |
| 22 const int64_t kServiceWorkerRegistrationId = 9001; | 22 const int64_t kServiceWorkerRegistrationId = 9001; |
| 23 | 23 |
| 24 const PlatformNotificationActionType kNotificationActionType = |
| 25 PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT; |
| 24 const char kOrigin[] = "https://example.com/"; | 26 const char kOrigin[] = "https://example.com/"; |
| 25 const char kNotificationTitle[] = "My Notification"; | 27 const char kNotificationTitle[] = "My Notification"; |
| 26 const char kNotificationLang[] = "nl"; | 28 const char kNotificationLang[] = "nl"; |
| 27 const char kNotificationBody[] = "Hello, world!"; | 29 const char kNotificationBody[] = "Hello, world!"; |
| 28 const char kNotificationTag[] = "my_tag"; | 30 const char kNotificationTag[] = "my_tag"; |
| 29 const char kNotificationIconUrl[] = "https://example.com/icon.png"; | 31 const char kNotificationIconUrl[] = "https://example.com/icon.png"; |
| 30 const char kNotificationBadgeUrl[] = "https://example.com/badge.png"; | 32 const char kNotificationBadgeUrl[] = "https://example.com/badge.png"; |
| 31 const char kNotificationActionIconUrl[] = "https://example.com/action_icon.png"; | 33 const char kNotificationActionIconUrl[] = "https://example.com/action_icon.png"; |
| 32 const int kNotificationVibrationPattern[] = {100, 200, 300}; | 34 const int kNotificationVibrationPattern[] = {100, 200, 300}; |
| 33 const double kNotificationTimestamp = 621046800.; | 35 const double kNotificationTimestamp = 621046800.; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 51 notification_data.icon = GURL(kNotificationIconUrl); | 53 notification_data.icon = GURL(kNotificationIconUrl); |
| 52 notification_data.badge = GURL(kNotificationBadgeUrl); | 54 notification_data.badge = GURL(kNotificationBadgeUrl); |
| 53 notification_data.vibration_pattern = vibration_pattern; | 55 notification_data.vibration_pattern = vibration_pattern; |
| 54 notification_data.timestamp = base::Time::FromJsTime(kNotificationTimestamp); | 56 notification_data.timestamp = base::Time::FromJsTime(kNotificationTimestamp); |
| 55 notification_data.renotify = true; | 57 notification_data.renotify = true; |
| 56 notification_data.silent = true; | 58 notification_data.silent = true; |
| 57 notification_data.require_interaction = true; | 59 notification_data.require_interaction = true; |
| 58 notification_data.data = developer_data; | 60 notification_data.data = developer_data; |
| 59 for (size_t i = 0; i < kPlatformNotificationMaxActions; i++) { | 61 for (size_t i = 0; i < kPlatformNotificationMaxActions; i++) { |
| 60 PlatformNotificationAction notification_action; | 62 PlatformNotificationAction notification_action; |
| 63 notification_action.type = kNotificationActionType; |
| 61 notification_action.action = base::SizeTToString(i); | 64 notification_action.action = base::SizeTToString(i); |
| 62 notification_action.title = base::SizeTToString16(i); | 65 notification_action.title = base::SizeTToString16(i); |
| 63 notification_action.icon = GURL(kNotificationActionIconUrl); | 66 notification_action.icon = GURL(kNotificationActionIconUrl); |
| 67 notification_action.placeholder = base::SizeTToString16(i); |
| 64 notification_data.actions.push_back(notification_action); | 68 notification_data.actions.push_back(notification_action); |
| 65 } | 69 } |
| 66 | 70 |
| 67 NotificationDatabaseData database_data; | 71 NotificationDatabaseData database_data; |
| 68 database_data.notification_id = kNotificationId; | 72 database_data.notification_id = kNotificationId; |
| 69 database_data.origin = GURL(kOrigin); | 73 database_data.origin = GURL(kOrigin); |
| 70 database_data.service_worker_registration_id = kServiceWorkerRegistrationId; | 74 database_data.service_worker_registration_id = kServiceWorkerRegistrationId; |
| 71 database_data.notification_data = notification_data; | 75 database_data.notification_data = notification_data; |
| 72 | 76 |
| 73 std::string serialized_data; | 77 std::string serialized_data; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 EXPECT_EQ(notification_data.require_interaction, | 111 EXPECT_EQ(notification_data.require_interaction, |
| 108 copied_notification_data.require_interaction); | 112 copied_notification_data.require_interaction); |
| 109 | 113 |
| 110 ASSERT_EQ(developer_data.size(), copied_notification_data.data.size()); | 114 ASSERT_EQ(developer_data.size(), copied_notification_data.data.size()); |
| 111 for (size_t i = 0; i < developer_data.size(); ++i) | 115 for (size_t i = 0; i < developer_data.size(); ++i) |
| 112 EXPECT_EQ(developer_data[i], copied_notification_data.data[i]); | 116 EXPECT_EQ(developer_data[i], copied_notification_data.data[i]); |
| 113 | 117 |
| 114 ASSERT_EQ(notification_data.actions.size(), | 118 ASSERT_EQ(notification_data.actions.size(), |
| 115 copied_notification_data.actions.size()); | 119 copied_notification_data.actions.size()); |
| 116 for (size_t i = 0; i < notification_data.actions.size(); ++i) { | 120 for (size_t i = 0; i < notification_data.actions.size(); ++i) { |
| 121 EXPECT_EQ(notification_data.actions[i].type, |
| 122 copied_notification_data.actions[i].type); |
| 117 EXPECT_EQ(notification_data.actions[i].action, | 123 EXPECT_EQ(notification_data.actions[i].action, |
| 118 copied_notification_data.actions[i].action); | 124 copied_notification_data.actions[i].action); |
| 119 EXPECT_EQ(notification_data.actions[i].title, | 125 EXPECT_EQ(notification_data.actions[i].title, |
| 120 copied_notification_data.actions[i].title); | 126 copied_notification_data.actions[i].title); |
| 121 EXPECT_EQ(notification_data.actions[i].icon, | 127 EXPECT_EQ(notification_data.actions[i].icon, |
| 122 copied_notification_data.actions[i].icon); | 128 copied_notification_data.actions[i].icon); |
| 129 EXPECT_EQ(notification_data.actions[i].placeholder, |
| 130 copied_notification_data.actions[i].placeholder); |
| 123 } | 131 } |
| 124 } | 132 } |
| 125 | 133 |
| 126 TEST(NotificationDatabaseDataTest, SerializeAndDeserializeDirections) { | 134 TEST(NotificationDatabaseDataTest, SerializeAndDeserializeDirections) { |
| 127 PlatformNotificationData::Direction directions[] = { | 135 PlatformNotificationData::Direction directions[] = { |
| 128 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT, | 136 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT, |
| 129 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT, | 137 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT, |
| 130 PlatformNotificationData::DIRECTION_AUTO}; | 138 PlatformNotificationData::DIRECTION_AUTO}; |
| 131 | 139 |
| 132 for (size_t i = 0; i < arraysize(directions); ++i) { | 140 for (size_t i = 0; i < arraysize(directions); ++i) { |
| 133 PlatformNotificationData notification_data; | 141 PlatformNotificationData notification_data; |
| 134 notification_data.direction = directions[i]; | 142 notification_data.direction = directions[i]; |
| 135 | 143 |
| 136 NotificationDatabaseData database_data; | 144 NotificationDatabaseData database_data; |
| 137 database_data.notification_data = notification_data; | 145 database_data.notification_data = notification_data; |
| 138 | 146 |
| 139 std::string serialized_data; | 147 std::string serialized_data; |
| 140 ASSERT_TRUE( | 148 ASSERT_TRUE( |
| 141 SerializeNotificationDatabaseData(database_data, &serialized_data)); | 149 SerializeNotificationDatabaseData(database_data, &serialized_data)); |
| 142 | 150 |
| 143 NotificationDatabaseData copied_data; | 151 NotificationDatabaseData copied_data; |
| 144 ASSERT_TRUE( | 152 ASSERT_TRUE( |
| 145 DeserializeNotificationDatabaseData(serialized_data, &copied_data)); | 153 DeserializeNotificationDatabaseData(serialized_data, &copied_data)); |
| 146 | 154 |
| 147 EXPECT_EQ(directions[i], copied_data.notification_data.direction); | 155 EXPECT_EQ(directions[i], copied_data.notification_data.direction); |
| 148 } | 156 } |
| 149 } | 157 } |
| 150 | 158 |
| 159 TEST(NotificationDatabaseDataTest, SerializeAndDeserializeActionTypes) { |
| 160 PlatformNotificationActionType action_types[] = { |
| 161 PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON, |
| 162 PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT}; |
| 163 |
| 164 for (PlatformNotificationActionType action_type : action_types) { |
| 165 PlatformNotificationData notification_data; |
| 166 |
| 167 PlatformNotificationAction action; |
| 168 action.type = action_type; |
| 169 notification_data.actions.push_back(action); |
| 170 |
| 171 NotificationDatabaseData database_data; |
| 172 database_data.notification_data = notification_data; |
| 173 |
| 174 std::string serialized_data; |
| 175 ASSERT_TRUE( |
| 176 SerializeNotificationDatabaseData(database_data, &serialized_data)); |
| 177 |
| 178 NotificationDatabaseData copied_data; |
| 179 ASSERT_TRUE( |
| 180 DeserializeNotificationDatabaseData(serialized_data, &copied_data)); |
| 181 |
| 182 EXPECT_EQ(action_type, copied_data.notification_data.actions[0].type); |
| 183 } |
| 184 } |
| 151 } // namespace content | 185 } // namespace content |
| OLD | NEW |