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 | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 23 | var $log, fs, gs; |
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; |
| 36 | })); |
| 37 | |
| 38 | it('should define GlyphService', function () { |
| 39 | expect(gs).toBeDefined(); |
| 40 | }); |
| 41 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 42 | it('should define api functions', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 43 | expect(fs.areFunctions(gs, [ |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 44 | 'init', 'register', 'ids', 'glyph', 'loadDefs' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 45 | ])).toBeTruthy(); |
| 46 | }); |
| 47 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 48 | it('should start with no glyphs loaded', function () { |
| 49 | expect(gs.ids()).toEqual([]); |
| 50 | }); |
| 51 | |
| 52 | it('should load the base set of glyphs', function () { |
| 53 | gs.init(); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame^] | 54 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | function verifyGlyphLoaded(id, vbox, prefix) { |
| 58 | var glyph = gs.glyph(id), |
| 59 | plen = prefix.length; |
| 60 | expect(fs.contains(gs.ids(), id)).toBeTruthy(); |
| 61 | expect(glyph).toBeDefined(); |
| 62 | expect(glyph.id).toEqual(id); |
| 63 | expect(glyph.vb).toEqual(vbox); |
| 64 | expect(glyph.d.slice(0, plen)).toEqual(prefix); |
| 65 | } |
| 66 | |
| 67 | it('should load the bird glyph', function() { |
| 68 | gs.init(); |
| 69 | verifyGlyphLoaded('bird', vbBird, 'M427.7,300.4'); |
| 70 | }); |
| 71 | it('should load the unknown glyph', function() { |
| 72 | gs.init(); |
| 73 | verifyGlyphLoaded('unknown', vbGlyph, 'M35,40a5'); |
| 74 | }); |
| 75 | it('should load the node glyph', function() { |
| 76 | gs.init(); |
| 77 | verifyGlyphLoaded('node', vbGlyph, 'M15,100a5'); |
| 78 | }); |
| 79 | it('should load the switch glyph', function() { |
| 80 | gs.init(); |
| 81 | verifyGlyphLoaded('switch', vbGlyph, 'M10,20a10'); |
| 82 | }); |
| 83 | it('should load the roadm glyph', function() { |
| 84 | gs.init(); |
| 85 | verifyGlyphLoaded('roadm', vbGlyph, 'M10,35l25-'); |
| 86 | }); |
| 87 | it('should load the endstation glyph', function() { |
| 88 | gs.init(); |
| 89 | verifyGlyphLoaded('endstation', vbGlyph, 'M10,15a5,5'); |
| 90 | }); |
| 91 | it('should load the router glyph', function() { |
| 92 | gs.init(); |
| 93 | verifyGlyphLoaded('router', vbGlyph, 'M10,55A45,45'); |
| 94 | }); |
| 95 | it('should load the bgpSpeaker glyph', function() { |
| 96 | gs.init(); |
| 97 | verifyGlyphLoaded('bgpSpeaker', vbGlyph, 'M10,40a45,35'); |
| 98 | }); |
| 99 | it('should load the chain glyph', function() { |
| 100 | gs.init(); |
| 101 | verifyGlyphLoaded('chain', vbGlyph, 'M60.4,77.6c-'); |
| 102 | }); |
| 103 | it('should load the crown glyph', function() { |
| 104 | gs.init(); |
| 105 | verifyGlyphLoaded('crown', vbGlyph, 'M99.5,21.6c0'); |
| 106 | }); |
| 107 | it('should load the uiAttached glyph', function() { |
| 108 | gs.init(); |
| 109 | verifyGlyphLoaded('uiAttached', vbBadge, 'M2,2.5a.5,.5'); |
| 110 | }); |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame^] | 111 | |
| 112 | // define some glyphs that we want to install |
| 113 | |
| 114 | var testVbox = '0 0 1 1', |
| 115 | dTriangle = 'M.5,.2l.3,.6,h-.6z', |
| 116 | dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z', |
| 117 | newGlyphs = { |
| 118 | triangle: dTriangle, |
| 119 | diamond: dDiamond |
| 120 | }, |
| 121 | dupGlyphs = { |
| 122 | router: dTriangle, |
| 123 | switch: dDiamond |
| 124 | }, |
| 125 | idCollision = 'GlyphService.register(): ID collision: '; |
| 126 | |
| 127 | it('should install new glyphs', function () { |
| 128 | gs.init(); |
| 129 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 130 | spyOn($log, 'warn'); |
| 131 | |
| 132 | var ok = gs.register(testVbox, newGlyphs); |
| 133 | expect(ok).toBeTruthy(); |
| 134 | expect($log.warn).not.toHaveBeenCalled(); |
| 135 | |
| 136 | expect(gs.ids().length).toEqual(numBaseGlyphs + 2); |
| 137 | verifyGlyphLoaded('triangle', testVbox, 'M.5,.2'); |
| 138 | verifyGlyphLoaded('diamond', testVbox, 'M.2,.5'); |
| 139 | }); |
| 140 | |
| 141 | it('should not overwrite glyphs with dup IDs', function () { |
| 142 | gs.init(); |
| 143 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 144 | spyOn($log, 'warn'); |
| 145 | |
| 146 | var ok = gs.register(testVbox, dupGlyphs); |
| 147 | expect(ok).toBeFalsy(); |
| 148 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"'); |
| 149 | expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"'); |
| 150 | |
| 151 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 152 | // verify original glyphs still exist... |
| 153 | verifyGlyphLoaded('router', vbGlyph, 'M10,55A45,45'); |
| 154 | verifyGlyphLoaded('switch', vbGlyph, 'M10,20a10'); |
| 155 | }); |
| 156 | |
| 157 | it('should replace glyphs if asked nicely', function () { |
| 158 | gs.init(); |
| 159 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 160 | spyOn($log, 'warn'); |
| 161 | |
| 162 | var ok = gs.register(testVbox, dupGlyphs, true); |
| 163 | expect(ok).toBeTruthy(); |
| 164 | expect($log.warn).not.toHaveBeenCalled(); |
| 165 | |
| 166 | expect(gs.ids().length).toEqual(numBaseGlyphs); |
| 167 | // verify glyphs have been overwritten... |
| 168 | verifyGlyphLoaded('router', testVbox, 'M.5,.2'); |
| 169 | verifyGlyphLoaded('switch', testVbox, 'M.2,.5'); |
| 170 | }); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 171 | }); |