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