Simon Hunt | 988934e | 2015-01-23 11:49:24 -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 | 988934e | 2015-01-23 11:49:24 -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 -- Layer -- Panel Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/layer/panel.js', function () { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 21 | var $log, $timeout, fs, ps, d3Elem; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 22 | |
| 23 | beforeEach(module('onosLayer')); |
| 24 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 25 | beforeEach(inject(function (_$log_, _$timeout_, FnService, PanelService) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 26 | $log = _$log_; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 27 | $timeout = _$timeout_; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 28 | fs = FnService; |
| 29 | ps = PanelService; |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 30 | |
| 31 | spyOn(fs, 'debugOn').and.returnValue(true); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 32 | d3Elem = d3.select('body').append('div').attr('id', 'floatpanels'); |
| 33 | ps.init(); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 34 | })); |
| 35 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 36 | afterEach(function () { |
| 37 | d3.select('#floatpanels').remove(); |
| 38 | ps.init(); |
| 39 | }); |
| 40 | |
| 41 | function floatPanelSelection() { |
| 42 | return d3Elem.selectAll('.floatpanel'); |
| 43 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 44 | |
| 45 | it('should define PanelService', function () { |
| 46 | expect(ps).toBeDefined(); |
| 47 | }); |
| 48 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 49 | it('should define api functions', function () { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 50 | expect(fs.areFunctions(ps, [ |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 51 | 'init', 'createPanel', 'destroyPanel' |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 52 | ])).toBeTruthy(); |
| 53 | }); |
| 54 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 55 | it('should have no panels to start', function () { |
| 56 | expect(floatPanelSelection().size()).toBe(0); |
| 57 | }); |
| 58 | |
| 59 | it('should log a warning if no ID is given', function () { |
| 60 | spyOn($log, 'warn'); |
| 61 | var p = ps.createPanel(); |
| 62 | expect(p).toBeNull(); |
| 63 | expect($log.warn).toHaveBeenCalledWith('createPanel: no ID given'); |
| 64 | expect(floatPanelSelection().size()).toBe(0); |
| 65 | }); |
| 66 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 67 | it('should create a default panel', function () { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 68 | spyOn($log, 'warn'); |
| 69 | spyOn($log, 'debug'); |
| 70 | var p = ps.createPanel('foo'); |
| 71 | expect(p).not.toBeNull(); |
| 72 | expect($log.warn).not.toHaveBeenCalled(); |
| 73 | expect(floatPanelSelection().size()).toBe(1); |
| 74 | expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', { |
| 75 | edge: 'right', |
| 76 | width: 200, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 77 | margin: 20, |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 78 | hideMargin: 20, |
| 79 | xtnTime: 750, |
| 80 | fade: true |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 81 | }); |
| 82 | |
| 83 | // check basic properties |
| 84 | expect(p.width()).toEqual(200); |
| 85 | expect(p.isVisible()).toBeFalsy(); |
| 86 | |
| 87 | var el = floatPanelSelection(); |
| 88 | expect(el.style('width')).toEqual('200px'); |
| 89 | }); |
| 90 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 91 | it('should provide an api of panel functions', function () { |
| 92 | var p = ps.createPanel('foo'); |
| 93 | expect(fs.areFunctions(p, [ |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 94 | 'show', 'hide', 'toggle', 'empty', 'append', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 95 | 'width', 'height', 'isVisible', 'classed', 'el' |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 96 | ])).toBeTruthy(); |
| 97 | }); |
| 98 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 99 | it('should complain when a duplicate ID is used', function () { |
| 100 | spyOn($log, 'warn'); |
| 101 | var p = ps.createPanel('foo'); |
| 102 | expect(p).not.toBeNull(); |
| 103 | expect($log.warn).not.toHaveBeenCalled(); |
| 104 | expect(floatPanelSelection().size()).toBe(1); |
| 105 | |
| 106 | var dup = ps.createPanel('foo'); |
| 107 | expect(dup).toBeNull(); |
| 108 | expect($log.warn).toHaveBeenCalledWith('Panel with ID "foo" already exists'); |
| 109 | expect(floatPanelSelection().size()).toBe(1); |
| 110 | }); |
| 111 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 112 | it('should note when there is no panel to destroy', function () { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 113 | spyOn($log, 'debug'); |
| 114 | ps.destroyPanel('bar'); |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 115 | expect($log.debug).toHaveBeenCalledWith('no panel to destroy:', 'bar'); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 116 | }); |
| 117 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 118 | it('should destroy the panel', function () { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 119 | spyOn($log, 'debug'); |
| 120 | var p = ps.createPanel('foo'); |
| 121 | expect(floatPanelSelection().size()).toBe(1); |
| 122 | |
| 123 | ps.destroyPanel('foo'); |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 124 | expect($log.debug).toHaveBeenCalledWith('destroying panel:', 'foo'); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 125 | expect(floatPanelSelection().size()).toBe(0); |
| 126 | }); |
| 127 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 128 | it('should allow alternate settings to be given', function () { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 129 | spyOn($log, 'debug'); |
| 130 | var p = ps.createPanel('foo', { width: 250, edge: 'left' }); |
| 131 | expect($log.debug).toHaveBeenCalledWith('creating panel:', 'foo', { |
| 132 | edge: 'left', |
| 133 | width: 250, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 134 | margin: 20, |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 135 | hideMargin: 20, |
| 136 | xtnTime: 750, |
| 137 | fade: true |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 138 | }); |
| 139 | }); |
| 140 | |
| 141 | it('should show and hide the panel', function () { |
| 142 | var p = ps.createPanel('foo', {xtnTime:0}); |
| 143 | expect(p.isVisible()).toBeFalsy(); |
| 144 | |
| 145 | p.show(); |
| 146 | expect(p.isVisible()).toBeTruthy(); |
| 147 | |
| 148 | p.hide(); |
| 149 | expect(p.isVisible()).toBeFalsy(); |
| 150 | }); |
| 151 | |
| 152 | it('should append content to the panel', function () { |
| 153 | var p = ps.createPanel('foo'); |
| 154 | var span = p.append('span').attr('id', 'thisIsMySpan'); |
| 155 | |
| 156 | expect(floatPanelSelection().selectAll('span').attr('id')) |
| 157 | .toEqual('thisIsMySpan'); |
| 158 | }); |
| 159 | |
| 160 | it('should remove content on empty', function () { |
| 161 | var p = ps.createPanel('voop'); |
| 162 | p.append('span'); |
| 163 | p.append('span'); |
| 164 | p.append('span'); |
| 165 | expect(floatPanelSelection().selectAll('span').size()).toEqual(3); |
| 166 | |
| 167 | p.empty(); |
| 168 | expect(floatPanelSelection().selectAll('span').size()).toEqual(0); |
| 169 | expect(floatPanelSelection().html()).toEqual(''); |
| 170 | }); |
| 171 | |
| 172 | it('should allow programmatic setting of width', function () { |
| 173 | var p = ps.createPanel('whatcha', {width:234}); |
| 174 | expect(floatPanelSelection().style('width')).toEqual('234px'); |
| 175 | expect(p.width()).toEqual(234); |
| 176 | |
| 177 | p.width(345); |
| 178 | expect(floatPanelSelection().style('width')).toEqual('345px'); |
| 179 | expect(p.width()).toEqual(345); |
| 180 | }); |
| 181 | |
| 182 | it('should allow programmatic setting of height', function () { |
| 183 | var p = ps.createPanel('ciao', {height:50}); |
| 184 | expect(floatPanelSelection().style('height')).toEqual('50px'); |
| 185 | expect(p.height()).toEqual(50); |
| 186 | |
| 187 | p.height(100); |
| 188 | expect(floatPanelSelection().style('height')).toEqual('100px'); |
| 189 | expect(p.height()).toEqual(100); |
| 190 | }); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 191 | }); |