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