Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 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 -- SVG Util Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/svg/svgUtil.js', function() { |
Bri Prebilic Cole | ef09190 | 2015-04-28 16:53:47 -0700 | [diff] [blame] | 21 | var $log, fs, sus, svg, defs, force; |
| 22 | |
| 23 | var noop = function () {}; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 24 | |
| 25 | beforeEach(module('onosUtil', 'onosSvg')); |
| 26 | |
| 27 | beforeEach(inject(function (_$log_, FnService, SvgUtilService) { |
| 28 | $log = _$log_; |
| 29 | fs = FnService; |
| 30 | sus = SvgUtilService; |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 31 | svg = d3.select('body').append('svg').attr('id', 'mySvg'); |
Bri Prebilic Cole | ef09190 | 2015-04-28 16:53:47 -0700 | [diff] [blame] | 32 | defs = svg.append('defs'); |
| 33 | force = d3.layout.force(); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 34 | })); |
| 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 Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 46 | 'createDragBehavior', 'loadGlowDefs', 'cat7', |
Simon Hunt | 56004a8 | 2015-02-19 13:53:20 -0800 | [diff] [blame] | 47 | 'translate', 'scale', 'skewX', 'rotate', |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 48 | 'stripPx', 'safeId', 'visible' |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 49 | ])).toBeTruthy(); |
| 50 | }); |
| 51 | |
Bri Prebilic Cole | ef09190 | 2015-04-28 16:53:47 -0700 | [diff] [blame] | 52 | // === createDragBehavior |
| 53 | // TODO: break up drag into separate functions for testing |
| 54 | // d3 needs better testing support... |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 55 | |
Bri Prebilic Cole | ef09190 | 2015-04-28 16:53:47 -0700 | [diff] [blame] | 56 | // 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 Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 146 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 147 | // === 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 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 156 | it('should provide blue', function () { |
| 157 | expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#5b99d2'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 158 | }); |
| 159 | |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 160 | it('should provide lt-blue', function () { |
| 161 | expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#66cef6'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 162 | }); |
| 163 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 164 | it('should provide paler shade of blue for muted', function () { |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 165 | expect(sus.cat7().getColor('foo', true, 'light')).toEqual('#9ebedf'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 166 | }); |
| 167 | |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 168 | it('should provide an alternate (dark) shade of blue', function () { |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 169 | expect(sus.cat7().getColor('foo', false, 'dark')).toEqual('#5b99d2'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 170 | }); |
| 171 | |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 172 | it('should provide an alternate (dark) shade of blue for muted', function () { |
| 173 | expect(sus.cat7().getColor('foo', true, 'dark')).toEqual('#9ebedf'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 174 | }); |
| 175 | |
| 176 | it('should iterate across the colors', function () { |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 177 | expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#5b99d2'); |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 178 | expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#66cef6'); |
| 179 | expect(sus.cat7().getColor('baz', false, 'light')).toEqual('#d05a55'); |
| 180 | expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#0f9d58'); |
| 181 | expect(sus.cat7().getColor('sdh', false, 'light')).toEqual('#ba7941'); |
| 182 | expect(sus.cat7().getColor('goo', false, 'light')).toEqual('#3dc0bf'); |
| 183 | expect(sus.cat7().getColor('pip', false, 'light')).toEqual('#56af00'); |
Simon Hunt | df2a4e6 | 2016-08-11 14:21:56 -0700 | [diff] [blame] | 184 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 185 | // and cycle back to the first color for item #8 |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 186 | expect(sus.cat7().getColor('bri', false, 'light')).toEqual('#5b99d2'); |
Simon Hunt | df2a4e6 | 2016-08-11 14:21:56 -0700 | [diff] [blame] | 187 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 188 | // and return the same color for the same ID |
Simon Hunt | bb0f267 | 2017-03-28 18:52:59 -0700 | [diff] [blame] | 189 | expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#0f9d58'); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 190 | }); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 191 | |
Simon Hunt | 56004a8 | 2015-02-19 13:53:20 -0800 | [diff] [blame] | 192 | // === translate(), scale(), skewX(), rotate() |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 193 | |
| 194 | it('should translate from two args', function () { |
| 195 | expect(sus.translate(1,2)).toEqual('translate(1,2)'); |
| 196 | }); |
| 197 | |
| 198 | it('should translate from an array', function () { |
| 199 | expect(sus.translate([3,4])).toEqual('translate(3,4)'); |
| 200 | }); |
| 201 | |
Simon Hunt | 56004a8 | 2015-02-19 13:53:20 -0800 | [diff] [blame] | 202 | it('should scale', function () { |
| 203 | expect(sus.scale(1.5,2.5)).toEqual('scale(1.5,2.5)'); |
| 204 | }); |
| 205 | |
| 206 | it('should skewX', function () { |
| 207 | expect(sus.skewX(3.14)).toEqual('skewX(3.14)'); |
| 208 | }); |
| 209 | |
| 210 | it('should rotate', function () { |
| 211 | expect(sus.rotate(45)).toEqual('rotate(45)'); |
| 212 | }); |
| 213 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 214 | |
| 215 | // === stripPx() |
| 216 | |
| 217 | it('should not affect a number', function () { |
| 218 | expect(sus.stripPx('4')).toEqual('4'); |
| 219 | }); |
| 220 | |
| 221 | it('should remove trailing px', function () { |
| 222 | expect(sus.stripPx('4px')).toEqual('4'); |
| 223 | }); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 224 | |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 225 | // === visible() |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 226 | |
| 227 | it('should hide and show an element', function () { |
| 228 | var r = svg.append('rect'); |
| 229 | |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 230 | sus.visible(r, false); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 231 | expect(r.style('visibility')).toEqual('hidden'); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 232 | expect(sus.visible(r)).toBe(false); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 233 | |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 234 | sus.visible(r, true); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 235 | expect(r.style('visibility')).toEqual('visible'); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 236 | expect(sus.visible(r)).toBe(true); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 237 | }); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 238 | }); |