Commit f052abfe authored by miguelg's avatar miguelg Committed by Commit bot

Create a stub NSUserNotificationCenter and make tests use it.

Also fix a bug in the bridge found by the tests.

BUG=571056

Review-Url: https://codereview.chromium.org/2458703003
Cr-Commit-Position: refs/heads/master@{#430359}
parent ce369229
...@@ -283,7 +283,7 @@ bool NotificationPlatformBridgeMac::GetDisplayed( ...@@ -283,7 +283,7 @@ bool NotificationPlatformBridgeMac::GetDisplayed(
[notification_center_ deliveredNotifications]) { [notification_center_ deliveredNotifications]) {
NSString* toast_profile_id = [toast.userInfo NSString* toast_profile_id = [toast.userInfo
objectForKey:notification_constants::kNotificationProfileId]; objectForKey:notification_constants::kNotificationProfileId];
if (toast_profile_id == current_profile_id) { if ([toast_profile_id isEqualToString:current_profile_id]) {
notifications->insert(base::SysNSStringToUTF8([toast.userInfo notifications->insert(base::SysNSStringToUTF8([toast.userInfo
objectForKey:notification_constants::kNotificationId])); objectForKey:notification_constants::kNotificationId]));
} }
......
// 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.
#ifndef CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_CENTER_MAC_H_
#define CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_CENTER_MAC_H_
#import <Foundation/Foundation.h>
// Stubs NSUserNotificationCenter so it can be used in tests without actually
// displaying notifications.
// Unlike the real class this is not a singleton and the lifecycle needs to be
// handled by the caller.
// See notification_platform_bridge_mac_unittest.mm for an example.
@interface StubNotificationCenter : NSUserNotificationCenter
- (instancetype)init;
- (void)setDelegate:(id<NSUserNotificationCenterDelegate>)delegate;
@end
#endif // CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_CENTER_MAC_H_
// 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 "chrome/browser/notifications/stub_notification_center_mac.h"
#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
@implementation StubNotificationCenter {
base::scoped_nsobject<NSMutableArray> alerts_;
}
- (instancetype)init {
if ((self = [super init])) {
alerts_.reset([[NSMutableArray alloc] init]);
}
return self;
}
// The default implementation adds some extra checks on what constructors can
// be used. isKindOfClass bypasses all of that.
- (BOOL)isKindOfClass:(Class)cls {
if ([cls isEqual:NSClassFromString(@"_NSConcreteUserNotificationCenter")]) {
return YES;
}
return [super isKindOfClass:cls];
}
- (void)deliverNotification:(NSUserNotification*)notification {
[alerts_ addObject:notification];
}
- (NSArray*)deliveredNotifications {
return [[alerts_ copy] autorelease];
}
- (void)removeDeliveredNotification:(NSUserNotification*)notification {
NSString* notificationId = [notification.userInfo
objectForKey:notification_constants::kNotificationId];
NSString* profileId = [notification.userInfo
objectForKey:notification_constants::kNotificationProfileId];
DCHECK(profileId);
DCHECK(notificationId);
for (NSUserNotification* toast in alerts_.get()) {
NSString* toastId =
[toast.userInfo objectForKey:notification_constants::kNotificationId];
NSString* persistentProfileId = [toast.userInfo
objectForKey:notification_constants::kNotificationProfileId];
if ([toastId isEqualToString:notificationId] &&
[persistentProfileId isEqualToString:profileId]) {
[alerts_ removeObject:toast];
break;
}
}
}
- (void)removeAllDeliveredNotifications {
[alerts_ removeAllObjects];
}
// Need to provide a nop implementation of setDelegate as it is
// used during the setup of the bridge.
- (void)setDelegate:(id<NSUserNotificationCenterDelegate>)delegate {
}
@end
...@@ -4155,6 +4155,8 @@ test("unit_tests") { ...@@ -4155,6 +4155,8 @@ test("unit_tests") {
"../browser/notifications/notification_permission_context_unittest.cc", "../browser/notifications/notification_permission_context_unittest.cc",
"../browser/notifications/notification_platform_bridge_mac_unittest.mm", "../browser/notifications/notification_platform_bridge_mac_unittest.mm",
"../browser/notifications/platform_notification_service_unittest.cc", "../browser/notifications/platform_notification_service_unittest.cc",
"../browser/notifications/stub_notification_center_mac.h",
"../browser/notifications/stub_notification_center_mac.mm",
] ]
if (is_android) { if (is_android) {
sources -= [ sources -= [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment