blob: a7a0ba811c1b0a38e4f85bc948e8e465ac5e934a [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt737c89f2015-01-28 12:23:19 -08003 *
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 -- SVG Util Service - Unit Tests
19 */
20describe('factory: fw/svg/svgUtil.js', function() {
Bri Prebilic Coleef091902015-04-28 16:53:47 -070021 var $log, fs, sus, svg, defs, force;
22
23 var noop = function () {};
Simon Hunt737c89f2015-01-28 12:23:19 -080024
25 beforeEach(module('onosUtil', 'onosSvg'));
26
27 beforeEach(inject(function (_$log_, FnService, SvgUtilService) {
28 $log = _$log_;
29 fs = FnService;
30 sus = SvgUtilService;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080031 svg = d3.select('body').append('svg').attr('id', 'mySvg');
Bri Prebilic Coleef091902015-04-28 16:53:47 -070032 defs = svg.append('defs');
33 force = d3.layout.force();
Simon Hunt737c89f2015-01-28 12:23:19 -080034 }));
35
36 afterEach(function () {
37 d3.select('svg').remove();
38 });
39
40 it('should define SvgUtilService', function () {
41 expect(sus).toBeDefined();
42 });
43
44 it('should define api functions', function () {
45 expect(fs.areFunctions(sus, [
Simon Hunt99ee1e22015-02-13 09:24:43 -080046 'createDragBehavior', 'loadGlowDefs', 'cat7',
Simon Hunt56004a82015-02-19 13:53:20 -080047 'translate', 'scale', 'skewX', 'rotate',
Simon Hunt99ee1e22015-02-13 09:24:43 -080048 'stripPx', 'safeId', 'visible'
Simon Hunt737c89f2015-01-28 12:23:19 -080049 ])).toBeTruthy();
50 });
51
Bri Prebilic Coleef091902015-04-28 16:53:47 -070052 // === createDragBehavior
53 // TODO: break up drag into separate functions for testing
54 // d3 needs better testing support...
Simon Hunt4b668592015-01-29 17:33:53 -080055
Bri Prebilic Coleef091902015-04-28 16:53:47 -070056 // Note: just checking to see if error message was called
57 // because jasmine spy isn't catching the right newline char
58 it('should complain if function given no parameters', function () {
59 spyOn($log, 'error');
60 expect(sus.createDragBehavior()).toBeNull();
61 expect($log.error).toHaveBeenCalled();
62 });
63
64 it('should complain if function is not given clickEnabled', function () {
65 spyOn($log, 'error');
66 expect(sus.createDragBehavior(force, noop, noop, noop)).toBeNull();
67 expect($log.error).toHaveBeenCalled();
68 });
69
70 it('should complain if function is not given dragEnabled', function () {
71 spyOn($log, 'error');
72 expect(sus.createDragBehavior(force, noop, noop)).toBeNull();
73 expect($log.error).toHaveBeenCalled();
74 });
75
76 it('should complain if function is not given atDragEnd', function () {
77 spyOn($log, 'error');
78 expect(sus.createDragBehavior(force, noop)).toBeNull();
79 expect($log.error).toHaveBeenCalled();
80 });
81
82 it('should complain if function is not given selectCb', function () {
83 spyOn($log, 'error');
84 expect(sus.createDragBehavior(force)).toBeNull();
85 expect($log.error).toHaveBeenCalled();
86 });
87
88 // === loadGlowDefs
89 function checkAttrs(glow, r, g, b) {
90 var filterEffects, feColor, feBlur, feMerge, feMergeNodes;
91
92 // filter attrs
93 expect(glow.attr('x')).toBe('-50%');
94 expect(glow.attr('y')).toBe('-50%');
95 expect(glow.attr('width')).toBe('200%');
96 expect(glow.attr('height')).toBe('200%');
97
98 filterEffects = d3.selectAll(glow.node().childNodes);
99 expect(filterEffects.size()).toBe(3);
100
101 // Note: d3 didn't recognize 'feColorMatrix' and others as valid selectors
102 // this is a work around
103 feColor = d3.select(filterEffects[0].shift());
104 feBlur = d3.select(filterEffects[0].shift());
105 feMerge = d3.select(filterEffects[0].shift());
106
107 // feColorMatrix attrs
108 expect(feColor.empty()).toBe(false);
109 expect(feColor.attr('type')).toBe('matrix');
110 expect(feColor.attr('values')).toBe(
111 '0 0 0 0 ' + r + ' ' +
112 '0 0 0 0 ' + g + ' ' +
113 '0 0 0 0 ' + b + ' ' +
114 '0 0 0 1 0 '
115 );
116
117 // feGuassianBlur attrs
118 expect(feBlur.empty()).toBe(false);
119 expect(feBlur.attr('stdDeviation')).toBe('3');
120 expect(feBlur.attr('result')).toBe('coloredBlur');
121
122 // feMerge attrs
123 feMergeNodes = d3.selectAll(feMerge.node().childNodes);
124 expect(feMergeNodes.size()).toBe(2);
125 expect(d3.select(feMergeNodes[0][0]).attr('in')).toBe('coloredBlur');
126 expect(d3.select(feMergeNodes[0][1]).attr('in')).toBe('SourceGraphic');
127 }
128
129 it('should load glow definitions', function () {
130 var blue, yellow;
131 sus.loadGlowDefs(defs);
132
133 expect(defs.empty()).toBe(false);
134 expect((defs.selectAll('filter')).size()).toBe(2);
135
136 // blue-glow specific
137 blue = defs.select('#blue-glow');
138 expect(blue.empty()).toBe(false);
139 checkAttrs(blue, 0.0, 0.0, 0.7);
140
141 // yellow-glow specific
142 yellow = defs.select('#yellow-glow');
143 expect(yellow.empty()).toBe(false);
144 checkAttrs(yellow, 1.0, 1.0, 0.3);
145 });
Simon Hunt4b668592015-01-29 17:33:53 -0800146
Simon Hunt48e61672015-01-30 14:48:25 -0800147 // === cat7
148
149 it('should define two methods on the api', function () {
150 var cat7 = sus.cat7();
151 expect(fs.areFunctions(cat7, [
152 'testCard', 'getColor'
153 ])).toBeTruthy();
154 });
155
156 it('should provide a certain shade of blue', function () {
157 expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#3E5780');
158 });
159
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700160 xit('should not matter what the ID really is for shade of blue', function () {
Simon Hunt48e61672015-01-30 14:48:25 -0800161 expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#3E5780');
162 });
163
164 it('should provide different shade of blue for muted', function () {
165 expect(sus.cat7().getColor('foo', true, 'light')).toEqual('#A8B8CC');
166 });
167
168
169 it('should provide an alternate (dark) shade of blue', function () {
Simon Hunt27e153a2015-02-02 18:45:44 -0800170 expect(sus.cat7().getColor('foo', false, 'dark')).toEqual('#304860');
Simon Hunt48e61672015-01-30 14:48:25 -0800171 });
172
173 it('should provide an alternate (dark) shade of blue for muted', function () {
Simon Hunt5724fb42015-02-05 16:59:40 -0800174 expect(sus.cat7().getColor('foo', true, 'dark')).toEqual('#304860');
Simon Hunt48e61672015-01-30 14:48:25 -0800175 });
176
177 it('should iterate across the colors', function () {
178 expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#3E5780');
179 expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#78533B');
180 expect(sus.cat7().getColor('baz', false, 'light')).toEqual('#CB4D28');
181 expect(sus.cat7().getColor('goo', false, 'light')).toEqual('#018D61');
182 expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#8A2979');
183 expect(sus.cat7().getColor('pip', false, 'light')).toEqual('#006D73');
184 expect(sus.cat7().getColor('sdh', false, 'light')).toEqual('#56AF00');
185 // and cycle back to the first color for item #8
186 expect(sus.cat7().getColor('bri', false, 'light')).toEqual('#3E5780');
187 // and return the same color for the same ID
188 expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#8A2979');
189 });
Simon Hunt4b668592015-01-29 17:33:53 -0800190
Simon Hunt56004a82015-02-19 13:53:20 -0800191 // === translate(), scale(), skewX(), rotate()
Simon Huntc9b73162015-01-29 14:02:15 -0800192
193 it('should translate from two args', function () {
194 expect(sus.translate(1,2)).toEqual('translate(1,2)');
195 });
196
197 it('should translate from an array', function () {
198 expect(sus.translate([3,4])).toEqual('translate(3,4)');
199 });
200
Simon Hunt56004a82015-02-19 13:53:20 -0800201 it('should scale', function () {
202 expect(sus.scale(1.5,2.5)).toEqual('scale(1.5,2.5)');
203 });
204
205 it('should skewX', function () {
206 expect(sus.skewX(3.14)).toEqual('skewX(3.14)');
207 });
208
209 it('should rotate', function () {
210 expect(sus.rotate(45)).toEqual('rotate(45)');
211 });
212
Simon Hunt4b668592015-01-29 17:33:53 -0800213
214 // === stripPx()
215
216 it('should not affect a number', function () {
217 expect(sus.stripPx('4')).toEqual('4');
218 });
219
220 it('should remove trailing px', function () {
221 expect(sus.stripPx('4px')).toEqual('4');
222 });
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800223
Simon Hunt18bf9822015-02-12 17:35:45 -0800224 // === visible()
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800225
226 it('should hide and show an element', function () {
227 var r = svg.append('rect');
228
Simon Hunt18bf9822015-02-12 17:35:45 -0800229 sus.visible(r, false);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800230 expect(r.style('visibility')).toEqual('hidden');
Simon Hunt18bf9822015-02-12 17:35:45 -0800231 expect(sus.visible(r)).toBe(false);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800232
Simon Hunt18bf9822015-02-12 17:35:45 -0800233 sus.visible(r, true);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800234 expect(r.style('visibility')).toEqual('visible');
Simon Hunt18bf9822015-02-12 17:35:45 -0800235 expect(sus.visible(r)).toBe(true);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800236 });
Simon Hunt737c89f2015-01-28 12:23:19 -0800237});