| 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_test_util.h" | 8 #include "chrome/browser/notifications/notification_test_util.h" |
| 9 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 9 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 74 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 75 | 75 |
| 76 MockDesktopNotificationDelegate* delegate = | 76 MockDesktopNotificationDelegate* delegate = |
| 77 new MockDesktopNotificationDelegate(); | 77 new MockDesktopNotificationDelegate(); |
| 78 | 78 |
| 79 service()->DisplayNotification(profile(), | 79 service()->DisplayNotification(profile(), |
| 80 GURL("https://chrome.com/"), | 80 GURL("https://chrome.com/"), |
| 81 SkBitmap(), | 81 SkBitmap(), |
| 82 notification_data, | 82 notification_data, |
| 83 make_scoped_ptr(delegate), | 83 make_scoped_ptr(delegate), |
| 84 0 /* render_process_id */, | |
| 85 close_closure); | 84 close_closure); |
| 86 | 85 |
| 87 return delegate; | 86 return delegate; |
| 88 } | 87 } |
| 89 | 88 |
| 90 // Returns the Platform Notification Service these unit tests are for. | 89 // Returns the Platform Notification Service these unit tests are for. |
| 91 PlatformNotificationServiceImpl* service() const { | 90 PlatformNotificationServiceImpl* service() const { |
| 92 return PlatformNotificationServiceImpl::GetInstance(); | 91 return PlatformNotificationServiceImpl::GetInstance(); |
| 93 } | 92 } |
| 94 | 93 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 125 | 124 |
| 126 // 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 |
| 127 // delegate given that it'd result in a use-after-free. | 126 // delegate given that it'd result in a use-after-free. |
| 128 } | 127 } |
| 129 | 128 |
| 130 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { | 129 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 131 content::PlatformNotificationData notification_data; | 130 content::PlatformNotificationData notification_data; |
| 132 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 131 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 133 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 132 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 134 | 133 |
| 135 service()->DisplayPersistentNotification(profile(), | 134 service()->DisplayPersistentNotification( |
| 136 42 /* sw_registration_id */, | 135 profile(), 42 /* sw_registration_id */, GURL("https://chrome.com/"), |
| 137 GURL("https://chrome.com/"), | 136 SkBitmap(), notification_data); |
| 138 SkBitmap(), | |
| 139 notification_data, | |
| 140 0 /* render_process_id */); | |
| 141 | 137 |
| 142 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 138 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 143 | 139 |
| 144 const Notification& notification = ui_manager()->GetNotificationAt(0); | 140 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 145 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 141 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 146 EXPECT_EQ("My notification's title", | 142 EXPECT_EQ("My notification's title", |
| 147 base::UTF16ToUTF8(notification.title())); | 143 base::UTF16ToUTF8(notification.title())); |
| 148 EXPECT_EQ("Hello, world!", | 144 EXPECT_EQ("Hello, world!", |
| 149 base::UTF16ToUTF8(notification.message())); | 145 base::UTF16ToUTF8(notification.message())); |
| 150 | 146 |
| 151 service()->ClosePersistentNotification(profile(), | 147 service()->ClosePersistentNotification(profile(), |
| 152 notification.delegate_id()); | 148 notification.delegate_id()); |
| 153 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 149 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 154 } | 150 } |
| 155 | 151 |
| 156 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 152 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 157 content::PlatformNotificationData notification_data; | 153 content::PlatformNotificationData notification_data; |
| 158 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 154 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 159 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 155 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 160 | 156 |
| 161 MockDesktopNotificationDelegate* delegate | 157 MockDesktopNotificationDelegate* delegate |
| 162 = new MockDesktopNotificationDelegate(); | 158 = new MockDesktopNotificationDelegate(); |
| 163 service()->DisplayNotification(profile(), | 159 service()->DisplayNotification(profile(), |
| 164 GURL("https://chrome.com/"), | 160 GURL("https://chrome.com/"), |
| 165 SkBitmap(), | 161 SkBitmap(), |
| 166 notification_data, | 162 notification_data, |
| 167 make_scoped_ptr(delegate), | 163 make_scoped_ptr(delegate), |
| 168 0 /* render_process_id */, | |
| 169 nullptr); | 164 nullptr); |
| 170 | 165 |
| 171 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 166 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 172 | 167 |
| 173 const Notification& notification = ui_manager()->GetNotificationAt(0); | 168 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 174 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 169 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 175 EXPECT_EQ("My notification's title", | 170 EXPECT_EQ("My notification's title", |
| 176 base::UTF16ToUTF8(notification.title())); | 171 base::UTF16ToUTF8(notification.title())); |
| 177 EXPECT_EQ("Hello, world!", | 172 EXPECT_EQ("Hello, world!", |
| 178 base::UTF16ToUTF8(notification.message())); | 173 base::UTF16ToUTF8(notification.message())); |
| 179 } | 174 } |
| 180 | 175 |
| 181 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { | 176 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { |
| 182 base::string16 display_name = | 177 base::string16 display_name = |
| 183 service()->DisplayNameForOriginInProcessId(profile(), | 178 service()->DisplayNameForOrigin(profile(), GURL("https://chrome.com/")); |
| 184 GURL("https://chrome.com/"), | |
| 185 0 /* render_process_id */); | |
| 186 | 179 |
| 187 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); | 180 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); |
| 188 | 181 |
| 189 // TODO(peter): Include unit tests for the extension-name translation | 182 // TODO(peter): Include unit tests for the extension-name translation |
| 190 // functionality of DisplayNameForOriginInProcessId. | 183 // functionality of DisplayNameForOriginInProcessId. |
| 191 } | 184 } |
| 192 | 185 |
| 186 TEST_F(PlatformNotificationServiceTest, TestWebOriginDisplayName) { |
| 187 std::string language("en-us"); |
| 188 |
| 189 GURL https_origin("https://mail.google.com/"); |
| 190 base::string16 expected_display_name = base::ASCIIToUTF16("mail.google.com"); |
| 191 EXPECT_EQ(expected_display_name, |
| 192 PlatformNotificationServiceImpl::WebOriginDisplayName(https_origin, |
| 193 language)); |
| 194 |
| 195 GURL https_origin_standard_port("https://mail.google.com:443/"); |
| 196 expected_display_name = base::ASCIIToUTF16("mail.google.com"); |
| 197 EXPECT_EQ(expected_display_name, |
| 198 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 199 https_origin_standard_port, language)); |
| 200 |
| 201 GURL https_origin_nonstandard_port("https://mail.google.com:444/"); |
| 202 expected_display_name = base::ASCIIToUTF16("mail.google.com:444"); |
| 203 EXPECT_EQ(expected_display_name, |
| 204 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 205 https_origin_nonstandard_port, language)); |
| 206 |
| 207 GURL http_origin("http://mail.google.com/"); |
| 208 expected_display_name = base::ASCIIToUTF16("http://mail.google.com"); |
| 209 EXPECT_EQ(expected_display_name, |
| 210 PlatformNotificationServiceImpl::WebOriginDisplayName(http_origin, |
| 211 language)); |
| 212 |
| 213 GURL http_origin_standard_port("http://mail.google.com:80/"); |
| 214 expected_display_name = base::ASCIIToUTF16("http://mail.google.com"); |
| 215 EXPECT_EQ(expected_display_name, |
| 216 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 217 http_origin_standard_port, language)); |
| 218 |
| 219 GURL http_origin_nonstandard_port("http://mail.google.com:81/"); |
| 220 expected_display_name = base::ASCIIToUTF16("http://mail.google.com:81"); |
| 221 EXPECT_EQ(expected_display_name, |
| 222 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 223 http_origin_nonstandard_port, language)); |
| 224 // TODO(dewittj): Add file origin once it's supported. |
| 225 } |
| 226 |
| 193 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { | 227 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { |
| 194 // Both page and persistent notifications should update the last usage | 228 // Both page and persistent notifications should update the last usage |
| 195 // time of the notification permission for the origin. | 229 // time of the notification permission for the origin. |
| 196 GURL origin("https://chrome.com/"); | 230 GURL origin("https://chrome.com/"); |
| 197 base::Time begin_time; | 231 base::Time begin_time; |
| 198 | 232 |
| 199 CreateSimplePageNotification(); | 233 CreateSimplePageNotification(); |
| 200 | 234 |
| 201 base::Time after_page_notification = | 235 base::Time after_page_notification = |
| 202 profile()->GetHostContentSettingsMap()->GetLastUsage( | 236 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 203 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 237 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 204 EXPECT_GT(after_page_notification, begin_time); | 238 EXPECT_GT(after_page_notification, begin_time); |
| 205 | 239 |
| 206 // Ensure that there is at least some time between the two calls. | 240 // Ensure that there is at least some time between the two calls. |
| 207 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 241 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 208 | 242 |
| 209 service()->DisplayPersistentNotification(profile(), | 243 service()->DisplayPersistentNotification( |
| 210 42 /* sw_registration_id */, | 244 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), |
| 211 origin, | 245 content::PlatformNotificationData()); |
| 212 SkBitmap(), | |
| 213 content::PlatformNotificationData(), | |
| 214 0 /* render_process_id */); | |
| 215 | 246 |
| 216 base::Time after_persistent_notification = | 247 base::Time after_persistent_notification = |
| 217 profile()->GetHostContentSettingsMap()->GetLastUsage( | 248 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 218 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 249 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 219 EXPECT_GT(after_persistent_notification, after_page_notification); | 250 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 220 } | 251 } |
| OLD | NEW |