Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -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 -- Widget -- Toolbar Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/widget/toolbar.js', function () { |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 21 | var $log, fs, tbs, ps, bns, is; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 22 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 23 | beforeEach(module('onosWidget', 'onosUtil', 'onosLayer', 'onosSvg')); |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 24 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 25 | beforeEach(inject(function (_$log_, FnService, ToolbarService, |
| 26 | PanelService, ButtonService, IconService) { |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 27 | $log = _$log_; |
| 28 | fs = FnService; |
| 29 | tbs = ToolbarService; |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 30 | ps = PanelService; |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 31 | bns = ButtonService; |
| 32 | is = IconService; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 33 | })); |
| 34 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 35 | beforeEach(function () { |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 36 | // panel service expects #floatpanels div into which panels are placed |
| 37 | d3.select('body').append('div').attr('id', 'floatpanels'); |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 38 | tbs.init(); |
| 39 | ps.init(); |
| 40 | }); |
| 41 | |
| 42 | afterEach(function () { |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 43 | tbs.init(); |
| 44 | ps.init(); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 45 | d3.select('#floatpanels').remove(); |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 46 | }); |
| 47 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 48 | function nullFunc() { } |
| 49 | |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 50 | it('should define ToolbarService', function () { |
| 51 | expect(tbs).toBeDefined(); |
| 52 | }); |
| 53 | |
| 54 | it('should define api functions', function () { |
| 55 | expect(fs.areFunctions(tbs, [ |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 56 | 'init', |
| 57 | 'createToolbar', 'destroyToolbar' |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 58 | ])).toBeTruthy(); |
| 59 | }); |
| 60 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 61 | it('should warn when no id is given', function () { |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 62 | spyOn($log, 'warn'); |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 63 | expect(tbs.createToolbar()).toBeNull(); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 64 | expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 65 | 'no ID given: [undefined]'); |
| 66 | }); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 67 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 68 | it('should warn when a duplicate id is given', function () { |
| 69 | spyOn($log, 'warn'); |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 70 | expect(tbs.createToolbar('test')).toBeTruthy(); |
| 71 | expect(tbs.createToolbar('test')).toBeNull(); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 72 | expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 73 | 'duplicate ID given: [test]'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 74 | }); |
| 75 | |
Bri Prebilic Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 76 | it('should verify the toolbar arrow div exists', function () { |
| 77 | tbs.createToolbar('test'); |
| 78 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 79 | // NOTE: toolbar service prefixes id with 'toolbar-' |
| 80 | var tbar = d3.select('#toolbar-test'), |
Bri Prebilic Cole | dd80557 | 2015-08-04 16:54:08 -0700 | [diff] [blame] | 81 | arrow = tbar.select('.tbar-arrow'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 82 | |
| 83 | expect(arrow.size()).toBe(1); |
| 84 | expect(arrow.select('svg').size()).toBe(1); |
| 85 | expect(arrow.select('svg').select('g').select('use') |
| 86 | .attr('xlink:href')).toEqual('#triangleUp'); |
Bri Prebilic Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 87 | }); |
| 88 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 89 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 90 | it('should create a button', function () { |
| 91 | spyOn($log, 'warn'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 92 | var toolbar = tbs.createToolbar('foo'), |
| 93 | btn = toolbar.addButton('btn0', 'gid'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 94 | expect(btn).not.toBeNull(); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 95 | expect(btn.id).toBe('toolbar-foo-btn0'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 96 | expect($log.warn).not.toHaveBeenCalled(); |
| 97 | }); |
| 98 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 99 | it('should not create an item with a duplicate id', function () { |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 100 | spyOn($log, 'warn'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 101 | var toolbar = tbs.createToolbar('foo'), |
| 102 | btn = toolbar.addButton('btn0', 'gid'), |
| 103 | dup; |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 104 | expect(btn).not.toBeNull(); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 105 | expect(btn.id).toBe('toolbar-foo-btn0'); |
| 106 | |
| 107 | dup = toolbar.addButton('btn0', 'gid'); |
| 108 | expect($log.warn).toHaveBeenCalledWith('addButton: duplicate ID:', 'btn0'); |
| 109 | expect(dup).toBeNull(); |
| 110 | |
| 111 | dup = toolbar.addToggle('btn0', 'gid'); |
| 112 | expect($log.warn).toHaveBeenCalledWith('addToggle: duplicate ID:', 'btn0'); |
| 113 | expect(dup).toBeNull(); |
| 114 | |
| 115 | dup = toolbar.addRadioSet('btn0', []); |
| 116 | expect($log.warn).toHaveBeenCalledWith('addRadioSet: duplicate ID:', 'btn0'); |
| 117 | expect(dup).toBeNull(); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 118 | }); |
| 119 | |
| 120 | it('should create a toggle', function () { |
| 121 | spyOn($log, 'warn'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 122 | var toolbar = tbs.createToolbar('foo'), |
| 123 | tog = toolbar.addButton('tog0', 'gid'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 124 | expect(tog).not.toBeNull(); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 125 | expect(tog.id).toBe('toolbar-foo-tog0'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 126 | expect($log.warn).not.toHaveBeenCalled(); |
| 127 | }); |
| 128 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 129 | it('should create a radio button set', function () { |
| 130 | spyOn($log, 'warn'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 131 | var toolbar = tbs.createToolbar('foo'), |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 132 | rset = [ |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 133 | { gid: 'crown', cb: nullFunc, tooltip: 'A Crown' }, |
| 134 | { gid: 'bird', cb: nullFunc, tooltip: 'A Bird' } |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 135 | ], |
| 136 | rad = toolbar.addRadioSet('rad0', rset); |
| 137 | expect(rad).not.toBeNull(); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 138 | expect(rad.selectedIndex()).toBe(0); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 139 | expect($log.warn).not.toHaveBeenCalled(); |
| 140 | }); |
| 141 | |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 142 | it('should create a separator div', function () { |
| 143 | spyOn($log, 'warn'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 144 | var toolbar = tbs.createToolbar('foo'), |
| 145 | tbar = d3.select('#toolbar-foo'); |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 146 | |
| 147 | toolbar.addSeparator(); |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 148 | expect($log.warn).not.toHaveBeenCalled(); |
| 149 | |
Simon Hunt | d3bcef3 | 2015-02-27 18:36:42 -0800 | [diff] [blame] | 150 | expect(tbar.select('.separator').size()).toBe(1); |
Bri Prebilic Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 151 | }); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 152 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 153 | it('should add another row of buttons', function () { |
| 154 | var toolbar = tbs.createToolbar('foo'), |
| 155 | tbar = d3.select('#toolbar-foo'), |
| 156 | rows; |
| 157 | toolbar.addButton('btn0', 'gid'); |
| 158 | toolbar.addRow(); |
| 159 | toolbar.addButton('btn1', 'gid'); |
| 160 | |
| 161 | rows = tbar.selectAll('.tbar-row'); |
| 162 | expect(rows.size()).toBe(2); |
| 163 | rows.each(function (d, i) { |
| 164 | expect(d3.select(this) |
| 165 | .select('div') |
| 166 | .attr('id','toolbar-foo-btn' + i) |
| 167 | .empty()) |
| 168 | .toBe(false); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | it('should not add a row if current row is empty', function () { |
| 173 | var toolbar = tbs.createToolbar('foo'); |
| 174 | expect(toolbar.addRow()).toBeNull(); |
| 175 | toolbar.addButton('btn0', 'gid'); |
| 176 | expect(toolbar.addRow()).not.toBeNull(); |
| 177 | expect(toolbar.addRow()).toBeNull(); |
| 178 | }); |
| 179 | |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 180 | }); |