| 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 | 6 |
| 7 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "chrome/browser/notifications/notification_common.h" | 8 #include "chrome/browser/notifications/notification_common.h" |
| 9 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 9 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 10 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | 10 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 11 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma
c.h" | 11 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma
c.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 class NotificationResponseBuilderMacTest : public testing::Test { | 14 TEST(NotificationResponseBuilderMacTest, TestNotificationClick) { |
| 15 protected: | 15 base::scoped_nsobject<NotificationBuilder> builder( |
| 16 base::scoped_nsobject<NotificationBuilder> NewTestBuilder() { | 16 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 17 base::scoped_nsobject<NotificationBuilder> builder( | 17 optionsLabel:@"Options" |
| 18 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" | 18 settingsLabel:@"Settings"]); |
| 19 optionsLabel:@"Options" | 19 [builder setTitle:@"Title"]; |
| 20 settingsLabel:@"Settings"]); | 20 [builder setSubTitle:@"https://www.miguel.com"]; |
| 21 [builder setTitle:@"Title"]; | 21 [builder setContextMessage:@""]; |
| 22 [builder setSubTitle:@"https://www.miguel.com"]; | 22 [builder setTag:@"tag1"]; |
| 23 [builder setContextMessage:@""]; | 23 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 24 [builder setTag:@"tag1"]; | 24 [builder setNotificationId:@"notificationId"]; |
| 25 [builder setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]]; | 25 [builder setProfileId:@"profileId"]; |
| 26 [builder setNotificationId:@"notificationId"]; | 26 [builder setIncognito:false]; |
| 27 [builder setProfileId:@"profileId"]; | 27 [builder |
| 28 [builder setIncognito:false]; | 28 setNotificationType:[NSNumber |
| 29 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; | 29 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 30 return builder; | |
| 31 } | |
| 32 }; | |
| 33 | 30 |
| 34 TEST_F(NotificationResponseBuilderMacTest, TestNotificationClick) { | |
| 35 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | |
| 36 NSUserNotification* notification = [builder buildUserNotification]; | 31 NSUserNotification* notification = [builder buildUserNotification]; |
| 37 // This will be set by the notification center to indicate the notification | |
| 38 // was clicked. | |
| 39 [notification setValue:@(NSUserNotificationActivationTypeContentsClicked) | |
| 40 forKey:@"_activationType"]; | |
| 41 | |
| 42 NSDictionary* response = | 32 NSDictionary* response = |
| 43 [NotificationResponseBuilder buildDictionary:notification]; | 33 [NotificationResponseBuilder buildDictionary:notification]; |
| 44 | 34 |
| 45 NSNumber* operation = | 35 NSNumber* operation = |
| 46 [response objectForKey:notification_constants::kNotificationOperation]; | 36 [response objectForKey:notification_constants::kNotificationOperation]; |
| 47 NSNumber* buttonIndex = | 37 NSNumber* buttonIndex = |
| 48 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 38 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 49 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); | 39 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); |
| 50 EXPECT_EQ(-1, buttonIndex.intValue); | 40 EXPECT_EQ(-1, buttonIndex.intValue); |
| 51 } | 41 } |
| 52 | 42 |
| 53 TEST_F(NotificationResponseBuilderMacTest, TestNotificationSettingsClick) { | 43 TEST(NotificationResponseBuilderMacTest, TestNotificationSettingsClick) { |
| 54 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | 44 base::scoped_nsobject<NotificationBuilder> builder( |
| 45 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 46 optionsLabel:@"Options" |
| 47 settingsLabel:@"Settings"]); |
| 48 [builder setTitle:@"Title"]; |
| 49 [builder setSubTitle:@"https://www.miguel.com"]; |
| 50 [builder setContextMessage:@""]; |
| 51 [builder setTag:@"tag1"]; |
| 52 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 53 [builder setNotificationId:@"notificationId"]; |
| 54 [builder setProfileId:@"profileId"]; |
| 55 [builder setIncognito:false]; |
| 56 [builder |
| 57 setNotificationType:[NSNumber |
| 58 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 59 |
| 55 NSUserNotification* notification = [builder buildUserNotification]; | 60 NSUserNotification* notification = [builder buildUserNotification]; |
| 56 | 61 |
| 57 // This will be set by the notification center to indicate the only available | 62 // This will be set by the notification center to indicate the only available |
| 58 // button was clicked. | 63 // button was clicked. |
| 59 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked) | 64 [notification |
| 60 forKey:@"_activationType"]; | 65 setValue: |
| 66 [NSNumber |
| 67 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] |
| 68 forKey:@"_activationType"]; |
| 61 NSDictionary* response = | 69 NSDictionary* response = |
| 62 [NotificationResponseBuilder buildDictionary:notification]; | 70 [NotificationResponseBuilder buildDictionary:notification]; |
| 63 | 71 |
| 64 NSNumber* operation = | 72 NSNumber* operation = |
| 65 [response objectForKey:notification_constants::kNotificationOperation]; | 73 [response objectForKey:notification_constants::kNotificationOperation]; |
| 66 NSNumber* buttonIndex = | 74 NSNumber* buttonIndex = |
| 67 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 75 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 68 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); | 76 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); |
| 69 EXPECT_EQ(-1, buttonIndex.intValue); | 77 EXPECT_EQ(-1, buttonIndex.intValue); |
| 70 } | 78 } |
| 71 | 79 |
| 72 TEST_F(NotificationResponseBuilderMacTest, TestNotificationOneActionClick) { | 80 TEST(NotificationResponseBuilderMacTest, TestNotificationOneActionClick) { |
| 73 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | 81 base::scoped_nsobject<NotificationBuilder> builder( |
| 82 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 83 optionsLabel:@"Options" |
| 84 settingsLabel:@"Settings"]); |
| 85 [builder setTitle:@"Title"]; |
| 86 [builder setSubTitle:@"https://www.miguel.com"]; |
| 87 [builder setContextMessage:@""]; |
| 74 [builder setButtons:@"Button1" secondaryButton:@""]; | 88 [builder setButtons:@"Button1" secondaryButton:@""]; |
| 89 [builder setTag:@"tag1"]; |
| 90 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 91 [builder setNotificationId:@"notificationId"]; |
| 92 [builder setProfileId:@"profileId"]; |
| 93 [builder setIncognito:false]; |
| 94 [builder |
| 95 setNotificationType:[NSNumber |
| 96 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 75 | 97 |
| 76 NSUserNotification* notification = [builder buildUserNotification]; | 98 NSUserNotification* notification = [builder buildUserNotification]; |
| 77 | 99 |
| 78 // These values will be set by the notification center to indicate that button | 100 // These values will be set by the notification center to indicate that button |
| 79 // 1 was clicked. | 101 // 1 was clicked. |
| 80 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked) | 102 [notification |
| 81 forKey:@"_activationType"]; | 103 setValue: |
| 104 [NSNumber |
| 105 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] |
| 106 forKey:@"_activationType"]; |
| 82 [notification setValue:[NSNumber numberWithInt:0] | 107 [notification setValue:[NSNumber numberWithInt:0] |
| 83 forKey:@"_alternateActionIndex"]; | 108 forKey:@"_alternateActionIndex"]; |
| 84 NSDictionary* response = | 109 NSDictionary* response = |
| 85 [NotificationResponseBuilder buildDictionary:notification]; | 110 [NotificationResponseBuilder buildDictionary:notification]; |
| 86 | 111 |
| 87 NSNumber* operation = | 112 NSNumber* operation = |
| 88 [response objectForKey:notification_constants::kNotificationOperation]; | 113 [response objectForKey:notification_constants::kNotificationOperation]; |
| 89 NSNumber* buttonIndex = | 114 NSNumber* buttonIndex = |
| 90 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 115 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 91 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); | 116 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); |
| 92 EXPECT_EQ(0, buttonIndex.intValue); | 117 EXPECT_EQ(0, buttonIndex.intValue); |
| 93 } | 118 } |
| 94 | 119 |
| 95 TEST_F(NotificationResponseBuilderMacTest, TestNotificationTwoActionClick) { | 120 TEST(NotificationResponseBuilderMacTest, TestNotificationTwoActionClick) { |
| 96 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | 121 base::scoped_nsobject<NotificationBuilder> builder( |
| 122 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 123 optionsLabel:@"Options" |
| 124 settingsLabel:@"Settings"]); |
| 125 [builder setTitle:@"Title"]; |
| 126 [builder setSubTitle:@"https://www.miguel.com"]; |
| 127 [builder setContextMessage:@""]; |
| 97 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | 128 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 129 [builder setTag:@"tag1"]; |
| 130 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 131 [builder setNotificationId:@"notificationId"]; |
| 132 [builder setProfileId:@"profileId"]; |
| 133 [builder setIncognito:false]; |
| 134 [builder |
| 135 setNotificationType:[NSNumber |
| 136 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 98 | 137 |
| 99 NSUserNotification* notification = [builder buildUserNotification]; | 138 NSUserNotification* notification = [builder buildUserNotification]; |
| 100 | 139 |
| 101 // These values will be set by the notification center to indicate that button | 140 // These values will be set by the notification center to indicate that button |
| 102 // 2 was clicked. | 141 // 2 was clicked. |
| 103 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked) | 142 [notification |
| 104 forKey:@"_activationType"]; | 143 setValue: |
| 144 [NSNumber |
| 145 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] |
| 146 forKey:@"_activationType"]; |
| 105 [notification setValue:[NSNumber numberWithInt:1] | 147 [notification setValue:[NSNumber numberWithInt:1] |
| 106 forKey:@"_alternateActionIndex"]; | 148 forKey:@"_alternateActionIndex"]; |
| 107 | 149 |
| 108 NSDictionary* response = | 150 NSDictionary* response = |
| 109 [NotificationResponseBuilder buildDictionary:notification]; | 151 [NotificationResponseBuilder buildDictionary:notification]; |
| 110 | 152 |
| 111 NSNumber* operation = | 153 NSNumber* operation = |
| 112 [response objectForKey:notification_constants::kNotificationOperation]; | 154 [response objectForKey:notification_constants::kNotificationOperation]; |
| 113 NSNumber* buttonIndex = | 155 NSNumber* buttonIndex = |
| 114 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 156 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 115 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); | 157 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); |
| 116 EXPECT_EQ(1, buttonIndex.intValue); | 158 EXPECT_EQ(1, buttonIndex.intValue); |
| 117 } | 159 } |
| 118 | 160 |
| 119 TEST_F(NotificationResponseBuilderMacTest, | 161 TEST(NotificationResponseBuilderMacTest, |
| 120 TestNotificationTwoActionSettingsClick) { | 162 TestNotificationTwoActionSettingsClick) { |
| 121 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | 163 base::scoped_nsobject<NotificationBuilder> builder( |
| 164 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" |
| 165 optionsLabel:@"Options" |
| 166 settingsLabel:@"Settings"]); |
| 167 [builder setTitle:@"Title"]; |
| 168 [builder setSubTitle:@"https://www.miguel.com"]; |
| 169 [builder setContextMessage:@""]; |
| 122 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; | 170 [builder setButtons:@"Button1" secondaryButton:@"Button2"]; |
| 171 [builder setTag:@"tag1"]; |
| 172 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; |
| 173 [builder setNotificationId:@"notificationId"]; |
| 174 [builder setProfileId:@"profileId"]; |
| 175 [builder setIncognito:false]; |
| 176 [builder |
| 177 setNotificationType:[NSNumber |
| 178 numberWithInt:NotificationCommon::PERSISTENT]]; |
| 179 |
| 123 NSUserNotification* notification = [builder buildUserNotification]; | 180 NSUserNotification* notification = [builder buildUserNotification]; |
| 124 | 181 |
| 125 // These values will be set by the notification center to indicate that button | 182 // These values will be set by the notification center to indicate that button |
| 126 // 2 was clicked. | 183 // 2 was clicked. |
| 127 [notification | 184 [notification |
| 128 setValue: | 185 setValue: |
| 129 [NSNumber | 186 [NSNumber |
| 130 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] | 187 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] |
| 131 forKey:@"_activationType"]; | 188 forKey:@"_activationType"]; |
| 132 [notification setValue:[NSNumber numberWithInt:2] | 189 [notification setValue:[NSNumber numberWithInt:2] |
| 133 forKey:@"_alternateActionIndex"]; | 190 forKey:@"_alternateActionIndex"]; |
| 134 | 191 |
| 135 NSDictionary* response = | 192 NSDictionary* response = |
| 136 [NotificationResponseBuilder buildDictionary:notification]; | 193 [NotificationResponseBuilder buildDictionary:notification]; |
| 137 | 194 |
| 138 NSNumber* operation = | 195 NSNumber* operation = |
| 139 [response objectForKey:notification_constants::kNotificationOperation]; | 196 [response objectForKey:notification_constants::kNotificationOperation]; |
| 140 NSNumber* buttonIndex = | 197 NSNumber* buttonIndex = |
| 141 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 198 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 142 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); | 199 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); |
| 143 EXPECT_EQ(-1, buttonIndex.intValue); | 200 EXPECT_EQ(-1, buttonIndex.intValue); |
| 144 } | 201 } |
| 145 | |
| 146 TEST_F(NotificationResponseBuilderMacTest, TestNotificationClose) { | |
| 147 base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder(); | |
| 148 NSUserNotification* notification = [builder buildUserNotification]; | |
| 149 | |
| 150 // None is what the NSUserNotification center emits when closing since it | |
| 151 // interprets it as not activated. | |
| 152 [notification setValue:@(NSUserNotificationActivationTypeNone) | |
| 153 forKey:@"_activationType"]; | |
| 154 | |
| 155 NSDictionary* response = | |
| 156 [NotificationResponseBuilder buildDictionary:notification]; | |
| 157 | |
| 158 NSNumber* operation = | |
| 159 [response objectForKey:notification_constants::kNotificationOperation]; | |
| 160 NSNumber* buttonIndex = | |
| 161 [response objectForKey:notification_constants::kNotificationButtonIndex]; | |
| 162 EXPECT_EQ(1 /* NOTIFICATION_CLOSE */, operation.intValue); | |
| 163 EXPECT_EQ(-1, buttonIndex.intValue); | |
| 164 } | |
| OLD | NEW |