| 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 "modules/notifications/NotificationData.h" | 5 #include "modules/notifications/NotificationData.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/modules/v8/UnionTypesModules.h" | 8 #include "bindings/modules/v8/UnionTypesModules.h" |
| 9 #include "core/testing/NullExecutionContext.h" | 9 #include "core/testing/NullExecutionContext.h" |
| 10 #include "modules/notifications/Notification.h" | 10 #include "modules/notifications/Notification.h" |
| 11 #include "modules/notifications/NotificationOptions.h" | 11 #include "modules/notifications/NotificationOptions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "wtf/CurrentTime.h" |
| 13 #include "wtf/HashMap.h" | 14 #include "wtf/HashMap.h" |
| 14 #include "wtf/Vector.h" | 15 #include "wtf/Vector.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kNotificationTitle[] = "My Notification"; | 20 const char kNotificationTitle[] = "My Notification"; |
| 20 | 21 |
| 21 const char kNotificationDir[] = "rtl"; | 22 const char kNotificationDir[] = "rtl"; |
| 22 const char kNotificationLang[] = "nl"; | 23 const char kNotificationLang[] = "nl"; |
| 23 const char kNotificationBody[] = "Hello, world"; | 24 const char kNotificationBody[] = "Hello, world"; |
| 24 const char kNotificationTag[] = "my_tag"; | 25 const char kNotificationTag[] = "my_tag"; |
| 25 const char kNotificationIcon[] = "https://example.com/icon.png"; | 26 const char kNotificationIcon[] = "https://example.com/icon.png"; |
| 26 const unsigned kNotificationVibration[] = { 42, 10, 20, 30, 40 }; | 27 const unsigned kNotificationVibration[] = { 42, 10, 20, 30, 40 }; |
| 28 const unsigned long long kNotificationTimestamp = 621046800ull; |
| 27 const bool kNotificationSilent = false; | 29 const bool kNotificationSilent = false; |
| 28 const bool kNotificationRequireInteraction = true; | 30 const bool kNotificationRequireInteraction = true; |
| 29 | 31 |
| 30 const char kNotificationActionAction[] = "my_action"; | 32 const char kNotificationActionAction[] = "my_action"; |
| 31 const char kNotificationActionTitle[] = "My Action"; | 33 const char kNotificationActionTitle[] = "My Action"; |
| 32 | 34 |
| 33 const unsigned kNotificationVibrationUnnormalized[] = { 10, 1000000, 50, 42 }; | 35 const unsigned kNotificationVibrationUnnormalized[] = { 10, 1000000, 50, 42 }; |
| 34 const int kNotificationVibrationNormalized[] = { 10, 10000, 50 }; | 36 const int kNotificationVibrationNormalized[] = { 10, 10000, 50 }; |
| 35 | 37 |
| 36 class NotificationDataTest : public ::testing::Test { | 38 class NotificationDataTest : public ::testing::Test { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 64 actions.append(action); | 66 actions.append(action); |
| 65 } | 67 } |
| 66 | 68 |
| 67 NotificationOptions options; | 69 NotificationOptions options; |
| 68 options.setDir(kNotificationDir); | 70 options.setDir(kNotificationDir); |
| 69 options.setLang(kNotificationLang); | 71 options.setLang(kNotificationLang); |
| 70 options.setBody(kNotificationBody); | 72 options.setBody(kNotificationBody); |
| 71 options.setTag(kNotificationTag); | 73 options.setTag(kNotificationTag); |
| 72 options.setIcon(kNotificationIcon); | 74 options.setIcon(kNotificationIcon); |
| 73 options.setVibrate(vibrationSequence); | 75 options.setVibrate(vibrationSequence); |
| 76 options.setTimestamp(kNotificationTimestamp); |
| 74 options.setSilent(kNotificationSilent); | 77 options.setSilent(kNotificationSilent); |
| 75 options.setRequireInteraction(kNotificationRequireInteraction); | 78 options.setRequireInteraction(kNotificationRequireInteraction); |
| 76 options.setActions(actions); | 79 options.setActions(actions); |
| 77 | 80 |
| 78 // TODO(peter): Test |options.data| and |notificationData.data|. | 81 // TODO(peter): Test |options.data| and |notificationData.data|. |
| 79 | 82 |
| 80 TrackExceptionState exceptionState; | 83 TrackExceptionState exceptionState; |
| 81 WebNotificationData notificationData = createWebNotificationData(executionCo
ntext(), kNotificationTitle, options, exceptionState); | 84 WebNotificationData notificationData = createWebNotificationData(executionCo
ntext(), kNotificationTitle, options, exceptionState); |
| 82 ASSERT_FALSE(exceptionState.hadException()); | 85 ASSERT_FALSE(exceptionState.hadException()); |
| 83 | 86 |
| 84 EXPECT_EQ(kNotificationTitle, notificationData.title); | 87 EXPECT_EQ(kNotificationTitle, notificationData.title); |
| 85 | 88 |
| 86 EXPECT_EQ(WebNotificationData::DirectionRightToLeft, notificationData.direct
ion); | 89 EXPECT_EQ(WebNotificationData::DirectionRightToLeft, notificationData.direct
ion); |
| 87 EXPECT_EQ(kNotificationLang, notificationData.lang); | 90 EXPECT_EQ(kNotificationLang, notificationData.lang); |
| 88 EXPECT_EQ(kNotificationBody, notificationData.body); | 91 EXPECT_EQ(kNotificationBody, notificationData.body); |
| 89 EXPECT_EQ(kNotificationTag, notificationData.tag); | 92 EXPECT_EQ(kNotificationTag, notificationData.tag); |
| 90 | 93 |
| 91 // TODO(peter): Test notificationData.icon when ExecutionContext::completeUR
L() works in this test. | 94 // TODO(peter): Test notificationData.icon when ExecutionContext::completeUR
L() works in this test. |
| 92 | 95 |
| 93 ASSERT_EQ(vibrationPattern.size(), notificationData.vibrate.size()); | 96 ASSERT_EQ(vibrationPattern.size(), notificationData.vibrate.size()); |
| 94 for (size_t i = 0; i < vibrationPattern.size(); ++i) | 97 for (size_t i = 0; i < vibrationPattern.size(); ++i) |
| 95 EXPECT_EQ(vibrationPattern[i], static_cast<unsigned>(notificationData.vi
brate[i])); | 98 EXPECT_EQ(vibrationPattern[i], static_cast<unsigned>(notificationData.vi
brate[i])); |
| 96 | 99 |
| 100 EXPECT_EQ(kNotificationTimestamp, notificationData.timestamp); |
| 97 EXPECT_EQ(kNotificationSilent, notificationData.silent); | 101 EXPECT_EQ(kNotificationSilent, notificationData.silent); |
| 98 EXPECT_EQ(kNotificationRequireInteraction, notificationData.requireInteracti
on); | 102 EXPECT_EQ(kNotificationRequireInteraction, notificationData.requireInteracti
on); |
| 99 EXPECT_EQ(actions.size(), notificationData.actions.size()); | 103 EXPECT_EQ(actions.size(), notificationData.actions.size()); |
| 100 } | 104 } |
| 101 | 105 |
| 102 TEST_F(NotificationDataTest, SilentNotificationWithVibration) | 106 TEST_F(NotificationDataTest, SilentNotificationWithVibration) |
| 103 { | 107 { |
| 104 Vector<unsigned> vibrationPattern; | 108 Vector<unsigned> vibrationPattern; |
| 105 for (size_t i = 0; i < WTF_ARRAY_LENGTH(kNotificationVibration); ++i) | 109 for (size_t i = 0; i < WTF_ARRAY_LENGTH(kNotificationVibration); ++i) |
| 106 vibrationPattern.append(kNotificationVibration[i]); | 110 vibrationPattern.append(kNotificationVibration[i]); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 153 |
| 150 Vector<int> normalizedPattern; | 154 Vector<int> normalizedPattern; |
| 151 for (size_t i = 0; i < WTF_ARRAY_LENGTH(kNotificationVibrationNormalized); +
+i) | 155 for (size_t i = 0; i < WTF_ARRAY_LENGTH(kNotificationVibrationNormalized); +
+i) |
| 152 normalizedPattern.append(kNotificationVibrationNormalized[i]); | 156 normalizedPattern.append(kNotificationVibrationNormalized[i]); |
| 153 | 157 |
| 154 ASSERT_EQ(normalizedPattern.size(), notificationData.vibrate.size()); | 158 ASSERT_EQ(normalizedPattern.size(), notificationData.vibrate.size()); |
| 155 for (size_t i = 0; i < normalizedPattern.size(); ++i) | 159 for (size_t i = 0; i < normalizedPattern.size(); ++i) |
| 156 EXPECT_EQ(normalizedPattern[i], notificationData.vibrate[i]); | 160 EXPECT_EQ(normalizedPattern[i], notificationData.vibrate[i]); |
| 157 } | 161 } |
| 158 | 162 |
| 163 TEST_F(NotificationDataTest, DefaultTimestampValue) |
| 164 { |
| 165 NotificationOptions options; |
| 166 |
| 167 TrackExceptionState exceptionState; |
| 168 WebNotificationData notificationData = createWebNotificationData(executionCo
ntext(), kNotificationTitle, options, exceptionState); |
| 169 EXPECT_FALSE(exceptionState.hadException()); |
| 170 |
| 171 // The timestamp should be set to the current time since the epoch if it was
n't supplied by the developer. |
| 172 // "32" has no significance, but an equal comparison of the value could lead
to flaky failures. |
| 173 EXPECT_NEAR(notificationData.timestamp, WTF::currentTimeMS(), 32); |
| 174 } |
| 175 |
| 159 TEST_F(NotificationDataTest, DirectionValues) | 176 TEST_F(NotificationDataTest, DirectionValues) |
| 160 { | 177 { |
| 161 WTF::HashMap<String, WebNotificationData::Direction> mappings; | 178 WTF::HashMap<String, WebNotificationData::Direction> mappings; |
| 162 mappings.add("ltr", WebNotificationData::DirectionLeftToRight); | 179 mappings.add("ltr", WebNotificationData::DirectionLeftToRight); |
| 163 mappings.add("rtl", WebNotificationData::DirectionRightToLeft); | 180 mappings.add("rtl", WebNotificationData::DirectionRightToLeft); |
| 164 mappings.add("auto", WebNotificationData::DirectionAuto); | 181 mappings.add("auto", WebNotificationData::DirectionAuto); |
| 165 | 182 |
| 166 // Invalid values should default to "auto". | 183 // Invalid values should default to "auto". |
| 167 mappings.add("peter", WebNotificationData::DirectionAuto); | 184 mappings.add("peter", WebNotificationData::DirectionAuto); |
| 168 | 185 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 ASSERT_EQ(Notification::maxActions(), notificationData.actions.size()); | 217 ASSERT_EQ(Notification::maxActions(), notificationData.actions.size()); |
| 201 | 218 |
| 202 for (size_t i = 0; i < Notification::maxActions(); ++i) { | 219 for (size_t i = 0; i < Notification::maxActions(); ++i) { |
| 203 WebString expectedAction = String::number(i); | 220 WebString expectedAction = String::number(i); |
| 204 EXPECT_EQ(expectedAction, notificationData.actions[i].action); | 221 EXPECT_EQ(expectedAction, notificationData.actions[i].action); |
| 205 } | 222 } |
| 206 } | 223 } |
| 207 | 224 |
| 208 } // namespace | 225 } // namespace |
| 209 } // namespace blink | 226 } // namespace blink |
| OLD | NEW |