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 | |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 25 | var numBaseGlyphs = 11, |
| 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', |
| 41 | |
| 42 | // our test ones.. |
| 43 | triangle: 'M.5,.2', |
| 44 | diamond: 'M.2,.5' |
| 45 | }; |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 46 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 47 | beforeEach(module('onosUtil', 'onosSvg')); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 49 | beforeEach(inject(function (_$log_, FnService, GlyphService) { |
| 50 | $log = _$log_; |
| 51 | fs = FnService; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 52 | gs = GlyphService; |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 53 | d3Elem = d3.select('body').append('defs').attr('id', 'myDefs'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 54 | })); |
| 55 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 56 | afterEach(function () { |
| 57 | d3.select('#myDefs').remove(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 58 | gs.clear(); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 59 | }); |
| 60 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 61 | it('should define GlyphService', function () { |
| 62 | expect(gs).toBeDefined(); |
| 63 | }); |
| 64 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 65 | it('should define api functions', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 66 | expect(fs.areFunctions(gs, [ |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 67 | 'clear', 'init', 'register', 'ids', 'glyph', 'loadDefs' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 68 | ])).toBeTruthy(); |
| 69 | }); |
| 70 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 71 | it('should start with no glyphs loaded', function () { |
| 72 | expect(gs.ids()).toEqual([]); |
| 73 | }); |
| 74 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 75 | it('should load the base set of glyphs into the cache', function () { |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 76 | gs.init(); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 77 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 78 | }); |
| 79 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 80 | it('should remove glyphs from the cache on clear', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 81 | gs.init(); |
| 82 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 83 | gs.clear(); |
| 84 | expect(gs.ids().length).toEqual(0); |
| 85 | }); |
| 86 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 87 | function verifyGlyphLoadedInCache(id, vbox, expPfxId) { |
| 88 | var pfxId = expPfxId || id, |
| 89 | glyph = gs.glyph(id), |
| 90 | prefix = prefixLookup[pfxId], |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 91 | plen = prefix.length; |
| 92 | expect(fs.contains(gs.ids(), id)).toBeTruthy(); |
| 93 | expect(glyph).toBeDefined(); |
| 94 | expect(glyph.id).toEqual(id); |
| 95 | expect(glyph.vb).toEqual(vbox); |
| 96 | expect(glyph.d.slice(0, plen)).toEqual(prefix); |
| 97 | } |
| 98 | |
| 99 | it('should load the bird glyph', function() { |
| 100 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 101 | verifyGlyphLoadedInCache('bird', vbBird); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 102 | }); |
| 103 | it('should load the unknown glyph', function() { |
| 104 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 105 | verifyGlyphLoadedInCache('unknown', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 106 | }); |
| 107 | it('should load the node glyph', function() { |
| 108 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 109 | verifyGlyphLoadedInCache('node', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 110 | }); |
| 111 | it('should load the switch glyph', function() { |
| 112 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 113 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 114 | }); |
| 115 | it('should load the roadm glyph', function() { |
| 116 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 117 | verifyGlyphLoadedInCache('roadm', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 118 | }); |
| 119 | it('should load the endstation glyph', function() { |
| 120 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 121 | verifyGlyphLoadedInCache('endstation', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 122 | }); |
| 123 | it('should load the router glyph', function() { |
| 124 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 125 | verifyGlyphLoadedInCache('router', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 126 | }); |
| 127 | it('should load the bgpSpeaker glyph', function() { |
| 128 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 129 | verifyGlyphLoadedInCache('bgpSpeaker', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 130 | }); |
| 131 | it('should load the chain glyph', function() { |
| 132 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 133 | verifyGlyphLoadedInCache('chain', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 134 | }); |
| 135 | it('should load the crown glyph', function() { |
| 136 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 137 | verifyGlyphLoadedInCache('crown', vbGlyph); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 138 | }); |
| 139 | it('should load the uiAttached glyph', function() { |
| 140 | gs.init(); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 141 | verifyGlyphLoadedInCache('uiAttached', vbBadge); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 142 | }); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 143 | |
| 144 | // define some glyphs that we want to install |
| 145 | |
| 146 | var testVbox = '0 0 1 1', |
| 147 | dTriangle = 'M.5,.2l.3,.6,h-.6z', |
| 148 | dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z', |
| 149 | newGlyphs = { |
| 150 | triangle: dTriangle, |
| 151 | diamond: dDiamond |
| 152 | }, |
| 153 | dupGlyphs = { |
| 154 | router: dTriangle, |
| 155 | switch: dDiamond |
| 156 | }, |
| 157 | idCollision = 'GlyphService.register(): ID collision: '; |
| 158 | |
| 159 | it('should install new glyphs', function () { |
| 160 | gs.init(); |
| 161 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 162 | spyOn($log, 'warn'); |
| 163 | |
| 164 | var ok = gs.register(testVbox, newGlyphs); |
| 165 | expect(ok).toBeTruthy(); |
| 166 | expect($log.warn).not.toHaveBeenCalled(); |
| 167 | |
| 168 | expect(gs.ids().length).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 169 | verifyGlyphLoadedInCache('triangle', testVbox); |
| 170 | verifyGlyphLoadedInCache('diamond', testVbox); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 171 | }); |
| 172 | |
| 173 | it('should not overwrite glyphs with dup IDs', function () { |
| 174 | gs.init(); |
| 175 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 176 | spyOn($log, 'warn'); |
| 177 | |
| 178 | var ok = gs.register(testVbox, dupGlyphs); |
| 179 | expect(ok).toBeFalsy(); |
| 180 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"'); |
| 181 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"'); |
| 182 | |
| 183 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 184 | // verify original glyphs still exist... |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 185 | verifyGlyphLoadedInCache('router', vbGlyph); |
| 186 | verifyGlyphLoadedInCache('switch', vbGlyph); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 187 | }); |
| 188 | |
| 189 | it('should replace glyphs if asked nicely', function () { |
| 190 | gs.init(); |
| 191 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 192 | spyOn($log, 'warn'); |
| 193 | |
| 194 | var ok = gs.register(testVbox, dupGlyphs, true); |
| 195 | expect(ok).toBeTruthy(); |
| 196 | expect($log.warn).not.toHaveBeenCalled(); |
| 197 | |
| 198 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 199 | // verify glyphs have been overwritten... |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 200 | verifyGlyphLoadedInCache('router', testVbox, 'triangle'); |
| 201 | verifyGlyphLoadedInCache('switch', testVbox, 'diamond'); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 202 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 203 | |
| 204 | function verifyPathPrefix(elem, prefix) { |
| 205 | var plen = prefix.length, |
| 206 | d = elem.select('path').attr('d'); |
| 207 | expect(d.slice(0, plen)).toEqual(prefix); |
| 208 | } |
| 209 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 210 | function verifyLoadedInDom(id, vb, expPfxId) { |
| 211 | var pfxId = expPfxId || id, |
| 212 | symbol = d3Elem.select('#' + id); |
| 213 | expect(symbol.size()).toEqual(1); |
| 214 | expect(symbol.attr('viewBox')).toEqual(vb); |
| 215 | verifyPathPrefix(symbol, prefixLookup[pfxId]); |
| 216 | } |
| 217 | |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 218 | it('should load base glyphs into the DOM', function () { |
| 219 | gs.init(); |
| 220 | gs.loadDefs(d3Elem); |
| 221 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 222 | verifyLoadedInDom('bgpSpeaker', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 223 | }); |
| 224 | |
| 225 | it('should load custom glyphs into the DOM', function () { |
| 226 | gs.init(); |
| 227 | gs.register(testVbox, newGlyphs); |
| 228 | gs.loadDefs(d3Elem); |
| 229 | expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2); |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 230 | verifyLoadedInDom('diamond', testVbox); |
| 231 | }); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 232 | |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame^] | 233 | it('should load only specified glyphs into the DOM', function () { |
| 234 | gs.init(); |
| 235 | gs.loadDefs(d3Elem, ['crown', 'chain', 'node']); |
| 236 | expect(d3Elem.selectAll('symbol').size()).toEqual(3); |
| 237 | verifyLoadedInDom('crown', vbGlyph); |
| 238 | verifyLoadedInDom('chain', vbGlyph); |
| 239 | verifyLoadedInDom('node', vbGlyph); |
Simon Hunt | 670e825 | 2015-01-06 18:31:30 -0800 | [diff] [blame] | 240 | }); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 241 | }); |