Chromium Code Reviews| Index: chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm |
| diff --git a/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm b/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm |
| index 05d30e51886a6c91367bbdfc88a1c10f8a74948e..dca9bc47529614bf11f733bf9f02a7eac3351eae 100644 |
| --- a/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm |
| +++ b/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm |
| @@ -5,12 +5,19 @@ |
| #import <AppKit/AppKit.h> |
| #include "base/mac/scoped_nsobject.h" |
| -#include "chrome/browser/notifications/notification_common.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/notifications/notification.h" |
| #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
| +#include "chrome/browser/notifications/notification_test_util.h" |
| +#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#import "third_party/ocmock/OCMock/OCMock.h" |
| +#include "third_party/ocmock/gtest_support.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "url/gurl.h" |
| namespace { |
| @@ -39,58 +46,99 @@ NSMutableDictionary* BuildDefaultNotificationResponse() { |
| } // namespace |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationValidResponse) { |
| +class NotificationPlatformBridgeMacTest : public CocoaTest { |
| + protected: |
| + std::unique_ptr<Notification> CreateNotification(const std::string& title, |
| + const std::string& subtitle, |
| + const std::string& context, |
| + const std::string& button1, |
| + 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
|
| + message_center::RichNotificationData optional_fields; |
| + optional_fields.context_message = base::UTF8ToUTF16(context); |
| + if (!button1.empty()) { |
| + optional_fields.buttons.push_back( |
| + message_center::ButtonInfo(base::UTF8ToUTF16(button1))); |
| + if (!button2.empty()) { |
| + optional_fields.buttons.push_back( |
| + message_center::ButtonInfo(base::UTF8ToUTF16(button2))); |
| + } |
| + } |
| + |
| + NotificationDelegate* delegate(new MockNotificationDelegate("id1")); |
| + |
| + SkBitmap bitmap; |
| + bitmap.allocN32Pixels(1, 1); |
| + bitmap.eraseColor(SkColorSetRGB(1, 2, 3)); |
| + 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.
|
| + |
| + std::unique_ptr<Notification> notification(new Notification( |
| + message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), |
| + base::UTF8ToUTF16(subtitle), icon, |
| + 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.
|
| + "Notifier 1"), |
| + base::UTF8ToUTF16("Notifier's Name"), GURL(), "id1", optional_fields, |
| + delegate)); |
| + |
| + return notification; |
| + } |
| +}; |
| + |
| +TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { |
| NSDictionary* response = BuildDefaultNotificationResponse(); |
| EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { |
| +TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response setValue:[NSNumber numberWithInt:210581] |
| forKey:notification_constants::kNotificationType]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownOperation) { |
| +TEST_F(NotificationPlatformBridgeMacTest, |
| + TestNotificationVerifyUnknownOperation) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response setValue:[NSNumber numberWithInt:40782] |
| forKey:notification_constants::kNotificationOperation]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingOperation) { |
| +TEST_F(NotificationPlatformBridgeMacTest, |
| + TestNotificationVerifyMissingOperation) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response removeObjectForKey:notification_constants::kNotificationOperation]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationNoProfileId) { |
| +TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyNoProfileId) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response removeObjectForKey:notification_constants::kNotificationProfileId]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationNoNotificationId) { |
| +TEST_F(NotificationPlatformBridgeMacTest, |
| + TestNotificationVerifyNoNotificationId) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response setValue:@"" forKey:notification_constants::kNotificationId]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationInvalidButton) { |
| +TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyInvalidButton) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response setValue:[NSNumber numberWithInt:-5] |
| forKey:notification_constants::kNotificationButtonIndex]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingButtonIndex) { |
| +TEST_F(NotificationPlatformBridgeMacTest, |
| + TestNotificationVerifyMissingButtonIndex) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response |
| removeObjectForKey:notification_constants::kNotificationButtonIndex]; |
| EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| -TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) { |
| +TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyOrigin) { |
| NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| [response setValue:@"invalidorigin" |
| forKey:notification_constants::kNotificationOrigin]; |
| @@ -100,3 +148,45 @@ TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) { |
| [response removeObjectForKey:notification_constants::kNotificationOrigin]; |
| EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| } |
| + |
| +TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { |
| + std::unique_ptr<Notification> notification = |
| + CreateNotification("Title", "Context", "https://gmail.com", "", ""); |
| + |
| + 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
|
| + [OCMockObject mockForClass:[NSUserNotificationCenter class]]; |
| + NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( |
| + notificationCenter, false /* set_delegate */); |
| + [[notificationCenter expect] |
| + deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { |
| + return [[toast title] isEqualToString:@"Title"] && |
| + [[toast informativeText] isEqualToString:@"Context"] && |
| + [[toast subtitle] isEqualToString:@"https://gmail.com"] && |
| + [[toast actionButtonTitle] isEqualToString:@"Settings"] && |
| + [[toast otherButtonTitle] isEqualToString:@"Close"]; |
| + }]]; |
| + bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
| + "profile_id", false, *notification); |
| + [notificationCenter verify]; |
| +} |
| + |
| +TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { |
| + std::unique_ptr<Notification> notification = CreateNotification( |
| + "Title", "Context", "https://gmail.com", "Button 1", ""); |
| + |
| + id notificationCenter = |
| + [OCMockObject mockForClass:[NSUserNotificationCenter class]]; |
| + NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( |
| + notificationCenter, false /* set_delegate */); |
| + [[notificationCenter expect] |
| + deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { |
| + return [[toast title] isEqualToString:@"Title"] && |
| + [[toast informativeText] isEqualToString:@"Context"] && |
| + [[toast subtitle] isEqualToString:@"https://gmail.com"] && |
| + [[toast actionButtonTitle] isEqualToString:@"Options"] && |
| + [[toast otherButtonTitle] isEqualToString:@"Close"]; |
| + }]]; |
| + bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
| + "profile_id", false, *notification); |
| + [notificationCenter verify]; |
| +} |