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

Show browser action as disabled when shield is disabled #37

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/assets/img/icon-16-disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 0 additions & 13 deletions app/background/api/badgeAPI.ts

This file was deleted.

29 changes: 29 additions & 0 deletions app/background/api/browserActionAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* Sets the badge text
* @param {string} text - The text to put on the badge
*/
export const setBadgeText = (text: string) => {
if (chrome.browserAction) {
chrome.browserAction.setBadgeText({ text: String(text) })
}
}

/**
* Updates the shields icon based on shields state
*/
export const setIcon = (url: string, tabId: number, shieldsOn: boolean) => {
const shieldsEnabledIcon = '../../img/icon-16.png'
const shieldsDisabledIcon = '../../img/icon-16-disabled.png'
const isHttpOrHttps = url && /^http/.test(url)

if (chrome.browserAction) {
chrome.browserAction.setIcon({
path: shieldsOn && isHttpOrHttps ? shieldsEnabledIcon : shieldsDisabledIcon,
tabId
})
}
}
16 changes: 15 additions & 1 deletion app/background/reducers/shieldsPanelReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
requestShieldPanelData,
setAllowScriptOriginsOnce
} from '../api/shieldsAPI'
import { setBadgeText } from '../api/badgeAPI'
import { setBadgeText, setIcon } from '../api/browserActionAPI'
import { reloadTab } from '../api/tabsAPI'
import * as shieldsPanelState from '../../state/shieldsPanelState'
import { State, Tab } from '../../types/state/shieldsPannelState'
Expand All @@ -33,12 +33,23 @@ const updateBadgeText = (state: State) => {
}
}

const updateShieldsIcon = (state: State) => {
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tab: Tab = state.tabs[tabId]
if (tab) {
const url: string = tab.url
const isShieldsActive: boolean = state.tabs[tabId].braveShields === 'allow'
setIcon(url, tabId, isShieldsActive)
}
}

const focusedWindowChanged = (state: State, windowId: number): State => {
if (windowId !== -1) {
state = shieldsPanelState.updateFocusedWindow(state, windowId)
if (shieldsPanelState.getActiveTabId(state)) {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
updateBadgeText(state)
updateShieldsIcon(state)
} else {
console.warn('no tab id so cannot request shield data from window focus change!')
}
Expand Down Expand Up @@ -84,6 +95,7 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
const tabId: number = action.tabId
state = updateActiveTab(state, windowId, tabId)
updateBadgeText(state)
updateShieldsIcon(state)
break
}
case tabTypes.TAB_DATA_CHANGED:
Expand All @@ -92,6 +104,7 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
if (tab.active && tab.id) {
state = updateActiveTab(state, tab.windowId, tab.id)
updateBadgeText(state)
updateShieldsIcon(state)
}
break
}
Expand All @@ -105,6 +118,7 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
if (tab.active && tab.id) {
state = updateActiveTab(state, tab.windowId, tab.id)
updateBadgeText(state)
updateShieldsIcon(state)
}
break
}
Expand Down
28 changes: 0 additions & 28 deletions test/app/background/api/badgeAPITest.ts

This file was deleted.

86 changes: 86 additions & 0 deletions test/app/background/api/browserActionAPITest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* global describe, it, before, after */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import 'mocha'
import * as sinon from 'sinon'
import * as assert from 'assert'
import * as browserActionAPI from '../../../../app/background/api/browserActionAPI'

