blob: 2f168aecd6bf911719d0f41323e5d613f5fbeb06 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -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 -- SVG -- Glyph Service - Unit Tests
Simon Hunt7ac7be92015-01-06 10:47:56 -080019 */
20describe('factory: fw/svg/glyph.js', function() {
Simon Huntc9b73162015-01-29 14:02:15 -080021 var $log, fs, gs, d3Elem, svg;
Simon Hunt7ac7be92015-01-06 10:47:56 -080022
Simon Hunt9c1c45e2015-04-10 13:38:27 -070023 var numBaseGlyphs = 36,
Simon Huntbc39f6d2015-01-06 17:34:28 -080024 vbBird = '352 224 113 112',
Simon Hunt6e459802015-01-06 15:05:42 -080025 vbGlyph = '0 0 110 110',
Simon Hunt58f23bb2015-01-16 16:32:24 -080026 vbBadge = '0 0 10 10',
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070027 longPrefix = 'M95.8,9.2H14.2c-2.8,0-5,2.2-5,5v81.5c0,2.8,2.2,5,5,' +
28 '5h81.5c2.8,0,5-2.2,5-5V14.2C100.8,11.5,98.5,9.2,95.8,9.2z ',
Simon Hunt58f23bb2015-01-16 16:32:24 -080029 prefixLookup = {
30 bird: 'M427.7,300.4',
31 unknown: 'M35,40a5',
32 node: 'M15,100a5',
33 switch: 'M10,20a10',
34 roadm: 'M10,35l25-',
35 endstation: 'M10,15a5,5',
36 router: 'M10,55A45,45',
37 bgpSpeaker: 'M10,40a45,35',
38 chain: 'M60.4,77.6c-',
39 crown: 'M99.5,21.6c0,',
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070040 lock: 'M79.4,48.6h',
41
42 // toolbar specific glyphs
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070043 summary: longPrefix + 'M16.7',
44 details: longPrefix + 'M16.9',
45 ports: 'M98,9.2H79.6c',
46 map: 'M95.8,9.2H14.2c-2.8,0-5,2.2-5,5v66',
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070047 cycleLabels: 'M72.5,33.9c',
48 oblique: 'M80.9,30.2h',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070049 filters: 'M24.8,13.3L',
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070050 resetZoom: 'M86,79.8L',
51 relatedIntents: 'M99.9,43.7',
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -070052 nextIntent: 'M88.1,55.7',
53 prevIntent: 'M22.5,55.6',
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070054 intentTraffic: 'M14.7,71.5h',
55 allTraffic: 'M15.7,64.5h-7v',
56 flows: 'M93.8,46.1c',
Bri Prebilic Cole5a206bb2015-03-25 16:33:27 -070057 eqMaster: 'M100.1,46.9l',
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070058
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070059 // badges
Simon Hunt58f23bb2015-01-16 16:32:24 -080060 uiAttached: 'M2,2.5a.5,.5',
Simon Huntaa26adf2015-01-20 10:32:49 -080061 checkMark: 'M2.6,4.5c0',
62 xMark: 'M9.0,7.2C8.2',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080063 triangleUp: 'M0.5,6.2c0',
64 triangleDown: 'M9.5,4.2c0',
Simon Hunt3074fb22015-03-31 15:06:25 -070065 plus: 'M4,2h2v2h2v2',
66 minus: 'M2,4h6v2',
67 play: 'M2.5,2l5.5,3',
68 stop: 'M2.5,2.5h5',
Simon Hunt58f23bb2015-01-16 16:32:24 -080069
Simon Hunt9c1c45e2015-04-10 13:38:27 -070070 cloud: 'M37.6,79.5c-6.9,8.7-20.4,8.6',
71
Simon Hunt58f23bb2015-01-16 16:32:24 -080072 // our test ones..
73 triangle: 'M.5,.2',
74 diamond: 'M.2,.5'
Simon Hunt3074fb22015-03-31 15:06:25 -070075 },
76 glyphIds = [
77 'unknown', 'node', 'switch', 'roadm', 'endstation', 'router',
78 'bgpSpeaker', 'chain', 'crown', 'lock',
79 'summary', 'details', 'ports', 'map', 'cycleLabels', 'oblique',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070080 'filters', 'resetZoom', 'relatedIntents', 'nextIntent',
81 'prevIntent', 'intentTraffic', 'allTraffic', 'flows', 'eqMaster'
Simon Hunt3074fb22015-03-31 15:06:25 -070082 ],
83 badgeIds = [
84 'uiAttached', 'checkMark', 'xMark', 'triangleUp', 'triangleDown',
85 'plus', 'minus', 'play', 'stop'
Simon Hunt9c1c45e2015-04-10 13:38:27 -070086 ],
87 spriteIds = [
88 'cloud'
Simon Hunt3074fb22015-03-31 15:06:25 -070089 ];
Simon Hunt6e459802015-01-06 15:05:42 -080090
Simon Hunt51fc40b2015-01-06 13:56:12 -080091 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080092
Simon Hunt51fc40b2015-01-06 13:56:12 -080093 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080094 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080095 $log = _$log_;
96 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080097 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080098 d3Elem = body.append('defs').attr('id', 'myDefs');
99 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -0800100 }));
101
Simon Hunt670e8252015-01-06 18:31:30 -0800102 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -0800103 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -0800104 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -0800105 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -0800106 });
107
Simon Hunt7ac7be92015-01-06 10:47:56 -0800108 it('should define GlyphService', function () {
109 expect(gs).toBeDefined();
110 });
111
Simon Hunt6e459802015-01-06 15:05:42 -0800112 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800113 expect(fs.areFunctions(gs, [
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700114 'clear', 'init', 'registerGlyphs', 'registerGlyphSet',
115 'ids', 'glyph', 'loadDefs', 'addGlyph'
116 ])).toBe(true);
Simon Hunt51fc40b2015-01-06 13:56:12 -0800117 });
118
Simon Hunt6e459802015-01-06 15:05:42 -0800119 it('should start with no glyphs loaded', function () {
120 expect(gs.ids()).toEqual([]);
121 });
122
Simon Hunt58f23bb2015-01-16 16:32:24 -0800123 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800124 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -0800125 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -0800126 });
127
Simon Hunt58f23bb2015-01-16 16:32:24 -0800128 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800129 gs.init();
130 expect(gs.ids().length).toEqual(numBaseGlyphs);
131 gs.clear();
132 expect(gs.ids().length).toEqual(0);
133 });
134
Simon Hunt58f23bb2015-01-16 16:32:24 -0800135 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
136 var pfxId = expPfxId || id,
137 glyph = gs.glyph(id),
138 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800139 plen = prefix.length;
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700140 expect(fs.contains(gs.ids(), id)).toBe(true);
Simon Hunt6e459802015-01-06 15:05:42 -0800141 expect(glyph).toBeDefined();
142 expect(glyph.id).toEqual(id);
143 expect(glyph.vb).toEqual(vbox);
144 expect(glyph.d.slice(0, plen)).toEqual(prefix);
145 }
146
Simon Hunt3074fb22015-03-31 15:06:25 -0700147 it('should be configured with the correct number of glyphs', function () {
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700148 var nGlyphs = 1 + glyphIds.length + badgeIds.length + spriteIds.length;
149 expect(nGlyphs).toEqual(numBaseGlyphs);
Simon Hunt3074fb22015-03-31 15:06:25 -0700150 });
151
Simon Hunt6e459802015-01-06 15:05:42 -0800152 it('should load the bird glyph', function() {
153 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800154 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800155 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700156
157 it('should load the regular glyphs', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800158 gs.init();
Simon Hunt3074fb22015-03-31 15:06:25 -0700159 glyphIds.forEach(function (id) {
160 verifyGlyphLoadedInCache(id, vbGlyph);
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800161 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700162 });
163
164 it('should load the badge glyphs', function () {
165 gs.init();
166 badgeIds.forEach(function (id) {
167 verifyGlyphLoadedInCache(id, vbBadge);
168 });
169 });
170
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700171 it('should load the sprites', function () {
172 gs.init();
173 spriteIds.forEach(function (id) {
174 verifyGlyphLoadedInCache(id, vbGlyph);
175 });
176 });
177
Simon Huntbc39f6d2015-01-06 17:34:28 -0800178
179 // define some glyphs that we want to install
180
181 var testVbox = '0 0 1 1',
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700182 triVbox = '0 0 12 12',
183 diaVbox = '0 0 15 15',
Simon Huntbc39f6d2015-01-06 17:34:28 -0800184 dTriangle = 'M.5,.2l.3,.6,h-.6z',
185 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
186 newGlyphs = {
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700187 _viewbox: testVbox,
Simon Huntbc39f6d2015-01-06 17:34:28 -0800188 triangle: dTriangle,
189 diamond: dDiamond
190 },
191 dupGlyphs = {
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700192 _viewbox: testVbox,
Simon Huntbc39f6d2015-01-06 17:34:28 -0800193 router: dTriangle,
194 switch: dDiamond
195 },
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700196 altNewGlyphs = {
197 _triangle: triVbox,
198 triangle: dTriangle,
199 _diamond: diaVbox,
200 diamond: dDiamond
201 },
202 altDupGlyphs = {
203 _router: triVbox,
204 router: dTriangle,
205 _switch: diaVbox,
206 switch: dDiamond
207 },
208 badGlyphSet = {
209 triangle: dTriangle,
210 diamond: dDiamond
211 },
212 warnMsg = 'GlyphService.registerGlyphs(): ',
213 warnMsgSet = 'GlyphService.registerGlyphSet(): ',
214 idCollision = warnMsg + 'ID collision: ',
215 idCollisionSet = warnMsgSet + 'ID collision: ',
216 missVbSet = warnMsgSet + 'no "_viewbox" property found',
217 missVbCustom = warnMsg + 'Missing viewbox property: ',
218 missVbTri = missVbCustom + '"_triangle"',
219 missVbDia = missVbCustom + '"_diamond"';
Simon Huntbc39f6d2015-01-06 17:34:28 -0800220
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700221
222 it('should install new glyphs as a glyph-set', function () {
Simon Huntbc39f6d2015-01-06 17:34:28 -0800223 gs.init();
224 expect(gs.ids().length).toEqual(numBaseGlyphs);
225 spyOn($log, 'warn');
226
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700227 var ok = gs.registerGlyphSet(newGlyphs);
228 expect(ok).toBe(true);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800229 expect($log.warn).not.toHaveBeenCalled();
230
231 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800232 verifyGlyphLoadedInCache('triangle', testVbox);
233 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800234 });
235
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700236 it('should not overwrite glyphs (via glyph-set) with dup IDs', function () {
237 gs.init();
238 expect(gs.ids().length).toEqual(numBaseGlyphs);
239 spyOn($log, 'warn');
240
241 var ok = gs.registerGlyphSet(dupGlyphs);
242 expect(ok).toBe(false);
243 expect($log.warn).toHaveBeenCalledWith(idCollisionSet + '"switch"');
244 expect($log.warn).toHaveBeenCalledWith(idCollisionSet + '"router"');
245
246 expect(gs.ids().length).toEqual(numBaseGlyphs);
247 // verify original glyphs still exist...
248 verifyGlyphLoadedInCache('router', vbGlyph);
249 verifyGlyphLoadedInCache('switch', vbGlyph);
250 });
251
252 it('should replace glyphs (via glyph-set) if asked nicely', function () {
253 gs.init();
254 expect(gs.ids().length).toEqual(numBaseGlyphs);
255 spyOn($log, 'warn');
256
257 var ok = gs.registerGlyphSet(dupGlyphs, true);
258 expect(ok).toBe(true);
259 expect($log.warn).not.toHaveBeenCalled();
260
261 expect(gs.ids().length).toEqual(numBaseGlyphs);
262 // verify glyphs have been overwritten...
263 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
264 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
265 });
266
267 it ('should complain if missing _viewbox in a glyph-set', function () {
268 gs.init();
269 expect(gs.ids().length).toEqual(numBaseGlyphs);
270 spyOn($log, 'warn');
271
272 var ok = gs.registerGlyphSet(badGlyphSet);
273 expect(ok).toBe(false);
274 expect($log.warn).toHaveBeenCalledWith(missVbSet);
275 expect(gs.ids().length).toEqual(numBaseGlyphs);
276 });
277
278 it('should install new glyphs', function () {
279 gs.init();
280 expect(gs.ids().length).toEqual(numBaseGlyphs);
281 spyOn($log, 'warn');
282
283 var ok = gs.registerGlyphs(altNewGlyphs);
284 expect(ok).toBe(true);
285 expect($log.warn).not.toHaveBeenCalled();
286
287 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
288 verifyGlyphLoadedInCache('triangle', triVbox);
289 verifyGlyphLoadedInCache('diamond', diaVbox);
290 });
291
Simon Huntbc39f6d2015-01-06 17:34:28 -0800292 it('should not overwrite glyphs with dup IDs', function () {
293 gs.init();
294 expect(gs.ids().length).toEqual(numBaseGlyphs);
295 spyOn($log, 'warn');
296
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700297 var ok = gs.registerGlyphs(altDupGlyphs);
298 expect(ok).toBe(false);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800299 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
300 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
301
302 expect(gs.ids().length).toEqual(numBaseGlyphs);
303 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800304 verifyGlyphLoadedInCache('router', vbGlyph);
305 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800306 });
307
308 it('should replace glyphs if asked nicely', function () {
309 gs.init();
310 expect(gs.ids().length).toEqual(numBaseGlyphs);
311 spyOn($log, 'warn');
312
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700313 var ok = gs.registerGlyphs(altDupGlyphs, true);
314 expect(ok).toBe(true);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800315 expect($log.warn).not.toHaveBeenCalled();
316
317 expect(gs.ids().length).toEqual(numBaseGlyphs);
318 // verify glyphs have been overwritten...
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700319 verifyGlyphLoadedInCache('router', triVbox, 'triangle');
320 verifyGlyphLoadedInCache('switch', diaVbox, 'diamond');
321 });
322
323 it ('should complain if missing custom viewbox', function () {
324 gs.init();
325 expect(gs.ids().length).toEqual(numBaseGlyphs);
326 spyOn($log, 'warn');
327
328 var ok = gs.registerGlyphs(badGlyphSet);
329 expect(ok).toBe(false);
330 expect($log.warn).toHaveBeenCalledWith(missVbTri);
331 expect($log.warn).toHaveBeenCalledWith(missVbDia);
332 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800333 });
Simon Hunt670e8252015-01-06 18:31:30 -0800334
335 function verifyPathPrefix(elem, prefix) {
336 var plen = prefix.length,
337 d = elem.select('path').attr('d');
338 expect(d.slice(0, plen)).toEqual(prefix);
339 }
340
Simon Hunt58f23bb2015-01-16 16:32:24 -0800341 function verifyLoadedInDom(id, vb, expPfxId) {
342 var pfxId = expPfxId || id,
343 symbol = d3Elem.select('#' + id);
344 expect(symbol.size()).toEqual(1);
345 expect(symbol.attr('viewBox')).toEqual(vb);
346 verifyPathPrefix(symbol, prefixLookup[pfxId]);
347 }
348
Simon Hunt670e8252015-01-06 18:31:30 -0800349 it('should load base glyphs into the DOM', function () {
350 gs.init();
351 gs.loadDefs(d3Elem);
352 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800353 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800354 });
355
356 it('should load custom glyphs into the DOM', function () {
357 gs.init();
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700358 gs.registerGlyphSet(newGlyphs);
Simon Hunt670e8252015-01-06 18:31:30 -0800359 gs.loadDefs(d3Elem);
360 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800361 verifyLoadedInDom('diamond', testVbox);
362 });
Simon Hunt670e8252015-01-06 18:31:30 -0800363
Simon Hunt58f23bb2015-01-16 16:32:24 -0800364 it('should load only specified glyphs into the DOM', function () {
365 gs.init();
366 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
367 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
368 verifyLoadedInDom('crown', vbGlyph);
369 verifyLoadedInDom('chain', vbGlyph);
370 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800371 });
Simon Huntc9b73162015-01-29 14:02:15 -0800372
373 it('should add a glyph with default size', function () {
374 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800375 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800376 var what = svg.selectAll('use');
377 expect(what.size()).toEqual(1);
378 expect(what.attr('width')).toEqual('40');
379 expect(what.attr('height')).toEqual('40');
380 expect(what.attr('xlink:href')).toEqual('#crown');
381 expect(what.classed('glyph')).toBeTruthy();
382 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800383
384 // check a couple on retval, which should be the same thing..
385 expect(retval.attr('xlink:href')).toEqual('#crown');
386 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800387 });
388
389 it('should add a glyph with given size', function () {
390 gs.init();
391 gs.addGlyph(svg, 'crown', 37);
392 var what = svg.selectAll('use');
393 expect(what.size()).toEqual(1);
394 expect(what.attr('width')).toEqual('37');
395 expect(what.attr('height')).toEqual('37');
396 expect(what.attr('xlink:href')).toEqual('#crown');
397 expect(what.classed('glyph')).toBeTruthy();
398 expect(what.classed('overlay')).toBeFalsy();
399 });
400
401 it('should add a glyph marked as overlay', function () {
402 gs.init();
403 gs.addGlyph(svg, 'crown', 20, true);
404 var what = svg.selectAll('use');
405 expect(what.size()).toEqual(1);
406 expect(what.attr('width')).toEqual('20');
407 expect(what.attr('height')).toEqual('20');
408 expect(what.attr('xlink:href')).toEqual('#crown');
409 expect(what.classed('glyph')).toBeTruthy();
410 expect(what.classed('overlay')).toBeTruthy();
411 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800412});