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

Commit

Permalink
Converts UpdateBar into redux component
Browse files Browse the repository at this point in the history
Resolves #9450

Auditors: @bsclifton @bridiver

Test Plan:
  • Loading branch information
NejcZdovc committed Jun 19, 2017
1 parent fab48e2 commit d3a6796
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 34 deletions.
44 changes: 44 additions & 0 deletions app/common/lib/updateUtil.js
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 7 additions & 2 deletions app/renderer/components/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -679,6 +680,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')

Expand Down Expand Up @@ -794,8 +796,11 @@ class Main extends ImmutableComponent {
onHide={this.onHideCheckDefaultBrowserDialog} />
: null
}

<UpdateBar updates={this.props.appState.get('updates')} />
{
updateIsVisible
? <UpdateBar />
: null
}
{
showBookmarksToolbar
? <BookmarksToolbar
Expand Down
68 changes: 36 additions & 32 deletions app/renderer/components/main/updateBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {StyleSheet, css} = require('aphrodite/no-important')

// Components
const ImmutableComponent = require('../immutableComponent')
const ReduxComponent = require('../reduxComponent')
const BrowserButton = require('../common/browserButton')

// Actions
Expand All @@ -18,6 +19,7 @@ const UpdateStatus = require('../../../../js/constants/updateStatus')

// Utils
const cx = require('../../../../js/lib/classSet')
const updateUtil = require('../../../common/lib/updateUtil')

// Styles
const commonStyles = require('../styles/commonStyles')
Expand Down Expand Up @@ -79,6 +81,7 @@ class UpdateLog extends ImmutableComponent {
onViewLog () {
appActions.updateLogOpened()
}

render () {
return <BrowserButton groupedItem notificationItem secondaryColor
testId='updateViewLogButton'
Expand All @@ -97,7 +100,7 @@ class UpdateAvailable extends ImmutableComponent {
</div>
<span className={css(styles.flexAlignCenter)} data-test-id='notificationOptions'>
{
this.props.metadata && this.props.metadata.get('notes')
this.props.notes
? <BrowserButton groupedItem notificationItem secondaryColor
testId='updateDetails'
l10nId='updateDetails'
Expand Down Expand Up @@ -176,55 +179,58 @@ class UpdateNotAvailable extends ImmutableComponent {
}
}

class UpdateBar extends ImmutableComponent {
render () {
if (!this.props.updates) {
return null
}

// 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.
const verbose = this.props.updates.get('verbose')
let updateStatus = this.props.updates.get('status')
if (!updateStatus ||
(!verbose && updateStatus !== UpdateStatus.UPDATE_AVAILABLE) ||
updateStatus === UpdateStatus.UPDATE_NONE ||
updateStatus === UpdateStatus.UPDATE_APPLYING_RESTART ||
updateStatus === UpdateStatus.UPDATE_APPLYING_NO_RESTART) {
return null
}

// The only difference between the deferred and non deferred variant is that
// the deferred allows hiding. Otherwise you couldn't hide available prompts.
if (updateStatus === UpdateStatus.UPDATE_AVAILABLE_DEFERRED) {
updateStatus = UpdateStatus.UPDATE_AVAILABLE
}
class UpdateBar extends React.Component {
mergeProps (state, ownProps) {
const props = {}
props.updateStatus = updateUtil.getUpdateStatus(state)
props.notes = state.getIn(['updates', 'metadata', 'notes'])
props.isAvailable = props.updateStatus === UpdateStatus.UPDATE_AVAILABLE
props.isChecking = props.updateStatus === UpdateStatus.UPDATE_CHECKING
props.isDownloading = props.updateStatus === UpdateStatus.UPDATE_DOWNLOADING
props.isNotAvailable = props.updateStatus === UpdateStatus.UPDATE_NOT_AVAILABLE
props.isError = props.updateStatus === UpdateStatus.UPDATE_ERROR

return props
}

render () {
// 'notificationItem' for styling with notificationBar.less
return <div className={cx({
[updateBarStyle]: true,
notificationItem: true
})} data-test-id='updateBar'>
{
updateStatus === UpdateStatus.UPDATE_AVAILABLE ? <UpdateAvailable metadata={this.props.updates.get('metadata')} updateStatus={updateStatus} /> : null
this.props.isAvailable
? <UpdateAvailable notes={this.props.notes} updateStatus={this.props.updateStatus} />
: null
}
{
updateStatus === UpdateStatus.UPDATE_CHECKING ? <UpdateChecking updateStatus={updateStatus} /> : null
this.props.isChecking
? <UpdateChecking updateStatus={this.props.updateStatus} />
: null
}
{
updateStatus === UpdateStatus.UPDATE_DOWNLOADING ? <UpdateDownloading updateStatus={updateStatus} /> : null
this.props.isDownloading
? <UpdateDownloading updateStatus={this.props.updateStatus} />
: null
}
{
updateStatus === UpdateStatus.UPDATE_NOT_AVAILABLE ? <UpdateNotAvailable updateStatus={updateStatus} /> : null

this.props.isNotAvailable
? <UpdateNotAvailable updateStatus={this.props.updateStatus} />
: null
}
{
updateStatus === UpdateStatus.UPDATE_ERROR ? <UpdateError updateStatus={updateStatus} /> : null
this.props.isError
? <UpdateError updateStatus={this.props.updateStatus} />
: null
}
</div>
}
}

module.exports = ReduxComponent.connect(UpdateBar)

const styles = StyleSheet.create({
flexJustifyBetween: {
display: 'flex',
Expand All @@ -242,5 +248,3 @@ const updateBarStyle = css(
commonStyles.notificationBar__notificationItem,
commonStyles.notificationBar__greetingStyle
)

module.exports = UpdateBar
108 changes: 108 additions & 0 deletions test/unit/app/common/lib/updateUtilTest.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit d3a6796

Please sign in to comment.