blob: 8637258136aaec2d0cc0d57c9667eda2016c31e9 [file] [log] [blame]
Simon Hunt988934e2015-01-23 11:49:24 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
Simon Hunt54442fa2015-01-26 14:17:38 -080030 d3Elem = d3.select('body').append('div').attr('id', 'floatpanels');
31 ps.init();
Simon Hunt988934e2015-01-23 11:49:24 -080032 }));
33
Simon Hunt54442fa2015-01-26 14:17:38 -080034 afterEach(function () {
35 d3.select('#floatpanels').remove();
36 ps.init();
37 });
38
39 function floatPanelSelection() {
40 return d3Elem.selectAll('.floatpanel');
41 }
Simon Hunt988934e2015-01-23 11:49:24 -080042
43 it('should define PanelService', function () {
44 expect(ps).toBeDefined();
45 });
46
47 it('should define api functions', function () {
48 expect(fs.areFunctions(ps, [
Simon Hunt54442fa2015-01-26 14:17:38 -080049 'init', 'createPanel', 'destroyPanel'
Simon Hunt988934e2015-01-23 11:49:24 -080050 ])).toBeTruthy();
51 });
52
Simon Hunt54442fa2015-01-26 14:17:38 -080053 it('should have no panels to start', function () {
54 expect(floatPanelSelection().size()).toBe(0);
55 });
56
57 it('should log a warning if no ID is given', function () {
58 spyOn($log, 'warn');
59 var p = ps.createPanel();
60 expect(p).toBeNull();
61 expect($log.warn).toHaveBeenCalledWith('createPanel: no ID given');
62 expect(floatPanelSelection().size()).toBe(0);
63 });
64
65 it('should create a default panel', function () {
66 spyOn($log, 'warn');
67 spyOn($log, 'debug');
68 var p = ps.createPanel('foo');
69 expect(p).not.toBeNull();
70 expect($log.warn).not.toHaveBeenCalled();
71 expect(floatPanelSelection().size()).toBe(1);
72 expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', {
73 edge: 'right',
74 width: 200,
Simon Hunt54442fa2015-01-26 14:17:38 -080075 margin: 20,
76 xtnTime: 750
77 });
78
79 // check basic properties
80 expect(p.width()).toEqual(200);
81 expect(p.isVisible()).toBeFalsy();
82
83 var el = floatPanelSelection();
84 expect(el.style('width')).toEqual('200px');
85 });
86
Simon Hunt4b668592015-01-29 17:33:53 -080087 it('should provide an api of panel functions', function () {
88 var p = ps.createPanel('foo');
89 expect(fs.areFunctions(p, [
Simon Hunt5724fb42015-02-05 16:59:40 -080090 'show', 'hide', 'toggle', 'empty', 'append',
Simon Hunt08f841d02015-02-10 14:39:20 -080091 'width', 'height', 'isVisible', 'classed', 'el'
Simon Hunt4b668592015-01-29 17:33:53 -080092 ])).toBeTruthy();
93 });
94
Simon Hunt54442fa2015-01-26 14:17:38 -080095 it('should complain when a duplicate ID is used', function () {
96 spyOn($log, 'warn');
97 var p = ps.createPanel('foo');
98 expect(p).not.toBeNull();
99 expect($log.warn).not.toHaveBeenCalled();
100 expect(floatPanelSelection().size()).toBe(1);
101
102 var dup = ps.createPanel('foo');
103 expect(dup).toBeNull();
104 expect($log.warn).toHaveBeenCalledWith('Panel with ID "foo" already exists');
105 expect(floatPanelSelection().size()).toBe(1);
106 });
107
108 it('should note when there is no panel to destroy', function () {
109 spyOn($log, 'debug');
110 ps.destroyPanel('bar');
111 expect($log.debug).toHaveBeenCalledWith('no panel to destroy:', 'bar')
112 });
113
114 it('should destroy the panel', function () {
115 spyOn($log, 'debug');
116 var p = ps.createPanel('foo');
117 expect(floatPanelSelection().size()).toBe(1);
118
119 ps.destroyPanel('foo');
120 expect($log.debug).toHaveBeenCalledWith('destroying panel:', 'foo')
121 expect(floatPanelSelection().size()).toBe(0);
122 });
123
124 it('should allow alternate settings to be given', function () {
125 spyOn($log, 'debug');
126 var p = ps.createPanel('foo', { width: 250, edge: 'left' });
127 expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', {
128 edge: 'left',
129 width: 250,
Simon Hunt54442fa2015-01-26 14:17:38 -0800130 margin: 20,
131 xtnTime: 750
132 });
133 });
134
135 it('should show and hide the panel', function () {
136 var p = ps.createPanel('foo', {xtnTime:0});
137 expect(p.isVisible()).toBeFalsy();
138
139 p.show();
140 expect(p.isVisible()).toBeTruthy();
141
142 p.hide();
143 expect(p.isVisible()).toBeFalsy();
144 });
145
146 it('should append content to the panel', function () {
147 var p = ps.createPanel('foo');
148 var span = p.append('span').attr('id', 'thisIsMySpan');
149
150 expect(floatPanelSelection().selectAll('span').attr('id'))
151 .toEqual('thisIsMySpan');
152 });
153
154 it('should remove content on empty', function () {
155 var p = ps.createPanel('voop');
156 p.append('span');
157 p.append('span');
158 p.append('span');
159 expect(floatPanelSelection().selectAll('span').size()).toEqual(3);
160
161 p.empty();
162 expect(floatPanelSelection().selectAll('span').size()).toEqual(0);
163 expect(floatPanelSelection().html()).toEqual('');
164 });
165
166 it('should allow programmatic setting of width', function () {
167 var p = ps.createPanel('whatcha', {width:234});
168 expect(floatPanelSelection().style('width')).toEqual('234px');
169 expect(p.width()).toEqual(234);
170
171 p.width(345);
172 expect(floatPanelSelection().style('width')).toEqual('345px');
173 expect(p.width()).toEqual(345);
174 });
175
176 it('should allow programmatic setting of height', function () {
177 var p = ps.createPanel('ciao', {height:50});
178 expect(floatPanelSelection().style('height')).toEqual('50px');
179 expect(p.height()).toEqual(50);
180
181 p.height(100);
182 expect(floatPanelSelection().style('height')).toEqual('100px');
183 expect(p.height()).toEqual(100);
184 });
Simon Hunt988934e2015-01-23 11:49:24 -0800185});