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