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

Commit 40fc107

Browse files
committed
Fix issue @alexwykoff found while testing 0.14.0 RC2. Fixes are reinforced by unit tests
Auditors: @alexwykoff, @NejcZdovc
1 parent 039c515 commit 40fc107

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

app/common/state/extensionState.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ const extensionState = {
1818
},
1919

2020
getEnabledExtensions: (state) => {
21-
return state.get('extensions').filter((installInfo, extensionId) => {
22-
return installInfo.get('enabled') === true
23-
})
21+
const extensions = state.get('extensions')
22+
if (extensions) {
23+
return extensions.filter((installInfo, extensionId) => {
24+
return installInfo.get('enabled') === true
25+
})
26+
}
27+
return Immutable.fromJS([])
2428
},
2529

2630
getExtensionById: (state, extensionId) => {

js/contextMenus.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {bookmarksToolbarMode} = require('../app/common/constants/settingsEnums')
3939
const extensionState = require('../app/common/state/extensionState')
4040
const extensionActions = require('../app/common/actions/extensionActions')
4141
const appStore = require('./stores/appStoreRenderer')
42+
const {makeImmutable} = require('../app/common/state/immutableUtil')
4243

4344
const isDarwin = process.platform === 'darwin'
4445
const isLinux = process.platform === 'linux'
@@ -951,12 +952,15 @@ function addLinkMenu (link, frame) {
951952
function mainTemplateInit (nodeProps, frame, tab) {
952953
let template = []
953954

955+
nodeProps = nodeProps || {}
956+
frame = makeImmutable(frame || {})
957+
954958
const isLink = nodeProps.linkURL && nodeProps.linkURL !== ''
955959
const isImage = nodeProps.mediaType === 'image'
956960
const isVideo = nodeProps.mediaType === 'video'
957961
const isAudio = nodeProps.mediaType === 'audio'
958962
const isInputField = nodeProps.isEditable || nodeProps.inputFieldType !== 'none'
959-
const isTextSelected = nodeProps.selectionText.length > 0
963+
const isTextSelected = nodeProps.selectionText && nodeProps.selectionText.length > 0
960964
const isAboutPage = aboutUrls.has(frame.get('location'))
961965

962966
if (isLink) {
@@ -1022,7 +1026,10 @@ function mainTemplateInit (nodeProps, frame, tab) {
10221026
}
10231027
}
10241028

1025-
const editableItems = getEditableItems(nodeProps.selectionText, nodeProps.editFlags, true)
1029+
const editableItems = nodeProps.selectionText
1030+
? getEditableItems(nodeProps.selectionText, nodeProps.editFlags, true)
1031+
: []
1032+
10261033
template.push(...misspelledSuggestions, {
10271034
label: locale.translation('undo'),
10281035
accelerator: 'CmdOrCtrl+Z',

test/unit/js/contextMenusTest.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
/* global describe, before, after, it */
5+
6+
const mockery = require('mockery')
7+
const assert = require('assert')
8+
const sinon = require('sinon')
9+
let fakeElectronMenu
10+
let contextMenus
11+
require('../braveUnit')
12+
13+
describe('Context menu module unit tests', function () {
14+
const fakeLocale = {
15+
translation: (token) => { return token }
16+
}
17+
18+
before(function () {
19+
mockery.enable({
20+
warnOnReplace: false,
21+
warnOnUnregistered: false,
22+
useCleanCache: true
23+
})
24+
mockery.registerMock('electron', require('../lib/fakeElectron'))
25+
mockery.registerMock('../js/l10n', fakeLocale)
26+
contextMenus = require('../../../js/contextMenus')
27+
fakeElectronMenu = require('../lib/fakeElectronMenu')
28+
})
29+
30+
after(function () {
31+
mockery.disable()
32+
})
33+
34+
describe('onMainContextMenu', function () {
35+
describe('when calling mainTemplateInit', function () {
36+
let menuPopupSpy
37+
let menuDestroySpy
38+
39+
before(function () {
40+
menuPopupSpy = sinon.spy(fakeElectronMenu, 'popup')
41+
menuDestroySpy = sinon.spy(fakeElectronMenu, 'destroy')
42+
})
43+
44+
after(function () {
45+
menuPopupSpy.restore()
46+
})
47+
48+
it('calls menu.popup', function () {
49+
menuPopupSpy.reset()
50+
contextMenus.onMainContextMenu()
51+
assert.equal(menuPopupSpy.calledOnce, true)
52+
})
53+
54+
it('calls menu.destroy', function () {
55+
menuDestroySpy.reset()
56+
contextMenus.onMainContextMenu()
57+
assert.equal(menuDestroySpy.calledOnce, true)
58+
})
59+
})
60+
})
61+
})

test/unit/lib/fakeElectron.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const fakeElectron = {
2323
isMaximized: () => false,
2424
webContents: {}
2525
}
26+
},
27+
Menu: {
28+
buildFromTemplate: (template) => {
29+
return require('./fakeElectronMenu')
30+
}
2631
}
2732
},
2833
app: {

test/unit/lib/fakeElectronMenu.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const fakeElectronMenu = {
2+
popup: () => {},
3+
destroy: () => {}
4+
}
5+
6+
module.exports = fakeElectronMenu

0 commit comments

Comments
 (0)