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

Side by Side Diff: chrome/browser/ui/cocoa/notifications/notification_response_builder_mac_unittest.mm

Issue 2390153005: Implement support for closing mac native notifications (Closed)
Patch Set: review Created 4 years, 2 months 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 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 @interface TestBuilder : NSObject
Robert Sesek 2016/10/13 16:50:37 Remove this?
15 + (NotificationBuilder*)newTestBuilder;
16 @end
17
18 @implementation TestBuilder
19 + (NotificationBuilder*)newTestBuilder {
20 NotificationBuilder* builder =
21 [[NotificationBuilder alloc] initWithCloseLabel:@"Close"
22 optionsLabel:@"Options"
23 settingsLabel:@"Settings"];
24 [builder setTitle:@"Title"];
25 [builder setSubTitle:@"https://www.miguel.com"];
26 [builder setContextMessage:@""];
27 [builder setTag:@"tag1"];
28 [builder setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]];
29 [builder setNotificationId:@"notificationId"];
30 [builder setProfileId:@"profileId"];
31 [builder setIncognito:false];
32 [builder setNotificationType:@(NotificationCommon::PERSISTENT)];
33 return builder;
34 }
35 @end
36
37 class NotificationResponseBuilderMacTest : public testing::Test {
38 protected:
39 base::scoped_nsobject<NotificationBuilder> testBuilder();
Robert Sesek 2016/10/13 16:50:36 naming: TestBuilder() (or maybe even NewTestBuilde
Miguel Garcia 2016/10/14 10:59:06 Done.
40 };
41
42 base::scoped_nsobject<NotificationBuilder>
43 NotificationResponseBuilderMacTest::testBuilder() {
Robert Sesek 2016/10/13 16:50:37 I generally just inline these into the class decla
Miguel Garcia 2016/10/14 10:59:06 Done.
15 base::scoped_nsobject<NotificationBuilder> builder( 44 base::scoped_nsobject<NotificationBuilder> builder(
16 [[NotificationBuilder alloc] initWithCloseLabel:@"Close" 45 [[NotificationBuilder alloc] initWithCloseLabel:@"Close"
17 optionsLabel:@"Options" 46 optionsLabel:@"Options"
18 settingsLabel:@"Settings"]); 47 settingsLabel:@"Settings"]);
19 [builder setTitle:@"Title"]; 48 [builder setTitle:@"Title"];
20 [builder setSubTitle:@"https://www.miguel.com"]; 49 [builder setSubTitle:@"https://www.miguel.com"];
21 [builder setContextMessage:@""]; 50 [builder setContextMessage:@""];
22 [builder setTag:@"tag1"]; 51 [builder setTag:@"tag1"];
23 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; 52 [builder setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]];
24 [builder setNotificationId:@"notificationId"]; 53 [builder setNotificationId:@"notificationId"];
25 [builder setProfileId:@"profileId"]; 54 [builder setProfileId:@"profileId"];
26 [builder setIncognito:false]; 55 [builder setIncognito:false];
27 [builder 56 [builder setNotificationType:@(NotificationCommon::PERSISTENT)];
28 setNotificationType:[NSNumber 57 return builder;
29 numberWithInt:NotificationCommon::PERSISTENT]]; 58 }
30 59
60 TEST_F(NotificationResponseBuilderMacTest, TestNotificationClick) {
61 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
31 NSUserNotification* notification = [builder buildUserNotification]; 62 NSUserNotification* notification = [builder buildUserNotification];
63 // This will be set by the notification center to indicate the notification
64 // was clicked.
65 [notification setValue:@(NSUserNotificationActivationTypeContentsClicked)
66 forKey:@"_activationType"];
Robert Sesek 2016/10/13 16:50:36 We may want to pull this out into notification_con
Miguel Garcia 2016/10/14 10:59:06 Happy to consider it in another CL but keep in min
67
32 NSDictionary* response = 68 NSDictionary* response =
33 [NotificationResponseBuilder buildDictionary:notification]; 69 [NotificationResponseBuilder buildDictionary:notification];
34 70
35 NSNumber* operation = 71 NSNumber* operation =
36 [response objectForKey:notification_constants::kNotificationOperation]; 72 [response objectForKey:notification_constants::kNotificationOperation];
37 NSNumber* buttonIndex = 73 NSNumber* buttonIndex =
38 [response objectForKey:notification_constants::kNotificationButtonIndex]; 74 [response objectForKey:notification_constants::kNotificationButtonIndex];
39 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); 75 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
40 EXPECT_EQ(-1, buttonIndex.intValue); 76 EXPECT_EQ(-1, buttonIndex.intValue);
41 } 77 }
42 78
43 TEST(NotificationResponseBuilderMacTest, TestNotificationSettingsClick) { 79 TEST_F(NotificationResponseBuilderMacTest, TestNotificationSettingsClick) {
44 base::scoped_nsobject<NotificationBuilder> builder( 80 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
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]; 81 NSUserNotification* notification = [builder buildUserNotification];
61 82
62 // This will be set by the notification center to indicate the only available 83 // This will be set by the notification center to indicate the only available
63 // button was clicked. 84 // button was clicked.
64 [notification 85 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
65 setValue: 86 forKey:@"_activationType"];
66 [NSNumber
67 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked]
68 forKey:@"_activationType"];
69 NSDictionary* response = 87 NSDictionary* response =
70 [NotificationResponseBuilder buildDictionary:notification]; 88 [NotificationResponseBuilder buildDictionary:notification];
71 89
72 NSNumber* operation = 90 NSNumber* operation =
73 [response objectForKey:notification_constants::kNotificationOperation]; 91 [response objectForKey:notification_constants::kNotificationOperation];
74 NSNumber* buttonIndex = 92 NSNumber* buttonIndex =
75 [response objectForKey:notification_constants::kNotificationButtonIndex]; 93 [response objectForKey:notification_constants::kNotificationButtonIndex];
76 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); 94 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue);
77 EXPECT_EQ(-1, buttonIndex.intValue); 95 EXPECT_EQ(-1, buttonIndex.intValue);
78 } 96 }
79 97
80 TEST(NotificationResponseBuilderMacTest, TestNotificationOneActionClick) { 98 TEST_F(NotificationResponseBuilderMacTest, TestNotificationOneActionClick) {
81 base::scoped_nsobject<NotificationBuilder> builder( 99 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
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:@""]; 100 [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 101
98 NSUserNotification* notification = [builder buildUserNotification]; 102 NSUserNotification* notification = [builder buildUserNotification];
99 103
100 // These values will be set by the notification center to indicate that button 104 // These values will be set by the notification center to indicate that button
101 // 1 was clicked. 105 // 1 was clicked.
102 [notification 106 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
103 setValue: 107 forKey:@"_activationType"];
104 [NSNumber
105 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked]
106 forKey:@"_activationType"];
107 [notification setValue:[NSNumber numberWithInt:0] 108 [notification setValue:[NSNumber numberWithInt:0]
108 forKey:@"_alternateActionIndex"]; 109 forKey:@"_alternateActionIndex"];
109 NSDictionary* response = 110 NSDictionary* response =
110 [NotificationResponseBuilder buildDictionary:notification]; 111 [NotificationResponseBuilder buildDictionary:notification];
111 112
112 NSNumber* operation = 113 NSNumber* operation =
113 [response objectForKey:notification_constants::kNotificationOperation]; 114 [response objectForKey:notification_constants::kNotificationOperation];
114 NSNumber* buttonIndex = 115 NSNumber* buttonIndex =
115 [response objectForKey:notification_constants::kNotificationButtonIndex]; 116 [response objectForKey:notification_constants::kNotificationButtonIndex];
116 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); 117 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
117 EXPECT_EQ(0, buttonIndex.intValue); 118 EXPECT_EQ(0, buttonIndex.intValue);
118 } 119 }
119 120
120 TEST(NotificationResponseBuilderMacTest, TestNotificationTwoActionClick) { 121 TEST_F(NotificationResponseBuilderMacTest, TestNotificationTwoActionClick) {
121 base::scoped_nsobject<NotificationBuilder> builder( 122 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
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"]; 123 [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 124
138 NSUserNotification* notification = [builder buildUserNotification]; 125 NSUserNotification* notification = [builder buildUserNotification];
139 126
140 // These values will be set by the notification center to indicate that button 127 // These values will be set by the notification center to indicate that button
141 // 2 was clicked. 128 // 2 was clicked.
142 [notification 129 [notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
143 setValue: 130 forKey:@"_activationType"];
144 [NSNumber
145 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked]
146 forKey:@"_activationType"];
147 [notification setValue:[NSNumber numberWithInt:1] 131 [notification setValue:[NSNumber numberWithInt:1]
148 forKey:@"_alternateActionIndex"]; 132 forKey:@"_alternateActionIndex"];
149 133
150 NSDictionary* response = 134 NSDictionary* response =
151 [NotificationResponseBuilder buildDictionary:notification]; 135 [NotificationResponseBuilder buildDictionary:notification];
152 136
153 NSNumber* operation = 137 NSNumber* operation =
154 [response objectForKey:notification_constants::kNotificationOperation]; 138 [response objectForKey:notification_constants::kNotificationOperation];
155 NSNumber* buttonIndex = 139 NSNumber* buttonIndex =
156 [response objectForKey:notification_constants::kNotificationButtonIndex]; 140 [response objectForKey:notification_constants::kNotificationButtonIndex];
157 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue); 141 EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
158 EXPECT_EQ(1, buttonIndex.intValue); 142 EXPECT_EQ(1, buttonIndex.intValue);
159 } 143 }
160 144
161 TEST(NotificationResponseBuilderMacTest, 145 TEST_F(NotificationResponseBuilderMacTest,
162 TestNotificationTwoActionSettingsClick) { 146 TestNotificationTwoActionSettingsClick) {
163 base::scoped_nsobject<NotificationBuilder> builder( 147 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
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"]; 148 [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]; 149 NSUserNotification* notification = [builder buildUserNotification];
181 150
182 // These values will be set by the notification center to indicate that button 151 // These values will be set by the notification center to indicate that button
183 // 2 was clicked. 152 // 2 was clicked.
184 [notification 153 [notification
185 setValue: 154 setValue:
186 [NSNumber 155 [NSNumber
187 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked] 156 numberWithInt:NSUserNotificationActivationTypeActionButtonClicked]
188 forKey:@"_activationType"]; 157 forKey:@"_activationType"];
189 [notification setValue:[NSNumber numberWithInt:2] 158 [notification setValue:[NSNumber numberWithInt:2]
190 forKey:@"_alternateActionIndex"]; 159 forKey:@"_alternateActionIndex"];
191 160
192 NSDictionary* response = 161 NSDictionary* response =
193 [NotificationResponseBuilder buildDictionary:notification]; 162 [NotificationResponseBuilder buildDictionary:notification];
194 163
195 NSNumber* operation = 164 NSNumber* operation =
196 [response objectForKey:notification_constants::kNotificationOperation]; 165 [response objectForKey:notification_constants::kNotificationOperation];
197 NSNumber* buttonIndex = 166 NSNumber* buttonIndex =
198 [response objectForKey:notification_constants::kNotificationButtonIndex]; 167 [response objectForKey:notification_constants::kNotificationButtonIndex];
199 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue); 168 EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue);
200 EXPECT_EQ(-1, buttonIndex.intValue); 169 EXPECT_EQ(-1, buttonIndex.intValue);
201 } 170 }
171
172 TEST_F(NotificationResponseBuilderMacTest, TestNotificationClose) {
173 base::scoped_nsobject<NotificationBuilder> builder = testBuilder();
174 NSUserNotification* notification = [builder buildUserNotification];
175
176 // None is what the NSUserNotification center emits when closing since it
177 // interprets it as not activated.
178 [notification setValue:@(NSUserNotificationActivationTypeNone)
179 forKey:@"_activationType"];
180
181 NSDictionary* response =
182 [NotificationResponseBuilder buildDictionary:notification];
183
184 NSNumber* operation =
185 [response objectForKey:notification_constants::kNotificationOperation];
186 NSNumber* buttonIndex =
187 [response objectForKey:notification_constants::kNotificationButtonIndex];
188 EXPECT_EQ(1 /* NOTIFICATION_CLOSE */, operation.intValue);
189 EXPECT_EQ(-1, buttonIndex.intValue);
190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698