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

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

Issue 2906883003: Deprecate per notification type delegates. (Closed)
Patch Set: review Created 3 years, 6 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 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/bind.h" 9 #include "base/bind.h"
10 #include "base/feature_list.h" 10 #include "base/feature_list.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/platform_thread.h" 15 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
19 #include "chrome/browser/notifications/message_center_display_service.h" 19 #include "chrome/browser/notifications/message_center_display_service.h"
20 #include "chrome/browser/notifications/notification_delegate.h" 20 #include "chrome/browser/notifications/native_notification_delegate.h"
21 #include "chrome/browser/notifications/notification_display_service_factory.h" 21 #include "chrome/browser/notifications/notification_display_service_factory.h"
22 #include "chrome/browser/notifications/notification_test_util.h" 22 #include "chrome/browser/notifications/notification_test_util.h"
23 #include "chrome/browser/notifications/platform_notification_service_impl.h" 23 #include "chrome/browser/notifications/platform_notification_service_impl.h"
24 #include "chrome/browser/notifications/stub_notification_platform_bridge.h" 24 #include "chrome/browser/notifications/stub_notification_platform_bridge.h"
25 #include "chrome/common/chrome_features.h" 25 #include "chrome/common/chrome_features.h"
26 #include "chrome/test/base/testing_browser_process.h" 26 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_profile.h" 27 #include "chrome/test/base/testing_profile.h"
28 #include "chrome/test/base/testing_profile_manager.h" 28 #include "chrome/test/base/testing_profile_manager.h"
29 #include "components/content_settings/core/browser/host_content_settings_map.h" 29 #include "components/content_settings/core/browser/host_content_settings_map.h"
30 #include "content/public/browser/desktop_notification_delegate.h"
31 #include "content/public/common/notification_resources.h" 30 #include "content/public/common/notification_resources.h"
32 #include "content/public/common/platform_notification_data.h" 31 #include "content/public/common/platform_notification_data.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 32 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "extensions/features/features.h" 33 #include "extensions/features/features.h"
35 #include "testing/gmock/include/gmock/gmock.h" 34 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
37 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h" 36 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h"
38 37
39 #if BUILDFLAG(ENABLE_EXTENSIONS) 38 #if BUILDFLAG(ENABLE_EXTENSIONS)
40 #include "base/command_line.h" 39 #include "base/command_line.h"
41 #include "chrome/browser/extensions/extension_service.h" 40 #include "chrome/browser/extensions/extension_service.h"
42 #include "chrome/browser/extensions/test_extension_system.h" 41 #include "chrome/browser/extensions/test_extension_system.h"
43 #include "extensions/browser/extension_registry.h" 42 #include "extensions/browser/extension_registry.h"
44 #include "extensions/browser/process_map.h" 43 #include "extensions/browser/process_map.h"
45 #include "extensions/common/extension.h" 44 #include "extensions/common/extension.h"
46 #include "extensions/common/extension_builder.h" 45 #include "extensions/common/extension_builder.h"
47 #include "extensions/common/permissions/api_permission.h" 46 #include "extensions/common/permissions/api_permission.h"
48 #include "extensions/common/value_builder.h" 47 #include "extensions/common/value_builder.h"
49 #endif 48 #endif
50 49
51 using content::NotificationResources; 50 using content::NotificationResources;
52 using content::PlatformNotificationData; 51 using content::PlatformNotificationData;
53 52
54 namespace { 53 namespace {
55 54
56 const char kNotificationId[] = "my-notification-id"; 55 const char kNotificationId[] = "my-notification-id";
57 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; 56 const int kNotificationVibrationPattern[] = { 100, 200, 300 };
58 57
59 class MockDesktopNotificationDelegate
60 : public content::DesktopNotificationDelegate {
61 public:
62 MockDesktopNotificationDelegate() : displayed_(false) {}
63
64 ~MockDesktopNotificationDelegate() override {}
65
66 // content::DesktopNotificationDelegate implementation.
67 void NotificationDisplayed() override { displayed_ = true; }
68 void NotificationClosed() override {}
69 void NotificationClick() override {}
70
71 bool displayed() const { return displayed_; }
72
73 private:
74 bool displayed_;
75
76 DISALLOW_COPY_AND_ASSIGN(MockDesktopNotificationDelegate);
77 };
78
79 } // namespace 58 } // namespace
80 59
81 class PlatformNotificationServiceTest : public testing::Test { 60 class PlatformNotificationServiceTest : public testing::Test {
82 public: 61 public:
83 void SetUp() override { 62 void SetUp() override {
84 profile_manager_ = base::MakeUnique<TestingProfileManager>( 63 profile_manager_ = base::MakeUnique<TestingProfileManager>(
85 TestingBrowserProcess::GetGlobal()); 64 TestingBrowserProcess::GetGlobal());
86 ASSERT_TRUE(profile_manager_->SetUp()); 65 ASSERT_TRUE(profile_manager_->SetUp());
87 profile_ = profile_manager_->CreateTestingProfile("Miguel"); 66 profile_ = profile_manager_->CreateTestingProfile("Miguel");
88 std::unique_ptr<NotificationUIManager> ui_manager = 67 std::unique_ptr<NotificationUIManager> ui_manager =
(...skipping 14 matching lines...) Expand all
103 TestingBrowserProcess::DeleteInstance(); 82 TestingBrowserProcess::DeleteInstance();
104 } 83 }
105 84
106 void DidGetDisplayedNotifications( 85 void DidGetDisplayedNotifications(
107 std::unique_ptr<std::set<std::string>> displayed_notifications, 86 std::unique_ptr<std::set<std::string>> displayed_notifications,
108 bool supports_synchronization) { 87 bool supports_synchronization) {
109 displayed_notifications_ = std::move(displayed_notifications); 88 displayed_notifications_ = std::move(displayed_notifications);
110 } 89 }
111 90
112 protected: 91 protected:
113 // Displays a simple, fake notifications and returns a weak pointer to the 92 // Displays a simple, fake notifications.
114 // delegate receiving events for it (ownership is transferred to the service). 93 void CreateSimplePageNotification() const {
115 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { 94 CreateSimplePageNotificationWithCloseClosure(nullptr);
116 return CreateSimplePageNotificationWithCloseClosure(nullptr);
117 } 95 }
118 96
119 // Displays a simple, fake notification and returns a weak pointer to the 97 // Displays a simple, fake notification.
120 // delegate receiving events for it (ownership is transferred to the service). 98 // The close closure may be specified if desired.
121 // The close closure may be specified if so desired. 99 void CreateSimplePageNotificationWithCloseClosure(
122 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure(
123 base::Closure* close_closure) const { 100 base::Closure* close_closure) const {
124 PlatformNotificationData notification_data; 101 PlatformNotificationData notification_data;
125 notification_data.title = base::ASCIIToUTF16("My Notification"); 102 notification_data.title = base::ASCIIToUTF16("My Notification");
126 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 103 notification_data.body = base::ASCIIToUTF16("Hello, world!");
127 104
128 MockDesktopNotificationDelegate* delegate = 105 service()->DisplayNotification(
129 new MockDesktopNotificationDelegate(); 106 profile(), kNotificationId, GURL("https://chrome.com/"),
130 107 notification_data, NotificationResources(), close_closure);
131 service()->DisplayNotification(profile(), kNotificationId,
132 GURL("https://chrome.com/"),
133 notification_data, NotificationResources(),
134 base::WrapUnique(delegate), close_closure);
135
136 return delegate;
137 } 108 }
138 109
139 // Returns the Platform Notification Service these unit tests are for. 110 // Returns the Platform Notification Service these unit tests are for.
140 PlatformNotificationServiceImpl* service() const { 111 PlatformNotificationServiceImpl* service() const {
141 return PlatformNotificationServiceImpl::GetInstance(); 112 return PlatformNotificationServiceImpl::GetInstance();
142 } 113 }
143 114
144 // Returns the Profile to be used for these tests. 115 // Returns the Profile to be used for these tests.
145 Profile* profile() const { return profile_; } 116 Profile* profile() const { return profile_; }
146 117
(...skipping 29 matching lines...) Expand all
176 NotificationDisplayService* display_service() const { 147 NotificationDisplayService* display_service() const {
177 return NotificationDisplayServiceFactory::GetForProfile(profile_); 148 return NotificationDisplayServiceFactory::GetForProfile(profile_);
178 } 149 }
179 150
180 std::unique_ptr<TestingProfileManager> profile_manager_; 151 std::unique_ptr<TestingProfileManager> profile_manager_;
181 TestingProfile* profile_; 152 TestingProfile* profile_;
182 content::TestBrowserThreadBundle thread_bundle_; 153 content::TestBrowserThreadBundle thread_bundle_;
183 std::unique_ptr<std::set<std::string>> displayed_notifications_; 154 std::unique_ptr<std::set<std::string>> displayed_notifications_;
184 }; 155 };
185 156
186 // Native, non persistent notifications don't have delegates any more
187 #if !defined(OS_MACOSX)
188 #if defined(OS_ANDROID)
189 // http://crbug.com/729247
190 #define DisplayPageDisplayedEvent DISABLED_DisplayPageDisplayedEvent
191 #endif
192 TEST_F(PlatformNotificationServiceTest, DisplayPageDisplayedEvent) {
193 auto* delegate = CreateSimplePageNotification();
194
195 EXPECT_EQ(1u, GetNotificationCount());
196 EXPECT_TRUE(delegate->displayed());
197 }
198 #endif // !defined(OS_MACOSX)
199 157
200 TEST_F(PlatformNotificationServiceTest, DisplayPageCloseClosure) { 158 TEST_F(PlatformNotificationServiceTest, DisplayPageCloseClosure) {
201 base::Closure close_closure; 159 base::Closure close_closure;
202 CreateSimplePageNotificationWithCloseClosure(&close_closure); 160 CreateSimplePageNotificationWithCloseClosure(&close_closure);
203 161
204 EXPECT_EQ(1u, GetNotificationCount()); 162 EXPECT_EQ(1u, GetNotificationCount());
205 163
206 ASSERT_FALSE(close_closure.is_null()); 164 ASSERT_FALSE(close_closure.is_null());
207 close_closure.Run(); 165 close_closure.Run();
208 166
(...skipping 28 matching lines...) Expand all
237 std::vector<int> vibration_pattern( 195 std::vector<int> vibration_pattern(
238 kNotificationVibrationPattern, 196 kNotificationVibrationPattern,
239 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); 197 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern));
240 198
241 PlatformNotificationData notification_data; 199 PlatformNotificationData notification_data;
242 notification_data.title = base::ASCIIToUTF16("My notification's title"); 200 notification_data.title = base::ASCIIToUTF16("My notification's title");
243 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 201 notification_data.body = base::ASCIIToUTF16("Hello, world!");
244 notification_data.vibration_pattern = vibration_pattern; 202 notification_data.vibration_pattern = vibration_pattern;
245 notification_data.silent = true; 203 notification_data.silent = true;
246 204
247 MockDesktopNotificationDelegate* delegate
248 = new MockDesktopNotificationDelegate();
249 service()->DisplayNotification(profile(), kNotificationId, 205 service()->DisplayNotification(profile(), kNotificationId,
250 GURL("https://chrome.com/"), notification_data, 206 GURL("https://chrome.com/"), notification_data,
251 NotificationResources(), 207 NotificationResources(), nullptr);
252 base::WrapUnique(delegate), nullptr);
253 208
254 ASSERT_EQ(1u, GetNotificationCount()); 209 ASSERT_EQ(1u, GetNotificationCount());
255 210
256 Notification notification = GetDisplayedNotification(); 211 Notification notification = GetDisplayedNotification();
257 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 212 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
258 EXPECT_EQ("My notification's title", 213 EXPECT_EQ("My notification's title",
259 base::UTF16ToUTF8(notification.title())); 214 base::UTF16ToUTF8(notification.title()));
260 EXPECT_EQ("Hello, world!", 215 EXPECT_EQ("Hello, world!",
261 base::UTF16ToUTF8(notification.message())); 216 base::UTF16ToUTF8(notification.message()));
262 217
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 } 348 }
394 349
395 TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) { 350 TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) {
396 PlatformNotificationData notification_data; 351 PlatformNotificationData notification_data;
397 notification_data.title = base::ASCIIToUTF16("My Notification"); 352 notification_data.title = base::ASCIIToUTF16("My Notification");
398 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 353 notification_data.body = base::ASCIIToUTF16("Hello, world!");
399 354
400 Notification notification = service()->CreateNotificationFromData( 355 Notification notification = service()->CreateNotificationFromData(
401 profile(), GURL() /* service_worker_scope */, GURL("https://chrome.com/"), 356 profile(), GURL() /* service_worker_scope */, GURL("https://chrome.com/"),
402 notification_data, NotificationResources(), 357 notification_data, NotificationResources(),
403 new MockNotificationDelegate("hello")); 358 new NativeNotificationDelegate("hello"));
404 EXPECT_TRUE(notification.context_message().empty()); 359 EXPECT_TRUE(notification.context_message().empty());
405 360
406 // Create a mocked extension. 361 // Create a mocked extension.
407 scoped_refptr<extensions::Extension> extension = 362 scoped_refptr<extensions::Extension> extension =
408 extensions::ExtensionBuilder() 363 extensions::ExtensionBuilder()
409 .SetID("honijodknafkokifofgiaalefdiedpko") 364 .SetID("honijodknafkokifofgiaalefdiedpko")
410 .SetManifest(extensions::DictionaryBuilder() 365 .SetManifest(extensions::DictionaryBuilder()
411 .Set("name", "NotificationTest") 366 .Set("name", "NotificationTest")
412 .Set("version", "1.0") 367 .Set("version", "1.0")
413 .Set("manifest_version", 2) 368 .Set("manifest_version", 2)
414 .Set("description", "Test Extension") 369 .Set("description", "Test Extension")
415 .Build()) 370 .Build())
416 .Build(); 371 .Build();
417 372
418 extensions::ExtensionRegistry* registry = 373 extensions::ExtensionRegistry* registry =
419 extensions::ExtensionRegistry::Get(profile()); 374 extensions::ExtensionRegistry::Get(profile());
420 EXPECT_TRUE(registry->AddEnabled(extension)); 375 EXPECT_TRUE(registry->AddEnabled(extension));
421 376
422 notification = service()->CreateNotificationFromData( 377 notification = service()->CreateNotificationFromData(
423 profile(), 378 profile(), GURL() /* service_worker_scope */,
424 GURL() /* service_worker_scope */,
425 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), 379 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"),
426 notification_data, NotificationResources(), 380 notification_data, NotificationResources(),
427 new MockNotificationDelegate("hello")); 381 new NativeNotificationDelegate("hello"));
428 EXPECT_EQ("NotificationTest", 382 EXPECT_EQ("NotificationTest",
429 base::UTF16ToUTF8(notification.context_message())); 383 base::UTF16ToUTF8(notification.context_message()));
430 } 384 }
431 385
432 #endif // BUILDFLAG(ENABLE_EXTENSIONS) 386 #endif // BUILDFLAG(ENABLE_EXTENSIONS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698