From a2b6f9b646f5dd9edd9cd82dfc010404e3b3789f Mon Sep 17 00:00:00 2001 From: Cezar Augusto Date: Fri, 24 May 2019 14:27:00 -0300 Subject: [PATCH] @delay incrementing shields counter until first render only update resources blocked after shields is active for the site fix https://github.com/brave/brave-browser/issues/888 --- .../reducers/shieldsPanelReducer.ts | 5 ++- .../state/shieldsPanelState.ts | 7 ++++ .../state/shieldsPanelState_test.ts | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts b/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts index 4e851ccd9f42..317065cb5b45 100644 --- a/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts +++ b/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts @@ -143,7 +143,10 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows: state = shieldsPanelState.updateResourceBlocked( state, tabId, action.details.blockType, action.details.subresource) if (tabId === currentTabId) { - shieldsPanelState.updateShieldsIconBadgeText(state) + const isShieldsActive: boolean = shieldsPanelState.isShieldsActive(state, tabId) + if (isShieldsActive) { + shieldsPanelState.updateShieldsIconBadgeText(state) + } } break } diff --git a/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts b/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts index ae3f60a5b3a3..1186cc642247 100644 --- a/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts +++ b/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts @@ -12,6 +12,13 @@ export const getActiveTabId: shieldState.GetActiveTabId = (state) => state.windo export const getActiveTabData: shieldState.GetActiveTabData = (state) => state.tabs[getActiveTabId(state)] +export const isShieldsActive = (state: shieldState.State, tabId: number): boolean => { + if (!state.tabs[tabId]) { + return false + } + return state.tabs[tabId].braveShields !== 'block' +} + export const updateActiveTab: shieldState.UpdateActiveTab = (state, windowId, tabId) => { let windows: shieldState.Windows = { ...state.windows } || {} windows[windowId] = tabId diff --git a/components/test/brave_extension/state/shieldsPanelState_test.ts b/components/test/brave_extension/state/shieldsPanelState_test.ts index 4a4631938ea9..6a1c44312cf4 100644 --- a/components/test/brave_extension/state/shieldsPanelState_test.ts +++ b/components/test/brave_extension/state/shieldsPanelState_test.ts @@ -51,6 +51,40 @@ describe('shieldsPanelState test', () => { }) }) }) + describe('isShieldsActive', () => { + it('returns false if tab id can not be found', () => { + const assertion = shieldsPanelState.isShieldsActive(state, 123123123123) + expect(assertion).toBe(false) + }) + it('returns false if braveShields is set to `block`', () => { + const newState: State = deepFreeze({ + ...state, + tabs: { + ...state.tabs, + 2: { + ...state.tabs[2], + braveShields: 'block' + } + } + }) + const assertion = shieldsPanelState.isShieldsActive(newState, 2) + expect(assertion).toBe(false) + }) + it('returns true if braveShields is not set to block', () => { + const newState: State = deepFreeze({ + ...state, + tabs: { + ...state.tabs, + 2: { + ...state.tabs[2], + braveShields: 'allow' + } + } + }) + const assertion = shieldsPanelState.isShieldsActive(newState, 2) + expect(assertion).toBe(true) + }) + }) describe('updateActiveTab', () => { it('can update focused window', () => { expect(shieldsPanelState.updateActiveTab(state, 1, 4)).toEqual({