Commit b3a06c1b authored by arthursonzogni's avatar arthursonzogni Committed by Commit Bot

Fix flaky HeadlessWebContentsTest focus test.

This test is a bit flaky:
https://test-results.appspot.com/dashboards/flakiness_dashboard.html#testType=headless_browsertests&tests=FocusOfHeadlessWebContents_IsIndependent

Furthermore, I am working on https://crbug.com/831155. It causes the
frame to commit a navigation more quickly. As a result the frame may
stop loading sooner. It causes this test to become even flakier.

Why this test is flaky?
It waits for the main frame to stop loading by calling WaitForLoad().
Then it waits for the main frame to gain focus by calling
WaitForFocus(). In reality, it may gain focus before it stops loading.
Since WaitForFocus() doesn't check whether or not the main frame is
already focused before waiting, it may wait forever.

The CL makes the tests wait for both event to happen, no matter the
order.

Related CL:
https://chromium-review.googlesource.com/c/chromium/src/+/1026993

Bug: 831155
Change-Id: Id9002b3ee2a06ef17d670858688d28959f334e03
Reviewed-on: https://chromium-review.googlesource.com/1053777
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558262}
parent eacd48f8
...@@ -334,8 +334,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, ...@@ -334,8 +334,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest,
browser_context->CreateWebContentsBuilder() browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/hello.html")) .SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build(); .Build();
EXPECT_TRUE(WaitForLoad(web_contents)); WaitForLoadAndGainFocus(web_contents);
WaitForFocus(web_contents);
std::unique_ptr<runtime::EvaluateResult> has_focus = std::unique_ptr<runtime::EvaluateResult> has_focus =
EvaluateScript(web_contents, "document.hasFocus()"); EvaluateScript(web_contents, "document.hasFocus()");
...@@ -345,8 +344,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, ...@@ -345,8 +344,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest,
browser_context->CreateWebContentsBuilder() browser_context->CreateWebContentsBuilder()
.SetInitialURL(embedded_test_server()->GetURL("/hello.html")) .SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build(); .Build();
EXPECT_TRUE(WaitForLoad(web_contents2)); WaitForLoadAndGainFocus(web_contents2);
WaitForFocus(web_contents2);
// Focus of different WebContents is independent. // Focus of different WebContents is independent.
has_focus = EvaluateScript(web_contents, "document.hasFocus()"); has_focus = EvaluateScript(web_contents, "document.hasFocus()");
......
...@@ -189,12 +189,18 @@ bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { ...@@ -189,12 +189,18 @@ bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) {
return observer.last_navigation_succeeded(); return observer.last_navigation_succeeded();
} }
void HeadlessBrowserTest::WaitForFocus(HeadlessWebContents* web_contents) { void HeadlessBrowserTest::WaitForLoadAndGainFocus(
HeadlessWebContentsImpl* web_contents_impl = HeadlessWebContents* web_contents) {
HeadlessWebContentsImpl::From(web_contents); content::WebContents* content =
content::FrameFocusedObserver observer( HeadlessWebContentsImpl::From(web_contents)->web_contents();
web_contents_impl->web_contents()->GetMainFrame());
observer.Wait(); // To finish loading and to gain focus are two independent events. Which one
// is issued first is undefined. The following code is waiting on both, in any
// order.
content::TestNavigationObserver load_observer(content, 1);
content::FrameFocusedObserver focus_observer(content->GetMainFrame());
load_observer.Wait();
focus_observer.Wait();
} }
std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript( std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript(
......
...@@ -74,8 +74,8 @@ class HeadlessBrowserTest : public content::BrowserTestBase { ...@@ -74,8 +74,8 @@ class HeadlessBrowserTest : public content::BrowserTestBase {
// Synchronously waits for a tab to finish loading. // Synchronously waits for a tab to finish loading.
bool WaitForLoad(HeadlessWebContents* web_contents); bool WaitForLoad(HeadlessWebContents* web_contents);
// Synchronously waits for a tab to finish gaining focus. // Synchronously waits for a tab to finish loading and to gain focus.
void WaitForFocus(HeadlessWebContents* web_contents); void WaitForLoadAndGainFocus(HeadlessWebContents* web_contents);
// Synchronously evaluates a script and returns the result. // Synchronously evaluates a script and returns the result.
std::unique_ptr<runtime::EvaluateResult> EvaluateScript( std::unique_ptr<runtime::EvaluateResult> EvaluateScript(
......
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