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 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include <memory>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_request_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "media/capture/mojom/video_capture_types.mojom.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "third_party/blink/public/common/features.h"
#include "ui/gl/gl_switches.h"
namespace {
using ::content::WebContents;
using ::media::mojom::SubCaptureTargetType;
using ::testing::Bool;
using ::testing::Combine;
using ::testing::TestParamInfo;
using ::testing::Values;
using ::testing::WithParamInterface;
// Comparisons with sub-capture targets (CropTargets/RestrictionTargets) are
// limited. We can only know that a one was returned by the test. However, we
// keep this matcher around in anticipation of targets being made either
// comparable or stringifiable. Then we can do more interesting comparisons,
// like ensuring uniqueness or checking that repeatedly calling
// CropTarget.fromElement() or RestrictionTarget.fromElement() on the same
// Element yields either the same target, or an equivalent one.
MATCHER_P(IsExpectedTarget, expected_target_index, "") {
static_assert(std::is_same<decltype(arg), const std::string&>::value, "");
return arg == expected_target_index;
}
const char kMainPageTitle[] = "Region/Element Capture Test - Page 1 (Main)";
const char kOtherPageTitle[] = "Region/Element Capture Test - Page 2 (Main)";
// Tracks SubCaptureTargetIdWebContentsHelper::kMaxIdsPerWebContents.
constexpr size_t kMaxIdsPerWebContents = 100;
enum {
kMainPageTopLevelDocument,
kMainPageEmbeddedDocument,
kOtherPageTopLevelDocument,
kOtherPageEmbeddedDocument,
kMailmanServer,
kServerCount // Must be last.
};
enum Tab {
kMainTab,
kOtherTab,
kTabCount // Must be last.
};
enum class Frame {
kNone,
kTopLevelDocument,
kEmbeddedFrame,
};
enum class Track { kOriginal, kClone, kSecond };
const char* ToString(Frame frame) {
switch (frame) {
case Frame::kNone:
return "none";
case Frame::kTopLevelDocument:
return "top-level";
case Frame::kEmbeddedFrame:
return "embedded";
}
NOTREACHED();
}
const char* ToString(Track track) {
switch (track) {
case Track::kOriginal:
return "original";
case Track::kClone:
return "clone";
case Track::kSecond:
return "second";
}
NOTREACHED();
}
const char* ToString(SubCaptureTargetType type) {
switch (type) {
case SubCaptureTargetType::kCropTarget:
return "crop-target";
case SubCaptureTargetType::kRestrictionTarget:
return "restriction-target";
}
NOTREACHED();
}
// Conveniently pack together all relevant information about a tab and
// conveniently expose test controls on it.
struct TabInfo {
void StartEmbeddingFrame(const GURL& url) {
EXPECT_EQ(content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StringPrintf("startEmbeddingFrame('%s');",
url.spec().c_str())),
"embedding-done");
}
void SetUpMailman(const GURL& url) {
EXPECT_EQ(content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StringPrintf("setUpMailman('%s');",
url.spec().c_str())),
"mailman-ready");
}
void StartCapture() {
// Bring the tab into focus. This avoids getDisplayMedia rejection.
browser->tab_strip_model()->ActivateTabAt(tab_strip_index);
EXPECT_EQ(
content::EvalJs(web_contents->GetPrimaryMainFrame(), "startCapture();"),
"top-level-capture-success");
}
void StartCaptureFromEmbeddedFrame() {
// Bring the tab into focus. This avoids getDisplayMedia rejection.
browser->tab_strip_model()->ActivateTabAt(tab_strip_index);
EXPECT_EQ(content::EvalJs(web_contents->GetPrimaryMainFrame(),
"startCaptureFromEmbeddedFrame();"),
"embedded-capture-success");
}
bool StartSecondCapture(Frame frame) {
// Bring the tab into focus. This avoids getDisplayMedia rejection.
browser->tab_strip_model()->ActivateTabAt(tab_strip_index);
return content::EvalJs(
web_contents->GetPrimaryMainFrame(),
base::StringPrintf("startSecondCapture('%s');", ToString(frame)))
.ExtractString() ==
base::StrCat({ToString(frame), "-second-capture-success"});
}
bool StopCapture(Frame frame, Track track) {
return content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StringPrintf("stopCapture('%s', '%s');",
ToString(frame), ToString(track)))
.ExtractString() ==
base::StrCat({ToString(frame), "-stop-success"});
}
std::string SubCaptureTargetFromElement(
SubCaptureTargetType type,
Frame frame,
const std::string& element_id = "div") {
CHECK_NE(frame, Frame::kNone);
const std::string js_instruction =
base::StringPrintf("subCaptureTargetFromElement('%s', '%s', '%s');",
ToString(type), ToString(frame), element_id.c_str());
return content::EvalJs(web_contents->GetPrimaryMainFrame(), js_instruction)
.ExtractString();
}
// Takes as input either the sub-capture target[*], or "undefined" if the test
// wants track.cropTo(undefined) or track.restrictTo(undefined) to be invoked.
//
// [*] Actually, because CropTargets/RestrictionTargets are not stringifiable,
// an index of the CropTarget/RestrictionTarget is used, and translated by JS
// code back into the token it had previously stored.
bool ApplySubCaptureTarget(const std::string& target,
SubCaptureTargetType sub_capture_type,
Frame frame,
Track track = Track::kOriginal) {
CHECK(frame == Frame::kTopLevelDocument || frame == Frame::kEmbeddedFrame);
std::string script_result =
content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StringPrintf(
"applySubCaptureByIndex('%s', '%s', '%s', '%s');",
target.c_str(), ToString(sub_capture_type),
ToString(frame), ToString(track)))
.ExtractString();
EXPECT_EQ(0u, script_result.rfind(ToString(frame), 0)) << script_result;
return script_result == base::StringPrintf("%s-%s-success", ToString(frame),
ToString(sub_capture_type));
}
bool CloneTrack() {
std::string script_result =
content::EvalJs(web_contents->GetPrimaryMainFrame(), "clone();")
.ExtractString();
DCHECK(script_result == "clone-track-success" ||
script_result == "clone-track-failure");
return script_result == "clone-track-success";
}
bool Deallocate(Track track) {
std::string script_result =
content::EvalJs(
web_contents->GetPrimaryMainFrame(),
base::StringPrintf("deallocate('%s');", ToString(track)))
.ExtractString();
DCHECK(script_result == "deallocate-failure" ||
script_result == "deallocate-success");
return script_result == "deallocate-success";
}
bool HideElement(const char* element_id) {
std::string script_result =
content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StringPrintf("hideElement('%s');", element_id))
.ExtractString();
DCHECK(script_result == "hide-element-failure" ||
script_result == "hide-element-success");
return script_result == "hide-element-success";
}
bool CreateNewElement(Frame frame, const char* tag, const std::string& id) {
DCHECK_NE(frame, Frame::kNone);
std::string script_result =
content::EvalJs(web_contents->GetPrimaryMainFrame(),
base::StrCat({"createNewElement(\"", ToString(frame),
"\", \"", tag, "\", \"", id, "\");"}))
.ExtractString();
if (frame == Frame::kEmbeddedFrame) {
EXPECT_EQ(0u, script_result.rfind("embedded-", 0)) << script_result;
return script_result == "embedded-new-element-success";
}
DCHECK(frame == Frame::kTopLevelDocument);
EXPECT_EQ(0u, script_result.rfind("top-level-", 0)) << script_result;
return script_result == "top-level-new-element-success";
}
bool CreateNewDivElement(Frame frame, const std::string& id) {
return CreateNewElement(frame, "div", id);
}
raw_ptr<Browser, AcrossTasksDanglingUntriaged> browser;
raw_ptr<WebContents, AcrossTasksDanglingUntriaged> web_contents;
int tab_strip_index;
};
} // namespace
// Essentially depends on InProcessBrowserTest, but WebRtcTestBase provides
// detection of JS errors.
class SubCaptureBrowserTestBase : public WebRtcTestBase {
public:
SubCaptureBrowserTestBase() = default;
~SubCaptureBrowserTestBase() override = default;
void SetUpInProcessBrowserTestFixture() override {
WebRtcTestBase::SetUpInProcessBrowserTestFixture();
DetectErrorsInJavaScript();
base::FilePath test_dir;
ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
for (int i = 0; i < kServerCount; ++i) {
servers_.emplace_back(std::make_unique<net::EmbeddedTestServer>());
servers_[i]->ServeFilesFromDirectory(test_dir);
ASSERT_TRUE(servers_[i]->Start());
}
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
// MSan and GL do not get along so avoid using the GPU with MSan.
// TODO(crbug.com/40260482): Remove this after fixing feature
// detection in 0c tab capture path as it'll no longer be needed.
#if !BUILDFLAG(IS_CHROMEOS) && !defined(MEMORY_SANITIZER)
command_line->AppendSwitch(switches::kUseGpuInTests);
#endif
command_line_ = command_line;
}
void TearDownOnMainThread() override {
for (auto& server : servers_) {
if (server) {
ASSERT_TRUE(server->ShutdownAndWaitUntilComplete());
}
}
WebRtcTestBase::TearDownOnMainThread();
}
// Same as WebRtcTestBase::OpenTestPageInNewTab, but does not assume
// a single embedded server is used for all pages, and also embeds
// a cross-origin iframe.
void SetUpPage(const std::string& top_level_document_document,
net::EmbeddedTestServer* top_level_document_server,
const std::string& embedded_iframe_document,
net::EmbeddedTestServer* embedded_iframe_server,
TabInfo* tab_info) const {
chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(),
top_level_document_server->GetURL(top_level_document_document)));
WebContents* const web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
permissions::PermissionRequestManager::FromWebContents(web_contents)
->set_auto_response_for_test(
permissions::PermissionRequestManager::ACCEPT_ALL);
*tab_info = {browser(), web_contents,
browser()->tab_strip_model()->active_index()};
tab_info->StartEmbeddingFrame(
embedded_iframe_server->GetURL(embedded_iframe_document));
// Start embedding a frame whose sole purpose is to be same-origin
// across all other documents and therefore allow communication
// between them all over a shared BroadcastChannel.
tab_info->SetUpMailman(
servers_[kMailmanServer]->GetURL("/webrtc/sub_capture_mailman.html"));
}
// Set up all (necessary) tabs, loads iframes, and start capturing the
// relevant tab.
void SetUpTest(Frame capturing_entity, bool self_capture) {
// Other page (for other-tab-capture).
SetUpPage("/webrtc/sub_capture_other_main.html",
servers_[kOtherPageTopLevelDocument].get(),
"/webrtc/sub_capture_other_embedded.html",
servers_[kOtherPageEmbeddedDocument].get(), &tabs_[kOtherTab]);
// Main page (for self-capture). Instantiate it second to help it get focus.
SetUpPage("/webrtc/sub_capture_main.html",
servers_[kMainPageTopLevelDocument].get(),
"/webrtc/sub_capture_embedded.html",
servers_[kMainPageEmbeddedDocument].get(), &tabs_[kMainTab]);
DCHECK(command_line_);
command_line_->AppendSwitchASCII(
switches::kAutoSelectTabCaptureSourceByTitle,
self_capture ? kMainPageTitle : kOtherPageTitle);
if (capturing_entity == Frame::kTopLevelDocument) {
tabs_[kMainTab].StartCapture();
} else if (capturing_entity == Frame::kEmbeddedFrame) {
tabs_[kMainTab].StartCaptureFromEmbeddedFrame();
}
}
// Manipulation after SetUpCommandLine, but before capture starts,
// allows tests to set which tab to capture.
raw_ptr<base::CommandLine> command_line_ = nullptr;
// Holds the tabs manipulated by this test.
std::array<TabInfo, kTabCount> tabs_;
// Each page is served from a distinct origin, thereby proving that
// cropping/restricting works irrespective of whether iframes are
// in/out-of-process.
std::vector<std::unique_ptr<net::EmbeddedTestServer>> servers_;
base::test::ScopedFeatureList scoped_feature_list_;
};
// Test that cropTo() and restrictTo() work on some expected element types.
class SubCaptureBrowserTargetElementTest
: public SubCaptureBrowserTestBase,
public WithParamInterface<std::tuple<SubCaptureTargetType, const char*>> {
public:
SubCaptureBrowserTargetElementTest()
: sub_capture_type_(std::get<0>(GetParam())),
target_element_(std::get<1>(GetParam())) {}
~SubCaptureBrowserTargetElementTest() override = default;
void TestCanApplyToElementTag(const char* tag) {
TabInfo& tab = tabs_[kMainTab];
const std::string element_id = base::StrCat({"new_id_", tag});
ASSERT_TRUE(
tab.CreateNewElement(Frame::kTopLevelDocument, tag, element_id));
const std::string target = tab.SubCaptureTargetFromElement(
sub_capture_type_, Frame::kTopLevelDocument, element_id);
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(tab.ApplySubCaptureTarget(target, sub_capture_type_,
Frame::kTopLevelDocument));
}
protected:
const SubCaptureTargetType sub_capture_type_;
const char* const target_element_;
};
std::string SubCaptureBrowserTargetElementTestParamsToString(
const TestParamInfo<SubCaptureBrowserTargetElementTest::ParamType>& info) {
const std::string api =
std::get<0>(info.param) == SubCaptureTargetType::kCropTarget
? "RegionCapture"
: "ElementCapture";
std::string element = std::get<1>(info.param);
element[0] = std::toupper(element[0]);
return api + element;
}
INSTANTIATE_TEST_SUITE_P(
,
SubCaptureBrowserTargetElementTest,
Combine(Values(SubCaptureTargetType::kCropTarget,
SubCaptureTargetType::kRestrictionTarget),
Values("a",
"blockquote",
"body",
"button",
"canvas",
"col",
"div",
"fieldset",
"form",
"h1",
"header",
"hr"
"iframe",
"img",
"input",
"output",
"span",
"svg",
"video")),
SubCaptureBrowserTargetElementTestParamsToString);
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTargetElementTest, ApplyToElement) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TestCanApplyToElementTag(target_element_);
}
class SubCaptureBrowserTest : public SubCaptureBrowserTestBase,
public WithParamInterface<SubCaptureTargetType> {
public:
SubCaptureBrowserTest() : type_(GetParam()) {}
~SubCaptureBrowserTest() override = default;
protected:
const SubCaptureTargetType type_;
};
std::string SubCaptureBrowserTestParamsToString(
const TestParamInfo<SubCaptureBrowserTest::ParamType>& info) {
return info.param == SubCaptureTargetType::kCropTarget ? "RegionCapture"
: "ElementCapture";
}
INSTANTIATE_TEST_SUITE_P(,
SubCaptureBrowserTest,
Values(SubCaptureTargetType::kCropTarget,
SubCaptureTargetType::kRestrictionTarget),
SubCaptureBrowserTestParamsToString);
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest,
FromElementReturnsValidIdInMainPage) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
EXPECT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument),
IsExpectedTarget("0"));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest,
FromElementReturnsValidIdInCrossOriginIframe) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
EXPECT_THAT(
tabs_[kMainTab].SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame),
IsExpectedTarget("0"));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest,
SubCaptureAllowedIfTopLevelAppliesToElementInTopLevel) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument);
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(
tab.ApplySubCaptureTarget(target, type_, Frame::kTopLevelDocument));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest,
SubCaptureAllowedIfTopLevelAppliesToElementInEmbedded) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame);
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(
tab.ApplySubCaptureTarget(target, type_, Frame::kTopLevelDocument));
}
IN_PROC_BROWSER_TEST_P(
SubCaptureBrowserTest,
SubCaptureAllowedIfEmbeddedFrameAppliesToElementInTopLevel) {
SetUpTest(Frame::kEmbeddedFrame, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument);
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(tab.ApplySubCaptureTarget(target, type_, Frame::kEmbeddedFrame));
}
IN_PROC_BROWSER_TEST_P(
SubCaptureBrowserTest,
SubCaptureAllowedIfEmbeddedFrameAppliesToElementInEmbedded) {
SetUpTest(Frame::kEmbeddedFrame, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame);
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(tab.ApplySubCaptureTarget(target, type_, Frame::kEmbeddedFrame));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest, SubCaptureAllowedToUndo) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument);
ASSERT_THAT(target, IsExpectedTarget("0"));
ASSERT_TRUE(
tab.ApplySubCaptureTarget(target, type_, Frame::kTopLevelDocument));
EXPECT_TRUE(
tab.ApplySubCaptureTarget("undefined", type_, Frame::kTopLevelDocument));
}
IN_PROC_BROWSER_TEST_P(
SubCaptureBrowserTest,
MayAttemptToUndoSubCaptureOnTracksWhereSubCaptureHasNotBeenApplied) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument);
ASSERT_THAT(target, IsExpectedTarget("0"));
// ApplySubCaptureTarget(target) is intentionally not called.
// Instead, the test immediately calls ApplySubCaptureTarget(undefined) on a
// still-uncropped-and-unrestricted track, attempting to undo sub-capture
// although it's not been applied.
EXPECT_TRUE(
tab.ApplySubCaptureTarget("undefined", type_, Frame::kTopLevelDocument));
}
// The Promise resolves when it's guaranteed that no additional frames will be
// issued with an earlier sub-capture-target version. That an actual frame be
// issued at all, let alone with the new sub-capture-target version, is not
// actually required, or else these promises could languish unfulfilled
// indefinitely.
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest,
SubCaptureOfInvisibleElementResolvesInTimelyFashion) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
TabInfo& tab = tabs_[kMainTab];
ASSERT_TRUE(tab.HideElement("div"));
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument, "div");
ASSERT_THAT(target, IsExpectedTarget("0"));
EXPECT_TRUE(
tab.ApplySubCaptureTarget(target, type_, Frame::kTopLevelDocument));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest, MaxIdsInTopLevelDocument) {
SetUpTest(Frame::kNone, /*self_capture=*/false);
TabInfo& tab = tabs_[kMainTab];
for (size_t i = 0; i < kMaxIdsPerWebContents; ++i) {
const std::string element_id = ("new_id_" + base::NumberToString(i));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kTopLevelDocument, element_id));
const std::string target = tab.SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument, element_id);
ASSERT_THAT(target, IsExpectedTarget(base::NumberToString(i)));
}
// Create one more element - this one won't get a sub-capture-target.
const std::string element_id =
("new_id_" + base::NumberToString(kMaxIdsPerWebContents));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kTopLevelDocument, element_id));
EXPECT_EQ(tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument,
element_id),
base::StringPrintf("top-level-produce-%s-error", ToString(type_)));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest, MaxIdsInEmbeddedFrame) {
SetUpTest(Frame::kNone, /*self_capture=*/false);
TabInfo& tab = tabs_[kMainTab];
for (size_t i = 0; i < kMaxIdsPerWebContents; ++i) {
const std::string element_id = ("new_id_" + base::NumberToString(i));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kEmbeddedFrame, element_id));
const std::string target = tab.SubCaptureTargetFromElement(
type_, Frame::kEmbeddedFrame, element_id);
ASSERT_THAT(target, IsExpectedTarget(base::NumberToString(i)));
}
// Create one more element - this one won't get a sub-capture-target.
const std::string element_id =
("new_id_" + base::NumberToString(kMaxIdsPerWebContents));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kEmbeddedFrame, element_id));
EXPECT_EQ(
tab.SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame, element_id),
base::StringPrintf("embedded-produce-%s-error", ToString(type_)));
}
IN_PROC_BROWSER_TEST_P(SubCaptureBrowserTest, MaxIdsSharedBetweenFramesInTab) {
SetUpTest(Frame::kNone, /*self_capture=*/false);
TabInfo& tab = tabs_[kMainTab];
static_assert(kMaxIdsPerWebContents > 1, "");
// Create (kMaxIdsPerWebContents - 1) new elements and assign each a
// sub-capture-target.
for (size_t i = 0; i < kMaxIdsPerWebContents - 1; ++i) {
const std::string element_id = ("new_id_" + base::NumberToString(i));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kTopLevelDocument, element_id));
const std::string target = tab.SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument, element_id);
ASSERT_THAT(target, IsExpectedTarget(base::NumberToString(i)));
}
// One more in the embedded frame is possible.
std::string element_id =
("new_id_" + base::NumberToString(kMaxIdsPerWebContents - 1));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kEmbeddedFrame, element_id));
const std::string target =
tab.SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame, element_id);
EXPECT_THAT(target, IsExpectedTarget(
base::NumberToString(kMaxIdsPerWebContents - 1)));
// Create one more element - this one won't get a sub-capture-target.
element_id = ("new_id_" + base::NumberToString(kMaxIdsPerWebContents));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kTopLevelDocument, element_id));
EXPECT_EQ(tab.SubCaptureTargetFromElement(type_, Frame::kTopLevelDocument,
element_id),
base::StringPrintf("top-level-produce-%s-error", ToString(type_)));
// Neither in the top-level nor in the embedded frame.
element_id = ("new_id_" + base::NumberToString(kMaxIdsPerWebContents + 1));
ASSERT_TRUE(tab.CreateNewDivElement(Frame::kEmbeddedFrame, element_id));
EXPECT_EQ(
tab.SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame, element_id),
base::StringPrintf("embedded-produce-%s-error", ToString(type_)));
}
// Tests related to behavior when cloning.
class SubCaptureClonesBrowserTestBase : public SubCaptureBrowserTestBase {
public:
static constexpr char kTarget0[] = "0";
static constexpr char kTarget1[] = "1";
~SubCaptureClonesBrowserTestBase() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII("js-flags", "--expose-gc");
SubCaptureBrowserTestBase::SetUpCommandLine(command_line);
}
// Has to be called from within the test's body.
void ManualSetUp(SubCaptureTargetType type) {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
EXPECT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type, Frame::kTopLevelDocument, "div"),
IsExpectedTarget(kTarget0));
EXPECT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type, Frame::kTopLevelDocument, "embedded_frame"),
IsExpectedTarget(kTarget1));
}
bool ApplySubCaptureTarget(SubCaptureTargetType type,
const std::string& target,
Frame frame,
Track track) {
return tabs_[kMainTab].ApplySubCaptureTarget(target, type, frame, track);
}
bool CloneTrack() { return tabs_[kMainTab].CloneTrack(); }
bool Deallocate(Track track) { return tabs_[kMainTab].Deallocate(track); }
};
class SubCaptureClonesBrowserTest
: public SubCaptureClonesBrowserTestBase,
public WithParamInterface<SubCaptureTargetType> {
public:
SubCaptureClonesBrowserTest() : type_(GetParam()) {}
~SubCaptureClonesBrowserTest() override = default;
protected:
const SubCaptureTargetType type_;
};
std::string SubCaptureClonesBrowserTestParamsToString(
const TestParamInfo<SubCaptureClonesBrowserTest::ParamType>& info) {
return info.param == SubCaptureTargetType::kCropTarget ? "RegionCapture"
: "ElementCapture";
}
INSTANTIATE_TEST_SUITE_P(,
SubCaptureClonesBrowserTest,
Values(SubCaptureTargetType::kCropTarget,
SubCaptureTargetType::kRestrictionTarget),
SubCaptureClonesBrowserTestParamsToString);
// Sanity cloning 1/2.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CanCloneTracksWhichHadNoSubCaptureApplied) {
ManualSetUp(type_);
EXPECT_TRUE(CloneTrack());
}
// Sanity cloning 2/2.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CanCloneTrackWhichHaveSubCaptureApplied) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
EXPECT_TRUE(CloneTrack());
}
// Restrictions on cloned tracks 1/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotApplySubCaptureOnClones) {
ManualSetUp(type_);
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kClone));
}
// Restrictions on cloned tracks 2/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotReapplySubCaptureOnClones) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kClone));
}
// Restrictions on cloned tracks 3/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotUndoSubCaptureOnClones) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(type_, "undefined",
Frame::kTopLevelDocument, Track::kClone));
}
// Restrictions on original track that has a clone 1/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotApplySubCaptureOnTracksThatHaveClones) {
ManualSetUp(type_);
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
}
// Restrictions on original track that has a clone 2/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotReapplySubCaptureOnTracksThatHaveClones) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
}
// Restrictions on original track that has a clone 3/3.
IN_PROC_BROWSER_TEST_P(SubCaptureClonesBrowserTest,
CannotUndoSubCaptureOnTracksThatHaveClones) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
EXPECT_FALSE(ApplySubCaptureTarget(
type_, "undefined", Frame::kTopLevelDocument, Track::kOriginal));
}
// Original track becomes unblocked for sub-capture after clone is GCed 1/3.
//
// TODO(crbug.com/40858400) Re-enable for macOS and ChromeOS after flakes are
// resolved.
// TODO(crbug.com/40919381): Also flakes on linux-bfcache-rel, so turning the
// test off entirely.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanApplySubCaptureOnOriginalTrackAfterCloneIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(Deallocate(Track::kClone));
EXPECT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
}
// Original track becomes unblocked for sub-capture after clone is GCed 2/3.
//
// TODO(crbug.com/40858400) Re-enable after flakes are resolved.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanReapplySubCaptureOnOriginalTrackAfterCloneIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(Deallocate(Track::kClone));
EXPECT_TRUE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
}
// Original track becomes unblocked for sub-capture clone is GCed 3/3.
//
// TODO(crbug.com/40860614): Re-enable this test.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanUndoSubCaptureOnOriginalTrackAfterCloneIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(
type_, "undefined", Frame::kTopLevelDocument, Track::kOriginal));
ASSERT_TRUE(Deallocate(Track::kClone));
EXPECT_TRUE(ApplySubCaptureTarget(
type_, "undefined", Frame::kTopLevelDocument, Track::kOriginal));
}
// Cloned track becomes unblocked for sub-capture after original is GCed 1/3.
//
// The following test is disabled because of a loosely-related issue,
// where an original track is kept alive if a clone exists, but not vice versa.
// TODO(crbug.com/40845775): Uncomment after fixing the aforementioned issue.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanApplySubCaptureOnCloneAfterOriginalTrackIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kClone));
ASSERT_TRUE(Deallocate(Track::kOriginal));
EXPECT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kClone));
}
// Cloned track becomes unblocked for sub-capture after original is GCed 2/3.
//
// The following test is disabled because of a loosely-related issue,
// where an original track is kept alive if a clone exists, but not vice versa.
// TODO(crbug.com/40845775): Uncomment after fixing the aforementioned issue.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanReapplySubCaptureOnCloneAfterOriginalTrackIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kClone));
ASSERT_TRUE(Deallocate(Track::kOriginal));
EXPECT_TRUE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kClone));
}
// Cloned track becomes unblocked for sub-capture after original is GCed 3/3.
//
// The following test is disabled because of a loosely-related issue,
// where an original track is kept alive if a clone exists, but not vice versa.
// TODO(crbug.com/40845775): Uncomment after fixing the aforementioned issue.
IN_PROC_BROWSER_TEST_P(
SubCaptureClonesBrowserTest,
DISABLED_CanUndoSubCaptureOnCloneAfterOriginalTrackIsGarbageCollected) {
ManualSetUp(type_);
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget0, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(CloneTrack());
ASSERT_FALSE(ApplySubCaptureTarget(type_, "undefined",
Frame::kTopLevelDocument, Track::kClone));
ASSERT_TRUE(Deallocate(Track::kOriginal));
EXPECT_TRUE(ApplySubCaptureTarget(type_, "undefined",
Frame::kTopLevelDocument, Track::kClone));
}
// Tests similar issues to cloning, but with multiple captures triggering
// these issues instead.
class SubCaptureMultiCaptureBrowserTest
: public SubCaptureClonesBrowserTestBase,
public WithParamInterface<std::tuple<SubCaptureTargetType, Frame>> {
public:
SubCaptureMultiCaptureBrowserTest()
: type_(std::get<0>(GetParam())),
second_capture_frame_(std::get<1>(GetParam())) {}
~SubCaptureMultiCaptureBrowserTest() override = default;
// Has to be called from within the test's body.
void ManualSetUp() {
SetUpTest(Frame::kTopLevelDocument, /*self_capture=*/true);
EXPECT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument, "div"),
IsExpectedTarget(kTarget0));
EXPECT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument, "embedded_frame"),
IsExpectedTarget(kTarget1));
}
bool StartSecondCapture() {
return tabs_[kMainTab].StartSecondCapture(second_capture_frame_);
}
bool StopCapture(Track track) {
return tabs_[kMainTab].StopCapture(Frame::kTopLevelDocument, track);
}
protected:
const SubCaptureTargetType type_;
const Frame second_capture_frame_;
};
std::string SubCaptureMultiCaptureBrowserTestParamsToString(
const TestParamInfo<SubCaptureMultiCaptureBrowserTest::ParamType>& info) {
const std::string api =
std::get<0>(info.param) == SubCaptureTargetType::kCropTarget
? "RegionCapture"
: "ElementCapture";
const std::string frame = std::get<1>(info.param) == Frame::kTopLevelDocument
? "TopLevelFrame"
: "EmbeddedFrame";
return base::StrCat({api, "AndSecondCaptureFrameIs", frame});
}
INSTANTIATE_TEST_SUITE_P(
,
SubCaptureMultiCaptureBrowserTest,
Combine(Values(SubCaptureTargetType::kCropTarget,
SubCaptureTargetType::kRestrictionTarget),
Values(Frame::kTopLevelDocument, Frame::kEmbeddedFrame)),
SubCaptureMultiCaptureBrowserTestParamsToString);
IN_PROC_BROWSER_TEST_P(SubCaptureMultiCaptureBrowserTest,
CanSelfCaptureAgainIfNeverAppliedSubCapture) {
ManualSetUp();
EXPECT_TRUE(StartSecondCapture());
}
IN_PROC_BROWSER_TEST_P(SubCaptureMultiCaptureBrowserTest,
CannotSelfCaptureAgainIfAppliedSubCapture) {
ManualSetUp();
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
EXPECT_FALSE(StartSecondCapture());
}
IN_PROC_BROWSER_TEST_P(SubCaptureMultiCaptureBrowserTest,
CannotSelfCaptureAgainIfSubCapturedAppliedAndUnapplied) {
ManualSetUp();
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(ApplySubCaptureTarget(
type_, "undefined", Frame::kTopLevelDocument, Track::kOriginal));
EXPECT_FALSE(StartSecondCapture());
}
IN_PROC_BROWSER_TEST_P(SubCaptureMultiCaptureBrowserTest,
CanSelfCaptureAgainIfSubCaptureSessionStopped) {
ManualSetUp();
ASSERT_TRUE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
ASSERT_TRUE(StopCapture(Track::kOriginal));
EXPECT_TRUE(StartSecondCapture());
}
IN_PROC_BROWSER_TEST_P(
SubCaptureMultiCaptureBrowserTest,
CannotApplySubCaptureIfMultipleSelfCaptureSessionsExist) {
ManualSetUp();
ASSERT_TRUE(StartSecondCapture());
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget1, Frame::kTopLevelDocument,
Track::kOriginal));
EXPECT_FALSE(ApplySubCaptureTarget(type_, kTarget1, second_capture_frame_,
Track::kSecond));
}
// Suite of tests ensuring that only self-capture sessions may be the target of
// sub-capture, and that the app may only apply sub-capture using targets in its
// own tab. (However, any target in the current tab is permitted.)
class SubCaptureSelfCaptureOnlyBrowserTest
: public SubCaptureBrowserTestBase,
public WithParamInterface<
std::tuple<SubCaptureTargetType, Frame, bool, Tab, Frame>> {
public:
SubCaptureSelfCaptureOnlyBrowserTest()
: type_(std::get<0>(GetParam())),
capturing_entity_(std::get<1>(GetParam())),
self_capture_(std::get<2>(GetParam())),
target_element_tab_(std::get<3>(GetParam())),
target_frame_(std::get<4>(GetParam())) {}
~SubCaptureSelfCaptureOnlyBrowserTest() override = default;
protected:
// Whether Region Capture or Element Capture is tested.
const SubCaptureTargetType type_;
// The capture is done from kMainTab in all instances of this parameterized
// test. |capturing_entity_| controls whether the capture is initiated
// from the top-level document of said tab, or an embedded frame.
const Frame capturing_entity_;
// Whether capturing self, or capturing the other tab.
const bool self_capture_;
// Determines the the element on whose sub-capture-target we'll call cropTo()
// or restrictTo():
// * |target_element_tab_| - whether it's in kMainTab or in kOtherTab.
// * |target_frame_| - whether it's in the top-level or an embedded frame.
const Tab target_element_tab_;
const Frame target_frame_; // Top-level or embedded frame.
};
std::string SubCaptureSelfCaptureOnlyBrowserTestParamsToString(
const TestParamInfo<SubCaptureSelfCaptureOnlyBrowserTest::ParamType>&
info) {
return base::StrCat(
{std::get<0>(info.param) == SubCaptureTargetType::kCropTarget
? "RegionCapture"
: "ElementCapture",
std::get<1>(info.param) == Frame::kTopLevelDocument ? "TopLevel"
: "EmbeddedFrame",
std::get<2>(info.param) ? "SelfCapturing" : "CapturingOtherTab",
"AndApplyingToElementIn",
std::get<3>(info.param) == kMainTab ? "OwnTabs" : "OtherTabs",
std::get<4>(info.param) == Frame::kTopLevelDocument ? "TopLevel"
: "EmbeddedFrame"});
}
INSTANTIATE_TEST_SUITE_P(
,
SubCaptureSelfCaptureOnlyBrowserTest,
Combine(Values(SubCaptureTargetType::kCropTarget,
SubCaptureTargetType::kRestrictionTarget),
Values(Frame::kTopLevelDocument, Frame::kEmbeddedFrame),
Bool(),
Values(kMainTab, kOtherTab),
Values(Frame::kTopLevelDocument, Frame::kEmbeddedFrame)),
SubCaptureSelfCaptureOnlyBrowserTestParamsToString);
IN_PROC_BROWSER_TEST_P(SubCaptureSelfCaptureOnlyBrowserTest, ApplySubCapture) {
SetUpTest(capturing_entity_, self_capture_);
// Prevent test false-positive - ensure that both tabs participating in the
// test have at least one associated sub-capture-target, or otherwise they
// would not have a SubCaptureTargetIdWebContentsHelper.
// To make things even clearer, ensure both the top-level and the embedded
// frame have produced sub-capture-targets. (This should not be necessary,
// but is done as an extra buffer against false-positives.)
ASSERT_THAT(tabs_[kMainTab].SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument),
IsExpectedTarget("0"));
ASSERT_THAT(
tabs_[kMainTab].SubCaptureTargetFromElement(type_, Frame::kEmbeddedFrame),
IsExpectedTarget("1"));
ASSERT_THAT(tabs_[kOtherTab].SubCaptureTargetFromElement(
type_, Frame::kTopLevelDocument),
IsExpectedTarget("2"));
ASSERT_THAT(tabs_[kOtherTab].SubCaptureTargetFromElement(
type_, Frame::kEmbeddedFrame),
IsExpectedTarget("3"));
const std::string target =
tabs_[target_element_tab_].SubCaptureTargetFromElement(type_,
target_frame_);
ASSERT_THAT(target, IsExpectedTarget("4"));
// Apply sub-capture only permitted if both conditions hold.
const bool expect_permitted =
(self_capture_ && target_element_tab_ == kMainTab);
EXPECT_EQ(expect_permitted, tabs_[kMainTab].ApplySubCaptureTarget(
target, type_, capturing_entity_));
}
|