blob: 587d617b63bfe1f0559d94995bc92e8375a097b7 [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 Hunt670e8252015-01-06 18:31:30 -080021 var $log, fs, gs, d3Elem;
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) {
50 $log = _$log_;
51 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080052 gs = GlyphService;
Simon Hunt670e8252015-01-06 18:31:30 -080053 d3Elem = d3.select('body').append('defs').attr('id', 'myDefs');
Simon Hunt7ac7be92015-01-06 10:47:56 -080054 }));
55
Simon Hunt670e8252015-01-06 18:31:30 -080056 afterEach(function () {
57 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080058 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080059 });
60
Simon Hunt7ac7be92015-01-06 10:47:56 -080061 it('should define GlyphService', function () {
62 expect(gs).toBeDefined();
63 });
64
Simon Hunt6e459802015-01-06 15:05:42 -080065 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080066 expect(fs.areFunctions(gs, [
Simon Hunt58f23bb2015-01-16 16:32:24 -080067 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs'
Simon Hunt51fc40b2015-01-06 13:56:12 -080068 ])).toBeTruthy();
69 });
70
Simon Hunt6e459802015-01-06 15:05:42 -080071 it('should start with no glyphs loaded', function () {
72 expect(gs.ids()).toEqual([]);
73 });
74
Simon Hunt58f23bb2015-01-16 16:32:24 -080075 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080076 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080077 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080078 });
79
Simon Hunt58f23bb2015-01-16 16:32:24 -080080 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080081 gs.init();
82 expect(gs.ids().length).toEqual(numBaseGlyphs);
83 gs.clear();
84 expect(gs.ids().length).toEqual(0);
85 });
86
Simon Hunt58f23bb2015-01-16 16:32:24 -080087 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
88 var pfxId = expPfxId || id,
89 glyph = gs.glyph(id),
90 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -080091 plen = prefix.length;
92 expect(fs.contains(gs.ids(), id)).toBeTruthy();
93 expect(glyph).toBeDefined();
94 expect(glyph.id).toEqual(id);
95 expect(glyph.vb).toEqual(vbox);
96 expect(glyph.d.slice(0, plen)).toEqual(prefix);
97 }
98
99 it('should load the bird glyph', function() {
100 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800101 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800102 });
103 it('should load the unknown glyph', function() {
104 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800105 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800106 });
107 it('should load the node glyph', function() {
108 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800109 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800110 });
111 it('should load the switch glyph', function() {
112 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800113 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800114 });
115 it('should load the roadm glyph', function() {
116 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800117 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800118 });
119 it('should load the endstation glyph', function() {
120 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800121 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800122 });
123 it('should load the router glyph', function() {
124 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800125 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800126 });
127 it('should load the bgpSpeaker glyph', function() {
128 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800129 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800130 });
131 it('should load the chain glyph', function() {
132 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800133 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800134 });
135 it('should load the crown glyph', function() {
136 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800137 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800138 });
139 it('should load the uiAttached glyph', function() {
140 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800141 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800142 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800143 it('should load the checkMark glyph', function() {
144 gs.init();
145 verifyGlyphLoadedInCache('checkMark', vbBadge);
146 });
147 it('should load the xMark glyph', function() {
148 gs.init();
149 verifyGlyphLoadedInCache('xMark', vbBadge);
150 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800151
152 // define some glyphs that we want to install
153
154 var testVbox = '0 0 1 1',
155 dTriangle = 'M.5,.2l.3,.6,h-.6z',
156 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
157 newGlyphs = {
158 triangle: dTriangle,
159 diamond: dDiamond
160 },
161 dupGlyphs = {
162 router: dTriangle,
163 switch: dDiamond
164 },
165 idCollision = 'GlyphService.register(): ID collision: ';
166
167 it('should install new glyphs', function () {
168 gs.init();
169 expect(gs.ids().length).toEqual(numBaseGlyphs);
170 spyOn($log, 'warn');
171
172 var ok = gs.register(testVbox, newGlyphs);
173 expect(ok).toBeTruthy();
174 expect($log.warn).not.toHaveBeenCalled();
175
176 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800177 verifyGlyphLoadedInCache('triangle', testVbox);
178 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800179 });
180
181 it('should not overwrite glyphs with dup IDs', function () {
182 gs.init();
183 expect(gs.ids().length).toEqual(numBaseGlyphs);
184 spyOn($log, 'warn');
185
186 var ok = gs.register(testVbox, dupGlyphs);
187 expect(ok).toBeFalsy();
188 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
189 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
190
191 expect(gs.ids().length).toEqual(numBaseGlyphs);
192 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800193 verifyGlyphLoadedInCache('router', vbGlyph);
194 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800195 });
196
197 it('should replace glyphs if asked nicely', function () {
198 gs.init();
199 expect(gs.ids().length).toEqual(numBaseGlyphs);
200 spyOn($log, 'warn');
201
202 var ok = gs.register(testVbox, dupGlyphs, true);
203 expect(ok).toBeTruthy();
204 expect($log.warn).not.toHaveBeenCalled();
205
206 expect(gs.ids().length).toEqual(numBaseGlyphs);
207 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800208 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
209 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800210 });
Simon Hunt670e8252015-01-06 18:31:30 -0800211
212 function verifyPathPrefix(elem, prefix) {
213 var plen = prefix.length,
214 d = elem.select('path').attr('d');
215 expect(d.slice(0, plen)).toEqual(prefix);
216 }
217
Simon Hunt58f23bb2015-01-16 16:32:24 -0800218 function verifyLoadedInDom(id, vb, expPfxId) {
219 var pfxId = expPfxId || id,
220 symbol = d3Elem.select('#' + id);
221 expect(symbol.size()).toEqual(1);
222 expect(symbol.attr('viewBox')).toEqual(vb);
223 verifyPathPrefix(symbol, prefixLookup[pfxId]);
224 }
225
Simon Hunt670e8252015-01-06 18:31:30 -0800226 it('should load base glyphs into the DOM', function () {
227 gs.init();
228 gs.loadDefs(d3Elem);
229 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800230 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800231 });
232
233 it('should load custom glyphs into the DOM', function () {
234 gs.init();
235 gs.register(testVbox, newGlyphs);
236 gs.loadDefs(d3Elem);
237 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800238 verifyLoadedInDom('diamond', testVbox);
239 });
Simon Hunt670e8252015-01-06 18:31:30 -0800240
Simon Hunt58f23bb2015-01-16 16:32:24 -0800241 it('should load only specified glyphs into the DOM', function () {
242 gs.init();
243 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
244 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
245 verifyLoadedInDom('crown', vbGlyph);
246 verifyLoadedInDom('chain', vbGlyph);
247 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800248 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800249});