blob: 01110943a2147bd1bbb5f68f349c8cbc6a6371d9 [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 Coledeca6e92015-03-19 12:03:14 -070023 var numBaseGlyphs = 19,
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',
44
Simon Hunt58f23bb2015-01-16 16:32:24 -080045 uiAttached: 'M2,2.5a.5,.5',
Simon Huntaa26adf2015-01-20 10:32:49 -080046 checkMark: 'M2.6,4.5c0',
47 xMark: 'M9.0,7.2C8.2',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080048 triangleUp: 'M0.5,6.2c0',
49 triangleDown: 'M9.5,4.2c0',
Simon Hunt58f23bb2015-01-16 16:32:24 -080050
51 // our test ones..
52 triangle: 'M.5,.2',
53 diamond: 'M.2,.5'
54 };
Simon Hunt6e459802015-01-06 15:05:42 -080055
Simon Hunt51fc40b2015-01-06 13:56:12 -080056 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080057
Simon Hunt51fc40b2015-01-06 13:56:12 -080058 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080059 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080060 $log = _$log_;
61 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080062 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080063 d3Elem = body.append('defs').attr('id', 'myDefs');
64 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080065 }));
66
Simon Hunt670e8252015-01-06 18:31:30 -080067 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080068 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080069 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080070 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080071 });
72
Simon Hunt7ac7be92015-01-06 10:47:56 -080073 it('should define GlyphService', function () {
74 expect(gs).toBeDefined();
75 });
76
Simon Hunt6e459802015-01-06 15:05:42 -080077 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080078 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -080079 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -080080 ])).toBeTruthy();
81 });
82
Simon Hunt6e459802015-01-06 15:05:42 -080083 it('should start with no glyphs loaded', function () {
84 expect(gs.ids()).toEqual([]);
85 });
86
Simon Hunt58f23bb2015-01-16 16:32:24 -080087 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080088 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080089 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080090 });
91
Simon Hunt58f23bb2015-01-16 16:32:24 -080092 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080093 gs.init();
94 expect(gs.ids().length).toEqual(numBaseGlyphs);
95 gs.clear();
96 expect(gs.ids().length).toEqual(0);
97 });
98
Simon Hunt58f23bb2015-01-16 16:32:24 -080099 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
100 var pfxId = expPfxId || id,
101 glyph = gs.glyph(id),
102 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800103 plen = prefix.length;
104 expect(fs.contains(gs.ids(), id)).toBeTruthy();
105 expect(glyph).toBeDefined();
106 expect(glyph.id).toEqual(id);
107 expect(glyph.vb).toEqual(vbox);
108 expect(glyph.d.slice(0, plen)).toEqual(prefix);
109 }
110
111 it('should load the bird glyph', function() {
112 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800113 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800114 });
115 it('should load the unknown glyph', function() {
116 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800117 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800118 });
119 it('should load the node glyph', function() {
120 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800121 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800122 });
123 it('should load the switch glyph', function() {
124 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800125 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800126 });
127 it('should load the roadm glyph', function() {
128 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800129 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800130 });
131 it('should load the endstation glyph', function() {
132 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800133 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800134 });
135 it('should load the router glyph', function() {
136 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800137 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800138 });
139 it('should load the bgpSpeaker glyph', function() {
140 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800141 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800142 });
143 it('should load the chain glyph', function() {
144 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800145 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800146 });
147 it('should load the crown glyph', function() {
148 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800149 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800150 });
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -0700151 it('should load the summary glyph', function () {
152 gs.init();
153 verifyGlyphLoadedInCache('summary', vbGlyph);
154 });
155 it('should load the details glyph', function () {
156 gs.init();
157 verifyGlyphLoadedInCache('details', vbGlyph);
158 });
159 it('should load the ports glyph', function () {
160 gs.init();
161 verifyGlyphLoadedInCache('ports', vbGlyph);
162 });
163 it('should load the map glyph', function () {
164 gs.init();
165 verifyGlyphLoadedInCache('map', vbGlyph);
166 });
Simon Hunt6e459802015-01-06 15:05:42 -0800167 it('should load the uiAttached glyph', function() {
168 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800169 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800170 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800171 it('should load the checkMark glyph', function() {
172 gs.init();
173 verifyGlyphLoadedInCache('checkMark', vbBadge);
174 });
175 it('should load the xMark glyph', function() {
176 gs.init();
177 verifyGlyphLoadedInCache('xMark', vbBadge);
178 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800179
180 // define some glyphs that we want to install
181
182 var testVbox = '0 0 1 1',
183 dTriangle = 'M.5,.2l.3,.6,h-.6z',
184 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
185 newGlyphs = {
186 triangle: dTriangle,
187 diamond: dDiamond
188 },
189 dupGlyphs = {
190 router: dTriangle,
191 switch: dDiamond
192 },
193 idCollision = 'GlyphService.register(): ID collision: ';
194
195 it('should install new glyphs', function () {
196 gs.init();
197 expect(gs.ids().length).toEqual(numBaseGlyphs);
198 spyOn($log, 'warn');
199
200 var ok = gs.register(testVbox, newGlyphs);
201 expect(ok).toBeTruthy();
202 expect($log.warn).not.toHaveBeenCalled();
203
204 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800205 verifyGlyphLoadedInCache('triangle', testVbox);
206 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800207 });
208
209 it('should not overwrite glyphs with dup IDs', function () {
210 gs.init();
211 expect(gs.ids().length).toEqual(numBaseGlyphs);
212 spyOn($log, 'warn');
213
214 var ok = gs.register(testVbox, dupGlyphs);
215 expect(ok).toBeFalsy();
216 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
217 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
218
219 expect(gs.ids().length).toEqual(numBaseGlyphs);
220 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800221 verifyGlyphLoadedInCache('router', vbGlyph);
222 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800223 });
224
225 it('should replace glyphs if asked nicely', function () {
226 gs.init();
227 expect(gs.ids().length).toEqual(numBaseGlyphs);
228 spyOn($log, 'warn');
229
230 var ok = gs.register(testVbox, dupGlyphs, true);
231 expect(ok).toBeTruthy();
232 expect($log.warn).not.toHaveBeenCalled();
233
234 expect(gs.ids().length).toEqual(numBaseGlyphs);
235 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800236 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
237 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800238 });
Simon Hunt670e8252015-01-06 18:31:30 -0800239
240 function verifyPathPrefix(elem, prefix) {
241 var plen = prefix.length,
242 d = elem.select('path').attr('d');
243 expect(d.slice(0, plen)).toEqual(prefix);
244 }
245
Simon Hunt58f23bb2015-01-16 16:32:24 -0800246 function verifyLoadedInDom(id, vb, expPfxId) {
247 var pfxId = expPfxId || id,
248 symbol = d3Elem.select('#' + id);
249 expect(symbol.size()).toEqual(1);
250 expect(symbol.attr('viewBox')).toEqual(vb);
251 verifyPathPrefix(symbol, prefixLookup[pfxId]);
252 }
253
Simon Hunt670e8252015-01-06 18:31:30 -0800254 it('should load base glyphs into the DOM', function () {
255 gs.init();
256 gs.loadDefs(d3Elem);
257 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800258 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800259 });
260
261 it('should load custom glyphs into the DOM', function () {
262 gs.init();
263 gs.register(testVbox, newGlyphs);
264 gs.loadDefs(d3Elem);
265 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800266 verifyLoadedInDom('diamond', testVbox);
267 });
Simon Hunt670e8252015-01-06 18:31:30 -0800268
Simon Hunt58f23bb2015-01-16 16:32:24 -0800269 it('should load only specified glyphs into the DOM', function () {
270 gs.init();
271 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
272 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
273 verifyLoadedInDom('crown', vbGlyph);
274 verifyLoadedInDom('chain', vbGlyph);
275 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800276 });
Simon Huntc9b73162015-01-29 14:02:15 -0800277
278 it('should add a glyph with default size', function () {
279 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800280 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800281 var what = svg.selectAll('use');
282 expect(what.size()).toEqual(1);
283 expect(what.attr('width')).toEqual('40');
284 expect(what.attr('height')).toEqual('40');
285 expect(what.attr('xlink:href')).toEqual('#crown');
286 expect(what.classed('glyph')).toBeTruthy();
287 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800288
289 // check a couple on retval, which should be the same thing..
290 expect(retval.attr('xlink:href')).toEqual('#crown');
291 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800292 });
293
294 it('should add a glyph with given size', function () {
295 gs.init();
296 gs.addGlyph(svg, 'crown', 37);
297 var what = svg.selectAll('use');
298 expect(what.size()).toEqual(1);
299 expect(what.attr('width')).toEqual('37');
300 expect(what.attr('height')).toEqual('37');
301 expect(what.attr('xlink:href')).toEqual('#crown');
302 expect(what.classed('glyph')).toBeTruthy();
303 expect(what.classed('overlay')).toBeFalsy();
304 });
305
306 it('should add a glyph marked as overlay', function () {
307 gs.init();
308 gs.addGlyph(svg, 'crown', 20, true);
309 var what = svg.selectAll('use');
310 expect(what.size()).toEqual(1);
311 expect(what.attr('width')).toEqual('20');
312 expect(what.attr('height')).toEqual('20');
313 expect(what.attr('xlink:href')).toEqual('#crown');
314 expect(what.classed('glyph')).toBeTruthy();
315 expect(what.classed('overlay')).toBeTruthy();
316 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800317});