Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm

Issue 2458703003: Create a stub NSUserNotificationCenter and make tests use it. (Closed)
Patch Set: review Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/notifications/stub_notification_center_mac.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 { 22 class NotificationPlatformBridgeMacTest : public testing::Test {
23 public:
24 void SetUp() override {
25 notification_center_.reset([[StubNotificationCenter alloc] init]);
26 }
23 27
24 NSUserNotification* BuildNotification() { 28 void TearDown() override {
25 base::scoped_nsobject<NotificationBuilder> builder( 29 [notification_center_ removeAllDeliveredNotifications];
26 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" 30 }
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 31
41 return [builder buildUserNotification]; 32 protected:
42 } 33 NSUserNotification* BuildNotification() {
34 base::scoped_nsobject<NotificationBuilder> builder(
35 [[NotificationBuilder alloc] initWithCloseLabel:@"Close"
36 optionsLabel:@"Options"
37 settingsLabel:@"Settings"]);
38 [builder setTitle:@"Title"];
39 [builder setSubTitle:@"https://www.miguel.com"];
40 [builder setOrigin:@"https://www.miguel.com/"];
41 [builder setContextMessage:@""];
42 [builder setButtons:@"Button1" secondaryButton:@"Button2"];
43 [builder setTag:@"tag1"];
44 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]];
45 [builder setNotificationId:@"notification_id"];
46 [builder setProfileId:@"profile_id"];
47 [builder setIncognito:false];
48 [builder setNotificationType:@(NotificationCommon::PERSISTENT)];
43 49
44 } // namespace 50 return [builder buildUserNotification];
51 }
45 52
46 class NotificationPlatformBridgeMacTest : public CocoaTest {
47 protected:
48 std::unique_ptr<Notification> CreateNotification(const char* title, 53 std::unique_ptr<Notification> CreateNotification(const char* title,
49 const char* subtitle, 54 const char* subtitle,
50 const char* origin, 55 const char* origin,
51 const char* button1, 56 const char* button1,
52 const char* button2) { 57 const char* button2) {
53 message_center::RichNotificationData optional_fields; 58 message_center::RichNotificationData optional_fields;
54 optional_fields.context_message = base::UTF8ToUTF16(origin); 59 optional_fields.context_message = base::UTF8ToUTF16(origin);
55 if (button1) { 60 if (button1) {
56 optional_fields.buttons.push_back( 61 optional_fields.buttons.push_back(
57 message_center::ButtonInfo(base::UTF8ToUTF16(button1))); 62 message_center::ButtonInfo(base::UTF8ToUTF16(button1)));
(...skipping 12 matching lines...) Expand all
70 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); 75 url, "id1", optional_fields, new MockNotificationDelegate("id1")));
71 76
72 return notification; 77 return notification;
73 } 78 }
74 79
75 NSMutableDictionary* BuildDefaultNotificationResponse() { 80 NSMutableDictionary* BuildDefaultNotificationResponse() {
76 return [NSMutableDictionary 81 return [NSMutableDictionary
77 dictionaryWithDictionary:[NotificationResponseBuilder 82 dictionaryWithDictionary:[NotificationResponseBuilder
78 buildDictionary:BuildNotification()]]; 83 buildDictionary:BuildNotification()]];
79 } 84 }
85
86 NSUserNotificationCenter* notification_center() {
87 return notification_center_.get();
88 }
89
90 private:
91 base::scoped_nsobject<StubNotificationCenter> notification_center_;
80 }; 92 };
81 93
82 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { 94 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) {
83 NSDictionary* response = BuildDefaultNotificationResponse(); 95 NSDictionary* response = BuildDefaultNotificationResponse();
84 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 96 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
85 } 97 }
86 98
87 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { 99 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) {
88 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 100 NSMutableDictionary* response = BuildDefaultNotificationResponse();
89 [response setValue:[NSNumber numberWithInt:210581] 101 [response setValue:[NSNumber numberWithInt:210581]
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 150 NSMutableDictionary* response = BuildDefaultNotificationResponse();
139 [response setValue:@"invalidorigin" 151 [response setValue:@"invalidorigin"
140 forKey:notification_constants::kNotificationOrigin]; 152 forKey:notification_constants::kNotificationOrigin];
141 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 153 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
142 154
143 // If however the origin is not present the response should be fine. 155 // If however the origin is not present the response should be fine.
144 [response removeObjectForKey:notification_constants::kNotificationOrigin]; 156 [response removeObjectForKey:notification_constants::kNotificationOrigin];
145 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 157 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
146 } 158 }
147 159
148 // The usual [NSUSerNotificationCenter defaultNotificationCenter] constructor
149 // is not available in tests. The private constructor fortunatelly is.
150 @interface NSUserNotificationCenter (PrivateAPI)
151 + (NSUserNotificationCenter*)_centerForIdentifier:(NSString*)ident
152 type:(NSUInteger)type;
153 @end
154
155 // Category to extend the notification center with different implementations
156 // of the deliverNotification selector which can be swizzled as part of the
157 // test.
158 // TODO(miguelg) replace this with OCMock once a version with support
159 // for dynamic properties is rolled out (crbug.com/622753).
160 @interface NSUserNotificationCenter (TestAdditions)
161 - (void)expectationsNoButtons:(NSUserNotification*)notification;
162 - (void)expectationsOneButton:(NSUserNotification*)notification;
163 @end
164
165 @implementation NSUserNotificationCenter (TestAdditions)
166
167 // Expectations for notifications with no buttons.
168 - (void)expectationsNoButtons:(NSUserNotification*)notification {
169 EXPECT_NSEQ(@"Title", [notification title]);
170 EXPECT_NSEQ(@"Context", [notification informativeText]);
171 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]);
172 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]);
173 EXPECT_NSEQ(@"Settings", [notification actionButtonTitle]);
174 }
175
176 // Expectations for notifications with one button.
177 - (void)expectationsOneButton:(NSUserNotification*)notification {
178 EXPECT_NSEQ(@"Title", [notification title]);
179 EXPECT_NSEQ(@"Context", [notification informativeText]);
180 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]);
181 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]);
182 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]);
183 }
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
200 @end
201
202 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { 160 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) {
203 base::scoped_nsobject<NSUserNotificationCenter> notification_center(
204 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
205 base::mac::ScopedObjCClassSwizzler swizzler(
206 [notification_center class], @selector(deliverNotification:),
207 @selector(expectationsNoButtons:));
208
209 std::unique_ptr<Notification> notification = CreateNotification( 161 std::unique_ptr<Notification> notification = CreateNotification(
210 "Title", "Context", "https://gmail.com", nullptr, nullptr); 162 "Title", "Context", "https://gmail.com", nullptr, nullptr);
163
211 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 164 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
212 new NotificationPlatformBridgeMac(notification_center)); 165 new NotificationPlatformBridgeMac(notification_center()));
213 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 166 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
214 "profile_id", false, *notification); 167 "profile_id", false, *notification);
168 NSArray* notifications = [notification_center() deliveredNotifications];
169
170 EXPECT_EQ(1u, [notifications count]);
171
172 NSUserNotification* delivered_notification = [notifications objectAtIndex:0];
173 EXPECT_NSEQ(@"Title", [delivered_notification title]);
174 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]);
175 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]);
176 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]);
177 EXPECT_NSEQ(@"Settings", [delivered_notification actionButtonTitle]);
215 } 178 }
216 179
217 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { 180 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) {
218 std::unique_ptr<Notification> notification = CreateNotification( 181 std::unique_ptr<Notification> notification = CreateNotification(
219 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 182 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
220 base::scoped_nsobject<NSUserNotificationCenter> notification_center( 183
221 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
222 base::mac::ScopedObjCClassSwizzler swizzler(
223 [notification_center class], @selector(deliverNotification:),
224 @selector(expectationsOneButton:));
225 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 184 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
226 new NotificationPlatformBridgeMac(notification_center)); 185 new NotificationPlatformBridgeMac(notification_center()));
227 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 186 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
228 "profile_id", false, *notification); 187 "profile_id", false, *notification);
188
189 NSArray* notifications = [notification_center() deliveredNotifications];
190 EXPECT_EQ(1u, [notifications count]);
191 NSUserNotification* delivered_notification = [notifications objectAtIndex:0];
192 EXPECT_NSEQ(@"Title", [delivered_notification title]);
193 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]);
194 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]);
195 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]);
196 EXPECT_NSEQ(@"Options", [delivered_notification actionButtonTitle]);
229 } 197 }
230 198
231 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) { 199 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) {
232 base::scoped_nsobject<NSUserNotificationCenter> notification_center( 200 std::unique_ptr<Notification> notification = CreateNotification(
233 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]); 201 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
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 202
242 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 203 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
243 new NotificationPlatformBridgeMac(notification_center)); 204 new NotificationPlatformBridgeMac(notification_center()));
205 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
206 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
207 "profile_id", false, *notification);
208 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
209
244 bridge->Close("profile_id", "notification_id"); 210 bridge->Close("profile_id", "notification_id");
211 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
245 } 212 }
213
214 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNonExistingNotification) {
215 std::unique_ptr<Notification> notification = CreateNotification(
216 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
217
218 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
219 new NotificationPlatformBridgeMac(notification_center()));
220 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
221 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
222 "profile_id", false, *notification);
223 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
224
225 bridge->Close("profile_id_does_not_exist", "notification_id");
226 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
227 }
228
229 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayed) {
230 std::unique_ptr<Notification> notification = CreateNotification(
231 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
232 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
233 new NotificationPlatformBridgeMac(notification_center()));
234 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
235 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
236 "profile_id", false, *notification);
237 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
238
239 std::set<std::string> notifications;
240 EXPECT_TRUE(bridge->GetDisplayed("profile_id", false, &notifications));
241 EXPECT_EQ(1u, notifications.size());
242 }
243
244 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayedUnknownProfile) {
245 std::unique_ptr<Notification> notification = CreateNotification(
246 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
247 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
248 new NotificationPlatformBridgeMac(notification_center()));
249 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
250 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
251 "profile_id", false, *notification);
252 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
253
254 std::set<std::string> notifications;
255 EXPECT_TRUE(
256 bridge->GetDisplayed("unknown_profile_id", false, &notifications));
257 EXPECT_EQ(0u, notifications.size());
258 }
259
260 TEST_F(NotificationPlatformBridgeMacTest, TestQuitRemovesNotifications) {
261 std::unique_ptr<Notification> notification = CreateNotification(
262 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
263 {
264 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
265 new NotificationPlatformBridgeMac(notification_center()));
266 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
267 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
268 "profile_id", false, *notification);
269 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
270 }
271
272 // The destructor of the bridge should close all notifications.
273 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698