File: notification_utils_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (212 lines) | stat: -rw-r--r-- 8,378 bytes parent folder | download | duplicates (6)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/mac/notification_utils.h"

#include <optional>
#include <string>

#include "base/path_service.h"
#include "chrome/browser/notifications/notification_platform_bridge.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notifications/notification_operation.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/public/cpp/notification.h"

using message_center::Notification;

class NotificationUtilsMacTest : public testing::Test {
 public:
  void SetUp() override { response_ = BuildDefaultNotificationResponse(); }

 protected:
  mac_notifications::mojom::NotificationMetadataPtr
  CreateNotificationMetadata() {
    auto profile_identifier = mac_notifications::mojom::ProfileIdentifier::New(
        "profile", /*incognito=*/false);
    auto notification_identifier =
        mac_notifications::mojom::NotificationIdentifier::New(
            "notification_id", std::move(profile_identifier));
    base::FilePath user_data_dir;
    EXPECT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
    return mac_notifications::mojom::NotificationMetadata::New(
        std::move(notification_identifier), /*notification_type=*/0,
        /*origin_url=*/GURL(), user_data_dir.value());
  }

  mac_notifications::mojom::NotificationActionInfoPtr
  BuildDefaultNotificationResponse() {
    auto meta = CreateNotificationMetadata();
    return mac_notifications::mojom::NotificationActionInfo::New(
        std::move(meta), NotificationOperation::kClick,
        /*button_index=*/-1, /*reply=*/std::nullopt);
  }

  Notification CreateNotification(
      const std::u16string& title,
      const std::u16string& subtitle,
      const std::string& origin,
      message_center::NotificationType type,
      int progress,
      const std::optional<std::u16string>& contextMessage) {
    GURL url(origin);

    Notification notification(type, "test_id", title, subtitle,
                              ui::ImageModel(), u"Notifier's Name", url,
                              message_center::NotifierId(url),
                              message_center::RichNotificationData(),
                              /*delegate=*/nullptr);

    if (type == message_center::NOTIFICATION_TYPE_PROGRESS)
      notification.set_progress(progress);

    if (contextMessage)
      notification.set_context_message(*contextMessage);

    return notification;
  }

  mac_notifications::mojom::NotificationActionInfoPtr response_;
};

TEST_F(NotificationUtilsMacTest, TestCreateNotificationTitle) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://moe.example.com",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdTitle = CreateMacNotificationTitle(notification);
  EXPECT_EQ(u"Title", createdTitle);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationTitleWithProgress) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://moe.example.com",
      message_center::NOTIFICATION_TYPE_PROGRESS, /*progress=*/50,
      /*contextMessage=*/std::nullopt);
  std::u16string createdTitle = CreateMacNotificationTitle(notification);
  EXPECT_EQ(u"50% - Title", createdTitle);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContextBanner) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://moe.example.com",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"moe.example.com", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContextAlert) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://moe.example.com",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/true, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"moe.example.com", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContextNoAttribution) {
  Notification notification =
      CreateNotification(u"Title", u"Subtitle", /*origin=*/std::string(),
                         message_center::NOTIFICATION_TYPE_SIMPLE,
                         /*progress=*/0,
                         /*contextMessage=*/u"moe");
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/false);
  EXPECT_EQ(u"moe", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContexteTLDPlusOne) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle",
      "https://thisisareallyreallyreaaalllyyylongorigin.moe.example.com/",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"example.com", createdContext);

  // Should also work if the eTLD is in the format of '/+.+/'
  notification.set_origin_url(GURL(
      "https://thisisareallyreallyreaaalllyyylongorigin.moe.example.co.uk/"));
  createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"example.co.uk", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContextAlertLongOrigin) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://thisisalongorigin.moe.co.uk",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/true, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"moe.co.uk", createdContext);

  // For banners this should pass
  createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"thisisalongorigin.moe.co.uk", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestCreateNotificationContextLongOrigin) {
  Notification notification = CreateNotification(
      u"Title", u"Subtitle", "https://thisisareallylongorigin.moe.co.uk",
      message_center::NOTIFICATION_TYPE_SIMPLE, /*progress=*/0,
      /*contextMessage=*/std::nullopt);
  std::u16string createdContext = CreateMacNotificationContext(
      /*isPersistent=*/true, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"moe.co.uk", createdContext);

  // It should get the eTLD+1 for banners too
  createdContext = CreateMacNotificationContext(
      /*isPersistent=*/false, notification, /*requiresAttribution=*/true);
  EXPECT_EQ(u"moe.co.uk", createdContext);
}

TEST_F(NotificationUtilsMacTest,
       TestNotificationVerifyValidResponse) {
  EXPECT_TRUE(VerifyMacNotificationData(response_));
}

TEST_F(NotificationUtilsMacTest, TestNotificationUnknownType) {
  response_->meta->type = 210581;
  EXPECT_FALSE(VerifyMacNotificationData(response_));
}

TEST_F(NotificationUtilsMacTest,
       TestNotificationVerifyNoProfileId) {
  response_->meta->id->profile = nullptr;
  EXPECT_FALSE(VerifyMacNotificationData(response_));
}

TEST_F(NotificationUtilsMacTest,
       TestNotificationVerifyNoNotificationId) {
  response_->meta->id = nullptr;
  EXPECT_FALSE(VerifyMacNotificationData(response_));
}

TEST_F(NotificationUtilsMacTest,
       TestNotificationVerifyInvalidButton) {
  response_->button_index = -5;
  EXPECT_FALSE(VerifyMacNotificationData(response_));
}

TEST_F(NotificationUtilsMacTest, TestNotificationVerifyOrigin) {
  response_->meta->origin_url = GURL("http://?");
  EXPECT_FALSE(VerifyMacNotificationData(response_));

  // If however the origin is not present the response should be fine.
  response_->meta->origin_url = GURL();
  EXPECT_TRUE(VerifyMacNotificationData(response_));
}