From d645afe6aa64187173e3d231bac2f6518ee092ce Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Sun, 26 Mar 2017 17:52:43 +0200 Subject: [PATCH 1/2] Adds fingerprint and scripts to the lion badge count Resolves #7886 Auditors: @bsclifton Test Plan: - Visit icloud.com with fingerprint enabled - badge count should be 1 --- js/components/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/components/main.js b/js/components/main.js index 6a59c175c9..8fc16ad8c1 100644 --- a/js/components/main.js +++ b/js/components/main.js @@ -893,7 +893,9 @@ class Main extends ImmutableComponent { getTotalBlocks (frames) { const ads = frames.getIn(['adblock', 'blocked']) const trackers = frames.getIn(['trackingProtection', 'blocked']) - const blocked = (ads ? ads.size : 0) + (trackers ? trackers.size : 0) + const scripts = frames.getIn(['noScript', 'blocked']) + const fingerprint = frames.getIn(['fingerprintingProtection', 'blocked']) + const blocked = (ads ? ads.size : 0) + (trackers ? trackers.size : 0) + (scripts ? scripts.size : 0) + (fingerprint ? fingerprint.size : 0) return (blocked.size === 0) ? false : ((blocked > 99) ? '99+' : blocked) } From 6092250c2b97f9a41cae9196f6952ad353803b37 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Sun, 26 Mar 2017 17:55:50 -0700 Subject: [PATCH 2/2] Added error handling logic and unit tests for the new getTotalBlocks method Auditors: @NejcZdovc --- js/components/main.js | 21 ++++++-- test/unit/js/components/mainTest.js | 75 +++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/js/components/main.js b/js/components/main.js index 8fc16ad8c1..44f475f46f 100644 --- a/js/components/main.js +++ b/js/components/main.js @@ -46,6 +46,7 @@ const LongPressButton = require('./longPressButton') const Menubar = require('../../app/renderer/components/menubar') const WindowCaptionButtons = require('../../app/renderer/components/windowCaptionButtons') const CheckDefaultBrowserDialog = require('../../app/renderer/components/checkDefaultBrowserDialog') + // Constants const appConfig = require('../constants/appConfig') const messages = require('../constants/messages') @@ -76,6 +77,7 @@ const debounce = require('../lib/debounce') const {currentWindow, isMaximized, isFocused, isFullScreen} = require('../../app/renderer/currentWindow') const emptyMap = new Immutable.Map() const emptyList = new Immutable.List() +const {makeImmutable} = require('../../app/common/state/immutableUtil') class Main extends ImmutableComponent { constructor () { @@ -891,13 +893,26 @@ class Main extends ImmutableComponent { } getTotalBlocks (frames) { + if (!frames) { + return false + } + + frames = makeImmutable(frames) + const ads = frames.getIn(['adblock', 'blocked']) const trackers = frames.getIn(['trackingProtection', 'blocked']) const scripts = frames.getIn(['noScript', 'blocked']) const fingerprint = frames.getIn(['fingerprintingProtection', 'blocked']) - const blocked = (ads ? ads.size : 0) + (trackers ? trackers.size : 0) + (scripts ? scripts.size : 0) + (fingerprint ? fingerprint.size : 0) - - return (blocked.size === 0) ? false : ((blocked > 99) ? '99+' : blocked) + const blocked = (ads && ads.size ? ads.size : 0) + + (trackers && trackers.size ? trackers.size : 0) + + (scripts && scripts.size ? scripts.size : 0) + + (fingerprint && fingerprint.size ? fingerprint.size : 0) + + return (blocked === 0) + ? false + : ((blocked > 99) + ? '99+' + : blocked) } render () { diff --git a/test/unit/js/components/mainTest.js b/test/unit/js/components/mainTest.js index f3692aa543..a2e44dbbc2 100644 --- a/test/unit/js/components/mainTest.js +++ b/test/unit/js/components/mainTest.js @@ -123,4 +123,79 @@ describe('Main component unit tests', function () { assert.equal(node.props.disabled, true) }) }) + + describe('getTotalBlocks', function () { + let instance + + before(function () { + let wrapper = shallow( +
+ ) + instance = wrapper.instance() + }) + + it('returns false if there are no units blocked', function () { + const frames = Immutable.fromJS({ + adblock: { blocked: [] }, + trackingProtection: { blocked: [] }, + noScript: { blocked: [] }, + fingerprintingProtection: { blocked: [] } + }) + const result = instance.getTotalBlocks(frames) + assert.equal(result, false) + }) + + it('returns total of items (ads / trackers / scripts / fingerprints) blocked', function () { + const frames = Immutable.fromJS({ + adblock: { blocked: [1] }, + trackingProtection: { blocked: [1, 2] }, + noScript: { blocked: [1, 2, 3, 4] }, + fingerprintingProtection: { blocked: [1, 2, 3, 4, 5, 6, 7, 8] } + }) + const result = instance.getTotalBlocks(frames) + assert.equal(result, 15) + }) + + it('defaults values to 0 if element is not a list or is not present', function () { + const frames = Immutable.fromJS({ + adblock: { blocked: 'not a list' }, + trackingProtection: {}, + noScript: { blocked: [1] }, + fingerprintingProtection: { blocked: {} } + }) + const result = instance.getTotalBlocks(frames) + assert.equal(result, 1) + }) + + it('returns false if the input is falsey', function () { + assert.equal(instance.getTotalBlocks(), false) + assert.equal(instance.getTotalBlocks(undefined), false) + assert.equal(instance.getTotalBlocks(null), false) + assert.equal(instance.getTotalBlocks(false), false) + }) + + it('converts the input to an immutable object', function () { + const mutableFrames = { + adblock: { blocked: [1] }, + trackingProtection: { blocked: [1, 2] }, + noScript: { blocked: [1, 2, 3, 4] }, + fingerprintingProtection: { blocked: [1, 2, 3, 4, 5, 6, 7, 8] } + } + const result = instance.getTotalBlocks(mutableFrames) + assert.equal(result, 15) + }) + + it('returns "99+" if tracker count is > 99', function () { + const mutableFrames = { + adblock: { blocked: [] } + } + + for (let i = 1; i < 101; i++) { + mutableFrames.adblock.blocked.push(i) + } + + const result = instance.getTotalBlocks(mutableFrames) + assert.equal(result, '99+') + }) + }) })