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 #import <objc/runtime.h> | 6 #import <objc/runtime.h> |
7 | 7 |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #import "base/mac/scoped_objc_class_swizzler.h" | 9 #import "base/mac/scoped_objc_class_swizzler.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/notifications/notification.h" | 11 #include "chrome/browser/notifications/notification.h" |
12 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" | 12 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
13 #include "chrome/browser/notifications/notification_test_util.h" | 13 #include "chrome/browser/notifications/notification_test_util.h" |
14 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 14 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
15 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 15 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
16 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | 16 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
17 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma
c.h" | 17 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma
c.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/gtest_mac.h" | 19 #include "testing/gtest_mac.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 | 21 |
| 22 namespace { |
| 23 |
| 24 NSUserNotification* BuildNotification() { |
| 25 base::scoped_nsobject<NotificationBuilder> builder( |
| 26 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 27 optionsLabel:@"Options" |
| 28 settingsLabel:@"Settings"]); |
| 29 [builder setTitle:@"Title"]; |
| 30 [builder setSubTitle:@"https://www.miguel.com"]; |
| 31 [builder setOrigin:@"https://www.miguel.com/"]; |
| 32 [builder setContextMessage:@""]; |
| 33 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 34 [builder setTag:@"tag1"]; |
| 35 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 36 [builder setNotificationId:@"notification_id"]; |
| 37 [builder setProfileId:@"profile_id"]; |
| 38 [builder setIncognito:false]; |
| 39 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; |
| 40 |
| 41 return [builder buildUserNotification]; |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
22 class NotificationPlatformBridgeMacTest : public CocoaTest { | 46 class NotificationPlatformBridgeMacTest : public CocoaTest { |
23 protected: | 47 protected: |
24 std::unique_ptr<Notification> CreateNotification(const char* title, | 48 std::unique_ptr<Notification> CreateNotification(const char* title, |
25 const char* subtitle, | 49 const char* subtitle, |
26 const char* origin, | 50 const char* origin, |
27 const char* button1, | 51 const char* button1, |
28 const char* button2) { | 52 const char* button2) { |
29 message_center::RichNotificationData optional_fields; | 53 message_center::RichNotificationData optional_fields; |
30 optional_fields.context_message = base::UTF8ToUTF16(origin); | 54 optional_fields.context_message = base::UTF8ToUTF16(origin); |
31 if (button1) { | 55 if (button1) { |
(...skipping 10 matching lines...) Expand all Loading... |
42 std::unique_ptr<Notification> notification(new Notification( | 66 std::unique_ptr<Notification> notification(new Notification( |
43 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), | 67 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), |
44 base::UTF8ToUTF16(subtitle), gfx::Image(), | 68 base::UTF8ToUTF16(subtitle), gfx::Image(), |
45 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"), | 69 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"), |
46 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); | 70 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); |
47 | 71 |
48 return notification; | 72 return notification; |
49 } | 73 } |
50 | 74 |
51 NSMutableDictionary* BuildDefaultNotificationResponse() { | 75 NSMutableDictionary* BuildDefaultNotificationResponse() { |
52 base::scoped_nsobject<NotificationBuilder> builder( | |
53 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" | |
54 optionsLabel:@"Options" | |
55 settingsLabel:@"Settings"]); | |
56 [builder setTitle:@"Title"]; | |
57 [builder setSubTitle:@"https://www.miguel.com"]; | |
58 [builder setOrigin:@"https://www.miguel.com/"]; | |
59 [builder setContextMessage:@""]; | |
60 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | |
61 [builder setTag:@"tag1"]; | |
62 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; | |
63 [builder setNotificationId:@"notificationId"]; | |
64 [builder setProfileId:@"profileId"]; | |
65 [builder setIncognito:false]; | |
66 [builder | |
67 setNotificationType:[NSNumber | |
68 numberWithInt:NotificationCommon::PERSISTENT]]; | |
69 | |
70 NSUserNotification* notification = [builder buildUserNotification]; | |
71 return [NSMutableDictionary | 76 return [NSMutableDictionary |
72 dictionaryWithDictionary:[NotificationResponseBuilder | 77 dictionaryWithDictionary:[NotificationResponseBuilder |
73 buildDictionary:notification]]; | 78 buildDictionary:BuildNotification()]]; |
74 } | 79 } |
75 }; | 80 }; |
76 | 81 |
77 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { | 82 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { |
78 NSDictionary* response = BuildDefaultNotificationResponse(); | 83 NSDictionary* response = BuildDefaultNotificationResponse(); |
79 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 84 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
80 } | 85 } |
81 | 86 |
82 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { | 87 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { |
83 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 88 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 175 |
171 // Expectations for notifications with one button. | 176 // Expectations for notifications with one button. |
172 - (void)expectationsOneButton:(NSUserNotification*)notification { | 177 - (void)expectationsOneButton:(NSUserNotification*)notification { |
173 EXPECT_NSEQ(@"Title", [notification title]); | 178 EXPECT_NSEQ(@"Title", [notification title]); |
174 EXPECT_NSEQ(@"Context", [notification informativeText]); | 179 EXPECT_NSEQ(@"Context", [notification informativeText]); |
175 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]); | 180 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]); |
176 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]); | 181 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]); |
177 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]); | 182 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]); |
178 } | 183 } |
179 | 184 |
| 185 - (NSArray*)expectationsDeliveredNotification { |
| 186 return @[ BuildNotification() ]; |
| 187 } |
| 188 |
| 189 - (void)expectationsRemoveDeliveredNotification: |
| 190 (NSUserNotification*)notification { |
| 191 EXPECT_NSEQ(@"Title", [notification title]); |
| 192 EXPECT_NSEQ(@"notification_id", |
| 193 [notification.userInfo |
| 194 objectForKey:notification_constants::kNotificationId]); |
| 195 EXPECT_NSEQ(@"profile_id", |
| 196 [notification.userInfo |
| 197 objectForKey:notification_constants::kNotificationProfileId]); |
| 198 } |
| 199 |
180 @end | 200 @end |
181 | 201 |
182 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { | 202 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { |
183 base::scoped_nsobject<NSUserNotificationCenter> notification_center( | 203 base::scoped_nsobject<NSUserNotificationCenter> notification_center( |
184 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); | 204 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); |
185 base::mac::ScopedObjCClassSwizzler swizzler( | 205 base::mac::ScopedObjCClassSwizzler swizzler( |
186 [notification_center class], @selector(deliverNotification:), | 206 [notification_center class], @selector(deliverNotification:), |
187 @selector(expectationsNoButtons:)); | 207 @selector(expectationsNoButtons:)); |
188 | 208 |
189 std::unique_ptr<Notification> notification = CreateNotification( | 209 std::unique_ptr<Notification> notification = CreateNotification( |
(...skipping 10 matching lines...) Expand all Loading... |
200 base::scoped_nsobject<NSUserNotificationCenter> notification_center( | 220 base::scoped_nsobject<NSUserNotificationCenter> notification_center( |
201 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); | 221 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); |
202 base::mac::ScopedObjCClassSwizzler swizzler( | 222 base::mac::ScopedObjCClassSwizzler swizzler( |
203 [notification_center class], @selector(deliverNotification:), | 223 [notification_center class], @selector(deliverNotification:), |
204 @selector(expectationsOneButton:)); | 224 @selector(expectationsOneButton:)); |
205 std::unique_ptr<NotificationPlatformBridgeMac> bridge( | 225 std::unique_ptr<NotificationPlatformBridgeMac> bridge( |
206 new NotificationPlatformBridgeMac(notification_center)); | 226 new NotificationPlatformBridgeMac(notification_center)); |
207 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | 227 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
208 "profile_id", false, *notification); | 228 "profile_id", false, *notification); |
209 } | 229 } |
| 230 |
| 231 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) { |
| 232 base::scoped_nsobject<NSUserNotificationCenter> notification_center( |
| 233 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); |
| 234 |
| 235 base::mac::ScopedObjCClassSwizzler delivered_notifications_swizzler( |
| 236 [notification_center class], @selector(deliveredNotifications), |
| 237 @selector(expectationsDeliveredNotification)); |
| 238 base::mac::ScopedObjCClassSwizzler remove_delivered_notification_swizzler( |
| 239 [notification_center class], @selector(removeDeliveredNotification:), |
| 240 @selector(expectationsRemoveDeliveredNotification:)); |
| 241 |
| 242 std::unique_ptr<NotificationPlatformBridgeMac> bridge( |
| 243 new NotificationPlatformBridgeMac(notification_center)); |
| 244 bridge->Close("profile_id", "notification_id"); |
| 245 } |
OLD | NEW |