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

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_mac_unittest.mm

Issue 2749453002: Make GetDisplayedNotifications asynchronous. (Closed)
Patch Set: review Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #import <AppKit/AppKit.h> 5 #import <AppKit/AppKit.h>
6 #import <objc/runtime.h> 6 #import <objc/runtime.h>
7 7
8 #include "base/bind.h"
8 #include "base/mac/scoped_nsobject.h" 9 #include "base/mac/scoped_nsobject.h"
10 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/notifications/notification.h" 12 #include "chrome/browser/notifications/notification.h"
11 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" 13 #include "chrome/browser/notifications/notification_platform_bridge_mac.h"
12 #include "chrome/browser/notifications/notification_test_util.h" 14 #include "chrome/browser/notifications/notification_test_util.h"
13 #include "chrome/browser/notifications/stub_alert_dispatcher_mac.h" 15 #include "chrome/browser/notifications/stub_alert_dispatcher_mac.h"
14 #include "chrome/browser/notifications/stub_notification_center_mac.h" 16 #include "chrome/browser/notifications/stub_notification_center_mac.h"
15 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" 17 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h"
16 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" 18 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
17 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h" 19 #include "chrome/browser/ui/cocoa/notifications/notification_response_builder_ma c.h"
18 #include "chrome/common/features.h" 20 #include "chrome/common/features.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/gtest_mac.h" 23 #include "testing/gtest_mac.h"
21 #include "url/gurl.h" 24 #include "url/gurl.h"
22 25
23 class NotificationPlatformBridgeMacTest : public testing::Test { 26 class NotificationPlatformBridgeMacTest : public testing::Test {
24 public: 27 public:
28 NotificationPlatformBridgeMacTest()
29 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {}
25 void SetUp() override { 30 void SetUp() override {
26 notification_center_.reset([[StubNotificationCenter alloc] init]); 31 notification_center_.reset([[StubNotificationCenter alloc] init]);
27 alert_dispatcher_.reset([[StubAlertDispatcher alloc] init]); 32 alert_dispatcher_.reset([[StubAlertDispatcher alloc] init]);
28 } 33 }
29 34
30 void TearDown() override { 35 void TearDown() override {
31 [notification_center_ removeAllDeliveredNotifications]; 36 [notification_center_ removeAllDeliveredNotifications];
32 [alert_dispatcher_ closeAllNotifications]; 37 [alert_dispatcher_ closeAllNotifications];
33 } 38 }
34 39
(...skipping 11 matching lines...) Expand all
46 [builder setTag:@"tag1"]; 51 [builder setTag:@"tag1"];
47 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]]; 52 [builder setIcon:[NSImage imageNamed:@"NSApplicationIcon"]];
48 [builder setNotificationId:@"notification_id"]; 53 [builder setNotificationId:@"notification_id"];
49 [builder setProfileId:@"profile_id"]; 54 [builder setProfileId:@"profile_id"];
50 [builder setIncognito:false]; 55 [builder setIncognito:false];
51 [builder setNotificationType:@(NotificationCommon::PERSISTENT)]; 56 [builder setNotificationType:@(NotificationCommon::PERSISTENT)];
52 57
53 return [builder buildUserNotification]; 58 return [builder buildUserNotification];
54 } 59 }
55 60
61 static void StoreNotificationCount(
62 int* out_notification_count,
63 std::unique_ptr<std::set<std::string>> notifications,
64 bool supports_synchronization) {
65 DCHECK(out_notification_count);
66 DCHECK(notifications);
67 *out_notification_count = notifications->size();
68 }
69
56 std::unique_ptr<Notification> CreateBanner(const char* title, 70 std::unique_ptr<Notification> CreateBanner(const char* title,
57 const char* subtitle, 71 const char* subtitle,
58 const char* origin, 72 const char* origin,
59 const char* button1, 73 const char* button1,
60 const char* button2) { 74 const char* button2) {
61 return CreateNotification(title, subtitle, origin, button1, button2, 75 return CreateNotification(title, subtitle, origin, button1, button2,
62 false /* require_interaction */); 76 false /* require_interaction */);
63 } 77 }
64 78
65 std::unique_ptr<Notification> CreateAlert(const char* title, 79 std::unique_ptr<Notification> CreateAlert(const char* title,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 123
110 NSUserNotificationCenter* notification_center() { 124 NSUserNotificationCenter* notification_center() {
111 return notification_center_.get(); 125 return notification_center_.get();
112 } 126 }
113 127
114 StubAlertDispatcher* alert_dispatcher() { return alert_dispatcher_.get(); } 128 StubAlertDispatcher* alert_dispatcher() { return alert_dispatcher_.get(); }
115 129
116 private: 130 private:
117 base::scoped_nsobject<StubNotificationCenter> notification_center_; 131 base::scoped_nsobject<StubNotificationCenter> notification_center_;
118 base::scoped_nsobject<StubAlertDispatcher> alert_dispatcher_; 132 base::scoped_nsobject<StubAlertDispatcher> alert_dispatcher_;
133 content::TestBrowserThreadBundle thread_bundle_;
119 }; 134 };
120 135
121 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) { 136 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationVerifyValidResponse) {
122 NSDictionary* response = BuildDefaultNotificationResponse(); 137 NSDictionary* response = BuildDefaultNotificationResponse();
123 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response)); 138 EXPECT_TRUE(NotificationPlatformBridgeMac::VerifyNotificationData(response));
124 } 139 }
125 140
126 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) { 141 TEST_F(NotificationPlatformBridgeMacTest, TestNotificationUnknownType) {
127 NSMutableDictionary* response = BuildDefaultNotificationResponse(); 142 NSMutableDictionary* response = BuildDefaultNotificationResponse();
128 [response setValue:[NSNumber numberWithInt:210581] 143 [response setValue:[NSNumber numberWithInt:210581]
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayed) { 271 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayed) {
257 std::unique_ptr<Notification> notification = CreateBanner( 272 std::unique_ptr<Notification> notification = CreateBanner(
258 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 273 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
259 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 274 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
260 new NotificationPlatformBridgeMac(notification_center(), nil)); 275 new NotificationPlatformBridgeMac(notification_center(), nil));
261 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 276 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
262 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 277 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
263 "profile_id", false, *notification); 278 "profile_id", false, *notification);
264 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 279 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
265 280
266 std::set<std::string> notifications; 281 int notification_count = -1;
267 EXPECT_TRUE(bridge->GetDisplayed("profile_id", false, &notifications)); 282 bridge->GetDisplayed(
268 EXPECT_EQ(1u, notifications.size()); 283 "profile_id", false /* incognito */,
284 base::Bind(&StoreNotificationCount, &notification_count));
285 base::RunLoop().RunUntilIdle();
286 EXPECT_EQ(1, notification_count);
269 } 287 }
270 288
271 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayedUnknownProfile) { 289 TEST_F(NotificationPlatformBridgeMacTest, TestGetDisplayedUnknownProfile) {
272 std::unique_ptr<Notification> notification = CreateBanner( 290 std::unique_ptr<Notification> notification = CreateBanner(
273 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 291 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
274 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 292 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
275 new NotificationPlatformBridgeMac(notification_center(), nil)); 293 new NotificationPlatformBridgeMac(notification_center(), nil));
276 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 294 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
277 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 295 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
278 "profile_id", false, *notification); 296 "profile_id", false, *notification);
279 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 297 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
280 298 int notification_count = -1;
281 std::set<std::string> notifications; 299 bridge->GetDisplayed(
282 EXPECT_TRUE( 300 "unknown_profile_id", false /* incognito */,
283 bridge->GetDisplayed("unknown_profile_id", false, &notifications)); 301 base::Bind(&StoreNotificationCount, &notification_count));
284 EXPECT_EQ(0u, notifications.size()); 302 base::RunLoop().RunUntilIdle();
303 EXPECT_EQ(0, notification_count);
285 } 304 }
286 305
287 TEST_F(NotificationPlatformBridgeMacTest, TestQuitRemovesNotifications) { 306 TEST_F(NotificationPlatformBridgeMacTest, TestQuitRemovesNotifications) {
288 std::unique_ptr<Notification> notification = CreateBanner( 307 std::unique_ptr<Notification> notification = CreateBanner(
289 "Title", "Context", "https://gmail.com", "Button 1", nullptr); 308 "Title", "Context", "https://gmail.com", "Button 1", nullptr);
290 { 309 {
291 std::unique_ptr<NotificationPlatformBridgeMac> bridge( 310 std::unique_ptr<NotificationPlatformBridgeMac> bridge(
292 new NotificationPlatformBridgeMac(notification_center(), nil)); 311 new NotificationPlatformBridgeMac(notification_center(), nil));
293 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 312 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
294 bridge->Display(NotificationCommon::PERSISTENT, "notification_id", 313 bridge->Display(NotificationCommon::PERSISTENT, "notification_id",
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]); 383 EXPECT_EQ(1u, [[notification_center() deliveredNotifications] count]);
365 EXPECT_EQ(1u, [[alert_dispatcher() alerts] count]); 384 EXPECT_EQ(1u, [[alert_dispatcher() alerts] count]);
366 } 385 }
367 386
368 // The destructor of the bridge should close all notifications. 387 // The destructor of the bridge should close all notifications.
369 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]); 388 EXPECT_EQ(0u, [[notification_center() deliveredNotifications] count]);
370 EXPECT_EQ(0u, [[alert_dispatcher() alerts] count]); 389 EXPECT_EQ(0u, [[alert_dispatcher() alerts] count]);
371 } 390 }
372 391
373 #endif 392 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698