| 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 | |
| 194 // the notification delegate ids. | |
| 195 #if !defined(OS_ANDROID) | |
| 196 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { | 191 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 197 PlatformNotificationData notification_data; | 192 PlatformNotificationData notification_data; |
| 198 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 193 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 199 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 194 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 200 | 195 |
| 201 service()->DisplayPersistentNotification( | 196 service()->DisplayPersistentNotification( |
| 202 profile(), kPersistentNotificationId, GURL() /* service_worker_scope */, | 197 profile(), kNotificationId, GURL() /* service_worker_scope */, |
| 203 GURL("https://chrome.com/"), notification_data, NotificationResources()); | 198 GURL("https://chrome.com/"), notification_data, NotificationResources()); |
| 204 | 199 |
| 205 ASSERT_EQ(1u, GetNotificationCount()); | 200 ASSERT_EQ(1u, GetNotificationCount()); |
| 206 | 201 |
| 207 Notification notification = GetDisplayedNotification(); | 202 Notification notification = GetDisplayedNotification(); |
| 208 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 203 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 209 EXPECT_EQ("My notification's title", | 204 EXPECT_EQ("My notification's title", |
| 210 base::UTF16ToUTF8(notification.title())); | 205 base::UTF16ToUTF8(notification.title())); |
| 211 EXPECT_EQ("Hello, world!", | 206 EXPECT_EQ("Hello, world!", |
| 212 base::UTF16ToUTF8(notification.message())); | 207 base::UTF16ToUTF8(notification.message())); |
| 213 | 208 |
| 214 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); | 209 service()->ClosePersistentNotification(profile(), kNotificationId); |
| 215 EXPECT_EQ(0u, GetNotificationCount()); | 210 EXPECT_EQ(0u, GetNotificationCount()); |
| 216 } | 211 } |
| 217 #endif // !defined(OS_ANDROID) | |
| 218 | 212 |
| 219 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 213 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 220 std::vector<int> vibration_pattern( | 214 std::vector<int> vibration_pattern( |
| 221 kNotificationVibrationPattern, | 215 kNotificationVibrationPattern, |
| 222 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); | 216 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); |
| 223 | 217 |
| 224 PlatformNotificationData notification_data; | 218 PlatformNotificationData notification_data; |
| 225 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 219 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 226 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 220 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 227 notification_data.vibration_pattern = vibration_pattern; | 221 notification_data.vibration_pattern = vibration_pattern; |
| 228 notification_data.silent = true; | 222 notification_data.silent = true; |
| 229 | 223 |
| 230 MockDesktopNotificationDelegate* delegate | 224 MockDesktopNotificationDelegate* delegate |
| 231 = new MockDesktopNotificationDelegate(); | 225 = new MockDesktopNotificationDelegate(); |
| 232 service()->DisplayNotification(profile(), GURL("https://chrome.com/"), | 226 service()->DisplayNotification(profile(), kNotificationId, |
| 233 notification_data, NotificationResources(), | 227 GURL("https://chrome.com/"), notification_data, |
| 228 NotificationResources(), |
| 234 base::WrapUnique(delegate), nullptr); | 229 base::WrapUnique(delegate), nullptr); |
| 235 | 230 |
| 236 ASSERT_EQ(1u, GetNotificationCount()); | 231 ASSERT_EQ(1u, GetNotificationCount()); |
| 237 | 232 |
| 238 Notification notification = GetDisplayedNotification(); | 233 Notification notification = GetDisplayedNotification(); |
| 239 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 234 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 240 EXPECT_EQ("My notification's title", | 235 EXPECT_EQ("My notification's title", |
| 241 base::UTF16ToUTF8(notification.title())); | 236 base::UTF16ToUTF8(notification.title())); |
| 242 EXPECT_EQ("Hello, world!", | 237 EXPECT_EQ("Hello, world!", |
| 243 base::UTF16ToUTF8(notification.message())); | 238 base::UTF16ToUTF8(notification.message())); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 259 notification_data.vibration_pattern = vibration_pattern; | 254 notification_data.vibration_pattern = vibration_pattern; |
| 260 notification_data.silent = true; | 255 notification_data.silent = true; |
| 261 notification_data.actions.resize(2); | 256 notification_data.actions.resize(2); |
| 262 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); | 257 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); |
| 263 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); | 258 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); |
| 264 | 259 |
| 265 NotificationResources notification_resources; | 260 NotificationResources notification_resources; |
| 266 notification_resources.action_icons.resize(notification_data.actions.size()); | 261 notification_resources.action_icons.resize(notification_data.actions.size()); |
| 267 | 262 |
| 268 service()->DisplayPersistentNotification( | 263 service()->DisplayPersistentNotification( |
| 269 profile(), 0u /* persistent notification */, | 264 profile(), kNotificationId, GURL() /* service_worker_scope */, |
| 270 GURL() /* service_worker_scope */, GURL("https://chrome.com/"), | 265 GURL("https://chrome.com/"), notification_data, notification_resources); |
| 271 notification_data, notification_resources); | |
| 272 | 266 |
| 273 ASSERT_EQ(1u, GetNotificationCount()); | 267 ASSERT_EQ(1u, GetNotificationCount()); |
| 274 | 268 |
| 275 Notification notification = GetDisplayedNotification(); | 269 Notification notification = GetDisplayedNotification(); |
| 276 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 270 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 277 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); | 271 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); |
| 278 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); | 272 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); |
| 279 | 273 |
| 280 EXPECT_THAT(notification.vibration_pattern(), | 274 EXPECT_THAT(notification.vibration_pattern(), |
| 281 testing::ElementsAreArray(kNotificationVibrationPattern)); | 275 testing::ElementsAreArray(kNotificationVibrationPattern)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 298 | 292 |
| 299 base::Time after_page_notification = | 293 base::Time after_page_notification = |
| 300 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( | 294 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( |
| 301 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 295 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 302 EXPECT_GT(after_page_notification, begin_time); | 296 EXPECT_GT(after_page_notification, begin_time); |
| 303 | 297 |
| 304 // Ensure that there is at least some time between the two calls. | 298 // Ensure that there is at least some time between the two calls. |
| 305 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 299 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 306 | 300 |
| 307 service()->DisplayPersistentNotification( | 301 service()->DisplayPersistentNotification( |
| 308 profile(), 42 /* sw_registration_id */, GURL() /* service_worker_scope */, | 302 profile(), kNotificationId, GURL() /* service_worker_scope */, origin, |
| 309 origin, PlatformNotificationData(), NotificationResources()); | 303 PlatformNotificationData(), NotificationResources()); |
| 310 | 304 |
| 311 base::Time after_persistent_notification = | 305 base::Time after_persistent_notification = |
| 312 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( | 306 HostContentSettingsMapFactory::GetForProfile(profile())->GetLastUsage( |
| 313 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 307 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 314 EXPECT_GT(after_persistent_notification, after_page_notification); | 308 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 315 } | 309 } |
| 316 | 310 |
| 317 #if defined(ENABLE_EXTENSIONS) | 311 #if defined(ENABLE_EXTENSIONS) |
| 318 | 312 |
| 319 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { | 313 TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 profile(), | 427 profile(), |
| 434 GURL() /* service_worker_scope */, | 428 GURL() /* service_worker_scope */, |
| 435 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), | 429 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), |
| 436 notification_data, NotificationResources(), | 430 notification_data, NotificationResources(), |
| 437 new MockNotificationDelegate("hello")); | 431 new MockNotificationDelegate("hello")); |
| 438 EXPECT_EQ("NotificationTest", | 432 EXPECT_EQ("NotificationTest", |
| 439 base::UTF16ToUTF8(notification.context_message())); | 433 base::UTF16ToUTF8(notification.context_message())); |
| 440 } | 434 } |
| 441 | 435 |
| 442 #endif // defined(ENABLE_EXTENSIONS) | 436 #endif // defined(ENABLE_EXTENSIONS) |
| OLD | NEW |