OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <deque> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/callback.h" | |
10 #include "base/command_line.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/strings/stringprintf.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "base/test/simple_test_clock.h" | |
18 #include "base/time/clock.h" | |
19 #include "build/build_config.h" | |
20 #include "chrome/browser/browser_process.h" | |
21 #include "chrome/browser/chrome_notification_types.h" | |
22 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
23 #include "chrome/browser/infobars/infobar_service.h" | |
24 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | |
25 #include "chrome/browser/notifications/notification.h" | |
26 #include "chrome/browser/profiles/profile.h" | |
27 #include "chrome/browser/ui/browser.h" | |
28 #include "chrome/browser/ui/browser_tabstrip.h" | |
29 #include "chrome/browser/ui/browser_window.h" | |
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
31 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | |
32 #include "chrome/test/base/in_process_browser_test.h" | |
33 #include "chrome/test/base/ui_test_utils.h" | |
34 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
35 #include "components/content_settings/core/common/content_settings.h" | |
36 #include "components/content_settings/core/common/content_settings_pattern.h" | |
37 #include "content/public/browser/notification_service.h" | |
38 #include "content/public/browser/notification_source.h" | |
39 #include "content/public/browser/notification_types.h" | |
40 #include "content/public/browser/render_view_host.h" | |
41 #include "content/public/browser/web_contents.h" | |
42 #include "content/public/test/browser_test_utils.h" | |
43 #include "content/public/test/test_utils.h" | |
44 #include "net/test/embedded_test_server/embedded_test_server.h" | |
45 #include "testing/gtest/include/gtest/gtest.h" | |
46 #include "ui/base/window_open_disposition.h" | |
47 #include "ui/message_center/message_center.h" | |
48 #include "ui/message_center/message_center_observer.h" | |
49 #include "ui/message_center/message_center_style.h" | |
50 #include "ui/message_center/notification_blocker.h" | |
51 #include "url/gurl.h" | |
52 | |
53 namespace { | |
54 | |
55 class ToggledNotificationBlocker : public message_center::NotificationBlocker { | |
56 public: | |
57 ToggledNotificationBlocker() | |
58 : message_center::NotificationBlocker( | |
59 message_center::MessageCenter::Get()), | |
60 notifications_enabled_(true) {} | |
61 ~ToggledNotificationBlocker() override {} | |
62 | |
63 void SetNotificationsEnabled(bool enabled) { | |
64 if (notifications_enabled_ != enabled) { | |
65 notifications_enabled_ = enabled; | |
66 NotifyBlockingStateChanged(); | |
67 } | |
68 } | |
69 | |
70 // NotificationBlocker overrides: | |
71 bool ShouldShowNotificationAsPopup( | |
72 const message_center::NotifierId& notifier_id) const override { | |
73 return notifications_enabled_; | |
74 } | |
75 | |
76 private: | |
77 bool notifications_enabled_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker); | |
80 }; | |
81 | |
82 } // namespace | |
83 | |
84 namespace { | |
85 | |
86 const char kExpectedIconUrl[] = "/notifications/no_such_file.png"; | |
87 | |
88 class NotificationChangeObserver { | |
89 public: | |
90 virtual ~NotificationChangeObserver() {} | |
91 virtual bool Wait() = 0; | |
92 }; | |
93 | |
94 class MessageCenterChangeObserver | |
95 : public message_center::MessageCenterObserver, | |
96 public NotificationChangeObserver { | |
97 public: | |
98 MessageCenterChangeObserver() | |
99 : notification_received_(false) { | |
100 message_center::MessageCenter::Get()->AddObserver(this); | |
101 } | |
102 | |
103 ~MessageCenterChangeObserver() override { | |
104 message_center::MessageCenter::Get()->RemoveObserver(this); | |
105 } | |
106 | |
107 // NotificationChangeObserver: | |
108 bool Wait() override { | |
109 if (notification_received_) | |
110 return true; | |
111 | |
112 message_loop_runner_ = new content::MessageLoopRunner; | |
113 message_loop_runner_->Run(); | |
114 return notification_received_; | |
115 } | |
116 | |
117 // message_center::MessageCenterObserver: | |
118 void OnNotificationAdded(const std::string& notification_id) override { | |
119 OnMessageCenterChanged(); | |
120 } | |
121 | |
122 void OnNotificationRemoved(const std::string& notification_id, | |
123 bool by_user) override { | |
124 OnMessageCenterChanged(); | |
125 } | |
126 | |
127 void OnNotificationUpdated(const std::string& notification_id) override { | |
128 OnMessageCenterChanged(); | |
129 } | |
130 | |
131 void OnMessageCenterChanged() { | |
132 notification_received_ = true; | |
133 if (message_loop_runner_.get()) | |
134 message_loop_runner_->Quit(); | |
135 } | |
136 | |
137 bool notification_received_; | |
138 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver); | |
141 }; | |
142 | |
143 // Used to observe the creation of permission prompt without responding. | |
144 class PermissionRequestObserver : public PermissionBubbleManager::Observer { | |
145 public: | |
146 explicit PermissionRequestObserver(content::WebContents* web_contents) | |
147 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)), | |
148 request_shown_(false), | |
149 message_loop_runner_(new content::MessageLoopRunner) { | |
150 bubble_manager_->AddObserver(this); | |
151 } | |
152 ~PermissionRequestObserver() override { | |
153 // Safe to remove twice if it happens. | |
154 bubble_manager_->RemoveObserver(this); | |
155 } | |
156 | |
157 void Wait() { message_loop_runner_->Run(); } | |
158 | |
159 bool request_shown() { return request_shown_; } | |
160 | |
161 private: | |
162 // PermissionBubbleManager::Observer | |
163 void OnBubbleAdded() override { | |
164 request_shown_ = true; | |
165 bubble_manager_->RemoveObserver(this); | |
166 message_loop_runner_->Quit(); | |
167 } | |
168 | |
169 PermissionBubbleManager* bubble_manager_; | |
170 bool request_shown_; | |
171 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
172 | |
173 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | |
174 }; | |
175 | |
176 } // namespace | |
177 | |
178 class NotificationsTest : public InProcessBrowserTest { | |
179 public: | |
180 NotificationsTest() {} | |
181 | |
182 protected: | |
183 int GetNotificationCount(); | |
184 int GetNotificationPopupCount(); | |
185 | |
186 void CloseBrowserWindow(Browser* browser); | |
187 void CrashTab(Browser* browser, int index); | |
188 | |
189 void DenyOrigin(const GURL& origin); | |
190 void AllowOrigin(const GURL& origin); | |
191 void AllowAllOrigins(); | |
192 void SetDefaultContentSetting(ContentSetting setting); | |
193 | |
194 std::string CreateNotification(Browser* browser, | |
195 bool wait_for_new_balloon, | |
196 const char* icon, | |
197 const char* title, | |
198 const char* body, | |
199 const char* replace_id); | |
200 std::string CreateSimpleNotification(Browser* browser, | |
201 bool wait_for_new_balloon); | |
202 bool RequestAndAcceptPermission(Browser* browser); | |
203 bool RequestAndDenyPermission(Browser* browser); | |
204 bool RequestAndDismissPermission(Browser* browser); | |
205 bool RequestPermissionAndWait(Browser* browser); | |
206 bool CancelNotification(const char* notification_id, Browser* browser); | |
207 void GetPrefsByContentSetting(ContentSetting setting, | |
208 ContentSettingsForOneType* settings); | |
209 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, | |
210 const GURL& origin); | |
211 | |
212 GURL GetTestPageURLForFile(const std::string& file) const { | |
213 return embedded_test_server()->GetURL( | |
214 std::string("/notifications/") + file); | |
215 } | |
216 | |
217 GURL GetTestPageURL() const { | |
218 return GetTestPageURLForFile("notification_tester.html"); | |
219 } | |
220 | |
221 content::WebContents* GetActiveWebContents(Browser* browser) { | |
222 return browser->tab_strip_model()->GetActiveWebContents(); | |
223 } | |
224 | |
225 private: | |
226 void DropOriginPreference(const GURL& origin); | |
227 std::string RequestAndRespondToPermission( | |
228 Browser* browser, | |
229 PermissionBubbleManager::AutoResponseType bubble_response); | |
230 }; | |
231 | |
232 int NotificationsTest::GetNotificationCount() { | |
233 return message_center::MessageCenter::Get()->NotificationCount(); | |
234 } | |
235 | |
236 int NotificationsTest::GetNotificationPopupCount() { | |
237 return message_center::MessageCenter::Get()->GetPopupNotifications().size(); | |
238 } | |
239 | |
240 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | |
241 content::WindowedNotificationObserver observer( | |
242 chrome::NOTIFICATION_BROWSER_CLOSED, | |
243 content::Source<Browser>(browser)); | |
244 browser->window()->Close(); | |
245 observer.Wait(); | |
246 } | |
247 | |
248 void NotificationsTest::CrashTab(Browser* browser, int index) { | |
249 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); | |
250 } | |
251 | |
252 void NotificationsTest::DenyOrigin(const GURL& origin) { | |
253 DropOriginPreference(origin); | |
254 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin); | |
255 } | |
256 | |
257 void NotificationsTest::AllowOrigin(const GURL& origin) { | |
258 DropOriginPreference(origin); | |
259 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin); | |
260 } | |
261 | |
262 void NotificationsTest::AllowAllOrigins() { | |
263 // Reset all origins | |
264 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
265 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
266 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
267 } | |
268 | |
269 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) { | |
270 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
271 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); | |
272 } | |
273 | |
274 std::string NotificationsTest::CreateNotification( | |
275 Browser* browser, | |
276 bool wait_for_new_balloon, | |
277 const char* icon, | |
278 const char* title, | |
279 const char* body, | |
280 const char* replace_id) { | |
281 std::string script = base::StringPrintf( | |
282 "createNotification('%s', '%s', '%s', '%s');", | |
283 icon, title, body, replace_id); | |
284 | |
285 MessageCenterChangeObserver observer; | |
286 std::string result; | |
287 bool success = content::ExecuteScriptAndExtractString( | |
288 GetActiveWebContents(browser), script, &result); | |
289 if (success && result != "-1" && wait_for_new_balloon) | |
290 success = observer.Wait(); | |
291 EXPECT_TRUE(success); | |
292 | |
293 return result; | |
294 } | |
295 | |
296 std::string NotificationsTest::CreateSimpleNotification( | |
297 Browser* browser, | |
298 bool wait_for_new_balloon) { | |
299 return CreateNotification( | |
300 browser, wait_for_new_balloon, | |
301 "no_such_file.png", "My Title", "My Body", ""); | |
302 } | |
303 | |
304 std::string NotificationsTest::RequestAndRespondToPermission( | |
305 Browser* browser, | |
306 PermissionBubbleManager::AutoResponseType bubble_response) { | |
307 std::string result; | |
308 content::WebContents* web_contents = GetActiveWebContents(browser); | |
309 PermissionBubbleManager::FromWebContents(web_contents) | |
310 ->set_auto_response_for_test(bubble_response); | |
311 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
312 web_contents, "requestPermission();", &result)); | |
313 return result; | |
314 } | |
315 | |
316 bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) { | |
317 std::string result = RequestAndRespondToPermission( | |
318 browser, PermissionBubbleManager::ACCEPT_ALL); | |
319 return "request-callback-granted" == result; | |
320 } | |
321 | |
322 bool NotificationsTest::RequestAndDenyPermission(Browser* browser) { | |
323 std::string result = | |
324 RequestAndRespondToPermission(browser, PermissionBubbleManager::DENY_ALL); | |
325 return "request-callback-denied" == result; | |
326 } | |
327 | |
328 bool NotificationsTest::RequestAndDismissPermission(Browser* browser) { | |
329 std::string result = | |
330 RequestAndRespondToPermission(browser, PermissionBubbleManager::DISMISS); | |
331 return "request-callback-default" == result; | |
332 } | |
333 | |
334 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) { | |
335 content::WebContents* web_contents = GetActiveWebContents(browser); | |
336 ui_test_utils::NavigateToURL(browser, GetTestPageURL()); | |
337 PermissionRequestObserver observer(web_contents); | |
338 std::string result; | |
339 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
340 web_contents, "requestPermissionAndRespond();", &result)); | |
341 EXPECT_EQ("requested", result); | |
342 observer.Wait(); | |
343 return observer.request_shown(); | |
344 } | |
345 | |
346 bool NotificationsTest::CancelNotification( | |
347 const char* notification_id, | |
348 Browser* browser) { | |
349 std::string script = base::StringPrintf( | |
350 "cancelNotification('%s');", | |
351 notification_id); | |
352 | |
353 MessageCenterChangeObserver observer; | |
354 std::string result; | |
355 bool success = content::ExecuteScriptAndExtractString( | |
356 GetActiveWebContents(browser), script, &result); | |
357 if (!success || result != "1") | |
358 return false; | |
359 return observer.Wait(); | |
360 } | |
361 | |
362 void NotificationsTest::GetPrefsByContentSetting( | |
363 ContentSetting setting, | |
364 ContentSettingsForOneType* settings) { | |
365 DesktopNotificationProfileUtil::GetNotificationsSettings( | |
366 browser()->profile(), settings); | |
367 for (ContentSettingsForOneType::iterator it = settings->begin(); | |
368 it != settings->end(); ) { | |
369 if (it->setting != setting || it->source.compare("preference") != 0) | |
370 it = settings->erase(it); | |
371 else | |
372 ++it; | |
373 } | |
374 } | |
375 | |
376 bool NotificationsTest::CheckOriginInSetting( | |
377 const ContentSettingsForOneType& settings, | |
378 const GURL& origin) { | |
379 ContentSettingsPattern pattern = | |
380 ContentSettingsPattern::FromURLNoWildcard(origin); | |
381 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
382 it != settings.end(); ++it) { | |
383 if (it->primary_pattern == pattern) | |
384 return true; | |
385 } | |
386 return false; | |
387 } | |
388 | |
389 void NotificationsTest::DropOriginPreference(const GURL& origin) { | |
390 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), origin); | |
391 } | |
392 | |
393 // Flaky on Windows, Mac, Linux: http://crbug.com/437414. | |
394 IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) { | |
395 ASSERT_TRUE(embedded_test_server()->Start()); | |
396 | |
397 ui_test_utils::NavigateToURL( | |
398 browser(), | |
399 embedded_test_server()->GetURL( | |
400 "/notifications/notifications_request_function.html")); | |
401 | |
402 // Request permission by calling request() while eval'ing an inline script; | |
403 // That's considered a user gesture to webkit, and should produce an infobar. | |
404 bool result; | |
405 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
406 GetActiveWebContents(browser()), | |
407 "window.domAutomationController.send(request());", &result)); | |
408 EXPECT_TRUE(result); | |
409 | |
410 InfoBarService* infobar_service = InfoBarService::FromWebContents( | |
411 browser()->tab_strip_model()->GetWebContentsAt(0)); | |
412 EXPECT_EQ(1U, infobar_service->infobar_count()); | |
413 } | |
414 | |
415 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) { | |
416 ASSERT_TRUE(embedded_test_server()->Start()); | |
417 | |
418 // Creates a simple notification. | |
419 AllowAllOrigins(); | |
420 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
421 | |
422 std::string result = CreateSimpleNotification(browser(), true); | |
423 EXPECT_NE("-1", result); | |
424 | |
425 GURL EXPECTED_ICON_URL = embedded_test_server()->GetURL(kExpectedIconUrl); | |
426 ASSERT_EQ(1, GetNotificationCount()); | |
427 message_center::NotificationList::Notifications notifications = | |
428 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
429 EXPECT_EQ(base::ASCIIToUTF16("My Title"), | |
430 (*notifications.rbegin())->title()); | |
431 EXPECT_EQ(base::ASCIIToUTF16("My Body"), | |
432 (*notifications.rbegin())->message()); | |
433 } | |
434 | |
435 IN_PROC_BROWSER_TEST_F(NotificationsTest, NotificationBlockerTest) { | |
436 ToggledNotificationBlocker blocker; | |
437 | |
438 ASSERT_TRUE(embedded_test_server()->Start()); | |
439 | |
440 // Creates a simple notification. | |
441 AllowAllOrigins(); | |
442 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
443 | |
444 std::string result = CreateSimpleNotification(browser(), true); | |
445 EXPECT_NE("-1", result); | |
446 result = CreateSimpleNotification(browser(), true); | |
447 EXPECT_NE("-1", result); | |
448 | |
449 blocker.SetNotificationsEnabled(false); | |
450 EXPECT_EQ(0, GetNotificationPopupCount()); | |
451 } | |
452 | |
453 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) { | |
454 ASSERT_TRUE(embedded_test_server()->Start()); | |
455 | |
456 // Creates a notification and closes it. | |
457 AllowAllOrigins(); | |
458 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
459 | |
460 std::string result = CreateSimpleNotification(browser(), true); | |
461 EXPECT_NE("-1", result); | |
462 ASSERT_EQ(1, GetNotificationCount()); | |
463 | |
464 message_center::NotificationList::Notifications notifications = | |
465 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
466 message_center::MessageCenter::Get()->RemoveNotification( | |
467 (*notifications.rbegin())->id(), | |
468 true); // by_user | |
469 | |
470 ASSERT_EQ(0, GetNotificationCount()); | |
471 } | |
472 | |
473 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) { | |
474 ASSERT_TRUE(embedded_test_server()->Start()); | |
475 | |
476 // Creates a notification and cancels it in the origin page. | |
477 AllowAllOrigins(); | |
478 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
479 | |
480 std::string note_id = CreateSimpleNotification(browser(), true); | |
481 EXPECT_NE(note_id, "-1"); | |
482 | |
483 ASSERT_EQ(1, GetNotificationCount()); | |
484 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser())); | |
485 ASSERT_EQ(0, GetNotificationCount()); | |
486 } | |
487 | |
488 // Requests notification privileges and verifies the prompt appears. | |
489 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionRequestUIAppears) { | |
490 ASSERT_TRUE(embedded_test_server()->Start()); | |
491 | |
492 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
493 EXPECT_TRUE(RequestPermissionAndWait(browser())); | |
494 ASSERT_EQ(0, GetNotificationCount()); | |
495 } | |
496 | |
497 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionRequestUI) { | |
498 ASSERT_TRUE(embedded_test_server()->Start()); | |
499 | |
500 // Tries to create a notification & clicks 'allow' on the prompt. | |
501 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
502 // This notification should not be shown because we do not have permission. | |
503 CreateSimpleNotification(browser(), false); | |
504 ASSERT_EQ(0, GetNotificationCount()); | |
505 | |
506 ASSERT_TRUE(RequestAndAcceptPermission(browser())); | |
507 | |
508 CreateSimpleNotification(browser(), true); | |
509 EXPECT_EQ(1, GetNotificationCount()); | |
510 } | |
511 | |
512 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionRequestUI) { | |
513 ASSERT_TRUE(embedded_test_server()->Start()); | |
514 | |
515 // Test that no notification is created when Deny is chosen from prompt. | |
516 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
517 ASSERT_TRUE(RequestAndDenyPermission(browser())); | |
518 CreateSimpleNotification(browser(), false); | |
519 ASSERT_EQ(0, GetNotificationCount()); | |
520 ContentSettingsForOneType settings; | |
521 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
522 EXPECT_TRUE(CheckOriginInSetting(settings, GetTestPageURL())); | |
523 } | |
524 | |
525 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionRequestUI) { | |
526 ASSERT_TRUE(embedded_test_server()->Start()); | |
527 | |
528 // Test that no notification is created when prompt is dismissed. | |
529 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
530 ASSERT_TRUE(RequestAndDismissPermission(browser())); | |
531 CreateSimpleNotification(browser(), false); | |
532 ASSERT_EQ(0, GetNotificationCount()); | |
533 ContentSettingsForOneType settings; | |
534 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
535 EXPECT_EQ(0U, settings.size()); | |
536 } | |
537 | |
538 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { | |
539 ASSERT_TRUE(embedded_test_server()->Start()); | |
540 | |
541 // Verify that all domains can be allowed to show notifications. | |
542 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
543 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
544 | |
545 std::string result = CreateSimpleNotification(browser(), true); | |
546 EXPECT_NE("-1", result); | |
547 | |
548 ASSERT_EQ(1, GetNotificationCount()); | |
549 } | |
550 | |
551 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { | |
552 ASSERT_TRUE(embedded_test_server()->Start()); | |
553 | |
554 // Verify that no domain can show notifications. | |
555 SetDefaultContentSetting(CONTENT_SETTING_BLOCK); | |
556 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
557 | |
558 std::string result = CreateSimpleNotification(browser(), false); | |
559 EXPECT_EQ("-1", result); | |
560 | |
561 ASSERT_EQ(0, GetNotificationCount()); | |
562 } | |
563 | |
564 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { | |
565 ASSERT_TRUE(embedded_test_server()->Start()); | |
566 | |
567 // Verify that denying a domain and allowing all shouldn't show | |
568 // notifications from the denied domain. | |
569 DenyOrigin(GetTestPageURL().GetOrigin()); | |
570 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
571 | |
572 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
573 | |
574 std::string result = CreateSimpleNotification(browser(), false); | |
575 EXPECT_EQ("-1", result); | |
576 | |
577 ASSERT_EQ(0, GetNotificationCount()); | |
578 } | |
579 | |
580 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { | |
581 ASSERT_TRUE(embedded_test_server()->Start()); | |
582 | |
583 // Verify that allowing a domain and denying all others should show | |
584 // notifications from the allowed domain. | |
585 AllowOrigin(GetTestPageURL().GetOrigin()); | |
586 SetDefaultContentSetting(CONTENT_SETTING_BLOCK); | |
587 | |
588 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
589 | |
590 std::string result = CreateSimpleNotification(browser(), true); | |
591 EXPECT_NE("-1", result); | |
592 | |
593 ASSERT_EQ(1, GetNotificationCount()); | |
594 } | |
595 | |
596 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { | |
597 ASSERT_TRUE(embedded_test_server()->Start()); | |
598 | |
599 // Verify that denying and again allowing should show notifications. | |
600 DenyOrigin(GetTestPageURL().GetOrigin()); | |
601 | |
602 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
603 | |
604 std::string result = CreateSimpleNotification(browser(), false); | |
605 EXPECT_EQ("-1", result); | |
606 | |
607 ASSERT_EQ(0, GetNotificationCount()); | |
608 | |
609 AllowOrigin(GetTestPageURL().GetOrigin()); | |
610 result = CreateSimpleNotification(browser(), true); | |
611 EXPECT_NE("-1", result); | |
612 | |
613 ASSERT_EQ(1, GetNotificationCount()); | |
614 } | |
615 | |
616 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) { | |
617 ASSERT_TRUE(embedded_test_server()->Start()); | |
618 | |
619 // Verify able to create, deny, and close the notification. | |
620 AllowAllOrigins(); | |
621 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
622 CreateSimpleNotification(browser(), true); | |
623 ASSERT_EQ(1, GetNotificationCount()); | |
624 | |
625 DenyOrigin(GetTestPageURL().GetOrigin()); | |
626 ContentSettingsForOneType settings; | |
627 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
628 ASSERT_TRUE(CheckOriginInSetting(settings, GetTestPageURL().GetOrigin())); | |
629 | |
630 EXPECT_EQ(1, GetNotificationCount()); | |
631 message_center::NotificationList::Notifications notifications = | |
632 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
633 message_center::MessageCenter::Get()->RemoveNotification( | |
634 (*notifications.rbegin())->id(), | |
635 true); // by_user | |
636 ASSERT_EQ(0, GetNotificationCount()); | |
637 } | |
638 | |
639 // Crashes on Linux/Win. See http://crbug.com/160657. | |
640 IN_PROC_BROWSER_TEST_F( | |
641 NotificationsTest, | |
642 DISABLED_TestOriginPrefsNotSavedInIncognito) { | |
643 ASSERT_TRUE(embedded_test_server()->Start()); | |
644 | |
645 // Verify that allow/deny origin preferences are not saved in incognito. | |
646 Browser* incognito = CreateIncognitoBrowser(); | |
647 ui_test_utils::NavigateToURL(incognito, GetTestPageURL()); | |
648 ASSERT_TRUE(RequestAndDenyPermission(incognito)); | |
649 CloseBrowserWindow(incognito); | |
650 | |
651 incognito = CreateIncognitoBrowser(); | |
652 ui_test_utils::NavigateToURL(incognito, GetTestPageURL()); | |
653 ASSERT_TRUE(RequestAndAcceptPermission(incognito)); | |
654 CreateSimpleNotification(incognito, true); | |
655 ASSERT_EQ(1, GetNotificationCount()); | |
656 CloseBrowserWindow(incognito); | |
657 | |
658 incognito = CreateIncognitoBrowser(); | |
659 ui_test_utils::NavigateToURL(incognito, GetTestPageURL()); | |
660 ASSERT_TRUE(RequestPermissionAndWait(incognito)); | |
661 | |
662 ContentSettingsForOneType settings; | |
663 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
664 EXPECT_EQ(0U, settings.size()); | |
665 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings); | |
666 EXPECT_EQ(0U, settings.size()); | |
667 } | |
668 | |
669 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionRequestUI) { | |
670 ASSERT_TRUE(embedded_test_server()->Start()); | |
671 | |
672 // Test that user can close tab when bubble present. | |
673 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
674 EXPECT_TRUE(RequestPermissionAndWait(browser())); | |
675 content::WebContentsDestroyedWatcher destroyed_watcher( | |
676 browser()->tab_strip_model()->GetWebContentsAt(0)); | |
677 browser()->tab_strip_model()->CloseWebContentsAt(0, | |
678 TabStripModel::CLOSE_NONE); | |
679 destroyed_watcher.Wait(); | |
680 } | |
681 | |
682 // See crbug.com/248470 | |
683 #if defined(OS_LINUX) | |
684 #define MAYBE_TestCrashRendererNotificationRemain \ | |
685 DISABLED_TestCrashRendererNotificationRemain | |
686 #else | |
687 #define MAYBE_TestCrashRendererNotificationRemain \ | |
688 TestCrashRendererNotificationRemain | |
689 #endif | |
690 | |
691 IN_PROC_BROWSER_TEST_F(NotificationsTest, | |
692 MAYBE_TestCrashRendererNotificationRemain) { | |
693 ASSERT_TRUE(embedded_test_server()->Start()); | |
694 | |
695 // Test crashing renderer does not close or crash notification. | |
696 AllowAllOrigins(); | |
697 ui_test_utils::NavigateToURLWithDisposition( | |
698 browser(), | |
699 GURL("about:blank"), | |
700 NEW_BACKGROUND_TAB, | |
701 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
702 browser()->tab_strip_model()->ActivateTabAt(0, true); | |
703 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
704 CreateSimpleNotification(browser(), true); | |
705 ASSERT_EQ(1, GetNotificationCount()); | |
706 CrashTab(browser(), 0); | |
707 ASSERT_EQ(1, GetNotificationCount()); | |
708 } | |
709 | |
710 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) { | |
711 ASSERT_TRUE(embedded_test_server()->Start()); | |
712 | |
713 // Test that we can replace a notification using the replaceId. | |
714 AllowAllOrigins(); | |
715 | |
716 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
717 | |
718 std::string result = CreateNotification( | |
719 browser(), true, "abc.png", "Title1", "Body1", "chat"); | |
720 EXPECT_NE("-1", result); | |
721 | |
722 ASSERT_EQ(1, GetNotificationCount()); | |
723 | |
724 result = CreateNotification( | |
725 browser(), false, "no_such_file.png", "Title2", "Body2", "chat"); | |
726 EXPECT_NE("-1", result); | |
727 | |
728 ASSERT_EQ(1, GetNotificationCount()); | |
729 message_center::NotificationList::Notifications notifications = | |
730 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
731 EXPECT_EQ(base::ASCIIToUTF16("Title2"), (*notifications.rbegin())->title()); | |
732 EXPECT_EQ(base::ASCIIToUTF16("Body2"), | |
733 (*notifications.rbegin())->message()); | |
734 } | |
735 | |
736 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestLastUsage) { | |
737 ASSERT_TRUE(embedded_test_server()->Start()); | |
738 | |
739 HostContentSettingsMap* settings_map = | |
740 HostContentSettingsMapFactory::GetForProfile(browser()->profile()); | |
741 base::SimpleTestClock* clock = new base::SimpleTestClock(); | |
742 settings_map->SetPrefClockForTesting(std::unique_ptr<base::Clock>(clock)); | |
743 clock->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10)); | |
744 | |
745 // Creates a simple notification. | |
746 AllowAllOrigins(); | |
747 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
748 | |
749 std::string result = CreateSimpleNotification(browser(), true); | |
750 EXPECT_NE("-1", result); | |
751 | |
752 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(), | |
753 GetTestPageURL().GetOrigin(), | |
754 CONTENT_SETTINGS_TYPE_NOTIFICATIONS) | |
755 .ToDoubleT(), | |
756 10); | |
757 | |
758 clock->Advance(base::TimeDelta::FromSeconds(3)); | |
759 | |
760 result = CreateSimpleNotification(browser(), true); | |
761 EXPECT_NE("-1", result); | |
762 | |
763 EXPECT_EQ(settings_map->GetLastUsage(GetTestPageURL().GetOrigin(), | |
764 GetTestPageURL().GetOrigin(), | |
765 CONTENT_SETTINGS_TYPE_NOTIFICATIONS) | |
766 .ToDoubleT(), | |
767 13); | |
768 } | |
769 | |
770 IN_PROC_BROWSER_TEST_F(NotificationsTest, | |
771 TestNotificationReplacementReappearance) { | |
772 ASSERT_TRUE(embedded_test_server()->Start()); | |
773 | |
774 // Test that we can replace a notification using the tag, and that it will | |
775 // cause the notification to reappear as a popup again. | |
776 AllowAllOrigins(); | |
777 | |
778 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
779 | |
780 ASSERT_EQ(0, GetNotificationPopupCount()); | |
781 | |
782 std::string result = CreateNotification( | |
783 browser(), true, "abc.png", "Title1", "Body1", "chat"); | |
784 EXPECT_NE("-1", result); | |
785 | |
786 ASSERT_EQ(1, GetNotificationPopupCount()); | |
787 | |
788 message_center::NotificationList::Notifications notifications = | |
789 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
790 message_center::MessageCenter::Get()->ClickOnNotification( | |
791 (*notifications.rbegin())->id()); | |
792 | |
793 #if defined(OS_CHROMEOS) | |
794 ASSERT_EQ(0, GetNotificationPopupCount()); | |
795 #else | |
796 ASSERT_EQ(1, GetNotificationPopupCount()); | |
797 #endif | |
798 | |
799 result = CreateNotification( | |
800 browser(), true, "abc.png", "Title2", "Body2", "chat"); | |
801 EXPECT_NE("-1", result); | |
802 | |
803 ASSERT_EQ(1, GetNotificationPopupCount()); | |
804 } | |
805 | |
806 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationValidIcon) { | |
807 ASSERT_TRUE(embedded_test_server()->Start()); | |
808 AllowAllOrigins(); | |
809 | |
810 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
811 ASSERT_EQ(0, GetNotificationPopupCount()); | |
812 | |
813 std::string result = CreateNotification( | |
814 browser(), true, "icon.png", "Title1", "Body1", "chat"); | |
815 EXPECT_NE("-1", result); | |
816 | |
817 message_center::NotificationList::PopupNotifications notifications = | |
818 message_center::MessageCenter::Get()->GetPopupNotifications(); | |
819 ASSERT_EQ(1u, notifications.size()); | |
820 | |
821 auto* notification = *notifications.rbegin(); | |
822 | |
823 EXPECT_EQ(100, notification->icon().Width()); | |
824 EXPECT_EQ(100, notification->icon().Height()); | |
825 } | |
826 | |
827 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationInvalidIcon) { | |
828 ASSERT_TRUE(embedded_test_server()->Start()); | |
829 AllowAllOrigins(); | |
830 | |
831 ui_test_utils::NavigateToURL(browser(), GetTestPageURL()); | |
832 ASSERT_EQ(0, GetNotificationPopupCount()); | |
833 | |
834 // Not supplying an icon URL. | |
835 std::string result = CreateNotification( | |
836 browser(), true, "", "Title1", "Body1", "chat"); | |
837 EXPECT_NE("-1", result); | |
838 | |
839 message_center::NotificationList::PopupNotifications notifications = | |
840 message_center::MessageCenter::Get()->GetPopupNotifications(); | |
841 ASSERT_EQ(1u, notifications.size()); | |
842 | |
843 auto* notification = *notifications.rbegin(); | |
844 EXPECT_TRUE(notification->icon().IsEmpty()); | |
845 | |
846 // Supplying an invalid icon URL. | |
847 result = CreateNotification( | |
848 browser(), true, "invalid.png", "Title1", "Body1", "chat"); | |
849 EXPECT_NE("-1", result); | |
850 | |
851 notifications = message_center::MessageCenter::Get()->GetPopupNotifications(); | |
852 ASSERT_EQ(1u, notifications.size()); | |
853 | |
854 notification = *notifications.rbegin(); | |
855 EXPECT_TRUE(notification->icon().IsEmpty()); | |
856 } | |
857 | |
858 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationDoubleClose) { | |
859 ASSERT_TRUE(embedded_test_server()->Start()); | |
860 AllowAllOrigins(); | |
861 | |
862 ui_test_utils::NavigateToURL( | |
863 browser(), GetTestPageURLForFile("notification-double-close.html")); | |
864 ASSERT_EQ(0, GetNotificationPopupCount()); | |
865 | |
866 std::string result = CreateNotification( | |
867 browser(), true, "", "Title1", "Body1", "chat"); | |
868 EXPECT_NE("-1", result); | |
869 | |
870 ASSERT_EQ(1, GetNotificationCount()); | |
871 message_center::NotificationList::Notifications notifications = | |
872 message_center::MessageCenter::Get()->GetVisibleNotifications(); | |
873 message_center::MessageCenter::Get()->RemoveNotification( | |
874 (*notifications.rbegin())->id(), | |
875 true); // by_user | |
876 | |
877 ASSERT_EQ(0, GetNotificationCount()); | |
878 | |
879 // Calling WebContents::IsCrashed() will return FALSE here, even if the WC did | |
880 // crash. Work around this timing issue by creating another notification, | |
881 // which requires interaction with the renderer process. | |
882 result = CreateNotification(browser(), true, "", "Title1", "Body1", "chat"); | |
883 EXPECT_NE("-1", result); | |
884 } | |
OLD | NEW |