blob: 1d33e4f0909dba7740e96ad36d2d60ac27127b8f [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,
75 height: 80,
76 margin: 20,
77 xtnTime: 750
78 });
79
80 // check basic properties
81 expect(p.width()).toEqual(200);
82 expect(p.isVisible()).toBeFalsy();
83
84 var el = floatPanelSelection();
85 expect(el.style('width')).toEqual('200px');
86 });
87
88 it('should complain when a duplicate ID is used', function () {
89 spyOn($log, 'warn');
90 var p = ps.createPanel('foo');
91 expect(p).not.toBeNull();
92 expect($log.warn).not.toHaveBeenCalled();
93 expect(floatPanelSelection().size()).toBe(1);
94
95 var dup = ps.createPanel('foo');
96 expect(dup).toBeNull();
97 expect($log.warn).toHaveBeenCalledWith('Panel with ID "foo" already exists');
98 expect(floatPanelSelection().size()).toBe(1);
99 });
100
101 it('should note when there is no panel to destroy', function () {
102 spyOn($log, 'debug');
103 ps.destroyPanel('bar');
104 expect($log.debug).toHaveBeenCalledWith('no panel to destroy:', 'bar')
105 });
106
107 it('should destroy the panel', function () {
108 spyOn($log, 'debug');
109 var p = ps.createPanel('foo');
110 expect(floatPanelSelection().size()).toBe(1);
111
112 ps.destroyPanel('foo');
113 expect($log.debug).toHaveBeenCalledWith('destroying panel:', 'foo')
114 expect(floatPanelSelection().size()).toBe(0);
115 });
116
117 it('should allow alternate settings to be given', function () {
118 spyOn($log, 'debug');
119 var p = ps.createPanel('foo', { width: 250, edge: 'left' });
120 expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', {
121 edge: 'left',
122 width: 250,
123 height: 80,
124 margin: 20,
125 xtnTime: 750
126 });
127 });
128
129 it('should show and hide the panel', function () {
130 var p = ps.createPanel('foo', {xtnTime:0});
131 expect(p.isVisible()).toBeFalsy();
132
133 p.show();
134 expect(p.isVisible()).toBeTruthy();
135
136 p.hide();
137 expect(p.isVisible()).toBeFalsy();
138 });
139
140 it('should append content to the panel', function () {
141 var p = ps.createPanel('foo');
142 var span = p.append('span').attr('id', 'thisIsMySpan');
143
144 expect(floatPanelSelection().selectAll('span').attr('id'))
145 .toEqual('thisIsMySpan');
146 });
147
148 it('should remove content on empty', function () {
149 var p = ps.createPanel('voop');
150 p.append('span');
151 p.append('span');
152 p.append('span');
153 expect(floatPanelSelection().selectAll('span').size()).toEqual(3);
154
155 p.empty();
156 expect(floatPanelSelection().selectAll('span').size()).toEqual(0);
157 expect(floatPanelSelection().html()).toEqual('');
158 });
159
160 it('should allow programmatic setting of width', function () {
161 var p = ps.createPanel('whatcha', {width:234});
162 expect(floatPanelSelection().style('width')).toEqual('234px');
163 expect(p.width()).toEqual(234);
164
165 p.width(345);
166 expect(floatPanelSelection().style('width')).toEqual('345px');
167 expect(p.width()).toEqual(345);
168 });
169
170 it('should allow programmatic setting of height', function () {
171 var p = ps.createPanel('ciao', {height:50});
172 expect(floatPanelSelection().style('height')).toEqual('50px');
173 expect(p.height()).toEqual(50);
174
175 p.height(100);
176 expect(floatPanelSelection().style('height')).toEqual('100px');
177 expect(p.height()).toEqual(100);
178 });
Simon Hunt988934e2015-01-23 11:49:24 -0800179});