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 |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | describe('factory: fw/svg/glyph.js', function() { |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 23 | var $log, fs, gs, d3Elem; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 24 | |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame^] | 25 | var numBaseGlyphs = 13, |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 26 | vbBird = '352 224 113 112', |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 27 | vbGlyph = '0 0 110 110', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 28 | 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 Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame^] | 41 | checkMark: 'M2.644,4.531c0', |
| 42 | xMark: 'M8.967,7.196C8.172', |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 43 | |
| 44 | // our test ones.. |
| 45 | triangle: 'M.5,.2', |
| 46 | diamond: 'M.2,.5' |
| 47 | }; |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 49 | beforeEach(module('onosUtil', 'onosSvg')); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 50 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 51 | beforeEach(inject(function (_$log_, FnService, GlyphService) { |
| 52 | $log = _$log_; |
| 53 | fs = FnService; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 54 | gs = GlyphService; |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 55 | d3Elem = d3.select('body').append('defs').attr('id', 'myDefs'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 56 | })); |
| 57 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 58 | afterEach(function () { |
| 59 | d3.select('#myDefs').remove(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 60 | gs.clear(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 61 | }); |
| 62 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 63 | it('should define GlyphService', function () { |
| 64 | expect(gs).toBeDefined(); |
| 65 | }); |
| 66 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 67 | it('should define api functions', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 68 | expect(fs.areFunctions(gs, [ |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 69 | 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 70 | ])).toBeTruthy(); |
| 71 | }); |
| 72 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 73 | it('should start with no glyphs loaded', function () { |
| 74 | expect(gs.ids()).toEqual([]); |
| 75 | }); |
| 76 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 77 | it('should load the base set of glyphs into the cache', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 78 | gs.init(); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 79 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 80 | }); |
| 81 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 82 | it('should remove glyphs from the cache on clear', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 83 | gs.init(); |
| 84 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 85 | gs.clear(); |
| 86 | expect(gs.ids().length).toEqual(0); |
| 87 | }); |
| 88 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 89 | function verifyGlyphLoadedInCache(id, vbox, expPfxId) { |
| 90 | var pfxId = expPfxId || id, |
| 91 | glyph = gs.glyph(id), |
| 92 | prefix = prefixLookup[pfxId], |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 93 | 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 103 | verifyGlyphLoadedInCache('bird', vbBird); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 104 | }); |
| 105 | it('should load the unknown glyph', function() { |
| 106 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 107 | verifyGlyphLoadedInCache('unknown', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 108 | }); |
| 109 | it('should load the node glyph', function() { |
| 110 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 111 | verifyGlyphLoadedInCache('node', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 112 | }); |
| 113 | it('should load the switch glyph', function() { |
| 114 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 115 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 116 | }); |
| 117 | it('should load the roadm glyph', function() { |
| 118 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 119 | verifyGlyphLoadedInCache('roadm', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 120 | }); |
| 121 | it('should load the endstation glyph', function() { |
| 122 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 123 | verifyGlyphLoadedInCache('endstation', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 124 | }); |
| 125 | it('should load the router glyph', function() { |
| 126 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 127 | verifyGlyphLoadedInCache('router', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 128 | }); |
| 129 | it('should load the bgpSpeaker glyph', function() { |
| 130 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 131 | verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 132 | }); |
| 133 | it('should load the chain glyph', function() { |
| 134 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 135 | verifyGlyphLoadedInCache('chain', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 136 | }); |
| 137 | it('should load the crown glyph', function() { |
| 138 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 139 | verifyGlyphLoadedInCache('crown', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 140 | }); |
| 141 | it('should load the uiAttached glyph', function() { |
| 142 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 143 | verifyGlyphLoadedInCache('uiAttached', vbBadge); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 144 | }); |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame^] | 145 | 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 Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 153 | |
| 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 179 | verifyGlyphLoadedInCache('triangle', testVbox); |
| 180 | verifyGlyphLoadedInCache('diamond', testVbox); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 181 | }); |
| 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 195 | verifyGlyphLoadedInCache('router', vbGlyph); |
| 196 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 197 | }); |
| 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 210 | verifyGlyphLoadedInCache('router', testVbox, 'triangle'); |
| 211 | verifyGlyphLoadedInCache('switch', testVbox, 'diamond'); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 212 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 213 | |
| 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 220 | 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 Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 228 | 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 232 | verifyLoadedInDom('bgpSpeaker', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 233 | }); |
| 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 Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 240 | verifyLoadedInDom('diamond', testVbox); |
| 241 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 242 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 243 | 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 Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 250 | }); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 251 | }); |