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

Side by Side Diff: chrome/browser/notifications/platform_notification_service_unittest.cc

Issue 2534443002: Use notification display service to collect persistent notifications. (Closed)
Patch Set: add extra test checks Created 4 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" 48 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
49 #include "chrome/browser/chromeos/settings/cros_settings.h" 49 #include "chrome/browser/chromeos/settings/cros_settings.h"
50 #include "chrome/browser/chromeos/settings/device_settings_service.h" 50 #include "chrome/browser/chromeos/settings/device_settings_service.h"
51 #endif 51 #endif
52 52
53 using content::NotificationResources; 53 using content::NotificationResources;
54 using content::PlatformNotificationData; 54 using content::PlatformNotificationData;
55 55
56 namespace { 56 namespace {
57 57
58 const char kNotificationId[] = "my-notification-id"; 58 const char kPersistentNotificationId[] = "p:my-notification-id";
59 const char kNonPersistentNotificationId[] = "n:my-notification-id";
59 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; 60 const int kNotificationVibrationPattern[] = { 100, 200, 300 };
60 61
61 class MockDesktopNotificationDelegate 62 class MockDesktopNotificationDelegate
62 : public content::DesktopNotificationDelegate { 63 : public content::DesktopNotificationDelegate {
63 public: 64 public:
64 MockDesktopNotificationDelegate() 65 MockDesktopNotificationDelegate()
65 : displayed_(false), 66 : displayed_(false),
66 clicked_(false) {} 67 clicked_(false) {}
67 68
68 ~MockDesktopNotificationDelegate() override {} 69 ~MockDesktopNotificationDelegate() override {}
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // The close closure may be specified if so desired. 120 // The close closure may be specified if so desired.
120 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( 121 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure(
121 base::Closure* close_closure) const { 122 base::Closure* close_closure) const {
122 PlatformNotificationData notification_data; 123 PlatformNotificationData notification_data;
123 notification_data.title = base::ASCIIToUTF16("My Notification"); 124 notification_data.title = base::ASCIIToUTF16("My Notification");
124 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 125 notification_data.body = base::ASCIIToUTF16("Hello, world!");
125 126
126 MockDesktopNotificationDelegate* delegate = 127 MockDesktopNotificationDelegate* delegate =
127 new MockDesktopNotificationDelegate(); 128 new MockDesktopNotificationDelegate();
128 129
129 service()->DisplayNotification(profile(), kNotificationId, 130 service()->DisplayNotification(profile(), kNonPersistentNotificationId,
130 GURL("https://chrome.com/"), 131 GURL("https://chrome.com/"),
131 notification_data, NotificationResources(), 132 notification_data, NotificationResources(),
132 base::WrapUnique(delegate), close_closure); 133 base::WrapUnique(delegate), close_closure);
133 134
134 return delegate; 135 return delegate;
135 } 136 }
136 137
137 // Returns the Platform Notification Service these unit tests are for. 138 // Returns the Platform Notification Service these unit tests are for.
138 PlatformNotificationServiceImpl* service() const { 139 PlatformNotificationServiceImpl* service() const {
139 return PlatformNotificationServiceImpl::GetInstance(); 140 return PlatformNotificationServiceImpl::GetInstance();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 // Note that we cannot verify whether the closed event was called on the 191 // Note that we cannot verify whether the closed event was called on the
191 // delegate given that it'd result in a use-after-free. 192 // delegate given that it'd result in a use-after-free.
192 } 193 }
193 194
194 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { 195 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) {
195 PlatformNotificationData notification_data; 196 PlatformNotificationData notification_data;
196 notification_data.title = base::ASCIIToUTF16("My notification's title"); 197 notification_data.title = base::ASCIIToUTF16("My notification's title");
197 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 198 notification_data.body = base::ASCIIToUTF16("Hello, world!");
198 199
199 service()->DisplayPersistentNotification( 200 service()->DisplayPersistentNotification(
200 profile(), kNotificationId, GURL() /* service_worker_scope */, 201 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */,
201 GURL("https://chrome.com/"), notification_data, NotificationResources()); 202 GURL("https://chrome.com/"), notification_data, NotificationResources());
202 203
203 ASSERT_EQ(1u, GetNotificationCount()); 204 ASSERT_EQ(1u, GetNotificationCount());
204 205
205 Notification notification = GetDisplayedNotification(); 206 Notification notification = GetDisplayedNotification();
206 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 207 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
207 EXPECT_EQ("My notification's title", 208 EXPECT_EQ("My notification's title",
208 base::UTF16ToUTF8(notification.title())); 209 base::UTF16ToUTF8(notification.title()));
209 EXPECT_EQ("Hello, world!", 210 EXPECT_EQ("Hello, world!",
210 base::UTF16ToUTF8(notification.message())); 211 base::UTF16ToUTF8(notification.message()));
211 212
212 service()->ClosePersistentNotification(profile(), kNotificationId); 213 service()->ClosePersistentNotification(profile(), kPersistentNotificationId);
213 EXPECT_EQ(0u, GetNotificationCount()); 214 EXPECT_EQ(0u, GetNotificationCount());
214 } 215 }
215 216
216 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { 217 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) {
217 std::vector<int> vibration_pattern( 218 std::vector<int> vibration_pattern(
218 kNotificationVibrationPattern, 219 kNotificationVibrationPattern,
219 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); 220 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern));
220 221
221 PlatformNotificationData notification_data; 222 PlatformNotificationData notification_data;
222 notification_data.title = base::ASCIIToUTF16("My notification's title"); 223 notification_data.title = base::ASCIIToUTF16("My notification's title");
223 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 224 notification_data.body = base::ASCIIToUTF16("Hello, world!");
224 notification_data.vibration_pattern = vibration_pattern; 225 notification_data.vibration_pattern = vibration_pattern;
225 notification_data.silent = true; 226 notification_data.silent = true;
226 227
227 MockDesktopNotificationDelegate* delegate 228 MockDesktopNotificationDelegate* delegate
228 = new MockDesktopNotificationDelegate(); 229 = new MockDesktopNotificationDelegate();
229 service()->DisplayNotification(profile(), kNotificationId, 230 service()->DisplayNotification(profile(), kNonPersistentNotificationId,
230 GURL("https://chrome.com/"), notification_data, 231 GURL("https://chrome.com/"), notification_data,
231 NotificationResources(), 232 NotificationResources(),
232 base::WrapUnique(delegate), nullptr); 233 base::WrapUnique(delegate), nullptr);
233 234
234 ASSERT_EQ(1u, GetNotificationCount()); 235 ASSERT_EQ(1u, GetNotificationCount());
235 236
236 Notification notification = GetDisplayedNotification(); 237 Notification notification = GetDisplayedNotification();
237 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 238 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
238 EXPECT_EQ("My notification's title", 239 EXPECT_EQ("My notification's title",
239 base::UTF16ToUTF8(notification.title())); 240 base::UTF16ToUTF8(notification.title()));
(...skipping 21 matching lines...) Expand all
261 content::PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON; 262 content::PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON;
262 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); 263 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1");
263 notification_data.actions[1].type = 264 notification_data.actions[1].type =
264 content::PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT; 265 content::PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT;
265 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); 266 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2");
266 267
267 NotificationResources notification_resources; 268 NotificationResources notification_resources;
268 notification_resources.action_icons.resize(notification_data.actions.size()); 269 notification_resources.action_icons.resize(notification_data.actions.size());
269 270
270 service()->DisplayPersistentNotification( 271 service()->DisplayPersistentNotification(
271 profile(), kNotificationId, GURL() /* service_worker_scope */, 272 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */,
272 GURL("https://chrome.com/"), notification_data, notification_resources); 273 GURL("https://chrome.com/"), notification_data, notification_resources);
273 274
274 ASSERT_EQ(1u, GetNotificationCount()); 275 ASSERT_EQ(1u, GetNotificationCount());
275 276
276 Notification notification = GetDisplayedNotification(); 277 Notification notification = GetDisplayedNotification();
277 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 278 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
278 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); 279 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title()));
279 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); 280 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message()));
280 281
281 EXPECT_THAT(notification.vibration_pattern(), 282 EXPECT_THAT(notification.vibration_pattern(),
(...skipping 19 matching lines...) Expand all
301 302
302 base::Time after_page_notification = 303 base::Time after_page_notification =
303 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( 304 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage(
304 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 305 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
305 EXPECT_GT(after_page_notification, begin_time); 306 EXPECT_GT(after_page_notification, begin_time);
306 307
307 // Ensure that there is at least some time between the two calls. 308 // Ensure that there is at least some time between the two calls.
308 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); 309 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
309 310
310 service()->DisplayPersistentNotification( 311 service()->DisplayPersistentNotification(
311 profile(), kNotificationId, GURL() /* service_worker_scope */, origin, 312 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */,
312 PlatformNotificationData(), NotificationResources()); 313 origin, PlatformNotificationData(), NotificationResources());
313 314
314 base::Time after_persistent_notification = 315 base::Time after_persistent_notification =
315 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( 316 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage(
316 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 317 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
317 EXPECT_GT(after_persistent_notification, after_page_notification); 318 EXPECT_GT(after_persistent_notification, after_page_notification);
318 } 319 }
319 320
320 #if BUILDFLAG(ENABLE_EXTENSIONS) 321 #if BUILDFLAG(ENABLE_EXTENSIONS)
321 322
322 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { 323 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 notification = service()->CreateNotificationFromData( 436 notification = service()->CreateNotificationFromData(
436 profile(), 437 profile(),
437 GURL() /* service_worker_scope */, 438 GURL() /* service_worker_scope */,
438 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), 439 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"),
439 notification_data, NotificationResources(), 440 notification_data, NotificationResources(),
440 new MockNotificationDelegate("hello")); 441 new MockNotificationDelegate("hello"));
441 EXPECT_EQ("NotificationTest", 442 EXPECT_EQ("NotificationTest",
442 base::UTF16ToUTF8(notification.context_message())); 443 base::UTF16ToUTF8(notification.context_message()));
443 } 444 }
444 445
446 TEST_F(PlatformNotificationServiceTest, GetDisplayedPersistentNotifications) {
447 PlatformNotificationData notification_data;
448 notification_data.title =
449 base::ASCIIToUTF16("Non Persistent notification's title");
450 notification_data.body = base::ASCIIToUTF16("Hello, world!");
451
452 MockDesktopNotificationDelegate* delegate =
453 new MockDesktopNotificationDelegate();
454 service()->DisplayNotification(profile(), kNonPersistentNotificationId,
455 GURL("https://chrome.com/"), notification_data,
456 NotificationResources(),
457 base::WrapUnique(delegate), nullptr);
458
459 notification_data.title =
460 base::ASCIIToUTF16("Persistent notification's title");
461 service()->DisplayPersistentNotification(
462 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */,
463 GURL("https://chrome.com/"), notification_data, NotificationResources());
464 ASSERT_EQ(2u, GetNotificationCount());
465
466 std::set<std::string> displayed_notifications;
467 ASSERT_TRUE(service()->GetDisplayedPersistentNotifications(
468 profile(), &displayed_notifications));
469 EXPECT_EQ(1u, displayed_notifications.size());
470 }
471
445 #endif // BUILDFLAG(ENABLE_EXTENSIONS) 472 #endif // BUILDFLAG(ENABLE_EXTENSIONS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698