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