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