2015-10-30 23:20:57 +00:00
|
|
|
/*****************************************************************************
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
2015-10-30 23:20:57 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-10-30 23:20:57 +00:00
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-10-30 23:20:57 +00:00
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2016-10-01 19:40:21 +00:00
|
|
|
/*global describe,it,expect,beforeEach,jasmine*/
|
2015-10-30 23:20:57 +00:00
|
|
|
|
|
|
|
define(
|
|
|
|
['../src/NotificationService'],
|
|
|
|
function (NotificationService) {
|
|
|
|
|
|
|
|
describe("The notification service ", function () {
|
|
|
|
var notificationService,
|
|
|
|
mockTimeout,
|
|
|
|
mockAutoDismiss,
|
|
|
|
mockMinimizeTimeout,
|
2016-02-06 01:40:04 +00:00
|
|
|
mockTopicFunction,
|
|
|
|
mockTopicObject,
|
2016-10-02 17:40:53 +00:00
|
|
|
infoModel,
|
2015-10-30 23:20:57 +00:00
|
|
|
errorModel;
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
beforeEach(function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
mockTimeout = jasmine.createSpy("$timeout");
|
2016-02-06 01:40:04 +00:00
|
|
|
mockTopicFunction = jasmine.createSpy("topic");
|
|
|
|
mockTopicObject = jasmine.createSpyObj("topicObject", ["listen", "notify"]);
|
|
|
|
mockTopicFunction.andReturn(mockTopicObject);
|
|
|
|
|
2015-10-30 23:20:57 +00:00
|
|
|
mockAutoDismiss = mockMinimizeTimeout = 1000;
|
|
|
|
notificationService = new NotificationService(
|
2016-02-06 01:40:04 +00:00
|
|
|
mockTimeout, mockTopicFunction, mockAutoDismiss, mockMinimizeTimeout);
|
2016-10-02 17:40:53 +00:00
|
|
|
|
|
|
|
infoModel = {
|
|
|
|
title: "Mock Info Notification",
|
2015-10-30 23:20:57 +00:00
|
|
|
severity: "info"
|
|
|
|
};
|
2016-10-02 17:40:53 +00:00
|
|
|
|
2015-10-30 23:20:57 +00:00
|
|
|
errorModel = {
|
|
|
|
title: "Mock Error Notification",
|
|
|
|
severity: "error"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-10-02 17:40:53 +00:00
|
|
|
it("activates info notifications", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification;
|
2016-10-02 17:40:53 +00:00
|
|
|
notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
});
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
it("notifies listeners on dismissal of notification", function () {
|
2016-02-06 01:40:04 +00:00
|
|
|
var notification,
|
|
|
|
dismissListener = jasmine.createSpy("ondismiss");
|
2016-10-02 17:40:53 +00:00
|
|
|
notification = notificationService.notify(infoModel);
|
2016-02-06 01:40:04 +00:00
|
|
|
notification.onDismiss(dismissListener);
|
|
|
|
expect(mockTopicObject.listen).toHaveBeenCalled();
|
|
|
|
notification.dismiss();
|
|
|
|
expect(mockTopicObject.notify).toHaveBeenCalled();
|
|
|
|
mockTopicObject.listen.mostRecentCall.args[0]();
|
|
|
|
expect(dismissListener).toHaveBeenCalled();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-10-01 19:40:21 +00:00
|
|
|
it("activates an info notification built with just the title", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification,
|
|
|
|
notificationTitle = "Test info notification";
|
|
|
|
notificationService.info(notificationTitle);
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification.model.title).toBe(notificationTitle);
|
|
|
|
expect(activeNotification.model.severity).toBe("info");
|
|
|
|
});
|
|
|
|
|
2016-10-02 17:40:53 +00:00
|
|
|
it("gets a new info notification with numerical auto-dismiss specified. ", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification;
|
2016-10-02 17:40:53 +00:00
|
|
|
infoModel.autoDismiss = 1000;
|
|
|
|
notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
expect(mockTimeout.calls.length).toBe(2);
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2016-10-01 19:40:21 +00:00
|
|
|
it("gets a new notification with boolean auto-dismiss specified. ", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification;
|
2016-10-02 17:40:53 +00:00
|
|
|
infoModel.autoDismiss = true;
|
|
|
|
notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
expect(mockTimeout.calls.length).toBe(2);
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
it("allows minimization of notifications", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var notification,
|
|
|
|
activeNotification;
|
|
|
|
|
2016-10-02 17:40:53 +00:00
|
|
|
infoModel.autoDismiss = false;
|
|
|
|
notification = notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
notification.minimize();
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
it("allows dismissal of notifications", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var notification,
|
|
|
|
activeNotification;
|
|
|
|
|
2016-10-02 17:40:53 +00:00
|
|
|
infoModel.autoDismiss = false;
|
|
|
|
notification = notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
notification.dismiss();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2016-10-01 19:40:21 +00:00
|
|
|
describe("when called with multiple notifications", function () {
|
|
|
|
it("auto-dismisses the previously active notification, making the new notification active", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification;
|
|
|
|
//First pre-load with a info message
|
2016-10-02 17:40:53 +00:00
|
|
|
notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
activeNotification =
|
|
|
|
notificationService.getActiveNotification();
|
|
|
|
//Initially expect the active notification to be info
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
//Then notify of an error
|
|
|
|
notificationService.notify(errorModel);
|
|
|
|
//But it should be auto-dismissed and replaced with the
|
|
|
|
// error notification
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Two timeouts, one is to force minimization after
|
|
|
|
// displaying the message for a minimum period, the
|
|
|
|
// second is to allow minimization animation to take place.
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification.model).toBe(errorModel);
|
|
|
|
});
|
2016-10-01 19:40:21 +00:00
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
it("auto-minimizes an active error notification", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification;
|
|
|
|
//First pre-load with an error message
|
|
|
|
notificationService.notify(errorModel);
|
|
|
|
//Then notify of info
|
2016-10-02 17:40:53 +00:00
|
|
|
notificationService.notify(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
expect(notificationService.notifications.length).toEqual(2);
|
|
|
|
//Mock the auto-minimize
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Two timeouts, one is to force minimization after
|
|
|
|
// displaying the message for a minimum period, the
|
|
|
|
// second is to allow minimization animation to take place.
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Previous error message should be minimized, not
|
|
|
|
// dismissed
|
|
|
|
expect(notificationService.notifications.length).toEqual(2);
|
|
|
|
activeNotification =
|
|
|
|
notificationService.getActiveNotification();
|
2016-10-02 17:40:53 +00:00
|
|
|
expect(activeNotification.model).toBe(infoModel);
|
2015-10-30 23:20:57 +00:00
|
|
|
expect(errorModel.minimized).toEqual(true);
|
|
|
|
});
|
2016-10-01 19:40:21 +00:00
|
|
|
|
|
|
|
it("auto-minimizes errors when a number of them arrive in short succession", function () {
|
2015-10-30 23:20:57 +00:00
|
|
|
var activeNotification,
|
|
|
|
error2 = {
|
|
|
|
title: "Second Mock Error Notification",
|
|
|
|
severity: "error"
|
|
|
|
},
|
|
|
|
error3 = {
|
|
|
|
title: "Third Mock Error Notification",
|
|
|
|
severity: "error"
|
|
|
|
};
|
|
|
|
|
|
|
|
//First pre-load with a info message
|
|
|
|
notificationService.notify(errorModel);
|
|
|
|
//Then notify of a third error
|
|
|
|
notificationService.notify(error2);
|
|
|
|
notificationService.notify(error3);
|
|
|
|
expect(notificationService.notifications.length).toEqual(3);
|
|
|
|
//Mock the auto-minimize
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Two timeouts, one is to force minimization after
|
|
|
|
// displaying the message for a minimum period, the
|
|
|
|
// second is to allow minimization animation to take place.
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Previous error message should be minimized, not
|
|
|
|
// dismissed
|
|
|
|
expect(notificationService.notifications.length).toEqual(3);
|
|
|
|
activeNotification =
|
|
|
|
notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification.model).toBe(error2);
|
|
|
|
expect(errorModel.minimized).toEqual(true);
|
|
|
|
|
|
|
|
//Mock the second auto-minimize
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
//Two timeouts, one is to force minimization after
|
|
|
|
// displaying the message for a minimum period, the
|
|
|
|
// second is to allow minimization animation to take place.
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
activeNotification =
|
|
|
|
notificationService.getActiveNotification();
|
|
|
|
expect(activeNotification.model).toBe(error3);
|
|
|
|
expect(error2.minimized).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|