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

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm

Issue 2138873002: Add tests for the Display operation of the mac notification bridge. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: format 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
« no previous file with comments | « chrome/browser/DEPS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #import <objc/runtime.h>
6 7
7 #include "base/mac/scoped_nsobject.h" 8 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/notifications/notification_common.h" 9 #import "base/mac/scoped_objc_class_swizzler.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/notifications/notification.h"
9 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" 12 #include "chrome/browser/notifications/notification_platform_bridge_mac.h"
13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" 15 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h"
11 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" 16 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
12 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" 17 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h"
13 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gtest_mac.h"
20 #include "url/gurl.h"
14 21
15 namespace { 22 class NotificationPlatformBridgeMacTest : public CocoaTest {
23 protected:
24 std::unique_ptr<Notification> CreateNotification(const char* title,
25 const char* subtitle,
26 const char* origin,
27 const char* button1,
28 const char* button2) {
29 message_center::RichNotificationData optional_fields;
30 optional_fields.context_message = base::UTF8ToUTF16(origin);
31 if (button1) {
32 optional_fields.buttons.push_back(
33 message_center::ButtonInfo(base::UTF8ToUTF16(button1)));
34 if (button2) {
35 optional_fields.buttons.push_back(
36 message_center::ButtonInfo(base::UTF8ToUTF16(button2)));
37 }
38 }
16 39
17 NSMutableDictionary* BuildDefaultNotificationResponse() { 40 GURL url = GURL(origin);
18 base::scoped_nsobject<NotificationBuilder> builder(
19 [[NotificationBuilder alloc] initWithCloseLabel:@"Close"
20 optionsLabel:@"Options"
21 settingsLabel:@"Settings"]);
22 [builder setTitle:@"Title"];
23 [builder setSubTitle:@"https://www.miguel.com"];
24 [builder setOrigin:@"https://www.miguel.com/"];
25 [builder setContextMessage:@""];
26 [builder setButtons:@"Button1" secondaryButton:@"Button2"];
27 [builder setTag:@"tag1"];
28 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]];
29 [builder setNotificationId:@"notificationId"];
30 [builder setProfileId:@"profileId"];
31 [builder setIncognito:false];
32 [builder
33 setNotificationType:[NSNumber
34 numberWithInt:NotificationCommon::PERSISTENT]];
35 41
36 NSUserNotification* notification = [builder buildUserNotification]; 42 std::unique_ptr<Notification> notification(new Notification(
37 return [NSMutableDictionary 43 message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title),
38 dictionaryWithDictionary:[NotificationResponseBuilder 44 base::UTF8ToUTF16(subtitle), gfx::Image(),
39 buildDictionary:notification]]; 45 message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"),
40 } 46 url, "id1", optional_fields, new MockNotificationDelegate("id1")));
41 47
42 } // namespace 48 return notification;
49 }
43 50
44 TEST(NotificationPlatformBridgeMacTest, TestNotificationValidResponse) { 51 NSMutableDictionary* BuildDefaultNotificationResponse() {
52 base::scoped_nsobject<NotificationBuilder> builder(
53 [[NotificationBuilder alloc] initWithCloseLabel:@"Close"
54 optionsLabel:@"Options"
55 settingsLabel:@"Settings"]);
56 [builder setTitle:@"Title"];
57 [builder setSubTitle:@"https://www.miguel.com"];
58 [builder setOrigin:@"https://www.miguel.com/"];
59 [builder setContextMessage:@""];
60 [builder setButtons:@"Button1" secondaryButton:@"Button2"];
61 [builder setTag:@"tag1"];
62 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]];
63 [builder setNotificationId:@"notificationId"];
64 [builder setProfileId:@"profileId"];
65 [builder setIncognito:false];
66 [builder
67 setNotificationType:[NSNumber
68 numberWithInt:NotificationCommon::PERSISTENT]];
69
70 NSUserNotification* notification = [builder buildUserNotification];
71 return [NSMutableDictionary
72 dictionaryWithDictionary:[NotificationResponseBuilder
73 buildDictionary:notification]];
74 }
75 };
76
77 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) {
45 NSDictionary* response = BuildDefaultNotificationResponse(); 78 NSDictionary* response = BuildDefaultNotificationResponse();
46 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 79 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
47 } 80 }
48 81
49 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { 82 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) {
50 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 83 NSMutableDictionary* response = BuildDefaultNotificationResponse();
51 [response setValue:[NSNumber numberWithInt:210581] 84 [response setValue:[NSNumber numberWithInt:210581]
52 forKey:notification_constants::kNotificationType]; 85 forKey:notification_constants::kNotificationType];
53 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 86 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
54 } 87 }
55 88
56 TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownOperation) { 89 TEST_F(NotificationPlatformBridgeMacTest,
90 TestNotificationVerifyUnknownOperation) {
57 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 91 NSMutableDictionary* response = BuildDefaultNotificationResponse();
58 [response setValue:[NSNumber numberWithInt:40782] 92 [response setValue:[NSNumber numberWithInt:40782]
59 forKey:notification_constants::kNotificationOperation]; 93 forKey:notification_constants::kNotificationOperation];
60 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 94 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
61 } 95 }
62 96
63 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingOperation) { 97 TEST_F(NotificationPlatformBridgeMacTest,
98 TestNotificationVerifyMissingOperation) {
64 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 99 NSMutableDictionary* response = BuildDefaultNotificationResponse();
65 [response removeObjectForKey:notification_constants::kNotificationOperation]; 100 [response removeObjectForKey:notification_constants::kNotificationOperation];
66 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 101 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
67 } 102 }
68 103
69 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoProfileId) { 104 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyNoProfileId) {
70 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 105 NSMutableDictionary* response = BuildDefaultNotificationResponse();
71 [response removeObjectForKey:notification_constants::kNotificationProfileId]; 106 [response removeObjectForKey:notification_constants::kNotificationProfileId];
72 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 107 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
73 } 108 }
74 109
75 TEST(NotificationPlatformBridgeMacTest, TestNotificationNoNotificationId) { 110 TEST_F(NotificationPlatformBridgeMacTest,
111 TestNotificationVerifyNoNotificationId) {
76 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 112 NSMutableDictionary* response = BuildDefaultNotificationResponse();
77 [response setValue:@"" forKey:notification_constants::kNotificationId]; 113 [response setValue:@"" forKey:notification_constants::kNotificationId];
78 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 114 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
79 } 115 }
80 116
81 TEST(NotificationPlatformBridgeMacTest, TestNotificationInvalidButton) { 117 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyInvalidButton) {
82 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 118 NSMutableDictionary* response = BuildDefaultNotificationResponse();
83 [response setValue:[NSNumber numberWithInt:-5] 119 [response setValue:[NSNumber numberWithInt:-5]
84 forKey:notification_constants::kNotificationButtonIndex]; 120 forKey:notification_constants::kNotificationButtonIndex];
85 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 121 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
86 } 122 }
87 123
88 TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingButtonIndex) { 124 TEST_F(NotificationPlatformBridgeMacTest,
125 TestNotificationVerifyMissingButtonIndex) {
89 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 126 NSMutableDictionary* response = BuildDefaultNotificationResponse();
90 [response 127 [response
91 removeObjectForKey:notification_constants::kNotificationButtonIndex]; 128 removeObjectForKey:notification_constants::kNotificationButtonIndex];
92 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 129 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
93 } 130 }
94 131
95 TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) { 132 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyOrigin) {
96 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 133 NSMutableDictionary* response = BuildDefaultNotificationResponse();
97 [response setValue:@"invalidorigin" 134 [response setValue:@"invalidorigin"
98 forKey:notification_constants::kNotificationOrigin]; 135 forKey:notification_constants::kNotificationOrigin];
99 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 136 EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
100 137
101 // If however the origin is not present the response should be fine. 138 // If however the origin is not present the response should be fine.
102 [response removeObjectForKey:notification_constants::kNotificationOrigin]; 139 [response removeObjectForKey:notification_constants::kNotificationOrigin];
103 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 140 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
104 } 141 }
142
143 // The usual [NSUSerNotificationCenter defaultNotificationCenter] constructor
144 // is not available in tests. The private constructor fortunatelly is.
145 @interface NSUserNotificationCenter (PrivateAPI)
146 + (NSUserNotificationCenter*)_centerForIdentifier:(NSString*)ident
147 type:(NSUInteger)type;
148 @end
149
150 // Category to extend the notification center with different implementations
151 // of the deliverNotification selector which can be swizzled as part of the
152 // test.
153 // TODO(miguelg) replace this with OCMock once a version with support
154 // for dynamic properties is rolled out (crbug.com/622753).
155 @interface NSUserNotificationCenter (TestAdditions)
156 - (void)expectationsNoButtons:(NSUserNotification*)notification;
157 - (void)expectationsOneButton:(NSUserNotification*)notification;
158 @end
159
160 @implementation NSUserNotificationCenter (TestAdditions)
161
162 // Expectations for notifications with no buttons.
163 - (void)expectationsNoButtons:(NSUserNotification*)notification {
164 EXPECT_NSEQ(@"Title", [notification title]);
165 EXPECT_NSEQ(@"Context", [notification informativeText]);
166 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]);
167 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]);
168 EXPECT_NSEQ(@"Settings", [notification actionButtonTitle]);
169 }
170
171 // Expectations for notifications with one button.
172 - (void)expectationsOneButton:(NSUserNotification*)notification {
173 EXPECT_NSEQ(@"Title", [notification title]);
174 EXPECT_NSEQ(@"Context", [notification informativeText]);
175 EXPECT_NSEQ(@"https://gmail.com", [notification subtitle]);
176 EXPECT_NSEQ(@"Close", [notification otherButtonTitle]);
177 EXPECT_NSEQ(@"Options", [notification actionButtonTitle]);
178 }
179
180 @end
181
182 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) {
183 base::scoped_nsobject<NSUserNotificationCenter> notification_center(
184 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
185 base::mac::ScopedObjCClassSwizzler swizzler(
186 [notification_center class], @selector(deliverNotification:),
187 @selector(expectationsNoButtons:));
188
189 std::unique_ptr<Notification> notification = CreateNotification(
190 "Title", "Context", "https://gmail.com", nullptr, nullptr);
191 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
192 new NotificationPlatformBridgeMac(notification_center));
193 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
194 "profile_id", false, *notification);
195 }
196
197 TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) {
198 std::unique_ptr<Notification> notification = CreateNotification(
199 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
200 base::scoped_nsobject<NSUserNotificationCenter> notification_center(
201 [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
202 base::mac::ScopedObjCClassSwizzler swizzler(
203 [notification_center class], @selector(deliverNotification:),
204 @selector(expectationsOneButton:));
205 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
206 new NotificationPlatformBridgeMac(notification_center));
207 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
208 "profile_id", false, *notification);
209 }
OLDNEW
« no previous file with comments | « chrome/browser/DEPS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698