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