blob: 698159c10d97906062153197ad06bbb54d64792e [file] [log] [blame]
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -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 -- Widget -- Toolbar Service - Unit Tests
19 */
20describe('factory: fw/widget/toolbar.js', function () {
Simon Huntd3bcef32015-02-27 18:36:42 -080021 var $log, fs, tbs, ps, bns, is;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080022
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080023 beforeEach(module('onosWidget', 'onosUtil', 'onosLayer', 'onosSvg'));
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080024
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080025 beforeEach(inject(function (_$log_, FnService, ToolbarService,
26 PanelService, ButtonService, IconService) {
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080027 $log = _$log_;
28 fs = FnService;
29 tbs = ToolbarService;
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080030 ps = PanelService;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080031 bns = ButtonService;
32 is = IconService;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080033 }));
34
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080035 beforeEach(function () {
Simon Huntd3bcef32015-02-27 18:36:42 -080036 // panel service expects #floatpanels div into which panels are placed
37 d3.select('body').append('div').attr('id', 'floatpanels');
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080038 tbs.init();
39 ps.init();
40 });
41
42 afterEach(function () {
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080043 tbs.init();
44 ps.init();
Simon Huntd3bcef32015-02-27 18:36:42 -080045 d3.select('#floatpanels').remove();
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080046 });
47
Simon Huntd3bcef32015-02-27 18:36:42 -080048 function nullFunc() { }
49
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080050 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 Colec4403322015-02-23 10:29:22 -080056 'init',
57 'createToolbar', 'destroyToolbar'
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080058 ])).toBeTruthy();
59 });
60
Simon Huntd3bcef32015-02-27 18:36:42 -080061 it('should warn when no id is given', function () {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080062 spyOn($log, 'warn');
Bri Prebilic Colec4403322015-02-23 10:29:22 -080063 expect(tbs.createToolbar()).toBeNull();
Bri Prebilic Cole18489172015-02-27 17:12:00 -080064 expect($log.warn).toHaveBeenCalledWith('createToolbar: ' +
Simon Huntd3bcef32015-02-27 18:36:42 -080065 'no ID given: [undefined]');
66 });
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080067
Simon Huntd3bcef32015-02-27 18:36:42 -080068 it('should warn when a duplicate id is given', function () {
69 spyOn($log, 'warn');
Bri Prebilic Colec4403322015-02-23 10:29:22 -080070 expect(tbs.createToolbar('test')).toBeTruthy();
71 expect(tbs.createToolbar('test')).toBeNull();
Bri Prebilic Cole18489172015-02-27 17:12:00 -080072 expect($log.warn).toHaveBeenCalledWith('createToolbar: ' +
Simon Huntd3bcef32015-02-27 18:36:42 -080073 'duplicate ID given: [test]');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080074 });
75
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080076 it('should verify the toolbar arrow div exists', function () {
77 tbs.createToolbar('test');
78
Simon Huntd3bcef32015-02-27 18:36:42 -080079 // NOTE: toolbar service prefixes id with 'toolbar-'
80 var tbar = d3.select('#toolbar-test'),
Bri Prebilic Coledd805572015-08-04 16:54:08 -070081 arrow = tbar.select('.tbar-arrow');
Simon Huntd3bcef32015-02-27 18:36:42 -080082
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 Cole5000a752015-02-23 17:20:53 -080087 });
88
Simon Huntd3bcef32015-02-27 18:36:42 -080089
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080090 it('should create a button', function () {
91 spyOn($log, 'warn');
Simon Huntd3bcef32015-02-27 18:36:42 -080092 var toolbar = tbs.createToolbar('foo'),
93 btn = toolbar.addButton('btn0', 'gid');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080094 expect(btn).not.toBeNull();
Simon Huntd3bcef32015-02-27 18:36:42 -080095 expect(btn.id).toBe('toolbar-foo-btn0');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080096 expect($log.warn).not.toHaveBeenCalled();
97 });
98
Simon Huntd3bcef32015-02-27 18:36:42 -080099 it('should not create an item with a duplicate id', function () {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800100 spyOn($log, 'warn');
Simon Huntd3bcef32015-02-27 18:36:42 -0800101 var toolbar = tbs.createToolbar('foo'),
102 btn = toolbar.addButton('btn0', 'gid'),
103 dup;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800104 expect(btn).not.toBeNull();
Simon Huntd3bcef32015-02-27 18:36:42 -0800105 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 Cole08381ce2015-02-20 17:01:50 -0800118 });
119
120 it('should create a toggle', function () {
121 spyOn($log, 'warn');
Simon Huntd3bcef32015-02-27 18:36:42 -0800122 var toolbar = tbs.createToolbar('foo'),
123 tog = toolbar.addButton('tog0', 'gid');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800124 expect(tog).not.toBeNull();
Simon Huntd3bcef32015-02-27 18:36:42 -0800125 expect(tog.id).toBe('toolbar-foo-tog0');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800126 expect($log.warn).not.toHaveBeenCalled();
127 });
128
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800129 it('should create a radio button set', function () {
130 spyOn($log, 'warn');
Simon Huntd3bcef32015-02-27 18:36:42 -0800131 var toolbar = tbs.createToolbar('foo'),
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800132 rset = [
Simon Huntd3bcef32015-02-27 18:36:42 -0800133 { gid: 'crown', cb: nullFunc, tooltip: 'A Crown' },
134 { gid: 'bird', cb: nullFunc, tooltip: 'A Bird' }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800135 ],
136 rad = toolbar.addRadioSet('rad0', rset);
137 expect(rad).not.toBeNull();
Simon Huntd3bcef32015-02-27 18:36:42 -0800138 expect(rad.selectedIndex()).toBe(0);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800139 expect($log.warn).not.toHaveBeenCalled();
140 });
141
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800142 it('should create a separator div', function () {
143 spyOn($log, 'warn');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700144 var toolbar = tbs.createToolbar('foo'),
145 tbar = d3.select('#toolbar-foo');
Simon Huntd3bcef32015-02-27 18:36:42 -0800146
147 toolbar.addSeparator();
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800148 expect($log.warn).not.toHaveBeenCalled();
149
Simon Huntd3bcef32015-02-27 18:36:42 -0800150 expect(tbar.select('.separator').size()).toBe(1);
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800151 });
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800152
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700153 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 Cole2e3f8562015-02-17 17:21:31 -0800180});