Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 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 | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 23 | var numBaseGlyphs = 42, |
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 ', |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 29 | tablePrefix = 'M15.9,19.1h-8v-13h8V19.1z M90.5,6.1H75.6v13h14.9V6.1' + |
| 30 | 'z M71.9,6.1H56.9v13h14.9V6.1z M53.2,6.1H38.3v13h14.9V6.1z M34.5,' + |
| 31 | '6.1H19.6v13h14.9V6.1z M102.2,6.1h-8v13h8V6.1z ', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 32 | prefixLookup = { |
| 33 | bird: 'M427.7,300.4', |
| 34 | unknown: 'M35,40a5', |
| 35 | node: 'M15,100a5', |
| 36 | switch: 'M10,20a10', |
| 37 | roadm: 'M10,35l25-', |
| 38 | endstation: 'M10,15a5,5', |
| 39 | router: 'M10,55A45,45', |
| 40 | bgpSpeaker: 'M10,40a45,35', |
| 41 | chain: 'M60.4,77.6c-', |
| 42 | crown: 'M99.5,21.6c0,', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 43 | lock: 'M79.4,48.6h', |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 44 | topo: 'M97.2,76.3H86.6', |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 45 | refresh: 'M102.6,40.8L88.4', |
Bri Prebilic Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 46 | garbage: 'M94.6,20.2c', |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 47 | |
| 48 | // navigation specific glyphs |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 49 | flowTable: tablePrefix + 'M102.2,23.6H7.9v', |
| 50 | portTable: tablePrefix + 'M102.6,23.6v78.5H', |
| 51 | groupTable: 'M16,19.1H8v-13h', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 52 | |
| 53 | // toolbar specific glyphs |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 54 | summary: longPrefix + 'M16.7', |
| 55 | details: longPrefix + 'M16.9', |
| 56 | ports: 'M98,9.2H79.6c', |
| 57 | 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] | 58 | cycleLabels: 'M72.5,33.9c', |
| 59 | oblique: 'M80.9,30.2h', |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 60 | filters: 'M24.8,13.3L', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 61 | resetZoom: 'M86,79.8L', |
| 62 | relatedIntents: 'M99.9,43.7', |
Bri Prebilic Cole | db4b87b | 2015-03-25 09:18:42 -0700 | [diff] [blame] | 63 | nextIntent: 'M88.1,55.7', |
| 64 | prevIntent: 'M22.5,55.6', |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 65 | intentTraffic: 'M14.7,71.5h', |
| 66 | allTraffic: 'M15.7,64.5h-7v', |
| 67 | flows: 'M93.8,46.1c', |
Bri Prebilic Cole | 5a206bb | 2015-03-25 16:33:27 -0700 | [diff] [blame] | 68 | eqMaster: 'M100.1,46.9l', |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 69 | |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 70 | // badges |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 71 | uiAttached: 'M2,2.5a.5,.5', |
Simon Hunt | aa26adf | 2015-01-20 10:32:49 -0800 | [diff] [blame] | 72 | checkMark: 'M2.6,4.5c0', |
| 73 | xMark: 'M9.0,7.2C8.2', |
Bri Prebilic Cole | 1dc32e6 | 2015-02-03 09:44:33 -0800 | [diff] [blame] | 74 | triangleUp: 'M0.5,6.2c0', |
| 75 | triangleDown: 'M9.5,4.2c0', |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 76 | plus: 'M4,2h2v2h2v2', |
| 77 | minus: 'M2,4h6v2', |
| 78 | play: 'M2.5,2l5.5,3', |
| 79 | stop: 'M2.5,2.5h5', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 80 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 81 | cloud: 'M37.6,79.5c-6.9,8.7-20.4,8.6', |
| 82 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 83 | // our test ones.. |
| 84 | triangle: 'M.5,.2', |
| 85 | diamond: 'M.2,.5' |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 86 | }, |
| 87 | glyphIds = [ |
| 88 | 'unknown', 'node', 'switch', 'roadm', 'endstation', 'router', |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 89 | 'bgpSpeaker', 'chain', 'crown', 'lock', 'topo', 'refresh', |
Bri Prebilic Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 90 | 'garbage', |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 91 | 'flowTable', 'portTable', 'groupTable', |
| 92 | 'summary', 'details', 'ports', 'map', 'cycleLabels', |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 93 | 'oblique', 'filters', 'resetZoom', 'relatedIntents', 'nextIntent', |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 94 | 'prevIntent', 'intentTraffic', 'allTraffic', 'flows', 'eqMaster' |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 95 | ], |
| 96 | badgeIds = [ |
| 97 | 'uiAttached', 'checkMark', 'xMark', 'triangleUp', 'triangleDown', |
| 98 | 'plus', 'minus', 'play', 'stop' |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 99 | ], |
| 100 | spriteIds = [ |
| 101 | 'cloud' |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 102 | ]; |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 103 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 104 | beforeEach(module('onosUtil', 'onosSvg')); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 105 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 106 | beforeEach(inject(function (_$log_, FnService, GlyphService) { |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 107 | var body = d3.select('body'); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 108 | $log = _$log_; |
| 109 | fs = FnService; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 110 | gs = GlyphService; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 111 | d3Elem = body.append('defs').attr('id', 'myDefs'); |
| 112 | svg = body.append('svg').attr('id', 'mySvg'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 113 | })); |
| 114 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 115 | afterEach(function () { |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 116 | d3.select('#mySvg').remove(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 117 | d3.select('#myDefs').remove(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 118 | gs.clear(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 119 | }); |
| 120 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 121 | it('should define GlyphService', function () { |
| 122 | expect(gs).toBeDefined(); |
| 123 | }); |
| 124 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 125 | it('should define api functions', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 126 | expect(fs.areFunctions(gs, [ |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 127 | 'clear', 'init', 'registerGlyphs', 'registerGlyphSet', |
| 128 | 'ids', 'glyph', 'loadDefs', 'addGlyph' |
| 129 | ])).toBe(true); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 130 | }); |
| 131 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 132 | it('should start with no glyphs loaded', function () { |
| 133 | expect(gs.ids()).toEqual([]); |
| 134 | }); |
| 135 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 136 | it('should load the base set of glyphs into the cache', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 137 | gs.init(); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 138 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 139 | }); |
| 140 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 141 | it('should remove glyphs from the cache on clear', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 142 | gs.init(); |
| 143 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 144 | gs.clear(); |
| 145 | expect(gs.ids().length).toEqual(0); |
| 146 | }); |
| 147 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 148 | function verifyGlyphLoadedInCache(id, vbox, expPfxId) { |
| 149 | var pfxId = expPfxId || id, |
| 150 | glyph = gs.glyph(id), |
| 151 | prefix = prefixLookup[pfxId], |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 152 | plen = prefix.length; |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 153 | expect(fs.contains(gs.ids(), id)).toBe(true); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 154 | expect(glyph).toBeDefined(); |
| 155 | expect(glyph.id).toEqual(id); |
| 156 | expect(glyph.vb).toEqual(vbox); |
| 157 | expect(glyph.d.slice(0, plen)).toEqual(prefix); |
| 158 | } |
| 159 | |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 160 | it('should be configured with the correct number of glyphs', function () { |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 161 | var nGlyphs = 1 + glyphIds.length + badgeIds.length + spriteIds.length; |
| 162 | expect(nGlyphs).toEqual(numBaseGlyphs); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 163 | }); |
| 164 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 165 | it('should load the bird glyph', function() { |
| 166 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 167 | verifyGlyphLoadedInCache('bird', vbBird); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 168 | }); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 169 | |
| 170 | it('should load the regular glyphs', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 171 | gs.init(); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 172 | glyphIds.forEach(function (id) { |
| 173 | verifyGlyphLoadedInCache(id, vbGlyph); |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame] | 174 | }); |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 175 | }); |
| 176 | |
| 177 | it('should load the badge glyphs', function () { |
| 178 | gs.init(); |
| 179 | badgeIds.forEach(function (id) { |
| 180 | verifyGlyphLoadedInCache(id, vbBadge); |
| 181 | }); |
| 182 | }); |
| 183 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 184 | it('should load the sprites', function () { |
| 185 | gs.init(); |
| 186 | spriteIds.forEach(function (id) { |
| 187 | verifyGlyphLoadedInCache(id, vbGlyph); |
| 188 | }); |
| 189 | }); |
| 190 | |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 191 | |
| 192 | // define some glyphs that we want to install |
| 193 | |
| 194 | var testVbox = '0 0 1 1', |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 195 | triVbox = '0 0 12 12', |
| 196 | diaVbox = '0 0 15 15', |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 197 | dTriangle = 'M.5,.2l.3,.6,h-.6z', |
| 198 | dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z', |
| 199 | newGlyphs = { |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 200 | _viewbox: testVbox, |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 201 | triangle: dTriangle, |
| 202 | diamond: dDiamond |
| 203 | }, |
| 204 | dupGlyphs = { |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 205 | _viewbox: testVbox, |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 206 | router: dTriangle, |
| 207 | switch: dDiamond |
| 208 | }, |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 209 | altNewGlyphs = { |
| 210 | _triangle: triVbox, |
| 211 | triangle: dTriangle, |
| 212 | _diamond: diaVbox, |
| 213 | diamond: dDiamond |
| 214 | }, |
| 215 | altDupGlyphs = { |
| 216 | _router: triVbox, |
| 217 | router: dTriangle, |
| 218 | _switch: diaVbox, |
| 219 | switch: dDiamond |
| 220 | }, |
| 221 | badGlyphSet = { |
| 222 | triangle: dTriangle, |
| 223 | diamond: dDiamond |
| 224 | }, |
| 225 | warnMsg = 'GlyphService.registerGlyphs(): ', |
| 226 | warnMsgSet = 'GlyphService.registerGlyphSet(): ', |
| 227 | idCollision = warnMsg + 'ID collision: ', |
| 228 | idCollisionSet = warnMsgSet + 'ID collision: ', |
| 229 | missVbSet = warnMsgSet + 'no "_viewbox" property found', |
| 230 | missVbCustom = warnMsg + 'Missing viewbox property: ', |
| 231 | missVbTri = missVbCustom + '"_triangle"', |
| 232 | missVbDia = missVbCustom + '"_diamond"'; |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 233 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 234 | |
| 235 | it('should install new glyphs as a glyph-set', function () { |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 236 | gs.init(); |
| 237 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 238 | spyOn($log, 'warn'); |
| 239 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 240 | var ok = gs.registerGlyphSet(newGlyphs); |
| 241 | expect(ok).toBe(true); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 242 | expect($log.warn).not.toHaveBeenCalled(); |
| 243 | |
| 244 | expect(gs.ids().length).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 245 | verifyGlyphLoadedInCache('triangle', testVbox); |
| 246 | verifyGlyphLoadedInCache('diamond', testVbox); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 247 | }); |
| 248 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 249 | it('should not overwrite glyphs (via glyph-set) with dup IDs', function () { |
| 250 | gs.init(); |
| 251 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 252 | spyOn($log, 'warn'); |
| 253 | |
| 254 | var ok = gs.registerGlyphSet(dupGlyphs); |
| 255 | expect(ok).toBe(false); |
| 256 | expect($log.warn).toHaveBeenCalledWith(idCollisionSet + '"switch"'); |
| 257 | expect($log.warn).toHaveBeenCalledWith(idCollisionSet + '"router"'); |
| 258 | |
| 259 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 260 | // verify original glyphs still exist... |
| 261 | verifyGlyphLoadedInCache('router', vbGlyph); |
| 262 | verifyGlyphLoadedInCache('switch', vbGlyph); |
| 263 | }); |
| 264 | |
| 265 | it('should replace glyphs (via glyph-set) if asked nicely', function () { |
| 266 | gs.init(); |
| 267 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 268 | spyOn($log, 'warn'); |
| 269 | |
| 270 | var ok = gs.registerGlyphSet(dupGlyphs, true); |
| 271 | expect(ok).toBe(true); |
| 272 | expect($log.warn).not.toHaveBeenCalled(); |
| 273 | |
| 274 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 275 | // verify glyphs have been overwritten... |
| 276 | verifyGlyphLoadedInCache('router', testVbox, 'triangle'); |
| 277 | verifyGlyphLoadedInCache('switch', testVbox, 'diamond'); |
| 278 | }); |
| 279 | |
| 280 | it ('should complain if missing _viewbox in a glyph-set', function () { |
| 281 | gs.init(); |
| 282 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 283 | spyOn($log, 'warn'); |
| 284 | |
| 285 | var ok = gs.registerGlyphSet(badGlyphSet); |
| 286 | expect(ok).toBe(false); |
| 287 | expect($log.warn).toHaveBeenCalledWith(missVbSet); |
| 288 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 289 | }); |
| 290 | |
| 291 | it('should install new glyphs', function () { |
| 292 | gs.init(); |
| 293 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 294 | spyOn($log, 'warn'); |
| 295 | |
| 296 | var ok = gs.registerGlyphs(altNewGlyphs); |
| 297 | expect(ok).toBe(true); |
| 298 | expect($log.warn).not.toHaveBeenCalled(); |
| 299 | |
| 300 | expect(gs.ids().length).toEqual(numBaseGlyphs + 2); |
| 301 | verifyGlyphLoadedInCache('triangle', triVbox); |
| 302 | verifyGlyphLoadedInCache('diamond', diaVbox); |
| 303 | }); |
| 304 | |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 305 | it('should not overwrite glyphs with dup IDs', function () { |
| 306 | gs.init(); |
| 307 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 308 | spyOn($log, 'warn'); |
| 309 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 310 | var ok = gs.registerGlyphs(altDupGlyphs); |
| 311 | expect(ok).toBe(false); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 312 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"'); |
| 313 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"'); |
| 314 | |
| 315 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 316 | // verify original glyphs still exist... |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 317 | verifyGlyphLoadedInCache('router', vbGlyph); |
| 318 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 319 | }); |
| 320 | |
| 321 | it('should replace glyphs if asked nicely', function () { |
| 322 | gs.init(); |
| 323 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 324 | spyOn($log, 'warn'); |
| 325 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 326 | var ok = gs.registerGlyphs(altDupGlyphs, true); |
| 327 | expect(ok).toBe(true); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 328 | expect($log.warn).not.toHaveBeenCalled(); |
| 329 | |
| 330 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 331 | // verify glyphs have been overwritten... |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 332 | verifyGlyphLoadedInCache('router', triVbox, 'triangle'); |
| 333 | verifyGlyphLoadedInCache('switch', diaVbox, 'diamond'); |
| 334 | }); |
| 335 | |
| 336 | it ('should complain if missing custom viewbox', function () { |
| 337 | gs.init(); |
| 338 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 339 | spyOn($log, 'warn'); |
| 340 | |
| 341 | var ok = gs.registerGlyphs(badGlyphSet); |
| 342 | expect(ok).toBe(false); |
| 343 | expect($log.warn).toHaveBeenCalledWith(missVbTri); |
| 344 | expect($log.warn).toHaveBeenCalledWith(missVbDia); |
| 345 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 346 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 347 | |
| 348 | function verifyPathPrefix(elem, prefix) { |
| 349 | var plen = prefix.length, |
| 350 | d = elem.select('path').attr('d'); |
| 351 | expect(d.slice(0, plen)).toEqual(prefix); |
| 352 | } |
| 353 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 354 | function verifyLoadedInDom(id, vb, expPfxId) { |
| 355 | var pfxId = expPfxId || id, |
| 356 | symbol = d3Elem.select('#' + id); |
| 357 | expect(symbol.size()).toEqual(1); |
| 358 | expect(symbol.attr('viewBox')).toEqual(vb); |
| 359 | verifyPathPrefix(symbol, prefixLookup[pfxId]); |
| 360 | } |
| 361 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 362 | it('should load base glyphs into the DOM', function () { |
| 363 | gs.init(); |
| 364 | gs.loadDefs(d3Elem); |
| 365 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 366 | verifyLoadedInDom('bgpSpeaker', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 367 | }); |
| 368 | |
| 369 | it('should load custom glyphs into the DOM', function () { |
| 370 | gs.init(); |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 371 | gs.registerGlyphSet(newGlyphs); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 372 | gs.loadDefs(d3Elem); |
| 373 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 374 | verifyLoadedInDom('diamond', testVbox); |
| 375 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 376 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 377 | it('should load only specified glyphs into the DOM', function () { |
| 378 | gs.init(); |
| 379 | gs.loadDefs(d3Elem, ['crown', 'chain', 'node']); |
| 380 | expect(d3Elem.selectAll('symbol').size()).toEqual(3); |
| 381 | verifyLoadedInDom('crown', vbGlyph); |
| 382 | verifyLoadedInDom('chain', vbGlyph); |
| 383 | verifyLoadedInDom('node', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 384 | }); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 385 | |
| 386 | it('should add a glyph with default size', function () { |
| 387 | gs.init(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 388 | var retval = gs.addGlyph(svg, 'crown'); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 389 | var what = svg.selectAll('use'); |
| 390 | expect(what.size()).toEqual(1); |
| 391 | expect(what.attr('width')).toEqual('40'); |
| 392 | expect(what.attr('height')).toEqual('40'); |
| 393 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 394 | expect(what.classed('glyph')).toBeTruthy(); |
| 395 | expect(what.classed('overlay')).toBeFalsy(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 396 | |
| 397 | // check a couple on retval, which should be the same thing.. |
| 398 | expect(retval.attr('xlink:href')).toEqual('#crown'); |
| 399 | expect(retval.classed('glyph')).toBeTruthy(); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 400 | }); |
| 401 | |
| 402 | it('should add a glyph with given size', function () { |
| 403 | gs.init(); |
| 404 | gs.addGlyph(svg, 'crown', 37); |
| 405 | var what = svg.selectAll('use'); |
| 406 | expect(what.size()).toEqual(1); |
| 407 | expect(what.attr('width')).toEqual('37'); |
| 408 | expect(what.attr('height')).toEqual('37'); |
| 409 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 410 | expect(what.classed('glyph')).toBeTruthy(); |
| 411 | expect(what.classed('overlay')).toBeFalsy(); |
| 412 | }); |
| 413 | |
| 414 | it('should add a glyph marked as overlay', function () { |
| 415 | gs.init(); |
| 416 | gs.addGlyph(svg, 'crown', 20, true); |
| 417 | var what = svg.selectAll('use'); |
| 418 | expect(what.size()).toEqual(1); |
| 419 | expect(what.attr('width')).toEqual('20'); |
| 420 | expect(what.attr('height')).toEqual('20'); |
| 421 | expect(what.attr('xlink:href')).toEqual('#crown'); |
| 422 | expect(what.classed('glyph')).toBeTruthy(); |
| 423 | expect(what.classed('overlay')).toBeTruthy(); |
| 424 | }); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 425 | }); |