blob: 9b24e471aa6eb3830939d8c588ed8e2c25dbe57c [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 Coleb5f2b152015-04-07 14:58:09 -070023 var numBaseGlyphs = 35,
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
70 // our test ones..
71 triangle: 'M.5,.2',
72 diamond: 'M.2,.5'
Simon Hunt3074fb22015-03-31 15:06:25 -070073 },
74 glyphIds = [
75 'unknown', 'node', 'switch', 'roadm', 'endstation', 'router',
76 'bgpSpeaker', 'chain', 'crown', 'lock',
77 'summary', 'details', 'ports', 'map', 'cycleLabels', 'oblique',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070078 'filters', 'resetZoom', 'relatedIntents', 'nextIntent',
79 'prevIntent', 'intentTraffic', 'allTraffic', 'flows', 'eqMaster'
Simon Hunt3074fb22015-03-31 15:06:25 -070080 ],
81 badgeIds = [
82 'uiAttached', 'checkMark', 'xMark', 'triangleUp', 'triangleDown',
83 'plus', 'minus', 'play', 'stop'
84 ];
Simon Hunt6e459802015-01-06 15:05:42 -080085
Simon Hunt51fc40b2015-01-06 13:56:12 -080086 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080087
Simon Hunt51fc40b2015-01-06 13:56:12 -080088 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080089 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080090 $log = _$log_;
91 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080092 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080093 d3Elem = body.append('defs').attr('id', 'myDefs');
94 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080095 }));
96
Simon Hunt670e8252015-01-06 18:31:30 -080097 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080098 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080099 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -0800100 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -0800101 });
102
Simon Hunt7ac7be92015-01-06 10:47:56 -0800103 it('should define GlyphService', function () {
104 expect(gs).toBeDefined();
105 });
106
Simon Hunt6e459802015-01-06 15:05:42 -0800107 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800108 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -0800109 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800110 ])).toBeTruthy();
111 });
112
Simon Hunt6e459802015-01-06 15:05:42 -0800113 it('should start with no glyphs loaded', function () {
114 expect(gs.ids()).toEqual([]);
115 });
116
Simon Hunt58f23bb2015-01-16 16:32:24 -0800117 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800118 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -0800119 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -0800120 });
121
Simon Hunt58f23bb2015-01-16 16:32:24 -0800122 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800123 gs.init();
124 expect(gs.ids().length).toEqual(numBaseGlyphs);
125 gs.clear();
126 expect(gs.ids().length).toEqual(0);
127 });
128
Simon Hunt58f23bb2015-01-16 16:32:24 -0800129 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
130 var pfxId = expPfxId || id,
131 glyph = gs.glyph(id),
132 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -0800133 plen = prefix.length;
134 expect(fs.contains(gs.ids(), id)).toBeTruthy();
135 expect(glyph).toBeDefined();
136 expect(glyph.id).toEqual(id);
137 expect(glyph.vb).toEqual(vbox);
138 expect(glyph.d.slice(0, plen)).toEqual(prefix);
139 }
140
Simon Hunt3074fb22015-03-31 15:06:25 -0700141 it('should be configured with the correct number of glyphs', function () {
142 expect(1 + glyphIds.length + badgeIds.length).toEqual(numBaseGlyphs);
143 });
144
Simon Hunt6e459802015-01-06 15:05:42 -0800145 it('should load the bird glyph', function() {
146 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800147 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800148 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700149
150 it('should load the regular glyphs', function () {
Simon Hunt6e459802015-01-06 15:05:42 -0800151 gs.init();
Simon Hunt3074fb22015-03-31 15:06:25 -0700152 glyphIds.forEach(function (id) {
153 verifyGlyphLoadedInCache(id, vbGlyph);
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800154 });
Simon Hunt3074fb22015-03-31 15:06:25 -0700155 });
156
157 it('should load the badge glyphs', function () {
158 gs.init();
159 badgeIds.forEach(function (id) {
160 verifyGlyphLoadedInCache(id, vbBadge);
161 });
162 });
163
Simon Huntbc39f6d2015-01-06 17:34:28 -0800164
165 // define some glyphs that we want to install
166
167 var testVbox = '0 0 1 1',
168 dTriangle = 'M.5,.2l.3,.6,h-.6z',
169 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
170 newGlyphs = {
171 triangle: dTriangle,
172 diamond: dDiamond
173 },
174 dupGlyphs = {
175 router: dTriangle,
176 switch: dDiamond
177 },
178 idCollision = 'GlyphService.register(): ID collision: ';
179
180 it('should install new glyphs', function () {
181 gs.init();
182 expect(gs.ids().length).toEqual(numBaseGlyphs);
183 spyOn($log, 'warn');
184
185 var ok = gs.register(testVbox, newGlyphs);
186 expect(ok).toBeTruthy();
187 expect($log.warn).not.toHaveBeenCalled();
188
189 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800190 verifyGlyphLoadedInCache('triangle', testVbox);
191 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800192 });
193
194 it('should not overwrite glyphs with dup IDs', function () {
195 gs.init();
196 expect(gs.ids().length).toEqual(numBaseGlyphs);
197 spyOn($log, 'warn');
198
199 var ok = gs.register(testVbox, dupGlyphs);
200 expect(ok).toBeFalsy();
201 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
202 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
203
204 expect(gs.ids().length).toEqual(numBaseGlyphs);
205 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800206 verifyGlyphLoadedInCache('router', vbGlyph);
207 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800208 });
209
210 it('should replace glyphs if asked nicely', function () {
211 gs.init();
212 expect(gs.ids().length).toEqual(numBaseGlyphs);
213 spyOn($log, 'warn');
214
215 var ok = gs.register(testVbox, dupGlyphs, true);
216 expect(ok).toBeTruthy();
217 expect($log.warn).not.toHaveBeenCalled();
218
219 expect(gs.ids().length).toEqual(numBaseGlyphs);
220 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800221 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
222 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800223 });
Simon Hunt670e8252015-01-06 18:31:30 -0800224
225 function verifyPathPrefix(elem, prefix) {
226 var plen = prefix.length,
227 d = elem.select('path').attr('d');
228 expect(d.slice(0, plen)).toEqual(prefix);
229 }
230
Simon Hunt58f23bb2015-01-16 16:32:24 -0800231 function verifyLoadedInDom(id, vb, expPfxId) {
232 var pfxId = expPfxId || id,
233 symbol = d3Elem.select('#' + id);
234 expect(symbol.size()).toEqual(1);
235 expect(symbol.attr('viewBox')).toEqual(vb);
236 verifyPathPrefix(symbol, prefixLookup[pfxId]);
237 }
238
Simon Hunt670e8252015-01-06 18:31:30 -0800239 it('should load base glyphs into the DOM', function () {
240 gs.init();
241 gs.loadDefs(d3Elem);
242 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800243 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800244 });
245
246 it('should load custom glyphs into the DOM', function () {
247 gs.init();
248 gs.register(testVbox, newGlyphs);
249 gs.loadDefs(d3Elem);
250 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800251 verifyLoadedInDom('diamond', testVbox);
252 });
Simon Hunt670e8252015-01-06 18:31:30 -0800253
Simon Hunt58f23bb2015-01-16 16:32:24 -0800254 it('should load only specified glyphs into the DOM', function () {
255 gs.init();
256 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
257 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
258 verifyLoadedInDom('crown', vbGlyph);
259 verifyLoadedInDom('chain', vbGlyph);
260 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800261 });
Simon Huntc9b73162015-01-29 14:02:15 -0800262
263 it('should add a glyph with default size', function () {
264 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800265 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800266 var what = svg.selectAll('use');
267 expect(what.size()).toEqual(1);
268 expect(what.attr('width')).toEqual('40');
269 expect(what.attr('height')).toEqual('40');
270 expect(what.attr('xlink:href')).toEqual('#crown');
271 expect(what.classed('glyph')).toBeTruthy();
272 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800273
274 // check a couple on retval, which should be the same thing..
275 expect(retval.attr('xlink:href')).toEqual('#crown');
276 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800277 });
278
279 it('should add a glyph with given size', function () {
280 gs.init();
281 gs.addGlyph(svg, 'crown', 37);
282 var what = svg.selectAll('use');
283 expect(what.size()).toEqual(1);
284 expect(what.attr('width')).toEqual('37');
285 expect(what.attr('height')).toEqual('37');
286 expect(what.attr('xlink:href')).toEqual('#crown');
287 expect(what.classed('glyph')).toBeTruthy();
288 expect(what.classed('overlay')).toBeFalsy();
289 });
290
291 it('should add a glyph marked as overlay', function () {
292 gs.init();
293 gs.addGlyph(svg, 'crown', 20, true);
294 var what = svg.selectAll('use');
295 expect(what.size()).toEqual(1);
296 expect(what.attr('width')).toEqual('20');
297 expect(what.attr('height')).toEqual('20');
298 expect(what.attr('xlink:href')).toEqual('#crown');
299 expect(what.classed('glyph')).toBeTruthy();
300 expect(what.classed('overlay')).toBeTruthy();
301 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800302});