blob: 30eec655de8b91cdcde58f5ae662308ef45cb7d5 [file] [log] [blame]
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -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 -- Widget -- Toolbar Service - Unit Tests
19 */
20describe('factory: fw/widget/toolbar.js', function () {
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080021 var $log, fs, tbs, ps,
22 d3Elem;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080023
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080024 beforeEach(module('onosWidget', 'onosUtil', 'onosLayer'));
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080025
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080026 beforeEach(inject(function (_$log_, FnService,
27 ToolbarService, PanelService) {
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080028 $log = _$log_;
29 fs = FnService;
30 tbs = ToolbarService;
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080031 ps = PanelService;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080032 }));
33
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080034 beforeEach(function () {
35 d3Elem = d3.select('body').append('div').attr('id', 'floatpanels');
36 tbs.init();
37 ps.init();
38 });
39
40 afterEach(function () {
41 d3.select('#floatpanels').remove();
42 tbs.init();
43 ps.init();
44 });
45
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080046 it('should define ToolbarService', function () {
47 expect(tbs).toBeDefined();
48 });
49
50 it('should define api functions', function () {
51 expect(fs.areFunctions(tbs, [
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080052 'init', 'makeButton', 'makeToggle', 'makeRadio', 'separator',
53 'createToolbar'
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080054 ])).toBeTruthy();
55 });
56
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080057 function toolbarSelection() {
58 return d3Elem.selectAll('.toolbar');
59 }
60
61 it('should have no toolbars to start', function () {
62 expect(toolbarSelection().size()).toBe(0);
63 });
64
65 it('should log a warning if no ID is given', function () {
66 spyOn($log, 'warn');
67 var tbar = tbs.createToolbar();
68 expect(tbar).toBeNull();
69 expect($log.warn).toHaveBeenCalledWith('createToolbar: no ID given');
70 expect(toolbarSelection().size()).toBe(0);
71 });
72
73 it('should log a warning if no tools are given', function () {
74 spyOn($log, 'warn');
75 var tbar = tbs.createToolbar(true);
76 expect(tbar).toBeNull();
77 expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
78 expect(toolbarSelection().size()).toBe(0);
79 });
80
81 it('should log a warning if tools are not an array', function () {
82 spyOn($log, 'warn');
83 var tbar = tbs.createToolbar(true, {});
84 expect(tbar).toBeNull();
85 expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
86 expect(toolbarSelection().size()).toBe(0);
87 });
88
89 it('should log a warning if tools array is empty', function () {
90 spyOn($log, 'warn');
91 var tbar = tbs.createToolbar(true, []);
92 expect(tbar).toBeNull();
93 expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
94 expect(toolbarSelection().size()).toBe(0);
95 });
96
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080097 it("should verify makeButton's returned object", function () {
98 var button = tbs.makeButton('foo', 'glyph-bar', function () {});
99
100 expect(button.t).toBe('btn');
101 expect(button.id).toBe('foo');
102 expect(button.gid).toBe('glyph-bar');
103 expect(fs.isF(button.cb)).toBeTruthy();
104 });
105
106 it("should verify makeToggle's returned object", function () {
107 var toggle = tbs.makeToggle('foo', 'glyph-bar', function () {});
108
109 expect(toggle.t).toBe('tog');
110 expect(toggle.id).toBe('foo');
111 expect(toggle.gid).toBe('glyph-bar');
112 expect(fs.isF(toggle.cb)).toBeTruthy();
113 });
114
115 it("should verify makeRadio's returned object", function () {
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800116 var rFoo0 = tbs.makeRadio('foo', 'glyph-foo0', function () {});
117 var rFoo1 = tbs.makeRadio('foo', 'glyph-foo1', function () {});
118 var rFoo2 = tbs.makeRadio('foo', 'glyph-foo2', function () {});
119 var rBar0 = tbs.makeRadio('bar', 'glyph-bar0', function () {});
120 var rBar1 = tbs.makeRadio('bar', 'glyph-bar1', function () {});
121 var rFoo3 = tbs.makeRadio('foo', 'glyph-foo3', function () {});
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800122
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800123 expect(rFoo0.t).toBe('rad');
124 expect(rFoo0.id).toBe('foo-0');
125 expect(rFoo0.rid).toBe('0');
126 expect(rFoo0.gid).toBe('glyph-foo0');
127 expect(fs.isF(rFoo0.cb)).toBeTruthy();
128
129 expect(rFoo1.t).toBe('rad');
130 expect(rFoo1.id).toBe('foo-1');
131 expect(rFoo1.rid).toBe('1');
132 expect(rFoo1.gid).toBe('glyph-foo1');
133 expect(fs.isF(rFoo1.cb)).toBeTruthy();
134
135 expect(rFoo2.t).toBe('rad');
136 expect(rFoo2.id).toBe('foo-2');
137 expect(rFoo2.rid).toBe('2');
138 expect(rFoo2.gid).toBe('glyph-foo2');
139 expect(fs.isF(rFoo2.cb)).toBeTruthy();
140
141 expect(rFoo3.t).toBe('rad');
142 expect(rFoo3.id).toBe('foo-3');
143 expect(rFoo3.rid).toBe('3');
144 expect(rFoo3.gid).toBe('glyph-foo3');
145 expect(fs.isF(rFoo3.cb)).toBeTruthy();
146
147 expect(rBar0.t).toBe('rad');
148 expect(rBar0.id).toBe('bar-0');
149 expect(rBar0.rid).toBe('0');
150 expect(rBar0.gid).toBe('glyph-bar0');
151 expect(fs.isF(rBar0.cb)).toBeTruthy();
152
153 expect(rBar1.t).toBe('rad');
154 expect(rBar1.id).toBe('bar-1');
155 expect(rBar1.rid).toBe('1');
156 expect(rBar1.gid).toBe('glyph-bar1');
157 expect(fs.isF(rBar1.cb)).toBeTruthy();
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800158 });
159
160 it("should verify separator's returned object", function () {
161 var separator = tbs.separator();
162 expect(separator.t).toBe('sep');
163 });
164
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800165 it('should log a warning if btn id is already in use', function () {
166 var tools = [
167 tbs.makeButton('id0', 'glyph-id0', function () {}),
168 tbs.makeButton('id0', 'glyph-id0', function () {})
169 ];
170
171 spyOn($log, 'warn');
172 var tbar = tbs.createToolbar('someId', tools);
173 expect(tbar).toBeNull();
174 expect($log.warn).toHaveBeenCalledWith('createToolbar: item with id ' +
175 'id0 already exists');
176 expect(toolbarSelection().size()).toBe(0);
177 });
178
179 it('should log a warning if tog id is already in use', function () {
180 var tools = [
181 tbs.makeToggle('id0', 'glyph-id0', function () {}),
182 tbs.makeToggle('id1', 'glyph-id1', function () {}),
183 tbs.makeToggle('id0', 'glyph-id0', function () {})
184 ];
185
186 spyOn($log, 'warn');
187 var tbar = tbs.createToolbar('someId', tools);
188 expect(tbar).toBeNull();
189 expect($log.warn).toHaveBeenCalledWith('createToolbar: item with id ' +
190 'id0 already exists');
191 expect(toolbarSelection().size()).toBe(0);
192 });
193
194 it('should create a toolbar', function () {
195 // need to create a button so it does not throw errors
196 var tools = [
197 tbs.makeButton('btn0', 'glyph0', function () {})
198 ];
199 spyOn($log, 'warn');
200 var tbar = tbs.createToolbar('test', tools);
201 expect($log.warn).not.toHaveBeenCalled();
202 expect(toolbarSelection().size()).toBe(1);
203 });
204
205 it('should test multiple separators in a row', function () {
206 var tools = [
207 tbs.separator(),
208 tbs.separator(),
209 tbs.separator()
210 ];
211 spyOn($log, 'warn');
212 var tbar = tbs.createToolbar('test', tools);
213 expect($log.warn).not.toHaveBeenCalled();
214 expect(toolbarSelection().size()).toBe(1);
215 });
216
217 it('should create a button div', function () {
218 var tools = [
219 tbs.makeButton('btn0', 'glyph0', function () {})
220 ];
221 spyOn($log, 'warn');
222 var tbar = tbs.createToolbar('test', tools);
223 expect($log.warn).not.toHaveBeenCalled();
224 expect(toolbarSelection().size()).toBe(1);
225
226 expect(d3Elem.select('.btn')).toBeTruthy();
227 });
228
229 it('should create a toggle div', function () {
230 var tools = [
231 tbs.makeToggle('tog0', 'glyph0', function () {})
232 ];
233 spyOn($log, 'warn');
234 var tbar = tbs.createToolbar('test', tools);
235 expect($log.warn).not.toHaveBeenCalled();
236 expect(toolbarSelection().size()).toBe(1);
237
238 expect(d3Elem.select('.tog')).toBeTruthy();
239 });
240
241 it('should create a radio btn div', function () {
242 var tools = [
243 tbs.makeRadio('rad0', 'glyph0', function () {})
244 ];
245 spyOn($log, 'warn');
246 var tbar = tbs.createToolbar('test', tools);
247 expect($log.warn).not.toHaveBeenCalled();
248 expect(toolbarSelection().size()).toBe(1);
249
250 expect(d3Elem.select('.rad')).toBeTruthy();
251 });
252
253
254 it('should create a separator div', function () {
255 var tools = [
256 tbs.separator()
257 ];
258 tbs.createToolbar('test', tools);
259
260 var sepDiv = d3Elem.select('.sep');
261 expect(sepDiv).toBeTruthy();
262 expect(sepDiv.style('width')).toBe('2px');
263 expect(sepDiv.style('border-width')).toBe('1px');
264 expect(sepDiv.style('border-style')).toBe('solid');
265 });
266
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800267});