Use status service to close read mode from action bar

This commit is contained in:
Tankred Hase 2014-12-04 16:24:42 +01:00
parent 33b30221f7
commit df9ba7fd66
13 changed files with 119 additions and 192 deletions

View File

@ -2,7 +2,11 @@
var JUNK_FOLDER_TYPE = 'Junk';
var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
var ActionBarCtrl = function($scope, email, dialog, status) {
//
// scope functions
//
/**
* Move a single message from the currently selected folder to another folder
@ -15,9 +19,9 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
}
// close read state
$scope.state.read.toggle(false);
statusDisplay.update('Moving message...');
status.setReading(false);
// show message
status.update('Moving message...');
email.moveMessage({
folder: currentFolder(),
@ -28,14 +32,14 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
// show errors where appropriate
if (err.code === 42) {
$scope.select(message);
statusDisplay.update('Unable to move message in offline mode!');
status.update('Unable to move message in offline mode!');
return;
}
statusDisplay.update('Error during move!');
status.update('Error during move!');
dialog.error(err);
return;
}
statusDisplay.update('Message moved.');
status.update('Message moved.');
$scope.$apply();
});
};
@ -76,9 +80,8 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
}
// close read state
$scope.state.read.toggle(false);
statusDisplay.update('Deleting message...');
status.setReading(false);
status.update('Deleting message...');
email.deleteMessage({
folder: currentFolder(),
@ -88,14 +91,14 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
// show errors where appropriate
if (err.code === 42) {
$scope.select(message);
statusDisplay.update('Unable to delete message in offline mode!');
status.update('Unable to delete message in offline mode!');
return;
}
statusDisplay.update('Error during delete!');
status.update('Error during delete!');
dialog.error(err);
return;
}
statusDisplay.update('Message deleted.');
status.update('Message deleted.');
$scope.$apply();
});
};
@ -118,11 +121,11 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
return;
}
statusDisplay.update('Updating unread flag...');
status.update('Updating unread flag...');
// close read state
if (!keepOpen) {
$scope.state.read.toggle(false);
status.setReading(false);
}
var originalState = message.unread;
@ -134,17 +137,17 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
if (err && err.code === 42) {
// offline, restore
message.unread = originalState;
statusDisplay.update('Unable to mark message in offline mode!');
status.update('Unable to mark message in offline mode!');
return;
}
if (err) {
statusDisplay.update('Error on sync!');
status.update('Error on sync!');
dialog.error(err);
return;
}
statusDisplay.update('Online');
status.update('Online');
$scope.$apply();
});
};
@ -169,7 +172,7 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
return;
}
statusDisplay.update(flagged ? 'Adding star to message...' : 'Removing star from message');
status.update(flagged ? 'Adding star to message...' : 'Removing star from message');
var originalState = message.flagged;
message.flagged = flagged;
@ -180,17 +183,17 @@ var ActionBarCtrl = function($scope, email, dialog, statusDisplay) {
if (err && err.code === 42) {
// offline, restore
message.unread = originalState;
statusDisplay.update('Unable to ' + (flagged ? 'add star to' : 'remove star from') + ' message in offline mode!');
status.update('Unable to ' + (flagged ? 'add star to' : 'remove star from') + ' message in offline mode!');
return;
}
if (err) {
statusDisplay.update('Error on sync!');
status.update('Error on sync!');
dialog.error(err);
return;
}
statusDisplay.update('Online');
status.update('Online');
$scope.$apply();
});
};

View File

@ -11,10 +11,10 @@ var INIT_DISPLAY_LEN = 50,
FOLDER_TYPE_INBOX = 'Inbox',
NOTIFICATION_INBOX_TIMEOUT = 5000;
var MailListCtrl = function($scope, $timeout, $location, $filter, statusDisplay, notification, email, keychain, dialog, search, dummy) {
var MailListCtrl = function($scope, $timeout, $location, $filter, status, notification, email, keychain, dialog, search, dummy) {
//
// Init
// scope state
//
$scope.state.mailList = {};
@ -156,7 +156,7 @@ var MailListCtrl = function($scope, $timeout, $location, $filter, statusDisplay,
// in development, display dummy mail objects
if ($location.search().dev) {
statusDisplay.update('Last update: ', new Date());
status.update('Last update: ', new Date());
currentFolder().messages = dummy.listMails();
return;
}
@ -217,20 +217,20 @@ var MailListCtrl = function($scope, $timeout, $location, $filter, statusDisplay,
if (!searchText) {
// set display buffer to first messages
$scope.displayMessages = currentFolder().messages.slice(0, INIT_DISPLAY_LEN);
statusDisplay.setSearching(false);
statusDisplay.update('Online');
status.setSearching(false);
status.update('Online');
return;
}
// display searching spinner
statusDisplay.setSearching(true);
statusDisplay.update('Searching ...');
status.setSearching(true);
status.update('Searching ...');
searchTimeout = setTimeout(function() {
$scope.$apply(function() {
// filter relevant messages
$scope.displayMessages = search.filter(currentFolder().messages, searchText);
statusDisplay.setSearching(false);
statusDisplay.update('Matches in this folder');
status.setSearching(false);
status.update('Matches in this folder');
});
}, 500);
};
@ -242,10 +242,10 @@ var MailListCtrl = function($scope, $timeout, $location, $filter, statusDisplay,
// wait one cycle for the status display controllers to init
$timeout(function() {
if (isOnline) {
statusDisplay.update('Online');
status.update('Online');
openCurrentFolder();
} else {
statusDisplay.update('Offline mode');
status.update('Offline mode');
}
});
}, true);

View File

@ -12,9 +12,6 @@ var ReadCtrl = function($scope, $location, email, invitation, outbox, pgp, keych
// scope state
//
// set default value so that the popover height is correct on init
$scope.keyId = 'No key found.';
$scope.state.read = {
open: false,
toggle: function(to) {
@ -22,6 +19,13 @@ var ReadCtrl = function($scope, $location, email, invitation, outbox, pgp, keych
}
};
$scope.$on('read', function(e, state) {
$scope.state.read.toggle(state);
});
// set default value so that the popover height is correct on init
$scope.keyId = 'No key found.';
//
// url/history handling
//

View File

@ -7,6 +7,6 @@ require('./dummy');
require('./dialog');
require('./connection-doctor');
require('./update/update-handler');
require('./status-display');
require('./status');
require('./download');
require('./notification');

View File

@ -1,13 +1,13 @@
'use strict';
var ngModule = angular.module('woUtil');
ngModule.service('statusDisplay', StatusDisplay);
module.exports = StatusDisplay;
ngModule.service('status', Status);
module.exports = Status;
/**
* A central service to display status updates to the user
*/
function StatusDisplay($rootScope, axe) {
function Status($rootScope, axe) {
this._rootScope = $rootScope;
this._axe = axe;
}
@ -17,7 +17,7 @@ function StatusDisplay($rootScope, axe) {
* @param {String} text The status message that is to be displayed to the user
* @param {Date} time The time of the last update
*/
StatusDisplay.prototype.update = function(text, time) {
Status.prototype.update = function(text, time) {
this._axe.info('status display', text);
this._rootScope.$broadcast('status', text, time);
};
@ -26,6 +26,14 @@ StatusDisplay.prototype.update = function(text, time) {
* Update the searching status to show a spinner while searching
* @param {Boolean} state If the spinner should be displayed or not
*/
StatusDisplay.prototype.setSearching = function(state) {
Status.prototype.setSearching = function(state) {
this._rootScope.$broadcast('searching', state);
};
/**
* Update the reading status to communicate between controllers, if we're in read mode.
* @param {Boolean} state If the reade mode should be open or not
*/
Status.prototype.setReading = function(state) {
this._rootScope.$broadcast('read', state);
};

View File

@ -2,26 +2,22 @@
var Email = require('../../../../src/js/email/email'),
Dialog = require('../../../../src/js/util/dialog'),
StatusDisplay = require('../../../../src/js/util/status-display'),
Status = require('../../../../src/js/util/status'),
ActionBarCtrl = require('../../../../src/js/controller/app/action-bar');
describe('Action Bar Controller unit test', function() {
var scope, actionBarCtrl, emailMock, dialogMock, statusDisplayMock;
var scope, actionBarCtrl, emailMock, dialogMock, statusMock;
beforeEach(function() {
emailMock = sinon.createStubInstance(Email);
dialogMock = sinon.createStubInstance(Dialog);
statusDisplayMock = sinon.createStubInstance(StatusDisplay);
statusMock = sinon.createStubInstance(Status);
angular.module('actionbartest', []);
angular.module('actionbartest', ['woUtil']);
angular.mock.module('actionbartest');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {
mailList: {
updateStatus: function() {}
}
};
scope.state = {};
scope.state.nav = {
currentFolder: {
@ -35,15 +31,11 @@ describe('Action Bar Controller unit test', function() {
}
};
scope.state.read = {
open: true
};
actionBarCtrl = $controller(ActionBarCtrl, {
$scope: scope,
email: emailMock,
dialog: dialogMock,
statusDisplay: statusDisplayMock
status: statusMock
});
});
});
@ -60,8 +52,8 @@ describe('Action Bar Controller unit test', function() {
scope.deleteMessage({});
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(emailMock.deleteMessage.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false;
});
});
@ -92,8 +84,8 @@ describe('Action Bar Controller unit test', function() {
scope.moveMessage({}, {});
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(emailMock.moveMessage.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false;
});
});
@ -157,8 +149,17 @@ describe('Action Bar Controller unit test', function() {
scope.markMessage({}, true);
expect(statusMock.setReading.withArgs(false).calledOnce).to.be.true;
expect(emailMock.setFlags.calledOnce).to.be.true;
});
it('should mark the selected mail and close read mode', function() {
emailMock.setFlags.yields();
scope.markMessage({}, true, true);
expect(statusMock.setReading.calledOnce).to.be.false;
expect(emailMock.setFlags.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false;
});
});

View File

@ -3,34 +3,15 @@
var MailListCtrl = require('../../../../src/js/controller/app/mail-list'),
EmailDAO = require('../../../../src/js/email/email'),
KeychainDAO = require('../../../../src/js/service/keychain'),
StatusDisplay = require('../../../../src/js/util/status-display'),
Status = require('../../../../src/js/util/status'),
Dialog = require('../../../../src/js/util/dialog'),
Search = require('../../../../src/js/email/search');
describe('Mail List controller unit test', function() {
var scope, ctrl, statusDisplayMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock,
emailAddress, emails,
hasChrome, hasSocket, hasRuntime, hasIdentity;
var scope, ctrl, statusMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock,
emailAddress, emails, location;
beforeEach(function() {
hasChrome = !!window.chrome;
hasSocket = !!window.chrome.socket;
hasIdentity = !!window.chrome.identity;
if (!hasChrome) {
window.chrome = {};
}
if (!hasSocket) {
window.chrome.socket = {};
}
if (!hasRuntime) {
window.chrome.runtime = {
getURL: function() {}
};
}
if (!hasIdentity) {
window.chrome.identity = {};
}
emails = [{
unread: true
}, {
@ -46,7 +27,7 @@ describe('Mail List controller unit test', function() {
close: function() {}
};
statusDisplayMock = sinon.createStubInstance(StatusDisplay);
statusMock = sinon.createStubInstance(Status);
emailMock = sinon.createStubInstance(EmailDAO);
keychainMock = sinon.createStubInstance(KeychainDAO);
dialogMock = sinon.createStubInstance(Dialog);
@ -54,19 +35,16 @@ describe('Mail List controller unit test', function() {
angular.module('maillisttest', ['woEmail', 'woServices', 'woUtil']);
angular.mock.module('maillisttest');
angular.mock.inject(function($rootScope, $controller) {
angular.mock.inject(function($rootScope, $controller, $location) {
scope = $rootScope.$new();
scope.state = {
read: {
toggle: function() {}
}
};
location = $location;
scope.state = {};
scope.loadVisibleBodies = function() {};
ctrl = $controller(MailListCtrl, {
$scope: scope,
$routeParams: {},
statusDisplay: statusDisplayMock,
$location: location,
status: statusMock,
notification: notificationMock,
email: emailMock,
keychain: keychainMock,
@ -76,20 +54,7 @@ describe('Mail List controller unit test', function() {
});
});
afterEach(function() {
if (!hasSocket) {
delete window.chrome.socket;
}
if (!hasRuntime) {
delete window.chrome.runtime;
}
if (!hasChrome) {
delete window.chrome;
}
if (!hasIdentity) {
delete window.chrome.identity;
}
});
afterEach(function() {});
describe('displayMore', function() {
beforeEach(function() {
@ -132,21 +97,21 @@ describe('Mail List controller unit test', function() {
it('should show initial message on empty', function() {
scope.displaySearchResults();
expect(statusDisplayMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusDisplayMock.update.withArgs('Online').calledOnce).to.be.true;
expect(statusMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusMock.update.withArgs('Online').calledOnce).to.be.true;
expect(scope.displayMessages.length).to.equal(2);
});
it('should show initial message on empty', function() {
searchMock.filter.returns(['a']);
scope.displaySearchResults('query');
expect(statusDisplayMock.setSearching.withArgs(true).calledOnce).to.be.true;
expect(statusDisplayMock.update.withArgs('Searching ...').calledOnce).to.be.true;
expect(statusMock.setSearching.withArgs(true).calledOnce).to.be.true;
expect(statusMock.update.withArgs('Searching ...').calledOnce).to.be.true;
clock.tick(500);
expect(scope.displayMessages).to.deep.equal(['a']);
expect(statusDisplayMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusDisplayMock.update.withArgs('Matches in this folder').calledOnce).to.be.true;
expect(statusMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusMock.update.withArgs('Matches in this folder').calledOnce).to.be.true;
});
});
@ -160,10 +125,12 @@ describe('Mail List controller unit test', function() {
describe('push notification', function() {
beforeEach(function() {
scope._stopWatchTask();
sinon.stub(scope, 'navigate');
});
afterEach(function() {
notificationMock.create.restore();
scope.navigate.restore();
});
it('should succeed for single mail', function(done) {
@ -181,7 +148,7 @@ describe('Mail List controller unit test', function() {
expect(opts.message).to.equal(mail.subject);
opts.onClick();
expect(scope.state.mailList.selected).to.equal(mail);
expect(scope.navigate.withArgs(mail).calledOnce).to.be.true;
done();
});
@ -224,7 +191,7 @@ describe('Mail List controller unit test', function() {
expect(opts.message).to.equal(mails[0].subject + '\n' + mails[1].subject);
opts.onClick();
expect(scope.state.mailList.selected).to.equal(mails[0]);
expect(scope.navigate.withArgs(mails[0]).calledOnce).to.be.true;
done();
});
@ -358,7 +325,7 @@ describe('Mail List controller unit test', function() {
}));
it('should output date only if date is not today', angular.mock.inject(function($filter) {
var yesterday = new Date();
yesterday.setTime(yesterday.getTime() - 24*60*60*1000);
yesterday.setTime(yesterday.getTime() - 24 * 60 * 60 * 1000);
var expected = $filter('date')(yesterday, 'mediumDate');
expect(scope.formatDate(yesterday)).to.equal(expected);

View File

@ -119,7 +119,7 @@ describe('Login Controller unit test', function() {
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /desktop', function() {
it('should redirect to /account', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
@ -133,7 +133,7 @@ describe('Login Controller unit test', function() {
createController();
expect(goToStub.withArgs('/desktop').calledOnce).to.be.true;
expect(goToStub.withArgs('/account').calledOnce).to.be.true;
});
it('should fail for keychain.requestPrivateKeyDownload', function() {

View File

@ -60,7 +60,7 @@ describe('Login (existing user) Controller unit test', function() {
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true;
expect(pathSpy.calledOnce).to.be.true;
expect(pathSpy.calledWith('/desktop')).to.be.true;
expect(pathSpy.calledWith('/account')).to.be.true;
});
it('should not work when keypair unavailable', function() {

View File

@ -102,7 +102,7 @@ describe('Login (initial user) Controller unit test', function() {
expect(scope.errMsg).to.not.exist;
expect(scope.state.ui).to.equal(2);
expect(newsletterStub.called).to.be.true;
expect(location.$$path).to.equal('/desktop');
expect(location.$$path).to.equal('/account');
expect(emailMock.unlock.calledOnce).to.be.true;
});
});

View File

@ -152,7 +152,7 @@ describe('Login Private Key Download Controller unit test', function() {
scope.decryptAndStorePrivateKeyLocally();
});
it('should goto /desktop on emailDao.unlock success', function(done) {
it('should goto /account on emailDao.unlock success', function(done) {
keychainMock.decryptAndStorePrivateKeyLocally.yields(null, {
encryptedKey: 'keyArmored'
});
@ -160,7 +160,7 @@ describe('Login Private Key Download Controller unit test', function() {
authMock.storeCredentials.yields();
scope.goTo = function(location) {
expect(location).to.equal('/desktop');
expect(location).to.equal('/account');
expect(keychainMock.decryptAndStorePrivateKeyLocally.calledOnce).to.be.true;
expect(emailDaoMock.unlock.calledOnce).to.be.true;
done();
@ -173,10 +173,10 @@ describe('Login Private Key Download Controller unit test', function() {
describe('goTo', function() {
it('should work', function() {
sinon.stub(location, 'path', function(path) {
expect(path).to.equal('/desktop');
expect(path).to.equal('/account');
});
scope.goTo('/desktop');
scope.goTo('/account');
});
});
});

View File

@ -1,64 +0,0 @@
'use strict';
var btnHandler = require('../../../src/js/util/backbutton-handler');
describe('Backbutton Handler', function() {
chai.config.includeStack = true;
var scope, event;
beforeEach(function() {
scope = {
state: {},
$apply: function() {}
};
event = new CustomEvent('backbutton');
// this is a precondition for the test. throw an exception
// if this would produce side effects
expect(navigator.app).to.not.exist;
navigator.app = {};
btnHandler.attachHandler(scope);
btnHandler.start();
});
afterEach(function() {
btnHandler.stop();
delete navigator.app;
});
it('should close lightbox', function() {
scope.state.lightbox = 'asd';
document.dispatchEvent(event);
expect(scope.state.lightbox).to.be.undefined;
});
it('should close reader', function() {
scope.state.read = {
open: true,
toggle: function(state) {
scope.state.read.open = state;
}
};
document.dispatchEvent(event);
expect(scope.state.read.open).to.be.false;
});
it('should close navigation', function() {
scope.state.nav = {
open: true,
toggle: function(state) {
scope.state.nav.open = state;
}
};
document.dispatchEvent(event);
expect(scope.state.nav.open).to.be.false;
});
it('should close app', function(done) {
navigator.app.exitApp = done;
document.dispatchEvent(event);
});
});

View File

@ -1,14 +1,14 @@
'use strict';
describe('Status Display Service unit test', function() {
var statusDisplay, logInfoStub, rootScope, broadcastSpy;
describe('Status Service unit test', function() {
var status, logInfoStub, rootScope, broadcastSpy;
beforeEach(function() {
angular.module('statusDisplay-test', ['woUtil']);
angular.mock.module('statusDisplay-test');
angular.module('status-test', ['woUtil']);
angular.mock.module('status-test');
angular.mock.inject(function($injector, axe) {
logInfoStub = sinon.stub(axe, 'info');
statusDisplay = $injector.get('statusDisplay');
status = $injector.get('status');
rootScope = $injector.get('$rootScope');
broadcastSpy = sinon.spy(rootScope, '$broadcast');
});
@ -23,7 +23,7 @@ describe('Status Display Service unit test', function() {
var message = 'Tada!',
time = new Date();
statusDisplay.update(message, time);
status.update(message, time);
expect(broadcastSpy.withArgs('status', message, time).calledOnce).to.be.true;
expect(logInfoStub.withArgs('status display', message).calledOnce).to.be.true;
@ -32,10 +32,18 @@ describe('Status Display Service unit test', function() {
describe('setSearching', function() {
it('should work', function() {
statusDisplay.setSearching(true);
status.setSearching(true);
expect(broadcastSpy.withArgs('searching', true).calledOnce).to.be.true;
});
});
describe('setReading', function() {
it('should work', function() {
status.setReading(true);
expect(broadcastSpy.withArgs('read', true).calledOnce).to.be.true;
});
});
});