blob: 023d2064f72587d8ea0021a8f6b549aa000028bf [file] [log] [blame]
Simon Hunt988934e2015-01-23 11:49:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt988934e2015-01-23 11:49:24 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Layer -- Panel Service - Unit Tests
19 */
20describe('factory: fw/layer/panel.js', function () {
Simon Hunt54442fa2015-01-26 14:17:38 -080021 var $log, $timeout, fs, ps, d3Elem;
Simon Hunt988934e2015-01-23 11:49:24 -080022
23 beforeEach(module('onosLayer'));
24
Simon Hunt54442fa2015-01-26 14:17:38 -080025 beforeEach(inject(function (_$log_, _$timeout_, FnService, PanelService) {
Simon Hunt988934e2015-01-23 11:49:24 -080026 $log = _$log_;
Simon Hunt54442fa2015-01-26 14:17:38 -080027 $timeout = _$timeout_;
Simon Hunt988934e2015-01-23 11:49:24 -080028 fs = FnService;
29 ps = PanelService;
Matteo Scandolo231c7542016-05-20 11:13:11 -070030
31 spyOn(fs, 'debugOn').and.returnValue(true);
Simon Hunt54442fa2015-01-26 14:17:38 -080032 d3Elem = d3.select('body').append('div').attr('id', 'floatpanels');
33 ps.init();
Simon Hunt988934e2015-01-23 11:49:24 -080034 }));
35
Simon Hunt54442fa2015-01-26 14:17:38 -080036 afterEach(function () {
37 d3.select('#floatpanels').remove();
38 ps.init();
39 });
40
41 function floatPanelSelection() {
42 return d3Elem.selectAll('.floatpanel');
43 }
Simon Hunt988934e2015-01-23 11:49:24 -080044
45 it('should define PanelService', function () {
46 expect(ps).toBeDefined();
47 });
48
Matteo Scandolo231c7542016-05-20 11:13:11 -070049 it('should define api functions', function () {
Simon Hunt988934e2015-01-23 11:49:24 -080050 expect(fs.areFunctions(ps, [
Simon Hunt54442fa2015-01-26 14:17:38 -080051 'init', 'createPanel', 'destroyPanel'
Simon Hunt988934e2015-01-23 11:49:24 -080052 ])).toBeTruthy();
53 });
54
Simon Hunt54442fa2015-01-26 14:17:38 -080055 it('should have no panels to start', function () {
56 expect(floatPanelSelection().size()).toBe(0);
57 });
58
59 it('should log a warning if no ID is given', function () {
60 spyOn($log, 'warn');
61 var p = ps.createPanel();
62 expect(p).toBeNull();
63 expect($log.warn).toHaveBeenCalledWith('createPanel: no ID given');
64 expect(floatPanelSelection().size()).toBe(0);
65 });
66
Matteo Scandolo231c7542016-05-20 11:13:11 -070067 it('should create a default panel', function () {
Simon Hunt54442fa2015-01-26 14:17:38 -080068 spyOn($log, 'warn');
69 spyOn($log, 'debug');
70 var p = ps.createPanel('foo');
71 expect(p).not.toBeNull();
72 expect($log.warn).not.toHaveBeenCalled();
73 expect(floatPanelSelection().size()).toBe(1);
74 expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', {
75 edge: 'right',
76 width: 200,
Simon Hunt54442fa2015-01-26 14:17:38 -080077 margin: 20,
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080078 hideMargin: 20,
79 xtnTime: 750,
80 fade: true
Simon Hunt54442fa2015-01-26 14:17:38 -080081 });
82
83 // check basic properties
84 expect(p.width()).toEqual(200);
85 expect(p.isVisible()).toBeFalsy();
86
87 var el = floatPanelSelection();
88 expect(el.style('width')).toEqual('200px');
89 });
90
Simon Hunt4b668592015-01-29 17:33:53 -080091 it('should provide an api of panel functions', function () {
92 var p = ps.createPanel('foo');
93 expect(fs.areFunctions(p, [
Simon Hunt5724fb42015-02-05 16:59:40 -080094 'show', 'hide', 'toggle', 'empty', 'append',
Simon Hunt8d47a5c2016-06-15 12:56:50 -070095 'width', 'height', 'bbox', 'isVisible', 'classed', 'el'
Simon Hunt4b668592015-01-29 17:33:53 -080096 ])).toBeTruthy();
97 });
98
Simon Hunt54442fa2015-01-26 14:17:38 -080099 it('should complain when a duplicate ID is used', function () {
100 spyOn($log, 'warn');
101 var p = ps.createPanel('foo');
102 expect(p).not.toBeNull();
103 expect($log.warn).not.toHaveBeenCalled();
104 expect(floatPanelSelection().size()).toBe(1);
105
106 var dup = ps.createPanel('foo');
107 expect(dup).toBeNull();
108 expect($log.warn).toHaveBeenCalledWith('Panel with ID "foo" already exists');
109 expect(floatPanelSelection().size()).toBe(1);
110 });
111
Matteo Scandolo231c7542016-05-20 11:13:11 -0700112 it('should note when there is no panel to destroy', function () {
Simon Hunt54442fa2015-01-26 14:17:38 -0800113 spyOn($log, 'debug');
114 ps.destroyPanel('bar');
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800115 expect($log.debug).toHaveBeenCalledWith('no panel to destroy:', 'bar');
Simon Hunt54442fa2015-01-26 14:17:38 -0800116 });
117
Matteo Scandolo231c7542016-05-20 11:13:11 -0700118 it('should destroy the panel', function () {
Simon Hunt54442fa2015-01-26 14:17:38 -0800119 spyOn($log, 'debug');
120 var p = ps.createPanel('foo');
121 expect(floatPanelSelection().size()).toBe(1);
122
123 ps.destroyPanel('foo');
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800124 expect($log.debug).toHaveBeenCalledWith('destroying panel:', 'foo');
Simon Hunt54442fa2015-01-26 14:17:38 -0800125 expect(floatPanelSelection().size()).toBe(0);
126 });
127
Matteo Scandolo231c7542016-05-20 11:13:11 -0700128 it('should allow alternate settings to be given', function () {
Simon Hunt54442fa2015-01-26 14:17:38 -0800129 spyOn($log, 'debug');
130 var p = ps.createPanel('foo', { width: 250, edge: 'left' });
131 expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', {
132 edge: 'left',
133 width: 250,
Simon Hunt54442fa2015-01-26 14:17:38 -0800134 margin: 20,
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800135 hideMargin: 20,
136 xtnTime: 750,
137 fade: true
Simon Hunt54442fa2015-01-26 14:17:38 -0800138 });
139 });
140
141 it('should show and hide the panel', function () {
142 var p = ps.createPanel('foo', {xtnTime:0});
143 expect(p.isVisible()).toBeFalsy();
144
145 p.show();
146 expect(p.isVisible()).toBeTruthy();
147
148 p.hide();
149 expect(p.isVisible()).toBeFalsy();
150 });
151
152 it('should append content to the panel', function () {
153 var p = ps.createPanel('foo');
154 var span = p.append('span').attr('id', 'thisIsMySpan');
155
156 expect(floatPanelSelection().selectAll('span').attr('id'))
157 .toEqual('thisIsMySpan');
158 });
159
160 it('should remove content on empty', function () {
161 var p = ps.createPanel('voop');
162 p.append('span');
163 p.append('span');
164 p.append('span');
165 expect(floatPanelSelection().selectAll('span').size()).toEqual(3);
166
167 p.empty();
168 expect(floatPanelSelection().selectAll('span').size()).toEqual(0);
169 expect(floatPanelSelection().html()).toEqual('');
170 });
171
172 it('should allow programmatic setting of width', function () {
173 var p = ps.createPanel('whatcha', {width:234});
174 expect(floatPanelSelection().style('width')).toEqual('234px');
175 expect(p.width()).toEqual(234);
176
177 p.width(345);
178 expect(floatPanelSelection().style('width')).toEqual('345px');
179 expect(p.width()).toEqual(345);
180 });
181
182 it('should allow programmatic setting of height', function () {
183 var p = ps.createPanel('ciao', {height:50});
184 expect(floatPanelSelection().style('height')).toEqual('50px');
185 expect(p.height()).toEqual(50);
186
187 p.height(100);
188 expect(floatPanelSelection().style('height')).toEqual('100px');
189 expect(p.height()).toEqual(100);
190 });
Simon Hunt988934e2015-01-23 11:49:24 -0800191});