blob: 64e8a0275b31bdb6c9a85822bdb3c3a290a20733 [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
19
20 @author Simon Hunt
21 */
22describe('factory: fw/svg/glyph.js', function() {
Simon Hunt670e8252015-01-06 18:31:30 -080023 var $log, fs, gs, d3Elem;
Simon Hunt7ac7be92015-01-06 10:47:56 -080024
Simon Huntbc39f6d2015-01-06 17:34:28 -080025 var numBaseGlyphs = 11,
26 vbBird = '352 224 113 112',
Simon Hunt6e459802015-01-06 15:05:42 -080027 vbGlyph = '0 0 110 110',
Simon Hunt58f23bb2015-01-16 16:32:24 -080028 vbBadge = '0 0 10 10',
29 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,',
40 uiAttached: 'M2,2.5a.5,.5',
41
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 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800143
144 // define some glyphs that we want to install
145
146 var testVbox = '0 0 1 1',
147 dTriangle = 'M.5,.2l.3,.6,h-.6z',
148 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
149 newGlyphs = {
150 triangle: dTriangle,
151 diamond: dDiamond
152 },
153 dupGlyphs = {
154 router: dTriangle,
155 switch: dDiamond
156 },
157 idCollision = 'GlyphService.register(): ID collision: ';
158
159 it('should install new glyphs', function () {
160 gs.init();
161 expect(gs.ids().length).toEqual(numBaseGlyphs);
162 spyOn($log, 'warn');
163
164 var ok = gs.register(testVbox, newGlyphs);
165 expect(ok).toBeTruthy();
166 expect($log.warn).not.toHaveBeenCalled();
167
168 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800169 verifyGlyphLoadedInCache('triangle', testVbox);
170 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800171 });
172
173 it('should not overwrite glyphs with dup IDs', function () {
174 gs.init();
175 expect(gs.ids().length).toEqual(numBaseGlyphs);
176 spyOn($log, 'warn');
177
178 var ok = gs.register(testVbox, dupGlyphs);
179 expect(ok).toBeFalsy();
180 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
181 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
182
183 expect(gs.ids().length).toEqual(numBaseGlyphs);
184 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800185 verifyGlyphLoadedInCache('router', vbGlyph);
186 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800187 });
188
189 it('should replace glyphs if asked nicely', function () {
190 gs.init();
191 expect(gs.ids().length).toEqual(numBaseGlyphs);
192 spyOn($log, 'warn');
193
194 var ok = gs.register(testVbox, dupGlyphs, true);
195 expect(ok).toBeTruthy();
196 expect($log.warn).not.toHaveBeenCalled();
197
198 expect(gs.ids().length).toEqual(numBaseGlyphs);
199 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800200 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
201 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800202 });
Simon Hunt670e8252015-01-06 18:31:30 -0800203
204 function verifyPathPrefix(elem, prefix) {
205 var plen = prefix.length,
206 d = elem.select('path').attr('d');
207 expect(d.slice(0, plen)).toEqual(prefix);
208 }
209
Simon Hunt58f23bb2015-01-16 16:32:24 -0800210 function verifyLoadedInDom(id, vb, expPfxId) {
211 var pfxId = expPfxId || id,
212 symbol = d3Elem.select('#' + id);
213 expect(symbol.size()).toEqual(1);
214 expect(symbol.attr('viewBox')).toEqual(vb);
215 verifyPathPrefix(symbol, prefixLookup[pfxId]);
216 }
217
Simon Hunt670e8252015-01-06 18:31:30 -0800218 it('should load base glyphs into the DOM', function () {
219 gs.init();
220 gs.loadDefs(d3Elem);
221 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800222 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800223 });
224
225 it('should load custom glyphs into the DOM', function () {
226 gs.init();
227 gs.register(testVbox, newGlyphs);
228 gs.loadDefs(d3Elem);
229 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800230 verifyLoadedInDom('diamond', testVbox);
231 });
Simon Hunt670e8252015-01-06 18:31:30 -0800232
Simon Hunt58f23bb2015-01-16 16:32:24 -0800233 it('should load only specified glyphs into the DOM', function () {
234 gs.init();
235 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
236 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
237 verifyLoadedInDom('crown', vbGlyph);
238 verifyLoadedInDom('chain', vbGlyph);
239 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800240 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800241});