blob: c0cbf67f116869c12a244c24628820b1ad1a27df [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
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -080025 var numBaseGlyphs = 13,
Simon Huntbc39f6d2015-01-06 17:34:28 -080026 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',
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -080041 checkMark: 'M2.644,4.531c0',
42 xMark: 'M8.967,7.196C8.172',
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) {
52 $log = _$log_;
53 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080054 gs = GlyphService;
Simon Hunt670e8252015-01-06 18:31:30 -080055 d3Elem = d3.select('body').append('defs').attr('id', 'myDefs');
Simon Hunt7ac7be92015-01-06 10:47:56 -080056 }));
57
Simon Hunt670e8252015-01-06 18:31:30 -080058 afterEach(function () {
59 d3.select('#myDefs').remove();
Simon Huntcacce342015-01-07 16:13:05 -080060 gs.clear();
Simon Hunt670e8252015-01-06 18:31:30 -080061 });
62
Simon Hunt7ac7be92015-01-06 10:47:56 -080063 it('should define GlyphService', function () {
64 expect(gs).toBeDefined();
65 });
66
Simon Hunt6e459802015-01-06 15:05:42 -080067 it('should define api functions', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -080068 expect(fs.areFunctions(gs, [
Simon Hunt58f23bb2015-01-16 16:32:24 -080069 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs'
Simon Hunt51fc40b2015-01-06 13:56:12 -080070 ])).toBeTruthy();
71 });
72
Simon Hunt6e459802015-01-06 15:05:42 -080073 it('should start with no glyphs loaded', function () {
74 expect(gs.ids()).toEqual([]);
75 });
76
Simon Hunt58f23bb2015-01-16 16:32:24 -080077 it('should load the base set of glyphs into the cache', function () {
Simon Hunt6e459802015-01-06 15:05:42 -080078 gs.init();
Simon Huntbc39f6d2015-01-06 17:34:28 -080079 expect(gs.ids().length).toEqual(numBaseGlyphs);
Simon Hunt6e459802015-01-06 15:05:42 -080080 });
81
Simon Hunt58f23bb2015-01-16 16:32:24 -080082 it('should remove glyphs from the cache on clear', function () {
Simon Huntcacce342015-01-07 16:13:05 -080083 gs.init();
84 expect(gs.ids().length).toEqual(numBaseGlyphs);
85 gs.clear();
86 expect(gs.ids().length).toEqual(0);
87 });
88
Simon Hunt58f23bb2015-01-16 16:32:24 -080089 function verifyGlyphLoadedInCache(id, vbox, expPfxId) {
90 var pfxId = expPfxId || id,
91 glyph = gs.glyph(id),
92 prefix = prefixLookup[pfxId],
Simon Hunt6e459802015-01-06 15:05:42 -080093 plen = prefix.length;
94 expect(fs.contains(gs.ids(), id)).toBeTruthy();
95 expect(glyph).toBeDefined();
96 expect(glyph.id).toEqual(id);
97 expect(glyph.vb).toEqual(vbox);
98 expect(glyph.d.slice(0, plen)).toEqual(prefix);
99 }
100
101 it('should load the bird glyph', function() {
102 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800103 verifyGlyphLoadedInCache('bird', vbBird);
Simon Hunt6e459802015-01-06 15:05:42 -0800104 });
105 it('should load the unknown glyph', function() {
106 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800107 verifyGlyphLoadedInCache('unknown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800108 });
109 it('should load the node glyph', function() {
110 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800111 verifyGlyphLoadedInCache('node', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800112 });
113 it('should load the switch glyph', function() {
114 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800115 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800116 });
117 it('should load the roadm glyph', function() {
118 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800119 verifyGlyphLoadedInCache('roadm', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800120 });
121 it('should load the endstation glyph', function() {
122 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800123 verifyGlyphLoadedInCache('endstation', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800124 });
125 it('should load the router glyph', function() {
126 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800127 verifyGlyphLoadedInCache('router', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800128 });
129 it('should load the bgpSpeaker glyph', function() {
130 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800131 verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800132 });
133 it('should load the chain glyph', function() {
134 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800135 verifyGlyphLoadedInCache('chain', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800136 });
137 it('should load the crown glyph', function() {
138 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800139 verifyGlyphLoadedInCache('crown', vbGlyph);
Simon Hunt6e459802015-01-06 15:05:42 -0800140 });
141 it('should load the uiAttached glyph', function() {
142 gs.init();
Simon Hunt58f23bb2015-01-16 16:32:24 -0800143 verifyGlyphLoadedInCache('uiAttached', vbBadge);
Simon Hunt6e459802015-01-06 15:05:42 -0800144 });
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -0800145 it('should load the checkMark glyph', function() {
146 gs.init();
147 verifyGlyphLoadedInCache('checkMark', vbBadge);
148 });
149 it('should load the xMark glyph', function() {
150 gs.init();
151 verifyGlyphLoadedInCache('xMark', vbBadge);
152 });
Simon Huntbc39f6d2015-01-06 17:34:28 -0800153
154 // define some glyphs that we want to install
155
156 var testVbox = '0 0 1 1',
157 dTriangle = 'M.5,.2l.3,.6,h-.6z',
158 dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
159 newGlyphs = {
160 triangle: dTriangle,
161 diamond: dDiamond
162 },
163 dupGlyphs = {
164 router: dTriangle,
165 switch: dDiamond
166 },
167 idCollision = 'GlyphService.register(): ID collision: ';
168
169 it('should install new glyphs', function () {
170 gs.init();
171 expect(gs.ids().length).toEqual(numBaseGlyphs);
172 spyOn($log, 'warn');
173
174 var ok = gs.register(testVbox, newGlyphs);
175 expect(ok).toBeTruthy();
176 expect($log.warn).not.toHaveBeenCalled();
177
178 expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800179 verifyGlyphLoadedInCache('triangle', testVbox);
180 verifyGlyphLoadedInCache('diamond', testVbox);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800181 });
182
183 it('should not overwrite glyphs with dup IDs', function () {
184 gs.init();
185 expect(gs.ids().length).toEqual(numBaseGlyphs);
186 spyOn($log, 'warn');
187
188 var ok = gs.register(testVbox, dupGlyphs);
189 expect(ok).toBeFalsy();
190 expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
191 expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
192
193 expect(gs.ids().length).toEqual(numBaseGlyphs);
194 // verify original glyphs still exist...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800195 verifyGlyphLoadedInCache('router', vbGlyph);
196 verifyGlyphLoadedInCache('switch', vbGlyph);
Simon Huntbc39f6d2015-01-06 17:34:28 -0800197 });
198
199 it('should replace glyphs if asked nicely', function () {
200 gs.init();
201 expect(gs.ids().length).toEqual(numBaseGlyphs);
202 spyOn($log, 'warn');
203
204 var ok = gs.register(testVbox, dupGlyphs, true);
205 expect(ok).toBeTruthy();
206 expect($log.warn).not.toHaveBeenCalled();
207
208 expect(gs.ids().length).toEqual(numBaseGlyphs);
209 // verify glyphs have been overwritten...
Simon Hunt58f23bb2015-01-16 16:32:24 -0800210 verifyGlyphLoadedInCache('router', testVbox, 'triangle');
211 verifyGlyphLoadedInCache('switch', testVbox, 'diamond');
Simon Huntbc39f6d2015-01-06 17:34:28 -0800212 });
Simon Hunt670e8252015-01-06 18:31:30 -0800213
214 function verifyPathPrefix(elem, prefix) {
215 var plen = prefix.length,
216 d = elem.select('path').attr('d');
217 expect(d.slice(0, plen)).toEqual(prefix);
218 }
219
Simon Hunt58f23bb2015-01-16 16:32:24 -0800220 function verifyLoadedInDom(id, vb, expPfxId) {
221 var pfxId = expPfxId || id,
222 symbol = d3Elem.select('#' + id);
223 expect(symbol.size()).toEqual(1);
224 expect(symbol.attr('viewBox')).toEqual(vb);
225 verifyPathPrefix(symbol, prefixLookup[pfxId]);
226 }
227
Simon Hunt670e8252015-01-06 18:31:30 -0800228 it('should load base glyphs into the DOM', function () {
229 gs.init();
230 gs.loadDefs(d3Elem);
231 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800232 verifyLoadedInDom('bgpSpeaker', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800233 });
234
235 it('should load custom glyphs into the DOM', function () {
236 gs.init();
237 gs.register(testVbox, newGlyphs);
238 gs.loadDefs(d3Elem);
239 expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
Simon Hunt58f23bb2015-01-16 16:32:24 -0800240 verifyLoadedInDom('diamond', testVbox);
241 });
Simon Hunt670e8252015-01-06 18:31:30 -0800242
Simon Hunt58f23bb2015-01-16 16:32:24 -0800243 it('should load only specified glyphs into the DOM', function () {
244 gs.init();
245 gs.loadDefs(d3Elem, ['crown', 'chain', 'node']);
246 expect(d3Elem.selectAll('symbol').size()).toEqual(3);
247 verifyLoadedInDom('crown', vbGlyph);
248 verifyLoadedInDom('chain', vbGlyph);
249 verifyLoadedInDom('node', vbGlyph);
Simon Hunt670e8252015-01-06 18:31:30 -0800250 });
Simon Hunt7ac7be92015-01-06 10:47:56 -0800251});