blob: b72de5a06695375dd5b6afd6918087fdf4364942 [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 Cole94a856e2015-01-19 15:16:40 -080023 var numBaseGlyphs = 13,
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',
Simon Hunt58f23bb2015-01-16 16:32:24 -080041
42 // our test ones..
43 triangle: 'M.5,.2',
44 diamond: 'M.2,.5'
45 };
Simon Hunt6e459802015-01-06 15:05:42 -080046
Simon Hunt51fc40b2015-01-06 13:56:12 -080047 beforeEach(module('onosUtil', 'onosSvg'));
Simon Hunt7ac7be92015-01-06 10:47:56 -080048
Simon Hunt51fc40b2015-01-06 13:56:12 -080049 beforeEach(inject(function (_$log_, FnService, GlyphService) {
Simon Huntc9b73162015-01-29 14:02:15 -080050 var body = d3.select('body');
Simon Hunt51fc40b2015-01-06 13:56:12 -080051 $log = _$log_;
52 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080053 gs = GlyphService;
Simon Huntc9b73162015-01-29 14:02:15 -080054 d3Elem = body.append('defs').attr('id', 'myDefs');
55 svg = body.append('svg').attr('id', 'mySvg');
Simon Hunt7ac7be92015-01-06 10:47:56 -080056 }));
57
Simon Hunt670e8252015-01-06 18:31:30 -080058 afterEach(function () {
Simon Huntc9b73162015-01-29 14:02:15 -080059 d3.select('#mySvg').remove();
Simon Hunt670e8252015-01-06 18:31:30 -080060 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080061 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080062 });
63
Simon Hunt7ac7be92015-01-06 10:47:56 -080064 it('should define GlyphService', function () {
65 expect(gs).toBeDefined();
66 });
67
Simon Hunt6e459802015-01-06 15:05:42 -080068 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080069 expect(fs.areFunctions(gs, [
Simon Huntc9b73162015-01-29 14:02:15 -080070 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph'
Simon Hunt51fc40b2015-01-06 13:56:12 -080071 ])).toBeTruthy();
72 });
73
Simon Hunt6e459802015-01-06 15:05:42 -080074 it('should start with no glyphs loaded', function () {
75 expect(gs.ids()).toEqual([]);
76 });
77
Simon Hunt58f23bb2015-01-16 16:32:24 -080078 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080079 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080080 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080081 });
82
Simon Hunt58f23bb2015-01-16 16:32:24 -080083 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080084 gs.init();
85 expect(gs.ids().length).toEqual(numBaseGlyphs);
86 gs.clear();
87 expect(gs.ids().length).toEqual(0);
88 });
89
Simon Hunt58f23bb2015-01-16 16:32:24 -080090 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
91 var pfxId = expPfxId || id,
92 glyph = gs.glyph(id),
93 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -080094 plen = prefix.length;
95 expect(fs.contains(gs.ids(), id)).toBeTruthy();
96 expect(glyph).toBeDefined();
97 expect(glyph.id).toEqual(id);
98 expect(glyph.vb).toEqual(vbox);
99 expect(glyph.d.slice(0, plen)).toEqual(prefix);
100 }
101
102 it('should load the bird glyph', function() {
103 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800104 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800105 });
106 it('should load the unknown glyph', function() {
107 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800108 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800109 });
110 it('should load the node glyph', function() {
111 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800112 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800113 });
114 it('should load the switch glyph', function() {
115 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800116 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800117 });
118 it('should load the roadm glyph', function() {
119 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800120 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800121 });
122 it('should load the endstation glyph', function() {
123 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800124 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800125 });
126 it('should load the router glyph', function() {
127 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800128 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800129 });
130 it('should load the bgpSpeaker glyph', function() {
131 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800132 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800133 });
134 it('should load the chain glyph', function() {
135 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800136 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800137 });
138 it('should load the crown glyph', function() {
139 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800140 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800141 });
142 it('should load the uiAttached glyph', function() {
143 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800144 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800145 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800146 it('should load the checkMark glyph', function() {
147 gs.init();
148 verifyGlyphLoadedInCache('checkMark', vbBadge);
149 });
150 it('should load the xMark glyph', function() {
151 gs.init();
152 verifyGlyphLoadedInCache('xMark', vbBadge);
153 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800154
155 // define some glyphs that we want to install
156
157 var testVbox = '0 0 1 1',
158 dTriangle = 'M.5,.2l.3,.6,h-.6z',
159 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
160 newGlyphs = {
161 triangle: dTriangle,
162 diamond: dDiamond
163 },
164 dupGlyphs = {
165 router: dTriangle,
166 switch: dDiamond
167 },
168 idCollision = 'GlyphService.register(): ID collision: ';
169
170 it('should install new glyphs', function () {
171 gs.init();
172 expect(gs.ids().length).toEqual(numBaseGlyphs);
173 spyOn($log, 'warn');
174
175 var ok = gs.register(testVbox, newGlyphs);
176 expect(ok).toBeTruthy();
177 expect($log.warn).not.toHaveBeenCalled();
178
179 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800180 verifyGlyphLoadedInCache('triangle', testVbox);
181 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800182 });
183
184 it('should not overwrite glyphs with dup IDs', function () {
185 gs.init();
186 expect(gs.ids().length).toEqual(numBaseGlyphs);
187 spyOn($log, 'warn');
188
189 var ok = gs.register(testVbox, dupGlyphs);
190 expect(ok).toBeFalsy();
191 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
192 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
193
194 expect(gs.ids().length).toEqual(numBaseGlyphs);
195 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800196 verifyGlyphLoadedInCache('router', vbGlyph);
197 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800198 });
199
200 it('should replace glyphs if asked nicely', function () {
201 gs.init();
202 expect(gs.ids().length).toEqual(numBaseGlyphs);
203 spyOn($log, 'warn');
204
205 var ok = gs.register(testVbox, dupGlyphs, true);
206 expect(ok).toBeTruthy();
207 expect($log.warn).not.toHaveBeenCalled();
208
209 expect(gs.ids().length).toEqual(numBaseGlyphs);
210 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800211 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
212 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800213 });
Simon Hunt670e8252015-01-06 18:31:30 -0800214
215 function verifyPathPrefix(elem, prefix) {
216 var plen = prefix.length,
217 d = elem.select('path').attr('d');
218 expect(d.slice(0, plen)).toEqual(prefix);
219 }
220
Simon Hunt58f23bb2015-01-16 16:32:24 -0800221 function verifyLoadedInDom(id, vb, expPfxId) {
222 var pfxId = expPfxId || id,
223 symbol = d3Elem.select('#' + id);
224 expect(symbol.size()).toEqual(1);
225 expect(symbol.attr('viewBox')).toEqual(vb);
226 verifyPathPrefix(symbol, prefixLookup[pfxId]);
227 }
228
Simon Hunt670e8252015-01-06 18:31:30 -0800229 it('should load base glyphs into the DOM', function () {
230 gs.init();
231 gs.loadDefs(d3Elem);
232 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800233 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800234 });
235
236 it('should load custom glyphs into the DOM', function () {
237 gs.init();
238 gs.register(testVbox, newGlyphs);
239 gs.loadDefs(d3Elem);
240 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800241 verifyLoadedInDom('diamond', testVbox);
242 });
Simon Hunt670e8252015-01-06 18:31:30 -0800243
Simon Hunt58f23bb2015-01-16 16:32:24 -0800244 it('should load only specified glyphs into the DOM', function () {
245 gs.init();
246 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
247 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
248 verifyLoadedInDom('crown', vbGlyph);
249 verifyLoadedInDom('chain', vbGlyph);
250 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800251 });
Simon Huntc9b73162015-01-29 14:02:15 -0800252
253 it('should add a glyph with default size', function () {
254 gs.init();
255 gs.addGlyph(svg, 'crown');
256 var what = svg.selectAll('use');
257 expect(what.size()).toEqual(1);
258 expect(what.attr('width')).toEqual('40');
259 expect(what.attr('height')).toEqual('40');
260 expect(what.attr('xlink:href')).toEqual('#crown');
261 expect(what.classed('glyph')).toBeTruthy();
262 expect(what.classed('overlay')).toBeFalsy();
263 });
264
265 it('should add a glyph with given size', function () {
266 gs.init();
267 gs.addGlyph(svg, 'crown', 37);
268 var what = svg.selectAll('use');
269 expect(what.size()).toEqual(1);
270 expect(what.attr('width')).toEqual('37');
271 expect(what.attr('height')).toEqual('37');
272 expect(what.attr('xlink:href')).toEqual('#crown');
273 expect(what.classed('glyph')).toBeTruthy();
274 expect(what.classed('overlay')).toBeFalsy();
275 });
276
277 it('should add a glyph marked as overlay', function () {
278 gs.init();
279 gs.addGlyph(svg, 'crown', 20, true);
280 var what = svg.selectAll('use');
281 expect(what.size()).toEqual(1);
282 expect(what.attr('width')).toEqual('20');
283 expect(what.attr('height')).toEqual('20');
284 expect(what.attr('xlink:href')).toEqual('#crown');
285 expect(what.classed('glyph')).toBeTruthy();
286 expect(what.classed('overlay')).toBeTruthy();
287 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800288});