Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/notifications/NotificationData.h" | |
| 7 | |
| 8 #include "bindings/core/v8/ExceptionState.h" | |
| 9 #include "bindings/modules/v8/UnionTypesModules.h" | |
| 10 #include "core/testing/NullExecutionContext.h" | |
| 11 #include "modules/notifications/Notification.h" | |
| 12 #include "modules/notifications/NotificationOptions.h" | |
| 13 #include "wtf/Vector.h" | |
| 14 #include <gtest/gtest.h> | |
| 15 | |
| 16 namespace blink { | |
| 17 namespace { | |
| 18 | |
| 19 const char kNotificationTitle[] = "My Notification"; | |
| 20 | |
| 21 const char kNotificationDir[] = "rtl"; | |
| 22 const char kNotificationLang[] = "nl"; | |
| 23 const char kNotificationBody[] = "Hello, world"; | |
| 24 const char kNotificationTag[] = "my_tag"; | |
| 25 const char kNotificationIcon[] = "https://example.com/icon.png"; | |
| 26 const unsigned kNotificationVibration[] = { 42, 10, 20, 30, 40 }; | |
| 27 const bool kNotificationSilent = false; | |
| 28 | |
| 29 const char kNotificationActionTitle[] = "My Action"; | |
| 30 | |
| 31 const unsigned kNotificationVibrationUnnormalized[] = { 10, 1000000, 50, 42 }; | |
| 32 const int kNotificationVibrationNormalized[] = { 10, 10000, 50 }; | |
| 33 | |
| 34 class NotificationDataTest : public ::testing::Test { | |
| 35 public: | |
| 36 void SetUp() override | |
| 37 { | |
| 38 m_executionContext = adoptRefWillBeNoop(new NullExecutionContext()); | |
| 39 } | |
| 40 | |
| 41 ExecutionContext* executionContext() { return m_executionContext.get(); } | |
| 42 | |
| 43 private: | |
| 44 RefPtrWillBePersistent<ExecutionContext> m_executionContext; | |
| 45 }; | |
| 46 | |
| 47 TEST_F(NotificationDataTest, ReflectProperties) | |
| 48 { | |
| 49 Vector<unsigned> vibrationPattern; | |
| 50 for (size_t i = 0; i < arraysize(kNotificationVibration); ++i) | |
| 51 vibrationPattern.append(kNotificationVibration[i]); | |
| 52 | |
| 53 UnsignedLongOrUnsignedLongSequence vibrationSequence; | |
| 54 vibrationSequence.setUnsignedLongSequence(vibrationPattern); | |
| 55 | |
| 56 HeapVector<NotificationAction> actions; | |
| 57 for (size_t i = 0; i < Notification::maxActions(); ++i) { | |
| 58 NotificationAction action; | |
| 59 action.setTitle(kNotificationActionTitle); | |
| 60 | |
| 61 actions.append(action); | |
| 62 } | |
| 63 | |
| 64 NotificationOptions options; | |
| 65 options.setDir(kNotificationDir); | |
| 66 options.setLang(kNotificationLang); | |
| 67 options.setBody(kNotificationBody); | |
| 68 options.setTag(kNotificationTag); | |
| 69 options.setIcon(kNotificationIcon); | |
| 70 options.setVibrate(vibrationSequence); | |
| 71 options.setSilent(kNotificationSilent); | |
| 72 options.setActions(actions); | |
| 73 | |
| 74 // TODO(peter): Test |options.data| and |notificationData.data|. | |
| 75 | |
| 76 TrackExceptionState exceptionState; | |
| 77 WebNotificationData notificationData = createWebNotificationData(executionCo ntext(), kNotificationTitle, options, exceptionState); | |
| 78 ASSERT_FALSE(exceptionState.hadException()); | |
| 79 | |
| 80 EXPECT_EQ(kNotificationTitle, notificationData.title); | |
| 81 | |
| 82 EXPECT_EQ(WebNotificationData::DirectionRightToLeft, notificationData.direct ion); | |
| 83 EXPECT_EQ(kNotificationLang, notificationData.lang); | |
| 84 EXPECT_EQ(kNotificationBody, notificationData.body); | |
| 85 EXPECT_EQ(kNotificationTag, notificationData.tag); | |
| 86 | |
| 87 // TODO(peter): Test notificationData.icon when ExecutionContext::completeUR L() works in this test. | |
| 88 | |
| 89 ASSERT_EQ(vibrationPattern.size(), notificationData.vibrate.size()); | |
| 90 for (size_t i = 0; i < vibrationPattern.size(); ++i) | |
| 91 EXPECT_EQ(vibrationPattern[i], static_cast<unsigned>(notificationData.vi brate[i])); | |
| 92 | |
| 93 EXPECT_EQ(kNotificationSilent, notificationData.silent); | |
| 94 EXPECT_EQ(actions.size(), notificationData.actions.size()); | |
| 95 } | |
| 96 | |
| 97 TEST_F(NotificationDataTest, VibrationNormalization) | |
| 98 { | |
| 99 Vector<unsigned> unnormalizedPattern; | |
| 100 for (size_t i = 0; i < arraysize(kNotificationVibrationUnnormalized); ++i) | |
| 101 unnormalizedPattern.append(kNotificationVibrationUnnormalized[i]); | |
| 102 | |
| 103 UnsignedLongOrUnsignedLongSequence vibrationSequence; | |
| 104 vibrationSequence.setUnsignedLongSequence(unnormalizedPattern); | |
| 105 | |
| 106 NotificationOptions options; | |
| 107 options.setVibrate(vibrationSequence); | |
| 108 | |
| 109 TrackExceptionState exceptionState; | |
| 110 WebNotificationData notificationData = createWebNotificationData(executionCo ntext(), kNotificationTitle, options, exceptionState); | |
| 111 EXPECT_FALSE(exceptionState.hadException()); | |
| 112 | |
| 113 Vector<int> normalizedPattern; | |
| 114 for (size_t i = 0; i < arraysize(kNotificationVibrationNormalized); ++i) | |
| 115 normalizedPattern.append(kNotificationVibrationNormalized[i]); | |
| 116 | |
| 117 ASSERT_EQ(normalizedPattern.size(), notificationData.vibrate.size()); | |
| 118 for (size_t i = 0; i < normalizedPattern.size(); ++i) | |
| 119 EXPECT_EQ(normalizedPattern[i], notificationData.vibrate[i]); | |
| 120 } | |
| 121 | |
| 122 TEST_F(NotificationDataTest, RequiredActionProperties) | |
| 123 { | |
| 124 NotificationOptions options; | |
| 125 | |
| 126 // The NotificationAction.title property is required. | |
| 127 { | |
| 128 HeapVector<NotificationAction> actions; | |
| 129 actions.append(NotificationAction()); | |
| 130 | |
| 131 options.setActions(actions); | |
| 132 | |
| 133 TrackExceptionState exceptionState; | |
| 134 WebNotificationData notificationData = createWebNotificationData(executi onContext(), kNotificationTitle, options, exceptionState); | |
| 135 ASSERT_TRUE(exceptionState.hadException()); | |
| 136 EXPECT_EQ("Notification action titles must not be empty.", exceptionStat e.message()); | |
| 137 } | |
| 138 | |
| 139 // TODO(johnme): Test NotificationAction.action. | |
| 140 } | |
| 141 | |
| 142 TEST_F(NotificationDataTest, MaximumActionCount) | |
| 143 { | |
| 144 HeapVector<NotificationAction> actions; | |
| 145 for (size_t i = 0; i < Notification::maxActions() + 2; ++i) { | |
| 146 NotificationAction action; | |
| 147 action.setTitle(kNotificationActionTitle); | |
| 148 | |
| 149 actions.append(action); | |
| 150 } | |
| 151 | |
| 152 NotificationOptions options; | |
| 153 options.setActions(actions); | |
| 154 | |
| 155 TrackExceptionState exceptionState; | |
| 156 WebNotificationData notificationData = createWebNotificationData(executionCo ntext(), kNotificationTitle, options, exceptionState); | |
| 157 ASSERT_FALSE(exceptionState.hadException()); | |
| 158 | |
| 159 // The stored actions will be capped to |maxActions| entries. | |
|
johnme
2015/08/03 15:41:36
Might be nice to test that it's the first N entrie
Peter Beverloo
2015/08/03 17:29:30
Done.
| |
| 160 EXPECT_EQ(Notification::maxActions(), notificationData.actions.size()); | |
| 161 } | |
| 162 | |
| 163 } // namespace | |
| 164 } // namespace blink | |
| OLD | NEW |