blob: 47b13265b76f747569ec697dfee69e856f3ff4a6 [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 Cole1dc32e62015-02-03 09:44:33 -080023 var numBaseGlyphs = 15,
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',
27 prefixLookup = {
28 bird: 'M427.7,300.4',
29 unknown: 'M35,40a5',
30 node: 'M15,100a5',
31 switch: 'M10,20a10',
32 roadm: 'M10,35l25-',
33 endstation: 'M10,15a5,5',
34 router: 'M10,55A45,45',
35 bgpSpeaker: 'M10,40a45,35',
36 chain: 'M60.4,77.6c-',
37 crown: 'M99.5,21.6c0,',
38 uiAttached: 'M2,2.5a.5,.5',
Simon Huntaa26adf2015-01-20 10:32:49 -080039 checkMark: 'M2.6,4.5c0',
40 xMark: 'M9.0,7.2C8.2',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080041 triangleUp: 'M0.5,6.2c0',
42 triangleDown: 'M9.5,4.2c0',
Simon Hunt58f23bb2015-01-16 16:32:24 -080043
44 // our test ones..
45 triangle: 'M.5,.2',
46 diamond: 'M.2,.5'
47 };
Simon Hunt6e459802015-01-06 15:05:42 -080048
Simon Hunt51fc40b2015-01-06 13:56:12 -080049 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080050
Simon Hunt51fc40b2015-01-06 13:56:12 -080051 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080052 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080053 $log = _$log_;
54 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080055 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080056 d3Elem = body.append('defs').attr('id', 'myDefs');
57 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080058 }));
59
Simon Hunt670e8252015-01-06 18:31:30 -080060 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080061 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080062 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080063 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080064 });
65
Simon Hunt7ac7be92015-01-06 10:47:56 -080066 it('should define GlyphService', function () {
67 expect(gs).toBeDefined();
68 });
69
Simon Hunt6e459802015-01-06 15:05:42 -080070 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080071 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -080072 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -080073 ])).toBeTruthy();
74 });
75
Simon Hunt6e459802015-01-06 15:05:42 -080076 it('should start with no glyphs loaded', function () {
77 expect(gs.ids()).toEqual([]);
78 });
79
Simon Hunt58f23bb2015-01-16 16:32:24 -080080 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080081 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080082 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080083 });
84
Simon Hunt58f23bb2015-01-16 16:32:24 -080085 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080086 gs.init();
87 expect(gs.ids().length).toEqual(numBaseGlyphs);
88 gs.clear();
89 expect(gs.ids().length).toEqual(0);
90 });
91
Simon Hunt58f23bb2015-01-16 16:32:24 -080092 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
93 var pfxId = expPfxId || id,
94 glyph = gs.glyph(id),
95 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -080096 plen = prefix.length;
97 expect(fs.contains(gs.ids(), id)).toBeTruthy();
98 expect(glyph).toBeDefined();
99 expect(glyph.id).toEqual(id);
100 expect(glyph.vb).toEqual(vbox);
101 expect(glyph.d.slice(0, plen)).toEqual(prefix);
102 }
103
104 it('should load the bird glyph', function() {
105 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800106 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800107 });
108 it('should load the unknown glyph', function() {
109 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800110 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800111 });
112 it('should load the node glyph', function() {
113 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800114 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800115 });
116 it('should load the switch glyph', function() {
117 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800118 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800119 });
120 it('should load the roadm glyph', function() {
121 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800122 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800123 });
124 it('should load the endstation glyph', function() {
125 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800126 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800127 });
128 it('should load the router glyph', function() {
129 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800130 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800131 });
132 it('should load the bgpSpeaker glyph', function() {
133 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800134 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800135 });
136 it('should load the chain glyph', function() {
137 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800138 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800139 });
140 it('should load the crown glyph', function() {
141 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800142 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800143 });
144 it('should load the uiAttached glyph', function() {
145 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800146 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800147 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800148 it('should load the checkMark glyph', function() {
149 gs.init();
150 verifyGlyphLoadedInCache('checkMark', vbBadge);
151 });
152 it('should load the xMark glyph', function() {
153 gs.init();
154 verifyGlyphLoadedInCache('xMark', vbBadge);
155 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800156
157 // define some glyphs that we want to install
158
159 var testVbox = '0 0 1 1',
160 dTriangle = 'M.5,.2l.3,.6,h-.6z',
161 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
162 newGlyphs = {
163 triangle: dTriangle,
164 diamond: dDiamond
165 },
166 dupGlyphs = {
167 router: dTriangle,
168 switch: dDiamond
169 },
170 idCollision = 'GlyphService.register(): ID collision: ';
171
172 it('should install new glyphs', function () {
173 gs.init();
174 expect(gs.ids().length).toEqual(numBaseGlyphs);
175 spyOn($log, 'warn');
176
177 var ok = gs.register(testVbox, newGlyphs);
178 expect(ok).toBeTruthy();
179 expect($log.warn).not.toHaveBeenCalled();
180
181 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800182 verifyGlyphLoadedInCache('triangle', testVbox);
183 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800184 });
185
186 it('should not overwrite glyphs with dup IDs', function () {
187 gs.init();
188 expect(gs.ids().length).toEqual(numBaseGlyphs);
189 spyOn($log, 'warn');
190
191 var ok = gs.register(testVbox, dupGlyphs);
192 expect(ok).toBeFalsy();
193 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
194 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
195
196 expect(gs.ids().length).toEqual(numBaseGlyphs);
197 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800198 verifyGlyphLoadedInCache('router', vbGlyph);
199 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800200 });
201
202 it('should replace glyphs if asked nicely', function () {
203 gs.init();
204 expect(gs.ids().length).toEqual(numBaseGlyphs);
205 spyOn($log, 'warn');
206
207 var ok = gs.register(testVbox, dupGlyphs, true);
208 expect(ok).toBeTruthy();
209 expect($log.warn).not.toHaveBeenCalled();
210
211 expect(gs.ids().length).toEqual(numBaseGlyphs);
212 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800213 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
214 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800215 });
Simon Hunt670e8252015-01-06 18:31:30 -0800216
217 function verifyPathPrefix(elem, prefix) {
218 var plen = prefix.length,
219 d = elem.select('path').attr('d');
220 expect(d.slice(0, plen)).toEqual(prefix);
221 }
222
Simon Hunt58f23bb2015-01-16 16:32:24 -0800223 function verifyLoadedInDom(id, vb, expPfxId) {
224 var pfxId = expPfxId || id,
225 symbol = d3Elem.select('#' + id);
226 expect(symbol.size()).toEqual(1);
227 expect(symbol.attr('viewBox')).toEqual(vb);
228 verifyPathPrefix(symbol, prefixLookup[pfxId]);
229 }
230
Simon Hunt670e8252015-01-06 18:31:30 -0800231 it('should load base glyphs into the DOM', function () {
232 gs.init();
233 gs.loadDefs(d3Elem);
234 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800235 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800236 });
237
238 it('should load custom glyphs into the DOM', function () {
239 gs.init();
240 gs.register(testVbox, newGlyphs);
241 gs.loadDefs(d3Elem);
242 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800243 verifyLoadedInDom('diamond', testVbox);
244 });
Simon Hunt670e8252015-01-06 18:31:30 -0800245
Simon Hunt58f23bb2015-01-16 16:32:24 -0800246 it('should load only specified glyphs into the DOM', function () {
247 gs.init();
248 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
249 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
250 verifyLoadedInDom('crown', vbGlyph);
251 verifyLoadedInDom('chain', vbGlyph);
252 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800253 });
Simon Huntc9b73162015-01-29 14:02:15 -0800254
255 it('should add a glyph with default size', function () {
256 gs.init();
Simon Hunt4b668592015-01-29 17:33:53 -0800257 var retval = gs.addGlyph(svg, 'crown');
Simon Huntc9b73162015-01-29 14:02:15 -0800258 var what = svg.selectAll('use');
259 expect(what.size()).toEqual(1);
260 expect(what.attr('width')).toEqual('40');
261 expect(what.attr('height')).toEqual('40');
262 expect(what.attr('xlink:href')).toEqual('#crown');
263 expect(what.classed('glyph')).toBeTruthy();
264 expect(what.classed('overlay')).toBeFalsy();
Simon Hunt4b668592015-01-29 17:33:53 -0800265
266 // check a couple on retval, which should be the same thing..
267 expect(retval.attr('xlink:href')).toEqual('#crown');
268 expect(retval.classed('glyph')).toBeTruthy();
Simon Huntc9b73162015-01-29 14:02:15 -0800269 });
270
271 it('should add a glyph with given size', function () {
272 gs.init();
273 gs.addGlyph(svg, 'crown', 37);
274 var what = svg.selectAll('use');
275 expect(what.size()).toEqual(1);
276 expect(what.attr('width')).toEqual('37');
277 expect(what.attr('height')).toEqual('37');
278 expect(what.attr('xlink:href')).toEqual('#crown');
279 expect(what.classed('glyph')).toBeTruthy();
280 expect(what.classed('overlay')).toBeFalsy();
281 });
282
283 it('should add a glyph marked as overlay', function () {
284 gs.init();
285 gs.addGlyph(svg, 'crown', 20, true);
286 var what = svg.selectAll('use');
287 expect(what.size()).toEqual(1);
288 expect(what.attr('width')).toEqual('20');
289 expect(what.attr('height')).toEqual('20');
290 expect(what.attr('xlink:href')).toEqual('#crown');
291 expect(what.classed('glyph')).toBeTruthy();
292 expect(what.classed('overlay')).toBeTruthy();
293 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800294});