spec: serviceWorker registration with file scheme
This commit is contained in:
parent
bff2861311
commit
68733eb8df
3 changed files with 48 additions and 1 deletions
|
@ -5,7 +5,7 @@ path = require 'path'
|
|||
ws = require 'ws'
|
||||
|
||||
{remote} = require 'electron'
|
||||
{BrowserWindow} = remote.require 'electron'
|
||||
{BrowserWindow, session} = remote.require 'electron'
|
||||
|
||||
describe 'chromium feature', ->
|
||||
fixtures = path.resolve __dirname, 'fixtures'
|
||||
|
@ -56,6 +56,26 @@ describe 'chromium feature', ->
|
|||
it 'should not be empty', ->
|
||||
assert.notEqual navigator.language, ''
|
||||
|
||||
describe 'navigator.serviceWorker', ->
|
||||
url = "file://#{fixtures}/pages/service-worker/index.html"
|
||||
w = null
|
||||
|
||||
afterEach ->
|
||||
w?.destroy()
|
||||
|
||||
it 'should register for file scheme', (done) ->
|
||||
w = new BrowserWindow(show:false)
|
||||
w.webContents.on 'ipc-message', (event, args) ->
|
||||
if args[0] == 'reload'
|
||||
w.webContents.reload()
|
||||
else if args[0] == 'error'
|
||||
done('unexpected error : ' + args[1])
|
||||
else if args[0] == 'response'
|
||||
assert.equal args[1], 'Hello from serviceWorker!'
|
||||
session.defaultSession.clearStorageData {storages: ['serviceworkers']}, ->
|
||||
done()
|
||||
w.loadURL url
|
||||
|
||||
describe 'window.open', ->
|
||||
@timeout 10000
|
||||
|
||||
|
|
18
spec/fixtures/pages/service-worker/index.html
vendored
Normal file
18
spec/fixtures/pages/service-worker/index.html
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
<script>
|
||||
const ipcRenderer = require('electron').ipcRenderer;
|
||||
navigator.serviceWorker.register('service-worker.js', {scope: './'}).then(function() {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', 'http://dummy/echo');
|
||||
xhr.setRequestHeader('X-Mock-Response', 'yes');
|
||||
xhr.addEventListener('load', function() {
|
||||
ipcRenderer.send('response', xhr.responseText);
|
||||
});
|
||||
xhr.send();
|
||||
} else {
|
||||
ipcRenderer.send('reload');
|
||||
}
|
||||
}).catch(function(error) {
|
||||
ipcRenderer.send('error', error);
|
||||
})
|
||||
</script>
|
9
spec/fixtures/pages/service-worker/service-worker.js
vendored
Normal file
9
spec/fixtures/pages/service-worker/service-worker.js
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
self.addEventListener('fetch', function(event) {
|
||||
var requestUrl = new URL(event.request.url);
|
||||
|
||||
if (requestUrl.pathname === '/echo' &&
|
||||
event.request.headers.has('X-Mock-Response')) {
|
||||
var mockResponse = new Response('Hello from serviceWorker!');
|
||||
event.respondWith(mockResponse);
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue