Remove unneeded reducers/filters/types/etc
Code was changed at some point (in the API) to directly use chrome storage APIs (not using redux / store)
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
/* 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 * as types from '../constants/cosmeticFilterTypes'
|
||||
import * as actions from '../types/actions/cosmeticFilterActions'
|
||||
|
||||
export const siteCosmeticFilterAdded: actions.SiteCosmeticFilterAdded = (origin: string, cssfilter: string) => {
|
||||
return {
|
||||
type: types.SITE_COSMETIC_FILTER_ADDED,
|
||||
origin,
|
||||
cssfilter
|
||||
}
|
||||
}
|
||||
|
||||
export const siteCosmeticFilterRemoved: actions.SiteCosmeticFilterRemoved = (origin: string) => {
|
||||
return {
|
||||
type: types.SITE_COSMETIC_FILTER_REMOVED,
|
||||
origin
|
||||
}
|
||||
}
|
||||
|
||||
export const allCosmeticFiltersRemoved: actions.AllCosmeticFiltersRemoved = () => {
|
||||
return {
|
||||
type: types.ALL_COSMETIC_FILTERS_REMOVED
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
* 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/. */
|
||||
|
||||
require('./actions/cosmeticFilterActions')
|
||||
require('./actions/shieldsPanelActions')
|
||||
require('./actions/webNavigationActions')
|
||||
require('./actions/runtimeActions')
|
||||
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
/* 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 { bindActionCreators } from 'redux'
|
||||
import store from '../store'
|
||||
import * as cosmeticFilterActions from '../../actions/cosmeticFilterActions'
|
||||
export default bindActionCreators(cosmeticFilterActions, store.dispatch)
|
||||
+10
-5
@@ -1,5 +1,9 @@
|
||||
import cosmeticFilterActions from '../actions/cosmeticFilterActions'
|
||||
import { getLocale } from '../api/localeAPI'
|
||||
import {
|
||||
addSiteCosmeticFilter,
|
||||
removeSiteFilter,
|
||||
removeAllFilters
|
||||
} from '../api/cosmeticFilterAPI'
|
||||
|
||||
export let rule = {
|
||||
host: '',
|
||||
@@ -54,11 +58,11 @@ export function onContextMenuClicked (info: chrome.contextMenus.OnClickData, tab
|
||||
query()
|
||||
break
|
||||
case 'resetSiteFilterSettings': {
|
||||
cosmeticFilterActions.siteCosmeticFilterRemoved(rule.host)
|
||||
removeSiteFilter(rule.host)
|
||||
break
|
||||
}
|
||||
case 'resetAllFilterSettings': {
|
||||
cosmeticFilterActions.allCosmeticFiltersRemoved()
|
||||
removeAllFilters()
|
||||
break
|
||||
}
|
||||
default: {
|
||||
@@ -77,7 +81,7 @@ export function tabsCallback (tabs: any) {
|
||||
chrome.tabs.sendMessage(tabs[0].id, { type: 'getTargetSelector' }, onSelectorReturned)
|
||||
}
|
||||
|
||||
export function onSelectorReturned (response: any) {
|
||||
export async function onSelectorReturned (response: any) {
|
||||
if (!response) {
|
||||
rule.selector = window.prompt('We were unable to automatically populate a correct CSS selector for you. Please manually enter a CSS selector to block:') || ''
|
||||
} else {
|
||||
@@ -89,6 +93,7 @@ export function onSelectorReturned (response: any) {
|
||||
code: `${rule.selector} {display: none !important;}`,
|
||||
cssOrigin: 'user'
|
||||
})
|
||||
cosmeticFilterActions.siteCosmeticFilterAdded(rule.host, rule.selector)
|
||||
|
||||
await addSiteCosmeticFilter(rule.host, rule.selector)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,11 @@
|
||||
import { combineReducers } from 'redux'
|
||||
|
||||
import shieldsPanelReducer from './reducers/shieldsPanelReducer'
|
||||
import cosmeticFilterReducer from './reducers/cosmeticFilterReducer'
|
||||
import dappDetectionReducer from './reducers/dappDetectionReducer'
|
||||
import runtimeReducer from './reducers/runtimeReducer'
|
||||
|
||||
export default combineReducers({
|
||||
shieldsPanel: shieldsPanelReducer,
|
||||
cosmeticFilter: cosmeticFilterReducer,
|
||||
dappDetection: dappDetectionReducer,
|
||||
runtime: runtimeReducer
|
||||
})
|
||||
|
||||
-162
@@ -1,162 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
// Background
|
||||
import * as storageAPI from '../api/storageAPI'
|
||||
|
||||
// Types
|
||||
import * as shieldsPanelTypes from '../../constants/shieldsPanelTypes'
|
||||
import * as windowTypes from '../../constants/windowTypes'
|
||||
import * as tabTypes from '../../constants/tabTypes'
|
||||
import * as webNavigationTypes from '../../constants/webNavigationTypes'
|
||||
import * as cosmeticFilterTypes from '../../constants/cosmeticFilterTypes'
|
||||
import { State, PersistentData } from '../../types/state/shieldsPannelState'
|
||||
import { Actions } from '../../types/actions/index'
|
||||
|
||||
// APIs
|
||||
import { setAllowBraveShields, requestShieldPanelData } from '../api/shieldsAPI'
|
||||
import { reloadTab } from '../api/tabsAPI'
|
||||
import {
|
||||
removeSiteFilter,
|
||||
addSiteCosmeticFilter,
|
||||
applySiteFilters,
|
||||
removeAllFilters
|
||||
} from '../api/cosmeticFilterAPI'
|
||||
|
||||
// State helpers
|
||||
import * as shieldsPanelState from '../../state/shieldsPanelState'
|
||||
import * as noScriptState from '../../state/noScriptState'
|
||||
import { getOrigin, getHostname } from '../../helpers/urlUtils'
|
||||
import { areObjectsEqual } from '../../helpers/objectUtils'
|
||||
|
||||
const focusedWindowChanged = (state: State, windowId: number): State => {
|
||||
if (windowId !== -1) {
|
||||
state = shieldsPanelState.updateFocusedWindow(state, windowId)
|
||||
if (shieldsPanelState.getActiveTabId(state)) {
|
||||
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
|
||||
} else {
|
||||
console.warn('no tab id so cannot request shield data from window focus change!')
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
const updateActiveTab = (state: State, windowId: number, tabId: number): State => {
|
||||
requestShieldPanelData(tabId)
|
||||
return shieldsPanelState.updateActiveTab(state, windowId, tabId)
|
||||
}
|
||||
|
||||
export default function cosmeticFilterReducer (
|
||||
state: State = {
|
||||
persistentData: storageAPI.loadPersistentData(),
|
||||
tabs: {},
|
||||
windows: {},
|
||||
currentWindowId: -1
|
||||
},
|
||||
action: Actions
|
||||
) {
|
||||
const initialPersistentData: PersistentData = state.persistentData
|
||||
|
||||
switch (action.type) {
|
||||
case webNavigationTypes.ON_COMMITTED: {
|
||||
const tabData = shieldsPanelState.getActiveTabData(state)
|
||||
if (!tabData) {
|
||||
console.error('Active tab not found')
|
||||
break
|
||||
}
|
||||
if (action.isMainFrame) {
|
||||
state = shieldsPanelState.resetBlockingStats(state, action.tabId)
|
||||
state = shieldsPanelState.resetBlockingResources(state, action.tabId)
|
||||
state = noScriptState.resetNoScriptInfo(state, action.tabId, getOrigin(action.url))
|
||||
}
|
||||
applySiteFilters(action.tabId, getHostname(action.url))
|
||||
break
|
||||
}
|
||||
case windowTypes.WINDOW_REMOVED: {
|
||||
state = shieldsPanelState.removeWindowInfo(state, action.windowId)
|
||||
break
|
||||
}
|
||||
case windowTypes.WINDOW_CREATED: {
|
||||
if (action.window.focused || Object.keys(state.windows).length === 0) {
|
||||
state = focusedWindowChanged(state, action.window.id)
|
||||
}
|
||||
break
|
||||
}
|
||||
case windowTypes.WINDOW_FOCUS_CHANGED: {
|
||||
state = focusedWindowChanged(state, action.windowId)
|
||||
break
|
||||
}
|
||||
case tabTypes.ACTIVE_TAB_CHANGED: {
|
||||
const windowId: number = action.windowId
|
||||
const tabId: number = action.tabId
|
||||
state = updateActiveTab(state, windowId, tabId)
|
||||
break
|
||||
}
|
||||
case tabTypes.TAB_DATA_CHANGED: {
|
||||
const tab: chrome.tabs.Tab = action.tab
|
||||
if (tab.active && tab.id) {
|
||||
state = updateActiveTab(state, tab.windowId, tab.id)
|
||||
}
|
||||
break
|
||||
}
|
||||
case tabTypes.TAB_CREATED: {
|
||||
const tab: chrome.tabs.Tab = action.tab
|
||||
if (!tab) {
|
||||
break
|
||||
}
|
||||
|
||||
if (tab.active && tab.id) {
|
||||
state = updateActiveTab(state, tab.windowId, tab.id)
|
||||
}
|
||||
break
|
||||
}
|
||||
case shieldsPanelTypes.SHIELDS_PANEL_DATA_UPDATED: {
|
||||
state = shieldsPanelState.updateTabShieldsData(state, action.details.id, action.details)
|
||||
break
|
||||
}
|
||||
case shieldsPanelTypes.SHIELDS_TOGGLED: {
|
||||
const tabId: number = shieldsPanelState.getActiveTabId(state)
|
||||
const tabData = shieldsPanelState.getActiveTabData(state)
|
||||
if (!tabData) {
|
||||
console.error('Active tab not found')
|
||||
break
|
||||
}
|
||||
setAllowBraveShields(tabData.origin, action.setting)
|
||||
.then(() => {
|
||||
reloadTab(tabId, true).catch((e) => {
|
||||
console.error('Tab reload was not successful', e)
|
||||
})
|
||||
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
|
||||
})
|
||||
.catch((e: any) => {
|
||||
console.error('Could not set shields', e)
|
||||
})
|
||||
state = shieldsPanelState
|
||||
.updateTabShieldsData(state, tabId, { braveShields: action.setting })
|
||||
break
|
||||
}
|
||||
case cosmeticFilterTypes.SITE_COSMETIC_FILTER_REMOVED: {
|
||||
let url = action.origin
|
||||
removeSiteFilter(url)
|
||||
break
|
||||
}
|
||||
case cosmeticFilterTypes.ALL_COSMETIC_FILTERS_REMOVED: {
|
||||
removeAllFilters()
|
||||
break
|
||||
}
|
||||
case cosmeticFilterTypes.SITE_COSMETIC_FILTER_ADDED: {
|
||||
addSiteCosmeticFilter(action.origin, action.cssfilter)
|
||||
.catch((e) => {
|
||||
console.error('Could not add filter:', e)
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!areObjectsEqual(state.persistentData, initialPersistentData)) {
|
||||
storageAPI.savePersistentDataDebounced(state.persistentData)
|
||||
}
|
||||
|
||||
return state
|
||||
}
|
||||
+3
@@ -32,10 +32,12 @@ import {
|
||||
onShieldsPanelShown
|
||||
} from '../api/shieldsAPI'
|
||||
import { reloadTab } from '../api/tabsAPI'
|
||||
import { applySiteFilters } from '../api/cosmeticFilterAPI'
|
||||
|
||||
// Helpers
|
||||
import { getAllowedScriptsOrigins } from '../../helpers/noScriptUtils'
|
||||
import { areObjectsEqual } from '../../helpers/objectUtils'
|
||||
import { getHostname } from '../../helpers/urlUtils'
|
||||
|
||||
export default function shieldsPanelReducer (
|
||||
state: State = {
|
||||
@@ -55,6 +57,7 @@ export default function shieldsPanelReducer (
|
||||
state = shieldsPanelState.resetBlockingResources(state, action.tabId)
|
||||
state = noScriptState.resetNoScriptInfo(state, action.tabId, new window.URL(action.url).origin)
|
||||
}
|
||||
applySiteFilters(action.tabId, getHostname(action.url))
|
||||
break
|
||||
}
|
||||
case windowTypes.WINDOW_REMOVED: {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
export const SITE_COSMETIC_FILTER_REMOVED = 'SITE_COSMETIC_FILTER_REMOVED'
|
||||
export const SITE_COSMETIC_FILTER_ADDED = 'SITE_COSMETIC_FILTER_ADDED'
|
||||
export const LOGGED_STORAGE = 'LOGGED_STORAGE'
|
||||
export const ALL_COSMETIC_FILTERS_REMOVED = 'ALL_COSMETIC_FILTERS_REMOVED'
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
/* 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 * as types from '../constants/cosmeticFilterTypes'
|
||||
|
||||
interface SiteCosmeticFilterAddedReturn {
|
||||
type: types.SITE_COSMETIC_FILTER_ADDED,
|
||||
origin: string,
|
||||
cssfilter: string
|
||||
}
|
||||
|
||||
export interface SiteCosmeticFilterAdded {
|
||||
(origin: string, cssfilter: string): SiteCosmeticFilterAddedReturn
|
||||
}
|
||||
|
||||
interface SiteCosmeticFilterRemovedReturn {
|
||||
type: types.SITE_COSMETIC_FILTER_REMOVED,
|
||||
origin: string
|
||||
}
|
||||
|
||||
export interface SiteCosmeticFilterRemoved {
|
||||
(origin: string): SiteCosmeticFilterRemovedReturn
|
||||
}
|
||||
|
||||
interface AllCosmeticFiltersRemovedReturn {
|
||||
type: types.ALL_COSMETIC_FILTERS_REMOVED
|
||||
}
|
||||
|
||||
export interface AllCosmeticFiltersRemoved {
|
||||
(): AllCosmeticFiltersRemovedReturn
|
||||
}
|
||||
|
||||
export type cosmeticFilterActions =
|
||||
SiteCosmeticFilterRemovedReturn |
|
||||
SiteCosmeticFilterAddedReturn |
|
||||
AllCosmeticFiltersRemovedReturn
|
||||
@@ -2,7 +2,6 @@ import { shieldPanelActions } from './shieldsPanelActions'
|
||||
import { tabActions } from './tabActions'
|
||||
import { webNavigationActions } from './webNavigationActions'
|
||||
import { windowActions } from './windowActions'
|
||||
import { cosmeticFilterActions } from './cosmeticFilterActions'
|
||||
import { runtimeActions } from './runtimeActions'
|
||||
|
||||
export type Actions =
|
||||
@@ -10,5 +9,4 @@ export type Actions =
|
||||
tabActions |
|
||||
webNavigationActions |
|
||||
windowActions |
|
||||
cosmeticFilterActions |
|
||||
runtimeActions
|
||||
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
/* 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 * as types from '../../constants/cosmeticFilterTypes'
|
||||
|
||||
export type SITE_COSMETIC_FILTER_REMOVED = typeof types.SITE_COSMETIC_FILTER_REMOVED
|
||||
export type SITE_COSMETIC_FILTER_ADDED = typeof types.SITE_COSMETIC_FILTER_ADDED
|
||||
export type LOGGED_STORAGE = typeof types.LOGGED_STORAGE
|
||||
export type ALL_COSMETIC_FILTERS_REMOVED = typeof types.ALL_COSMETIC_FILTERS_REMOVED
|
||||
@@ -1,33 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
// Types
|
||||
import * as types from '../../../brave_extension/extension/brave_extension/constants/cosmeticFilterTypes'
|
||||
|
||||
// Actions
|
||||
import * as actions from '../../../brave_extension/extension/brave_extension/actions/cosmeticFilterActions'
|
||||
|
||||
describe('cosmeticFilterActions', () => {
|
||||
it('siteCosmeticFilterAdded action', () => {
|
||||
const origin = 'https://a.com'
|
||||
const cssfilter = '#filter'
|
||||
expect(actions.siteCosmeticFilterAdded(origin, cssfilter)).toEqual({
|
||||
type: types.SITE_COSMETIC_FILTER_ADDED,
|
||||
origin,
|
||||
cssfilter
|
||||
})
|
||||
})
|
||||
it('siteCosmeticFilterRemoved action', () => {
|
||||
const origin = 'https://a.com'
|
||||
expect(actions.siteCosmeticFilterRemoved(origin)).toEqual({
|
||||
type: types.SITE_COSMETIC_FILTER_REMOVED,
|
||||
origin
|
||||
})
|
||||
})
|
||||
it('allCosmeticFiltersRemoved action', () => {
|
||||
expect(actions.allCosmeticFiltersRemoved()).toEqual({
|
||||
type: types.ALL_COSMETIC_FILTERS_REMOVED
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,7 @@
|
||||
* 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 cosmeticFilterActions from '../../../../brave_extension/extension/brave_extension/background/actions/cosmeticFilterActions'
|
||||
import * as cosmeticFilterAPI from '../../../../brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI'
|
||||
import * as cosmeticFilterEvents from '../../../../brave_extension/extension/brave_extension/background/events/cosmeticFilterEvents'
|
||||
|
||||
let lastInputText: string
|
||||
@@ -36,8 +36,8 @@ describe('cosmeticFilterEvents events', () => {
|
||||
beforeEach(() => {
|
||||
contextMenuOnClickedSpy = jest.spyOn(chrome.tabs, 'create')
|
||||
chromeTabsQuerySpy = jest.spyOn(chrome.tabs, 'query')
|
||||
resetSiteFilterSettingsSpy = jest.spyOn(cosmeticFilterActions, 'siteCosmeticFilterRemoved')
|
||||
resetAllFilterSettingsSpy = jest.spyOn(cosmeticFilterActions, 'allCosmeticFiltersRemoved')
|
||||
resetSiteFilterSettingsSpy = jest.spyOn(cosmeticFilterAPI, 'removeSiteFilter')
|
||||
resetAllFilterSettingsSpy = jest.spyOn(cosmeticFilterAPI, 'removeAllFilters')
|
||||
chromeTabsSendMessageSpy = jest.spyOn(chrome.tabs, 'sendMessage')
|
||||
})
|
||||
afterEach(() => {
|
||||
@@ -124,15 +124,17 @@ describe('cosmeticFilterEvents events', () => {
|
||||
describe('when prompting user with selector', function () {
|
||||
describe('when a selector is returned', function () {
|
||||
it('calls window.prompt with selector as input', function () {
|
||||
cosmeticFilterEvents.onSelectorReturned('abc')
|
||||
expect(lastInputText).toBe('CSS selector:')
|
||||
expect(lastPromptText).toBe('abc')
|
||||
return cosmeticFilterEvents.onSelectorReturned('abc').then(() => {
|
||||
expect(lastInputText).toBe('CSS selector:')
|
||||
expect(lastPromptText).toBe('abc')
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('when a selector is not returned', function () {
|
||||
it('calls window.prompt with `not found` message', function () {
|
||||
cosmeticFilterEvents.onSelectorReturned(null)
|
||||
expect(lastInputText.indexOf('We were unable to automatically populat') > -1).toBe(true)
|
||||
return cosmeticFilterEvents.onSelectorReturned(null).then(() => {
|
||||
expect(lastInputText.indexOf('We were unable to automatically populat') > -1).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -146,22 +148,25 @@ describe('cosmeticFilterEvents events', () => {
|
||||
})
|
||||
it('calls `chrome.tabs.insertCSS` when selector is NOT null/undefined', function () {
|
||||
selectorToReturn = '#test_selector'
|
||||
cosmeticFilterEvents.onSelectorReturned(selectorToReturn)
|
||||
let returnObj = {
|
||||
'code': '#test_selector {display: none !important;}',
|
||||
'cssOrigin': 'user'
|
||||
}
|
||||
expect(insertCssSpy).toBeCalledWith(returnObj)
|
||||
return cosmeticFilterEvents.onSelectorReturned(selectorToReturn).then(() => {
|
||||
let returnObj = {
|
||||
'code': '#test_selector {display: none !important;}',
|
||||
'cssOrigin': 'user'
|
||||
}
|
||||
expect(insertCssSpy).toBeCalledWith(returnObj)
|
||||
})
|
||||
})
|
||||
it('does NOT call `chrome.tabs.insertCSS` when selector is undefined', function () {
|
||||
selectorToReturn = undefined
|
||||
cosmeticFilterEvents.onSelectorReturned(undefined)
|
||||
expect(insertCssSpy).not.toBeCalled()
|
||||
return cosmeticFilterEvents.onSelectorReturned(undefined).then(() => {
|
||||
expect(insertCssSpy).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
it('does NOT call `chrome.tabs.insertCSS` when selector is null', function () {
|
||||
selectorToReturn = null
|
||||
cosmeticFilterEvents.onSelectorReturned(null)
|
||||
expect(insertCssSpy).not.toBeCalled()
|
||||
return cosmeticFilterEvents.onSelectorReturned(null).then(() => {
|
||||
expect(insertCssSpy).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,377 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
// Types
|
||||
import * as shieldPanelTypes from '../../../../brave_extension/extension/brave_extension/constants/shieldsPanelTypes'
|
||||
import * as cosmeticFilterTypes from '../../../../brave_extension/extension/brave_extension/constants/cosmeticFilterTypes'
|
||||
import * as windowTypes from '../../../../brave_extension/extension/brave_extension/constants/windowTypes'
|
||||
import * as tabTypes from '../../../../brave_extension/extension/brave_extension/constants/tabTypes'
|
||||
import * as webNavigationTypes from '../../../../brave_extension/extension/brave_extension/constants/webNavigationTypes'
|
||||
import { State } from '../../../../brave_extension/extension/brave_extension/types/state/shieldsPannelState'
|
||||
import { ShieldDetails } from '../../../../brave_extension/extension/brave_extension/types/actions/shieldsPanelActions'
|
||||
|
||||
// APIs
|
||||
import * as shieldsAPI from '../../../../brave_extension/extension/brave_extension/background/api/shieldsAPI'
|
||||
import * as cosmeticFilterAPI from '../../../../brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI'
|
||||
import * as tabsAPI from '../../../../brave_extension/extension/brave_extension/background/api/tabsAPI'
|
||||
|
||||
// Reducers
|
||||
import shieldsPanelReducer from '../../../../brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer'
|
||||
import cosmeticFilterReducer from '../../../../brave_extension/extension/brave_extension/background/reducers/cosmeticFilterReducer'
|
||||
|
||||
// State helpers
|
||||
import * as shieldsPanelState from '../../../../brave_extension/extension/brave_extension/state/shieldsPanelState'
|
||||
import * as noScriptState from '../../../../brave_extension/extension/brave_extension/state/noScriptState'
|
||||
|
||||
// Helpers
|
||||
import { initialState } from '../../../testData'
|
||||
import * as deepFreeze from 'deep-freeze-node'
|
||||
import * as actions from '../../../../brave_extension/extension/brave_extension/actions/shieldsPanelActions'
|
||||
|
||||
const origin = 'https://brave.com'
|
||||
const windowId = 1
|
||||
const tabId = 2
|
||||
|
||||
const details: ShieldDetails = {
|
||||
id: tabId,
|
||||
origin,
|
||||
hostname: 'brave.com',
|
||||
httpUpgradableResources: 'block',
|
||||
javascript: 'block',
|
||||
trackers: 'block',
|
||||
ads: 'block',
|
||||
fingerprinting: 'block',
|
||||
cookies: 'block'
|
||||
}
|
||||
|
||||
const tab: chrome.tabs.Tab = {
|
||||
active: true,
|
||||
id: tabId,
|
||||
windowId,
|
||||
index: 1,
|
||||
pinned: false,
|
||||
highlighted: false,
|
||||
incognito: false,
|
||||
selected: false,
|
||||
discarded: false,
|
||||
autoDiscardable: false
|
||||
}
|
||||
|
||||
const state: State = deepFreeze({
|
||||
persistentData: {
|
||||
isFirstAccess: true
|
||||
},
|
||||
tabs: {
|
||||
2: {
|
||||
...details,
|
||||
adsBlocked: 0,
|
||||
controlsOpen: true,
|
||||
braveShields: 'allow',
|
||||
trackersBlocked: 0,
|
||||
httpsRedirected: 0,
|
||||
javascriptBlocked: 0,
|
||||
fingerprintingBlocked: 0,
|
||||
noScriptInfo: {},
|
||||
adsBlockedResources: [],
|
||||
fingerprintingBlockedResources: [],
|
||||
httpsRedirectedResources: [],
|
||||
trackersBlockedResources: []
|
||||
}
|
||||
},
|
||||
windows: {
|
||||
1: 2
|
||||
},
|
||||
currentWindowId: 1
|
||||
})
|
||||
|
||||
describe('cosmeticFilterReducer', () => {
|
||||
it('should handle initial state', () => {
|
||||
// avoid printing error logs to the test console.
|
||||
// this is expected since state is undefined but we want to avoid polluting the test logs
|
||||
console.error = () => ''
|
||||
expect(shieldsPanelReducer(undefined, actions.allowScriptOriginsOnce()))
|
||||
.toEqual(initialState.cosmeticFilter)
|
||||
})
|
||||
describe('ON_COMMITTED', () => {
|
||||
const tabId = 1
|
||||
let spy: jest.SpyInstance
|
||||
let resetNoScriptInfoSpy: jest.SpyInstance
|
||||
let resetBlockingResourcesSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
spy = jest.spyOn(shieldsPanelState, 'resetBlockingStats')
|
||||
resetNoScriptInfoSpy = jest.spyOn(noScriptState, 'resetNoScriptInfo')
|
||||
resetBlockingResourcesSpy = jest.spyOn(shieldsPanelState, 'resetBlockingResources')
|
||||
})
|
||||
afterEach(() => {
|
||||
spy.mockRestore()
|
||||
resetNoScriptInfoSpy.mockRestore()
|
||||
resetBlockingResourcesSpy.mockRestore()
|
||||
})
|
||||
it('calls resetBlockingStats when isMainFrame is true', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: true
|
||||
})
|
||||
expect(spy).toBeCalledTimes(1)
|
||||
expect(spy.mock.calls[0][1]).toBe(tabId)
|
||||
})
|
||||
it('does not call resetBlockingStats when isMainFrame is false', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: false
|
||||
})
|
||||
expect(spy).not.toBeCalled()
|
||||
})
|
||||
it('calls resetNoScriptInfo when isMainFrame is true', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: true
|
||||
})
|
||||
expect(resetNoScriptInfoSpy).toBeCalledTimes(1)
|
||||
expect(resetNoScriptInfoSpy.mock.calls[0][1]).toBe(tabId)
|
||||
expect(resetNoScriptInfoSpy.mock.calls[0][2]).toBe('https://www.brave.com')
|
||||
})
|
||||
it('does not call resetNoScriptInfo when isMainFrame is false', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: false
|
||||
})
|
||||
expect(resetNoScriptInfoSpy).not.toBeCalled()
|
||||
})
|
||||
it('calls resetBlockingResources when isMainFrame is true', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: true
|
||||
})
|
||||
expect(spy).toBeCalledTimes(1)
|
||||
expect(spy.mock.calls[0][1]).toBe(tabId)
|
||||
})
|
||||
it('does not call resetBlockingResources when isMainFrame is false', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: webNavigationTypes.ON_COMMITTED,
|
||||
tabId: tabId,
|
||||
url: 'https://www.brave.com',
|
||||
isMainFrame: false
|
||||
})
|
||||
expect(spy).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
describe('WINDOW_REMOVED', () => {
|
||||
const windowId = 1
|
||||
let spy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
spy = jest.spyOn(shieldsPanelState, 'removeWindowInfo')
|
||||
})
|
||||
afterEach(() => {
|
||||
spy.mockRestore()
|
||||
})
|
||||
it('calls shieldsPanelState.removeWindowInfo', () => {
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: windowTypes.WINDOW_REMOVED,
|
||||
windowId
|
||||
})
|
||||
expect(spy).toBeCalledTimes(1)
|
||||
expect(spy.mock.calls[0][1]).toBe(windowId)
|
||||
})
|
||||
})
|
||||
describe('WINDOW_FOCUS_CHANGED', () => {
|
||||
const windowId = 1
|
||||
const tabId = 2
|
||||
let updateFocusedWindowSpy: jest.SpyInstance
|
||||
let requestShieldPanelDataSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
updateFocusedWindowSpy = jest.spyOn(shieldsPanelState, 'updateFocusedWindow')
|
||||
requestShieldPanelDataSpy = jest.spyOn(shieldsAPI, 'requestShieldPanelData')
|
||||
const state = deepFreeze({
|
||||
...initialState.shieldsPanel,
|
||||
windows: {
|
||||
1: tabId
|
||||
},
|
||||
tabs: {
|
||||
[tabId]: { url: 'https://brave.com' }
|
||||
}
|
||||
})
|
||||
shieldsPanelReducer(state, {
|
||||
type: windowTypes.WINDOW_FOCUS_CHANGED,
|
||||
windowId: windowId
|
||||
})
|
||||
})
|
||||
afterEach(() => {
|
||||
updateFocusedWindowSpy.mockRestore()
|
||||
requestShieldPanelDataSpy.mockRestore()
|
||||
})
|
||||
it('calls shieldsPanelState.updateFocusedWindow', () => {
|
||||
expect(updateFocusedWindowSpy).toBeCalledTimes(1)
|
||||
expect(updateFocusedWindowSpy.mock.calls[0][1]).toBe(windowId)
|
||||
})
|
||||
it('calls shieldsPanelState.requestShieldPanelDataSpy ', () => {
|
||||
expect(requestShieldPanelDataSpy).toBeCalledWith(tabId)
|
||||
})
|
||||
})
|
||||
describe('TAB_DATA_CHANGED', () => {
|
||||
const windowId = 1
|
||||
const tabId = 2
|
||||
const state = deepFreeze({ ...initialState.shieldsPanel, windows: { 1: tabId }, tabs: {} })
|
||||
let updateActiveTabSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
updateActiveTabSpy = jest.spyOn(shieldsPanelState, 'updateActiveTab')
|
||||
})
|
||||
afterEach(() => {
|
||||
updateActiveTabSpy.mockRestore()
|
||||
})
|
||||
it('calls shieldsPanelState.updateActiveTab when the tab is active', () => {
|
||||
shieldsPanelReducer(state, {
|
||||
type: tabTypes.TAB_DATA_CHANGED,
|
||||
tabId: tabId,
|
||||
tab,
|
||||
changeInfo: {}
|
||||
})
|
||||
expect(updateActiveTabSpy).toBeCalledTimes(1)
|
||||
expect(updateActiveTabSpy.mock.calls[0][1]).toBe(windowId)
|
||||
expect(updateActiveTabSpy.mock.calls[0][2]).toBe(tabId)
|
||||
})
|
||||
it('does not call shieldsPanelState.updateActiveTab when the tab is not active', () => {
|
||||
shieldsPanelReducer(state, {
|
||||
type: tabTypes.TAB_DATA_CHANGED,
|
||||
tabId: tabId,
|
||||
tab: {
|
||||
...tab,
|
||||
active: false
|
||||
},
|
||||
changeInfo: {}
|
||||
})
|
||||
expect(updateActiveTabSpy).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
describe('TAB_CREATED', () => {
|
||||
const state = {
|
||||
...initialState.shieldsPanel,
|
||||
windows: {
|
||||
1: tabId
|
||||
},
|
||||
tabs: {}
|
||||
}
|
||||
let updateActiveTabSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
updateActiveTabSpy = jest.spyOn(shieldsPanelState, 'updateActiveTab')
|
||||
})
|
||||
afterEach(() => {
|
||||
updateActiveTabSpy.mockRestore()
|
||||
})
|
||||
it('calls shieldsPanelState.updateActiveTab when the tab is active', () => {
|
||||
shieldsPanelReducer(state, {
|
||||
type: tabTypes.TAB_CREATED,
|
||||
tab: {
|
||||
...tab,
|
||||
active: true
|
||||
}
|
||||
})
|
||||
expect(updateActiveTabSpy).toBeCalledTimes(1)
|
||||
expect(updateActiveTabSpy.mock.calls[0][1]).toBe(windowId)
|
||||
expect(updateActiveTabSpy.mock.calls[0][2]).toBe(tabId)
|
||||
})
|
||||
it('does not call shieldsPanelState.updateActiveTab when the tab is not active', () => {
|
||||
shieldsPanelReducer(state, {
|
||||
type: tabTypes.TAB_CREATED,
|
||||
tab: {
|
||||
...tab,
|
||||
active: false
|
||||
}
|
||||
})
|
||||
expect(updateActiveTabSpy).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
describe('SHIELDS_PANEL_DATA_UPDATED', () => {
|
||||
it('updates state detail', () => {
|
||||
expect(
|
||||
shieldsPanelReducer(initialState.shieldsPanel, {
|
||||
type: shieldPanelTypes.SHIELDS_PANEL_DATA_UPDATED,
|
||||
details
|
||||
})).toEqual({
|
||||
...state,
|
||||
currentWindowId: -1,
|
||||
windows: {}
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('SHIELDS_TOGGLED', () => {
|
||||
let reloadTabSpy: jest.SpyInstance
|
||||
let setAllowBraveShieldsSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
reloadTabSpy = jest.spyOn(tabsAPI, 'reloadTab')
|
||||
setAllowBraveShieldsSpy = jest.spyOn(shieldsAPI, 'setAllowBraveShields')
|
||||
})
|
||||
afterEach(() => {
|
||||
reloadTabSpy.mockRestore()
|
||||
setAllowBraveShieldsSpy.mockRestore()
|
||||
})
|
||||
it('should call setAllowBraveShields', () => {
|
||||
expect(
|
||||
shieldsPanelReducer(state, {
|
||||
type: shieldPanelTypes.SHIELDS_TOGGLED,
|
||||
setting: 'allow'
|
||||
})).toEqual(state)
|
||||
expect(setAllowBraveShieldsSpy).toBeCalledWith(origin, 'allow')
|
||||
})
|
||||
})
|
||||
describe('SITE_COSMETIC_FILTER_REMOVED', () => {
|
||||
let removeSiteFilterSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
removeSiteFilterSpy = jest.spyOn(cosmeticFilterAPI, 'removeSiteFilter')
|
||||
})
|
||||
afterEach(() => {
|
||||
removeSiteFilterSpy.mockRestore()
|
||||
})
|
||||
it('should call removeSiteFilter', () => {
|
||||
expect(
|
||||
cosmeticFilterReducer(state, {
|
||||
type: cosmeticFilterTypes.SITE_COSMETIC_FILTER_REMOVED,
|
||||
origin
|
||||
})).toEqual(state)
|
||||
})
|
||||
})
|
||||
describe('ALL_COSMETIC_FILTERS_REMOVED', () => {
|
||||
let removeSiteFilterSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
removeSiteFilterSpy = jest.spyOn(cosmeticFilterAPI, 'removeAllFilters')
|
||||
})
|
||||
afterEach(() => {
|
||||
removeSiteFilterSpy.mockRestore()
|
||||
})
|
||||
it('should call removeAllFilters', () => {
|
||||
expect(
|
||||
cosmeticFilterReducer(state, {
|
||||
type: cosmeticFilterTypes.ALL_COSMETIC_FILTERS_REMOVED
|
||||
})).toEqual(state)
|
||||
})
|
||||
})
|
||||
describe('SITE_COSMETIC_FILTER_ADDED', () => {
|
||||
let removeSiteFilterSpy: jest.SpyInstance
|
||||
beforeEach(() => {
|
||||
removeSiteFilterSpy = jest.spyOn(cosmeticFilterAPI, 'addSiteCosmeticFilter')
|
||||
})
|
||||
afterEach(() => {
|
||||
removeSiteFilterSpy.mockRestore()
|
||||
})
|
||||
it('should call addSiteCosmeticFilter', () => {
|
||||
const cssfilter = '#test'
|
||||
expect(
|
||||
cosmeticFilterReducer(state, {
|
||||
type: cosmeticFilterTypes.SITE_COSMETIC_FILTER_ADDED,
|
||||
origin,
|
||||
cssfilter
|
||||
})).toEqual(state)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -332,12 +332,6 @@ export const window = () => {
|
||||
}
|
||||
|
||||
export const initialState = deepFreeze({
|
||||
cosmeticFilter: {
|
||||
currentWindowId: -1,
|
||||
tabs: {},
|
||||
windows: {},
|
||||
persistentData: { isFirstAccess: true }
|
||||
},
|
||||
dappDetection: {},
|
||||
runtime: {},
|
||||
shieldsPanel: {
|
||||
|
||||
Reference in New Issue
Block a user