Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/svg/glyph.js', function() { |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 21 | var $log, fs, gs, d3Elem, svg; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 22 | |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 23 | var numBaseGlyphs = 34, |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 24 | vbBird = '352 224 113 112', |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 25 | vbGlyph = '0 0 110 110', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 26 | vbBadge = '0 0 10 10', |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 27 | longPrefix = 'M95.8,9.2H14.2c-2.8,0-5,2.2-5,5v81.5c0,2.8,2.2,5,5,' + |
| 28 | '5h81.5c2.8,0,5-2.2,5-5V14.2C100.8,11.5,98.5,9.2,95.8,9.2z ', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 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,', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 40 | lock: 'M79.4,48.6h', |
| 41 | |
| 42 | // toolbar specific glyphs |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 43 | summary: longPrefix + 'M16.7', |
| 44 | details: longPrefix + 'M16.9', |
| 45 | ports: 'M98,9.2H79.6c', |
| 46 | map: 'M95.8,9.2H14.2c-2.8,0-5,2.2-5,5v66', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 47 | cycleLabels: 'M72.5,33.9c', |
| 48 | oblique: 'M80.9,30.2h', |
| 49 | resetZoom: 'M86,79.8L', |
| 50 | relatedIntents: 'M99.9,43.7', |
Bri Prebilic Cole | db4b87b | 2015-03-25 09:18:42 -0700 | [diff] [blame] | 51 | nextIntent: 'M88.1,55.7', |
| 52 | prevIntent: 'M22.5,55.6', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 53 | intentTraffic: 'M14.7,71.5h', |
| 54 | allTraffic: 'M15.7,64.5h-7v', |
| 55 | flows: 'M93.8,46.1c', |
Bri Prebilic Cole | 5a206bb | 2015-03-25 16:33:27 -0700 | [diff] [blame] | 56 | eqMaster: 'M100.1,46.9l', |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 57 | |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 58 | // badges |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 59 | uiAttached: 'M2,2.5a.5,.5', |
Simon Hunt | aa26adf | 2015-01-20 10:32:49 -0800 | [diff] [blame] | 60 | checkMark: 'M2.6,4.5c0', |
| 61 | xMark: 'M9.0,7.2C8.2', |
Bri Prebilic Cole | 1dc32e6 | 2015-02-03 09:44:33 -0800 | [diff] [blame] | 62 | triangleUp: 'M0.5,6.2c0', |
| 63 | triangleDown: 'M9.5,4.2c0', |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 64 | plus: 'M4,2h2v2h2v2', |
| 65 | minus: 'M2,4h6v2', |
| 66 | play: 'M2.5,2l5.5,3', |
| 67 | stop: 'M2.5,2.5h5', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 68 | |
| 69 | // our test ones.. |
| 70 | triangle: 'M.5,.2', |
| 71 | diamond: 'M.2,.5' |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 72 | }, |
| 73 | glyphIds = [ |
| 74 | 'unknown', 'node', 'switch', 'roadm', 'endstation', 'router', |
| 75 | 'bgpSpeaker', 'chain', 'crown', 'lock', |
| 76 | 'summary', 'details', 'ports', 'map', 'cycleLabels', 'oblique', |
| 77 | 'resetZoom', 'relatedIntents', 'nextIntent', 'prevIntent', |
| 78 | 'intentTraffic', 'allTraffic', 'flows', 'eqMaster' |
| 79 | ], |
| 80 | badgeIds = [ |
| 81 | 'uiAttached', 'checkMark', 'xMark', 'triangleUp', 'triangleDown', |
| 82 | 'plus', 'minus', 'play', 'stop' |
| 83 | ]; |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 84 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 85 | beforeEach(module('onosUtil', 'onosSvg')); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 86 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 87 | beforeEach(inject(function (_$log_, FnService, GlyphService) { |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 88 | var body = d3.select('body'); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 89 | $log = _$log_; |
| 90 | fs = FnService; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 91 | gs = GlyphService; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 92 | d3Elem = body.append('defs').attr('id', 'myDefs'); |
| 93 | svg = body.append('svg').attr('id', 'mySvg'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 94 | })); |
| 95 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 96 | afterEach(function () { |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 97 | d3.select('#mySvg').remove(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 98 | d3.select('#myDefs').remove(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 99 | gs.clear(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 100 | }); |
| 101 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 102 | it('should define GlyphService', function () { |
| 103 | expect(gs).toBeDefined(); |
| 104 | }); |
| 105 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 106 | it('should define api functions', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 107 | expect(fs.areFunctions(gs, [ |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 108 | 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs', 'addGlyph' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 109 | ])).toBeTruthy(); |
| 110 | }); |
| 111 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 112 | it('should start with no glyphs loaded', function () { |
| 113 | expect(gs.ids()).toEqual([]); |
| 114 | }); |
| 115 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 116 | it('should load the base set of glyphs into the cache', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 117 | gs.init(); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 118 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 119 | }); |
| 120 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 121 | it('should remove glyphs from the cache on clear', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 122 | gs.init(); |
| 123 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 124 | gs.clear(); |
| 125 | expect(gs.ids().length).toEqual(0); |
| 126 | }); |
| 127 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 128 | function verifyGlyphLoadedInCache(id, vbox, expPfxId) { |
| 129 | var pfxId = expPfxId || id, |
| 130 | glyph = gs.glyph(id), |
| 131 | prefix = prefixLookup[pfxId], |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 132 | plen = prefix.length; |
| 133 | expect(fs.contains(gs.ids(), id)).toBeTruthy(); |
| 134 | expect(glyph).toBeDefined(); |
| 135 | expect(glyph.id).toEqual(id); |
| 136 | expect(glyph.vb).toEqual(vbox); |
| 137 | expect(glyph.d.slice(0, plen)).toEqual(prefix); |
| 138 | } |
| 139 | |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 140 | it('should be configured with the correct number of glyphs', function () { |
| 141 | expect(1 + glyphIds.length + badgeIds.length).toEqual(numBaseGlyphs); |
| 142 | }); |
| 143 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 144 | it('should load the bird glyph', function() { |
| 145 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 146 | verifyGlyphLoadedInCache('bird', vbBird); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 147 | }); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 148 | |
| 149 | it('should load the regular glyphs', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 150 | gs.init(); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 151 | glyphIds.forEach(function (id) { |
| 152 | verifyGlyphLoadedInCache(id, vbGlyph); |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame] | 153 | }); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame^] | 154 | }); |
| 155 | |
| 156 | it('should load the badge glyphs', function () { |
| 157 | gs.init(); |
| 158 | badgeIds.forEach(function (id) { |
| 159 | verifyGlyphLoadedInCache(id, vbBadge); |
| 160 | }); |
| 161 | }); |
| 162 | |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 163 | |
| 164 | // define some glyphs that we want to install |
| 165 | |
| 166 | var testVbox = '0 0 1 1', |
| 167 | dTriangle = 'M.5,.2l.3,.6,h-.6z', |
| 168 | dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z', |
| 169 | newGlyphs = { |
| 170 | triangle: dTriangle, |
| 171 | diamond: dDiamond |
| 172 | }, |
| 173 | dupGlyphs = { |
| 174 | router: dTriangle, |
| 175 | switch: dDiamond |
| 176 | }, |
| 177 | idCollision = 'GlyphService.register(): ID collision: '; |
| 178 | |
| 179 | it('should install new glyphs', function () { |
| 180 | gs.init(); |
| 181 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 182 | spyOn($log, 'warn'); |
| 183 | |
| 184 | var ok = gs.register(testVbox, newGlyphs); |
| 185 | expect(ok).toBeTruthy(); |
| 186 | expect($log.warn).not.toHaveBeenCalled(); |
| 187 | |
| 188 | expect(gs.ids().length).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 189 | verifyGlyphLoadedInCache('triangle', testVbox); |
| 190 | verifyGlyphLoadedInCache('diamond', testVbox); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 191 | }); |
| 192 | |
| 193 | it('should not overwrite glyphs with dup IDs', function () { |
| 194 | gs.init(); |
| 195 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 196 | spyOn($log, 'warn'); |
| 197 | |
| 198 | var ok = gs.register(testVbox, dupGlyphs); |
| 199 | expect(ok).toBeFalsy(); |
| 200 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"'); |
| 201 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"'); |
| 202 | |
| 203 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 204 | // verify original glyphs still exist... |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 205 | verifyGlyphLoadedInCache('router', vbGlyph); |
| 206 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 207 | }); |
| 208 | |
| 209 | it('should replace glyphs if asked nicely', function () { |
| 210 | gs.init(); |
| 211 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 212 | spyOn($log, 'warn'); |
| 213 | |
| 214 | var ok = gs.register(testVbox, dupGlyphs, true); |
| 215 | expect(ok).toBeTruthy(); |
| 216 | expect($log.warn).not.toHaveBeenCalled(); |
| 217 | |
| 218 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 219 | // verify glyphs have been overwritten... |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 220 | verifyGlyphLoadedInCache('router', testVbox, 'triangle'); |
| 221 | verifyGlyphLoadedInCache('switch', testVbox, 'diamond'); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 222 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 223 | |
| 224 | function verifyPathPrefix(elem, prefix) { |
| 225 | var plen = prefix.length, |
| 226 | d = elem.select('path').attr('d'); |
| 227 | expect(d.slice(0, plen)).toEqual(prefix); |
| 228 | } |
| 229 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 230 | function verifyLoadedInDom(id, vb, expPfxId) { |
| 231 | var pfxId = expPfxId || id, |
| 232 | symbol = d3Elem.select('#' + id); |
| 233 | expect(symbol.size()).toEqual(1); |
| 234 | expect(symbol.attr('viewBox')).toEqual(vb); |
| 235 | verifyPathPrefix(symbol, prefixLookup[pfxId]); |
| 236 | } |
| 237 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 238 | it('should load base glyphs into the DOM', function () { |
| 239 | gs.init(); |
| 240 | gs.loadDefs(d3Elem); |
| 241 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 242 | verifyLoadedInDom('bgpSpeaker', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 243 | }); |
| 244 | |
| 245 | it('should load custom glyphs into the DOM', function () { |
| 246 | gs.init(); |
| 247 | gs.register(testVbox, newGlyphs); |
| 248 | gs.loadDefs(d3Elem); |
| 249 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 250 | verifyLoadedInDom('diamond', testVbox); |
| 251 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 252 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 253 | it('should load only specified glyphs into the DOM', function () { |
| 254 | gs.init(); |
| 255 | gs.loadDefs(d3Elem, ['crown', 'chain', 'node']); |
| 256 | expect(d3Elem.selectAll('symbol').size()).toEqual(3); |
| 257 | verifyLoadedInDom('crown', vbGlyph); |
| 258 | verifyLoadedInDom('chain', vbGlyph); |
| 259 | verifyLoadedInDom('node', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 260 | }); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 261 | |
| 262 | it('should add a glyph with default size', function () { |
| 263 | gs.init(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 264 | var retval = gs.addGlyph(svg, 'crown'); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 265 | var what = svg.selectAll('use'); |
| 266 | expect(what.size()).toEqual(1); |
| 267 | expect(what.attr('width')).toEqual('40'); |
| 268 | expect(what.attr('height')).toEqual('40'); |
| 269 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 270 | expect(what.classed('glyph')).toBeTruthy(); |
| 271 | expect(what.classed('overlay')).toBeFalsy(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 272 | |
| 273 | // check a couple on retval, which should be the same thing.. |
| 274 | expect(retval.attr('xlink:href')).toEqual('#crown'); |
| 275 | expect(retval.classed('glyph')).toBeTruthy(); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 276 | }); |
| 277 | |
| 278 | it('should add a glyph with given size', function () { |
| 279 | gs.init(); |
| 280 | gs.addGlyph(svg, 'crown', 37); |
| 281 | var what = svg.selectAll('use'); |
| 282 | expect(what.size()).toEqual(1); |
| 283 | expect(what.attr('width')).toEqual('37'); |
| 284 | expect(what.attr('height')).toEqual('37'); |
| 285 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 286 | expect(what.classed('glyph')).toBeTruthy(); |
| 287 | expect(what.classed('overlay')).toBeFalsy(); |
| 288 | }); |
| 289 | |
| 290 | it('should add a glyph marked as overlay', function () { |
| 291 | gs.init(); |
| 292 | gs.addGlyph(svg, 'crown', 20, true); |
| 293 | var what = svg.selectAll('use'); |
| 294 | expect(what.size()).toEqual(1); |
| 295 | expect(what.attr('width')).toEqual('20'); |
| 296 | expect(what.attr('height')).toEqual('20'); |
| 297 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 298 | expect(what.classed('glyph')).toBeTruthy(); |
| 299 | expect(what.classed('overlay')).toBeTruthy(); |
| 300 | }); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 301 | }); |