Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Set initial default download path
Browse files Browse the repository at this point in the history
fix #9822

Auditors: @bridiver, @bbondy, @bsclifton

Test Plan:
a. Manual Test
1. Open Brave with fresh profile
2. Go to about:preferences#general
3. It should show default download path instead of empty

b. unit test
  • Loading branch information
darkdh committed Dec 4, 2017
1 parent 0147755 commit c60ba4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/browser/reducers/downloadsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ const {cancelDownload, pauseDownload, resumeDownload} = require('../electronDown
const {CANCEL, PAUSE, RESUME} = require('../../common/constants/electronDownloadItemActions')
const appActions = require('../../../js/actions/appActions')
const userPrefs = require('../../../js/state/userPrefs')
const getSetting = require('../../../js/settings').getSetting

const downloadsReducer = (state, action) => {
const download = action.downloadId ? state.getIn(['downloads', action.downloadId]) : undefined
if (!download &&
![appConstants.APP_MERGE_DOWNLOAD_DETAIL,
appConstants.APP_CLEAR_COMPLETED_DOWNLOADS,
appConstants.APP_SELECT_DEFAULT_DOWNLOAD_PATH,
appConstants.APP_CHANGE_SETTING].includes(action.actionType)) {
appConstants.APP_CHANGE_SETTING,
appConstants.APP_SET_STATE].includes(action.actionType)) {
return state
}
switch (action.actionType) {
case appConstants.APP_SET_STATE:
if (getSetting(settings.DOWNLOAD_DEFAULT_PATH) === '') {
const defaultPath = app.getPath('downloads')
appActions.changeSetting(settings.DOWNLOAD_DEFAULT_PATH, defaultPath)
}
break
case appConstants.APP_DOWNLOAD_REVEALED:
fs.access(download.get('savePath'), fs.constants.F_OK, (err) => {
if (err) {
Expand Down
35 changes: 35 additions & 0 deletions test/unit/app/browser/reducers/downloadsReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const process = require('process')
const assert = require('assert')
const uuid = require('uuid')
const path = require('path')
const appActions = require('../../../../../js/actions/appActions')
const fakeElectron = require('../../../lib/fakeElectron')

const appConstants = require('../../../../../js/constants/appConstants')
const {PENDING, IN_PROGRESS, RESUMING, PAUSED, COMPLETED, CANCELLED, INTERRUPTED} = require('../../../../../js/constants/downloadStates')
const settings = require('../../../../../js/constants/settings')
const {CANCEL} = require('../../../../../app/common/constants/electronDownloadItemActions')
require('../../../braveUnit')

Expand All @@ -32,17 +34,35 @@ const oneDownloadWithState = (state) => Immutable.fromJS({

describe('downloadsReducer', function () {
let downloadsReducer
let fakeSettings
let downloadDefaultPath
let changeSettingSpy
before(function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
})

fakeSettings = {
getSetting: (settingKey, settingsCollection) => {
switch (settingKey) {
case settings.DOWNLOAD_DEFAULT_PATH:
return downloadDefaultPath
}
}
}

changeSettingSpy = sinon.spy(appActions, 'changeSetting')

mockery.registerMock('electron', fakeElectron)
mockery.registerMock('../../../js/settings', fakeSettings)
mockery.registerMock('../../../js/actions/appActions', appActions)
downloadsReducer = require('../../../../../app/browser/reducers/downloadsReducer')
})

after(function () {
changeSettingSpy.restore()
mockery.disable()
})

Expand Down Expand Up @@ -257,4 +277,19 @@ describe('downloadsReducer', function () {
})
})
})

describe('APP_SET_STATE', function () {
it('DOWNLOAD_DEFAULT_PATH is empty', function () {
changeSettingSpy.reset()
downloadDefaultPath = ''
downloadsReducer({}, {actionType: appConstants.APP_SET_STATE})
assert(changeSettingSpy.withArgs(settings.DOWNLOAD_DEFAULT_PATH, `${process.cwd()}/downloads`).calledOnce)
})
it('DOWNLOAD_DEFAULT_PATH is not empty', function () {
changeSettingSpy.reset()
downloadDefaultPath = '123'
downloadsReducer({}, {actionType: appConstants.APP_SET_STATE})
assert(changeSettingSpy.notCalled)
})
})
})

0 comments on commit c60ba4b

Please sign in to comment.