blob: 467d8668d6ee6bb2f29d96867debad8154a59f7b [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 -- Zoom Service - Unit Tests
19
20 @author Simon Hunt
21 */
22describe('factory: fw/svg/zoom.js', function() {
Simon Huntcacce342015-01-07 16:13:05 -080023 var $log, fs, zs, svg, zoomLayer, zoomer;
Simon Hunt7ac7be92015-01-06 10:47:56 -080024
Simon Huntcacce342015-01-07 16:13:05 -080025 var cz = 'ZoomService.createZoomer(): ',
26 d3s = ' (D3 selection) property defined';
Simon Hunt7ac7be92015-01-06 10:47:56 -080027
Simon Huntcacce342015-01-07 16:13:05 -080028 beforeEach(module('onosUtil', 'onosSvg'));
29
30 beforeEach(inject(function (_$log_, FnService, ZoomService) {
31 $log = _$log_;
32 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080033 zs = ZoomService;
Simon Huntcacce342015-01-07 16:13:05 -080034 svg = d3.select('body').append('svg').attr('id', 'mySvg');
35 zoomLayer = svg.append('g').attr('id', 'myZoomlayer');
Simon Hunt7ac7be92015-01-06 10:47:56 -080036 }));
37
Simon Huntcacce342015-01-07 16:13:05 -080038 afterEach(function () {
39 d3.select('#mySvg').remove();
40 // Note: since zoomLayer is a child of svg, it should be removed also
41 });
42
Simon Hunt7ac7be92015-01-06 10:47:56 -080043 it('should define ZoomService', function () {
44 expect(zs).toBeDefined();
45 });
46
Simon Huntcacce342015-01-07 16:13:05 -080047 it('should define api functions', function () {
48 expect(fs.areFunctions(zs, ['createZoomer'])).toBeTruthy();
49 });
50
51 function verifyZoomerApi() {
52 expect(fs.areFunctions(zoomer, [
53 'panZoom', 'reset', 'translate', 'scale'
54 ])).toBeTruthy();
55 }
56
57 it('should fail gracefully with no option object', function () {
58 spyOn($log, 'error');
59
60 zoomer = zs.createZoomer();
61 expect($log.error).toHaveBeenCalledWith(cz + 'No "svg" (svg tag)' + d3s);
62 expect($log.error).toHaveBeenCalledWith(cz + 'No "zoomLayer" (g tag)' + d3s);
63 expect(zoomer).toBeNull();
64 });
65
66 it('should complain if we miss required options', function () {
67 spyOn($log, 'error');
68
69 zoomer = zs.createZoomer({});
70 expect($log.error).toHaveBeenCalledWith(cz + 'No "svg" (svg tag)' + d3s);
71 expect($log.error).toHaveBeenCalledWith(cz + 'No "zoomLayer" (g tag)' + d3s);
72 expect(zoomer).toBeNull();
73 });
74
75 it('should work with minimal parameters', function () {
76 spyOn($log, 'error');
77
78 zoomer = zs.createZoomer({
79 svg: svg,
80 zoomLayer: zoomLayer
81 });
82 expect($log.error).not.toHaveBeenCalled();
83 verifyZoomerApi();
84 });
85
86 it('should start at scale 1 and translate 0,0', function () {
87 zoomer = zs.createZoomer({
88 svg: svg,
89 zoomLayer: zoomLayer
90 });
91 verifyZoomerApi();
92 expect(zoomer.translate()).toEqual([0,0]);
93 expect(zoomer.scale()).toEqual(1);
94 });
95
96 it('should allow programmatic pan/zoom', function () {
97 zoomer = zs.createZoomer({
98 svg: svg,
99 zoomLayer: zoomLayer
100 });
101 verifyZoomerApi();
102 expect(zoomer.translate()).toEqual([0,0]);
103 expect(zoomer.scale()).toEqual(1);
104
105 zoomer.panZoom([20,30], 3);
106 expect(zoomer.translate()).toEqual([20,30]);
107 expect(zoomer.scale()).toEqual(3);
108 });
109
110 it('should provide default scale extent', function () {
111 zoomer = zs.createZoomer({
112 svg: svg,
113 zoomLayer: zoomLayer
114 });
115 expect(zoomer.scaleExtent()).toEqual([0.25, 10]);
116 });
117
118 it('should allow us to override the minimum zoom', function () {
119 zoomer = zs.createZoomer({
120 svg: svg,
121 zoomLayer: zoomLayer,
122 zoomMin: 1.23
123 });
124 expect(zoomer.scaleExtent()).toEqual([1.23, 10]);
125 });
126
127 it('should allow us to override the maximum zoom', function () {
128 zoomer = zs.createZoomer({
129 svg: svg,
130 zoomLayer: zoomLayer,
131 zoomMax: 13
132 });
133 expect(zoomer.scaleExtent()).toEqual([0.25, 13]);
134 });
135
136 // TODO: test zoomed() where we fake out the d3.event.sourceEvent etc...
137 // need to check default enabled (true) and custom enabled predicate
138 // need to check that the callback is invoked also
139
140 it('should invoke the callback on programmatic pan/zoom', function () {
141 var foo = { cb: function () {} };
142 spyOn(foo, 'cb');
143
144 zoomer = zs.createZoomer({
145 svg: svg,
146 zoomLayer: zoomLayer,
147 zoomCallback: foo.cb
148 });
149
150 zoomer.panZoom([0,0], 2);
151 expect(foo.cb).toHaveBeenCalled();
152 });
153
Simon Hunt7ac7be92015-01-06 10:47:56 -0800154});