| 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/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 [alert_dispatcher_ closeAllNotifications]; | 40 [alert_dispatcher_ closeAllNotifications]; |
| 41 } | 41 } |
| 42 | 42 |
| 43 protected: | 43 protected: |
| 44 NSUserNotification* BuildNotification() { | 44 NSUserNotification* BuildNotification() { |
| 45 base::scoped_nsobject<NotificationBuilder> builder( | 45 base::scoped_nsobject<NotificationBuilder> builder( |
| 46 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" | 46 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 47 optionsLabel:@"More" | 47 optionsLabel:@"More" |
| 48 settingsLabel:@"Settings"]); | 48 settingsLabel:@"Settings"]); |
| 49 [builder setTitle:@"Title"]; | 49 [builder setTitle:@"Title"]; |
| 50 [builder setSubTitle:@"https://www.miguel.com"]; | |
| 51 [builder setOrigin:@"https://www.miguel.com/"]; | 50 [builder setOrigin:@"https://www.miguel.com/"]; |
| 52 [builder setContextMessage:@""]; | 51 [builder setContextMessage:@""]; |
| 53 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | 52 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 54 [builder setTag:@"tag1"]; | 53 [builder setTag:@"tag1"]; |
| 55 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; | 54 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 56 [builder setNotificationId:@"notification_id"]; | 55 [builder setNotificationId:@"notification_id"]; |
| 57 [builder setProfileId:@"profile_id"]; | 56 [builder setProfileId:@"profile_id"]; |
| 58 [builder setIncognito:false]; | 57 [builder setIncognito:false]; |
| 59 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; | 58 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; |
| 60 [builder setShowSettingsButton:true]; | 59 [builder setShowSettingsButton:true]; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 89 true /* require_interaction */); | 88 true /* require_interaction */); |
| 90 } | 89 } |
| 91 | 90 |
| 92 std::unique_ptr<Notification> CreateNotification(const char* title, | 91 std::unique_ptr<Notification> CreateNotification(const char* title, |
| 93 const char* subtitle, | 92 const char* subtitle, |
| 94 const char* origin, | 93 const char* origin, |
| 95 const char* button1, | 94 const char* button1, |
| 96 const char* button2, | 95 const char* button2, |
| 97 bool require_interaction) { | 96 bool require_interaction) { |
| 98 message_center::RichNotificationData optional_fields; | 97 message_center::RichNotificationData optional_fields; |
| 99 optional_fields.context_message = base::UTF8ToUTF16(origin); | |
| 100 if (button1) { | 98 if (button1) { |
| 101 optional_fields.buttons.push_back( | 99 optional_fields.buttons.push_back( |
| 102 message_center::ButtonInfo(base::UTF8ToUTF16(button1))); | 100 message_center::ButtonInfo(base::UTF8ToUTF16(button1))); |
| 103 if (button2) { | 101 if (button2) { |
| 104 optional_fields.buttons.push_back( | 102 optional_fields.buttons.push_back( |
| 105 message_center::ButtonInfo(base::UTF8ToUTF16(button2))); | 103 message_center::ButtonInfo(base::UTF8ToUTF16(button2))); |
| 106 } | 104 } |
| 107 } | 105 } |
| 108 | 106 |
| 109 GURL url = GURL(origin); | 107 GURL url = GURL(origin); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 alert_dispatcher())); | 210 alert_dispatcher())); |
| 213 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | 211 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
| 214 "profile_id", false, *notification); | 212 "profile_id", false, *notification); |
| 215 NSArray* notifications = [notification_center() deliveredNotifications]; | 213 NSArray* notifications = [notification_center() deliveredNotifications]; |
| 216 | 214 |
| 217 EXPECT_EQ(1u, [notifications count]); | 215 EXPECT_EQ(1u, [notifications count]); |
| 218 | 216 |
| 219 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; | 217 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; |
| 220 EXPECT_NSEQ(@"Title", [delivered_notification title]); | 218 EXPECT_NSEQ(@"Title", [delivered_notification title]); |
| 221 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); | 219 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); |
| 222 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]); | 220 EXPECT_NSEQ(@"gmail.com", [delivered_notification subtitle]); |
| 223 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); | 221 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); |
| 224 EXPECT_NSEQ(@"Settings", [delivered_notification actionButtonTitle]); | 222 EXPECT_NSEQ(@"Settings", [delivered_notification actionButtonTitle]); |
| 225 } | 223 } |
| 226 | 224 |
| 227 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { | 225 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) { |
| 228 std::unique_ptr<Notification> notification = CreateBanner( | 226 std::unique_ptr<Notification> notification = CreateBanner( |
| 229 "Title", "Context", "https://gmail.com", "Button 1", nullptr); | 227 "Title", "Context", "https://gmail.com", "Button 1", nullptr); |
| 230 | 228 |
| 231 std::unique_ptr<NotificationPlatformBridgeMac> bridge( | 229 std::unique_ptr<NotificationPlatformBridgeMac> bridge( |
| 232 new NotificationPlatformBridgeMac(notification_center(), | 230 new NotificationPlatformBridgeMac(notification_center(), |
| 233 alert_dispatcher())); | 231 alert_dispatcher())); |
| 234 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", | 232 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", |
| 235 "profile_id", false, *notification); | 233 "profile_id", false, *notification); |
| 236 | 234 |
| 237 NSArray* notifications = [notification_center() deliveredNotifications]; | 235 NSArray* notifications = [notification_center() deliveredNotifications]; |
| 238 EXPECT_EQ(1u, [notifications count]); | 236 EXPECT_EQ(1u, [notifications count]); |
| 239 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; | 237 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; |
| 240 EXPECT_NSEQ(@"Title", [delivered_notification title]); | 238 EXPECT_NSEQ(@"Title", [delivered_notification title]); |
| 241 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); | 239 EXPECT_NSEQ(@"Context", [delivered_notification informativeText]); |
| 242 EXPECT_NSEQ(@"https://gmail.com", [delivered_notification subtitle]); | 240 EXPECT_NSEQ(@"gmail.com", [delivered_notification subtitle]); |
| 243 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); | 241 EXPECT_NSEQ(@"Close", [delivered_notification otherButtonTitle]); |
| 244 EXPECT_NSEQ(@"More", [delivered_notification actionButtonTitle]); | 242 EXPECT_NSEQ(@"More", [delivered_notification actionButtonTitle]); |
| 245 } | 243 } |
| 246 | 244 |
| 247 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayProgress) { | 245 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayProgress) { |
| 248 std::unique_ptr<Notification> notification = | 246 std::unique_ptr<Notification> notification = |
| 249 CreateBanner("Title", "Context", "https://gmail.com", nullptr, nullptr); | 247 CreateBanner("Title", "Context", "https://gmail.com", nullptr, nullptr); |
| 250 const int kSamplePercent = 10; | 248 const int kSamplePercent = 10; |
| 251 | 249 |
| 252 notification->set_progress(kSamplePercent); | 250 notification->set_progress(kSamplePercent); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 bridge->Display(NotificationCommon::PERSISTENT, "notification_id2", | 413 bridge->Display(NotificationCommon::PERSISTENT, "notification_id2", |
| 416 "profile_id", false, *alert); | 414 "profile_id", false, *alert); |
| 417 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); | 415 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); |
| 418 EXPECT_EQ(1u, [[alert_dispatcher() alerts] count]); | 416 EXPECT_EQ(1u, [[alert_dispatcher() alerts] count]); |
| 419 } | 417 } |
| 420 | 418 |
| 421 // The destructor of the bridge should close all notifications. | 419 // The destructor of the bridge should close all notifications. |
| 422 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); | 420 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); |
| 423 EXPECT_EQ(0u, [[alert_dispatcher() alerts] count]); | 421 EXPECT_EQ(0u, [[alert_dispatcher() alerts] count]); |
| 424 } | 422 } |
| 423 |
| 424 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayETLDPlusOne) { |
| 425 std::unique_ptr<Notification> notification = |
| 426 CreateBanner("Title", "Context", "https://hello.world.test.co.uk", |
| 427 "Button 1", nullptr); |
| 428 |
| 429 std::unique_ptr<NotificationPlatformBridgeMac> bridge( |
| 430 new NotificationPlatformBridgeMac(notification_center(), |
| 431 alert_dispatcher())); |
| 432 bridge->Display(NotificationCommon::PERSISTENT, "notification_id1", |
| 433 "profile_id", false, *notification); |
| 434 |
| 435 notification = CreateBanner("Title", "Context", "https://mail.appspot.com", |
| 436 "Button 1", nullptr); |
| 437 |
| 438 bridge->Display(NotificationCommon::PERSISTENT, "notification_id2", |
| 439 "profile_id", false, *notification); |
| 440 notification = CreateBanner("Title", "Context", "https://tests.peter.sh", |
| 441 "Button 1", nullptr); |
| 442 |
| 443 bridge->Display(NotificationCommon::PERSISTENT, "notification_id3", |
| 444 "profile_id", false, *notification); |
| 445 |
| 446 notification = CreateBanner("Title", "Context", "http://localhost:8080", |
| 447 "Button 1", nullptr); |
| 448 |
| 449 bridge->Display(NotificationCommon::PERSISTENT, "notification_id4", |
| 450 "profile_id", false, *notification); |
| 451 |
| 452 notification = CreateBanner("Title", "Context", "https://93.186.186.172", |
| 453 "Button 1", nullptr); |
| 454 |
| 455 bridge->Display(NotificationCommon::PERSISTENT, "notification_id5", |
| 456 "profile_id", false, *notification); |
| 457 |
| 458 NSArray* notifications = [notification_center() deliveredNotifications]; |
| 459 EXPECT_EQ(5u, [notifications count]); |
| 460 NSUserNotification* delivered_notification = [notifications objectAtIndex:0]; |
| 461 EXPECT_NSEQ(@"test.co.uk", [delivered_notification subtitle]); |
| 462 delivered_notification = [notifications objectAtIndex:1]; |
| 463 EXPECT_NSEQ(@"mail.appspot.com", [delivered_notification subtitle]); |
| 464 delivered_notification = [notifications objectAtIndex:2]; |
| 465 EXPECT_NSEQ(@"peter.sh", [delivered_notification subtitle]); |
| 466 delivered_notification = [notifications objectAtIndex:3]; |
| 467 EXPECT_NSEQ(@"localhost:8080", [delivered_notification subtitle]); |
| 468 delivered_notification = [notifications objectAtIndex:4]; |
| 469 EXPECT_NSEQ(@"93.186.186.172", [delivered_notification subtitle]); |
| 470 } |
| OLD | NEW |