Commit 60a0db35 authored by Wojciech Dzierżanowski's avatar Wojciech Dzierżanowski Committed by Commit Bot

Modernize JS execution in PictureInPictureContentBrowserTest

Replace the semi-deprecated ExecuteScript*() functions with the modern
counterparts ExecJs(), EvalJs().

While we're here, let's make sure the test doesn't try to
requestPictureInPicture() until metadata has been loaded.

Bug: 1138168
Change-Id: Id247c2922f26e922417496f9cc406317ac061ebe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2529121
Commit-Queue: Wojciech Dzierżanowski <wdzierzanowski@opera.com>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827738}
parent 719b80d5
......@@ -12,6 +12,7 @@
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
......@@ -153,7 +154,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
shell(), GetTestUrl("media/picture_in_picture", "two-videos.html")));
// Play first video.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "videos[0].play();"));
ASSERT_TRUE(ExecJs(shell(), "videos[0].play();"));
base::string16 expected_title = base::ASCIIToUTF16("videos[0] playing");
EXPECT_EQ(
......@@ -161,7 +162,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
TitleWatcher(shell()->web_contents(), expected_title).WaitAndGetTitle());
// Play second video.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "videos[1].play();"));
ASSERT_TRUE(ExecJs(shell(), "videos[1].play();"));
expected_title = base::ASCIIToUTF16("videos[1] playing");
EXPECT_EQ(
......@@ -171,8 +172,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
ASSERT_FALSE(web_contents_delegate()->is_in_picture_in_picture());
// Send first video in Picture-in-Picture.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"videos[0].requestPictureInPicture();"));
ASSERT_TRUE(ExecJs(shell(), "videos[0].requestPictureInPicture();"));
expected_title = base::ASCIIToUTF16("videos[0] entered picture-in-picture");
EXPECT_EQ(
......@@ -181,8 +181,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
EXPECT_TRUE(web_contents_delegate()->is_in_picture_in_picture());
// Send second video in Picture-in-Picture.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"videos[1].requestPictureInPicture();"));
ASSERT_TRUE(ExecJs(shell(), "videos[1].requestPictureInPicture();"));
expected_title = base::ASCIIToUTF16("videos[1] entered picture-in-picture");
EXPECT_EQ(
......@@ -209,7 +208,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
TitleWatcher(shell()->web_contents(), expected_title).WaitAndGetTitle());
// Play first video.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "videos[0].play();"));
ASSERT_TRUE(ExecJs(shell(), "videos[0].play();"));
expected_title = base::ASCIIToUTF16("videos[0] playing");
EXPECT_EQ(
......@@ -217,8 +216,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
TitleWatcher(shell()->web_contents(), expected_title).WaitAndGetTitle());
// Play second video (in iframe).
ASSERT_TRUE(
ExecuteScript(shell()->web_contents(), "iframeVideos[0].play();"));
ASSERT_TRUE(ExecJs(shell(), "iframeVideos[0].play();"));
expected_title = base::ASCIIToUTF16("iframeVideos[0] playing");
EXPECT_EQ(
......@@ -228,8 +226,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
ASSERT_FALSE(web_contents_delegate()->is_in_picture_in_picture());
// Send first video in Picture-in-Picture.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"videos[0].requestPictureInPicture();"));
ASSERT_TRUE(ExecJs(shell(), "videos[0].requestPictureInPicture();"));
expected_title = base::ASCIIToUTF16("videos[0] entered picture-in-picture");
EXPECT_EQ(
......@@ -238,8 +235,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
EXPECT_TRUE(web_contents_delegate()->is_in_picture_in_picture());
// Send second video in Picture-in-Picture.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"iframeVideos[0].requestPictureInPicture();"));
ASSERT_TRUE(ExecJs(shell(), "iframeVideos[0].requestPictureInPicture();"));
expected_title =
base::ASCIIToUTF16("iframeVideos[0] entered picture-in-picture");
......@@ -257,17 +253,11 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
ASSERT_TRUE(NavigateToURL(
shell(), GetTestUrl("media/picture_in_picture", "one-video.html")));
bool result = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterPictureInPictureAndGetResult();",
&result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterPictureInPicture();"));
ASSERT_TRUE(web_contents_delegate()->is_in_picture_in_picture());
// The Picture-in-Picture window should be closed upon entering fullscreen.
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterFullscreenAndGetResult();", &result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterFullscreen();"));
EXPECT_TRUE(shell()->web_contents()->IsFullscreen());
EXPECT_FALSE(web_contents_delegate()->is_in_picture_in_picture());
......@@ -278,17 +268,11 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
ASSERT_TRUE(NavigateToURL(
shell(), GetTestUrl("media/picture_in_picture", "one-video.html")));
bool result = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterFullscreenAndGetResult();", &result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterFullscreen();"));
ASSERT_TRUE(shell()->web_contents()->IsFullscreen());
// We should leave fullscreen upon entering Picture-in-Picture.
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterPictureInPictureAndGetResult();",
&result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterPictureInPicture();"));
EXPECT_FALSE(shell()->web_contents()->IsFullscreen());
EXPECT_TRUE(web_contents_delegate()->is_in_picture_in_picture());
......@@ -302,23 +286,15 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
shell(), GetTestUrl("media/picture_in_picture", "one-video.html")));
// Play and pause the player from script.
bool result = false;
ASSERT_TRUE(
ExecuteScriptAndExtractBool(shell()->web_contents(), "play();", &result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "play();"));
ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "video.pause();"));
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterPictureInPictureAndGetResult();",
&result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterPictureInPicture();"));
EXPECT_EQ(overlay_window()->playback_state(),
OverlayWindow::PlaybackState::kPaused);
// Simulate resuming playback by interacting with the PiP window.
ASSERT_TRUE(
ExecuteScript(shell()->web_contents(), "addPlayEventListener();"));
ASSERT_TRUE(ExecJs(shell(), "addPlayEventListener();"));
window_controller()->TogglePlayPause();
base::string16 expected_title = base::ASCIIToUTF16("play");
......@@ -329,8 +305,7 @@ IN_PROC_BROWSER_TEST_F(PictureInPictureContentBrowserTest,
OverlayWindow::PlaybackState::kPlaying);
// Simulate pausing playback by interacting with the PiP window.
ASSERT_TRUE(
ExecuteScript(shell()->web_contents(), "addPauseEventListener();"));
ASSERT_TRUE(ExecJs(shell(), "addPauseEventListener();"));
window_controller()->TogglePlayPause();
expected_title = base::ASCIIToUTF16("pause");
......@@ -365,26 +340,17 @@ IN_PROC_BROWSER_TEST_F(MediaSessionPictureInPictureContentBrowserTest,
shell(), GetTestUrl("media/picture_in_picture", "one-video.html")));
// Play and pause the player from script.
bool result = false;
ASSERT_TRUE(
ExecuteScriptAndExtractBool(shell()->web_contents(), "play();", &result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "play();"));
ASSERT_TRUE(ExecuteScript(shell()->web_contents(), "video.pause();"));
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterPictureInPictureAndGetResult();",
&result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterPictureInPicture();"));
EXPECT_EQ(overlay_window()->playback_state(),
OverlayWindow::PlaybackState::kPaused);
// Simulate resuming playback by invoking the Media Session "play" action
// through interaction with the PiP window.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"setMediaSessionPlayActionHandler();"));
ASSERT_TRUE(
ExecuteScript(shell()->web_contents(), "addPlayEventListener();"));
ASSERT_TRUE(ExecJs(shell(), "setMediaSessionPlayActionHandler();"));
ASSERT_TRUE(ExecJs(shell(), "addPlayEventListener();"));
window_controller()->TogglePlayPause();
base::string16 expected_title = base::ASCIIToUTF16("play");
......@@ -396,10 +362,8 @@ IN_PROC_BROWSER_TEST_F(MediaSessionPictureInPictureContentBrowserTest,
// Simulate pausing playback by invoking the Media Session "pause" action
// through interaction with the PiP window.
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"setMediaSessionPauseActionHandler();"));
ASSERT_TRUE(
ExecuteScript(shell()->web_contents(), "addPauseEventListener();"));
ASSERT_TRUE(ExecJs(shell(), "setMediaSessionPauseActionHandler();"));
ASSERT_TRUE(ExecJs(shell(), "addPauseEventListener();"));
window_controller()->TogglePlayPause();
expected_title = base::ASCIIToUTF16("pause");
......@@ -429,18 +393,11 @@ IN_PROC_BROWSER_TEST_F(AutoPictureInPictureContentBrowserTest,
ASSERT_TRUE(NavigateToURL(
shell(), GetTestUrl("media/picture_in_picture", "one-video.html")));
bool result = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(), "enterFullscreenAndGetResult();", &result));
ASSERT_TRUE(result);
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"video.autoPictureInPicture = true;"));
ASSERT_TRUE(ExecuteScript(shell()->web_contents(),
"addPictureInPictureEventListeners();"));
ASSERT_TRUE(
ExecuteScriptAndExtractBool(shell()->web_contents(), "play();", &result));
ASSERT_TRUE(result);
ASSERT_EQ(true, EvalJs(shell(), "enterFullscreen();"));
ASSERT_TRUE(ExecJs(shell(), "video.autoPictureInPicture = true;"));
ASSERT_TRUE(ExecJs(shell(), "addPictureInPictureEventListeners();"));
ASSERT_EQ(true, EvalJs(shell(), "play();"));
// Hide page and check that video entered Picture-in-Picture automatically.
shell()->web_contents()->WasHidden();
......
......@@ -16,24 +16,6 @@
});
}
function play() {
video.play()
.then(_ => window.domAutomationController.send(true))
.catch(e => window.domAutomationController.send(false));
}
function enterPictureInPictureAndGetResult() {
video.requestPictureInPicture()
.then(_ => window.domAutomationController.send(true))
.catch(e => window.domAutomationController.send(false));
}
function enterFullscreenAndGetResult() {
video.requestFullscreen()
.then(_ => window.domAutomationController.send(true))
.catch(e => window.domAutomationController.send(false));
}
function addPlayEventListener() {
video.addEventListener('play', () => {
document.title = 'play';
......@@ -46,6 +28,35 @@
}, { once: true });
}
async function play() {
await video.play();
return true;
}
async function enterPictureInPicture() {
await _waitForMetadata();
await video.requestPictureInPicture();
return true;
}
function _waitForMetadata() {
return new Promise((resolve, _) => {
if (video.readyState >= HTMLMediaElement.HAVE_METADATA) {
resolve();
return;
}
video.addEventListener('loadedmetadata', () => {
resolve();
}, { once: true });
});
}
async function enterFullscreen() {
await video.requestFullscreen();
return true;
}
function setMediaSessionPlayActionHandler() {
navigator.mediaSession.setActionHandler("play", _ => {
video.play();
......
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