Chromium Code Reviews| 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 <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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | 47 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
| 48 #include "chrome/browser/chromeos/settings/cros_settings.h" | 48 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 49 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 49 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 50 #endif | 50 #endif |
| 51 | 51 |
| 52 using content::NotificationResources; | 52 using content::NotificationResources; |
| 53 using content::PlatformNotificationData; | 53 using content::PlatformNotificationData; |
| 54 | 54 |
| 55 namespace { | 55 namespace { |
| 56 | 56 |
| 57 const char kNotificationId[] = "my-notification-id"; | |
| 57 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; | 58 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; |
| 58 | 59 |
| 59 #if !defined(OS_ANDROID) | |
| 60 const int64_t kPersistentNotificationId = 42; | |
| 61 #endif | |
| 62 | |
| 63 class MockDesktopNotificationDelegate | 60 class MockDesktopNotificationDelegate |
| 64 : public content::DesktopNotificationDelegate { | 61 : public content::DesktopNotificationDelegate { |
| 65 public: | 62 public: |
| 66 MockDesktopNotificationDelegate() | 63 MockDesktopNotificationDelegate() |
| 67 : displayed_(false), | 64 : displayed_(false), |
| 68 clicked_(false) {} | 65 clicked_(false) {} |
| 69 | 66 |
| 70 ~MockDesktopNotificationDelegate() override {} | 67 ~MockDesktopNotificationDelegate() override {} |
| 71 | 68 |
| 72 // content::DesktopNotificationDelegate implementation. | 69 // content::DesktopNotificationDelegate implementation. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // The close closure may be specified if so desired. | 116 // The close closure may be specified if so desired. |
| 120 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( | 117 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( |
| 121 base::Closure* close_closure) const { | 118 base::Closure* close_closure) const { |
| 122 PlatformNotificationData notification_data; | 119 PlatformNotificationData notification_data; |
| 123 notification_data.title = base::ASCIIToUTF16("My Notification"); | 120 notification_data.title = base::ASCIIToUTF16("My Notification"); |
| 124 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 121 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 125 | 122 |
| 126 MockDesktopNotificationDelegate* delegate = | 123 MockDesktopNotificationDelegate* delegate = |
| 127 new MockDesktopNotificationDelegate(); | 124 new MockDesktopNotificationDelegate(); |
| 128 | 125 |
| 129 service()->DisplayNotification(profile(), GURL("https://chrome.com/"), | 126 service()->DisplayNotification(profile(), kNotificationId, |
| 127 GURL("https://chrome.com/"), | |
| 130 notification_data, NotificationResources(), | 128 notification_data, NotificationResources(), |
| 131 base::WrapUnique(delegate), close_closure); | 129 base::WrapUnique(delegate), close_closure); |
| 132 | 130 |
| 133 return delegate; | 131 return delegate; |
| 134 } | 132 } |
| 135 | 133 |
| 136 // Returns the Platform Notification Service these unit tests are for. | 134 // Returns the Platform Notification Service these unit tests are for. |
| 137 PlatformNotificationServiceImpl* service() const { | 135 PlatformNotificationServiceImpl* service() const { |
| 138 return PlatformNotificationServiceImpl::GetInstance(); | 136 return PlatformNotificationServiceImpl::GetInstance(); |
| 139 } | 137 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 EXPECT_EQ(1u, GetNotificationCount()); | 181 EXPECT_EQ(1u, GetNotificationCount()); |
| 184 | 182 |
| 185 ASSERT_FALSE(close_closure.is_null()); | 183 ASSERT_FALSE(close_closure.is_null()); |
| 186 close_closure.Run(); | 184 close_closure.Run(); |
| 187 | 185 |
| 188 EXPECT_EQ(0u, GetNotificationCount()); | 186 EXPECT_EQ(0u, GetNotificationCount()); |
| 189 // Note that we cannot verify whether the closed event was called on the | 187 // Note that we cannot verify whether the closed event was called on the |
| 190 // delegate given that it'd result in a use-after-free. | 188 // delegate given that it'd result in a use-after-free. |
| 191 } | 189 } |
| 192 | 190 |
| 193 // TODO(peter): Re-enable this test when //content is responsible for creating | 191 // TODO(peter): Re-enable this test when //content is responsible for creating |
|
johnme
2016/09/02 15:07:44
Can you re-enable this test now?
Peter Beverloo
2016/09/05 15:11:01
I have no idea. Let's try!
| |
| 194 // the notification delegate ids. | 192 // the notification delegate ids. |
| 195 #if !defined(OS_ANDROID) | 193 #if !defined(OS_ANDROID) |
| 196 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { | 194 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 197 PlatformNotificationData notification_data; | 195 PlatformNotificationData notification_data; |
| 198 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 196 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 199 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 197 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 200 | 198 |
| 201 service()->DisplayPersistentNotification( | 199 service()->DisplayPersistentNotification( |
| 202 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */, | 200 profile(), kNotificationId, GURL() /* service_worker_scope */, |
| 203 GURL("https://chrome.com/"), notification_data, NotificationResources()); | 201 GURL("https://chrome.com/"), notification_data, NotificationResources()); |
| 204 | 202 |
| 205 ASSERT_EQ(1u, GetNotificationCount()); | 203 ASSERT_EQ(1u, GetNotificationCount()); |
| 206 | 204 |
| 207 Notification notification = GetDisplayedNotification(); | 205 Notification notification = GetDisplayedNotification(); |
| 208 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 206 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 209 EXPECT_EQ("My notification's title", | 207 EXPECT_EQ("My notification's title", |
| 210 base::UTF16ToUTF8(notification.title())); | 208 base::UTF16ToUTF8(notification.title())); |
| 211 EXPECT_EQ("Hello, world!", | 209 EXPECT_EQ("Hello, world!", |
| 212 base::UTF16ToUTF8(notification.message())); | 210 base::UTF16ToUTF8(notification.message())); |
| 213 | 211 |
| 214 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); | 212 service()->ClosePersistentNotification(profile(), kNotificationId); |
| 215 EXPECT_EQ(0u, GetNotificationCount()); | 213 EXPECT_EQ(0u, GetNotificationCount()); |
| 216 } | 214 } |
| 217 #endif // !defined(OS_ANDROID) | 215 #endif // !defined(OS_ANDROID) |
| 218 | 216 |
| 219 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 217 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 220 std::vector<int> vibration_pattern( | 218 std::vector<int> vibration_pattern( |
| 221 kNotificationVibrationPattern, | 219 kNotificationVibrationPattern, |
| 222 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); | 220 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); |
| 223 | 221 |
| 224 PlatformNotificationData notification_data; | 222 PlatformNotificationData notification_data; |
| 225 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 223 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 226 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 224 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 227 notification_data.vibration_pattern = vibration_pattern; | 225 notification_data.vibration_pattern = vibration_pattern; |
| 228 notification_data.silent = true; | 226 notification_data.silent = true; |
| 229 | 227 |
| 230 MockDesktopNotificationDelegate* delegate | 228 MockDesktopNotificationDelegate* delegate |
| 231 = new MockDesktopNotificationDelegate(); | 229 = new MockDesktopNotificationDelegate(); |
| 232 service()->DisplayNotification(profile(), GURL("https://chrome.com/"), | 230 service()->DisplayNotification(profile(), kNotificationId, |
| 233 notification_data, NotificationResources(), | 231 GURL("https://chrome.com/"), notification_data, |
| 232 NotificationResources(), | |
| 234 base::WrapUnique(delegate), nullptr); | 233 base::WrapUnique(delegate), nullptr); |
| 235 | 234 |
| 236 ASSERT_EQ(1u, GetNotificationCount()); | 235 ASSERT_EQ(1u, GetNotificationCount()); |
| 237 | 236 |
| 238 Notification notification = GetDisplayedNotification(); | 237 Notification notification = GetDisplayedNotification(); |
| 239 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 238 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 240 EXPECT_EQ("My notification's title", | 239 EXPECT_EQ("My notification's title", |
| 241 base::UTF16ToUTF8(notification.title())); | 240 base::UTF16ToUTF8(notification.title())); |
| 242 EXPECT_EQ("Hello, world!", | 241 EXPECT_EQ("Hello, world!", |
| 243 base::UTF16ToUTF8(notification.message())); | 242 base::UTF16ToUTF8(notification.message())); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 | 297 |
| 299 base::Time after_page_notification = | 298 base::Time after_page_notification = |
| 300 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( | 299 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( |
| 301 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 300 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 302 EXPECT_GT(after_page_notification, begin_time); | 301 EXPECT_GT(after_page_notification, begin_time); |
| 303 | 302 |
| 304 // Ensure that there is at least some time between the two calls. | 303 // Ensure that there is at least some time between the two calls. |
| 305 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 304 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 306 | 305 |
| 307 service()->DisplayPersistentNotification( | 306 service()->DisplayPersistentNotification( |
| 308 profile(), 42 /* sw_registration_id */, GURL() /* service_worker_scope */, | 307 profile(), kNotificationId, GURL() /* service_worker_scope */, origin, |
| 309 origin, PlatformNotificationData(), NotificationResources()); | 308 PlatformNotificationData(), NotificationResources()); |
| 310 | 309 |
| 311 base::Time after_persistent_notification = | 310 base::Time after_persistent_notification = |
| 312 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( | 311 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( |
| 313 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 312 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 314 EXPECT_GT(after_persistent_notification, after_page_notification); | 313 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 315 } | 314 } |
| 316 | 315 |
| 317 #if defined(ENABLE_EXTENSIONS) | 316 #if defined(ENABLE_EXTENSIONS) |
| 318 | 317 |
| 319 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { | 318 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 profile(), | 432 profile(), |
| 434 GURL() /* service_worker_scope */, | 433 GURL() /* service_worker_scope */, |
| 435 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), | 434 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), |
| 436 notification_data, NotificationResources(), | 435 notification_data, NotificationResources(), |
| 437 new MockNotificationDelegate("hello")); | 436 new MockNotificationDelegate("hello")); |
| 438 EXPECT_EQ("NotificationTest", | 437 EXPECT_EQ("NotificationTest", |
| 439 base::UTF16ToUTF8(notification.context_message())); | 438 base::UTF16ToUTF8(notification.context_message())); |
| 440 } | 439 } |
| 441 | 440 |
| 442 #endif // defined(ENABLE_EXTENSIONS) | 441 #endif // defined(ENABLE_EXTENSIONS) |
| OLD | NEW |