diff --git a/app/common/main-controller.js b/app/common/main-controller.js index 430a41c..dbc1112 100644 --- a/app/common/main-controller.js +++ b/app/common/main-controller.js @@ -348,6 +348,26 @@ angular.module('JamStash') } }; + $scope.turnVolumeUp = function () { + var volume = player.volume; + if ((volume+0.1) > 1 || volume < 0) { + volume = 0.9; + } + volume += 0.1; + player.volume = volume; + persistence.saveVolume(volume); + }; + + $scope.turnVolumeDown = function () { + var volume = player.volume; + if (volume > 1 || (volume-0.1) < 0) { + volume = 0.1; + } + volume -= 0.1; + player.volume = volume; + persistence.saveVolume(volume); + }; + $rootScope.addToJukebox = function (id) { if (globals.settings.Debug) { console.log("LOAD JUKEBOX"); } $.ajax({ @@ -416,6 +436,7 @@ angular.module('JamStash') persistence.loadQueue(); persistence.loadTrackPosition(); } + player.volume = persistence.getVolume(); } /* End Startup */ }]); diff --git a/app/common/main-controller_test.js b/app/common/main-controller_test.js index 5256c20..362d80c 100644 --- a/app/common/main-controller_test.js +++ b/app/common/main-controller_test.js @@ -1,7 +1,7 @@ describe("Main controller", function() { 'use strict'; - var scope, mockGlobals, player, utils; + var controllerParams, $controller, scope, mockGlobals, player, utils, persistence; beforeEach(function() { mockGlobals = { settings: { @@ -17,14 +17,20 @@ describe("Main controller", function() { // Mock the player service player = jasmine.createSpyObj("player", ["togglePause"]); + player.queue = []; + player.volume = 1.0; - inject(function ($controller, $rootScope, _$document_, _$window_, _$location_, _$cookieStore_, _utils_, globals, _model_, _notifications_, _locker_, _Page_) { + // Mock the persistence service + persistence = jasmine.createSpyObj("persistence", ["getVolume", "saveVolume"]); + + inject(function (_$controller_, $rootScope, _$document_, _$window_, _$location_, _$cookieStore_, _utils_, globals, _model_, _notifications_, _Page_) { scope = $rootScope.$new(); utils = _utils_; spyOn(utils, "switchTheme"); - $controller('AppController', { + $controller = _$controller_; + controllerParams = { $scope: scope, $rootScope: $rootScope, $document: _$document_, @@ -36,11 +42,10 @@ describe("Main controller", function() { model: _model_, notifications: _notifications_, player: player, - locker: _locker_, + persistence: persistence, Page: _Page_ - }); + }; }); - player.queue = []; }); xdescribe("updateFavorite -", function() { @@ -70,30 +75,85 @@ describe("Main controller", function() { }); - it("given that the global setting 'ShowQueue' is true, when the playing queue's length changes and is not empty, it shows the queue", function() { - mockGlobals.settings.ShowQueue = true; - player.queue = [{ - id: 684 - }]; - spyOn(scope, "showQueue"); - - scope.$apply(); - - expect(scope.showQueue).toHaveBeenCalled(); - }); - - describe("When I toggle pause,", function() { - it("given that we're using the Jukebox mode, it sends a 'pause' command to the jukebox", function() { - mockGlobals.settings.Jukebox = true; - spyOn(scope, "sendToJukebox"); - - scope.togglePause(); - expect(scope.sendToJukebox).toHaveBeenCalledWith('pause'); + describe("", function () { + beforeEach(function() { + $controller('AppController', controllerParams); }); - it("it toggles pause using the player service", function() { - scope.togglePause(); - expect(player.togglePause).toHaveBeenCalled(); + it("given that the global setting 'ShowQueue' is true, when the playing queue's length changes and is not empty, it shows the queue", function() { + mockGlobals.settings.ShowQueue = true; + player.queue = [{ + id: 684 + }]; + spyOn(scope, "showQueue"); + + scope.$apply(); + + expect(scope.showQueue).toHaveBeenCalled(); + }); + + describe("When I toggle pause,", function() { + it("given that we're using the Jukebox mode, it sends a 'pause' command to the jukebox", function() { + mockGlobals.settings.Jukebox = true; + spyOn(scope, "sendToJukebox"); + + scope.togglePause(); + expect(scope.sendToJukebox).toHaveBeenCalledWith('pause'); + }); + + it("it toggles pause using the player service", function() { + scope.togglePause(); + expect(player.togglePause).toHaveBeenCalled(); + }); + }); + + describe("When I turn the volume up,", function() { + it("it sets the player's volume up by 10% and saves it using the persistence service", function() { + player.volume = 0.5; + + scope.turnVolumeUp(); + + expect(player.volume).toBe(0.6); + expect(persistence.saveVolume).toHaveBeenCalledWith(0.6); + }); + + it("if the player's resulting volume won't be between 0 and 1, it sets it to 1", function() { + player.volume = 5.91488; + + scope.turnVolumeUp(); + + expect(player.volume).toBe(1.0); + }); + }); + + describe("When I turn the volume down,", function() { + it("it sets the player's volume down by 10% and saves it using the persistence service", function() { + player.volume = 0.5; + + scope.turnVolumeDown(); + + expect(player.volume).toBe(0.4); + expect(persistence.saveVolume).toHaveBeenCalledWith(0.4); + }); + + it("if the player's resulting volume won't be between 0 and 1, it sets it to 0", function() { + player.volume = 5.91488; + + scope.turnVolumeDown(); + + expect(player.volume).toBe(0); + }); + }); + }); + + describe("When starting up,", function() { + it("it loads the volume from the persistence service and sets the player service's volume with it", function() { + persistence.getVolume.and.returnValue(0.551835); + + $controller('AppController', controllerParams); + + expect(persistence.getVolume).toHaveBeenCalled(); + expect(player.volume).toBe(0.551835); }); }); }); diff --git a/app/common/persistence-service.js b/app/common/persistence-service.js index a77cded..dfc097a 100644 --- a/app/common/persistence-service.js +++ b/app/common/persistence-service.js @@ -54,5 +54,17 @@ angular.module('jamstash.persistence', ['jamstash.settings', 'jamstash.player.se locker.forget('CurrentQueue'); if (globals.settings.Debug) { console.log('Removing Play Queue from localStorage'); } }; + + this.getVolume = function () { + return locker.get('Volume'); + }; + + this.saveVolume = function (volume) { + locker.put('Volume', volume); + }; + + this.deleteVolume = function () { + locker.forget('Volume'); + }; }]); diff --git a/app/common/persistence-service_test.js b/app/common/persistence-service_test.js index 4bb9d48..f60ae3e 100644 --- a/app/common/persistence-service_test.js +++ b/app/common/persistence-service_test.js @@ -85,19 +85,37 @@ describe("Persistence service", function() { expect(notifications.updateMessage).not.toHaveBeenCalled(); }); }); + + describe("getVolume -", function() { + it("Given that we previously saved the volume in local Storage, it retrieves it", function() { + fakeStorage = { 'Volume': 0.46582 }; + + var volume = persistence.getVolume(); + + expect(locker.get).toHaveBeenCalledWith('Volume'); + expect(volume).toBe(0.46582); + }); + + it("Given that we didn't save the volume in local Storage, it returns undefined", function() { + var volume = persistence.getVolume(); + + expect(locker.get).toHaveBeenCalledWith('Volume'); + expect(volume).toBeUndefined(); + }); + }); }); - describe("save from localStorage -", function() { + describe("save to localStorage -", function() { beforeEach(function() { spyOn(locker, "put"); }); - it("it saves the current track's position in local Storage", function() { + it("saves the current track's position in local Storage", function() { persistence.saveTrackPosition(song); expect(locker.put).toHaveBeenCalledWith('CurrentSong', song); }); - it("it saves the playing queue in local Storage", function() { + it("saves the playing queue in local Storage", function() { player.queue = [ { id: 1245 }, { id: 7465 }, @@ -106,6 +124,11 @@ describe("Persistence service", function() { persistence.saveQueue(); expect(locker.put).toHaveBeenCalledWith('CurrentQueue', player.queue); }); + + it("saves the volume in local Storage", function() { + persistence.saveVolume(0.05167); + expect(locker.put).toHaveBeenCalledWith('Volume', 0.05167); + }); }); describe("remove from localStorage -", function() { @@ -113,14 +136,19 @@ describe("Persistence service", function() { spyOn(locker, "forget"); }); - it("it deletes the current track from local Storage", function() { + it("deletes the current track from local Storage", function() { persistence.deleteTrackPosition(); expect(locker.forget).toHaveBeenCalledWith('CurrentSong'); }); - it("it deletes the saved playing queue from local Storage", function() { + it("deletes the saved playing queue from local Storage", function() { persistence.deleteQueue(); expect(locker.forget).toHaveBeenCalledWith('CurrentQueue'); }); + + it("deletes the saved volume from local Storage", function() { + persistence.deleteVolume(); + expect(locker.forget).toHaveBeenCalledWith('Volume'); + }); }); }); diff --git a/app/index.html b/app/index.html index ff433ee..b5d0756 100755 --- a/app/index.html +++ b/app/index.html @@ -20,7 +20,7 @@ -
+