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