blob: cddbb306b7a14f6b0bd133f87b45d14b29a715b2 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt7ac7be92015-01-06 10:47:56 -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 -- Zoom Service - Unit Tests
Simon Hunt7ac7be92015-01-06 10:47:56 -080019 */
20describe('factory: fw/svg/zoom.js', function() {
Simon Huntcacce342015-01-07 16:13:05 -080021 var $log, fs, zs, svg, zoomLayer, zoomer;
Simon Hunt7ac7be92015-01-06 10:47:56 -080022
Simon Huntcacce342015-01-07 16:13:05 -080023 var cz = 'ZoomService.createZoomer(): ',
24 d3s = ' (D3 selection) property defined';
Simon Hunt7ac7be92015-01-06 10:47:56 -080025
Simon Huntcacce342015-01-07 16:13:05 -080026 beforeEach(module('onosUtil', 'onosSvg'));
27
28 beforeEach(inject(function (_$log_, FnService, ZoomService) {
29 $log = _$log_;
30 fs = FnService;
Simon Hunt7ac7be92015-01-06 10:47:56 -080031 zs = ZoomService;
Simon Huntcacce342015-01-07 16:13:05 -080032 svg = d3.select('body').append('svg').attr('id', 'mySvg');
33 zoomLayer = svg.append('g').attr('id', 'myZoomlayer');
Simon Hunt7ac7be92015-01-06 10:47:56 -080034 }));
35
Simon Huntcacce342015-01-07 16:13:05 -080036 afterEach(function () {
37 d3.select('#mySvg').remove();
38 // Note: since zoomLayer is a child of svg, it should be removed also
39 });
40
Simon Hunt7ac7be92015-01-06 10:47:56 -080041 it('should define ZoomService', function () {
42 expect(zs).toBeDefined();
43 });
44
Simon Huntcacce342015-01-07 16:13:05 -080045 it('should define api functions', function () {
46 expect(fs.areFunctions(zs, ['createZoomer'])).toBeTruthy();
47 });
48
49 function verifyZoomerApi() {
50 expect(fs.areFunctions(zoomer, [
Simon Hunt48e61672015-01-30 14:48:25 -080051 'panZoom', 'reset', 'translate', 'scale', 'scaleExtent'
Simon Huntcacce342015-01-07 16:13:05 -080052 ])).toBeTruthy();
53 }
54
55 it('should fail gracefully with no option object', function () {
56 spyOn($log, 'error');
57
58 zoomer = zs.createZoomer();
59 expect($log.error).toHaveBeenCalledWith(cz + 'No "svg" (svg tag)' + d3s);
60 expect($log.error).toHaveBeenCalledWith(cz + 'No "zoomLayer" (g tag)' + d3s);
61 expect(zoomer).toBeNull();
62 });
63
64 it('should complain if we miss required options', function () {
65 spyOn($log, 'error');
66
67 zoomer = zs.createZoomer({});
68 expect($log.error).toHaveBeenCalledWith(cz + 'No "svg" (svg tag)' + d3s);
69 expect($log.error).toHaveBeenCalledWith(cz + 'No "zoomLayer" (g tag)' + d3s);
70 expect(zoomer).toBeNull();
71 });
72
73 it('should work with minimal parameters', function () {
74 spyOn($log, 'error');
75
76 zoomer = zs.createZoomer({
77 svg: svg,
78 zoomLayer: zoomLayer
79 });
80 expect($log.error).not.toHaveBeenCalled();
81 verifyZoomerApi();
82 });
83
84 it('should start at scale 1 and translate 0,0', function () {
85 zoomer = zs.createZoomer({
86 svg: svg,
87 zoomLayer: zoomLayer
88 });
89 verifyZoomerApi();
90 expect(zoomer.translate()).toEqual([0,0]);
91 expect(zoomer.scale()).toEqual(1);
92 });
93
94 it('should allow programmatic pan/zoom', function () {
95 zoomer = zs.createZoomer({
96 svg: svg,
97 zoomLayer: zoomLayer
98 });
99 verifyZoomerApi();
100 expect(zoomer.translate()).toEqual([0,0]);
101 expect(zoomer.scale()).toEqual(1);
102
103 zoomer.panZoom([20,30], 3);
104 expect(zoomer.translate()).toEqual([20,30]);
105 expect(zoomer.scale()).toEqual(3);
106 });
107
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700108 xit('should provide default scale extent', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800109 zoomer = zs.createZoomer({
110 svg: svg,
111 zoomLayer: zoomLayer
112 });
113 expect(zoomer.scaleExtent()).toEqual([0.25, 10]);
114 });
115
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700116 xit('should allow us to override the minimum zoom', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800117 zoomer = zs.createZoomer({
118 svg: svg,
119 zoomLayer: zoomLayer,
120 zoomMin: 1.23
121 });
122 expect(zoomer.scaleExtent()).toEqual([1.23, 10]);
123 });
124
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700125 xit('should allow us to override the maximum zoom', function () {
Simon Huntcacce342015-01-07 16:13:05 -0800126 zoomer = zs.createZoomer({
127 svg: svg,
128 zoomLayer: zoomLayer,
129 zoomMax: 13
130 });
131 expect(zoomer.scaleExtent()).toEqual([0.25, 13]);
132 });
133
134 // TODO: test zoomed() where we fake out the d3.event.sourceEvent etc...
135 // need to check default enabled (true) and custom enabled predicate
136 // need to check that the callback is invoked also
137
138 it('should invoke the callback on programmatic pan/zoom', function () {
139 var foo = { cb: function () {} };
140 spyOn(foo, 'cb');
141
142 zoomer = zs.createZoomer({
143 svg: svg,
144 zoomLayer: zoomLayer,
145 zoomCallback: foo.cb
146 });
147
148 zoomer.panZoom([0,0], 2);
149 expect(foo.cb).toHaveBeenCalled();
150 });
151
Simon Hunt7ac7be92015-01-06 10:47:56 -0800152});