Commit 03212fef authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

Wait for focus before invoking document.hasFocus().

I'm not sure whether it should be this way - maybe
waiting for the page load (WaitForPageLoad) should
imply waiting for the focus as well; but this does
fix the previously flaky test. I've clarified the
name of the test also, since all this does is to
show that the two HeadlessWebContents instances
are independently focused at the same time.

R=caseq@chromium.com, eseckler@chromium.com

Bug: 828042
Change-Id: I174863b49ae525605a6aacb8750880d02fae3246
Reviewed-on: https://chromium-review.googlesource.com/1026993Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Johannes Henkel <johannes@google.com>
Cr-Commit-Position: refs/heads/master@{#553627}
parent f09f52a1
...@@ -322,14 +322,8 @@ IN_PROC_BROWSER_TEST_F(HeadlessNoDevToolsTabSocketTest, Test) { ...@@ -322,14 +322,8 @@ IN_PROC_BROWSER_TEST_F(HeadlessNoDevToolsTabSocketTest, Test) {
RunAsynchronousTest(); RunAsynchronousTest();
} }
#if defined(OS_WIN) IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest,
// crbug.com/828042 FocusOfHeadlessWebContents_IsIndependent) {
#define MAYBE_Focus DISABLED_Focus
#else
#define MAYBE_Focus Focus
#endif
IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, MAYBE_Focus) {
EXPECT_TRUE(embedded_test_server()->Start()); EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context = HeadlessBrowserContext* browser_context =
...@@ -340,6 +334,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, MAYBE_Focus) { ...@@ -340,6 +334,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, MAYBE_Focus) {
.SetInitialURL(embedded_test_server()->GetURL("/hello.html")) .SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build(); .Build();
EXPECT_TRUE(WaitForLoad(web_contents)); EXPECT_TRUE(WaitForLoad(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()");
...@@ -350,6 +345,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, MAYBE_Focus) { ...@@ -350,6 +345,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, MAYBE_Focus) {
.SetInitialURL(embedded_test_server()->GetURL("/hello.html")) .SetInitialURL(embedded_test_server()->GetURL("/hello.html"))
.Build(); .Build();
EXPECT_TRUE(WaitForLoad(web_contents2)); EXPECT_TRUE(WaitForLoad(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()");
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h" #include "content/public/test/test_navigation_observer.h"
#include "headless/lib/browser/headless_browser_impl.h" #include "headless/lib/browser/headless_browser_impl.h"
#include "headless/lib/browser/headless_web_contents_impl.h" #include "headless/lib/browser/headless_web_contents_impl.h"
...@@ -188,6 +189,14 @@ bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) { ...@@ -188,6 +189,14 @@ bool HeadlessBrowserTest::WaitForLoad(HeadlessWebContents* web_contents) {
return observer.last_navigation_succeeded(); return observer.last_navigation_succeeded();
} }
void HeadlessBrowserTest::WaitForFocus(HeadlessWebContents* web_contents) {
HeadlessWebContentsImpl* web_contents_impl =
HeadlessWebContentsImpl::From(web_contents);
content::FrameFocusedObserver observer(
web_contents_impl->web_contents()->GetMainFrame());
observer.Wait();
}
std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript( std::unique_ptr<runtime::EvaluateResult> HeadlessBrowserTest::EvaluateScript(
HeadlessWebContents* web_contents, HeadlessWebContents* web_contents,
const std::string& script) { const std::string& script) {
......
...@@ -74,6 +74,9 @@ class HeadlessBrowserTest : public content::BrowserTestBase { ...@@ -74,6 +74,9 @@ 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.
void WaitForFocus(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(
HeadlessWebContents* web_contents, HeadlessWebContents* web_contents,
......
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