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

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

Powered by Google App Engine
This is Rietveld 408576698