Skip to content

Commit

Permalink
Harden AutoplayPermissionContextBrowserTest
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Aug 5, 2018
1 parent 3d3e4b6 commit 34fc976
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
39 changes: 16 additions & 23 deletions browser/autoplay/autoplay_permission_context_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@ const char kVideoPlayingDetect[] =
"window.domAutomationController.send(document.getElementById('status')."
"textContent);";

namespace {

class PageReloadWaiter {
public:
explicit PageReloadWaiter(content::WebContents* web_contents)
: web_contents_(web_contents),
navigation_observer_(web_contents,
web_contents->GetLastCommittedURL()) {}

bool Wait() {
navigation_observer_.WaitForNavigationFinished();
return content::WaitForLoadStop(web_contents_);
}

private:
content::WebContents* web_contents_;
content::TestNavigationManager navigation_observer_;
};

} // namespace

class AutoplayPermissionContextBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
Expand All @@ -59,7 +38,7 @@ class AutoplayPermissionContextBrowserTest : public InProcessBrowserTest {

brave::RegisterPathProvider();
base::FilePath test_data_dir;
PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
test_data_dir = test_data_dir.AppendASCII("autoplay");
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);

Expand Down Expand Up @@ -141,6 +120,13 @@ class AutoplayPermissionContextBrowserTest : public InProcessBrowserTest {
return WaitForLoadStop(contents());
}

void WaitForPlaying() {
std::string msg_from_renderer;
ASSERT_TRUE(ExecuteScriptAndExtractString(contents(), "notifyWhenPlaying();",
&msg_from_renderer));
ASSERT_EQ("PLAYING", msg_from_renderer);
}

private:
GURL autoplay_method_url_;
GURL autoplay_attr_url_;
Expand Down Expand Up @@ -206,6 +192,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, ClickAllow) {
EXPECT_TRUE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(1, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -220,6 +207,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, ClickAllow) {
EXPECT_TRUE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(1, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -235,6 +223,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, ClickAllow) {
EXPECT_TRUE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(1, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -249,6 +238,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, ClickAllow) {
EXPECT_TRUE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(1, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand Down Expand Up @@ -333,6 +323,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, AllowAutoplay) {
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -344,6 +335,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, AllowAutoplay) {
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -358,6 +350,7 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, AllowAutoplay) {
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
Expand All @@ -369,10 +362,10 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, AllowAutoplay) {
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(ExecuteScriptAndExtractString(contents(),
kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);

}

// Block autoplay
Expand Down
9 changes: 9 additions & 0 deletions test/data/autoplay/autoplay_by_attr.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
function playing () {
let status = document.getElementById('status')
status.textContent = 'Video playing'
window.playing = true;
if (window.notifyBrowser)
notifyWhenPlaying();
}
function notifyWhenPlaying() {
if (playing)
window.domAutomationController.send("PLAYING");
else
window.notifyBrowser = true;
}
</script>

Expand Down
9 changes: 9 additions & 0 deletions test/data/autoplay/autoplay_by_attr_muted.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
function playing () {
let status = document.getElementById('status')
status.textContent = 'Video playing'
window.playing = true;
if (window.notifyBrowser)
notifyWhenPlaying();
}
function notifyWhenPlaying() {
if (playing)
window.domAutomationController.send("PLAYING");
else
window.notifyBrowser = true;
}
</script>

Expand Down
9 changes: 9 additions & 0 deletions test/data/autoplay/autoplay_by_method.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
function playing () {
let status = document.getElementById('status')
status.textContent = 'Video playing'
window.playing = true;
if (window.notifyBrowser)
notifyWhenPlaying();
}
function notifyWhenPlaying() {
if (playing)
window.domAutomationController.send("PLAYING");
else
window.notifyBrowser = true;
}
</script>

Expand Down
9 changes: 9 additions & 0 deletions test/data/autoplay/autoplay_by_method_muted.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
function playing () {
let status = document.getElementById('status')
status.textContent = 'Video playing'
window.playing = true;
if (window.notifyBrowser)
notifyWhenPlaying();
}
function notifyWhenPlaying() {
if (playing)
window.domAutomationController.send("PLAYING");
else
window.notifyBrowser = true;
}
</script>

Expand Down

0 comments on commit 34fc976

Please sign in to comment.