OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <deque> | 5 #include <deque> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/run_loop.h" | |
15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
17 #include "base/test/scoped_feature_list.h" | |
18 #include "base/test/simple_test_clock.h" | 16 #include "base/test/simple_test_clock.h" |
19 #include "base/time/clock.h" | 17 #include "base/time/clock.h" |
20 #include "build/build_config.h" | 18 #include "build/build_config.h" |
21 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
24 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
25 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | |
26 #include "chrome/browser/notifications/notification.h" | 22 #include "chrome/browser/notifications/notification.h" |
| 23 #include "chrome/browser/notifications/notification_interactive_uitest_support.h
" |
27 #include "chrome/browser/notifications/notification_test_util.h" | 24 #include "chrome/browser/notifications/notification_test_util.h" |
28 #include "chrome/browser/notifications/web_notification_delegate.h" | |
29 #include "chrome/browser/permissions/permission_request_manager.h" | |
30 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
32 #include "chrome/browser/ui/browser_tabstrip.h" | 27 #include "chrome/browser/ui/browser_tabstrip.h" |
33 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
34 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" | 29 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" |
35 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" | 30 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
36 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
37 #include "chrome/common/chrome_features.h" | |
38 #include "chrome/test/base/in_process_browser_test.h" | |
39 #include "chrome/test/base/interactive_test_utils.h" | 32 #include "chrome/test/base/interactive_test_utils.h" |
40 #include "chrome/test/base/ui_test_utils.h" | 33 #include "chrome/test/base/ui_test_utils.h" |
41 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
42 #include "components/content_settings/core/common/content_settings.h" | |
43 #include "components/content_settings/core/common/content_settings_pattern.h" | 34 #include "components/content_settings/core/common/content_settings_pattern.h" |
44 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
45 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
46 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
47 #include "content/public/browser/render_view_host.h" | 38 #include "content/public/browser/render_view_host.h" |
48 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
49 #include "content/public/test/browser_test_utils.h" | 40 #include "content/public/test/browser_test_utils.h" |
50 #include "content/public/test/test_utils.h" | 41 #include "content/public/test/test_utils.h" |
51 #include "net/test/embedded_test_server/embedded_test_server.h" | 42 #include "net/test/embedded_test_server/embedded_test_server.h" |
52 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 81 |
91 DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker); | 82 DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker); |
92 }; | 83 }; |
93 | 84 |
94 } // namespace | 85 } // namespace |
95 | 86 |
96 namespace { | 87 namespace { |
97 | 88 |
98 const char kExpectedIconUrl[] = "/notifications/no_such_file.png"; | 89 const char kExpectedIconUrl[] = "/notifications/no_such_file.png"; |
99 | 90 |
100 class NotificationChangeObserver { | |
101 public: | |
102 virtual ~NotificationChangeObserver() {} | |
103 virtual bool Wait() = 0; | |
104 }; | |
105 | |
106 class MessageCenterChangeObserver | |
107 : public message_center::MessageCenterObserver, | |
108 public NotificationChangeObserver { | |
109 public: | |
110 MessageCenterChangeObserver() | |
111 : notification_received_(false) { | |
112 message_center::MessageCenter::Get()->AddObserver(this); | |
113 } | |
114 | |
115 ~MessageCenterChangeObserver() override { | |
116 message_center::MessageCenter::Get()->RemoveObserver(this); | |
117 } | |
118 | |
119 // NotificationChangeObserver: | |
120 bool Wait() override { | |
121 if (notification_received_) | |
122 return true; | |
123 | |
124 message_loop_runner_ = new content::MessageLoopRunner; | |
125 message_loop_runner_->Run(); | |
126 return notification_received_; | |
127 } | |
128 | |
129 // message_center::MessageCenterObserver: | |
130 void OnNotificationAdded(const std::string& notification_id) override { | |
131 OnMessageCenterChanged(); | |
132 } | |
133 | |
134 void OnNotificationRemoved(const std::string& notification_id, | |
135 bool by_user) override { | |
136 OnMessageCenterChanged(); | |
137 } | |
138 | |
139 void OnNotificationUpdated(const std::string& notification_id) override { | |
140 OnMessageCenterChanged(); | |
141 } | |
142 | |
143 void OnMessageCenterChanged() { | |
144 notification_received_ = true; | |
145 if (message_loop_runner_.get()) | |
146 message_loop_runner_->Quit(); | |
147 } | |
148 | |
149 bool notification_received_; | |
150 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver); | |
153 }; | |
154 | |
155 // Used to observe the creation of permission prompt without responding. | |
156 class PermissionRequestObserver : public PermissionRequestManager::Observer { | |
157 public: | |
158 explicit PermissionRequestObserver(content::WebContents* web_contents) | |
159 : request_manager_( | |
160 PermissionRequestManager::FromWebContents(web_contents)), | |
161 request_shown_(false), | |
162 message_loop_runner_(new content::MessageLoopRunner) { | |
163 request_manager_->AddObserver(this); | |
164 } | |
165 ~PermissionRequestObserver() override { | |
166 // Safe to remove twice if it happens. | |
167 request_manager_->RemoveObserver(this); | |
168 } | |
169 | |
170 void Wait() { message_loop_runner_->Run(); } | |
171 | |
172 bool request_shown() { return request_shown_; } | |
173 | |
174 private: | |
175 // PermissionRequestManager::Observer | |
176 void OnBubbleAdded() override { | |
177 request_shown_ = true; | |
178 request_manager_->RemoveObserver(this); | |
179 message_loop_runner_->Quit(); | |
180 } | |
181 | |
182 PermissionRequestManager* request_manager_; | |
183 bool request_shown_; | |
184 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
185 | |
186 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | |
187 }; | |
188 | |
189 } // namespace | 91 } // namespace |
190 | 92 |
191 class NotificationsTest : public InProcessBrowserTest { | |
192 public: | |
193 NotificationsTest() {} | |
194 | |
195 protected: | |
196 int GetNotificationCount(); | |
197 int GetNotificationPopupCount(); | |
198 | |
199 void CloseBrowserWindow(Browser* browser); | |
200 void CrashTab(Browser* browser, int index); | |
201 | |
202 void DenyOrigin(const GURL& origin); | |
203 void AllowOrigin(const GURL& origin); | |
204 void AllowAllOrigins(); | |
205 void SetDefaultContentSetting(ContentSetting setting); | |
206 | |
207 std::string CreateNotification(Browser* browser, | |
208 bool wait_for_new_balloon, | |
209 const char* icon, | |
210 const char* title, | |
211 const char* body, | |
212 const char* replace_id); | |
213 std::string CreateSimpleNotification(Browser* browser, | |
214 bool wait_for_new_balloon); | |
215 bool RequestAndAcceptPermission(Browser* browser); | |
216 bool RequestAndDenyPermission(Browser* browser); | |
217 bool RequestAndDismissPermission(Browser* browser); | |
218 bool RequestPermissionAndWait(Browser* browser); | |
219 bool CancelNotification(const char* notification_id, Browser* browser); | |
220 void GetPrefsByContentSetting(ContentSetting setting, | |
221 ContentSettingsForOneType* settings); | |
222 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, | |
223 const GURL& origin); | |
224 | |
225 GURL GetTestPageURLForFile(const std::string& file) const { | |
226 return embedded_test_server()->GetURL( | |
227 std::string("/notifications/") + file); | |
228 } | |
229 | |
230 GURL GetTestPageURL() const { | |
231 return GetTestPageURLForFile("notification_tester.html"); | |
232 } | |
233 | |
234 content::WebContents* GetActiveWebContents(Browser* browser) { | |
235 return browser->tab_strip_model()->GetActiveWebContents(); | |
236 } | |
237 | |
238 protected: | |
239 void EnableFullscreenNotifications() { | |
240 feature_list_.InitWithFeatures({ | |
241 features::kPreferHtmlOverPlugins, | |
242 features::kAllowFullscreenWebNotificationsFeature}, {}); | |
243 } | |
244 | |
245 void DisableFullscreenNotifications() { | |
246 feature_list_.InitWithFeatures( | |
247 {features::kPreferHtmlOverPlugins}, | |
248 {features::kAllowFullscreenWebNotificationsFeature}); | |
249 } | |
250 | |
251 private: | |
252 void DropOriginPreference(const GURL& origin); | |
253 std::string RequestAndRespondToPermission( | |
254 Browser* browser, | |
255 PermissionRequestManager::AutoResponseType bubble_response); | |
256 | |
257 base::test::ScopedFeatureList feature_list_; | |
258 }; | |
259 | |
260 int NotificationsTest::GetNotificationCount() { | |
261 return message_center::MessageCenter::Get()->NotificationCount(); | |
262 } | |
263 | |
264 int NotificationsTest::GetNotificationPopupCount() { | |
265 return message_center::MessageCenter::Get()->GetPopupNotifications().size(); | |
266 } | |
267 | |
268 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | |
269 content::WindowedNotificationObserver observer( | |
270 chrome::NOTIFICATION_BROWSER_CLOSED, | |
271 content::Source<Browser>(browser)); | |
272 browser->window()->Close(); | |
273 observer.Wait(); | |
274 } | |
275 | |
276 void NotificationsTest::CrashTab(Browser* browser, int index) { | |
277 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); | |
278 } | |
279 | |
280 void NotificationsTest::DenyOrigin(const GURL& origin) { | |
281 DropOriginPreference(origin); | |
282 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin); | |
283 } | |
284 | |
285 void NotificationsTest::AllowOrigin(const GURL& origin) { | |
286 DropOriginPreference(origin); | |
287 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin); | |
288 } | |
289 | |
290 void NotificationsTest::AllowAllOrigins() { | |
291 // Reset all origins | |
292 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
293 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
294 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
295 } | |
296 | |
297 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) { | |
298 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
299 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); | |
300 } | |
301 | |
302 std::string NotificationsTest::CreateNotification( | |
303 Browser* browser, | |
304 bool wait_for_new_balloon, | |
305 const char* icon, | |
306 const char* title, | |
307 const char* body, | |
308 const char* replace_id) { | |
309 std::string script = base::StringPrintf( | |
310 "createNotification('%s', '%s', '%s', '%s');", | |
311 icon, title, body, replace_id); | |
312 | |
313 MessageCenterChangeObserver observer; | |
314 std::string result; | |
315 bool success = content::ExecuteScriptAndExtractString( | |
316 GetActiveWebContents(browser), script, &result); | |
317 if (success && result != "-1" && wait_for_new_balloon) | |
318 success = observer.Wait(); | |
319 EXPECT_TRUE(success); | |
320 | |
321 return result; | |
322 } | |
323 | |
324 std::string NotificationsTest::CreateSimpleNotification( | |
325 Browser* browser, | |
326 bool wait_for_new_balloon) { | |
327 return CreateNotification( | |
328 browser, wait_for_new_balloon, | |
329 "no_such_file.png", "My Title", "My Body", ""); | |
330 } | |
331 | |
332 std::string NotificationsTest::RequestAndRespondToPermission( | |
333 Browser* browser, | |
334 PermissionRequestManager::AutoResponseType bubble_response) { | |
335 std::string result; | |
336 content::WebContents* web_contents = GetActiveWebContents(browser); | |
337 PermissionRequestManager::FromWebContents(web_contents) | |
338 ->set_auto_response_for_test(bubble_response); | |
339 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
340 web_contents, "requestPermission();", &result)); | |
341 return result; | |
342 } | |
343 | |
344 bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) { | |
345 std::string result = RequestAndRespondToPermission( | |
346 browser, PermissionRequestManager::ACCEPT_ALL); | |
347 return "request-callback-granted" == result; | |
348 } | |
349 | |
350 bool NotificationsTest::RequestAndDenyPermission(Browser* browser) { | |
351 std::string result = RequestAndRespondToPermission( | |
352 browser, PermissionRequestManager::DENY_ALL); | |
353 return "request-callback-denied" == result; | |
354 } | |
355 | |
356 bool NotificationsTest::RequestAndDismissPermission(Browser* browser) { | |
357 std::string result = | |
358 RequestAndRespondToPermission(browser, PermissionRequestManager::DISMISS); | |
359 return "request-callback-default" == result; | |
360 } | |
361 | |
362 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) { | |
363 content::WebContents* web_contents = GetActiveWebContents(browser); | |
364 ui_test_utils::NavigateToURL(browser, GetTestPageURL()); | |
365 PermissionRequestObserver observer(web_contents); | |
366 std::string result; | |
367 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
368 web_contents, "requestPermissionAndRespond();", &result)); | |
369 EXPECT_EQ("requested", result); | |
370 observer.Wait(); | |
371 return observer.request_shown(); | |
372 } | |
373 | |
374 bool NotificationsTest::CancelNotification( | |
375 const char* notification_id, | |
376 Browser* browser) { | |
377 std::string script = base::StringPrintf( | |
378 "cancelNotification('%s');", | |
379 notification_id); | |
380 | |
381 MessageCenterChangeObserver observer; | |
382 std::string result; | |
383 bool success = content::ExecuteScriptAndExtractString( | |
384 GetActiveWebContents(browser), script, &result); | |
385 if (!success || result != "1") | |
386 return false; | |
387 return observer.Wait(); | |
388 } | |
389 | |
390 void NotificationsTest::GetPrefsByContentSetting( | |
391 ContentSetting setting, | |
392 ContentSettingsForOneType* settings) { | |
393 DesktopNotificationProfileUtil::GetNotificationsSettings( | |
394 browser()->profile(), settings); | |
395 for (ContentSettingsForOneType::iterator it = settings->begin(); | |
396 it != settings->end(); ) { | |
397 if (it->setting != setting || it->source.compare("preference") != 0) | |
398 it = settings->erase(it); | |
399 else | |
400 ++it; | |
401 } | |
402 } | |
403 | |
404 bool NotificationsTest::CheckOriginInSetting( | |
405 const ContentSettingsForOneType& settings, | |
406 const GURL& origin) { | |
407 ContentSettingsPattern pattern = | |
408 ContentSettingsPattern::FromURLNoWildcard(origin); | |
409 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
410 it != settings.end(); ++it) { | |
411 if (it->primary_pattern == pattern) | |
412 return true; | |
413 } | |
414 return false; | |
415 } | |
416 | |
417 void NotificationsTest::DropOriginPreference(const GURL& origin) { | |
418 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), origin); | |
419 } | |
420 | |
421 // Flaky on Windows, Mac, Linux: http://crbug.com/437414. | 93 // Flaky on Windows, Mac, Linux: http://crbug.com/437414. |
422 IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) { | 94 IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) { |
423 ASSERT_TRUE(embedded_test_server()->Start()); | 95 ASSERT_TRUE(embedded_test_server()->Start()); |
424 | 96 |
425 ui_test_utils::NavigateToURL( | 97 ui_test_utils::NavigateToURL( |
426 browser(), | 98 browser(), |
427 embedded_test_server()->GetURL( | 99 embedded_test_server()->GetURL( |
428 "/notifications/notifications_request_function.html")); | 100 "/notifications/notifications_request_function.html")); |
429 | 101 |
430 // Request permission by calling request() while eval'ing an inline script; | 102 // Request permission by calling request() while eval'ing an inline script; |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 browser()->window()->GetNativeWindow())); | 718 browser()->window()->GetNativeWindow())); |
1047 | 719 |
1048 std::string result = CreateSimpleNotification(browser(), true); | 720 std::string result = CreateSimpleNotification(browser(), true); |
1049 EXPECT_NE("-1", result); | 721 EXPECT_NE("-1", result); |
1050 | 722 |
1051 ASSERT_EQ(1, GetNotificationCount()); | 723 ASSERT_EQ(1, GetNotificationCount()); |
1052 message_center::NotificationList::PopupNotifications notifications = | 724 message_center::NotificationList::PopupNotifications notifications = |
1053 message_center::MessageCenter::Get()->GetPopupNotifications(); | 725 message_center::MessageCenter::Get()->GetPopupNotifications(); |
1054 ASSERT_EQ(1u, notifications.size()); | 726 ASSERT_EQ(1u, notifications.size()); |
1055 } | 727 } |
OLD | NEW |