describe('BrowserAction API', () => {
describe('setBadgeText', function () {
before(function () {
this.spy = sinon.spy(chrome.browserAction, 'setBadgeText')
this.text = '42'
browserActionAPI.setBadgeText(this.text)
})
after(function () {
this.spy.restore()
})
it('calls chrome.browserAction.setBadgeText with the text', function () {
assert(this.spy.calledOnce)
assert.deepEqual(this.spy.getCall(0).args[0], {
text: this.text
})
})
})
describe('setIcon', function () {
const enabledIconPath = '../../img/icon-16.png'
const disabledIconPath = '../../img/icon-16-disabled.png'
before(function () {
this.spy = sinon.spy(chrome.browserAction, 'setIcon')
this.url = 'https://brave.com'
this.tabId = 1
this.shieldsEnabled = true
})
after(function () {
this.spy.restore()
})
afterEach(function () {
this.spy.reset()
})
it('sets the enabled icon when protocol is http', function () {
this.url = 'http://not-very-awesome-http-page.com'
browserActionAPI.setIcon(this.url, this.tabId, this.shieldsEnabled)
assert.deepEqual(this.spy.getCall(0).args[0], {
path: enabledIconPath,
tabId: this.tabId
})
})
it('sets the enabled icon when protocol is https', function () {
this.url = 'https://very-awesome-https-page.com'
browserActionAPI.setIcon(this.url, this.tabId, this.shieldsEnabled)
assert.deepEqual(this.spy.getCall(0).args[0], {
path: enabledIconPath,
tabId: this.tabId
})
})
it('sets the disabled icon when the protocol is neither https nor http', function () {
this.url = 'brave://welcome'
browserActionAPI.setIcon(this.url, this.tabId, this.shieldsEnabled)
assert.deepEqual(this.spy.getCall(0).args[0], {
path: disabledIconPath,
tabId: this.tabId
})
})
it('sets the disabled icon when the protocol is http and shield is off', function () {
this.url = 'http://not-very-awesome-http-page.com'
this.shieldsEnabled = false
browserActionAPI.setIcon(this.url, this.tabId, this.shieldsEnabled)
assert.deepEqual(this.spy.getCall(0).args[0], {
path: disabledIconPath,
tabId: this.tabId
})
})
it('sets the disabled icon when the protocol is https and shield is off', function () {
this.url = 'https://very-awesome-https-page.com'
this.shieldsEnabled = false
browserActionAPI.setIcon(this.url, this.tabId, this.shieldsEnabled)
assert.deepEqual(this.spy.getCall(0).args[0], {
path: disabledIconPath,
tabId: this.tabId
})
})
})
})
8 changes: 5 additions & 3 deletions test/app/background/reducers/shieldsPanelReducerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as webNavigationTypes from '../../../../app/constants/webNavigationType
import shieldsPanelReducer from '../../../../app/background/reducers/shieldsPanelReducer'
import * as shieldsAPI from '../../../../app/background/api/shieldsAPI'
import * as tabsAPI from '../../../../app/background/api/tabsAPI'
import * as badgeAPI from '../../../../app/background/api/badgeAPI'
import * as browserActionAPI from '../../../../app/background/api/browserActionAPI'
import * as shieldsPanelState from '../../../../app/state/shieldsPanelState'
import { initialState } from '../../../testData'
import * as deepFreeze from 'deep-freeze-node'
Expand Down Expand Up @@ -112,7 +112,9 @@ describe('braveShieldsPanelReducer', () => {
windows: {
1: this.tabId
},
tabs: {}
tabs: {
[this.tabId]: { url: 'https://brave.com' }
}
})
shieldsPanelReducer(state, {
type: windowTypes.WINDOW_FOCUS_CHANGED,
Expand Down Expand Up @@ -419,7 +421,7 @@ describe('braveShieldsPanelReducer', () => {

describe('RESOURCE_BLOCKED', function () {
before(function () {
this.spy = sinon.spy(badgeAPI, 'setBadgeText')
this.spy = sinon.spy(browserActionAPI, 'setBadgeText')
})
after(function () {
this.spy.restore()
Expand Down
3 changes: 2 additions & 1 deletion test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export const getMockChrome = () => {
onStartup: new ChromeEvent()
},
browserAction: {
setBadgeText: function (text: string) { }
setBadgeText: function (text: string) { },
setIcon: function (icon: string, tabId: number) { }
},
tabs: {
queryAsync: function () {
Expand Down