From cfb957a6031b3864ed4039c48a951969af60b993 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 2 Sep 2013 16:28:36 +0800 Subject: [PATCH] Add specs for the beforeunload handler. --- spec/api/window.coffee | 42 +++++++++++++++++-- spec/fixtures/api/beforeunload-false.html | 12 ++++++ .../api/close-beforeunload-false.html | 13 ++++++ .../api/close-beforeunload-string.html | 13 ++++++ .../fixtures/api/close-beforeunload-true.html | 14 +++++++ 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/api/beforeunload-false.html create mode 100644 spec/fixtures/api/close-beforeunload-false.html create mode 100644 spec/fixtures/api/close-beforeunload-string.html create mode 100644 spec/fixtures/api/close-beforeunload-true.html diff --git a/spec/api/window.coffee b/spec/api/window.coffee index fd8175055966..976c5772faac 100644 --- a/spec/api/window.coffee +++ b/spec/api/window.coffee @@ -21,10 +21,20 @@ describe 'window module', -> done() w.loadUrl 'file://' + path.join(fixtures, 'api', 'unload.html') - xdescribe 'window.close()', -> - it 'should emit unload handler', (done) -> + it 'should emit beforeunload handler', (done) -> w = new BrowserWindow(show: false) - w.on 'destroyed', -> + w.on 'onbeforeunload', -> + w.destroy() + done() + w.on 'loading-state-changed', (event, isLoading) -> + if (!isLoading) + w.close() + w.loadUrl 'file://' + path.join(fixtures, 'api', 'beforeunload-false.html') + + describe 'window.close()', -> + xit 'should emit unload handler', (done) -> + w = new BrowserWindow(show: false) + w.on 'closed', -> test = path.join(fixtures, 'api', 'close') content = fs.readFileSync(test) fs.unlinkSync(test) @@ -32,6 +42,11 @@ describe 'window module', -> done() w.loadUrl 'file://' + path.join(fixtures, 'api', 'close.html') + it 'should emit beforeunload handler', (done) -> + w = new BrowserWindow(show: false) + w.on 'onbeforeunload', done + w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-true.html') + describe 'BrowserWindow.loadUrl(url)', -> it 'should emit loading-state-changed event', (done) -> w = new BrowserWindow(show: false) @@ -49,3 +64,24 @@ describe 'window module', -> ++count w.loadUrl 'about:blank' + + describe 'beforeunload handler', -> + it 'returning true would not prevent close', (done) -> + w = new BrowserWindow(show: false) + w.on 'closed', -> + done() + w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-true.html') + + it 'returning false would prevent close', (done) -> + w = new BrowserWindow(show: false) + w.on 'onbeforeunload', -> + w.destroy() + done() + w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html') + + it 'returning non-empty string would prevent close', (done) -> + w = new BrowserWindow(show: false) + w.on 'onbeforeunload', -> + w.destroy() + done() + w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html') diff --git a/spec/fixtures/api/beforeunload-false.html b/spec/fixtures/api/beforeunload-false.html new file mode 100644 index 000000000000..06a2ecf804ec --- /dev/null +++ b/spec/fixtures/api/beforeunload-false.html @@ -0,0 +1,12 @@ + + + + + diff --git a/spec/fixtures/api/close-beforeunload-false.html b/spec/fixtures/api/close-beforeunload-false.html new file mode 100644 index 000000000000..d8e4f1f5817a --- /dev/null +++ b/spec/fixtures/api/close-beforeunload-false.html @@ -0,0 +1,13 @@ + + + + + diff --git a/spec/fixtures/api/close-beforeunload-string.html b/spec/fixtures/api/close-beforeunload-string.html new file mode 100644 index 000000000000..c4fc0baa592c --- /dev/null +++ b/spec/fixtures/api/close-beforeunload-string.html @@ -0,0 +1,13 @@ + + + + + diff --git a/spec/fixtures/api/close-beforeunload-true.html b/spec/fixtures/api/close-beforeunload-true.html new file mode 100644 index 000000000000..667afbb3d3d7 --- /dev/null +++ b/spec/fixtures/api/close-beforeunload-true.html @@ -0,0 +1,14 @@ + + + + + +