2015-10-30 23:20:57 +00:00
|
|
|
/*****************************************************************************
|
2017-04-05 21:35:12 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2017, 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,
|
2016-10-03 01:19:55 +00:00
|
|
|
alertModel,
|
2015-10-30 23:20:57 +00:00
|
|
|
errorModel;
|
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
function elapseTimeout() {
|
|
|
|
mockTimeout.mostRecentCall.args[0]();
|
|
|
|
}
|
|
|
|
|
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;
|
2016-10-03 01:19:55 +00:00
|
|
|
notificationService = new NotificationService(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
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
alertModel = {
|
|
|
|
title: "Mock Alert Notification",
|
|
|
|
severity: "alert"
|
|
|
|
};
|
|
|
|
|
2015-10-30 23:20:57 +00:00
|
|
|
errorModel = {
|
|
|
|
title: "Mock Error Notification",
|
|
|
|
severity: "error"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
it("notifies listeners on dismissal of notification", function () {
|
2016-10-03 01:19:55 +00:00
|
|
|
var dismissListener = jasmine.createSpy("ondismiss");
|
|
|
|
var 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-16 12:34:11 +00:00
|
|
|
it("dismisses a notification when the notification's dismiss method is used", function () {
|
|
|
|
var notification = notificationService.info(infoModel);
|
|
|
|
notification.dismiss();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("minimizes a notification when the notification's minimize method is used", function () {
|
|
|
|
var notification = notificationService.info(infoModel);
|
|
|
|
notification.minimize();
|
|
|
|
elapseTimeout(); // needed for the minimize animation timeout
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
describe("when receiving info notifications", function () {
|
|
|
|
it("minimizes info notifications if the caller disables auto-dismiss", function () {
|
|
|
|
infoModel.autoDismiss = false;
|
|
|
|
var notification = notificationService.info(infoModel);
|
|
|
|
elapseTimeout();
|
|
|
|
// 2nd elapse for the minimize animation timeout
|
|
|
|
elapseTimeout();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
it("dismisses info notifications if the caller ignores auto-dismiss", function () {
|
|
|
|
notificationService.info(infoModel);
|
|
|
|
elapseTimeout();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(0);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-16 12:34:11 +00:00
|
|
|
it("dismisses info notifications if the caller requests auto-dismiss", function () {
|
2016-10-03 01:19:55 +00:00
|
|
|
infoModel.autoDismiss = true;
|
|
|
|
notificationService.info(infoModel);
|
|
|
|
elapseTimeout();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(0);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
});
|
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
describe("when receiving alert notifications", function () {
|
|
|
|
it("minimizes alert notifications if the caller enables auto-dismiss", function () {
|
|
|
|
alertModel.autoDismiss = true;
|
|
|
|
var notification = notificationService.alert(alertModel);
|
|
|
|
elapseTimeout();
|
|
|
|
elapseTimeout();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
it("keeps alert notifications active if the caller disables auto-dismiss", function () {
|
|
|
|
mockTimeout.andCallFake(function (callback, time) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
alertModel.autoDismiss = false;
|
|
|
|
var notification = notificationService.alert(alertModel);
|
|
|
|
expect(notificationService.getActiveNotification()).toEqual(notification);
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
it("keeps alert notifications active if the caller ignores auto-dismiss", function () {
|
|
|
|
mockTimeout.andCallFake(function (callback, time) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
var notification = notificationService.alert(alertModel);
|
|
|
|
expect(notificationService.getActiveNotification()).toEqual(notification);
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
});
|
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
describe("when receiving error notifications", function () {
|
|
|
|
it("minimizes error notifications if the caller enables auto-dismiss", function () {
|
|
|
|
errorModel.autoDismiss = true;
|
|
|
|
var notification = notificationService.error(errorModel);
|
|
|
|
elapseTimeout();
|
|
|
|
elapseTimeout();
|
|
|
|
expect(notificationService.getActiveNotification()).toBeUndefined();
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
it("keeps error notifications active if the caller disables auto-dismiss", function () {
|
|
|
|
mockTimeout.andCallFake(function (callback, time) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
errorModel.autoDismiss = false;
|
|
|
|
var notification = notificationService.error(errorModel);
|
|
|
|
expect(notificationService.getActiveNotification()).toEqual(notification);
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
|
2016-10-03 01:19:55 +00:00
|
|
|
it("keeps error notifications active if the caller ignores auto-dismiss", function () {
|
|
|
|
mockTimeout.andCallFake(function (callback, time) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
var notification = notificationService.error(errorModel);
|
|
|
|
expect(notificationService.getActiveNotification()).toEqual(notification);
|
|
|
|
expect(notificationService.notifications.length).toEqual(1);
|
|
|
|
expect(notificationService.notifications[0]).toEqual(notification);
|
|
|
|
});
|
2015-10-30 23:20:57 +00:00
|
|
|
});
|
|
|
|
|
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;
|
2016-10-03 01:19:55 +00:00
|
|
|
infoModel.autoDismiss = false;
|
2015-10-30 23:20:57 +00:00
|
|
|
//First pre-load with a info message
|
2016-10-02 17:40:53 +00:00
|
|
|
notificationService.notify(infoModel);
|
2016-10-03 01:19:55 +00:00
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2015-10-30 23:20:57 +00:00
|
|
|
//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
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//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.
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
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
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//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.
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//Previous error message should be minimized, not
|
|
|
|
// dismissed
|
|
|
|
expect(notificationService.notifications.length).toEqual(2);
|
2016-10-03 01:19:55 +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
|
|
|
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
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//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.
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//Previous error message should be minimized, not
|
|
|
|
// dismissed
|
|
|
|
expect(notificationService.notifications.length).toEqual(3);
|
2016-10-03 01:19:55 +00:00
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2015-10-30 23:20:57 +00:00
|
|
|
expect(activeNotification.model).toBe(error2);
|
|
|
|
expect(errorModel.minimized).toEqual(true);
|
|
|
|
|
|
|
|
//Mock the second auto-minimize
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
2015-10-30 23:20:57 +00:00
|
|
|
//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.
|
2016-10-03 01:19:55 +00:00
|
|
|
elapseTimeout();
|
|
|
|
activeNotification = notificationService.getActiveNotification();
|
2015-10-30 23:20:57 +00:00
|
|
|
expect(activeNotification.model).toBe(error3);
|
|
|
|
expect(error2.minimized).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|