From 6589e9aa2979869a31f1b5ea43e407efa41fe16e Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 16 Jun 2017 13:55:50 +0200 Subject: [PATCH] Converts UpdateBar into redux component Resolves #9450 Auditors: @bsclifton @bridiver Test Plan: --- app/common/lib/updateUtil.js | 44 +++++++++ app/renderer/components/main/main.js | 9 +- app/renderer/components/main/updateBar.js | 68 +++++++------ test/unit/app/common/lib/updateUtilTest.js | 108 +++++++++++++++++++++ 4 files changed, 195 insertions(+), 34 deletions(-) create mode 100644 app/common/lib/updateUtil.js create mode 100644 test/unit/app/common/lib/updateUtilTest.js diff --git a/app/common/lib/updateUtil.js b/app/common/lib/updateUtil.js new file mode 100644 index 00000000000..f4f86d18cb0 --- /dev/null +++ b/app/common/lib/updateUtil.js @@ -0,0 +1,44 @@ +/* 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/. */ + +const Immutable = require('immutable') +const updateStatus = require('../../../js/constants/updateStatus') + +const isUpdateVisible = (state) => { + const updates = state.get('updates', Immutable.Map()) + + const isVerbose = updates.get('verbose', false) + const status = updates.get('status') + + // When verbose is not set we only want to show update available + // prompts, because otherwise the check is a background check and + // the user shouldn't be bothered. + return !( + !status || + ( + !isVerbose && + status !== updateStatus.UPDATE_AVAILABLE + ) || + status === updateStatus.UPDATE_NONE || + status === updateStatus.UPDATE_APPLYING_RESTART || + status === updateStatus.UPDATE_APPLYING_NO_RESTART + ) +} + +const getUpdateStatus = (state) => { + let status = state.getIn(['updates', 'status']) + + // The only difference between the deferred and non deferred variant is that + // the deferred allows hiding. Otherwise you couldn't hide available prompts. + if (status === updateStatus.UPDATE_AVAILABLE_DEFERRED) { + status = updateStatus.UPDATE_AVAILABLE + } + + return status +} + +module.exports = { + isUpdateVisible, + getUpdateStatus +} diff --git a/app/renderer/components/main/main.js b/app/renderer/components/main/main.js index fe99991f648..5b5b3555968 100644 --- a/app/renderer/components/main/main.js +++ b/app/renderer/components/main/main.js @@ -66,6 +66,7 @@ const cx = require('../../../../js/lib/classSet') const eventUtil = require('../../../../js/lib/eventUtil') const siteSettings = require('../../../../js/state/siteSettings') const debounce = require('../../../../js/lib/debounce') +const updateUtil = require('../../../common/lib/updateUtil') const {isSourceAboutUrl} = require('../../../../js/lib/appUrlUtil') const {getCurrentWindowId, isMaximized, isFocused, isFullScreen} = require('../../currentWindow') const {isDarwin, isWindows} = require('../../../common/lib/platformUtil') @@ -688,6 +689,7 @@ class Main extends ImmutableComponent { const customTitlebar = this.customTitlebar const contextMenuDetail = this.props.windowState.get('contextMenuDetail') const shouldAllowWindowDrag = windowState.shouldAllowWindowDrag(this.props.appState, this.props.windowState, activeFrame, isFocused()) + const updateIsVisible = updateUtil.isUpdateVisible(this.props.appState) const appStateSites = this.props.appState.get('sites') @@ -808,8 +810,11 @@ class Main extends ImmutableComponent { onHide={this.onHideCheckDefaultBrowserDialog} /> : null } - - + { + updateIsVisible + ? + : null + } { showBookmarksToolbar ? { - this.props.metadata && this.props.metadata.get('notes') + this.props.notes ? { - updateStatus === UpdateStatus.UPDATE_AVAILABLE ? : null + this.props.isAvailable + ? + : null } { - updateStatus === UpdateStatus.UPDATE_CHECKING ? : null + this.props.isChecking + ? + : null } { - updateStatus === UpdateStatus.UPDATE_DOWNLOADING ? : null + this.props.isDownloading + ? + : null } { - updateStatus === UpdateStatus.UPDATE_NOT_AVAILABLE ? : null + + this.props.isNotAvailable + ? + : null } { - updateStatus === UpdateStatus.UPDATE_ERROR ? : null + this.props.isError + ? + : null } } } +module.exports = ReduxComponent.connect(UpdateBar) + const styles = StyleSheet.create({ flexJustifyBetween: { display: 'flex', @@ -242,5 +248,3 @@ const updateBarStyle = css( commonStyles.notificationBar__notificationItem, commonStyles.notificationBar__greetingStyle ) - -module.exports = UpdateBar diff --git a/test/unit/app/common/lib/updateUtilTest.js b/test/unit/app/common/lib/updateUtilTest.js new file mode 100644 index 00000000000..b43a200d5d1 --- /dev/null +++ b/test/unit/app/common/lib/updateUtilTest.js @@ -0,0 +1,108 @@ +/* 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/. */ + +/* global describe, it */ +const assert = require('assert') +const Immutable = require('immutable') + +const updateUtil = require('../../../../../app/common/lib/updateUtil') +const updateStatus = require('../../../../../js/constants/updateStatus') +require('../../../braveUnit') + +describe('updateUtil test', function () { + describe('getUpdateStatus', function () { + it('update is not available', function () { + const result = updateUtil.getUpdateStatus(Immutable.Map()) + assert.equal(result, null) + }) + + it('update status is UPDATE_AVAILABLE_DEFERRED', function () { + const result = updateUtil.getUpdateStatus(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_AVAILABLE_DEFERRED + } + })) + assert.equal(result, updateStatus.UPDATE_AVAILABLE) + }) + + it('update status is returned normally', function () { + const result = updateUtil.getUpdateStatus(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_CHECKING + } + })) + assert.equal(result, updateStatus.UPDATE_CHECKING) + }) + }) + + describe('isUpdateVisible', function () { + it('update is not available', function () { + const result = updateUtil.isUpdateVisible(Immutable.Map()) + assert.equal(result, false) + }) + + it('status is not available', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: {} + })) + assert.equal(result, false) + }) + + it('update is not verbose and status is different then UPDATE_AVAILABLE', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + verbose: false, + status: updateStatus.UPDATE_CHECKING + } + })) + assert.equal(result, false) + }) + + it('update is verbose and status is UPDATE_AVAILABLE', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + verbose: true, + status: updateStatus.UPDATE_AVAILABLE + } + })) + assert.equal(result, true) + }) + + it('update status is UPDATE_AVAILABLE', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_AVAILABLE + } + })) + assert.equal(result, true) + }) + + it('update status is UPDATE_NONE', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_NONE + } + })) + assert.equal(result, false) + }) + + it('update status is UPDATE_APPLYING_RESTART', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_APPLYING_RESTART + } + })) + assert.equal(result, false) + }) + + it('update status is UPDATE_APPLYING_NO_RESTART', function () { + const result = updateUtil.isUpdateVisible(Immutable.fromJS({ + updates: { + status: updateStatus.UPDATE_APPLYING_NO_RESTART + } + })) + assert.equal(result, false) + }) + }) +})