Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import <AppKit/AppKit.h> | 5 #import <AppKit/AppKit.h> |
| 6 | 6 |
| 7 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "chrome/browser/notifications/notification_common.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/notifications/notification.h" | |
| 9 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" | 10 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
| 11 #include "chrome/browser/notifications/notification_test_util.h" | |
| 12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 10 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 13 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 11 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | 14 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 12 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" | 15 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #import "third_party/ocmock/OCMock/OCMock.h" | |
| 18 #include "third_party/ocmock/gtest_support.h" | |
| 19 #include "third_party/skia/include/core/SkBitmap.h" | |
| 20 #include "url/gurl.h" | |
| 14 | 21 |
| 15 namespace { | 22 namespace { |
| 16 | 23 |
| 17 NSMutableDictionary* BuildDefaultNotificationResponse() { | 24 NSMutableDictionary* BuildDefaultNotificationResponse() { |
| 18 base::scoped_nsobject<NotificationBuilder> builder( | 25 base::scoped_nsobject<NotificationBuilder> builder( |
| 19 [[NotificationBuilder alloc] init]); | 26 [[NotificationBuilder alloc] init]); |
| 20 [builder setTitle:@"Title"]; | 27 [builder setTitle:@"Title"]; |
| 21 [builder setSubTitle:@"https://www.miguel.com"]; | 28 [builder setSubTitle:@"https://www.miguel.com"]; |
| 22 [builder setOrigin:@"https://www.miguel.com/"]; | 29 [builder setOrigin:@"https://www.miguel.com/"]; |
| 23 [builder setContextMessage:@""]; | 30 [builder setContextMessage:@""]; |
| 24 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | 31 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 25 [builder setTag:@"tag1"]; | 32 [builder setTag:@"tag1"]; |
| 26 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; | 33 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 27 [builder setNotificationId:@"notificationId"]; | 34 [builder setNotificationId:@"notificationId"]; |
| 28 [builder setProfileId:@"profileId"]; | 35 [builder setProfileId:@"profileId"]; |
| 29 [builder setIncognito:false]; | 36 [builder setIncognito:false]; |
| 30 [builder | 37 [builder |
| 31 setNotificationType:[NSNumber | 38 setNotificationType:[NSNumber |
| 32 numberWithInt:NotificationCommon::PERSISTENT]]; | 39 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 33 | 40 |
| 34 NSUserNotification* notification = [builder buildUserNotification]; | 41 NSUserNotification* notification = [builder buildUserNotification]; |
| 35 return [NSMutableDictionary | 42 return [NSMutableDictionary |
| 36 dictionaryWithDictionary:[NotificationResponseBuilder | 43 dictionaryWithDictionary:[NotificationResponseBuilder |
| 37 buildDictionary:notification]]; | 44 buildDictionary:notification]]; |
| 38 } | 45 } |
| 39 | 46 |
| 40 } // namespace | 47 } // namespace |
| 41 | 48 |
| 42 TEST(NotificationPlatformBridgeMacTest, TestNotificationValidResponse) { | 49 class NotificationPlatformBridgeMacTest : public CocoaTest { |
| 50 protected: | |
| 51 std::unique_ptr<Notification> CreateNotification(const std::string& title, | |
| 52 const std::string& subtitle, | |
| 53 const std::string& context, | |
| 54 const std::string& button1, | |
| 55 const std::string& button2) { | |
|
Peter Beverloo
2016/07/12 17:44:19
All of these arguments could be defined as "const
Miguel Garcia
2016/07/13 14:05:16
I changed but for the record, I would trade some e
| |
| 56 message_center::RichNotificationData optional_fields; | |
| 57 optional_fields.context_message = base::UTF8ToUTF16(context); | |
| 58 if (!button1.empty()) { | |
| 59 optional_fields.buttons.push_back( | |
| 60 message_center::ButtonInfo(base::UTF8ToUTF16(button1))); | |
| 61 if (!button2.empty()) { | |
| 62 optional_fields.buttons.push_back( | |
| 63 message_center::ButtonInfo(base::UTF8ToUTF16(button2))); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 NotificationDelegate* delegate(new MockNotificationDelegate("id1")); | |
| 68 | |
| 69 SkBitmap bitmap; | |
| 70 bitmap.allocN32Pixels(1, 1); | |
| 71 bitmap.eraseColor(SkColorSetRGB(1, 2, 3)); | |
| 72 gfx::Image icon = gfx::Image::CreateFrom1xBitmap(bitmap); | |
|
Peter Beverloo
2016/07/12 17:44:19
fwiw, using an empty gfx::Image() as the icon is p
Miguel Garcia
2016/07/13 14:05:16
Done.
| |
| 73 | |
| 74 std::unique_ptr<Notification> notification(new Notification( | |
| 75 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), | |
| 76 base::UTF8ToUTF16(subtitle), icon, | |
| 77 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
|
Peter Beverloo
2016/07/12 17:44:19
optional: APPLICATION -> WEB_PAGE? :)
Miguel Garcia
2016/07/13 14:05:16
Done.
| |
| 78 "Notifier 1"), | |
| 79 base::UTF8ToUTF16("Notifier's Name"), GURL(), "id1", optional_fields, | |
| 80 delegate)); | |
| 81 | |
| 82 return notification; | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { | |
| 43 NSDictionary* response = BuildDefaultNotificationResponse(); | 87 NSDictionary* response = BuildDefaultNotificationResponse(); |
| 44 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 88 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 45 } | 89 } |
| 46 | 90 |
| 47 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { | 91 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { |
| 48 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 92 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 49 [response setValue:[NSNumber numberWithInt:210581] | 93 [response setValue:[NSNumber numberWithInt:210581] |
| 50 forKey:notification_constants::kNotificationType]; | 94 forKey:notification_constants::kNotificationType]; |
| 51 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 95 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 52 } | 96 } |
| 53 | 97 |
| 54 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownOperation) { | 98 TEST_F(NotificationPlatformBridgeMacTest, |
| 99 TestNotificationVerifyUnknownOperation) { | |
| 55 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 100 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 56 [response setValue:[NSNumber numberWithInt:40782] | 101 [response setValue:[NSNumber numberWithInt:40782] |
| 57 forKey:notification_constants::kNotificationOperation]; | 102 forKey:notification_constants::kNotificationOperation]; |
| 58 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 103 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 59 } | 104 } |
| 60 | 105 |
| 61 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingOperation) { | 106 TEST_F(NotificationPlatformBridgeMacTest, |
| 107 TestNotificationVerifyMissingOperation) { | |
| 62 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 108 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 63 [response removeObjectForKey:notification_constants::kNotificationOperation]; | 109 [response removeObjectForKey:notification_constants::kNotificationOperation]; |
| 64 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 110 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 65 } | 111 } |
| 66 | 112 |
| 67 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoProfileId) { | 113 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyNoProfileId) { |
| 68 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 114 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 69 [response removeObjectForKey:notification_constants::kNotificationProfileId]; | 115 [response removeObjectForKey:notification_constants::kNotificationProfileId]; |
| 70 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 116 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 71 } | 117 } |
| 72 | 118 |
| 73 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoNotificationId) { | 119 TEST_F(NotificationPlatformBridgeMacTest, |
| 120 TestNotificationVerifyNoNotificationId) { | |
| 74 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 121 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 75 [response setValue:@"" forKey:notification_constants::kNotificationId]; | 122 [response setValue:@"" forKey:notification_constants::kNotificationId]; |
| 76 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 123 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 77 } | 124 } |
| 78 | 125 |
| 79 TEST(NotificationPlatformBridgeMacTest, TestNotificationInvalidButton) { | 126 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyInvalidButton) { |
| 80 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 127 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 81 [response setValue:[NSNumber numberWithInt:-5] | 128 [response setValue:[NSNumber numberWithInt:-5] |
| 82 forKey:notification_constants::kNotificationButtonIndex]; | 129 forKey:notification_constants::kNotificationButtonIndex]; |
| 83 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 130 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 84 } | 131 } |
| 85 | 132 |
| 86 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingButtonIndex) { | 133 TEST_F(NotificationPlatformBridgeMacTest, |
| 134 TestNotificationVerifyMissingButtonIndex) { | |
| 87 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 135 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 88 [response | 136 [response |
| 89 removeObjectForKey:notification_constants::kNotificationButtonIndex]; | 137 removeObjectForKey:notification_constants::kNotificationButtonIndex]; |
| 90 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 138 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 91 } | 139 } |
| 92 | 140 |
| 93 TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) { | 141 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyOrigin) { |
| 94 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 142 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 95 [response setValue:@"invalidorigin" | 143 [response setValue:@"invalidorigin" |
| 96 forKey:notification_constants::kNotificationOrigin]; | 144 forKey:notification_constants::kNotificationOrigin]; |
| 97 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 145 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 98 | 146 |
| 99 // If however the origin is not present the response should be fine. | 147 // If however the origin is not present the response should be fine. |
| 100 [response removeObjectForKey:notification_constants::kNotificationOrigin]; | 148 [response removeObjectForKey:notification_constants::kNotificationOrigin]; |
| 101 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 149 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 102 } | 150 } |
| 151 | |
| 152 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { | |
| 153 std::unique_ptr<Notification> notification = | |
| 154 CreateNotification("Title", "Context", "https://gmail.com", "", ""); | |
| 155 | |
| 156 id notificationCenter = | |
|
Robert Sesek
2016/07/12 17:33:36
naming: under_scores
(maybe also just create the
Miguel Garcia
2016/07/13 14:05:16
Yeah this one was a great suggestion since allowed
| |
| 157 [OCMockObject mockForClass:[NSUserNotificationCenter class]]; | |
| 158 NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( | |
| 159 notificationCenter, false /* set_delegate */); | |
| 160 [[notificationCenter expect] | |
| 161 deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { | |
| 162 return [[toast title] isEqualToString:@"Title"] && | |
| 163 [[toast informativeText] isEqualToString:@"Context"] && | |
| 164 [[toast subtitle] isEqualToString:@"https://gmail.com"] && | |
| 165 [[toast actionButtonTitle] isEqualToString:@"Settings"] && | |
| 166 [[toast otherButtonTitle] isEqualToString:@"Close"]; | |
| 167 }]]; | |
| 168 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | |
| 169 "profile_id", false, *notification); | |
| 170 [notificationCenter verify]; | |
| 171 } | |
| 172 | |
| 173 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { | |
| 174 std::unique_ptr<Notification> notification = CreateNotification( | |
| 175 "Title", "Context", "https://gmail.com", "Button 1", ""); | |
| 176 | |
| 177 id notificationCenter = | |
| 178 [OCMockObject mockForClass:[NSUserNotificationCenter class]]; | |
| 179 NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( | |
| 180 notificationCenter, false /* set_delegate */); | |
| 181 [[notificationCenter expect] | |
| 182 deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { | |
| 183 return [[toast title] isEqualToString:@"Title"] && | |
| 184 [[toast informativeText] isEqualToString:@"Context"] && | |
| 185 [[toast subtitle] isEqualToString:@"https://gmail.com"] && | |
| 186 [[toast actionButtonTitle] isEqualToString:@"Options"] && | |
| 187 [[toast otherButtonTitle] isEqualToString:@"Close"]; | |
| 188 }]]; | |
| 189 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | |
| 190 "profile_id", false, *notification); | |
| 191 [notificationCenter verify]; | |
| 192 } | |
| OLD | NEW |