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

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

Issue 2479143003: Add tests for the XPC client. (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"
10 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/notifications/notification.h" 10 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" 11 #include "chrome/browser/notifications/notification_platform_bridge_mac.h"
13 #include "chrome/browser/notifications/notification_test_util.h" 12 #include "chrome/browser/notifications/notification_test_util.h"
13 #include "chrome/browser/notifications/stub_alert_dispatcher_mac.h"
14 #include "chrome/browser/notifications/stub_notification_center_mac.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 "chrome/common/features.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gtest_mac.h" 20 #include "testing/gtest_mac.h"
20 #include "url/gurl.h" 21 #include "url/gurl.h"
21 22
22 class NotificationPlatformBridgeMacTest : public testing::Test { 23 class NotificationPlatformBridgeMacTest : public testing::Test {
23 public: 24 public:
24 void SetUp() override { 25 void SetUp() override {
25 notification_center_.reset([[StubNotificationCenter alloc] init]); 26 notification_center_.reset([[StubNotificationCenter alloc] init]);
26 } 27 }
27 28
(...skipping 15 matching lines...) Expand all
43 [builder setTag:@"tag1"]; 44 [builder setTag:@"tag1"];
44 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; 45 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]];
45 [builder setNotificationId:@"notification_id"]; 46 [builder setNotificationId:@"notification_id"];
46 [builder setProfileId:@"profile_id"]; 47 [builder setProfileId:@"profile_id"];
47 [builder setIncognito:false]; 48 [builder setIncognito:false];
48 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; 49 [builder setNotificationType:@(NotificationCommon::PERSISTENT)];
49 50
50 return [builder buildUserNotification]; 51 return [builder buildUserNotification];
51 } 52 }
52 53
54 std::unique_ptr<Notification> CreateBanner(const char* title,
55 const char* subtitle,
56 const char* origin,
57 const char* button1,
58 const char* button2) {
59 return CreateNotification(title, subtitle, origin, button1, button2,
60 false /* require_interaction */);
61 }
62
63 std::unique_ptr<Notification> CreateAlert(const char* title,
64 const char* subtitle,
65 const char* origin,
66 const char* button1,
67 const char* button2) {
68 return CreateNotification(title, subtitle, origin, button1, button2,
69 true /* require_interaction */);
70 }
71
53 std::unique_ptr<Notification> CreateNotification(const char* title, 72 std::unique_ptr<Notification> CreateNotification(const char* title,
54 const char* subtitle, 73 const char* subtitle,
55 const char* origin, 74 const char* origin,
56 const char* button1, 75 const char* button1,
57 const char* button2) { 76 const char* button2,
77 bool require_interaction) {
58 message_center::RichNotificationData optional_fields; 78 message_center::RichNotificationData optional_fields;
59 optional_fields.context_message = base::UTF8ToUTF16(origin); 79 optional_fields.context_message = base::UTF8ToUTF16(origin);
60 if (button1) { 80 if (button1) {
61 optional_fields.buttons.push_back( 81 optional_fields.buttons.push_back(
62 message_center::ButtonInfo(base::UTF8ToUTF16(button1))); 82 message_center::ButtonInfo(base::UTF8ToUTF16(button1)));
63 if (button2) { 83 if (button2) {
64 optional_fields.buttons.push_back( 84 optional_fields.buttons.push_back(
65 message_center::ButtonInfo(base::UTF8ToUTF16(button2))); 85 message_center::ButtonInfo(base::UTF8ToUTF16(button2)));
66 } 86 }
67 } 87 }
68 88
69 GURL url = GURL(origin); 89 GURL url = GURL(origin);
70 90
71 std::unique_ptr<Notification> notification(new Notification( 91 std::unique_ptr<Notification> notification(new Notification(
72 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title), 92 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title),
73 base::UTF8ToUTF16(subtitle), gfx::Image(), 93 base::UTF8ToUTF16(subtitle), gfx::Image(),
74 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"), 94 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"),
75 url, "id1", optional_fields, new MockNotificationDelegate("id1"))); 95 url, "id1", optional_fields, new MockNotificationDelegate("id1")));
96 if (require_interaction)
97 notification->set_never_timeout(true);
76 98
77 return notification; 99 return notification;
78 } 100 }
79 101
80 NSMutableDictionary* BuildDefaultNotificationResponse() { 102 NSMutableDictionary* BuildDefaultNotificationResponse() {
81 return [NSMutableDictionary 103 return [NSMutableDictionary
82 dictionaryWithDictionary:[NotificationResponseBuilder 104 dictionaryWithDictionary:[NotificationResponseBuilder
83 buildDictionary:BuildNotification()]]; 105 buildDictionary:BuildNotification()]];
84 } 106 }
85 107
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 [response setValue:@"invalidorigin" 173 [response setValue:@"invalidorigin"
152 forKey:notification_constants::kNotificationOrigin]; 174 forKey:notification_constants::kNotificationOrigin];
153 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 175 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
154 176
155 // If however the origin is not present the response should be fine. 177 // If however the origin is not present the response should be fine.
156 [response removeObjectForKey:notification_constants::kNotificationOrigin]; 178 [response removeObjectForKey:notification_constants::kNotificationOrigin];
157 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 179 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
158 } 180 }
159 181
160 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) { 182 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) {
161 std::unique_ptr<Notification> notification = CreateNotification( 183 std::unique_ptr<Notification> notification =
162 "Title", "Context", "https://gmail.com", nullptr, nullptr); 184 CreateBanner("Title", "Context", "https://gmail.com", nullptr, nullptr);
163 185
164 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 186 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
165 new NotificationPlatformBridgeMac(notification_center())); 187 new NotificationPlatformBridgeMac(notification_center(), nullptr));
166 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 188 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
167 "profile_id", false, *notification); 189 "profile_id", false, *notification);
168 NSArray* notifications = [notification_center() deliveredNotifications]; 190 NSArray* notifications = [notification_center() deliveredNotifications];
169 191
170 EXPECT_EQ(1u, [notifications count]); 192 EXPECT_EQ(1u, [notifications count]);
171 193
172 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; 194 NSUserNotification* delivered_notification = [notifications objectAtIndex:0];
173 EXPECT_NSEQ(@"Title", [delivered_notification title]); 195 EXPECT_NSEQ(@"Title", [delivered_notification title]);
174 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); 196 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]);
175 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]); 197 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]);
176 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); 198 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]);
177 EXPECT_NSEQ(@"Settings", [delivered_notification actionButtonTitle]); 199 EXPECT_NSEQ(@"Settings", [delivered_notification actionButtonTitle]);
178 } 200 }
179 201
180 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { 202 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) {
181 std::unique_ptr<Notification> notification = CreateNotification( 203 std::unique_ptr<Notification> notification = CreateBanner(
182 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 204 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
183 205
184 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 206 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
185 new NotificationPlatformBridgeMac(notification_center())); 207 new NotificationPlatformBridgeMac(notification_center(), nullptr));
186 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 208 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
187 "profile_id", false, *notification); 209 "profile_id", false, *notification);
188 210
189 NSArray* notifications = [notification_center() deliveredNotifications]; 211 NSArray* notifications = [notification_center() deliveredNotifications];
190 EXPECT_EQ(1u, [notifications count]); 212 EXPECT_EQ(1u, [notifications count]);
191 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; 213 NSUserNotification* delivered_notification = [notifications objectAtIndex:0];
192 EXPECT_NSEQ(@"Title", [delivered_notification title]); 214 EXPECT_NSEQ(@"Title", [delivered_notification title]);
193 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); 215 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]);
194 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]); 216 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]);
195 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); 217 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]);
196 EXPECT_NSEQ(@"Options", [delivered_notification actionButtonTitle]); 218 EXPECT_NSEQ(@"Options", [delivered_notification actionButtonTitle]);
197 } 219 }
198 220
199 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) { 221 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNotification) {
200 std::unique_ptr<Notification> notification = CreateNotification( 222 std::unique_ptr<Notification> notification = CreateBanner(
201 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 223 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
202 224
203 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 225 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
204 new NotificationPlatformBridgeMac(notification_center())); 226 new NotificationPlatformBridgeMac(notification_center(), nullptr));
205 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 227 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
206 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 228 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
207 "profile_id", false, *notification); 229 "profile_id", false, *notification);
208 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 230 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
209 231
210 bridge->Close("profile_id", "notification_id"); 232 bridge->Close("profile_id", "notification_id");
211 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 233 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
212 } 234 }
213 235
214 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNonExistingNotification) { 236 TEST_F(NotificationPlatformBridgeMacTest, TestCloseNonExistingNotification) {
215 std::unique_ptr<Notification> notification = CreateNotification( 237 std::unique_ptr<Notification> notification = CreateBanner(
216 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 238 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
217 239
218 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 240 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
219 new NotificationPlatformBridgeMac(notification_center())); 241 new NotificationPlatformBridgeMac(notification_center(), nullptr));
220 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 242 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
221 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 243 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
222 "profile_id", false, *notification); 244 "profile_id", false, *notification);
223 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 245 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
224 246
225 bridge->Close("profile_id_does_not_exist", "notification_id"); 247 bridge->Close("profile_id_does_not_exist", "notification_id");
226 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 248 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
227 } 249 }
228 250
229 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayed) { 251 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayed) {
230 std::unique_ptr<Notification> notification = CreateNotification( 252 std::unique_ptr<Notification> notification = CreateBanner(
231 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 253 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
232 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 254 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
233 new NotificationPlatformBridgeMac(notification_center())); 255 new NotificationPlatformBridgeMac(notification_center(), nullptr));
234 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 256 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
235 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 257 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
236 "profile_id", false, *notification); 258 "profile_id", false, *notification);
237 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 259 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
238 260
239 std::set<std::string> notifications; 261 std::set<std::string> notifications;
240 EXPECT_TRUE(bridge->GetDisplayed("profile_id", false, &notifications)); 262 EXPECT_TRUE(bridge->GetDisplayed("profile_id", false, &notifications));
241 EXPECT_EQ(1u, notifications.size()); 263 EXPECT_EQ(1u, notifications.size());
242 } 264 }
243 265
244 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayedUnknownProfile) { 266 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayedUnknownProfile) {
245 std::unique_ptr<Notification> notification = CreateNotification( 267 std::unique_ptr<Notification> notification = CreateBanner(
246 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 268 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
247 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 269 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
248 new NotificationPlatformBridgeMac(notification_center())); 270 new NotificationPlatformBridgeMac(notification_center(), nullptr));
249 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 271 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
250 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 272 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
251 "profile_id", false, *notification); 273 "profile_id", false, *notification);
252 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 274 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
253 275
254 std::set<std::string> notifications; 276 std::set<std::string> notifications;
255 EXPECT_TRUE( 277 EXPECT_TRUE(
256 bridge->GetDisplayed("unknown_profile_id", false, &notifications)); 278 bridge->GetDisplayed("unknown_profile_id", false, &notifications));
257 EXPECT_EQ(0u, notifications.size()); 279 EXPECT_EQ(0u, notifications.size());
258 } 280 }
259 281
260 TEST_F(NotificationPlatformBridgeMacTest, TestQuitRemovesNotifications) { 282 TEST_F(NotificationPlatformBridgeMacTest, TestQuitRemovesNotifications) {
261 std::unique_ptr<Notification> notification = CreateNotification( 283 std::unique_ptr<Notification> notification = CreateBanner(
262 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 284 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
263 { 285 {
264 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 286 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
265 new NotificationPlatformBridgeMac(notification_center())); 287 new NotificationPlatformBridgeMac(notification_center(), nullptr));
266 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 288 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
267 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 289 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
268 "profile_id", false, *notification); 290 "profile_id", false, *notification);
269 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 291 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
270 } 292 }
271 293
272 // The destructor of the bridge should close all notifications. 294 // The destructor of the bridge should close all notifications.
273 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 295 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
274 } 296 }
297
298 // TODO(miguelg) There is some duplication between these tests and the ones
299 // Above. Once the flag is removed most tests can be merged.
300 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS)
301 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayAlert) {
302 StubAlertDispatcher* alert_dispatcher = [[StubAlertDispatcher alloc] init];
Robert Sesek 2016/11/11 19:21:16 The ownership here is a bit odd. I'd expect this t
Miguel Garcia 2016/11/21 11:57:56 Ok so after our chat I went with retaining both th
303 std::unique_ptr<Notification> alert =
304 CreateAlert("Title", "Context", "https://gmail.com", "Button 1", nullptr);
305 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
306 new NotificationPlatformBridgeMac(notification_center(),
307 alert_dispatcher));
308 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
309 "profile_id", false, *alert);
310 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
311 EXPECT_EQ(1u, [[alert_dispatcher alerts] count]);
312 }
313
314 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayBannerAndAlert) {
315 StubAlertDispatcher* alert_dispatcher = [[StubAlertDispatcher alloc] init];
316 std::unique_ptr<Notification> alert =
317 CreateAlert("Title", "Context", "https://gmail.com", "Button 1", nullptr);
318 std::unique_ptr<Notification> banner = CreateBanner(
319 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
320 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
321 new NotificationPlatformBridgeMac(notification_center(),
322 alert_dispatcher));
323 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
324 "profile_id", false, *banner);
325 bridge->Display(NotificationCommon::PERSISTENT, "notification_id2",
326 "profile_id", false, *alert);
327 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
328 EXPECT_EQ(1u, [[alert_dispatcher alerts] count]);
329 }
330
331 TEST_F(NotificationPlatformBridgeMacTest, TestCloseAlert) {
332 std::unique_ptr<Notification> alert =
333 CreateAlert("Title", "Context", "https://gmail.com", "Button 1", nullptr);
334 StubAlertDispatcher* alert_dispatcher = [[StubAlertDispatcher alloc] init];
335 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
336 new NotificationPlatformBridgeMac(notification_center(),
337 alert_dispatcher));
338 EXPECT_EQ(0u, [[alert_dispatcher alerts] count]);
339 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
340 "profile_id", false, *alert);
341 EXPECT_EQ(1u, [[alert_dispatcher alerts] count]);
342
343 bridge->Close("profile_id", "notification_id");
344 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
345 }
346
347 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698