blob: 5226984dcecbb3516e0bfc6ecb1d4786ec25c16d [file] [log] [blame]
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -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 -- Button Service - Unit Tests
19 */
20describe('factory: fw/widget/button.js', function () {
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080021 var $log, fs, bns, d3Elem;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080022
23 beforeEach(module('onosWidget', 'onosSvg'));
24
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080025 beforeEach(inject(function (_$log_, FnService, ButtonService) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080026 $log = _$log_;
27 fs = FnService;
28 bns = ButtonService;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080029 }));
30
31 beforeEach(function () {
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080032 d3Elem = d3.select('body').append('div').attr('id', 'testDiv');
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080033 });
34
35 afterEach(function () {
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080036 d3.select('#testDiv').remove();
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080037 });
38
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080039
40 // re-usable null function
41 function nullFunc () {}
42
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080043 it('should define ButtonService', function () {
44 expect(bns).toBeDefined();
45 });
46
47 it('should define api functions', function () {
48 expect(fs.areFunctions(bns, [
49 'button', 'toggle', 'radioSet'
50 ])).toBeTruthy();
51 });
52
53 it('should verify button glyph', function () {
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080054 var btn = bns.button(d3Elem, 'foo-id', 'crown', nullFunc);
55 var el = d3Elem.select('#foo-id');
56 expect(el.classed('button')).toBeTruthy();
57 expect(el.attr('id')).toBe('foo-id');
58 expect(el.select('svg')).toBeTruthy();
59 expect(el.select('use')).toBeTruthy();
60 expect(el.select('use').classed('glyph')).toBeTruthy();
61 expect(el.select('use').attr('xlink:href')).toBe('#crown');
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080062 });
63
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080064
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080065 it('should not append button to an undefined div', function () {
66 spyOn($log, 'warn');
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080067 expect(bns.button(null, 'id', 'gid', nullFunc)).toBeNull();
68 expect($log.warn).toHaveBeenCalledWith('div undefined (button)');
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080069 });
70
71 it('should verify button callback', function () {
72 var count = 0;
73 function cb() { count++; }
74 var btn = bns.button(d3Elem, 'test', 'nothing', cb);
75 expect(count).toBe(0);
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080076 d3Elem.select('#test').on('click')();
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080077 expect(count).toBe(1);
78 });
79
80 it('should ignore non-function callbacks button', function () {
81 var count = 0;
82 var btn = bns.button(d3Elem, 'test', 'nothing', 'foo');
83 expect(count).toBe(0);
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080084 d3Elem.select('#test').on('click')();
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080085 expect(count).toBe(0);
86 });
87
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080088 it('should not append toggle to an undefined div', function () {
89 spyOn($log, 'warn');
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080090 expect(bns.toggle(undefined, 'id', 'gid', false, nullFunc)).toBeNull();
91 expect($log.warn).toHaveBeenCalledWith('div undefined (toggle button)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080092 });
93
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080094 it('should verify toggle glyph', function () {
Simon Hunt7c7dd3e2015-02-27 11:42:18 -080095 var tog = bns.toggle(d3Elem, 'foo-id', 'crown', false, nullFunc);
96 var el = d3Elem.select('#foo-id');
97 expect(el.classed('toggleButton')).toBeTruthy();
98 expect(el.attr('id')).toBe('foo-id');
99 expect(el.select('svg')).toBeTruthy();
100 expect(el.select('use')).toBeTruthy();
101 expect(el.select('use').classed('glyph')).toBeTruthy();
102 expect(el.select('use').attr('xlink:href')).toBe('#crown');
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800103 });
104
105 it('should toggle the selected state', function () {
106 var tog = bns.toggle(d3Elem, 'test', 'nothing');
107 expect(tog.selected()).toBe(false);
108 tog.toggle();
109 expect(tog.selected()).toBe(true);
110 tog.toggle();
111 expect(tog.selected()).toBe(false);
112 });
113
114 it('should set toggle state', function () {
115 var tog = bns.toggle(d3Elem, 'test', 'nothing');
116 tog.toggle(true);
117 expect(tog.selected()).toBe(true);
118 tog.toggle();
119 expect(tog.selected()).toBe(false);
120 tog.toggle('truthy string');
121 expect(tog.selected()).toBe(true);
122 tog.toggle(null);
123 expect(tog.selected()).toBe(false);
124 tog.toggle('');
125 expect(tog.selected()).toBe(false);
126 });
127
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800128 it('should verity toggle initial state', function () {
129 var tog = bns.toggle(d3Elem, 'id', 'gid', true);
130 expect(tog.selected()).toBe(true);
131 tog = bns.toggle(d3Elem, 'id', 'gid', false);
132 expect(tog.selected()).toBe(false);
133 tog = bns.toggle(d3Elem, 'id', 'gid', '');
134 expect(tog.selected()).toBe(false);
135 tog = bns.toggle(d3Elem, 'id', 'gid', 'something');
136 expect(tog.selected()).toBe(true);
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800137 });
138
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800139 it('should not append radio button set to an undefined div', function () {
140 spyOn($log, 'warn');
141 expect(bns.radioSet(undefined, 'id', [])).toBeNull();
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800142 expect($log.warn).toHaveBeenCalledWith('div undefined (radio button set)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800143 });
144
145 it('should not create radio button set from a non-array', function () {
146 var rads = {test: 'test'};
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800147 var warning = 'invalid array (radio button set)';
148
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800149 spyOn($log, 'warn');
150
151 expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull();
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800152 expect($log.warn).toHaveBeenCalledWith(warning);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800153 rads = 'rads';
154 expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull();
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800155 expect($log.warn).toHaveBeenCalledWith(warning);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800156 rads = {arr: [1, 2, 3]};
157 expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull();
Simon Hunt7c7dd3e2015-02-27 11:42:18 -0800158 expect($log.warn).toHaveBeenCalledWith(warning);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800159 });
160
161 it('should not create radio button set from empty array', function () {
162 var rads = [];
163 spyOn($log, 'warn');
164 expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull();
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800165 expect($log.warn).toHaveBeenCalledWith('invalid array (radio button set)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800166 });
167
168 it('should verify radio button glyph structure', function () {
169 var rads = [
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800170 { gid: 'crown', cb: nullFunc, tooltip: 'n/a'}
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800171 ], rdiv;
172
173 spyOn($log, 'warn');
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800174 expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy();
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800175 expect($log.warn).not.toHaveBeenCalled();
176
177 rdiv = d3Elem.select('div');
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800178 expect(rdiv.classed('radioSet')).toBe(true);
179 expect(rdiv.select('div').classed('radioButton')).toBe(true);
180 expect(rdiv.select('div').attr('id')).toBe('foo-0');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800181 expect(rdiv.select('div').select('svg')).toBeTruthy();
182 expect(rdiv.select('use').classed('glyph')).toBeTruthy();
183 expect(rdiv.select('use').attr('xlink:href')).toBe('#crown');
184 });
185
186 it('should verify more than one radio button glyph was added', function () {
187 var rads = [
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800188 { gid: 'crown', cb: nullFunc, tooltip: 'n/a'},
189 { gid: 'router', cb: nullFunc, tooltip: 'n/a'}
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800190 ], rdiv;
191
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800192 expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy();
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800193 rdiv = d3Elem.select('div');
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800194 expect(rdiv.select('#foo-0')).toBeTruthy();
195 expect(rdiv.select('#foo-1')).toBeTruthy();
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800196
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800197 expect(rdiv.select('#foo-0')
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800198 .select('use')
199 .classed('glyph'))
200 .toBeTruthy();
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800201 expect(rdiv.select('#foo-0')
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800202 .select('use')
203 .attr('xlink:href'))
204 .toBe('#crown');
205
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800206 expect(rdiv.select('#foo-1')
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800207 .select('use')
208 .classed('glyph'))
209 .toBeTruthy();
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800210 expect(rdiv.select('#foo-1')
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800211 .select('use')
212 .attr('xlink:href'))
213 .toBe('#router');
214 });
215
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800216 it('should select radio button by index', function () {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800217 var count0 = 0,
218 count1 = 9;
219 function cb0() { count0++; }
220 function cb1() { count1++; }
221
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800222 function validate(expSel, exp0, exp1) {
223 expect(rset.selected()).toBe(expSel);
224 expect(count0).toBe(exp0);
225 expect(count1).toBe(exp1);
226 }
227
228 function checkWarn(msg, index) {
229 expect($log.warn).toHaveBeenCalledWith(msg, index);
230 }
231
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800232 var rads = [
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800233 { gid: 'crown', cb: cb0, tooltip: 'n/a'},
234 { gid: 'router', cb: cb1, tooltip: 'n/a'}
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800235 ],
236 rset = bns.radioSet(d3Elem, 'test', rads);
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800237 spyOn($log, 'warn');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800238
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800239 validate(0, 0, 9);
240 rset.selectedIndex(0);
241 validate(0, 0, 9);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800242
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800243 rset.selectedIndex(1);
244 validate(1, 0, 10);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800245
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800246 rset.selectedIndex(-1);
247 checkWarn('invalid radio button index:', -1);
248 validate(1, 0, 10);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800249
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800250 rset.selectedIndex(66);
251 checkWarn('invalid radio button index:', 66);
252 validate(1, 0, 10);
253
254 rset.selectedIndex(0);
255 validate(0, 1, 10);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800256 });
257
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800258 it('should select radio button by key', function () {
259 var count0 = 0,
260 count1 = 9;
261 function cb0() { count0++; }
262 function cb1() { count1++; }
263
264 function validate(expSel, exp0, exp1) {
265 expect(rset.selected()).toBe(expSel);
266 expect(count0).toBe(exp0);
267 expect(count1).toBe(exp1);
268 }
269
270 function checkWarn(msg, index) {
271 expect($log.warn).toHaveBeenCalledWith(msg, index);
272 }
273
274 var rads = [
275 { key: 'foo', gid: 'crown', cb: cb0, tooltip: 'n/a'},
276 { key: 'bar', gid: 'router', cb: cb1, tooltip: 'n/a'}
277 ],
278 rset = bns.radioSet(d3Elem, 'test', rads);
279 spyOn($log, 'warn');
280
281 validate('foo', 0, 9);
282 rset.selected('foo');
283 validate('foo', 0, 9);
284
285 rset.selected('bar');
286 validate('bar', 0, 10);
287
288 rset.selected('blob');
289 checkWarn('no radio button with key:', 'blob');
290 validate('bar', 0, 10);
291
292 rset.selected('foo');
293 validate('foo', 1, 10);
294
295 rset.selected('foo');
296 validate('foo', 1, 10);
297 checkWarn('current index already selected:', 0);
298 });
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800299
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800300});