blob: c5b47d40ad223cd34d12b694fcd4beb5855ae9d6 [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 Hunt3074fb22015-03-31 15:06:25 -070023 var numBaseGlyphs = 34,
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 Hunt3074fb22015-03-31 15:06:25 -070064 plus: 'M4,2h2v2h2v2',
65 minus: 'M2,4h6v2',
66 play: 'M2.5,2l5.5,3',
67 stop: 'M2.5,2.5h5',
Simon Hunt58f23bb2015-01-16 16:32:24 -080068
69 // our test ones..
70 triangle: 'M.5,.2',
71 diamond: 'M.2,.5'
Simon Hunt3074fb22015-03-31 15:06:25 -070072 },
73 glyphIds = [
74 'unknown', 'node', 'switch', 'roadm', 'endstation', 'router',
75 'bgpSpeaker', 'chain', 'crown', 'lock',
76 'summary', 'details', 'ports', 'map', 'cycleLabels', 'oblique',
77 'resetZoom', 'relatedIntents', 'nextIntent', 'prevIntent',
78 'intentTraffic', 'allTraffic', 'flows', 'eqMaster'
79 ],
80 badgeIds = [
81 'uiAttached', 'checkMark', 'xMark', 'triangleUp', 'triangleDown',
82 'plus', 'minus', 'play', 'stop'
83 ];
Simon Hunt6e459802015-01-06 15:05:42 -080084
Simon Hunt51fc40b2015-01-06 13:56:12 -080085 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080086
Simon Hunt51fc40b2015-01-06 13:56:12 -080087 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080088 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080089 $log = _$log_;
90 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080091 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080092 d3Elem = body.append('defs').attr('id', 'myDefs');
93 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080094 }));
95
Simon Hunt670e8252015-01-06 18:31:30 -080096 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080097 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080098 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080099 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -0800100 });
101
Simon Hunt7ac7be92015-01-06 10:47:56 -0800102 it('should define GlyphService', function () {
103 expect(gs).toBeDefined();
104 });
105
Simon Hunt6e459802015-01-06 15:05:42 -0800106 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800107 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -0800108 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800109 ])).toBeTruthy();
110 });
111
Simon Hunt6e459802015-01-06 15:05:42 -0800112 it('should start with no glyphs loaded', function () {
113 expect(gs.ids()).toEqual([]);
114 });
115
Simon Hunt58f23bb2015-01-16 16:32:24 -0800116 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800117 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -0800118 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -0800119 });
120
Simon Hunt58f23bb2015-01-16 16:32:24 -0800121 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800122 gs.init();
123 expect(gs.ids().length).toEqual(numBaseGlyphs);
124 gs.clear();
125 expect(gs.ids().length).toEqual(0);
126 });
127
Simon Hunt58f23bb2015-01-16 16:32:24 -0800128 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
129 var pfxId = expPfxId || id,
130 glyph = gs.glyph(id),
131 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800132 plen = prefix.length;
133 expect(fs.contains(gs.ids(), id)).toBeTruthy();
134 expect(glyph).toBeDefined();
135 expect(glyph.id).toEqual(id);
136 expect(glyph.vb).toEqual(vbox);
137 expect(glyph.d.slice(0, plen)).toEqual(prefix);
138 }
139
Simon Hunt3074fb22015-03-31 15:06:25 -0700140 it('should be configured with the correct number of glyphs', function () {
141 expect(1 + glyphIds.length + badgeIds.length).toEqual(numBaseGlyphs);
142 });
143
Simon Hunt6e459802015-01-06 15:05:42 -0800144 it('should load the bird glyph', function() {
145 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800146 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800147 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700148
149 it('should load the regular glyphs', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800150 gs.init();
Simon Hunt3074fb22015-03-31 15:06:25 -0700151 glyphIds.forEach(function (id) {
152 verifyGlyphLoadedInCache(id, vbGlyph);
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800153 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700154 });
155
156 it('should load the badge glyphs', function () {
157 gs.init();
158 badgeIds.forEach(function (id) {
159 verifyGlyphLoadedInCache(id, vbBadge);
160 });
161 });
162
Simon Huntbc39f6d2015-01-06 17:34:28 -0800163
164 // define some glyphs that we want to install
165
166 var testVbox = '0 0 1 1',
167 dTriangle = 'M.5,.2l.3,.6,h-.6z',
168 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
169 newGlyphs = {
170 triangle: dTriangle,
171 diamond: dDiamond
172 },
173 dupGlyphs = {
174 router: dTriangle,
175 switch: dDiamond
176 },
177 idCollision = 'GlyphService.register(): ID collision: ';
178
179 it('should install new glyphs', function () {
180 gs.init();
181 expect(gs.ids().length).toEqual(numBaseGlyphs);
182 spyOn($log, 'warn');
183
184 var ok = gs.register(testVbox, newGlyphs);
185 expect(ok).toBeTruthy();
186 expect($log.warn).not.toHaveBeenCalled();
187
188 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800189 verifyGlyphLoadedInCache('triangle', testVbox);
190 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800191 });
192
193 it('should not overwrite glyphs with dup IDs', function () {
194 gs.init();
195 expect(gs.ids().length).toEqual(numBaseGlyphs);
196 spyOn($log, 'warn');
197
198 var ok = gs.register(testVbox, dupGlyphs);
199 expect(ok).toBeFalsy();
200 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
201 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
202
203 expect(gs.ids().length).toEqual(numBaseGlyphs);
204 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800205 verifyGlyphLoadedInCache('router', vbGlyph);
206 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800207 });
208
209 it('should replace glyphs if asked nicely', function () {
210 gs.init();
211 expect(gs.ids().length).toEqual(numBaseGlyphs);
212 spyOn($log, 'warn');
213
214 var ok = gs.register(testVbox, dupGlyphs, true);
215 expect(ok).toBeTruthy();
216 expect($log.warn).not.toHaveBeenCalled();
217
218 expect(gs.ids().length).toEqual(numBaseGlyphs);
219 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800220 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
221 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800222 });
Simon Hunt670e8252015-01-06 18:31:30 -0800223
224 function verifyPathPrefix(elem, prefix) {
225 var plen = prefix.length,
226 d = elem.select('path').attr('d');
227 expect(d.slice(0, plen)).toEqual(prefix);
228 }
229
Simon Hunt58f23bb2015-01-16 16:32:24 -0800230 function verifyLoadedInDom(id, vb, expPfxId) {
231 var pfxId = expPfxId || id,
232 symbol = d3Elem.select('#' + id);
233 expect(symbol.size()).toEqual(1);
234 expect(symbol.attr('viewBox')).toEqual(vb);
235 verifyPathPrefix(symbol, prefixLookup[pfxId]);
236 }
237
Simon Hunt670e8252015-01-06 18:31:30 -0800238 it('should load base glyphs into the DOM', function () {
239 gs.init();
240 gs.loadDefs(d3Elem);
241 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800242 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800243 });
244
245 it('should load custom glyphs into the DOM', function () {
246 gs.init();
247 gs.register(testVbox, newGlyphs);
248 gs.loadDefs(d3Elem);
249 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800250 verifyLoadedInDom('diamond', testVbox);
251 });
Simon Hunt670e8252015-01-06 18:31:30 -0800252
Simon Hunt58f23bb2015-01-16 16:32:24 -0800253 it('should load only specified glyphs into the DOM', function () {
254 gs.init();
255 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
256 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
257 verifyLoadedInDom('crown', vbGlyph);
258 verifyLoadedInDom('chain', vbGlyph);
259 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800260 });
Simon Huntc9b73162015-01-29 14:02:15 -0800261
262 it('should add a glyph with default size', function () {
263 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800264 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800265 var what = svg.selectAll('use');
266 expect(what.size()).toEqual(1);
267 expect(what.attr('width')).toEqual('40');
268 expect(what.attr('height')).toEqual('40');
269 expect(what.attr('xlink:href')).toEqual('#crown');
270 expect(what.classed('glyph')).toBeTruthy();
271 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800272
273 // check a couple on retval, which should be the same thing..
274 expect(retval.attr('xlink:href')).toEqual('#crown');
275 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800276 });
277
278 it('should add a glyph with given size', function () {
279 gs.init();
280 gs.addGlyph(svg, 'crown', 37);
281 var what = svg.selectAll('use');
282 expect(what.size()).toEqual(1);
283 expect(what.attr('width')).toEqual('37');
284 expect(what.attr('height')).toEqual('37');
285 expect(what.attr('xlink:href')).toEqual('#crown');
286 expect(what.classed('glyph')).toBeTruthy();
287 expect(what.classed('overlay')).toBeFalsy();
288 });
289
290 it('should add a glyph marked as overlay', function () {
291 gs.init();
292 gs.addGlyph(svg, 'crown', 20, true);
293 var what = svg.selectAll('use');
294 expect(what.size()).toEqual(1);
295 expect(what.attr('width')).toEqual('20');
296 expect(what.attr('height')).toEqual('20');
297 expect(what.attr('xlink:href')).toEqual('#crown');
298 expect(what.classed('glyph')).toBeTruthy();
299 expect(what.classed('overlay')).toBeTruthy();
300 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800301});