OLD | NEW |
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 "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
6 #include "base/threading/platform_thread.h" | 6 #include "base/threading/platform_thread.h" |
7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "chrome/browser/notifications/notification_delegate.h" |
8 #include "chrome/browser/notifications/notification_test_util.h" | 9 #include "chrome/browser/notifications/notification_test_util.h" |
9 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 10 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
10 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
11 #include "components/content_settings/core/browser/host_content_settings_map.h" | 12 #include "components/content_settings/core/browser/host_content_settings_map.h" |
12 #include "content/public/browser/desktop_notification_delegate.h" | 13 #include "content/public/browser/desktop_notification_delegate.h" |
13 #include "content/public/common/platform_notification_data.h" | 14 #include "content/public/common/platform_notification_data.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 testing::ElementsAreArray(kNotificationVibrationPattern)); | 214 testing::ElementsAreArray(kNotificationVibrationPattern)); |
214 | 215 |
215 EXPECT_TRUE(notification.silent()); | 216 EXPECT_TRUE(notification.silent()); |
216 | 217 |
217 const auto& buttons = notification.buttons(); | 218 const auto& buttons = notification.buttons(); |
218 ASSERT_EQ(2u, buttons.size()); | 219 ASSERT_EQ(2u, buttons.size()); |
219 EXPECT_EQ("Button 1", base::UTF16ToUTF8(buttons[0].title)); | 220 EXPECT_EQ("Button 1", base::UTF16ToUTF8(buttons[0].title)); |
220 EXPECT_EQ("Button 2", base::UTF16ToUTF8(buttons[1].title)); | 221 EXPECT_EQ("Button 2", base::UTF16ToUTF8(buttons[1].title)); |
221 } | 222 } |
222 | 223 |
223 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { | |
224 base::string16 display_name = | |
225 service()->DisplayNameForOrigin(profile(), GURL("https://chrome.com/")); | |
226 | |
227 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); | |
228 | |
229 // TODO(peter): Include unit tests for the extension-name translation | |
230 // functionality of DisplayNameForOriginInProcessId. | |
231 } | |
232 | |
233 TEST_F(PlatformNotificationServiceTest, TestWebOriginDisplayName) { | |
234 std::string language("en-us"); | |
235 | |
236 GURL https_origin("https://mail.google.com/"); | |
237 base::string16 expected_display_name = base::ASCIIToUTF16("mail.google.com"); | |
238 EXPECT_EQ(expected_display_name, | |
239 PlatformNotificationServiceImpl::WebOriginDisplayName(https_origin, | |
240 language)); | |
241 | |
242 GURL https_origin_standard_port("https://mail.google.com:443/"); | |
243 expected_display_name = base::ASCIIToUTF16("mail.google.com"); | |
244 EXPECT_EQ(expected_display_name, | |
245 PlatformNotificationServiceImpl::WebOriginDisplayName( | |
246 https_origin_standard_port, language)); | |
247 | |
248 GURL https_origin_nonstandard_port("https://mail.google.com:444/"); | |
249 expected_display_name = base::ASCIIToUTF16("mail.google.com:444"); | |
250 EXPECT_EQ(expected_display_name, | |
251 PlatformNotificationServiceImpl::WebOriginDisplayName( | |
252 https_origin_nonstandard_port, language)); | |
253 | |
254 GURL http_origin("http://mail.google.com/"); | |
255 expected_display_name = base::ASCIIToUTF16("http://mail.google.com"); | |
256 EXPECT_EQ(expected_display_name, | |
257 PlatformNotificationServiceImpl::WebOriginDisplayName(http_origin, | |
258 language)); | |
259 | |
260 GURL http_origin_standard_port("http://mail.google.com:80/"); | |
261 expected_display_name = base::ASCIIToUTF16("http://mail.google.com"); | |
262 EXPECT_EQ(expected_display_name, | |
263 PlatformNotificationServiceImpl::WebOriginDisplayName( | |
264 http_origin_standard_port, language)); | |
265 | |
266 GURL http_origin_nonstandard_port("http://mail.google.com:81/"); | |
267 expected_display_name = base::ASCIIToUTF16("http://mail.google.com:81"); | |
268 EXPECT_EQ(expected_display_name, | |
269 PlatformNotificationServiceImpl::WebOriginDisplayName( | |
270 http_origin_nonstandard_port, language)); | |
271 // TODO(dewittj): Add file origin once it's supported. | |
272 } | |
273 | |
274 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { | 224 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { |
275 // Both page and persistent notifications should update the last usage | 225 // Both page and persistent notifications should update the last usage |
276 // time of the notification permission for the origin. | 226 // time of the notification permission for the origin. |
277 GURL origin("https://chrome.com/"); | 227 GURL origin("https://chrome.com/"); |
278 base::Time begin_time; | 228 base::Time begin_time; |
279 | 229 |
280 CreateSimplePageNotification(); | 230 CreateSimplePageNotification(); |
281 | 231 |
282 base::Time after_page_notification = | 232 base::Time after_page_notification = |
283 profile()->GetHostContentSettingsMap()->GetLastUsage( | 233 profile()->GetHostContentSettingsMap()->GetLastUsage( |
284 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 234 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
285 EXPECT_GT(after_page_notification, begin_time); | 235 EXPECT_GT(after_page_notification, begin_time); |
286 | 236 |
287 // Ensure that there is at least some time between the two calls. | 237 // Ensure that there is at least some time between the two calls. |
288 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 238 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
289 | 239 |
290 service()->DisplayPersistentNotification( | 240 service()->DisplayPersistentNotification( |
291 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), | 241 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), |
292 content::PlatformNotificationData()); | 242 content::PlatformNotificationData()); |
293 | 243 |
294 base::Time after_persistent_notification = | 244 base::Time after_persistent_notification = |
295 profile()->GetHostContentSettingsMap()->GetLastUsage( | 245 profile()->GetHostContentSettingsMap()->GetLastUsage( |
296 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 246 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
297 EXPECT_GT(after_persistent_notification, after_page_notification); | 247 EXPECT_GT(after_persistent_notification, after_page_notification); |
298 } | 248 } |
299 | 249 |
300 #if defined(ENABLE_EXTENSIONS) | 250 #if defined(ENABLE_EXTENSIONS) |
301 | 251 |
| 252 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { |
| 253 base::string16 display_name = service()->DisplayNameForContextMessage( |
| 254 profile(), GURL("https://chrome.com/")); |
| 255 |
| 256 EXPECT_TRUE(display_name.empty()); |
| 257 |
| 258 // Create a mocked extension. |
| 259 scoped_refptr<extensions::Extension> extension = |
| 260 extensions::ExtensionBuilder() |
| 261 .SetID("honijodknafkokifofgiaalefdiedpko") |
| 262 .SetManifest(extensions::DictionaryBuilder() |
| 263 .Set("name", "NotificationTest") |
| 264 .Set("version", "1.0") |
| 265 .Set("manifest_version", 2) |
| 266 .Set("description", "Test Extension")) |
| 267 .Build(); |
| 268 |
| 269 extensions::ExtensionRegistry* registry = |
| 270 extensions::ExtensionRegistry::Get(profile()); |
| 271 EXPECT_TRUE(registry->AddEnabled(extension)); |
| 272 |
| 273 display_name = service()->DisplayNameForContextMessage( |
| 274 profile(), |
| 275 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html")); |
| 276 EXPECT_EQ("NotificationTest", base::UTF16ToUTF8(display_name)); |
| 277 } |
| 278 |
302 TEST_F(PlatformNotificationServiceTest, ExtensionPermissionChecks) { | 279 TEST_F(PlatformNotificationServiceTest, ExtensionPermissionChecks) { |
303 #if defined(OS_CHROMEOS) | 280 #if defined(OS_CHROMEOS) |
304 // The ExtensionService on Chrome OS requires these objects to be initialized. | 281 // The ExtensionService on Chrome OS requires these objects to be initialized. |
305 chromeos::ScopedTestDeviceSettingsService test_device_settings_service; | 282 chromeos::ScopedTestDeviceSettingsService test_device_settings_service; |
306 chromeos::ScopedTestCrosSettings test_cros_settings; | 283 chromeos::ScopedTestCrosSettings test_cros_settings; |
307 chromeos::ScopedTestUserManager test_user_manager; | 284 chromeos::ScopedTestUserManager test_user_manager; |
308 #endif | 285 #endif |
309 | 286 |
310 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 287 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
311 extensions::TestExtensionSystem* test_extension_system = | 288 extensions::TestExtensionSystem* test_extension_system = |
312 static_cast<extensions::TestExtensionSystem*>( | 289 static_cast<extensions::TestExtensionSystem*>( |
313 extensions::ExtensionSystem::Get(profile())); | 290 extensions::ExtensionSystem::Get(profile())); |
314 | 291 |
315 ExtensionService* extension_service = | 292 ExtensionService* extension_service = |
316 test_extension_system->CreateExtensionService( | 293 test_extension_system->CreateExtensionService( |
317 &command_line, base::FilePath() /* install_directory */, | 294 &command_line, base::FilePath() /* install_directory */, |
318 false /* autoupdate_enabled*/); | 295 false /* autoupdate_enabled */); |
319 | 296 |
320 // Create a mocked extension that has the notifications API permission. | 297 // Create a mocked extension that has the notifications API permission. |
321 scoped_refptr<extensions::Extension> extension = | 298 scoped_refptr<extensions::Extension> extension = |
322 extensions::ExtensionBuilder().SetManifest( | 299 extensions::ExtensionBuilder() |
323 extensions::DictionaryBuilder() | 300 .SetManifest(extensions::DictionaryBuilder() |
324 .Set("name", "NotificationTest") | 301 .Set("name", "NotificationTest") |
325 .Set("version", "1.0") | 302 .Set("version", "1.0") |
326 .Set("manifest_version", 2) | 303 .Set("manifest_version", 2) |
327 .Set("description", "Test Extension") | 304 .Set("description", "Test Extension") |
328 .Set("permissions", | 305 .Set("permissions", extensions::ListBuilder().Append( |
329 extensions::ListBuilder().Append("notifications"))).Build(); | 306 "notifications"))) |
| 307 .Build(); |
330 | 308 |
331 // Install the extension on the faked extension service, and verify that it | 309 // Install the extension on the faked extension service, and verify that it |
332 // has been added to the extension registry successfully. | 310 // has been added to the extension registry successfully. |
333 extension_service->AddExtension(extension.get()); | 311 extension_service->AddExtension(extension.get()); |
334 extensions::ExtensionRegistry* registry = | 312 extensions::ExtensionRegistry* registry = |
335 extensions::ExtensionRegistry::Get(profile()); | 313 extensions::ExtensionRegistry::Get(profile()); |
336 | 314 |
337 ASSERT_TRUE(registry->GetExtensionById( | 315 ASSERT_TRUE(registry->GetExtensionById( |
338 extension->id(), extensions::ExtensionRegistry::ENABLED)); | 316 extension->id(), extensions::ExtensionRegistry::ENABLED)); |
339 | 317 |
340 const int kFakeRenderProcessId = 42; | 318 const int kFakeRenderProcessId = 42; |
341 | 319 |
342 // Mock that the extension is running in a fake render process id. | 320 // Mock that the extension is running in a fake render process id. |
343 extensions::ProcessMap::Get(profile())->Insert(extension->id(), | 321 extensions::ProcessMap::Get(profile())->Insert(extension->id(), |
344 kFakeRenderProcessId, | 322 kFakeRenderProcessId, |
345 -1 /* site_instance_id */); | 323 -1 /* site_instance_id */); |
346 | 324 |
347 // Verify that the service indicates that permission has been granted. We only | 325 // Verify that the service indicates that permission has been granted. We only |
348 // check the UI thread-method for now, as that's the one guarding the behavior | 326 // check the UI thread-method for now, as that's the one guarding the behavior |
349 // in the browser process. | 327 // in the browser process. |
350 EXPECT_EQ(blink::WebNotificationPermissionAllowed, | 328 EXPECT_EQ(blink::WebNotificationPermissionAllowed, |
351 service()->CheckPermissionOnUIThread(profile(), | 329 service()->CheckPermissionOnUIThread(profile(), |
352 extension->url(), | 330 extension->url(), |
353 kFakeRenderProcessId)); | 331 kFakeRenderProcessId)); |
354 } | 332 } |
355 | 333 |
| 334 TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) { |
| 335 content::PlatformNotificationData notification_data; |
| 336 notification_data.title = base::ASCIIToUTF16("My Notification"); |
| 337 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 338 |
| 339 Notification notification = service()->CreateNotificationFromData( |
| 340 profile(), GURL("https://chrome.com/"), SkBitmap(), notification_data, |
| 341 new MockNotificationDelegate("hello")); |
| 342 EXPECT_TRUE(notification.context_message().empty()); |
| 343 |
| 344 // Create a mocked extension. |
| 345 scoped_refptr<extensions::Extension> extension = |
| 346 extensions::ExtensionBuilder() |
| 347 .SetID("honijodknafkokifofgiaalefdiedpko") |
| 348 .SetManifest(extensions::DictionaryBuilder() |
| 349 .Set("name", "NotificationTest") |
| 350 .Set("version", "1.0") |
| 351 .Set("manifest_version", 2) |
| 352 .Set("description", "Test Extension")) |
| 353 .Build(); |
| 354 |
| 355 extensions::ExtensionRegistry* registry = |
| 356 extensions::ExtensionRegistry::Get(profile()); |
| 357 EXPECT_TRUE(registry->AddEnabled(extension)); |
| 358 |
| 359 notification = service()->CreateNotificationFromData( |
| 360 profile(), |
| 361 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), |
| 362 SkBitmap(), notification_data, new MockNotificationDelegate("hello")); |
| 363 EXPECT_EQ("NotificationTest", |
| 364 base::UTF16ToUTF8(notification.context_message())); |
| 365 } |
| 366 |
356 #endif // defined(ENABLE_EXTENSIONS) | 367 #endif // defined(ENABLE_EXTENSIONS) |
OLD | NEW |