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