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 "chrome/browser/notifications/notification.h" |
| 9 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" | 9 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
| 10 #include "chrome/browser/notifications/notification_test_util.h" | |
| 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 10 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 12 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 11 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | 13 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 12 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" | 14 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #import "third_party/ocmock/OCMock/OCMock.h" | |
| 17 #include "third_party/ocmock/gtest_support.h" | |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 NSMutableDictionary* BuildDefaultNotificationResponse() { | 21 NSMutableDictionary* BuildDefaultNotificationResponse() { |
| 18 base::scoped_nsobject<NotificationBuilder> builder( | 22 base::scoped_nsobject<NotificationBuilder> builder( |
| 19 [[NotificationBuilder alloc] init]); | 23 [[NotificationBuilder alloc] init]); |
| 20 [builder setTitle:@"Title"]; | 24 [builder setTitle:@"Title"]; |
| 21 [builder setSubTitle:@"https://www.miguel.com"]; | 25 [builder setSubTitle:@"https://www.miguel.com"]; |
| 22 [builder setOrigin:@"https://www.miguel.com/"]; | 26 [builder setOrigin:@"https://www.miguel.com/"]; |
| 23 [builder setContextMessage:@""]; | 27 [builder setContextMessage:@""]; |
| 24 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | 28 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 25 [builder setTag:@"tag1"]; | 29 [builder setTag:@"tag1"]; |
| 26 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; | 30 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 27 [builder setNotificationId:@"notificationId"]; | 31 [builder setNotificationId:@"notificationId"]; |
| 28 [builder setProfileId:@"profileId"]; | 32 [builder setProfileId:@"profileId"]; |
| 29 [builder setIncognito:false]; | 33 [builder setIncognito:false]; |
| 30 [builder | 34 [builder |
| 31 setNotificationType:[NSNumber | 35 setNotificationType:[NSNumber |
| 32 numberWithInt:NotificationCommon::PERSISTENT]]; | 36 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 33 | 37 |
| 34 NSUserNotification* notification = [builder buildUserNotification]; | 38 NSUserNotification* notification = [builder buildUserNotification]; |
| 35 return [NSMutableDictionary | 39 return [NSMutableDictionary |
| 36 dictionaryWithDictionary:[NotificationResponseBuilder | 40 dictionaryWithDictionary:[NotificationResponseBuilder |
| 37 buildDictionary:notification]]; | 41 buildDictionary:notification]]; |
| 38 } | 42 } |
| 39 | 43 |
| 40 } // namespace | 44 } // namespace |
| 41 | 45 |
| 42 TEST(NotificationPlatformBridgeMacTest, TestNotificationValidResponse) { | 46 class NotificationPlatformBridgeMacTest : public CocoaTest { |
| 47 public: | |
| 48 NotificationPlatformBridgeMacTest() {} | |
| 49 | |
| 50 void SetUp() override { CocoaTest::SetUp(); } | |
| 51 | |
| 52 void TearDown() override { CocoaTest::TearDown(); } | |
|
Peter Beverloo
2016/07/11 17:25:48
Please define this as:
class NotificationPlatform
Miguel Garcia
2016/07/12 16:16:24
Really? Don't we have to call CocoaTest::SetUp and
Peter Beverloo
2016/07/12 17:44:19
CocoaTest already defines them, which you inherit
Miguel Garcia
2016/07/13 14:05:16
Duh, I guess I deserve the lecture :) I misread it
| |
| 53 | |
| 54 private: | |
| 55 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeMacTest); | |
| 56 }; | |
| 57 | |
| 58 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { | |
| 43 NSDictionary* response = BuildDefaultNotificationResponse(); | 59 NSDictionary* response = BuildDefaultNotificationResponse(); |
| 44 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 60 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 45 } | 61 } |
| 46 | 62 |
| 47 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { | 63 TEST_F(NotificationPlatformBridgeMacTest, |
|
Peter Beverloo
2016/07/11 17:25:48
Why did this test go away?
Miguel Garcia
2016/07/12 16:16:24
I think it was a merge issue. Brought it back
| |
| 48 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 64 TestNotificationVerifyUnknownOperation) { |
| 49 [response setValue:[NSNumber numberWithInt:210581] | |
| 50 forKey:notification_constants::kNotificationType]; | |
| 51 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | |
| 52 } | |
| 53 | |
| 54 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownOperation) { | |
| 55 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 65 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 56 [response setValue:[NSNumber numberWithInt:40782] | 66 [response setValue:[NSNumber numberWithInt:40782] |
| 57 forKey:notification_constants::kNotificationOperation]; | 67 forKey:notification_constants::kNotificationOperation]; |
| 58 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 68 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 59 } | 69 } |
| 60 | 70 |
| 61 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingOperation) { | 71 TEST_F(NotificationPlatformBridgeMacTest, |
| 72 TestNotificationVerifyMissingOperation) { | |
| 62 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 73 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 63 [response removeObjectForKey:notification_constants::kNotificationOperation]; | 74 [response removeObjectForKey:notification_constants::kNotificationOperation]; |
| 64 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 75 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 65 } | 76 } |
| 66 | 77 |
| 67 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoProfileId) { | 78 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyNoProfileId) { |
| 68 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 79 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 69 [response removeObjectForKey:notification_constants::kNotificationProfileId]; | 80 [response removeObjectForKey:notification_constants::kNotificationProfileId]; |
| 70 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 81 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 71 } | 82 } |
| 72 | 83 |
| 73 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoNotificationId) { | 84 TEST_F(NotificationPlatformBridgeMacTest, |
| 85 TestNotificationVerifyNoNotificationId) { | |
| 74 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 86 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 75 [response setValue:@"" forKey:notification_constants::kNotificationId]; | 87 [response setValue:@"" forKey:notification_constants::kNotificationId]; |
| 76 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 88 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 77 } | 89 } |
| 78 | 90 |
| 79 TEST(NotificationPlatformBridgeMacTest, TestNotificationInvalidButton) { | 91 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyInvalidButton) { |
| 80 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 92 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 81 [response setValue:[NSNumber numberWithInt:-5] | 93 [response setValue:[NSNumber numberWithInt:-5] |
| 82 forKey:notification_constants::kNotificationButtonIndex]; | 94 forKey:notification_constants::kNotificationButtonIndex]; |
| 83 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 95 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 84 } | 96 } |
| 85 | 97 |
| 86 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingButtonIndex) { | 98 TEST_F(NotificationPlatformBridgeMacTest, |
| 99 TestNotificationVerifyMissingButtonIndex) { | |
| 87 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 100 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 88 [response | 101 [response |
| 89 removeObjectForKey:notification_constants::kNotificationButtonIndex]; | 102 removeObjectForKey:notification_constants::kNotificationButtonIndex]; |
| 90 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 103 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 91 } | 104 } |
| 92 | 105 |
| 93 TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) { | 106 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyOrigin) { |
| 94 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 107 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
| 95 [response setValue:@"invalidorigin" | 108 [response setValue:@"invalidorigin" |
| 96 forKey:notification_constants::kNotificationOrigin]; | 109 forKey:notification_constants::kNotificationOrigin]; |
| 97 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 110 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 98 | 111 |
| 99 // If however the origin is not present the response should be fine. | 112 // If however the origin is not present the response should be fine. |
| 100 [response removeObjectForKey:notification_constants::kNotificationOrigin]; | 113 [response removeObjectForKey:notification_constants::kNotificationOrigin]; |
| 101 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 114 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
| 102 } | 115 } |
| 116 | |
| 117 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { | |
| 118 std::unique_ptr<Notification> notification = | |
| 119 NotificationGenerator::CreateNotification( | |
| 120 "Title", "Context", "https://gmail.com", "", "", | |
|
Peter Beverloo
2016/07/11 17:25:48
I'm don't think I'm in favor of this generalizatio
Miguel Garcia
2016/07/12 11:06:59
What do you suggest? I can't really use the constr
Miguel Garcia
2016/07/12 16:16:24
Discussed offline, added its own CreateNotificatio
| |
| 121 message_center::NOTIFICATION_TYPE_SIMPLE); | |
| 122 | |
| 123 id notificationCenter = | |
| 124 [OCMockObject mockForClass:[NSUserNotificationCenter class]]; | |
| 125 NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( | |
| 126 notificationCenter, false /* set_delegate */); | |
| 127 [[notificationCenter expect] | |
| 128 deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { | |
| 129 return [[toast title] isEqualToString:@"Title"] && | |
| 130 [[toast informativeText] isEqualToString:@"Context"] && | |
| 131 [[toast subtitle] isEqualToString:@"https://gmail.com"] && | |
| 132 [[toast actionButtonTitle] isEqualToString:@"Settings"] && | |
| 133 [[toast otherButtonTitle] isEqualToString:@"Close"]; | |
| 134 }]]; | |
| 135 bridge->Display(NotificationCommon::PERSISTENT, | |
| 136 "notification_id", "profile_id", false, *notification); | |
| 137 [notificationCenter verify]; | |
| 138 } | |
| 139 | |
| 140 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { | |
| 141 std::unique_ptr<Notification> notification = | |
| 142 NotificationGenerator::CreateNotification( | |
| 143 "Title", "Context", "https://gmail.com", "Button 1", "", | |
| 144 message_center::NOTIFICATION_TYPE_SIMPLE); | |
| 145 | |
| 146 id notificationCenter = | |
| 147 [OCMockObject mockForClass:[NSUserNotificationCenter class]]; | |
| 148 NotificationPlatformBridgeMac* bridge = new NotificationPlatformBridgeMac( | |
| 149 notificationCenter, false /* set_delegate */); | |
| 150 [[notificationCenter expect] | |
| 151 deliverNotification:[OCMArg checkWithBlock:^BOOL(id toast) { | |
| 152 return [[toast title] isEqualToString:@"Title"] && | |
| 153 [[toast informativeText] isEqualToString:@"Context"] && | |
| 154 [[toast subtitle] isEqualToString:@"https://gmail.com"] && | |
| 155 [[toast actionButtonTitle] isEqualToString:@"Options"] && | |
| 156 [[toast otherButtonTitle] isEqualToString:@"Close"]; | |
| 157 }]]; | |
| 158 bridge->Display(NotificationCommon::PERSISTENT, | |
| 159 "notification_id", "profile_id", false, *notification); | |
| 160 [notificationCenter verify]; | |
| 161 } | |
| OLD | NEW |