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

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

Issue 1634933006: Use NotificationResources instead of a bare SkBitmap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests. Created 4 years, 11 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/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/threading/platform_thread.h" 11 #include "base/threading/platform_thread.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
15 #include "chrome/browser/notifications/notification_delegate.h" 15 #include "chrome/browser/notifications/notification_delegate.h"
16 #include "chrome/browser/notifications/notification_test_util.h" 16 #include "chrome/browser/notifications/notification_test_util.h"
17 #include "chrome/browser/notifications/platform_notification_service_impl.h" 17 #include "chrome/browser/notifications/platform_notification_service_impl.h"
18 #include "chrome/test/base/testing_profile.h" 18 #include "chrome/test/base/testing_profile.h"
19 #include "components/content_settings/core/browser/host_content_settings_map.h" 19 #include "components/content_settings/core/browser/host_content_settings_map.h"
20 #include "content/public/browser/desktop_notification_delegate.h" 20 #include "content/public/browser/desktop_notification_delegate.h"
21 #include "content/public/common/notification_resources.h"
21 #include "content/public/common/platform_notification_data.h" 22 #include "content/public/common/platform_notification_data.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 26
27 #if defined(ENABLE_EXTENSIONS) 27 #if defined(ENABLE_EXTENSIONS)
28 #include "base/command_line.h" 28 #include "base/command_line.h"
29 #include "chrome/browser/extensions/extension_service.h" 29 #include "chrome/browser/extensions/extension_service.h"
30 #include "chrome/browser/extensions/test_extension_system.h" 30 #include "chrome/browser/extensions/test_extension_system.h"
31 #include "extensions/browser/extension_registry.h" 31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/process_map.h" 32 #include "extensions/browser/process_map.h"
33 #include "extensions/common/extension.h" 33 #include "extensions/common/extension.h"
34 #include "extensions/common/extension_builder.h" 34 #include "extensions/common/extension_builder.h"
35 #include "extensions/common/permissions/api_permission.h" 35 #include "extensions/common/permissions/api_permission.h"
36 #include "extensions/common/value_builder.h" 36 #include "extensions/common/value_builder.h"
37 #endif 37 #endif
38 38
39 #if defined(ENABLE_EXTENSIONS) && defined(OS_CHROMEOS) 39 #if defined(ENABLE_EXTENSIONS) && defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" 40 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
41 #include "chrome/browser/chromeos/settings/cros_settings.h" 41 #include "chrome/browser/chromeos/settings/cros_settings.h"
42 #include "chrome/browser/chromeos/settings/device_settings_service.h" 42 #include "chrome/browser/chromeos/settings/device_settings_service.h"
43 #endif 43 #endif
44 44
45 using content::NotificationResources;
46 using content::PlatformNotificationData;
47
45 namespace { 48 namespace {
46 49
47 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; 50 const int kNotificationVibrationPattern[] = { 100, 200, 300 };
48 51
49 #if !defined(OS_ANDROID) 52 #if !defined(OS_ANDROID)
50 const int64_t kPersistentNotificationId = 42; 53 const int64_t kPersistentNotificationId = 42;
51 #endif 54 #endif
52 55
53 class MockDesktopNotificationDelegate 56 class MockDesktopNotificationDelegate
54 : public content::DesktopNotificationDelegate { 57 : public content::DesktopNotificationDelegate {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 // delegate receiving events for it (ownership is transferred to the service). 98 // delegate receiving events for it (ownership is transferred to the service).
96 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { 99 MockDesktopNotificationDelegate* CreateSimplePageNotification() const {
97 return CreateSimplePageNotificationWithCloseClosure(nullptr); 100 return CreateSimplePageNotificationWithCloseClosure(nullptr);
98 } 101 }
99 102
100 // Displays a simple, fake notification and returns a weak pointer to the 103 // Displays a simple, fake notification and returns a weak pointer to the
101 // delegate receiving events for it (ownership is transferred to the service). 104 // delegate receiving events for it (ownership is transferred to the service).
102 // The close closure may be specified if so desired. 105 // The close closure may be specified if so desired.
103 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( 106 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure(
104 base::Closure* close_closure) const { 107 base::Closure* close_closure) const {
105 content::PlatformNotificationData notification_data; 108 PlatformNotificationData notification_data;
106 notification_data.title = base::ASCIIToUTF16("My Notification"); 109 notification_data.title = base::ASCIIToUTF16("My Notification");
107 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 110 notification_data.body = base::ASCIIToUTF16("Hello, world!");
108 111
109 MockDesktopNotificationDelegate* delegate = 112 MockDesktopNotificationDelegate* delegate =
110 new MockDesktopNotificationDelegate(); 113 new MockDesktopNotificationDelegate();
111 114
112 service()->DisplayNotification(profile(), 115 service()->DisplayNotification(profile(), GURL("https://chrome.com/"),
113 GURL("https://chrome.com/"), 116 notification_data, NotificationResources(),
114 SkBitmap(), 117 make_scoped_ptr(delegate), close_closure);
115 notification_data,
116 make_scoped_ptr(delegate),
117 close_closure);
118 118
119 return delegate; 119 return delegate;
120 } 120 }
121 121
122 // Returns the Platform Notification Service these unit tests are for. 122 // Returns the Platform Notification Service these unit tests are for.
123 PlatformNotificationServiceImpl* service() const { 123 PlatformNotificationServiceImpl* service() const {
124 return PlatformNotificationServiceImpl::GetInstance(); 124 return PlatformNotificationServiceImpl::GetInstance();
125 } 125 }
126 126
127 // Returns the Profile to be used for these tests. 127 // Returns the Profile to be used for these tests.
(...skipping 28 matching lines...) Expand all
156 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); 156 EXPECT_EQ(0u, ui_manager()->GetNotificationCount());
157 157
158 // Note that we cannot verify whether the closed event was called on the 158 // Note that we cannot verify whether the closed event was called on the
159 // delegate given that it'd result in a use-after-free. 159 // delegate given that it'd result in a use-after-free.
160 } 160 }
161 161
162 // TODO(peter): Re-enable this test when //content is responsible for creating 162 // TODO(peter): Re-enable this test when //content is responsible for creating
163 // the notification delegate ids. 163 // the notification delegate ids.
164 #if !defined(OS_ANDROID) 164 #if !defined(OS_ANDROID)
165 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { 165 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) {
166 content::PlatformNotificationData notification_data; 166 PlatformNotificationData notification_data;
167 notification_data.title = base::ASCIIToUTF16("My notification's title"); 167 notification_data.title = base::ASCIIToUTF16("My notification's title");
168 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 168 notification_data.body = base::ASCIIToUTF16("Hello, world!");
169 169
170 service()->DisplayPersistentNotification( 170 service()->DisplayPersistentNotification(
171 profile(), kPersistentNotificationId, GURL("https://chrome.com/"), 171 profile(), kPersistentNotificationId, GURL("https://chrome.com/"),
172 SkBitmap(), notification_data); 172 notification_data, NotificationResources());
173 173
174 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 174 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
175 175
176 const Notification& notification = ui_manager()->GetNotificationAt(0); 176 const Notification& notification = ui_manager()->GetNotificationAt(0);
177 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 177 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
178 EXPECT_EQ("My notification's title", 178 EXPECT_EQ("My notification's title",
179 base::UTF16ToUTF8(notification.title())); 179 base::UTF16ToUTF8(notification.title()));
180 EXPECT_EQ("Hello, world!", 180 EXPECT_EQ("Hello, world!",
181 base::UTF16ToUTF8(notification.message())); 181 base::UTF16ToUTF8(notification.message()));
182 182
183 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); 183 service()->ClosePersistentNotification(profile(), kPersistentNotificationId);
184 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); 184 EXPECT_EQ(0u, ui_manager()->GetNotificationCount());
185 } 185 }
186 #endif // !defined(OS_ANDROID) 186 #endif // !defined(OS_ANDROID)
187 187
188 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { 188 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) {
189 std::vector<int> vibration_pattern( 189 std::vector<int> vibration_pattern(
190 kNotificationVibrationPattern, 190 kNotificationVibrationPattern,
191 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); 191 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern));
192 192
193 content::PlatformNotificationData notification_data; 193 PlatformNotificationData notification_data;
194 notification_data.title = base::ASCIIToUTF16("My notification's title"); 194 notification_data.title = base::ASCIIToUTF16("My notification's title");
195 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 195 notification_data.body = base::ASCIIToUTF16("Hello, world!");
196 notification_data.vibration_pattern = vibration_pattern; 196 notification_data.vibration_pattern = vibration_pattern;
197 notification_data.silent = true; 197 notification_data.silent = true;
198 198
199 MockDesktopNotificationDelegate* delegate 199 MockDesktopNotificationDelegate* delegate
200 = new MockDesktopNotificationDelegate(); 200 = new MockDesktopNotificationDelegate();
201 service()->DisplayNotification(profile(), 201 service()->DisplayNotification(profile(), GURL("https://chrome.com/"),
202 GURL("https://chrome.com/"), 202 notification_data, NotificationResources(),
203 SkBitmap(), 203 make_scoped_ptr(delegate), nullptr);
204 notification_data,
205 make_scoped_ptr(delegate),
206 nullptr);
207 204
208 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 205 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
209 206
210 const Notification& notification = ui_manager()->GetNotificationAt(0); 207 const Notification& notification = ui_manager()->GetNotificationAt(0);
211 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 208 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
212 EXPECT_EQ("My notification's title", 209 EXPECT_EQ("My notification's title",
213 base::UTF16ToUTF8(notification.title())); 210 base::UTF16ToUTF8(notification.title()));
214 EXPECT_EQ("Hello, world!", 211 EXPECT_EQ("Hello, world!",
215 base::UTF16ToUTF8(notification.message())); 212 base::UTF16ToUTF8(notification.message()));
216 213
217 EXPECT_THAT(notification.vibration_pattern(), 214 EXPECT_THAT(notification.vibration_pattern(),
218 testing::ElementsAreArray(kNotificationVibrationPattern)); 215 testing::ElementsAreArray(kNotificationVibrationPattern));
219 216
220 EXPECT_TRUE(notification.silent()); 217 EXPECT_TRUE(notification.silent());
221 } 218 }
222 219
223 TEST_F(PlatformNotificationServiceTest, DisplayPersistentNotificationMatches) { 220 TEST_F(PlatformNotificationServiceTest, DisplayPersistentNotificationMatches) {
224 std::vector<int> vibration_pattern( 221 std::vector<int> vibration_pattern(
225 kNotificationVibrationPattern, 222 kNotificationVibrationPattern,
226 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); 223 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern));
227 224
228 content::PlatformNotificationData notification_data; 225 PlatformNotificationData notification_data;
229 notification_data.title = base::ASCIIToUTF16("My notification's title"); 226 notification_data.title = base::ASCIIToUTF16("My notification's title");
230 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 227 notification_data.body = base::ASCIIToUTF16("Hello, world!");
231 notification_data.vibration_pattern = vibration_pattern; 228 notification_data.vibration_pattern = vibration_pattern;
232 notification_data.silent = true; 229 notification_data.silent = true;
233 notification_data.actions.resize(2); 230 notification_data.actions.resize(2);
234 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); 231 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1");
235 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); 232 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2");
236 233
237 service()->DisplayPersistentNotification( 234 service()->DisplayPersistentNotification(
238 profile(), 0u /* persistent notification */, GURL("https://chrome.com/"), 235 profile(), 0u /* persistent notification */, GURL("https://chrome.com/"),
239 SkBitmap(), notification_data); 236 notification_data, NotificationResources());
240 237
241 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 238 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
242 239
243 const Notification& notification = ui_manager()->GetNotificationAt(0); 240 const Notification& notification = ui_manager()->GetNotificationAt(0);
244 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); 241 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
245 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); 242 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title()));
246 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); 243 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message()));
247 244
248 EXPECT_THAT(notification.vibration_pattern(), 245 EXPECT_THAT(notification.vibration_pattern(),
249 testing::ElementsAreArray(kNotificationVibrationPattern)); 246 testing::ElementsAreArray(kNotificationVibrationPattern));
(...skipping 16 matching lines...) Expand all
266 263
267 base::Time after_page_notification = 264 base::Time after_page_notification =
268 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( 265 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage(
269 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 266 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
270 EXPECT_GT(after_page_notification, begin_time); 267 EXPECT_GT(after_page_notification, begin_time);
271 268
272 // Ensure that there is at least some time between the two calls. 269 // Ensure that there is at least some time between the two calls.
273 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); 270 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
274 271
275 service()->DisplayPersistentNotification( 272 service()->DisplayPersistentNotification(
276 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), 273 profile(), 42 /* sw_registration_id */, origin,
277 content::PlatformNotificationData()); 274 PlatformNotificationData(), NotificationResources());
278 275
279 base::Time after_persistent_notification = 276 base::Time after_persistent_notification =
280 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( 277 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage(
281 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 278 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
282 EXPECT_GT(after_persistent_notification, after_page_notification); 279 EXPECT_GT(after_persistent_notification, after_page_notification);
283 } 280 }
284 281
285 #if defined(ENABLE_EXTENSIONS) 282 #if defined(ENABLE_EXTENSIONS)
286 283
287 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { 284 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 // Verify that the service indicates that permission has been granted. We only 359 // Verify that the service indicates that permission has been granted. We only
363 // check the UI thread-method for now, as that's the one guarding the behavior 360 // check the UI thread-method for now, as that's the one guarding the behavior
364 // in the browser process. 361 // in the browser process.
365 EXPECT_EQ(blink::WebNotificationPermissionAllowed, 362 EXPECT_EQ(blink::WebNotificationPermissionAllowed,
366 service()->CheckPermissionOnUIThread(profile(), 363 service()->CheckPermissionOnUIThread(profile(),
367 extension->url(), 364 extension->url(),
368 kFakeRenderProcessId)); 365 kFakeRenderProcessId));
369 } 366 }
370 367
371 TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) { 368 TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) {
372 content::PlatformNotificationData notification_data; 369 PlatformNotificationData notification_data;
373 notification_data.title = base::ASCIIToUTF16("My Notification"); 370 notification_data.title = base::ASCIIToUTF16("My Notification");
374 notification_data.body = base::ASCIIToUTF16("Hello, world!"); 371 notification_data.body = base::ASCIIToUTF16("Hello, world!");
375 372
376 Notification notification = service()->CreateNotificationFromData( 373 Notification notification = service()->CreateNotificationFromData(
377 profile(), GURL("https://chrome.com/"), SkBitmap(), notification_data, 374 profile(), GURL("https://chrome.com/"), notification_data,
378 new MockNotificationDelegate("hello")); 375 NotificationResources(), new MockNotificationDelegate("hello"));
379 EXPECT_TRUE(notification.context_message().empty()); 376 EXPECT_TRUE(notification.context_message().empty());
380 377
381 // Create a mocked extension. 378 // Create a mocked extension.
382 scoped_refptr<extensions::Extension> extension = 379 scoped_refptr<extensions::Extension> extension =
383 extensions::ExtensionBuilder() 380 extensions::ExtensionBuilder()
384 .SetID("honijodknafkokifofgiaalefdiedpko") 381 .SetID("honijodknafkokifofgiaalefdiedpko")
385 .SetManifest(std::move(extensions::DictionaryBuilder() 382 .SetManifest(std::move(extensions::DictionaryBuilder()
386 .Set("name", "NotificationTest") 383 .Set("name", "NotificationTest")
387 .Set("version", "1.0") 384 .Set("version", "1.0")
388 .Set("manifest_version", 2) 385 .Set("manifest_version", 2)
389 .Set("description", "Test Extension"))) 386 .Set("description", "Test Extension")))
390 .Build(); 387 .Build();
391 388
392 extensions::ExtensionRegistry* registry = 389 extensions::ExtensionRegistry* registry =
393 extensions::ExtensionRegistry::Get(profile()); 390 extensions::ExtensionRegistry::Get(profile());
394 EXPECT_TRUE(registry->AddEnabled(extension)); 391 EXPECT_TRUE(registry->AddEnabled(extension));
395 392
396 notification = service()->CreateNotificationFromData( 393 notification = service()->CreateNotificationFromData(
397 profile(), 394 profile(),
398 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), 395 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"),
399 SkBitmap(), notification_data, new MockNotificationDelegate("hello")); 396 notification_data, NotificationResources(),
397 new MockNotificationDelegate("hello"));
400 EXPECT_EQ("NotificationTest", 398 EXPECT_EQ("NotificationTest",
401 base::UTF16ToUTF8(notification.context_message())); 399 base::UTF16ToUTF8(notification.context_message()));
402 } 400 }
403 401
404 #endif // defined(ENABLE_EXTENSIONS) 402 #endif // defined(ENABLE_EXTENSIONS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698