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 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/media_controls/media_controls_impl.h"
#include <limits>
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/mojom/input/focus_type.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_pointer_event_init.h"
#include "third_party/blink/renderer/core/css/css_property_value_set.h"
#include "third_party/blink/renderer/core/css/document_style_environment_variables.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/document_parser.h"
#include "third_party/blink/renderer/core/dom/dom_token_list.h"
#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/core/html/shadow/shadow_element_names.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/input/event_handler.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/loader/empty_clients.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_cast_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_current_time_display_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_download_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_fullscreen_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_mute_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_overflow_menu_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_overflow_menu_list_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_overlay_play_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_play_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_playback_speed_button_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_remaining_time_display_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_timeline_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_volume_slider_element.h"
#include "third_party/blink/renderer/modules/remoteplayback/remote_playback.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/media/remote_playback_client.h"
#include "third_party/blink/renderer/platform/testing/empty_web_media_player.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/web_test_support.h"
#include "ui/display/mojom/screen_orientation.mojom-blink.h"
#include "ui/display/screen_info.h"
// The MediaTimelineWidths histogram suffix expected to be encountered in these
// tests.
#define TIMELINE_W "256_511"
namespace blink {
namespace {
class FakeChromeClient : public EmptyChromeClient {
public:
FakeChromeClient() {
screen_info_.orientation_type =
display::mojom::blink::ScreenOrientation::kLandscapePrimary;
}
// ChromeClient overrides.
const display::ScreenInfo& GetScreenInfo(LocalFrame&) const override {
return screen_info_;
}
private:
display::ScreenInfo screen_info_;
};
class MockWebMediaPlayerForImpl : public EmptyWebMediaPlayer {
public:
// WebMediaPlayer overrides:
WebTimeRanges Seekable() const override { return seekable_; }
bool HasVideo() const override { return true; }
bool HasAudio() const override { return has_audio_; }
bool has_audio_ = false;
WebTimeRanges seekable_;
};
class StubLocalFrameClientForImpl : public EmptyLocalFrameClient {
public:
std::unique_ptr<WebMediaPlayer> CreateWebMediaPlayer(
HTMLMediaElement&,
const WebMediaPlayerSource&,
WebMediaPlayerClient*) override {
return std::make_unique<MockWebMediaPlayerForImpl>();
}
RemotePlaybackClient* CreateRemotePlaybackClient(
HTMLMediaElement& element) override {
return &RemotePlayback::From(element);
}
};
Element* GetElementByShadowPseudoId(Node& root_node,
const char* shadow_pseudo_id) {
for (Element& element : ElementTraversal::DescendantsOf(root_node)) {
if (element.ShadowPseudoId() == shadow_pseudo_id)
return &element;
}
return nullptr;
}
bool IsElementVisible(Element& element) {
const CSSPropertyValueSet* inline_style = element.InlineStyle();
if (!inline_style)
return element.getAttribute(html_names::kClassAttr) != "transparent";
if (inline_style->GetPropertyValue(CSSPropertyID::kDisplay) == "none")
return false;
if (inline_style->HasProperty(CSSPropertyID::kOpacity) &&
inline_style->GetPropertyValue(CSSPropertyID::kOpacity).ToDouble() ==
0.0) {
return false;
}
if (inline_style->GetPropertyValue(CSSPropertyID::kVisibility) == "hidden")
return false;
if (Element* parent = element.parentElement())
return IsElementVisible(*parent);
return true;
}
void SimulateTransitionEnd(Element& element) {
element.DispatchEvent(*Event::Create(event_type_names::kTransitionend));
}
// This must match MediaControlDownloadButtonElement::DownloadActionMetrics.
enum DownloadActionMetrics {
kShown = 0,
kClicked,
kCount // Keep last.
};
} // namespace
class MediaControlsImplTest
: public PageTestBase,
private ScopedMediaCastOverlayButtonForTest,
private ScopedMediaControlsOverlayPlayButtonForTest {
public:
explicit MediaControlsImplTest(
base::test::TaskEnvironment::TimeSource time_source)
: PageTestBase(time_source),
ScopedMediaCastOverlayButtonForTest(true),
ScopedMediaControlsOverlayPlayButtonForTest(true) {}
MediaControlsImplTest()
: ScopedMediaCastOverlayButtonForTest(true),
ScopedMediaControlsOverlayPlayButtonForTest(true) {}
protected:
void SetUp() override {
InitializePage();
}
void InitializePage() {
SetupPageWithClients(MakeGarbageCollected<FakeChromeClient>(),
MakeGarbageCollected<StubLocalFrameClientForImpl>());
GetDocument().write("<video controls>");
auto& video = To<HTMLVideoElement>(
*GetDocument().QuerySelector(AtomicString("video")));
media_controls_ = static_cast<MediaControlsImpl*>(video.GetMediaControls());
// Scripts are disabled by default which forces controls to be on.
GetFrame().GetSettings()->SetScriptEnabled(true);
}
void SetMediaControlsFromElement(HTMLMediaElement& elm) {
media_controls_ = static_cast<MediaControlsImpl*>(elm.GetMediaControls());
}
void SimulateRemotePlaybackAvailable() {
RemotePlayback::From(media_controls_->MediaElement())
.AvailabilityChangedForTesting(/* screen_is_available */ true);
}
void EnsureSizing() {
// Fire the size-change callback to ensure that the controls have
// been properly notified of the video size.
media_controls_->NotifyElementSizeChanged(
media_controls_->MediaElement().GetBoundingClientRect());
}
void SetElementHeight(int height) {
auto* size = media_controls_->MediaElement().GetBoundingClientRect();
media_controls_->NotifyElementSizeChanged(DOMRectReadOnly::FromRect(
gfx::Rect(size->left(), size->top(), size->width(), height)));
test::RunPendingTasks();
}
void SimulateHideMediaControlsTimerFired() {
media_controls_->HideMediaControlsTimerFired(nullptr);
}
void SimulateLoadedMetadata() { media_controls_->OnLoadedMetadata(); }
void SimulateOnSeeking() { media_controls_->OnSeeking(); }
void SimulateOnSeeked() { media_controls_->OnSeeked(); }
void SimulateOnWaiting() { media_controls_->OnWaiting(); }
void SimulateOnPlaying() { media_controls_->OnPlaying(); }
void SimulateMediaControlPlaying() {
MediaControls().MediaElement().SetReadyState(
HTMLMediaElement::kHaveEnoughData);
MediaControls().MediaElement().SetNetworkState(
WebMediaPlayer::NetworkState::kNetworkStateLoading);
}
void SimulateMediaControlPlayingForFutureData() {
MediaControls().MediaElement().SetReadyState(
HTMLMediaElement::kHaveFutureData);
MediaControls().MediaElement().SetNetworkState(
WebMediaPlayer::NetworkState::kNetworkStateLoading);
}
void SimulateMediaControlBuffering() {
MediaControls().MediaElement().SetReadyState(
HTMLMediaElement::kHaveCurrentData);
MediaControls().MediaElement().SetNetworkState(
WebMediaPlayer::NetworkState::kNetworkStateLoading);
}
MediaControlsImpl& MediaControls() { return *media_controls_; }
MediaControlVolumeSliderElement* VolumeSliderElement() const {
return media_controls_->volume_slider_.Get();
}
MediaControlTimelineElement* TimelineElement() const {
return media_controls_->timeline_.Get();
}
Element* TimelineTrackElement() const {
if (!TimelineElement())
return nullptr;
return &TimelineElement()->GetTrackElement();
}
MediaControlCurrentTimeDisplayElement* GetCurrentTimeDisplayElement() const {
return media_controls_->current_time_display_.Get();
}
MediaControlRemainingTimeDisplayElement* GetRemainingTimeDisplayElement()
const {
return media_controls_->duration_display_.Get();
}
MediaControlMuteButtonElement* MuteButtonElement() const {
return media_controls_->mute_button_.Get();
}
MediaControlCastButtonElement* CastButtonElement() const {
return media_controls_->cast_button_.Get();
}
MediaControlDownloadButtonElement* DownloadButtonElement() const {
return media_controls_->download_button_.Get();
}
MediaControlFullscreenButtonElement* FullscreenButtonElement() const {
return media_controls_->fullscreen_button_.Get();
}
MediaControlPlaybackSpeedButtonElement* PlaybackSpeedButtonElement() const {
return media_controls_->playback_speed_button_.Get();
}
MediaControlPlayButtonElement* PlayButtonElement() const {
return media_controls_->play_button_.Get();
}
MediaControlOverflowMenuButtonElement* OverflowMenuButtonElement() const {
return media_controls_->overflow_menu_.Get();
}
MediaControlOverflowMenuListElement* OverflowMenuListElement() const {
return media_controls_->overflow_list_.Get();
}
MediaControlOverlayPlayButtonElement* OverlayPlayButtonElement() const {
return media_controls_->overlay_play_button_.Get();
}
MockWebMediaPlayerForImpl* WebMediaPlayer() {
return static_cast<MockWebMediaPlayerForImpl*>(
MediaControls().MediaElement().GetWebMediaPlayer());
}
base::HistogramTester& GetHistogramTester() { return histogram_tester_; }
void LoadMediaWithDuration(double duration) {
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
test::RunPendingTasks();
WebMediaPlayer()->seekable_ = WebTimeRanges(0.0, duration);
MediaControls().MediaElement().DurationChanged(duration,
false /* requestSeek */);
SimulateLoadedMetadata();
}
void SetHasAudio(bool has_audio) { WebMediaPlayer()->has_audio_ = has_audio; }
void ClickOverflowButton() {
MediaControls()
.download_button_->OverflowElementForTests()
->DispatchSimulatedClick(nullptr);
}
void SetReady() {
MediaControls().MediaElement().SetReadyState(
HTMLMediaElement::kHaveEnoughData);
}
// Set the focus .
void FocusElement(
Element* element,
mojom::blink::FocusType focus_type = blink::mojom::FocusType::kMouse) {
// GetDocument().SetLastFocusType(focus_type);
FocusParams params(SelectionBehaviorOnFocus::kNone, focus_type, nullptr);
GetDocument().SetFocusedElement(element, params);
// element->SetFocused(true, focus_type);
// Doesn't matter what, but we want some event to trigger the show / hide
// logic in the controls.
// MediaControls().DispatchEvent(*Event::Create(event_type_names::kPointerout));
}
// Clear the focus from `element`, as if some other element was focused.
void UnfocusElement(
Element* element,
mojom::blink::FocusType focus_type = blink::mojom::FocusType::kMouse) {
GetDocument().SetLastFocusType(focus_type);
element->SetFocused(false, focus_type);
MediaControls().DispatchEvent(
*Event::Create(event_type_names::kPointerout));
}
void MouseDownAt(gfx::PointF pos);
void MouseMoveTo(gfx::PointF pos);
void MouseUpAt(gfx::PointF pos);
void GestureTapAt(gfx::PointF pos);
void GestureDoubleTapAt(gfx::PointF pos);
bool HasAvailabilityCallbacks(RemotePlayback& remote_playback) {
return !remote_playback.availability_callbacks_.empty();
}
const String GetDisplayedTime(MediaControlTimeDisplayElement* display) {
return To<Text>(display->firstChild())->data();
}
bool IsOverflowElementVisible(MediaControlInputElement& element) {
MediaControlInputElement* overflow_element =
element.OverflowElementForTests();
if (!overflow_element)
return false;
Element* overflow_parent_label = overflow_element->parentElement();
if (!overflow_parent_label)
return false;
const CSSPropertyValueSet* inline_style =
overflow_parent_label->InlineStyle();
if (inline_style->GetPropertyValue(CSSPropertyID::kDisplay) == "none")
return false;
return true;
}
PointerEvent* CreatePointerEvent(const AtomicString& name) {
PointerEventInit* init = PointerEventInit::Create();
return PointerEvent::Create(name, init);
}
private:
Persistent<MediaControlsImpl> media_controls_;
base::HistogramTester histogram_tester_;
};
void MediaControlsImplTest::MouseDownAt(gfx::PointF pos) {
WebMouseEvent mouse_down_event(WebInputEvent::Type::kMouseDown,
pos /* client pos */, pos /* screen pos */,
WebPointerProperties::Button::kLeft, 1,
WebInputEvent::Modifiers::kLeftButtonDown,
WebInputEvent::GetStaticTimeStampForTests());
mouse_down_event.SetFrameScale(1);
GetDocument().GetFrame()->GetEventHandler().HandleMousePressEvent(
mouse_down_event);
}
void MediaControlsImplTest::MouseMoveTo(gfx::PointF pos) {
WebMouseEvent mouse_move_event(WebInputEvent::Type::kMouseMove,
pos /* client pos */, pos /* screen pos */,
WebPointerProperties::Button::kLeft, 1,
WebInputEvent::Modifiers::kLeftButtonDown,
WebInputEvent::GetStaticTimeStampForTests());
mouse_move_event.SetFrameScale(1);
GetDocument().GetFrame()->GetEventHandler().HandleMouseMoveEvent(
mouse_move_event, {}, {});
}
void MediaControlsImplTest::MouseUpAt(gfx::PointF pos) {
WebMouseEvent mouse_up_event(
WebMouseEvent::Type::kMouseUp, pos /* client pos */, pos /* screen pos */,
WebPointerProperties::Button::kLeft, 1, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
mouse_up_event.SetFrameScale(1);
GetDocument().GetFrame()->GetEventHandler().HandleMouseReleaseEvent(
mouse_up_event);
}
void MediaControlsImplTest::GestureTapAt(gfx::PointF pos) {
WebGestureEvent gesture_tap_event(
WebInputEvent::Type::kGestureTap, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
// Adjust |pos| by current frame scale.
float frame_scale = GetDocument().GetFrame()->LayoutZoomFactor();
gesture_tap_event.SetFrameScale(frame_scale);
pos.Scale(frame_scale);
gesture_tap_event.SetPositionInWidget(pos);
// Fire the event.
GetDocument().GetFrame()->GetEventHandler().HandleGestureEvent(
gesture_tap_event);
}
void MediaControlsImplTest::GestureDoubleTapAt(gfx::PointF pos) {
GestureTapAt(pos);
GestureTapAt(pos);
}
TEST_F(MediaControlsImplTest, HideAndShow) {
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
ASSERT_TRUE(IsElementVisible(*panel));
MediaControls().Hide();
ASSERT_FALSE(IsElementVisible(*panel));
MediaControls().MaybeShow();
ASSERT_TRUE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTest, Reset) {
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
ASSERT_TRUE(IsElementVisible(*panel));
MediaControls().Reset();
ASSERT_TRUE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTest, HideAndReset) {
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
ASSERT_TRUE(IsElementVisible(*panel));
MediaControls().Hide();
ASSERT_FALSE(IsElementVisible(*panel));
MediaControls().Reset();
ASSERT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTest, ResetDoesNotTriggerInitialLayout) {
Document& document = GetDocument();
int old_element_count = document.GetStyleEngine().StyleForElementCount();
// Also assert that there are no layouts yet.
ASSERT_EQ(0, old_element_count);
MediaControls().Reset();
int new_element_count = document.GetStyleEngine().StyleForElementCount();
ASSERT_EQ(old_element_count, new_element_count);
}
TEST_F(MediaControlsImplTest, CastButtonRequiresRoute) {
EnsureSizing();
MediaControlCastButtonElement* cast_button = CastButtonElement();
ASSERT_NE(nullptr, cast_button);
ASSERT_FALSE(IsOverflowElementVisible(*cast_button));
SimulateRemotePlaybackAvailable();
ASSERT_TRUE(IsOverflowElementVisible(*cast_button));
}
TEST_F(MediaControlsImplTest, CastButtonDisableRemotePlaybackAttr) {
EnsureSizing();
MediaControlCastButtonElement* cast_button = CastButtonElement();
ASSERT_NE(nullptr, cast_button);
ASSERT_FALSE(IsOverflowElementVisible(*cast_button));
SimulateRemotePlaybackAvailable();
ASSERT_TRUE(IsOverflowElementVisible(*cast_button));
MediaControls().MediaElement().SetBooleanAttribute(
html_names::kDisableremoteplaybackAttr, true);
test::RunPendingTasks();
ASSERT_FALSE(IsOverflowElementVisible(*cast_button));
MediaControls().MediaElement().SetBooleanAttribute(
html_names::kDisableremoteplaybackAttr, false);
test::RunPendingTasks();
ASSERT_TRUE(IsOverflowElementVisible(*cast_button));
}
TEST_F(MediaControlsImplTest, CastOverlayDefault) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
SimulateRemotePlaybackAvailable();
ASSERT_TRUE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayDisabled) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
ScopedMediaCastOverlayButtonForTest media_cast_overlay_button(false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
SimulateRemotePlaybackAvailable();
ASSERT_FALSE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayDisableRemotePlaybackAttr) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
ASSERT_FALSE(IsElementVisible(*cast_overlay_button));
SimulateRemotePlaybackAvailable();
ASSERT_TRUE(IsElementVisible(*cast_overlay_button));
MediaControls().MediaElement().SetBooleanAttribute(
html_names::kDisableremoteplaybackAttr, true);
test::RunPendingTasks();
ASSERT_FALSE(IsElementVisible(*cast_overlay_button));
MediaControls().MediaElement().SetBooleanAttribute(
html_names::kDisableremoteplaybackAttr, false);
test::RunPendingTasks();
ASSERT_TRUE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayMediaControlsDisabled) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
SimulateRemotePlaybackAvailable();
EXPECT_TRUE(IsElementVisible(*cast_overlay_button));
GetDocument().GetSettings()->SetMediaControlsEnabled(false);
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
GetDocument().GetSettings()->SetMediaControlsEnabled(true);
EXPECT_TRUE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayDisabledMediaControlsDisabled) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
ScopedMediaCastOverlayButtonForTest media_cast_overlay_button(false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
SimulateRemotePlaybackAvailable();
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
GetDocument().GetSettings()->SetMediaControlsEnabled(false);
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
GetDocument().GetSettings()->SetMediaControlsEnabled(true);
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayDisabledAutoplayMuted) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
// Set the video to autoplay muted.
ScopedMediaEngagementBypassAutoplayPoliciesForTest scoped_feature(true);
MediaControls().MediaElement().GetDocument().GetSettings()->SetAutoplayPolicy(
AutoplayPolicy::Type::kDocumentUserActivationRequired);
MediaControls().MediaElement().setMuted(true);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
SimulateRemotePlaybackAvailable();
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastButtonVisibilityDependsOnControlslistAttr) {
EnsureSizing();
MediaControlCastButtonElement* cast_button = CastButtonElement();
ASSERT_NE(nullptr, cast_button);
SimulateRemotePlaybackAvailable();
ASSERT_TRUE(IsOverflowElementVisible(*cast_button));
MediaControls().MediaElement().setAttribute(
blink::html_names::kControlslistAttr, AtomicString("noremoteplayback"));
test::RunPendingTasks();
// Cast button should not be displayed because of
// controlslist="noremoteplayback".
ASSERT_FALSE(IsOverflowElementVisible(*cast_button));
// If the user explicitly shows all controls, that should override the
// controlsList attribute and cast button should be displayed.
MediaControls().MediaElement().SetUserWantsControlsVisible(true);
ASSERT_TRUE(IsOverflowElementVisible(*cast_button));
}
TEST_F(MediaControlsImplTest, KeepControlsVisibleIfOverflowListVisible) {
Element* overflow_list = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overflow-menu-list");
ASSERT_NE(nullptr, overflow_list);
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
MediaControls().MaybeShow();
MediaControls().ToggleOverflowMenu();
EXPECT_TRUE(IsElementVisible(*overflow_list));
SimulateHideMediaControlsTimerFired();
EXPECT_TRUE(IsElementVisible(*overflow_list));
EXPECT_TRUE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTest, DownloadButtonDisplayed) {
EnsureSizing();
MediaControlDownloadButtonElement* download_button = DownloadButtonElement();
ASSERT_NE(nullptr, download_button);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
test::RunPendingTasks();
SimulateLoadedMetadata();
// Download button should normally be displayed.
EXPECT_TRUE(IsOverflowElementVisible(*download_button));
}
TEST_F(MediaControlsImplTest, DownloadButtonNotDisplayedEmptyUrl) {
EnsureSizing();
MediaControlDownloadButtonElement* download_button = DownloadButtonElement();
ASSERT_NE(nullptr, download_button);
// Download button should not be displayed when URL is empty.
MediaControls().MediaElement().SetSrc(g_empty_atom);
test::RunPendingTasks();
SimulateLoadedMetadata();
EXPECT_FALSE(IsOverflowElementVisible(*download_button));
}
TEST_F(MediaControlsImplTest, DownloadButtonNotDisplayedInfiniteDuration) {
EnsureSizing();
MediaControlDownloadButtonElement* download_button = DownloadButtonElement();
ASSERT_NE(nullptr, download_button);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
test::RunPendingTasks();
// Download button should not be displayed when duration is infinite.
MediaControls().MediaElement().DurationChanged(
std::numeric_limits<double>::infinity(), false /* requestSeek */);
SimulateLoadedMetadata();
EXPECT_FALSE(IsOverflowElementVisible(*download_button));
// Download button should be shown if the duration changes back to finite.
MediaControls().MediaElement().DurationChanged(20.0f,
false /* requestSeek */);
SimulateLoadedMetadata();
EXPECT_TRUE(IsOverflowElementVisible(*download_button));
}
TEST_F(MediaControlsImplTest, DownloadButtonNotDisplayedHLS) {
EnsureSizing();
MediaControlDownloadButtonElement* download_button = DownloadButtonElement();
ASSERT_NE(nullptr, download_button);
// Download button should not be displayed for HLS streams.
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.m3u8"));
test::RunPendingTasks();
SimulateLoadedMetadata();
EXPECT_FALSE(IsOverflowElementVisible(*download_button));
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.m3u8?title=foo"));
test::RunPendingTasks();
SimulateLoadedMetadata();
EXPECT_FALSE(IsOverflowElementVisible(*download_button));
// However, it *should* be displayed for otherwise valid sources containing
// the text 'm3u8'.
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.m3u8.mp4"));
test::RunPendingTasks();
SimulateLoadedMetadata();
EXPECT_TRUE(IsOverflowElementVisible(*download_button));
}
TEST_F(MediaControlsImplTest,
DownloadButtonVisibilityDependsOnControlslistAttr) {
EnsureSizing();
MediaControlDownloadButtonElement* download_button = DownloadButtonElement();
ASSERT_NE(nullptr, download_button);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().setAttribute(
blink::html_names::kControlslistAttr, AtomicString("nodownload"));
test::RunPendingTasks();
SimulateLoadedMetadata();
// Download button should not be displayed because of
// controlslist="nodownload".
EXPECT_FALSE(IsOverflowElementVisible(*download_button));
// If the user explicitly shows all controls, that should override the
// controlsList attribute and download button should be displayed.
MediaControls().MediaElement().SetUserWantsControlsVisible(true);
EXPECT_TRUE(IsOverflowElementVisible(*download_button));
}
TEST_F(MediaControlsImplTest,
FullscreenButtonDisabledDependsOnControlslistAttr) {
EnsureSizing();
MediaControlFullscreenButtonElement* fullscreen_button =
FullscreenButtonElement();
ASSERT_NE(nullptr, fullscreen_button);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().setAttribute(
blink::html_names::kControlslistAttr, AtomicString("nofullscreen"));
test::RunPendingTasks();
SimulateLoadedMetadata();
// Fullscreen button should be disabled because of
// controlslist="nofullscreen".
EXPECT_TRUE(fullscreen_button->IsDisabled());
// If the user explicitly shows all controls, that should override the
// controlsList attribute and fullscreen button should be enabled.
MediaControls().MediaElement().SetUserWantsControlsVisible(true);
EXPECT_FALSE(fullscreen_button->IsDisabled());
}
TEST_F(MediaControlsImplTest,
PlaybackSpeedButtonVisibilityDependsOnControlslistAttr) {
EnsureSizing();
MediaControlPlaybackSpeedButtonElement* playback_speed_button =
PlaybackSpeedButtonElement();
ASSERT_NE(nullptr, playback_speed_button);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().setAttribute(
blink::html_names::kControlslistAttr, AtomicString("noplaybackrate"));
test::RunPendingTasks();
SimulateLoadedMetadata();
// Fullscreen button should not be displayed because of
// controlslist="noplaybackrate".
EXPECT_FALSE(IsOverflowElementVisible(*playback_speed_button));
// If the user explicitly shows all controls, that should override the
// controlsList attribute and playback speed button should be displayed.
MediaControls().MediaElement().SetUserWantsControlsVisible(true);
EXPECT_TRUE(IsOverflowElementVisible(*playback_speed_button));
}
TEST_F(MediaControlsImplTest, TimelineSeekToRoundedEnd) {
EnsureSizing();
// Tests the case where the real length of the video, |exact_duration|, gets
// rounded up slightly to |rounded_up_duration| when setting the timeline's
// |max| attribute (crbug.com/695065).
double exact_duration = 596.586667;
double rounded_up_duration = 596.586667;
LoadMediaWithDuration(exact_duration);
// Simulate a click slightly past the end of the track of the timeline's
// underlying <input type="range">. This would set the |value| to the |max|
// attribute, which can be slightly rounded relative to the duration.
MediaControlTimelineElement* timeline = TimelineElement();
timeline->setValueAsNumber(rounded_up_duration, ASSERT_NO_EXCEPTION);
ASSERT_EQ(rounded_up_duration, timeline->valueAsNumber());
EXPECT_EQ(0.0, MediaControls().MediaElement().currentTime());
timeline->DispatchInputEvent();
EXPECT_EQ(exact_duration, MediaControls().MediaElement().currentTime());
}
TEST_F(MediaControlsImplTest, TimelineImmediatelyUpdatesCurrentTime) {
EnsureSizing();
MediaControlCurrentTimeDisplayElement* current_time_display =
GetCurrentTimeDisplayElement();
double duration = 600;
LoadMediaWithDuration(duration);
// Simulate seeking the underlying range to 50%. Current time display should
// update synchronously (rather than waiting for media to finish seeking).
TimelineElement()->setValueAsNumber(duration / 2, ASSERT_NO_EXCEPTION);
TimelineElement()->DispatchInputEvent();
EXPECT_EQ(duration / 2, current_time_display->CurrentValue());
}
TEST_F(MediaControlsImplTest, TimeIndicatorsUpdatedOnSeeking) {
EnsureSizing();
MediaControlCurrentTimeDisplayElement* current_time_display =
GetCurrentTimeDisplayElement();
MediaControlTimelineElement* timeline = TimelineElement();
double duration = 1000;
LoadMediaWithDuration(duration);
EXPECT_EQ(0, current_time_display->CurrentValue());
EXPECT_EQ(0, timeline->valueAsNumber());
MediaControls().MediaElement().setCurrentTime(duration / 4);
// Time indicators are not yet updated.
EXPECT_EQ(0, current_time_display->CurrentValue());
EXPECT_EQ(0, timeline->valueAsNumber());
SimulateOnSeeking();
// The time indicators should be updated immediately when the 'seeking' event
// is fired.
EXPECT_EQ(duration / 4, current_time_display->CurrentValue());
EXPECT_EQ(duration / 4, timeline->valueAsNumber());
}
TEST_F(MediaControlsImplTest, TimeIsCorrectlyFormatted) {
struct {
double time;
String expected_result;
} tests[] = {
{-3661, "-1:01:01"}, {-1, "-0:01"}, {0, "0:00"},
{1, "0:01"}, {15, "0:15"}, {125, "2:05"},
{615, "10:15"}, {3666, "1:01:06"}, {75123, "20:52:03"},
{360600, "100:10:00"},
};
double duration = 360600; // Long enough to check each of the tests.
LoadMediaWithDuration(duration);
EnsureSizing();
test::RunPendingTasks();
MediaControlCurrentTimeDisplayElement* current_display =
GetCurrentTimeDisplayElement();
MediaControlRemainingTimeDisplayElement* duration_display =
GetRemainingTimeDisplayElement();
// The value and format of the duration display should be correct.
EXPECT_EQ(360600, duration_display->CurrentValue());
EXPECT_EQ("/ 100:10:00", GetDisplayedTime(duration_display));
for (const auto& testcase : tests) {
current_display->SetCurrentValue(testcase.time);
// Current value should be updated.
EXPECT_EQ(testcase.time, current_display->CurrentValue());
// Display text should be updated and correctly formatted.
EXPECT_EQ(testcase.expected_result, GetDisplayedTime(current_display));
}
}
namespace {
class MediaControlsImplTestWithMockScheduler : public MediaControlsImplTest {
public:
MediaControlsImplTestWithMockScheduler()
: MediaControlsImplTest(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
EnablePlatform();
}
protected:
void SetUp() override {
// DocumentParserTiming has DCHECKS to make sure time > 0.0.
AdvanceClock(base::Seconds(1));
MediaControlsImplTest::SetUp();
}
void TearDown() override { PageTestBase::TearDown(); }
void ToggleOverflowMenu() {
MediaControls().ToggleOverflowMenu();
platform()->RunUntilIdle();
}
bool IsCursorHidden() {
const CSSPropertyValueSet* style = MediaControls().InlineStyle();
if (!style)
return false;
return style->GetPropertyValue(CSSPropertyID::kCursor) == "none";
}
};
} // namespace
TEST_F(MediaControlsImplTestWithMockScheduler, SeekingShowsControls) {
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
// Hide the controls to start.
MediaControls().Hide();
EXPECT_FALSE(IsElementVisible(*panel));
// Seeking should cause the controls to become visible.
SimulateOnSeeking();
EXPECT_TRUE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
SeekingDoesNotShowControlsWhenNoControlsAttr) {
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
// Hide the controls to start.
MediaControls().Hide();
EXPECT_FALSE(IsElementVisible(*panel));
// Seeking should not cause the controls to become visible because the
// controls attribute is not set.
SimulateOnSeeking();
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ControlsRemainVisibleDuringKeyboardInteraction) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
// Controls start out visible.
EXPECT_TRUE(IsElementVisible(*panel));
// Tabbing between controls prevents controls from hiding.
FastForwardBy(base::Seconds(2));
MuteButtonElement()->DispatchEvent(
*Event::CreateBubble(event_type_names::kFocusin));
FastForwardBy(base::Seconds(2));
EXPECT_TRUE(IsElementVisible(*panel));
// Seeking on the timeline or volume bar prevents controls from hiding.
TimelineElement()->DispatchEvent(
*Event::CreateBubble(event_type_names::kInput));
FastForwardBy(base::Seconds(2));
EXPECT_TRUE(IsElementVisible(*panel));
// Pressing a key prevents controls from hiding.
MuteButtonElement()->DispatchEvent(
*Event::CreateBubble(event_type_names::kKeypress));
FastForwardBy(base::Seconds(2));
EXPECT_TRUE(IsElementVisible(*panel));
// Once user interaction stops, controls can hide.
FastForwardBy(base::Seconds(2));
SimulateTransitionEnd(*panel);
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ControlsHideAfterFocusedAndMouseMovement) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
// Controls start out visible
EXPECT_TRUE(IsElementVisible(*panel));
FastForwardBy(base::Seconds(1));
// Mouse move while focused
MediaControls().DispatchEvent(*Event::Create(event_type_names::kFocusin));
FocusElement(&MediaControls().MediaElement());
MediaControls().DispatchEvent(
*CreatePointerEvent(event_type_names::kPointermove));
// Controls should remain visible
FastForwardBy(base::Seconds(2));
EXPECT_TRUE(IsElementVisible(*panel));
// Controls should hide after being inactive for 4 seconds.
FastForwardBy(base::Seconds(2));
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ControlsHideAfterFocusedAndMouseMoveout) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
// Controls start out visible
EXPECT_TRUE(IsElementVisible(*panel));
FastForwardBy(base::Seconds(1));
// Mouse move out while focused, controls should hide
MediaControls().DispatchEvent(*Event::Create(event_type_names::kFocusin));
FocusElement(&MediaControls().MediaElement());
MediaControls().DispatchEvent(*Event::Create(event_type_names::kPointerout));
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ControlsDoNotHideOnKeyboardFocus) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
auto* player = &MediaControls().MediaElement();
player->SetSrc(AtomicString("http://example.com"));
player->Play();
// Controls start out visible
EXPECT_TRUE(IsElementVisible(*panel));
EXPECT_TRUE(IsElementVisible(*player));
FastForwardBy(base::Seconds(1));
// Focus via keyboard.
EXPECT_TRUE(player->IsFocusable());
FocusElement(player, mojom::blink::FocusType::kNone);
EXPECT_TRUE(player->IsFocused());
// Controls should remain visible.
FastForwardBy(base::Seconds(5));
EXPECT_TRUE(IsElementVisible(*panel));
// Unfocus the element. Controls should hide, even if the unfocus was via
// keyboard. They will re-show when the user refocuses the video player.
// This behavior was tested above.
EXPECT_TRUE(player->IsFocused());
UnfocusElement(player, mojom::blink::FocusType::kNone);
EXPECT_FALSE(player->IsFocused());
FastForwardBy(base::Seconds(5));
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ControlsDoNotHideIfPlaybackSpeedWanted) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
auto* player = &MediaControls().MediaElement();
player->SetSrc(AtomicString("http://example.com"));
player->Play();
// Controls start out visible
EXPECT_TRUE(IsElementVisible(*panel));
FastForwardBy(base::Seconds(1));
// Pretend that the user has the playback speed button pressed, and then
// unfocuses the panel.
MediaControls().TogglePlaybackSpeedList();
UnfocusElement(player);
// Controls should remain visible.
FastForwardBy(base::Seconds(5));
EXPECT_TRUE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTestWithMockScheduler, CursorHidesWhenControlsHide) {
EnsureSizing();
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
// Cursor is not initially hidden.
EXPECT_FALSE(IsCursorHidden());
MediaControls().MediaElement().Play();
// Tabbing into the controls shows the controls and therefore the cursor.
MediaControls().DispatchEvent(*Event::Create(event_type_names::kFocusin));
EXPECT_FALSE(IsCursorHidden());
// Once the controls hide, the cursor is hidden.
FastForwardBy(base::Seconds(4));
EXPECT_TRUE(IsCursorHidden());
// If the mouse moves, the controls are shown and the cursor is no longer
// hidden.
MediaControls().DispatchEvent(
*CreatePointerEvent(event_type_names::kPointermove));
EXPECT_FALSE(IsCursorHidden());
// Once the controls hide again, the cursor is hidden again.
FastForwardBy(base::Seconds(4));
EXPECT_TRUE(IsCursorHidden());
}
TEST_F(MediaControlsImplTestWithMockScheduler, AccessibleFocusShowsControls) {
EnsureSizing();
Element* panel = MediaControls().PanelElement();
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
FastForwardBy(base::Seconds(4));
EXPECT_TRUE(IsElementVisible(*panel));
MediaControls().OnAccessibleFocus();
FastForwardBy(base::Seconds(4));
EXPECT_TRUE(IsElementVisible(*panel));
FastForwardBy(base::Seconds(4));
SimulateHideMediaControlsTimerFired();
EXPECT_TRUE(IsElementVisible(*panel));
MediaControls().OnAccessibleBlur();
FastForwardBy(base::Seconds(4));
SimulateHideMediaControlsTimerFired();
EXPECT_FALSE(IsElementVisible(*panel));
}
TEST_F(MediaControlsImplTest,
RemovingFromDocumentRemovesListenersAndCallbacks) {
auto page_holder = std::make_unique<DummyPageHolder>();
auto* element =
MakeGarbageCollected<HTMLVideoElement>(page_holder->GetDocument());
page_holder->GetDocument().body()->AppendChild(element);
RemotePlayback& remote_playback = RemotePlayback::From(*element);
EXPECT_TRUE(remote_playback.HasEventListeners());
EXPECT_TRUE(HasAvailabilityCallbacks(remote_playback));
WeakPersistent<HTMLMediaElement> weak_persistent_video = element;
{
Persistent<HTMLMediaElement> persistent_video = element;
page_holder->GetDocument().body()->setInnerHTML("");
// When removed from the document, the event listeners should have been
// dropped.
EXPECT_FALSE(remote_playback.HasEventListeners());
EXPECT_FALSE(HasAvailabilityCallbacks(remote_playback));
}
page_holder->GetDocument().View()->UpdateAllLifecyclePhasesForTest();
test::RunPendingTasks();
ThreadState::Current()->CollectAllGarbageForTesting();
// It has been GC'd.
EXPECT_EQ(nullptr, weak_persistent_video);
}
TEST_F(MediaControlsImplTest,
RemovingFromDocumentWhenResettingSrcAllowsReclamation) {
// Regression test: https://crbug.com/918064
//
// Test ensures that unified heap garbage collections are able to collect
// detached HTMLVideoElements. The tricky part is that ResizeObserver's are
// treated as roots as long as they have observations which prevent the video
// element from being collected.
auto page_holder = std::make_unique<DummyPageHolder>();
page_holder->GetDocument().write("<video controls>");
page_holder->GetDocument().Parser()->Finish();
auto& video = To<HTMLVideoElement>(
*page_holder->GetDocument().QuerySelector(AtomicString("video")));
WeakPersistent<HTMLMediaElement> weak_persistent_video = &video;
video.remove();
page_holder->GetDocument().View()->UpdateAllLifecyclePhasesForTest();
test::RunPendingTasks();
ThreadState::Current()->CollectAllGarbageForTesting();
EXPECT_EQ(nullptr, weak_persistent_video);
}
TEST_F(MediaControlsImplTest,
ReInsertingInDocumentRestoresListenersAndCallbacks) {
auto page_holder = std::make_unique<DummyPageHolder>();
auto* element =
MakeGarbageCollected<HTMLVideoElement>(page_holder->GetDocument());
page_holder->GetDocument().body()->AppendChild(element);
RemotePlayback& remote_playback = RemotePlayback::From(*element);
// This should be a no-op. We keep a reference on the media element to avoid
// an unexpected GC.
{
Persistent<HTMLMediaElement> video_holder = element;
page_holder->GetDocument().body()->RemoveChild(element);
page_holder->GetDocument().body()->AppendChild(video_holder.Get());
EXPECT_TRUE(remote_playback.HasEventListeners());
EXPECT_TRUE(HasAvailabilityCallbacks(remote_playback));
}
}
TEST_F(MediaControlsImplTest, InitialInfinityDurationHidesDurationField) {
EnsureSizing();
LoadMediaWithDuration(std::numeric_limits<double>::infinity());
MediaControlRemainingTimeDisplayElement* duration_display =
GetRemainingTimeDisplayElement();
EXPECT_FALSE(duration_display->IsWanted());
EXPECT_EQ(std::numeric_limits<double>::infinity(),
duration_display->CurrentValue());
}
TEST_F(MediaControlsImplTest, InfinityDurationChangeHidesDurationField) {
EnsureSizing();
LoadMediaWithDuration(42);
MediaControlRemainingTimeDisplayElement* duration_display =
GetRemainingTimeDisplayElement();
EXPECT_TRUE(duration_display->IsWanted());
EXPECT_EQ(42, duration_display->CurrentValue());
MediaControls().MediaElement().DurationChanged(
std::numeric_limits<double>::infinity(), false /* request_seek */);
test::RunPendingTasks();
EXPECT_FALSE(duration_display->IsWanted());
EXPECT_EQ(std::numeric_limits<double>::infinity(),
duration_display->CurrentValue());
}
TEST_F(MediaControlsImplTestWithMockScheduler,
ShowVolumeSliderAfterHoverTimerFired) {
const double kTimeToShowVolumeSlider = 0.2;
EnsureSizing();
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
FastForwardBy(base::Seconds(1));
SetHasAudio(true);
SimulateLoadedMetadata();
ScopedWebTestMode web_test_mode(false);
Element* volume_slider = VolumeSliderElement();
Element* mute_btn = MuteButtonElement();
ASSERT_NE(nullptr, volume_slider);
ASSERT_NE(nullptr, mute_btn);
EXPECT_TRUE(IsElementVisible(*mute_btn));
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
DOMRect* mute_btn_rect = mute_btn->GetBoundingClientRect();
gfx::PointF mute_btn_center(
mute_btn_rect->left() + mute_btn_rect->width() / 2,
mute_btn_rect->top() + mute_btn_rect->height() / 2);
gfx::PointF edge(0, 0);
// Hover on mute button and stay
MouseMoveTo(mute_btn_center);
FastForwardBy(base::Seconds(kTimeToShowVolumeSlider - 0.001));
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
FastForwardBy(base::Seconds(0.002));
EXPECT_FALSE(volume_slider->classList().contains(AtomicString("closed")));
MouseMoveTo(edge);
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
// Hover on mute button and move away before timer fired
MouseMoveTo(mute_btn_center);
FastForwardBy(base::Seconds(kTimeToShowVolumeSlider - 0.001));
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
MouseMoveTo(edge);
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
VolumeSliderBehaviorWhenFocused) {
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
FastForwardBy(base::Seconds(1));
SetHasAudio(true);
ScopedWebTestMode web_test_mode(false);
Element* volume_slider = VolumeSliderElement();
ASSERT_NE(nullptr, volume_slider);
// Volume slider starts out hidden
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
// Tab focus should open volume slider immediately.
volume_slider->SetFocused(true, mojom::blink::FocusType::kNone);
volume_slider->DispatchEvent(*Event::Create(event_type_names::kFocus));
EXPECT_FALSE(volume_slider->classList().contains(AtomicString("closed")));
// Unhover slider while focused should not close slider.
volume_slider->DispatchEvent(*Event::Create(event_type_names::kMouseout));
EXPECT_FALSE(volume_slider->classList().contains(AtomicString("closed")));
}
TEST_F(MediaControlsImplTestWithMockScheduler,
VolumeSliderDoesNotOpenWithoutAudio) {
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
FastForwardBy(base::Seconds(1));
SetHasAudio(false);
ScopedWebTestMode web_test_mode(false);
Element* volume_slider = VolumeSliderElement();
Element* mute_button = MuteButtonElement();
ASSERT_NE(nullptr, volume_slider);
// Volume slider starts out hidden.
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
// Tab focus on the mute button should not open the volume slider since there
// is no audio to control.
mute_button->SetFocused(true, mojom::blink::FocusType::kNone);
mute_button->DispatchEvent(*Event::Create(event_type_names::kFocus));
EXPECT_TRUE(volume_slider->classList().contains(AtomicString("closed")));
}
TEST_F(MediaControlsImplTest, CastOverlayDefaultHidesOnTimer) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
SimulateRemotePlaybackAvailable();
EXPECT_TRUE(IsElementVisible(*cast_overlay_button));
// Starts playback because overlay never hides if paused.
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
SimulateHideMediaControlsTimerFired();
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, CastOverlayShowsOnSomeEvents) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
Element* cast_overlay_button = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overlay-cast-button");
ASSERT_NE(nullptr, cast_overlay_button);
Element* overlay_enclosure = GetElementByShadowPseudoId(
MediaControls(), "-webkit-media-controls-overlay-enclosure");
ASSERT_NE(nullptr, overlay_enclosure);
SimulateRemotePlaybackAvailable();
EXPECT_TRUE(IsElementVisible(*cast_overlay_button));
// Starts playback because overlay never hides if paused.
MediaControls().MediaElement().SetSrc(AtomicString("http://example.com"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
SimulateRemotePlaybackAvailable();
SimulateHideMediaControlsTimerFired();
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
// The overlay button appears on tap and click.
for (const AtomicString& event_name :
{event_type_names::kGesturetap, event_type_names::kClick}) {
overlay_enclosure->DispatchEvent(event_name == "gesturetap"
? *Event::Create(event_name)
: *CreatePointerEvent(event_name));
EXPECT_TRUE(IsElementVisible(*cast_overlay_button));
SimulateHideMediaControlsTimerFired();
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
}
// The overlay button does not appear on pointer move.
overlay_enclosure->DispatchEvent(
*CreatePointerEvent(event_type_names::kPointerover));
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
// The overlay button does not appear on click if the overlay button shouldn't
// be shown.
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
true);
overlay_enclosure->DispatchEvent(
*CreatePointerEvent(event_type_names::kClick));
EXPECT_FALSE(IsElementVisible(*cast_overlay_button));
}
TEST_F(MediaControlsImplTest, isConnected) {
EXPECT_TRUE(MediaControls().isConnected());
MediaControls().MediaElement().remove();
EXPECT_FALSE(MediaControls().isConnected());
}
TEST_F(MediaControlsImplTest, ControlsShouldUseSafeAreaInsets) {
UpdateAllLifecyclePhasesForTest();
{
const ComputedStyle* style = MediaControls().GetComputedStyle();
EXPECT_EQ(0.0, style->MarginTop().Pixels());
EXPECT_EQ(0.0, style->MarginLeft().Pixels());
EXPECT_EQ(0.0, style->MarginBottom().Pixels());
EXPECT_EQ(0.0, style->MarginRight().Pixels());
}
GetStyleEngine().EnsureEnvironmentVariables().SetVariable(
UADefinedVariable::kSafeAreaInsetTop, "1px");
GetStyleEngine().EnsureEnvironmentVariables().SetVariable(
UADefinedVariable::kSafeAreaInsetLeft, "2px");
GetStyleEngine().EnsureEnvironmentVariables().SetVariable(
UADefinedVariable::kSafeAreaInsetBottom, "3px");
GetStyleEngine().EnsureEnvironmentVariables().SetVariable(
UADefinedVariable::kSafeAreaInsetRight, "4px");
EXPECT_TRUE(GetDocument().NeedsLayoutTreeUpdate());
UpdateAllLifecyclePhasesForTest();
{
const ComputedStyle* style = MediaControls().GetComputedStyle();
EXPECT_EQ(1.0, style->MarginTop().Pixels());
EXPECT_EQ(2.0, style->MarginLeft().Pixels());
EXPECT_EQ(3.0, style->MarginBottom().Pixels());
EXPECT_EQ(4.0, style->MarginRight().Pixels());
}
}
TEST_F(MediaControlsImplTest, MediaControlsDisabledWithNoSource) {
EXPECT_EQ(MediaControls().State(), MediaControlsImpl::kNoSource);
EXPECT_TRUE(PlayButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_TRUE(
OverflowMenuButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_TRUE(TimelineElement()->FastHasAttribute(html_names::kDisabledAttr));
MediaControls().MediaElement().setAttribute(html_names::kPreloadAttr,
AtomicString("none"));
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
test::RunPendingTasks();
SimulateLoadedMetadata();
EXPECT_EQ(MediaControls().State(), MediaControlsImpl::kNotLoaded);
EXPECT_FALSE(
PlayButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_FALSE(
OverflowMenuButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_TRUE(TimelineElement()->FastHasAttribute(html_names::kDisabledAttr));
MediaControls().MediaElement().removeAttribute(html_names::kPreloadAttr);
SimulateLoadedMetadata();
EXPECT_EQ(MediaControls().State(), MediaControlsImpl::kLoadingMetadataPaused);
EXPECT_FALSE(
PlayButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_FALSE(
OverflowMenuButtonElement()->FastHasAttribute(html_names::kDisabledAttr));
EXPECT_FALSE(TimelineElement()->FastHasAttribute(html_names::kDisabledAttr));
}
TEST_F(MediaControlsImplTest, DoubleTouchChangesTime) {
double duration = 60; // 1 minute.
LoadMediaWithDuration(duration);
EnsureSizing();
MediaControls().MediaElement().setCurrentTime(30);
test::RunPendingTasks();
// We've set the video to the halfway mark.
EXPECT_EQ(30, MediaControls().MediaElement().currentTime());
DOMRect* videoRect = MediaControls().MediaElement().GetBoundingClientRect();
ASSERT_LT(0, videoRect->width());
gfx::PointF leftOfCenter(videoRect->left() + (videoRect->width() / 2) - 5,
videoRect->top() + 5);
gfx::PointF rightOfCenter(videoRect->left() + (videoRect->width() / 2) + 5,
videoRect->top() + 5);
// Double-tapping left of center should shift the time backwards by 10
// seconds.
GestureDoubleTapAt(leftOfCenter);
test::RunPendingTasks();
EXPECT_EQ(20, MediaControls().MediaElement().currentTime());
// Double-tapping right of center should shift the time forwards by 10
// seconds.
GestureDoubleTapAt(rightOfCenter);
test::RunPendingTasks();
EXPECT_EQ(30, MediaControls().MediaElement().currentTime());
}
TEST_F(MediaControlsImplTest, DoubleTouchChangesTimeWhenZoomed) {
double duration = 60; // 1 minute.
LoadMediaWithDuration(duration);
EnsureSizing();
MediaControls().MediaElement().setCurrentTime(30);
test::RunPendingTasks();
// We've set the video to the halfway mark.
EXPECT_EQ(30, MediaControls().MediaElement().currentTime());
DOMRect* videoRect = MediaControls().MediaElement().GetBoundingClientRect();
ASSERT_LT(0, videoRect->width());
gfx::PointF leftOfCenter(videoRect->left() + (videoRect->width() / 2) - 5,
videoRect->top() + 10);
gfx::PointF rightOfCenter(videoRect->left() + (videoRect->width() / 2) + 5,
videoRect->top() + 10);
// Add a zoom factor and ensure that it's properly handled.
MediaControls().GetDocument().GetFrame()->SetLayoutZoomFactor(2);
// Double-tapping left of center should shift the time backwards by 10
// seconds.
GestureDoubleTapAt(leftOfCenter);
test::RunPendingTasks();
EXPECT_EQ(20, MediaControls().MediaElement().currentTime());
// Double-tapping right of center should shift the time forwards by 10
// seconds.
GestureDoubleTapAt(rightOfCenter);
test::RunPendingTasks();
EXPECT_EQ(30, MediaControls().MediaElement().currentTime());
}
TEST_F(MediaControlsImplTest, HideControlsDefersStyleCalculationOnPlaying) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
EXPECT_FALSE(IsElementVisible(*panel));
UpdateAllLifecyclePhasesForTest();
Document& document = this->GetDocument();
EXPECT_FALSE(document.NeedsLayoutTreeUpdate());
int old_element_count = document.GetStyleEngine().StyleForElementCount();
SimulateMediaControlPlaying();
SimulateOnPlaying();
EXPECT_EQ(MediaControls().State(),
MediaControlsImpl::ControlsState::kPlaying);
// With the controls hidden, playback state change should not trigger style
// calculation.
EXPECT_FALSE(document.NeedsLayoutTreeUpdate());
UpdateAllLifecyclePhasesForTest();
int new_element_count = document.GetStyleEngine().StyleForElementCount();
EXPECT_EQ(old_element_count, new_element_count);
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
true);
EXPECT_TRUE(IsElementVisible(*panel));
// Showing the controls should trigger the deferred style calculation.
EXPECT_TRUE(document.NeedsLayoutTreeUpdate());
UpdateAllLifecyclePhasesForTest();
new_element_count = document.GetStyleEngine().StyleForElementCount();
EXPECT_LT(old_element_count, new_element_count);
}
TEST_F(MediaControlsImplTest, HideControlsDefersStyleCalculationOnWaiting) {
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
false);
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
Element* panel = GetElementByShadowPseudoId(MediaControls(),
"-webkit-media-controls-panel");
ASSERT_NE(nullptr, panel);
EXPECT_FALSE(IsElementVisible(*panel));
UpdateAllLifecyclePhasesForTest();
Document& document = this->GetDocument();
EXPECT_FALSE(document.NeedsLayoutTreeUpdate());
int old_element_count = document.GetStyleEngine().StyleForElementCount();
SimulateMediaControlBuffering();
SimulateOnWaiting();
EXPECT_EQ(MediaControls().State(),
MediaControlsImpl::ControlsState::kBuffering);
// With the controls hidden, playback state change should not trigger style
// calculation.
EXPECT_FALSE(document.NeedsLayoutTreeUpdate());
UpdateAllLifecyclePhasesForTest();
int new_element_count = document.GetStyleEngine().StyleForElementCount();
EXPECT_EQ(old_element_count, new_element_count);
MediaControls().MediaElement().SetBooleanAttribute(html_names::kControlsAttr,
true);
EXPECT_TRUE(IsElementVisible(*panel));
// Showing the controls should trigger the deferred style calculation.
EXPECT_TRUE(document.NeedsLayoutTreeUpdate());
UpdateAllLifecyclePhasesForTest();
new_element_count = document.GetStyleEngine().StyleForElementCount();
EXPECT_LT(old_element_count, new_element_count);
}
TEST_F(MediaControlsImplTest, CheckStateOnPlayingForFutureData) {
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
MediaControls().MediaElement().Play();
test::RunPendingTasks();
UpdateAllLifecyclePhasesForTest();
SimulateMediaControlPlayingForFutureData();
EXPECT_EQ(MediaControls().State(),
MediaControlsImpl::ControlsState::kPlaying);
}
TEST_F(MediaControlsImplTest, OverflowMenuInPaintContainment) {
// crbug.com/1244130
auto page_holder = std::make_unique<DummyPageHolder>();
page_holder->GetDocument().write("<audio controls style='contain:paint'>");
page_holder->GetDocument().Parser()->Finish();
test::RunPendingTasks();
UpdateAllLifecyclePhasesForTest();
SetMediaControlsFromElement(To<HTMLMediaElement>(
*page_holder->GetDocument().QuerySelector(AtomicString("audio"))));
MediaControls().ToggleOverflowMenu();
UpdateAllLifecyclePhasesForTest();
Element* overflow_list = GetElementByShadowPseudoId(
MediaControls(), "-internal-media-controls-overflow-menu-list");
ASSERT_TRUE(overflow_list);
EXPECT_TRUE(overflow_list->IsInTopLayer());
MediaControls().ToggleOverflowMenu();
UpdateAllLifecyclePhasesForTest();
EXPECT_FALSE(overflow_list->IsInTopLayer());
}
TEST_F(MediaControlsImplTest, OverlayPlayButtonHidesWhenTooShort) {
EnsureSizing();
auto* overlay_play_button = OverlayPlayButtonElement();
ASSERT_NE(nullptr, overlay_play_button);
// The overflow button must fit with enough vertical space for itself, the
// timeline, and the row of buttons.
const int min_height = TimelineElement()->GetSizeOrDefault().height() +
PlayButtonElement()->GetSizeOrDefault().height() +
overlay_play_button->GetSizeOrDefault().height();
MediaControls().MediaElement().SetSrc(
AtomicString("https://example.com/foo.mp4"));
test::RunPendingTasks();
SimulateLoadedMetadata();
// Set the size to be too small.
SetElementHeight(min_height - 1);
EXPECT_FALSE(overlay_play_button->DoesFit());
// Set the size to be large enough.
SetElementHeight(min_height);
EXPECT_TRUE(overlay_play_button->DoesFit());
}
} // namespace blink
|