1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// 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>
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/notifications/notification_common.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"
class NotificationResponseBuilderMacTest : public testing::Test {
protected:
base::scoped_nsobject<NotificationBuilder> NewTestBuilder() {
base::scoped_nsobject<NotificationBuilder> builder(
[[NotificationBuilder alloc] initWithCloseLabel:@"Close"
optionsLabel:@"Options"
settingsLabel:@"Settings"]);
[builder setTitle:@"Title"];
[builder setSubTitle:@"https://www.miguel.com"];
[builder setContextMessage:@""];
[builder setTag:@"tag1"];
[builder setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]];
[builder setNotificationId:@"notificationId"];
[builder setProfileId:@"profileId"];
[builder setIncognito:false];
[builder setNotificationType:@(NotificationCommon::PERSISTENT)];
return builder;
}
};
TEST_F(NotificationResponseBuilderMacTest, TestNotificationClick) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
NSUserNotification* notification = [builder buildUserNotification];
// This will be set by the notification center to indicate the notification
// was clicked.
[notification setValue:@(NSUserNotificationActivationTypeContentsClicked)
forKey:@"_activationType"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
EXPECT_EQ(-1, buttonIndex.intValue);
}
TEST_F(NotificationResponseBuilderMacTest, TestNotificationSettingsClick) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
NSUserNotification* notification = [builder buildUserNotification];
// This will be set by the notification center to indicate the only available
// button was clicked.
[notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
forKey:@"_activationType"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue);
EXPECT_EQ(-1, buttonIndex.intValue);
}
TEST_F(NotificationResponseBuilderMacTest, TestNotificationOneActionClick) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
[builder setButtons:@"Button1" secondaryButton:@""];
NSUserNotification* notification = [builder buildUserNotification];
// These values will be set by the notification center to indicate that button
// 1 was clicked.
[notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
forKey:@"_activationType"];
[notification setValue:[NSNumber numberWithInt:0]
forKey:@"_alternateActionIndex"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
EXPECT_EQ(0, buttonIndex.intValue);
}
TEST_F(NotificationResponseBuilderMacTest, TestNotificationTwoActionClick) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
[builder setButtons:@"Button1" secondaryButton:@"Button2"];
NSUserNotification* notification = [builder buildUserNotification];
// These values will be set by the notification center to indicate that button
// 2 was clicked.
[notification setValue:@(NSUserNotificationActivationTypeActionButtonClicked)
forKey:@"_activationType"];
[notification setValue:[NSNumber numberWithInt:1]
forKey:@"_alternateActionIndex"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(0 /* NOTIFICATION_CLICK */, operation.intValue);
EXPECT_EQ(1, buttonIndex.intValue);
}
TEST_F(NotificationResponseBuilderMacTest,
TestNotificationTwoActionSettingsClick) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
[builder setButtons:@"Button1" secondaryButton:@"Button2"];
NSUserNotification* notification = [builder buildUserNotification];
// These values will be set by the notification center to indicate that button
// 2 was clicked.
[notification
setValue:
[NSNumber
numberWithInt:NSUserNotificationActivationTypeActionButtonClicked]
forKey:@"_activationType"];
[notification setValue:[NSNumber numberWithInt:2]
forKey:@"_alternateActionIndex"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(2 /* NOTIFICATION_SETTINGS */, operation.intValue);
EXPECT_EQ(-1, buttonIndex.intValue);
}
TEST_F(NotificationResponseBuilderMacTest, TestNotificationClose) {
base::scoped_nsobject<NotificationBuilder> builder = NewTestBuilder();
NSUserNotification* notification = [builder buildUserNotification];
// None is what the NSUserNotification center emits when closing since it
// interprets it as not activated.
[notification setValue:@(NSUserNotificationActivationTypeNone)
forKey:@"_activationType"];
NSDictionary* response =
[NotificationResponseBuilder buildDictionary:notification];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex =
[response objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(1 /* NOTIFICATION_CLOSE */, operation.intValue);
EXPECT_EQ(-1, buttonIndex.intValue);
}
|