diff --git a/lib/gui/app.js b/lib/gui/app.js index cf8aa977..a446502c 100644 --- a/lib/gui/app.js +++ b/lib/gui/app.js @@ -165,34 +165,6 @@ app.controller('AppController', function( if (_.isEmpty(drives)) { DriveSelectorService.close(); } - - // Notice we only autoselect the drive if there is an image, - // which means that the first step was completed successfully, - // otherwise the drive is selected while the drive step is disabled - // which looks very weird. - if (drives.length === 1 && self.selection.hasImage()) { - const drive = _.first(drives); - - if (!self.selection.isDriveValid(drive)) { - return; - } - - // Do not autoselect the same drive over and over again - // and fill the logs unnecessary. - // `angular.equals` is used instead of `_.isEqual` to - // cope with `$$hashKey`. - if (!angular.equals(self.selection.getDrive(), drive)) { - - if (self.selection.isDriveLargeEnough(drive)) { - self.selectDrive(drive); - - AnalyticsService.logEvent('Auto-select drive', { - device: drive.device - }); - } - } - - } }); this.selectImage = function(image) { diff --git a/lib/gui/models/store.js b/lib/gui/models/store.js index 035668f8..3f7f5bdd 100644 --- a/lib/gui/models/store.js +++ b/lib/gui/models/store.js @@ -53,6 +53,25 @@ const store = function(state, action) { } const newState = state.set('availableDrives', Immutable.fromJS(action.data)); + + // Notice we only autoselect the drive if there is an image, + // which means that the first step was completed successfully, + // otherwise the drive is selected while the drive step is disabled + // which looks very weird. + if (action.data.length === 1 && state.hasIn([ 'selection', 'image' ])) { + + const drive = _.first(action.data); + + // TODO: Reuse from SelectionStateModel.isDriveValid() + if (state.getIn([ 'selection', 'image', 'size' ], 0) <= drive.size && !drive.protected) { + return store(newState, { + type: 'SELECT_DRIVE', + data: drive + }); + } + + } + const selectedDevice = state.getIn([ 'selection', 'drive', 'device' ]); if (selectedDevice && !_.find(action.data, { diff --git a/tests/gui/models/drives.spec.js b/tests/gui/models/drives.spec.js index 16c6f013..748806cc 100644 --- a/tests/gui/models/drives.spec.js +++ b/tests/gui/models/drives.spec.js @@ -78,6 +78,131 @@ describe('Browser: DrivesModel', function() { m.chai.expect(DrivesModel.getDrives()).to.deep.equal(drives); }); + describe('given no selected image and no selected drive', function() { + + beforeEach(function() { + SelectionStateModel.removeDrive(); + SelectionStateModel.removeImage(); + }); + + it('should not auto-select a single valid available drive', function() { + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + + DrivesModel.setDrives([ + { + device: '/dev/sdb', + name: 'Foo', + size: 999999999, + mountpoint: '/mnt/foo', + system: false, + protected: false + } + ]); + + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + }); + + }); + + describe('given a selected image and no selected drive', function() { + + beforeEach(function() { + SelectionStateModel.removeDrive(); + SelectionStateModel.setImage({ + path: 'foo.img', + size: 999999999 + }); + }); + + afterEach(function() { + SelectionStateModel.removeImage(); + }); + + it('should not auto-select when there are multiple valid available drives', function() { + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + + DrivesModel.setDrives([ + { + device: '/dev/sdb', + name: 'Foo', + size: 999999999, + mountpoint: '/mnt/foo', + system: false, + protected: false + }, + { + device: '/dev/sdc', + name: 'Bar', + size: 999999999, + mountpoint: '/mnt/bar', + system: false, + protected: false + } + ]); + + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + }); + + it('should auto-select a single valid available drive', function() { + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + + DrivesModel.setDrives([ + { + device: '/dev/sdb', + name: 'Foo', + size: 999999999, + mountpoint: '/mnt/foo', + system: false, + protected: false + } + ]); + + m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal({ + device: '/dev/sdb', + name: 'Foo', + size: 999999999, + mountpoint: '/mnt/foo', + system: false, + protected: false + }); + }); + + it('should not auto-select a single too small drive', function() { + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + + DrivesModel.setDrives([ + { + device: '/dev/sdb', + name: 'Foo', + size: 99999999, + mountpoint: '/mnt/foo', + system: false, + protected: false + } + ]); + + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + }); + + it('should not auto-select a single protected drive', function() { + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + + DrivesModel.setDrives([ + { + device: '/dev/sdb', + name: 'Foo', + size: 999999999, + mountpoint: '/mnt/foo', + system: false, + protected: true + } + ]); + + m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; + }); + + }); + }); });