blob: 4164492cf192c578fb0dee340db23401509fec6e [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -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 -- SVG Util Service - Unit Tests
19 */
20describe('factory: fw/svg/svgUtil.js', function() {
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080021 var $log, fs, sus, svg, d3Elem;
Simon Hunt737c89f2015-01-28 12:23:19 -080022
23 beforeEach(module('onosUtil', 'onosSvg'));
24
25 beforeEach(inject(function (_$log_, FnService, SvgUtilService) {
26 $log = _$log_;
27 fs = FnService;
28 sus = SvgUtilService;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080029 svg = d3.select('body').append('svg').attr('id', 'mySvg');
30 d3Elem = svg.append('defs');
Simon Hunt737c89f2015-01-28 12:23:19 -080031 }));
32
33 afterEach(function () {
34 d3.select('svg').remove();
35 });
36
37 it('should define SvgUtilService', function () {
38 expect(sus).toBeDefined();
39 });
40
41 it('should define api functions', function () {
42 expect(fs.areFunctions(sus, [
Simon Hunt99ee1e22015-02-13 09:24:43 -080043 'createDragBehavior', 'loadGlowDefs', 'cat7',
Simon Hunt56004a82015-02-19 13:53:20 -080044 'translate', 'scale', 'skewX', 'rotate',
Simon Hunt99ee1e22015-02-13 09:24:43 -080045 'stripPx', 'safeId', 'visible'
Simon Hunt737c89f2015-01-28 12:23:19 -080046 ])).toBeTruthy();
47 });
48
Simon Hunt4b668592015-01-29 17:33:53 -080049
50 // TODO: add unit tests for drag behavior
Simon Hunt99ee1e22015-02-13 09:24:43 -080051 // TODO: add unit tests for loadGlowDefs
Simon Hunt4b668592015-01-29 17:33:53 -080052
Simon Hunt48e61672015-01-30 14:48:25 -080053 // === cat7
54
55 it('should define two methods on the api', function () {
56 var cat7 = sus.cat7();
57 expect(fs.areFunctions(cat7, [
58 'testCard', 'getColor'
59 ])).toBeTruthy();
60 });
61
62 it('should provide a certain shade of blue', function () {
63 expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#3E5780');
64 });
65
66 it('should not matter what the ID really is for shade of blue', function () {
67 expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#3E5780');
68 });
69
70 it('should provide different shade of blue for muted', function () {
71 expect(sus.cat7().getColor('foo', true, 'light')).toEqual('#A8B8CC');
72 });
73
74
75 it('should provide an alternate (dark) shade of blue', function () {
Simon Hunt27e153a2015-02-02 18:45:44 -080076 expect(sus.cat7().getColor('foo', false, 'dark')).toEqual('#304860');
Simon Hunt48e61672015-01-30 14:48:25 -080077 });
78
79 it('should provide an alternate (dark) shade of blue for muted', function () {
Simon Hunt5724fb42015-02-05 16:59:40 -080080 expect(sus.cat7().getColor('foo', true, 'dark')).toEqual('#304860');
Simon Hunt48e61672015-01-30 14:48:25 -080081 });
82
83 it('should iterate across the colors', function () {
84 expect(sus.cat7().getColor('foo', false, 'light')).toEqual('#3E5780');
85 expect(sus.cat7().getColor('bar', false, 'light')).toEqual('#78533B');
86 expect(sus.cat7().getColor('baz', false, 'light')).toEqual('#CB4D28');
87 expect(sus.cat7().getColor('goo', false, 'light')).toEqual('#018D61');
88 expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#8A2979');
89 expect(sus.cat7().getColor('pip', false, 'light')).toEqual('#006D73');
90 expect(sus.cat7().getColor('sdh', false, 'light')).toEqual('#56AF00');
91 // and cycle back to the first color for item #8
92 expect(sus.cat7().getColor('bri', false, 'light')).toEqual('#3E5780');
93 // and return the same color for the same ID
94 expect(sus.cat7().getColor('zoo', false, 'light')).toEqual('#8A2979');
95 });
Simon Hunt4b668592015-01-29 17:33:53 -080096
Simon Hunt56004a82015-02-19 13:53:20 -080097 // === translate(), scale(), skewX(), rotate()
Simon Huntc9b73162015-01-29 14:02:15 -080098
99 it('should translate from two args', function () {
100 expect(sus.translate(1,2)).toEqual('translate(1,2)');
101 });
102
103 it('should translate from an array', function () {
104 expect(sus.translate([3,4])).toEqual('translate(3,4)');
105 });
106
Simon Hunt56004a82015-02-19 13:53:20 -0800107 it('should scale', function () {
108 expect(sus.scale(1.5,2.5)).toEqual('scale(1.5,2.5)');
109 });
110
111 it('should skewX', function () {
112 expect(sus.skewX(3.14)).toEqual('skewX(3.14)');
113 });
114
115 it('should rotate', function () {
116 expect(sus.rotate(45)).toEqual('rotate(45)');
117 });
118
Simon Hunt4b668592015-01-29 17:33:53 -0800119
120 // === stripPx()
121
122 it('should not affect a number', function () {
123 expect(sus.stripPx('4')).toEqual('4');
124 });
125
126 it('should remove trailing px', function () {
127 expect(sus.stripPx('4px')).toEqual('4');
128 });
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800129
Simon Hunt18bf9822015-02-12 17:35:45 -0800130 // === visible()
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800131
132 it('should hide and show an element', function () {
133 var r = svg.append('rect');
134
Simon Hunt18bf9822015-02-12 17:35:45 -0800135 sus.visible(r, false);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800136 expect(r.style('visibility')).toEqual('hidden');
Simon Hunt18bf9822015-02-12 17:35:45 -0800137 expect(sus.visible(r)).toBe(false);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800138
Simon Hunt18bf9822015-02-12 17:35:45 -0800139 sus.visible(r, true);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800140 expect(r.style('visibility')).toEqual('visible');
Simon Hunt18bf9822015-02-12 17:35:45 -0800141 expect(sus.visible(r)).toBe(true);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800142 });
Simon Hunt737c89f2015-01-28 12:23:19 -0800143});