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

Commit

Permalink
Add always deny options support
Browse files Browse the repository at this point in the history
(It won't popup notification if media is blocked)

fix #9755

Auditors: @bsclifton

Test Plan:
a. Manual Test
1. Go to about:preferences#security
2. Set Autoplay Media to `Always deny`
3. Visit https://www.cnet.com/news/mozilla-layoff-firefox-device-relevance/
4. It should blocked video without asking users

b Automatic Test
Covered by unit test
  • Loading branch information
darkdh committed Dec 21, 2017
1 parent 0147755 commit 1442c27
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/browser/reducers/autoplayReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ const appActions = require('../../../js/actions/appActions')
const {getOrigin} = require('../../../js/lib/urlutil')
const locale = require('../../locale')
const messages = require('../../../js/constants/messages')
const getSetting = require('../../../js/settings').getSetting
const settings = require('../../../js/constants/settings')
const {autoplayOption} = require('../../common/constants/settingsEnums')

let notificationCallbacks = []

const showAutoplayMessageBox = (state, tabId) => {
const tab = webContents.fromTabID(tabId)
if (!tab || tab.isDestroyed()) {
if (!tab || tab.isDestroyed() ||
getSetting(settings.AUTOPLAY_MEDIA) === autoplayOption.ALWAYS_DENY) {
return
}
const location = tab.getURL()
Expand Down
3 changes: 2 additions & 1 deletion app/common/constants/settingsEnums.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const fullscreenOption = {

const autoplayOption = {
ALWAYS_ASK: 'alwaysAsk',
ALWAYS_ALLOW: 'alwaysAllow'
ALWAYS_ALLOW: 'alwaysAllow',
ALWAYS_DENY: 'alwaysDeny'
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions js/about/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ class SecurityTab extends ImmutableComponent {
onChange={changeSetting.bind(null, this.props.onChangeSetting, settings.AUTOPLAY_MEDIA)}>
<option data-l10n-id='alwaysAsk' value={autoplayOption.ALWAYS_ASK} />
<option data-l10n-id='alwaysAllow' value={autoplayOption.ALWAYS_ALLOW} />
<option data-l10n-id='alwaysDeny' value={autoplayOption.ALWAYS_DENY} />
</SettingDropdown>
</SettingItem>
</SettingsList>
Expand Down
47 changes: 45 additions & 2 deletions test/unit/app/browser/reducers/autoplayReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ const fakeElectron = require('../../../lib/fakeElectron')

const appConstants = require('../../../../../js/constants/appConstants')
const messages = require('../../../../../js/constants/messages')
const settings = require('../../../../../js/constants/settings')
const {autoplayOption} = require('../../../../../app/common/constants/settingsEnums')
require('../../../braveUnit')

describe('autoplayReducer unit tests', function () {
let autoplayReducer
let fakeWebContents, fakeAppActions, fakeLocale
let fakeWebContents, fakeAppActions, fakeLocale, fakeSettings
let showNotificationSpy, hideNotificationSpy, translationSpy,
removeListenerSpy, changeSiteSettingSpy, removeSiteSettingSpy
removeListenerSpy, changeSiteSettingSpy, removeSiteSettingSpy, getSettingSpy
let autoplayMedia = autoplayOption.ALWAYS_ASK
const tabId = 123
const url = 'https://www.brave.com/niceplay'
const origin = 'https://www.brave.com'
Expand Down Expand Up @@ -78,16 +81,27 @@ describe('autoplayReducer unit tests', function () {
}
}

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

showNotificationSpy = sinon.spy(fakeAppActions, 'showNotification')
hideNotificationSpy = sinon.spy(fakeAppActions, 'hideNotification')
translationSpy = sinon.spy(fakeLocale, 'translation')
removeListenerSpy = sinon.spy(fakeElectron.ipcMain, 'removeListener')
changeSiteSettingSpy = sinon.spy(fakeAppActions, 'changeSiteSetting')
removeSiteSettingSpy = sinon.spy(fakeAppActions, 'removeSiteSetting')
getSettingSpy = sinon.spy(fakeSettings, 'getSetting')

mockery.registerMock('electron', fakeElectron)
mockery.registerMock('../../../js/actions/appActions', fakeAppActions)
mockery.registerMock('../../locale', fakeLocale)
mockery.registerMock('../../../js/settings', fakeSettings)

autoplayReducer = require('../../../../../app/browser/reducers/autoplayReducer')
})
Expand All @@ -99,12 +113,14 @@ describe('autoplayReducer unit tests', function () {
removeListenerSpy.restore()
changeSiteSettingSpy.restore()
removeSiteSettingSpy.restore()
getSettingSpy.restore()
mockery.deregisterAll()
mockery.disable()
})

describe('Allow autoplay once', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
Expand Down Expand Up @@ -143,6 +159,7 @@ describe('autoplayReducer unit tests', function () {

describe('Allow autoplay and remember', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
Expand Down Expand Up @@ -177,6 +194,7 @@ describe('autoplayReducer unit tests', function () {

describe('Deny autoplay once', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
Expand Down Expand Up @@ -207,6 +225,7 @@ describe('autoplayReducer unit tests', function () {

describe('Deny autoplay and remember', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
Expand Down Expand Up @@ -239,8 +258,31 @@ describe('autoplayReducer unit tests', function () {
})
})

describe('Always deny', function () {
before(function () {
translationSpy.reset()
showNotificationSpy.reset()
autoplayMedia = autoplayOption.ALWAYS_DENY
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
actionType: appConstants.APP_AUTOPLAY_BLOCKED,
tabId: tabId
}))
})

it('no calls local.translation', function () {
assert(translationSpy.notCalled)
})

it('no calls appActions.showNotification', function () {
assert(showNotificationSpy.withArgs(showNotificationArg).notCalled)
})
})

describe('Calling with exsting deny rules', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
showNotificationSpy.reset()
autoplayReducer(Immutable.fromJS({
siteSettings: {
Expand All @@ -265,6 +307,7 @@ describe('autoplayReducer unit tests', function () {

describe('APP_AUTOPLAY_DISMISSED', function () {
before(function () {
autoplayMedia = autoplayOption.ALWAYS_ASK
autoplayReducer(Immutable.fromJS({
siteSettings: {}
}), Immutable.fromJS({
Expand Down

0 comments on commit 1442c27

Please sign in to comment.