blob: 6ba9e321a503ce2738f71fbf965cb35770396269 [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 Coledb4b87b2015-03-25 09:18:42 -070023 var numBaseGlyphs = 22,
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 Coledeca6e92015-03-19 12:03:14 -070040 summary: longPrefix + 'M16.7',
41 details: longPrefix + 'M16.9',
42 ports: 'M98,9.2H79.6c',
43 map: 'M95.8,9.2H14.2c-2.8,0-5,2.2-5,5v66',
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -070044 resetZoom: 'M86.4,81',
45 nextIntent: 'M88.1,55.7',
46 prevIntent: 'M22.5,55.6',
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070047
Simon Hunt58f23bb2015-01-16 16:32:24 -080048 uiAttached: 'M2,2.5a.5,.5',
Simon Huntaa26adf2015-01-20 10:32:49 -080049 checkMark: 'M2.6,4.5c0',
50 xMark: 'M9.0,7.2C8.2',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080051 triangleUp: 'M0.5,6.2c0',
52 triangleDown: 'M9.5,4.2c0',
Simon Hunt58f23bb2015-01-16 16:32:24 -080053
54 // our test ones..
55 triangle: 'M.5,.2',
56 diamond: 'M.2,.5'
57 };
Simon Hunt6e459802015-01-06 15:05:42 -080058
Simon Hunt51fc40b2015-01-06 13:56:12 -080059 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080060
Simon Hunt51fc40b2015-01-06 13:56:12 -080061 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080062 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080063 $log = _$log_;
64 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080065 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080066 d3Elem = body.append('defs').attr('id', 'myDefs');
67 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080068 }));
69
Simon Hunt670e8252015-01-06 18:31:30 -080070 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080071 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080072 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080073 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080074 });
75
Simon Hunt7ac7be92015-01-06 10:47:56 -080076 it('should define GlyphService', function () {
77 expect(gs).toBeDefined();
78 });
79
Simon Hunt6e459802015-01-06 15:05:42 -080080 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080081 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -080082 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -080083 ])).toBeTruthy();
84 });
85
Simon Hunt6e459802015-01-06 15:05:42 -080086 it('should start with no glyphs loaded', function () {
87 expect(gs.ids()).toEqual([]);
88 });
89
Simon Hunt58f23bb2015-01-16 16:32:24 -080090 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080091 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080092 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080093 });
94
Simon Hunt58f23bb2015-01-16 16:32:24 -080095 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080096 gs.init();
97 expect(gs.ids().length).toEqual(numBaseGlyphs);
98 gs.clear();
99 expect(gs.ids().length).toEqual(0);
100 });
101
Simon Hunt58f23bb2015-01-16 16:32:24 -0800102 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
103 var pfxId = expPfxId || id,
104 glyph = gs.glyph(id),
105 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800106 plen = prefix.length;
107 expect(fs.contains(gs.ids(), id)).toBeTruthy();
108 expect(glyph).toBeDefined();
109 expect(glyph.id).toEqual(id);
110 expect(glyph.vb).toEqual(vbox);
111 expect(glyph.d.slice(0, plen)).toEqual(prefix);
112 }
113
114 it('should load the bird glyph', function() {
115 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800116 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800117 });
118 it('should load the unknown glyph', function() {
119 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800120 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800121 });
122 it('should load the node glyph', function() {
123 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800124 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800125 });
126 it('should load the switch glyph', function() {
127 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800128 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800129 });
130 it('should load the roadm glyph', function() {
131 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800132 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800133 });
134 it('should load the endstation glyph', function() {
135 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800136 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800137 });
138 it('should load the router glyph', function() {
139 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800140 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800141 });
142 it('should load the bgpSpeaker glyph', function() {
143 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800144 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800145 });
146 it('should load the chain glyph', function() {
147 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800148 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800149 });
150 it('should load the crown glyph', function() {
151 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800152 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800153 });
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -0700154 it('should load the summary glyph', function () {
155 gs.init();
156 verifyGlyphLoadedInCache('summary', vbGlyph);
157 });
158 it('should load the details glyph', function () {
159 gs.init();
160 verifyGlyphLoadedInCache('details', vbGlyph);
161 });
162 it('should load the ports glyph', function () {
163 gs.init();
164 verifyGlyphLoadedInCache('ports', vbGlyph);
165 });
166 it('should load the map glyph', function () {
167 gs.init();
168 verifyGlyphLoadedInCache('map', vbGlyph);
169 });
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700170 it('should load the resetZoom glyph', function () {
171 gs.init();
172 verifyGlyphLoadedInCache('resetZoom', vbGlyph);
173 });
174 it('should load the nextIntent glyph', function () {
175 gs.init();
176 verifyGlyphLoadedInCache('nextIntent', vbGlyph);
177 });
178 it('should load the prevIntent glyph', function () {
179 gs.init();
180 verifyGlyphLoadedInCache('prevIntent', vbGlyph);
181 });
Simon Hunt6e459802015-01-06 15:05:42 -0800182 it('should load the uiAttached glyph', function() {
183 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800184 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800185 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800186 it('should load the checkMark glyph', function() {
187 gs.init();
188 verifyGlyphLoadedInCache('checkMark', vbBadge);
189 });
190 it('should load the xMark glyph', function() {
191 gs.init();
192 verifyGlyphLoadedInCache('xMark', vbBadge);
193 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800194
195 // define some glyphs that we want to install
196
197 var testVbox = '0 0 1 1',
198 dTriangle = 'M.5,.2l.3,.6,h-.6z',
199 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
200 newGlyphs = {
201 triangle: dTriangle,
202 diamond: dDiamond
203 },
204 dupGlyphs = {
205 router: dTriangle,
206 switch: dDiamond
207 },
208 idCollision = 'GlyphService.register(): ID collision: ';
209
210 it('should install new glyphs', function () {
211 gs.init();
212 expect(gs.ids().length).toEqual(numBaseGlyphs);
213 spyOn($log, 'warn');
214
215 var ok = gs.register(testVbox, newGlyphs);
216 expect(ok).toBeTruthy();
217 expect($log.warn).not.toHaveBeenCalled();
218
219 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800220 verifyGlyphLoadedInCache('triangle', testVbox);
221 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800222 });
223
224 it('should not overwrite glyphs with dup IDs', function () {
225 gs.init();
226 expect(gs.ids().length).toEqual(numBaseGlyphs);
227 spyOn($log, 'warn');
228
229 var ok = gs.register(testVbox, dupGlyphs);
230 expect(ok).toBeFalsy();
231 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
232 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
233
234 expect(gs.ids().length).toEqual(numBaseGlyphs);
235 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800236 verifyGlyphLoadedInCache('router', vbGlyph);
237 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800238 });
239
240 it('should replace glyphs if asked nicely', function () {
241 gs.init();
242 expect(gs.ids().length).toEqual(numBaseGlyphs);
243 spyOn($log, 'warn');
244
245 var ok = gs.register(testVbox, dupGlyphs, true);
246 expect(ok).toBeTruthy();
247 expect($log.warn).not.toHaveBeenCalled();
248
249 expect(gs.ids().length).toEqual(numBaseGlyphs);
250 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800251 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
252 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800253 });
Simon Hunt670e8252015-01-06 18:31:30 -0800254
255 function verifyPathPrefix(elem, prefix) {
256 var plen = prefix.length,
257 d = elem.select('path').attr('d');
258 expect(d.slice(0, plen)).toEqual(prefix);
259 }
260
Simon Hunt58f23bb2015-01-16 16:32:24 -0800261 function verifyLoadedInDom(id, vb, expPfxId) {
262 var pfxId = expPfxId || id,
263 symbol = d3Elem.select('#' + id);
264 expect(symbol.size()).toEqual(1);
265 expect(symbol.attr('viewBox')).toEqual(vb);
266 verifyPathPrefix(symbol, prefixLookup[pfxId]);
267 }
268
Simon Hunt670e8252015-01-06 18:31:30 -0800269 it('should load base glyphs into the DOM', function () {
270 gs.init();
271 gs.loadDefs(d3Elem);
272 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800273 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800274 });
275
276 it('should load custom glyphs into the DOM', function () {
277 gs.init();
278 gs.register(testVbox, newGlyphs);
279 gs.loadDefs(d3Elem);
280 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800281 verifyLoadedInDom('diamond', testVbox);
282 });
Simon Hunt670e8252015-01-06 18:31:30 -0800283
Simon Hunt58f23bb2015-01-16 16:32:24 -0800284 it('should load only specified glyphs into the DOM', function () {
285 gs.init();
286 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
287 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
288 verifyLoadedInDom('crown', vbGlyph);
289 verifyLoadedInDom('chain', vbGlyph);
290 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800291 });
Simon Huntc9b73162015-01-29 14:02:15 -0800292
293 it('should add a glyph with default size', function () {
294 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800295 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800296 var what = svg.selectAll('use');
297 expect(what.size()).toEqual(1);
298 expect(what.attr('width')).toEqual('40');
299 expect(what.attr('height')).toEqual('40');
300 expect(what.attr('xlink:href')).toEqual('#crown');
301 expect(what.classed('glyph')).toBeTruthy();
302 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800303
304 // check a couple on retval, which should be the same thing..
305 expect(retval.attr('xlink:href')).toEqual('#crown');
306 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800307 });
308
309 it('should add a glyph with given size', function () {
310 gs.init();
311 gs.addGlyph(svg, 'crown', 37);
312 var what = svg.selectAll('use');
313 expect(what.size()).toEqual(1);
314 expect(what.attr('width')).toEqual('37');
315 expect(what.attr('height')).toEqual('37');
316 expect(what.attr('xlink:href')).toEqual('#crown');
317 expect(what.classed('glyph')).toBeTruthy();
318 expect(what.classed('overlay')).toBeFalsy();
319 });
320
321 it('should add a glyph marked as overlay', function () {
322 gs.init();
323 gs.addGlyph(svg, 'crown', 20, true);
324 var what = svg.selectAll('use');
325 expect(what.size()).toEqual(1);
326 expect(what.attr('width')).toEqual('20');
327 expect(what.attr('height')).toEqual('20');
328 expect(what.attr('xlink:href')).toEqual('#crown');
329 expect(what.classed('glyph')).toBeTruthy();
330 expect(what.classed('overlay')).toBeTruthy();
331 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800332});