Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/browser/ui/blocked_content/popup_blocker_browsertest.cc

Issue 2784533002: Turn on auto-dismissing dialogs for trunk builds. (Closed)
Patch Set: chromeos Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/mouseleave_browsertest.cc ('k') | chrome/browser/ui/browser_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/scoped_feature_list.h"
14 #include "build/build_config.h" 13 #include "build/build_config.h"
15 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
17 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 16 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
18 #include "chrome/browser/history/history_test_utils.h" 17 #include "chrome/browser/history/history_test_utils.h"
19 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h" 19 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h" 20 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
22 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_commands.h" 22 #include "chrome/browser/ui/browser_commands.h"
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 tab->GetController().GoBack(); 515 tab->GetController().GoBack();
517 content::WaitForLoadStop(tab); 516 content::WaitForLoadStop(tab);
518 517
519 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile())); 518 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
520 ASSERT_EQ(1, browser()->tab_strip_model()->count()); 519 ASSERT_EQ(1, browser()->tab_strip_model()->count());
521 520
522 // The popup from the unload event handler should not show up for about:blank. 521 // The popup from the unload event handler should not show up for about:blank.
523 ASSERT_EQ(0, GetBlockedContentsCount()); 522 ASSERT_EQ(0, GetBlockedContentsCount());
524 } 523 }
525 524
526 // Verify that app modal prompts can't be used to create pop unders. 525 // Verify that JavaScript dialogs can't be used to create pop unders.
527 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) { 526 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) {
528 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 527 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
529 GURL url( 528 GURL url(
530 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html")); 529 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
531 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) 530 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
532 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, 531 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
533 std::string(), CONTENT_SETTING_ALLOW); 532 std::string(), CONTENT_SETTING_ALLOW);
534 533
535 NavigateAndCheckPopupShown(url, ExpectPopup); 534 NavigateAndCheckPopupShown(url, ExpectPopup);
536 535
537 Browser* popup_browser = chrome::FindLastActive(); 536 Browser* popup_browser = chrome::FindLastActive();
538 ASSERT_NE(popup_browser, browser()); 537 ASSERT_NE(popup_browser, browser());
539 538
540 // Showing an alert will raise the tab over the popup. 539 // Showing an alert will raise the tab over the popup.
541 #if !defined(OS_MACOSX) 540 #if !defined(OS_MACOSX)
542 // Mac doesn't activate the browser during modal dialogs, see 541 // Mac doesn't activate the browser during modal dialogs, see
543 // https://crbug.com/687732 for details. 542 // https://crbug.com/687732 for details.
544 ui_test_utils::BrowserActivationWaiter alert_waiter(browser()); 543 ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
545 #endif 544 #endif
545 JavaScriptDialogTabHelper* js_helper =
546 JavaScriptDialogTabHelper::FromWebContents(tab);
547 base::RunLoop dialog_wait;
548 js_helper->SetDialogShownCallbackForTesting(dialog_wait.QuitClosure());
546 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()")); 549 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
547 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); 550 dialog_wait.Run();
548 #if !defined(OS_MACOSX) 551 #if !defined(OS_MACOSX)
549 if (chrome::FindLastActive() != browser()) 552 if (chrome::FindLastActive() != browser())
550 alert_waiter.WaitForActivation(); 553 alert_waiter.WaitForActivation();
551 #endif 554 #endif
552 555
553 // Verify that after the dialog is closed, the popup is in front again. 556 // Verify that after the dialog is closed, the popup is in front again.
554 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
555 app_modal::JavaScriptAppModalDialog* js_dialog =
556 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
557
558 ui_test_utils::BrowserActivationWaiter waiter(popup_browser); 557 ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
559 js_dialog->native_dialog()->AcceptAppModalDialog(); 558 js_helper->HandleJavaScriptDialog(tab, true, nullptr);
560 waiter.WaitForActivation(); 559 waiter.WaitForActivation();
561 ASSERT_EQ(popup_browser, chrome::FindLastActive()); 560 ASSERT_EQ(popup_browser, chrome::FindLastActive());
562 } 561 }
563 562
564 // Verify that setting the window.opener doesn't interfere with popup blocking. 563 // Verify that setting the window.opener doesn't interfere with popup blocking.
565 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderWindowOpener) { 564 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderWindowOpener) {
566 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 565 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
567 GURL url( 566 GURL url(
568 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html")); 567 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
569 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) 568 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
(...skipping 10 matching lines...) Expand all
580 ASSERT_TRUE(popup_browser->is_type_popup()); 579 ASSERT_TRUE(popup_browser->is_type_popup());
581 content::ExecuteScriptAndGetValue(popup->GetMainFrame(), 580 content::ExecuteScriptAndGetValue(popup->GetMainFrame(),
582 "window.open('', 'mywindow')"); 581 "window.open('', 'mywindow')");
583 582
584 // Showing an alert will raise the tab over the popup. 583 // Showing an alert will raise the tab over the popup.
585 #if !defined(OS_MACOSX) 584 #if !defined(OS_MACOSX)
586 // Mac doesn't activate the browser during modal dialogs, see 585 // Mac doesn't activate the browser during modal dialogs, see
587 // https://crbug.com/687732 for details. 586 // https://crbug.com/687732 for details.
588 ui_test_utils::BrowserActivationWaiter alert_waiter(browser()); 587 ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
589 #endif 588 #endif
589 JavaScriptDialogTabHelper* js_helper =
590 JavaScriptDialogTabHelper::FromWebContents(tab);
591 base::RunLoop dialog_wait;
592 js_helper->SetDialogShownCallbackForTesting(dialog_wait.QuitClosure());
590 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()")); 593 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
591 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); 594 dialog_wait.Run();
592 #if !defined(OS_MACOSX) 595 #if !defined(OS_MACOSX)
593 if (chrome::FindLastActive() != browser()) 596 if (chrome::FindLastActive() != browser())
594 alert_waiter.WaitForActivation(); 597 alert_waiter.WaitForActivation();
595 #endif 598 #endif
596 599
597 // Verify that after the dialog is closed, the popup is in front again. 600 // Verify that after the dialog is closed, the popup is in front again.
598 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
599 app_modal::JavaScriptAppModalDialog* js_dialog =
600 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
601
602 ui_test_utils::BrowserActivationWaiter waiter(popup_browser); 601 ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
603 js_dialog->native_dialog()->AcceptAppModalDialog(); 602 js_helper->HandleJavaScriptDialog(tab, true, nullptr);
604 waiter.WaitForActivation(); 603 waiter.WaitForActivation();
605 ASSERT_EQ(popup_browser, chrome::FindLastActive()); 604 ASSERT_EQ(popup_browser, chrome::FindLastActive());
606 } 605 }
607 606
608 // Verify that popunders from subframes are prevented. https://crbug.com/705316 607 // Verify that popunders from subframes are prevented. https://crbug.com/705316
609 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderSubframe) { 608 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderSubframe) {
610 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 609 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
611 GURL url(embedded_test_server()->GetURL( 610 GURL url(embedded_test_server()->GetURL(
612 "/popup_blocker/popup-window-subframe-open.html")); 611 "/popup_blocker/popup-window-subframe-open.html"));
613 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) 612 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
614 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, 613 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
615 std::string(), CONTENT_SETTING_ALLOW); 614 std::string(), CONTENT_SETTING_ALLOW);
616 615
617 NavigateAndCheckPopupShown(url, ExpectPopup); 616 NavigateAndCheckPopupShown(url, ExpectPopup);
618 617
619 Browser* popup_browser = chrome::FindLastActive(); 618 Browser* popup_browser = chrome::FindLastActive();
620 ASSERT_NE(popup_browser, browser()); 619 ASSERT_NE(popup_browser, browser());
621 620
622 // Showing an alert will raise the tab over the popup. 621 // Showing an alert will raise the tab over the popup.
623 #if !defined(OS_MACOSX) 622 #if !defined(OS_MACOSX)
624 // Mac doesn't activate the browser during modal dialogs, see 623 // Mac doesn't activate the browser during modal dialogs, see
625 // https://crbug.com/687732 for details. 624 // https://crbug.com/687732 for details.
626 ui_test_utils::BrowserActivationWaiter alert_waiter(browser()); 625 ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
627 #endif 626 #endif
627 JavaScriptDialogTabHelper* js_helper =
628 JavaScriptDialogTabHelper::FromWebContents(tab);
629 base::RunLoop dialog_wait;
630 js_helper->SetDialogShownCallbackForTesting(dialog_wait.QuitClosure());
628 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()")); 631 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
629 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); 632 dialog_wait.Run();
630 #if !defined(OS_MACOSX) 633 #if !defined(OS_MACOSX)
631 if (chrome::FindLastActive() != browser()) 634 if (chrome::FindLastActive() != browser())
632 alert_waiter.WaitForActivation(); 635 alert_waiter.WaitForActivation();
633 #endif 636 #endif
634 637
635 // Verify that after the dialog is closed, the popup is in front again. 638 // Verify that after the dialog is closed, the popup is in front again.
636 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
637 app_modal::JavaScriptAppModalDialog* js_dialog =
638 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
639
640 ui_test_utils::BrowserActivationWaiter waiter(popup_browser); 639 ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
641 js_dialog->native_dialog()->AcceptAppModalDialog(); 640 js_helper->HandleJavaScriptDialog(tab, true, nullptr);
642 waiter.WaitForActivation(); 641 waiter.WaitForActivation();
643 ASSERT_EQ(popup_browser, chrome::FindLastActive()); 642 ASSERT_EQ(popup_browser, chrome::FindLastActive());
644 } 643 }
645 644
646 // Verify that popups without an opener don't interfere with popup blocking. 645 // Verify that popups without an opener don't interfere with popup blocking.
647 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderNoOpener) { 646 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnderNoOpener) {
648 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 647 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
649 GURL url(embedded_test_server()->GetURL( 648 GURL url(embedded_test_server()->GetURL(
650 "/popup_blocker/popup-window-open-noopener.html")); 649 "/popup_blocker/popup-window-open-noopener.html"));
651 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) 650 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
652 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, 651 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
653 std::string(), CONTENT_SETTING_ALLOW); 652 std::string(), CONTENT_SETTING_ALLOW);
654 653
655 NavigateAndCheckPopupShown(url, ExpectPopup); 654 NavigateAndCheckPopupShown(url, ExpectPopup);
656 655
657 Browser* popup_browser = chrome::FindLastActive(); 656 Browser* popup_browser = chrome::FindLastActive();
658 ASSERT_NE(popup_browser, browser()); 657 ASSERT_NE(popup_browser, browser());
659 658
660 // Showing an alert will raise the tab over the popup. 659 // Showing an alert will raise the tab over the popup.
661 #if !defined(OS_MACOSX) 660 #if !defined(OS_MACOSX)
662 // Mac doesn't activate the browser during modal dialogs, see 661 // Mac doesn't activate the browser during modal dialogs, see
663 // https://crbug.com/687732 for details. 662 // https://crbug.com/687732 for details.
664 ui_test_utils::BrowserActivationWaiter alert_waiter(browser()); 663 ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
665 #endif 664 #endif
665 JavaScriptDialogTabHelper* js_helper =
666 JavaScriptDialogTabHelper::FromWebContents(tab);
667 base::RunLoop dialog_wait;
668 js_helper->SetDialogShownCallbackForTesting(dialog_wait.QuitClosure());
666 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()")); 669 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
667 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); 670 dialog_wait.Run();
668 #if !defined(OS_MACOSX) 671 #if !defined(OS_MACOSX)
669 if (chrome::FindLastActive() != browser()) 672 if (chrome::FindLastActive() != browser())
670 alert_waiter.WaitForActivation(); 673 alert_waiter.WaitForActivation();
671 #endif
672
673 // Verify that after the dialog is closed, the popup is in front again.
674 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
675 app_modal::JavaScriptAppModalDialog* js_dialog =
676 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
677
678 ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
679 js_dialog->native_dialog()->AcceptAppModalDialog();
680 waiter.WaitForActivation();
681 ASSERT_EQ(popup_browser, chrome::FindLastActive());
682 }
683
684 // Verify that app modal prompts can't be used to create pop unders, while the
685 // new auto-dismissing JavaScript dialogs are enabled.
686 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
687 ModalPopUnderAutoDismissingDialogs) {
688 base::test::ScopedFeatureList feature_list;
689 feature_list.InitAndEnableFeature(features::kAutoDismissingDialogs);
690
691 // One tab to test in.
692 TabStripModel* tab_strip = browser()->tab_strip_model();
693 WebContents* tab = tab_strip->GetActiveWebContents();
694 EXPECT_EQ(1, tab_strip->count());
695
696 // One blank tab for later.
697 ui_test_utils::NavigateToURLWithDisposition(
698 browser(), GURL(url::kAboutBlankURL),
699 WindowOpenDisposition::NEW_BACKGROUND_TAB,
700 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
701 EXPECT_EQ(2, tab_strip->count());
702 ASSERT_EQ(0, tab_strip->GetIndexOfWebContents(tab));
703
704 // Show a popup.
705 JavaScriptDialogTabHelper* js_helper =
706 JavaScriptDialogTabHelper::FromWebContents(tab);
707 GURL url(
708 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
709 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
710 ->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
711 std::string(), CONTENT_SETTING_ALLOW);
712
713 NavigateAndCheckPopupShown(url, ExpectPopup);
714
715 Browser* popup_browser = chrome::FindLastActive();
716 ASSERT_NE(popup_browser, browser());
717
718 // Showing an alert will raise the tab over the popup.
719 #if !defined(OS_MACOSX)
720 // Mac doesn't activate the browser during modal dialogs, see
721 // https://crbug.com/687732 for details.
722 ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
723 #endif
724 scoped_refptr<content::MessageLoopRunner> runner =
725 new content::MessageLoopRunner;
726 js_helper->SetDialogShownCallbackForTesting(runner->QuitClosure());
727 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
728 runner->Run();
729 #if !defined(OS_MACOSX)
730 if (chrome::FindLastActive() != browser())
731 alert_waiter.WaitForActivation();
732 #endif 674 #endif
733 675
734 // Verify that after the dialog is closed, the popup is in front again. 676 // Verify that after the dialog is closed, the popup is in front again.
735 ui_test_utils::BrowserActivationWaiter waiter(popup_browser); 677 ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
736 tab_strip->ActivateTabAt(1, true); 678 js_helper->HandleJavaScriptDialog(tab, true, nullptr);
737 waiter.WaitForActivation(); 679 waiter.WaitForActivation();
738 ASSERT_EQ(popup_browser, chrome::FindLastActive()); 680 ASSERT_EQ(popup_browser, chrome::FindLastActive());
739 } 681 }
740 682
741 #if BUILDFLAG(ENABLE_EXTENSIONS) 683 #if BUILDFLAG(ENABLE_EXTENSIONS)
742 #define MAYBE_ModalPopUnderViaGuestView DISABLED_ModalPopUnderViaGuestView 684 #define MAYBE_ModalPopUnderViaGuestView DISABLED_ModalPopUnderViaGuestView
743 #else 685 #else
744 #define MAYBE_ModalPopUnderViaGuestView ModalPopUnderViaGuestView 686 #define MAYBE_ModalPopUnderViaGuestView ModalPopUnderViaGuestView
745 #endif 687 #endif
746 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, 688 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 819
878 wait_for_new_tab.Wait(); 820 wait_for_new_tab.Wait();
879 821
880 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile())); 822 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
881 ASSERT_EQ(2, browser()->tab_strip_model()->count()); 823 ASSERT_EQ(2, browser()->tab_strip_model()->count());
882 // Check that we create the background tab. 824 // Check that we create the background tab.
883 ASSERT_EQ(0, browser()->tab_strip_model()->active_index()); 825 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
884 } 826 }
885 827
886 } // namespace 828 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/mouseleave_browsertest.cc ('k') | chrome/browser/ui/browser_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698