Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -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 -- Zoom Service - Unit Tests |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/svg/zoom.js', function() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 21 | var $log, fs, zs, svg, zoomLayer, zoomer; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 22 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 23 | var cz = 'ZoomService.createZoomer(): ', |
| 24 | d3s = ' (D3 selection) property defined'; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 25 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 26 | beforeEach(module('onosUtil', 'onosSvg')); |
| 27 | |
| 28 | beforeEach(inject(function (_$log_, FnService, ZoomService) { |
| 29 | $log = _$log_; |
| 30 | fs = FnService; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 31 | zs = ZoomService; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 32 | svg = d3.select('body').append('svg').attr('id', 'mySvg'); |
| 33 | zoomLayer = svg.append('g').attr('id', 'myZoomlayer'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 34 | })); |
| 35 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 36 | afterEach(function () { |
| 37 | d3.select('#mySvg').remove(); |
| 38 | // Note: since zoomLayer is a child of svg, it should be removed also |
| 39 | }); |
| 40 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 41 | it('should define ZoomService', function () { |
| 42 | expect(zs).toBeDefined(); |
| 43 | }); |
| 44 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 45 | it('should define api functions', function () { |
| 46 | expect(fs.areFunctions(zs, ['createZoomer'])).toBeTruthy(); |
| 47 | }); |
| 48 | |
| 49 | function verifyZoomerApi() { |
| 50 | expect(fs.areFunctions(zoomer, [ |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 51 | 'panZoom', 'reset', 'translate', 'scale', 'scaleExtent' |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 52 | ])).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 Scandolo | 812aa5a | 2016-04-19 18:12:45 -0700 | [diff] [blame] | 108 | xit('should provide default scale extent', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 109 | zoomer = zs.createZoomer({ |
| 110 | svg: svg, |
| 111 | zoomLayer: zoomLayer |
| 112 | }); |
| 113 | expect(zoomer.scaleExtent()).toEqual([0.25, 10]); |
| 114 | }); |
| 115 | |
Matteo Scandolo | 812aa5a | 2016-04-19 18:12:45 -0700 | [diff] [blame] | 116 | xit('should allow us to override the minimum zoom', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 117 | 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 Scandolo | 812aa5a | 2016-04-19 18:12:45 -0700 | [diff] [blame] | 125 | xit('should allow us to override the maximum zoom', function () { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 126 | 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 Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 152 | }); |