blob: 8f5728dec926e8254612bca2b44c39233442da10 [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
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070023 var numBaseGlyphs = 30,
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',
49 resetZoom: 'M86,79.8L',
50 relatedIntents: 'M99.9,43.7',
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -070051 nextIntent: 'M88.1,55.7',
52 prevIntent: 'M22.5,55.6',
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070053 intentTraffic: 'M14.7,71.5h',
54 allTraffic: 'M15.7,64.5h-7v',
55 flows: 'M93.8,46.1c',
Bri Prebilic Cole5a206bb2015-03-25 16:33:27 -070056 eqMaster: 'M100.1,46.9l',
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070057
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070058 // badges
Simon Hunt58f23bb2015-01-16 16:32:24 -080059 uiAttached: 'M2,2.5a.5,.5',
Simon Huntaa26adf2015-01-20 10:32:49 -080060 checkMark: 'M2.6,4.5c0',
61 xMark: 'M9.0,7.2C8.2',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080062 triangleUp: 'M0.5,6.2c0',
63 triangleDown: 'M9.5,4.2c0',
Simon Hunt58f23bb2015-01-16 16:32:24 -080064
65 // our test ones..
66 triangle: 'M.5,.2',
67 diamond: 'M.2,.5'
68 };
Simon Hunt6e459802015-01-06 15:05:42 -080069
Simon Hunt51fc40b2015-01-06 13:56:12 -080070 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080071
Simon Hunt51fc40b2015-01-06 13:56:12 -080072 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080073 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080074 $log = _$log_;
75 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080076 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080077 d3Elem = body.append('defs').attr('id', 'myDefs');
78 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080079 }));
80
Simon Hunt670e8252015-01-06 18:31:30 -080081 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080082 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080083 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080084 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080085 });
86
Simon Hunt7ac7be92015-01-06 10:47:56 -080087 it('should define GlyphService', function () {
88 expect(gs).toBeDefined();
89 });
90
Simon Hunt6e459802015-01-06 15:05:42 -080091 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080092 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -080093 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -080094 ])).toBeTruthy();
95 });
96
Simon Hunt6e459802015-01-06 15:05:42 -080097 it('should start with no glyphs loaded', function () {
98 expect(gs.ids()).toEqual([]);
99 });
100
Simon Hunt58f23bb2015-01-16 16:32:24 -0800101 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800102 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -0800103 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -0800104 });
105
Simon Hunt58f23bb2015-01-16 16:32:24 -0800106 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800107 gs.init();
108 expect(gs.ids().length).toEqual(numBaseGlyphs);
109 gs.clear();
110 expect(gs.ids().length).toEqual(0);
111 });
112
Simon Hunt58f23bb2015-01-16 16:32:24 -0800113 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
114 var pfxId = expPfxId || id,
115 glyph = gs.glyph(id),
116 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800117 plen = prefix.length;
118 expect(fs.contains(gs.ids(), id)).toBeTruthy();
119 expect(glyph).toBeDefined();
120 expect(glyph.id).toEqual(id);
121 expect(glyph.vb).toEqual(vbox);
122 expect(glyph.d.slice(0, plen)).toEqual(prefix);
123 }
124
125 it('should load the bird glyph', function() {
126 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800127 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800128 });
129 it('should load the unknown glyph', function() {
130 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800131 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800132 });
133 it('should load the node glyph', function() {
134 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800135 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800136 });
137 it('should load the switch glyph', function() {
138 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800139 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800140 });
141 it('should load the roadm glyph', function() {
142 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800143 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800144 });
145 it('should load the endstation glyph', function() {
146 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800147 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800148 });
149 it('should load the router glyph', function() {
150 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800151 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800152 });
153 it('should load the bgpSpeaker glyph', function() {
154 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800155 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800156 });
157 it('should load the chain glyph', function() {
158 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800159 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800160 });
161 it('should load the crown glyph', function() {
162 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800163 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800164 });
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700165 it('should load the lock glyph', function() {
166 gs.init();
167 verifyGlyphLoadedInCache('lock', vbGlyph);
168 });
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -0700169 it('should load the summary glyph', function () {
170 gs.init();
171 verifyGlyphLoadedInCache('summary', vbGlyph);
172 });
173 it('should load the details glyph', function () {
174 gs.init();
175 verifyGlyphLoadedInCache('details', vbGlyph);
176 });
177 it('should load the ports glyph', function () {
178 gs.init();
179 verifyGlyphLoadedInCache('ports', vbGlyph);
180 });
181 it('should load the map glyph', function () {
182 gs.init();
183 verifyGlyphLoadedInCache('map', vbGlyph);
184 });
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700185 it('should load the cycleLabels glyph', function () {
186 gs.init();
187 verifyGlyphLoadedInCache('cycleLabels', vbGlyph);
188 });
189 it('should load the oblique glyph', function () {
190 gs.init();
191 verifyGlyphLoadedInCache('oblique', vbGlyph);
192 });
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700193 it('should load the resetZoom glyph', function () {
194 gs.init();
195 verifyGlyphLoadedInCache('resetZoom', vbGlyph);
196 });
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700197 it('should load the relatedIntents glyph', function () {
198 gs.init();
199 verifyGlyphLoadedInCache('relatedIntents', vbGlyph);
200 });
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700201 it('should load the nextIntent glyph', function () {
202 gs.init();
203 verifyGlyphLoadedInCache('nextIntent', vbGlyph);
204 });
205 it('should load the prevIntent glyph', function () {
206 gs.init();
207 verifyGlyphLoadedInCache('prevIntent', vbGlyph);
208 });
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700209 it('should load the intentTraffic glyph', function () {
210 gs.init();
211 verifyGlyphLoadedInCache('intentTraffic', vbGlyph);
212 });
213 it('should load the flows glyph', function () {
214 gs.init();
215 verifyGlyphLoadedInCache('flows', vbGlyph);
216 });
217 it('should load the flows eqMaster', function () {
218 gs.init();
219 verifyGlyphLoadedInCache('eqMaster', vbGlyph);
220 });
Simon Hunt6e459802015-01-06 15:05:42 -0800221 it('should load the uiAttached glyph', function() {
222 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800223 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800224 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800225 it('should load the checkMark glyph', function() {
226 gs.init();
227 verifyGlyphLoadedInCache('checkMark', vbBadge);
228 });
229 it('should load the xMark glyph', function() {
230 gs.init();
231 verifyGlyphLoadedInCache('xMark', vbBadge);
232 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800233
234 // define some glyphs that we want to install
235
236 var testVbox = '0 0 1 1',
237 dTriangle = 'M.5,.2l.3,.6,h-.6z',
238 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
239 newGlyphs = {
240 triangle: dTriangle,
241 diamond: dDiamond
242 },
243 dupGlyphs = {
244 router: dTriangle,
245 switch: dDiamond
246 },
247 idCollision = 'GlyphService.register(): ID collision: ';
248
249 it('should install new glyphs', function () {
250 gs.init();
251 expect(gs.ids().length).toEqual(numBaseGlyphs);
252 spyOn($log, 'warn');
253
254 var ok = gs.register(testVbox, newGlyphs);
255 expect(ok).toBeTruthy();
256 expect($log.warn).not.toHaveBeenCalled();
257
258 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800259 verifyGlyphLoadedInCache('triangle', testVbox);
260 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800261 });
262
263 it('should not overwrite glyphs with dup IDs', function () {
264 gs.init();
265 expect(gs.ids().length).toEqual(numBaseGlyphs);
266 spyOn($log, 'warn');
267
268 var ok = gs.register(testVbox, dupGlyphs);
269 expect(ok).toBeFalsy();
270 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
271 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
272
273 expect(gs.ids().length).toEqual(numBaseGlyphs);
274 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800275 verifyGlyphLoadedInCache('router', vbGlyph);
276 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800277 });
278
279 it('should replace glyphs if asked nicely', function () {
280 gs.init();
281 expect(gs.ids().length).toEqual(numBaseGlyphs);
282 spyOn($log, 'warn');
283
284 var ok = gs.register(testVbox, dupGlyphs, true);
285 expect(ok).toBeTruthy();
286 expect($log.warn).not.toHaveBeenCalled();
287
288 expect(gs.ids().length).toEqual(numBaseGlyphs);
289 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800290 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
291 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800292 });
Simon Hunt670e8252015-01-06 18:31:30 -0800293
294 function verifyPathPrefix(elem, prefix) {
295 var plen = prefix.length,
296 d = elem.select('path').attr('d');
297 expect(d.slice(0, plen)).toEqual(prefix);
298 }
299
Simon Hunt58f23bb2015-01-16 16:32:24 -0800300 function verifyLoadedInDom(id, vb, expPfxId) {
301 var pfxId = expPfxId || id,
302 symbol = d3Elem.select('#' + id);
303 expect(symbol.size()).toEqual(1);
304 expect(symbol.attr('viewBox')).toEqual(vb);
305 verifyPathPrefix(symbol, prefixLookup[pfxId]);
306 }
307
Simon Hunt670e8252015-01-06 18:31:30 -0800308 it('should load base glyphs into the DOM', function () {
309 gs.init();
310 gs.loadDefs(d3Elem);
311 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800312 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800313 });
314
315 it('should load custom glyphs into the DOM', function () {
316 gs.init();
317 gs.register(testVbox, newGlyphs);
318 gs.loadDefs(d3Elem);
319 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800320 verifyLoadedInDom('diamond', testVbox);
321 });
Simon Hunt670e8252015-01-06 18:31:30 -0800322
Simon Hunt58f23bb2015-01-16 16:32:24 -0800323 it('should load only specified glyphs into the DOM', function () {
324 gs.init();
325 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
326 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
327 verifyLoadedInDom('crown', vbGlyph);
328 verifyLoadedInDom('chain', vbGlyph);
329 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800330 });
Simon Huntc9b73162015-01-29 14:02:15 -0800331
332 it('should add a glyph with default size', function () {
333 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800334 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800335 var what = svg.selectAll('use');
336 expect(what.size()).toEqual(1);
337 expect(what.attr('width')).toEqual('40');
338 expect(what.attr('height')).toEqual('40');
339 expect(what.attr('xlink:href')).toEqual('#crown');
340 expect(what.classed('glyph')).toBeTruthy();
341 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800342
343 // check a couple on retval, which should be the same thing..
344 expect(retval.attr('xlink:href')).toEqual('#crown');
345 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800346 });
347
348 it('should add a glyph with given size', function () {
349 gs.init();
350 gs.addGlyph(svg, 'crown', 37);
351 var what = svg.selectAll('use');
352 expect(what.size()).toEqual(1);
353 expect(what.attr('width')).toEqual('37');
354 expect(what.attr('height')).toEqual('37');
355 expect(what.attr('xlink:href')).toEqual('#crown');
356 expect(what.classed('glyph')).toBeTruthy();
357 expect(what.classed('overlay')).toBeFalsy();
358 });
359
360 it('should add a glyph marked as overlay', function () {
361 gs.init();
362 gs.addGlyph(svg, 'crown', 20, true);
363 var what = svg.selectAll('use');
364 expect(what.size()).toEqual(1);
365 expect(what.attr('width')).toEqual('20');
366 expect(what.attr('height')).toEqual('20');
367 expect(what.attr('xlink:href')).toEqual('#crown');
368 expect(what.classed('glyph')).toBeTruthy();
369 expect(what.classed('overlay')).toBeTruthy();
370 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800371});