Skip to content

test: fix flaky beforeunload tests - #23699

Merged
jkleinsc merged 1 commit into
masterfrom
refactor-beforeunload-tests
May 26, 2020
Merged

jkleinsc merged 1 commit into
masterfrom
refactor-beforeunload-tests

Conversation

@zcbenz

@zcbenz zcbenz commented May 21, 2020

Copy link
Copy Markdown
Contributor

Description of Change

The beforeunload tests are flaky for following reasons:

  1. Chromium disallows beforeunload handler to block window closing if there is no user interaction. (The purpose is to block ads.)
  2. Sending sync message in beforeunload handler sometimes do not work.

For 1), in our tests we rely on BrowserWindow.close and WebContents.loadURL to trigger beforeunload event, and they used to imply user interaction. After recent Chromium upgrades, they no longer imply so. The tests are still passing for most times because we are running lots of executeJavaScript and other APIs that make Chromium think there is user interaction.

For 2), I'm still very confused of it and I'm not even sure whether it is a correct statement, I only ran into it for a few times when testing and it went away once I tried to debug it.

So in order to fix the beforeunload tests, this PR does:

  1. Explicitly mark close and loadURL as user interaction to recover old behaviors.
  2. Use the BeforeUnloadFired event of WebContents instead of IPC messages to detect if beforeunload has ran.
  3. Due to Chromium not emitting BeforeUnloadFired for navigations, some tests are using console-message to get signals from beforeunload handler, which is still more robust than sending IPC messages directly.
  4. Tests are refactored so we don't run window.close() directly in the web pages, otherwise Chromium might not allow beforeunload to block closing.

Release Notes

Notes: none

@zcbenz
zcbenz requested a review from nornagon May 21, 2020 12:16
@zcbenz

zcbenz commented May 21, 2020

Copy link
Copy Markdown
Contributor Author

The beforeunload tests only seem to be flaky on master, so I'm not backporting this to old branches.

@deepak1556

Copy link
Copy Markdown
Member

Sending sync message in beforeunload handler sometimes do not work.

If the beforeunload handler returns a non-null value then the renderer will put the page into paused state to run the javascript dialog, I don't know if this is the scenario you had encountered.

Due to Chromium not emitting BeforeUnloadFired for navigations, some tests are using console-message to get signals from beforeunload handler, which is still more robust than sending IPC messages directly.

TIL, chromium seems to let the FrameTreeNode navigator handle it https://source.chromium.org/chromium/chromium/src/+/master:content/browser/frame_host/render_frame_host_impl.cc;l=3033 . But since the FrameTreeNode is very much alive, wonder if we can propose a patch in upstream to notify the WebContentsDelegate::BeforeUnloadFired for this case as well.

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zcbenz
zcbenz force-pushed the refactor-beforeunload-tests branch from 8b789f3 to ddf9f6d Compare May 26, 2020 02:45
@deepak1556 deepak1556 mentioned this pull request May 26, 2020
5 tasks done
@jkleinsc
jkleinsc merged commit 08f288f into master May 26, 2020
@release-clerk

release-clerk Bot commented May 26, 2020

Copy link
Copy Markdown

No Release Notes

@jkleinsc
jkleinsc deleted the refactor-beforeunload-tests branch May 26, 2020 13:21
@trop

trop Bot commented May 26, 2020

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "10-x-y", please check out #23766

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants