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 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/history/history_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "components/blocked_content/list_item_position.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/embedder_support/switches.h"
#include "components/javascript_dialogs/app_modal_dialog_controller.h"
#include "components/javascript_dialogs/app_modal_dialog_manager.h"
#include "components/javascript_dialogs/app_modal_dialog_view.h"
#include "components/javascript_dialogs/tab_modal_dialog_manager.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_result.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
#include "third_party/blink/public/common/switches.h"
#endif
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#endif
using content::WebContents;
using testing::_;
using testing::Return;
namespace {
class CloseObserver : public content::WebContentsObserver {
public:
explicit CloseObserver(WebContents* contents)
: content::WebContentsObserver(contents) {}
CloseObserver(const CloseObserver&) = delete;
CloseObserver& operator=(const CloseObserver&) = delete;
void Wait() { close_loop_.Run(); }
// content::WebContentsObserver:
void WebContentsDestroyed() override { close_loop_.Quit(); }
private:
base::RunLoop close_loop_;
};
class PopupBlockerBrowserTest : public InProcessBrowserTest {
public:
PopupBlockerBrowserTest() = default;
PopupBlockerBrowserTest(const PopupBlockerBrowserTest&) = delete;
PopupBlockerBrowserTest& operator=(const PopupBlockerBrowserTest&) = delete;
~PopupBlockerBrowserTest() override = default;
// InProcessBrowserTest:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
void SetUpCommandLine(base::CommandLine* command_line) override {
// Testing on some platforms is flaky due to slower loading interacting with
// deferred commits.
command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
}
#endif
int GetBlockedContentsCount() {
// Do a round trip to the renderer first to flush any in-flight IPCs to
// create a to-be-blocked window.
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
if (!content::ExecJs(tab, std::string(),
content::EXECUTE_SCRIPT_NO_USER_GESTURE)) {
ADD_FAILURE() << "Failed to execute script in active tab.";
return -1;
}
blocked_content::PopupBlockerTabHelper* popup_blocker_helper =
blocked_content::PopupBlockerTabHelper::FromWebContents(tab);
return popup_blocker_helper->GetBlockedPopupsCount();
}
enum WhatToExpect {
kExpectPopup,
kExpectForegroundTab,
kExpectBackgroundTab,
kExpectNewWindow
};
enum ShouldCheckTitle { kCheckTitle, kDontCheckTitle };
void NavigateAndCheckPopupShown(const GURL& url,
WhatToExpect what_to_expect) {
ui_test_utils::TabAddedWaiter tab_added(browser());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
if (what_to_expect == kExpectPopup) {
ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile()));
} else {
tab_added.Wait();
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(2, browser()->tab_strip_model()->count());
// Check that we always create foreground tabs.
ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
}
ASSERT_EQ(0, GetBlockedContentsCount());
}
// Navigates to the test indicated by |test_name| using |browser| which is
// expected to try to open a popup. Verifies that the popup was blocked and
// then opens the blocked popup. Once the popup stopped loading, verifies
// that the title of the page is "PASS" if |check_title| is set.
//
// If |disposition| is CURRENT_TAB, the blocked content will be opened as
// it was specified by renderer. In this case possible WhatToExpect is
// kExpectPopup or kExpectForegroundTab.
//
// But if |disposition| is something else but CURRENT_TAB, blocked contents
// will be opened in that alternative disposition. This is for allowing users
// to use keyboard modifiers in ContentSettingBubble. Therefore possible
// WhatToExpect is kExpectForegroundTab, kExpectBackgroundTab, or
// kExpectNewWindow.
//
// Returns the WebContents of the launched popup.
WebContents* RunCheckTest(Browser* browser,
const std::string& test_name,
WindowOpenDisposition disposition,
WhatToExpect what_to_expect,
ShouldCheckTitle check_title,
const std::u16string& expected_title = u"PASS") {
GURL url(embedded_test_server()->GetURL(test_name));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser, url));
// Since the popup blocker blocked the window.open, there should be only one
// tab and window in the profile.
EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile()));
EXPECT_EQ(1, browser->tab_strip_model()->count());
WebContents* web_contents =
browser->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(url, web_contents->GetURL());
ui_test_utils::TabAddedWaiter tab_add(browser);
// Launch the blocked popup.
blocked_content::PopupBlockerTabHelper* popup_blocker_helper =
blocked_content::PopupBlockerTabHelper::FromWebContents(web_contents);
ui_test_utils::WaitForViewVisibility(browser, VIEW_ID_CONTENT_SETTING_POPUP,
true);
EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount());
std::map<int32_t, GURL> blocked_requests =
popup_blocker_helper->GetBlockedPopupRequests();
std::map<int32_t, GURL>::const_iterator iter = blocked_requests.begin();
ui_test_utils::BrowserChangeObserver popup_observer(
nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
popup_blocker_helper->ShowBlockedPopup(iter->first, disposition);
Browser* new_browser;
if (what_to_expect == kExpectPopup || what_to_expect == kExpectNewWindow) {
ui_test_utils::WaitForBrowserSetLastActive(popup_observer.Wait());
new_browser = BrowserList::GetInstance()->GetLastActive();
EXPECT_NE(browser, new_browser);
web_contents = new_browser->tab_strip_model()->GetActiveWebContents();
if (what_to_expect == kExpectNewWindow) {
EXPECT_TRUE(new_browser->is_type_normal());
}
} else {
tab_add.Wait();
new_browser = browser;
EXPECT_EQ(2, browser->tab_strip_model()->count());
int expected_active_tab =
(what_to_expect == kExpectForegroundTab) ? 1 : 0;
EXPECT_EQ(expected_active_tab,
browser->tab_strip_model()->active_index());
web_contents = browser->tab_strip_model()->GetWebContentsAt(1);
}
if (check_title == kCheckTitle) {
// Check that the check passed.
content::TitleWatcher title_watcher(web_contents, expected_title);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
}
return web_contents;
}
};
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, BlockWebContentsCreation) {
RunCheckTest(browser(), "/popup_blocker/popup-blocked-to-post-blank.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kDontCheckTitle);
}
// TODO(crbug.com/40144522): Flaky on Mac ASAN and Chrome OS.
#if (BUILDFLAG(IS_MAC) && defined(ADDRESS_SANITIZER)) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_BlockWebContentsCreationIncognito \
DISABLED_BlockWebContentsCreationIncognito
#else
#define MAYBE_BlockWebContentsCreationIncognito \
BlockWebContentsCreationIncognito
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
MAYBE_BlockWebContentsCreationIncognito) {
RunCheckTest(CreateIncognitoBrowser(),
"/popup_blocker/popup-blocked-to-post-blank.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kDontCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedFakeClickOnAnchor) {
RunCheckTest(browser(), "/popup_blocker/popup-fake-click-on-anchor.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kDontCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
PopupBlockedFakeClickOnAnchorNoTarget) {
RunCheckTest(browser(), "/popup_blocker/popup-fake-click-on-anchor2.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kDontCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupMetrics) {
const char kPopupActions[] = "ContentSettings.Popups.BlockerActions";
base::HistogramTester tester;
const GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-many.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
EXPECT_EQ(2, GetBlockedContentsCount());
tester.ExpectBucketCount(
kPopupActions,
static_cast<int>(
blocked_content::PopupBlockerTabHelper::Action::kInitiated),
2);
tester.ExpectBucketCount(
kPopupActions,
static_cast<int>(
blocked_content::PopupBlockerTabHelper::Action::kBlocked),
2);
// Click through one of them.
auto* popup_blocker = blocked_content::PopupBlockerTabHelper::FromWebContents(
browser()->tab_strip_model()->GetActiveWebContents());
popup_blocker->ShowBlockedPopup(
popup_blocker->GetBlockedPopupRequests().begin()->first,
WindowOpenDisposition::NEW_BACKGROUND_TAB);
tester.ExpectBucketCount(
kPopupActions,
static_cast<int>(blocked_content::PopupBlockerTabHelper::Action::
kClickedThroughNoGesture),
1);
// Allowlist the site and navigate again.
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
tester.ExpectBucketCount(
kPopupActions,
static_cast<int>(
blocked_content::PopupBlockerTabHelper::Action::kInitiated),
4);
// 4 initiated popups, 2 blocked, and 1 clicked through.
tester.ExpectTotalCount(kPopupActions, 4 + 2 + 1);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) {
GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-many.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_EQ(2, GetBlockedContentsCount());
}
// Verify that popups are launched on browser back button.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
AllowPopupThroughContentSetting) {
GURL url(embedded_test_server()->GetURL(
"/popup_blocker/popup-blocked-to-post-blank.html"));
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
NavigateAndCheckPopupShown(url, kExpectForegroundTab);
}
// Verify that content settings are applied based on the top-level frame URL.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
AllowPopupThroughContentSettingIFrame) {
GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-frames.html"));
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
// Popup from the iframe should be allowed since the top-level URL is
// allowlisted.
NavigateAndCheckPopupShown(url, kExpectForegroundTab);
// Allowlist iframe URL instead.
GURL::Replacements replace_host;
replace_host.SetHostStr("www.a.com");
GURL frame_url(embedded_test_server()
->GetURL("/popup_blocker/popup-frames-iframe.html")
.ReplaceComponents(replace_host));
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->ClearSettingsForOneType(ContentSettingsType::POPUPS);
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(frame_url, GURL(),
ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
// Popup should be blocked.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_EQ(1, GetBlockedContentsCount());
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, NoPopupsLaunchWhenTabIsClosed) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
embedder_support::kDisablePopupBlocking);
GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
GURL url2(
embedded_test_server()->GetURL("/popup_blocker/popup-success.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
// Expect no popup.
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
PopupsAllowedWhenPopupBlockingIsDisabled) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
embedder_support::kDisablePopupBlocking);
GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
NavigateAndCheckPopupShown(url, kExpectPopup);
}
// Verify that when you unblock popup, the popup shows in history and omnibox.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
UnblockedPopupShowsInHistoryAndOmnibox) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
embedder_support::kDisablePopupBlocking);
GURL url(embedded_test_server()->GetURL(
"/popup_blocker/popup-blocked-to-post-blank.html"));
NavigateAndCheckPopupShown(url, kExpectForegroundTab);
// Make sure the navigation in the new tab actually finished.
WebContents* web_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
std::u16string expected_title(u"Popup Success!");
content::TitleWatcher title_watcher(web_contents, expected_title);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
WaitForHistoryBackendToRun(browser()->profile());
std::string search_string =
"data:text/html,<title>Popup Success!</title>you should not see this "
"message if popup blocker is enabled";
ui_test_utils::HistoryEnumerator history(browser()->profile());
std::vector<GURL>& history_urls = history.urls();
ASSERT_EQ(2u, history_urls.size());
ASSERT_EQ(embedded_test_server()->GetURL("/popup_blocker/popup-success.html"),
history_urls[0]);
ASSERT_EQ(url, history_urls[1]);
TemplateURLService* service =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
search_test_utils::WaitForTemplateURLServiceToLoad(service);
ui_test_utils::SendToOmniboxAndSubmit(browser(), search_string);
OmniboxEditModel* model =
browser()->window()->GetLocationBar()->GetOmniboxView()->model();
EXPECT_EQ(GURL(search_string), model->CurrentMatch(nullptr).destination_url);
EXPECT_EQ(base::ASCIIToUTF16(search_string),
model->CurrentMatch(nullptr).contents);
}
// This test fails on linux AURA with this change
// https://codereview.chromium.org/23903056
// BUG=https://code.google.com/p/chromium/issues/detail?id=295299
// TODO(ananta). Debug and fix this test.
#if defined(USE_AURA) && \
(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN))
#define MAYBE_WindowFeatures DISABLED_WindowFeatures
#else
#define MAYBE_WindowFeatures WindowFeatures
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_WindowFeatures) {
WebContents* popup = RunCheckTest(
browser(), "/popup_blocker/popup-window-open.html",
WindowOpenDisposition::CURRENT_TAB, kExpectPopup, kDontCheckTitle);
// Check that the new popup has (roughly) the requested size.
gfx::Size window_size = popup->GetContainerBounds().size();
EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351);
EXPECT_GE(window_size.height(), 249);
EXPECT_LE(window_size.height(), 253);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CorrectReferrer) {
RunCheckTest(browser(), "/popup_blocker/popup-referrer.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CorrectFrameName) {
RunCheckTest(browser(), "/popup_blocker/popup-framename.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WindowFeaturesBarProps) {
RunCheckTest(browser(), "/popup_blocker/popup-windowfeatures.html",
WindowOpenDisposition::CURRENT_TAB, kExpectPopup, kCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, SessionStorage) {
RunCheckTest(browser(), "/popup_blocker/popup-sessionstorage.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Opener) {
RunCheckTest(browser(), "/popup_blocker/popup-opener.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kCheckTitle);
}
// Tests that the popup can still close itself after navigating. This tests that
// the openedByDOM bit is preserved across blocked popups.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ClosableAfterNavigation) {
// Open a popup.
WebContents* popup = RunCheckTest(
browser(), "/popup_blocker/popup-opener.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab, kCheckTitle);
// Navigate it elsewhere.
content::TestNavigationObserver nav_observer(popup);
popup->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
u"location.href = '/empty.html'", base::NullCallback(),
content::ISOLATED_WORLD_ID_GLOBAL);
nav_observer.Wait();
// Have it close itself.
CloseObserver close_observer(popup);
popup->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
u"window.close()", base::NullCallback(),
content::ISOLATED_WORLD_ID_GLOBAL);
close_observer.Wait();
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenerSuppressed) {
RunCheckTest(browser(), "/popup_blocker/popup-openersuppressed.html",
WindowOpenDisposition::CURRENT_TAB, kExpectForegroundTab,
kCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ShiftClick) {
RunCheckTest(browser(), "/popup_blocker/popup-fake-click-on-anchor3.html",
WindowOpenDisposition::CURRENT_TAB, kExpectPopup, kCheckTitle,
u"Popup Success!");
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WebUI) {
GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-webui.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// A popup to a webui url should be blocked without ever creating a new tab.
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(1, browser()->tab_strip_model()->count());
ASSERT_EQ(0, GetBlockedContentsCount());
}
// Verify that the renderer can't DOS the browser by creating arbitrarily many
// popups.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DenialOfService) {
GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-dos.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_EQ(25, GetBlockedContentsCount());
}
// Verify that an onunload popup does not show up for about:blank.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Regress427477) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUINewTabURL)));
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)));
GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
tab->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(tab));
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(1, browser()->tab_strip_model()->count());
// The popup from the unload event handler should not show up for about:blank.
ASSERT_EQ(0, GetBlockedContentsCount());
}
// Verify that JavaScript dialogs can't be used to create popunders.
//
// Note that this is a test of the PopunderPreventer, used to ensure that
// dialogs that activate don't allow popunders. The problem is that there are no
// page-triggerable dialogs that activate any more.
//
// The dialogs that are page-triggerable don't activate, and the dialogs that
// activate aren't page-triggerable. However, in order to test the
// PopunderPreventer and ensure that it doesn't regress in functionality, in
// this test we will generate an old-style activating dialog, one that cannot
// actually be triggered by the page, to ensure that if somehow a page manages
// to do it (a bug), the popunder is still prevented.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) {
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
NavigateAndCheckPopupShown(url, kExpectPopup);
Browser* popup_browser = chrome::FindLastActive();
ASSERT_NE(popup_browser, browser());
// Showing an alert will raise the tab over the popup.
#if !BUILDFLAG(IS_MAC)
// Mac doesn't activate the browser during modal dialogs, see
// https://crbug.com/687732 for details.
ui_test_utils::BrowserActivationWaiter alert_waiter(browser());
#endif
bool ignored;
javascript_dialogs::AppModalDialogManager::GetInstance()->RunJavaScriptDialog(
tab, tab->GetPrimaryMainFrame(), content::JAVASCRIPT_DIALOG_TYPE_ALERT,
std::u16string(), std::u16string(), base::DoNothing(), &ignored);
javascript_dialogs::AppModalDialogController* dialog =
ui_test_utils::WaitForAppModalDialog();
ASSERT_TRUE(dialog);
#if !BUILDFLAG(IS_MAC)
if (chrome::FindLastActive() != browser()) {
alert_waiter.WaitForActivation();
}
#endif
// Verify that after the dialog is closed, the popup is in front again.
#if !BUILDFLAG(IS_MAC)
ui_test_utils::BrowserActivationWaiter waiter(popup_browser);
#endif
javascript_dialogs::AppModalDialogManager::GetInstance()
->HandleJavaScriptDialog(tab, true, nullptr);
#if !BUILDFLAG(IS_MAC)
waiter.WaitForActivation();
#endif
ASSERT_EQ(popup_browser, chrome::FindLastActive());
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Tests that the print preview dialog can't be used to create popunders. The
// test was added due to a bug in MacViews that causes dialogs to activate
// their parents (https://crbug.com/1073587).
// TODO(weili): investigate why this failed on Linux and ChromeOS bots,
// and why it was flaky on Windows. https://crbug.com/1241815.
#if BUILDFLAG(IS_MAC)
#define MAYBE_PrintPreviewPopUnder PrintPreviewPopUnder
#else
#define MAYBE_PrintPreviewPopUnder DISABLED_PrintPreviewPopUnder
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_PrintPreviewPopUnder) {
WebContents* original_tab =
browser()->tab_strip_model()->GetActiveWebContents();
GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
NavigateAndCheckPopupShown(url, kExpectPopup);
Browser* popup_browser = chrome::FindLastActive();
ASSERT_NE(popup_browser, browser());
// Show a print preview dialog and confirm it doesn't activate the
// browser window containing the original WebContents.
content::TestNavigationObserver observer(nullptr);
observer.StartWatchingNewWebContents();
printing::PrintPreviewDialogController* dialog_controller =
printing::PrintPreviewDialogController::GetInstance();
WebContents* print_preview_dialog =
dialog_controller->GetOrCreatePreviewDialogForTesting(original_tab);
observer.Wait();
observer.StopWatchingNewWebContents();
EXPECT_EQ(popup_browser, chrome::FindLastActive());
// Navigate away; this will close the print preview dialog.
content::WebContentsDestroyedWatcher watcher(print_preview_dialog);
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)));
watcher.Wait();
// The popup is still in front and being activated.
EXPECT_EQ(popup_browser, chrome::FindLastActive());
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
class PopupBlockerBrowserTestWithWebApps : public PopupBlockerBrowserTest {
private:
web_app::OsIntegrationTestOverrideBlockingRegistration faked_os_integration_;
};
// Reentrancy regression test for PopunderPreventer attempting to activate a
// fullscreen web app window that is being closed; see crbug.com/331095620.
// TODO(crbug.com/335493696): Mac shims don't work with faked fullscreen.
#if BUILDFLAG(IS_MAC)
#define MAYBE_CloseFullscreenStandaloneWebApp \
DISABLED_CloseFullscreenStandaloneWebApp
#else
#define MAYBE_CloseFullscreenStandaloneWebApp CloseFullscreenStandaloneWebApp
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTestWithWebApps,
MAYBE_CloseFullscreenStandaloneWebApp) {
GURL url = embedded_test_server()->GetURL("/web_apps/basic.html");
webapps::AppId id = web_app::InstallWebAppFromPage(browser(), url);
Browser* app = web_app::LaunchWebAppBrowserAndWait(browser()->profile(), id);
WebContents* tab = app->tab_strip_model()->GetActiveWebContents();
tab->GetDelegate()->EnterFullscreenModeForTab(tab->GetPrimaryMainFrame(), {});
ui_test_utils::FullscreenWaiter(app, {.tab_fullscreen = true}).Wait();
app->window()->Close();
ui_test_utils::WaitForBrowserToClose(app);
}
// Tests that Ctrl+Enter/Cmd+Enter keys on a link open the background tab.
// TODO(crbug.com/40901768): Re-enable this test
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DISABLED_CtrlEnterKey) {
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
GURL url(embedded_test_server()->GetURL(
"/popup_blocker/popup-simulated-click-on-anchor.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ui_test_utils::TabAddedWaiter tab_add(browser());
bool command = false;
#if BUILDFLAG(IS_MAC)
command = true;
#endif
SimulateKeyPress(tab, ui::DomKey::ENTER, ui::DomCode::ENTER, ui::VKEY_RETURN,
!command, false, false, command);
tab_add.Wait();
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(2, browser()->tab_strip_model()->count());
// Check that we create the background tab.
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
}
// Tests that the tapping gesture with cntl/cmd key on a link open the
// background tab.
#if BUILDFLAG(IS_WIN)
#define MAYBE_TapGestureWithCtrlKey DISABLED_TapGestureWithCtrlKey
#else
#define MAYBE_TapGestureWithCtrlKey TapGestureWithCtrlKey
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_TapGestureWithCtrlKey) {
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
GURL url(embedded_test_server()->GetURL(
"/popup_blocker/popup-simulated-click-on-anchor2.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ui_test_utils::TabAddedWaiter tab_add(browser());
#if BUILDFLAG(IS_MAC)
unsigned modifiers = blink::WebInputEvent::kMetaKey;
#else
unsigned modifiers = blink::WebInputEvent::kControlKey;
#endif
content::SimulateTapWithModifiersAt(tab, modifiers, gfx::Point(350, 250));
tab_add.Wait();
ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(2, browser()->tab_strip_model()->count());
// Check that we create the background tab.
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopupsViaPostMessage) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(
"/popup_blocker/post-message-popup.html")));
content::WebContents* opener =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(1, content::EvalJs(opener, "openPopupsAndReport();"));
}
// Test that popup blocker can show blocked contents in new foreground tab.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenInNewForegroundTab) {
RunCheckTest(browser(), "/popup_blocker/popup-window-open.html",
WindowOpenDisposition::NEW_FOREGROUND_TAB, kExpectForegroundTab,
kDontCheckTitle);
}
// Test that popup blocker can show blocked contents in new background tab.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenInNewBackgroundTab) {
RunCheckTest(browser(), "/popup_blocker/popup-window-open.html",
WindowOpenDisposition::NEW_BACKGROUND_TAB, kExpectBackgroundTab,
kDontCheckTitle);
}
// Test that popup blocker can show blocked contents in new window.
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenInNewWindow) {
RunCheckTest(browser(), "/popup_blocker/popup-window-open.html",
WindowOpenDisposition::NEW_WINDOW, kExpectNewWindow,
kDontCheckTitle);
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupsDisableBackForwardCache) {
content::BackForwardCacheDisabledTester tester;
// Navigate to a page with a popup that will be blocked.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(
"a.com", "/popup_blocker/popup-many.html")));
content::RenderFrameHostWrapper rfh(browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame());
int process_id = rfh->GetProcess()->GetDeprecatedID();
int frame_routing_id = rfh->GetRoutingID();
// Navigate to another page on the same domain. This will trigger a check on
// whether or not the RenderFrameHost can be cached.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("a.com", "/title1.html")));
// Ensure the RFH can not be cached due to the blocked popup.
EXPECT_TRUE(tester.IsDisabledForFrameWithReason(
process_id, frame_routing_id,
back_forward_cache::DisabledReason(
back_forward_cache::DisabledReasonId::kPopupBlockerTabHelper)));
// Navigate to a different domain which will create a new RFH.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("b.com", "/title1.html")));
// Because the original RFH is not cacheable it will be deleted.
ASSERT_TRUE(rfh.WaitUntilRenderFrameDeleted());
}
// Make sure the poput is attributed to the right WebContents when it is
// triggered from a different WebContents. Regression test for
// https://crbug.com/1128495
// Flaky on windows and mac: b/40896665.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#define MAYBE_PopupTriggeredFromDifferentWebContents \
DISABLED_PopupTriggeredFromDifferentWebContents
#else
#define MAYBE_PopupTriggeredFromDifferentWebContents \
PopupTriggeredFromDifferentWebContents
#endif
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
MAYBE_PopupTriggeredFromDifferentWebContents) {
const GURL url(
embedded_test_server()->GetURL("/popup_blocker/popup-in-href.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_EQ(browser()->tab_strip_model()->count(), 1);
content::WebContents* tab_1 =
browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::TabAddedWaiter tab_Added_waiter(browser());
SimulateMouseClickOrTapElementWithId(tab_1, "link");
tab_Added_waiter.Wait();
content::WebContents* tab_2 =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(tab_1, tab_2);
// We need to make sure the js in the new tab that comes from the href runs
// before we perform the checks further down. Since we have no control over
// that script we just run some more (that we do control) and wait for it to
// finish.
//
// crbug.com/368578515: The ExecJs script here runs out of the renderer's
// default task queue, which has a different task deferral policy with
// DeferRendererTasksAfterInput than the queue that runs the loading script
// task we're waiting for. This means the ExecJs task might run before the
// loading script task, depending on rendering timing. To get around this, use
// a lower priority task queue that has the same task deferral policy.
EXPECT_TRUE(content::ExecJs(tab_2, R"(
(() => {
return scheduler.postTask(() => {}, {priority: 'background'});
})();
)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_FALSE(content_settings::PageSpecificContentSettings::GetForFrame(
tab_1->GetPrimaryMainFrame())
->IsContentBlocked(ContentSettingsType::POPUPS));
EXPECT_TRUE(content_settings::PageSpecificContentSettings::GetForFrame(
tab_2->GetPrimaryMainFrame())
->IsContentBlocked(ContentSettingsType::POPUPS));
}
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
DocumentPictureInPictureIsNotConsideredForBlocking) {
EXPECT_FALSE(blocked_content::ConsiderForPopupBlocking(
WindowOpenDisposition::NEW_PICTURE_IN_PICTURE));
}
class PopupBlockerFencedFrameTest : public PopupBlockerBrowserTest {
public:
PopupBlockerFencedFrameTest() = default;
~PopupBlockerFencedFrameTest() override = default;
content::RenderFrameHost* primary_main_frame_host() {
return browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame();
}
protected:
content::test::FencedFrameTestHelper fenced_frame_helper_;
};
IN_PROC_BROWSER_TEST_F(PopupBlockerFencedFrameTest,
AllowPopupThroughContentSettingFencedFrame) {
HostContentSettingsMap* content_settings =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
// The content setting of the main frame URL is set to allow popup.
const GURL main_frame_url(
embedded_test_server()->GetURL("a.com", "/title1.html"));
content_settings->SetContentSettingDefaultScope(main_frame_url, GURL(),
ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
// The content setting of the fenced frame URL is set to block popup.
const GURL fenced_frame_url(embedded_test_server()->GetURL(
"b.com", "/popup_blocker/popup-window-open.html"));
content_settings->SetContentSettingDefaultScope(fenced_frame_url, GURL(),
ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
// Create a fenced frame opening a popup.
content::RenderFrameHost* fenced_frame_rfh =
fenced_frame_helper_.CreateFencedFrame(primary_main_frame_host(),
fenced_frame_url);
ASSERT_NE(nullptr, fenced_frame_rfh);
// The popup should be shown even the iframe URL is blocked, since the
// top-level URL allows popups.
ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile()));
ASSERT_EQ(0, GetBlockedContentsCount());
}
IN_PROC_BROWSER_TEST_F(PopupBlockerFencedFrameTest,
BlockPopupThroughContentSettingFencedFrame) {
HostContentSettingsMap* content_settings =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
// The content setting of the main frame URL is set to block popup.
const GURL main_frame_url(
embedded_test_server()->GetURL("a.com", "/title1.html"));
content_settings->SetContentSettingDefaultScope(main_frame_url, GURL(),
ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
// The content setting of the fenced frame URL is set to allow popup.
const GURL fenced_frame_url(embedded_test_server()->GetURL(
"b.com", "/popup_blocker/popup-window-open.html"));
content_settings->SetContentSettingDefaultScope(fenced_frame_url, GURL(),
ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
// Create a fenced frame opening a popup.
content::RenderFrameHost* fenced_frame_rfh =
fenced_frame_helper_.CreateFencedFrame(primary_main_frame_host(),
fenced_frame_url);
ASSERT_NE(nullptr, fenced_frame_rfh);
// Popup should be blocked even the iframe URL is in AllowList, since the
// top-level URL blocks popups.
ASSERT_EQ(1, GetBlockedContentsCount());
}
} // namespace
|