Skip to content

Commit

Permalink
test: add test for get/setSaveDialogOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
brenca committed Nov 2, 2018
1 parent 38c7378 commit ab3fc3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions spec/api-session-spec.js
@@ -1,4 +1,5 @@
const assert = require('assert')
const chai = require('chai')
const http = require('http')
const https = require('https')
const path = require('path')
Expand All @@ -9,6 +10,7 @@ const { closeWindow } = require('./window-helpers')

const { ipcRenderer, remote } = require('electron')
const { ipcMain, session, BrowserWindow, net } = remote
const { expect } = chai

/* The whole session API doesn't use standard callbacks */
/* eslint-disable standard/no-callback-literal */
Expand Down Expand Up @@ -421,6 +423,37 @@ describe('session module', () => {
})
})

it('can set options for the save dialog', (done) => {
downloadServer.listen(0, '127.0.0.1', () => {
const port = downloadServer.address().port
const options = {
window: null,
title: 'title',
message: 'message',
buttonLabel: 'buttonLabel',
nameFieldLabel: 'nameFieldLabel',
defaultPath: '/',
filters: [{
name: '1', extensions: ['.1', '.2']
}, {
name: '2', extensions: ['.3', '.4', '.5']
}],
showsTagField: true,
securityScopedBookmarks: true
}

ipcRenderer.sendSync('set-download-option', true, false, undefined, options)
w.webContents.downloadURL(`${url}:${port}`)
ipcRenderer.once('download-done', (event, state, url,
mimeType, receivedBytes,
totalBytes, disposition,
filename, savePath, dialogOptions) => {
expect(dialogOptions).to.deep.equal(options)
done()
})
})
})

describe('when a save path is specified and the URL is unavailable', () => {
it('does not display a save dialog and reports the done state as interrupted', (done) => {
ipcRenderer.sendSync('set-download-option', false, false)
Expand Down
9 changes: 8 additions & 1 deletion spec/static/main.js
Expand Up @@ -138,6 +138,7 @@ app.on('ready', function () {
backgroundThrottling: false
}
})
window.webContents.openDevTools({ mode: 'detach' })
window.loadFile('static/index.html', {
query: {
grep: argv.grep,
Expand All @@ -161,7 +162,11 @@ app.on('ready', function () {
// For session's download test, listen 'will-download' event in browser, and
// reply the result to renderer for verifying
const downloadFilePath = path.join(__dirname, '..', 'fixtures', 'mock.pdf')
ipcMain.on('set-download-option', function (event, needCancel, preventDefault, filePath = downloadFilePath) {
ipcMain.on('set-download-option', function (event, needCancel, preventDefault, filePath = downloadFilePath, dialogOptions = {}) {
// Set default when undefined was given on the other side of the IPC channel
if (filePath === null) {
filePath = downloadFilePath
}
window.webContents.session.once('will-download', function (e, item) {
window.webContents.send('download-created',
item.getState(),
Expand All @@ -187,6 +192,7 @@ app.on('ready', function () {
item.resume()
} else {
item.setSavePath(filePath)
item.setSaveDialogOptions(dialogOptions)
}
item.on('done', function (e, state) {
window.webContents.send('download-done',
Expand All @@ -198,6 +204,7 @@ app.on('ready', function () {
item.getContentDisposition(),
item.getFilename(),
item.getSavePath(),
item.getSaveDialogOptions(),
item.getURLChain(),
item.getLastModifiedTime(),
item.getETag())
Expand Down

0 comments on commit ab3fc3f

Please sign in to comment.