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

Unified 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: scoped swizzle 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/DEPS ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm
diff --git a/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm b/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm
index 32bcb93e7149b5cdcf86a0bd0e91c21656ed3ec4..c1a42c8e70c981f4c09fb57b45febf97ee9dc75c 100644
--- a/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm
+++ b/chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm
@@ -1,16 +1,21 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-
-#import <AppKit/AppKit.h>
+#import <objc/runtime.h>
Robert Sesek 2016/10/13 16:47:32 nit: blank line before, and keep AppKit.h
#include "base/mac/scoped_nsobject.h"
-#include "chrome/browser/notifications/notification_common.h"
+#import "base/mac/scoped_objc_class_swizzler.h"
+#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_platform_bridge_mac.h"
+#include "chrome/browser/notifications/notification_test_util.h"
+#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h"
#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
#include "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/ocmock/gtest_support.h"
Robert Sesek 2016/10/13 16:47:32 Remove for now.
Miguel Garcia 2016/10/14 10:58:13 Done.
+#include "url/gurl.h"
namespace {
@@ -41,58 +46,93 @@ NSMutableDictionary* BuildDefaultNotificationResponse() {
} // namespace
-TEST(NotificationPlatformBridgeMacTest, TestNotificationValidResponse) {
+class NotificationPlatformBridgeMacTest : public CocoaTest {
+ protected:
+ std::unique_ptr<Notification> CreateNotification(const char* title,
+ const char* subtitle,
+ const char* origin,
+ const char* button1,
+ const char* button2) {
+ message_center::RichNotificationData optional_fields;
+ optional_fields.context_message = base::UTF8ToUTF16(origin);
+ if (strlen(button1)) {
Robert Sesek 2016/10/13 16:47:32 Why not just use nullptrs instead of strlen?
Miguel Garcia 2016/10/14 10:58:13 Changed to nullptrs, I kind of prefer empty string
+ optional_fields.buttons.push_back(
+ message_center::ButtonInfo(base::UTF8ToUTF16(button1)));
+ if (strlen(button2)) {
+ optional_fields.buttons.push_back(
+ message_center::ButtonInfo(base::UTF8ToUTF16(button2)));
+ }
+ }
+
+ NotificationDelegate* delegate(new MockNotificationDelegate("id1"));
Robert Sesek 2016/10/13 16:47:32 Who owns this? Should this be a scoped_refptr<>?
Miguel Garcia 2016/10/14 10:58:13 The notification object owns it though a scoped_re
Robert Sesek 2016/10/14 14:21:26 I think it should just be wrapped in a scoped_refp
+ GURL url = GURL(origin);
+
+ std::unique_ptr<Notification> notification(new Notification(
+ message_center::NOTIFICATION_TYPE_SIMPLE, base::UTF8ToUTF16(title),
+ base::UTF8ToUTF16(subtitle), gfx::Image(),
+ message_center::NotifierId(url), base::UTF8ToUTF16("Notifier's Name"),
+ url, "id1", optional_fields, delegate));
+
+ return notification;
+ }
+};
+
+TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) {
NSDictionary* response = BuildDefaultNotificationResponse();
EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) {
+TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response setValue:[NSNumber numberWithInt:210581]
forKey:notification_constants::kNotificationType];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationUnknownOperation) {
+TEST_F(NotificationPlatformBridgeMacTest,
+ TestNotificationVerifyUnknownOperation) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response setValue:[NSNumber numberWithInt:40782]
forKey:notification_constants::kNotificationOperation];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingOperation) {
+TEST_F(NotificationPlatformBridgeMacTest,
+ TestNotificationVerifyMissingOperation) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response removeObjectForKey:notification_constants::kNotificationOperation];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationNoProfileId) {
+TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyNoProfileId) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response removeObjectForKey:notification_constants::kNotificationProfileId];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationNoNotificationId) {
+TEST_F(NotificationPlatformBridgeMacTest,
+ TestNotificationVerifyNoNotificationId) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response setValue:@"" forKey:notification_constants::kNotificationId];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationInvalidButton) {
+TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyInvalidButton) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response setValue:[NSNumber numberWithInt:-5]
forKey:notification_constants::kNotificationButtonIndex];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationMissingButtonIndex) {
+TEST_F(NotificationPlatformBridgeMacTest,
+ TestNotificationVerifyMissingButtonIndex) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response
removeObjectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_FALSE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
-TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) {
+TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyOrigin) {
NSMutableDictionary* response = BuildDefaultNotificationResponse();
[response setValue:@"invalidorigin"
forKey:notification_constants::kNotificationOrigin];
@@ -102,3 +142,71 @@ TEST(NotificationPlatformBridgeMacTest, TestNotificationOrigin) {
[response removeObjectForKey:notification_constants::kNotificationOrigin];
EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
}
+
+// The usual [NSUSerNotificationCenter defaultNotificationCenter] constructor
+// is not available in tests. The private constructor fortunatelly is.
+@interface NSUserNotificationCenter (PrivateAPI)
++ (NSUserNotificationCenter*)_centerForIdentifier:(NSString*)ident
+ type:(NSUInteger)type;
+@end
+
+// Category to extend the notification center with different implementations
+// of the deliverNotification selector which can be swizzled as part of the
+// test.
+// TODO(miguelg) replace this with OCMock once a version with support
+// for dynamic properties is rolled out (crbug.com/622753).
+@interface NSUserNotificationCenter (TestAdditions)
+- (void)expectationsNoButtons:(NSUserNotification*)notification;
+- (void)expectationsOneButton:(NSUserNotification*)notification;
+@end
+
+@implementation NSUserNotificationCenter (TestAdditions)
+
+// Expectations for notifications with no buttons.
+- (void)expectationsNoButtons:(NSUserNotification*)notification {
+ ASSERT_TRUE([[notification title] isEqualToString:@"Title"]);
Robert Sesek 2016/10/13 16:47:32 Use EXPECT_NSEQ if you want a little bit better fa
Miguel Garcia 2016/10/14 10:58:14 Done
+ ASSERT_TRUE([[notification informativeText] isEqualToString:@"Context"]);
+ ASSERT_TRUE([[notification subtitle] isEqualToString:@"https://gmail.com"]);
+ ASSERT_TRUE([[notification otherButtonTitle] isEqualToString:@"Close"]);
+ ASSERT_TRUE([[notification actionButtonTitle] isEqualToString:@"Settings"]);
+}
+
+// Expectations for notifications with one button.
+- (void)expectationsOneButton:(NSUserNotification*)notification {
+ ASSERT_TRUE([[notification title] isEqualToString:@"Title"]);
+ ASSERT_TRUE([[notification informativeText] isEqualToString:@"Context"]);
+ ASSERT_TRUE([[notification subtitle] isEqualToString:@"https://gmail.com"]);
+ ASSERT_TRUE([[notification otherButtonTitle] isEqualToString:@"Close"]);
+ ASSERT_TRUE([[notification actionButtonTitle] isEqualToString:@"Options"]);
+}
+
+@end
+
+TEST_F(NotificationPlatformBridgeMacTest, TestDisplayNoButtons) {
+ base::scoped_nsobject<NSUserNotificationCenter> notification_center(
+ [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
+ base::mac::ScopedObjCClassSwizzler swizzler(
+ [notification_center class], @selector(deliverNotification:),
+ @selector(expectationsNoButtons:));
+
+ std::unique_ptr<Notification> notification =
+ CreateNotification("Title", "Context", "https://gmail.com", "", "");
+ std::unique_ptr<NotificationPlatformBridgeMac> bridge(
+ new NotificationPlatformBridgeMac(notification_center));
+ bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
+ "profile_id", false, *notification);
+}
+
+TEST_F(NotificationPlatformBridgeMacTest, TestDisplayOneButton) {
+ std::unique_ptr<Notification> notification = CreateNotification(
+ "Title", "Context", "https://gmail.com", "Button 1", "");
+ base::scoped_nsobject<NSUserNotificationCenter> notification_center(
+ [NSUserNotificationCenter _centerForIdentifier:@"" type:0x0]);
+ base::mac::ScopedObjCClassSwizzler swizzler(
+ [notification_center class], @selector(deliverNotification:),
+ @selector(expectationsOneButton:));
+ std::unique_ptr<NotificationPlatformBridgeMac> bridge(
+ new NotificationPlatformBridgeMac(notification_center));
+ bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
+ "profile_id", false, *notification);
+}
« 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