test: use WebContents event to test beforeunload (#23699)

This commit is contained in:
Cheng Zhao 2020-05-26 22:21:38 +09:00 committed by GitHub
parent 0dabd5e8c7
commit 08f288faf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 97 additions and 134 deletions

View file

@ -1,15 +1,14 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
function preventNextBeforeUnload() {
function installBeforeUnload(removeAfterNTimes) {
let count = 0
window.addEventListener('beforeunload', function handler(e) {
e.preventDefault();
e.returnValue = '';
window.removeEventListener('beforeunload', handler)
setTimeout(function() {
require('electron').ipcRenderer.sendSync('onbeforeunload')
}, 0);
return false;
setTimeout(() => console.log('beforeunload'))
if (++count <= removeAfterNTimes) {
e.preventDefault();
e.returnValue = '';
}
})
}
</script>

View file

@ -4,16 +4,11 @@
// Only prevent unload on the first window close
var unloadPrevented = false;
window.onbeforeunload = function() {
setTimeout(function() {
require('electron').ipcRenderer.sendSync('onbeforeunload');
}, 0);
if (!unloadPrevented) {
unloadPrevented = true;
return '';
}
}
window.onload = () => window.close();
</script>
</body>
</html>

View file

@ -1,23 +1,14 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
function run() {
// Only prevent unload on the first window close
var unloadPrevented = false;
window.onbeforeunload = function() {
setTimeout(function() {
require('electron').ipcRenderer.sendSync('onbeforeunload');
}, 0);
if (!unloadPrevented) {
unloadPrevented = true;
return false;
}
// Only prevent unload on the first window close
var unloadPrevented = false;
window.onbeforeunload = function() {
if (!unloadPrevented) {
unloadPrevented = true;
console.log('prevent')
return false;
}
// unload events don't get run unless load events have run.
if (document.readyState === 'complete')
window.close()
else
window.onload = () => window.close()
}
</script>
</body>

View file

@ -2,11 +2,7 @@
<body>
<script type="text/javascript" charset="utf-8">
window.onbeforeunload = function() {
setTimeout(function() {
require('electron').ipcRenderer.sendSync('onbeforeunload');
}, 0);
}
window.onload = () => window.close();
</script>
</body>
</html>