| 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/time/time.h" |
| 6 #include "chrome/browser/notifications/notification_test_util.h" | 7 #include "chrome/browser/notifications/notification_test_util.h" |
| 7 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 8 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 8 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 9 #include "content/public/browser/desktop_notification_delegate.h" | 11 #include "content/public/browser/desktop_notification_delegate.h" |
| 10 #include "content/public/common/platform_notification_data.h" | 12 #include "content/public/common/platform_notification_data.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 class MockDesktopNotificationDelegate | 19 class MockDesktopNotificationDelegate |
| 18 : public content::DesktopNotificationDelegate { | 20 : public content::DesktopNotificationDelegate { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( | 69 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( |
| 68 base::Closure* close_closure) const { | 70 base::Closure* close_closure) const { |
| 69 content::PlatformNotificationData notification_data; | 71 content::PlatformNotificationData notification_data; |
| 70 notification_data.title = base::ASCIIToUTF16("My Notification"); | 72 notification_data.title = base::ASCIIToUTF16("My Notification"); |
| 71 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 73 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 72 | 74 |
| 73 MockDesktopNotificationDelegate* delegate = | 75 MockDesktopNotificationDelegate* delegate = |
| 74 new MockDesktopNotificationDelegate(); | 76 new MockDesktopNotificationDelegate(); |
| 75 | 77 |
| 76 service()->DisplayNotification(profile(), | 78 service()->DisplayNotification(profile(), |
| 77 GURL("https://example.com/"), | 79 GURL("https://chrome.com/"), |
| 78 SkBitmap(), | 80 SkBitmap(), |
| 79 notification_data, | 81 notification_data, |
| 80 make_scoped_ptr(delegate), | 82 make_scoped_ptr(delegate), |
| 81 0 /* render_process_id */, | 83 0 /* render_process_id */, |
| 82 close_closure); | 84 close_closure); |
| 83 | 85 |
| 84 return delegate; | 86 return delegate; |
| 85 } | 87 } |
| 86 | 88 |
| 87 // Returns the Platform Notification Service these unit tests are for. | 89 // Returns the Platform Notification Service these unit tests are for. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 117 | 119 |
| 118 ASSERT_FALSE(close_closure.is_null()); | 120 ASSERT_FALSE(close_closure.is_null()); |
| 119 close_closure.Run(); | 121 close_closure.Run(); |
| 120 | 122 |
| 121 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 123 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 122 | 124 |
| 123 // Note that we cannot verify whether the closed event was called on the | 125 // Note that we cannot verify whether the closed event was called on the |
| 124 // delegate given that it'd result in a use-after-free. | 126 // delegate given that it'd result in a use-after-free. |
| 125 } | 127 } |
| 126 | 128 |
| 129 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 130 content::PlatformNotificationData notification_data; |
| 131 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 132 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 133 |
| 134 service()->DisplayPersistentNotification(profile(), |
| 135 42 /* sw_registration_id */, |
| 136 GURL("https://chrome.com/"), |
| 137 SkBitmap(), |
| 138 notification_data, |
| 139 0 /* render_process_id */); |
| 140 |
| 141 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 142 |
| 143 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 144 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 145 EXPECT_EQ("My notification's title", |
| 146 base::UTF16ToUTF8(notification.title())); |
| 147 EXPECT_EQ("Hello, world!", |
| 148 base::UTF16ToUTF8(notification.message())); |
| 149 |
| 150 service()->ClosePersistentNotification(profile(), |
| 151 notification.delegate_id()); |
| 152 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 153 } |
| 154 |
| 127 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 155 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 128 content::PlatformNotificationData notification_data; | 156 content::PlatformNotificationData notification_data; |
| 129 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 157 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 130 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 158 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 131 | 159 |
| 132 MockDesktopNotificationDelegate* delegate | 160 MockDesktopNotificationDelegate* delegate |
| 133 = new MockDesktopNotificationDelegate(); | 161 = new MockDesktopNotificationDelegate(); |
| 134 service()->DisplayNotification(profile(), | 162 service()->DisplayNotification(profile(), |
| 135 GURL("https://chrome.com/"), | 163 GURL("https://chrome.com/"), |
| 136 SkBitmap(), | 164 SkBitmap(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 153 base::string16 display_name = | 181 base::string16 display_name = |
| 154 service()->DisplayNameForOriginInProcessId(profile(), | 182 service()->DisplayNameForOriginInProcessId(profile(), |
| 155 GURL("https://chrome.com/"), | 183 GURL("https://chrome.com/"), |
| 156 0 /* render_process_id */); | 184 0 /* render_process_id */); |
| 157 | 185 |
| 158 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); | 186 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); |
| 159 | 187 |
| 160 // TODO(peter): Include unit tests for the extension-name translation | 188 // TODO(peter): Include unit tests for the extension-name translation |
| 161 // functionality of DisplayNameForOriginInProcessId. | 189 // functionality of DisplayNameForOriginInProcessId. |
| 162 } | 190 } |
| 191 |
| 192 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { |
| 193 // Both page and persistent notifications should update the last usage |
| 194 // time of the notification permission for the origin. |
| 195 GURL origin("https://chrome.com/"); |
| 196 base::Time begin_time; |
| 197 |
| 198 CreateSimplePageNotification(); |
| 199 |
| 200 base::Time after_page_notification = |
| 201 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 202 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 203 EXPECT_GT(after_page_notification, begin_time); |
| 204 |
| 205 service()->DisplayPersistentNotification(profile(), |
| 206 42 /* sw_registration_id */, |
| 207 origin, |
| 208 SkBitmap(), |
| 209 content::PlatformNotificationData(), |
| 210 0 /* render_process_id */); |
| 211 |
| 212 base::Time after_persistent_notification = |
| 213 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 214 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 215 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 216 } |
| OLD | NEW |