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() { | |
Robert Sesek
2016/10/19 18:22:49
This could be on NotificationPlatformBridgeMacTest
Miguel Garcia
2016/10/20 10:24:45
Acknowledged.
| |
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:@"notificationId"]; | |
37 [builder setProfileId:@"profileId"]; | |
38 [builder setIncognito:false]; | |
39 [builder | |
40 setNotificationType:[NSNumber | |
41 numberWithInt:NotificationCommon::PERSISTENT]]; | |
Robert Sesek
2016/10/19 18:22:49
Does @() work ?
Miguel Garcia
2016/10/20 10:24:45
Done.
| |
42 | |
43 return [builder buildUserNotification]; | |
44 } | |
45 | |
46 } // namespace | |
47 | |
22 class NotificationPlatformBridgeMacTest : public CocoaTest { | 48 class NotificationPlatformBridgeMacTest : public CocoaTest { |
23 protected: | 49 protected: |
24 std::unique_ptr<Notification> CreateNotification(const char* title, | 50 std::unique_ptr<Notification> CreateNotification(const char* title, |
25 const char* subtitle, | 51 const char* subtitle, |
26 const char* origin, | 52 const char* origin, |
27 const char* button1, | 53 const char* button1, |
28 const char* button2) { | 54 const char* button2) { |
29 message_center::RichNotificationData optional_fields; | 55 message_center::RichNotificationData optional_fields; |
30 optional_fields.context_message = base::UTF8ToUTF16(origin); | 56 optional_fields.context_message = base::UTF8ToUTF16(origin); |
31 if (button1) { | 57 if (button1) { |
(...skipping 10 matching lines...) Expand all Loading... | |
42 std::unique_ptr<Notification> notification(new Notification( | 68 std::unique_ptr<Notification> notification(new Notification( |
43 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), | 69 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), |
44 base::UTF8ToUTF16(subtitle), gfx::Image(), | 70 base::UTF8ToUTF16(subtitle), gfx::Image(), |
45 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"), | 71 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"), |
46 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); | 72 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); |
47 | 73 |
48 return notification; | 74 return notification; |
49 } | 75 } |
50 | 76 |
51 NSMutableDictionary* BuildDefaultNotificationResponse() { | 77 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 | 78 return [NSMutableDictionary |
72 dictionaryWithDictionary:[NotificationResponseBuilder | 79 dictionaryWithDictionary:[NotificationResponseBuilder |
73 buildDictionary:notification]]; | 80 buildDictionary:BuildNotification()]]; |
74 } | 81 } |
75 }; | 82 }; |
76 | 83 |
77 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { | 84 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { |
78 NSDictionary* response = BuildDefaultNotificationResponse(); | 85 NSDictionary* response = BuildDefaultNotificationResponse(); |
79 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); | 86 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); |
80 } | 87 } |
81 | 88 |
82 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { | 89 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { |
83 NSMutableDictionary* response = BuildDefaultNotificationResponse(); | 90 NSMutableDictionary* response = BuildDefaultNotificationResponse(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 | 177 |
171 // Expectations for notifications with one button. | 178 // Expectations for notifications with one button. |
172 - (void)expectationsOneButton:(NSUserNotification*)notification { | 179 - (void)expectationsOneButton:(NSUserNotification*)notification { |
173 EXPECT_NSEQ(@"Title", [notification title]); | 180 EXPECT_NSEQ(@"Title", [notification title]); |
174 EXPECT_NSEQ(@"Context", [notification informativeText]); | 181 EXPECT_NSEQ(@"Context", [notification informativeText]); |
175 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]); | 182 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]); |
176 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]); | 183 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]); |
177 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]); | 184 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]); |
178 } | 185 } |
179 | 186 |
187 - (NSArray<NSUserNotification*>*)expectationsDeliveredNotification { | |
188 return @[ BuildNotification() ]; | |
189 } | |
190 | |
191 - (void)expectationsRemoveDeliveredNotification: | |
192 (NSUserNotification*)notification { | |
193 EXPECT_NSEQ(@"Title", [notification title]); | |
194 EXPECT_NSEQ(@"notificationId", | |
195 [notification.userInfo | |
196 objectForKey:notification_constants::kNotificationId]); | |
197 EXPECT_NSEQ(@"profileId", | |
198 [notification.userInfo | |
199 objectForKey:notification_constants::kNotificationProfileId]); | |
200 } | |
201 | |
180 @end | 202 @end |
181 | 203 |
182 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { | 204 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { |
183 base::scoped_nsobject<NSUserNotificationCenter> notification_center( | 205 base::scoped_nsobject<NSUserNotificationCenter> notification_center( |
184 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); | 206 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); |
185 base::mac::ScopedObjCClassSwizzler swizzler( | 207 base::mac::ScopedObjCClassSwizzler swizzler( |
186 [notification_center class], @selector(deliverNotification:), | 208 [notification_center class], @selector(deliverNotification:), |
187 @selector(expectationsNoButtons:)); | 209 @selector(expectationsNoButtons:)); |
188 | 210 |
189 std::unique_ptr<Notification> notification = CreateNotification( | 211 std::unique_ptr<Notification> notification = CreateNotification( |
(...skipping 10 matching lines...) Expand all Loading... | |
200 base::scoped_nsobject<NSUserNotificationCenter> notification_center( | 222 base::scoped_nsobject<NSUserNotificationCenter> notification_center( |
201 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); | 223 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); |
202 base::mac::ScopedObjCClassSwizzler swizzler( | 224 base::mac::ScopedObjCClassSwizzler swizzler( |
203 [notification_center class], @selector(deliverNotification:), | 225 [notification_center class], @selector(deliverNotification:), |
204 @selector(expectationsOneButton:)); | 226 @selector(expectationsOneButton:)); |
205 std::unique_ptr<NotificationPlatformBridgeMac> bridge( | 227 std::unique_ptr<NotificationPlatformBridgeMac> bridge( |
206 new NotificationPlatformBridgeMac(notification_center)); | 228 new NotificationPlatformBridgeMac(notification_center)); |
207 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | 229 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
208 "profile_id", false, *notification); | 230 "profile_id", false, *notification); |
209 } | 231 } |
232 | |
233 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) { | |
234 base::scoped_nsobject<NSUserNotificationCenter> notification_center( | |
235 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); | |
236 base::mac::ScopedObjCClassSwizzler swizzler1( | |
237 [notification_center class], @selector(deliveredNotifications), | |
238 @selector(expectationsDeliveredNotification)); | |
239 base::mac::ScopedObjCClassSwizzler swizzler2( | |
Miguel Garcia
2016/10/17 14:17:43
This is needed because base::mac::ScopedObjCClassS
Peter Beverloo
2016/10/17 14:27:14
My only comment is that they could have more descr
Robert Sesek
2016/10/19 18:22:49
I think this is fine, since hopefully it'll be tem
Miguel Garcia
2016/10/20 10:24:45
SG going with that then
Miguel Garcia
2016/10/20 10:24:45
renamed
On 2016/10/17 14:27:14, Peter Beverloo wr
| |
240 [notification_center class], @selector(removeDeliveredNotification:), | |
241 @selector(expectationsRemoveDeliveredNotification:)); | |
242 | |
243 std::unique_ptr<NotificationPlatformBridgeMac> bridge( | |
244 new NotificationPlatformBridgeMac(notification_center)); | |
Peter Beverloo
2016/10/17 14:27:14
auto bridge = base::MakeUnique<NotificationPlatfor
Miguel Garcia
2016/10/20 10:24:45
Acknowledged.
| |
245 bridge->Close("profileId", "notificationId"); | |
Peter Beverloo
2016/10/17 14:27:14
You're using profile_id and notification_id elsewh
Miguel Garcia
2016/10/20 10:24:45
Done.
| |
246 } | |
OLD | NEW |