Commit c14930db authored by François Beaufort's avatar François Beaufort Committed by Commit Bot

Context menu shows audio if video has no video track but an audio track.

This makes sure Picture-in-Picture context menu does not show at all
(instead of disabled) for video elements with audio track only (e.g.
MediaDocument).
It also shows audio related items such as "Open audio in new tab"
insteaad of "Open video in new tab" for these cases.

Screenshots: https://imgur.com/a/1d1UNrg

Bug: 806249
Change-Id: I6341f476557389521ab2b6f21e53c2bd6a5df34d
Reviewed-on: https://chromium-review.googlesource.com/1096759
Commit-Queue: François Beaufort <beaufort.francois@gmail.com>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567177}
parent 4c9c4653
......@@ -253,7 +253,12 @@ bool ContextMenuController::ShowContextMenu(LocalFrame* frame,
// is a media element.
HTMLMediaElement* media_element = ToHTMLMediaElement(r.InnerNode());
if (IsHTMLVideoElement(*media_element)) {
data.media_type = WebContextMenuData::kMediaTypeVideo;
// A video element should be presented as an audio element when it has an
// audio track but no video track.
if (media_element->HasAudio() && !media_element->HasVideo())
data.media_type = WebContextMenuData::kMediaTypeAudio;
else
data.media_type = WebContextMenuData::kMediaTypeVideo;
if (media_element->SupportsPictureInPicture()) {
data.media_flags |= WebContextMenuData::kMediaCanPictureInPicture;
if (PictureInPictureController::From(media_element->GetDocument())
......
......@@ -24,6 +24,7 @@ namespace {
class MockWebMediaPlayerForContextMenu : public EmptyWebMediaPlayer {
public:
MOCK_CONST_METHOD0(HasAudio, bool());
MOCK_CONST_METHOD0(HasVideo, bool());
};
......@@ -145,6 +146,65 @@ TEST_F(ContextMenuControllerTest, VideoNotLoaded) {
}
}
TEST_F(ContextMenuControllerTest, VideoWithAudioOnly) {
ContextMenuAllowedScope context_menu_allowed_scope;
HitTestResult hit_test_result;
const char video_url[] = "https://example.com/foo.webm";
// Make sure Picture-in-Picture is enabled.
GetDocument()->GetSettings()->SetPictureInPictureEnabled(true);
// Setup video element.
Persistent<HTMLVideoElement> video = HTMLVideoElement::Create(*GetDocument());
video->SetSrc(video_url);
GetDocument()->body()->AppendChild(video);
test::RunPendingTasks();
SetReadyState(video.Get(), HTMLMediaElement::kHaveNothing);
test::RunPendingTasks();
EXPECT_CALL(*static_cast<MockWebMediaPlayerForContextMenu*>(
video->GetWebMediaPlayer()),
HasVideo())
.WillRepeatedly(Return(false));
EXPECT_CALL(*static_cast<MockWebMediaPlayerForContextMenu*>(
video->GetWebMediaPlayer()),
HasAudio())
.WillRepeatedly(Return(true));
DOMRect* rect = video->getBoundingClientRect();
LayoutPoint location((rect->left() + rect->right()) / 2,
(rect->top() + rect->bottom()) / 2);
EXPECT_TRUE(ShowContextMenu(location, kMenuSourceMouse));
// Context menu info are sent to the WebLocalFrameClient.
WebContextMenuData context_menu_data =
GetWebFrameClient().GetContextMenuData();
EXPECT_EQ(WebContextMenuData::kMediaTypeAudio, context_menu_data.media_type);
EXPECT_EQ(video_url, context_menu_data.src_url.GetString());
const std::vector<std::pair<WebContextMenuData::MediaFlags, bool>>
expected_media_flags = {
{WebContextMenuData::kMediaInError, false},
{WebContextMenuData::kMediaPaused, true},
{WebContextMenuData::kMediaMuted, false},
{WebContextMenuData::kMediaLoop, false},
{WebContextMenuData::kMediaCanSave, true},
{WebContextMenuData::kMediaHasAudio, true},
{WebContextMenuData::kMediaCanToggleControls, false},
{WebContextMenuData::kMediaControls, false},
{WebContextMenuData::kMediaCanPrint, false},
{WebContextMenuData::kMediaCanRotate, false},
{WebContextMenuData::kMediaCanPictureInPicture, false},
{WebContextMenuData::kMediaPictureInPicture, false},
};
for (const auto& expected_media_flag : expected_media_flags) {
EXPECT_EQ(expected_media_flag.second,
!!(context_menu_data.media_flags & expected_media_flag.first))
<< "Flag 0x" << std::hex << expected_media_flag.first;
}
}
TEST_F(ContextMenuControllerTest, PictureInPictureEnabledVideoLoaded) {
// Make sure Picture-in-Picture is enabled.
GetDocument()->GetSettings()->SetPictureInPictureEnabled(true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment