GUI -- Fixing broken Toolbar unit tests.
Change-Id: Ic82fd281cc6e51b8395781c39bad075ecb24c096
Showing
2 changed files
with
75 additions
and
64 deletions
... | @@ -79,10 +79,11 @@ | ... | @@ -79,10 +79,11 @@ |
79 | // ================================== | 79 | // ================================== |
80 | 80 | ||
81 | function createToolbar(id, opts) { | 81 | function createToolbar(id, opts) { |
82 | - if (!id) return warn('no ID given', id); | 82 | + if (!id) return warn('no ID given'); |
83 | if (tbars[id]) return warn('duplicate ID given', id); | 83 | if (tbars[id]) return warn('duplicate ID given', id); |
84 | 84 | ||
85 | var settings = angular.extend({}, defaultSettings, fs.isO(opts)), | 85 | var settings = angular.extend({}, defaultSettings, fs.isO(opts)), |
86 | + items = {}, | ||
86 | tbid = 'toolbar-' + id, | 87 | tbid = 'toolbar-' + id, |
87 | panel = ps.createPanel(tbid, settings), | 88 | panel = ps.createPanel(tbid, settings), |
88 | arrowDiv = createArrow(panel), | 89 | arrowDiv = createArrow(panel), |
... | @@ -94,6 +95,7 @@ | ... | @@ -94,6 +95,7 @@ |
94 | // add a descriptor for this toolbar | 95 | // add a descriptor for this toolbar |
95 | tbars[id] = { | 96 | tbars[id] = { |
96 | settings: settings, | 97 | settings: settings, |
98 | + items: items, | ||
97 | panel: panel, | 99 | panel: panel, |
98 | panelId: tbid | 100 | panelId: tbid |
99 | }; | 101 | }; |
... | @@ -102,27 +104,47 @@ | ... | @@ -102,27 +104,47 @@ |
102 | .style('top', settings.top); | 104 | .style('top', settings.top); |
103 | 105 | ||
104 | 106 | ||
107 | + function dupId(id, caller) { | ||
108 | + if (items[id]) { | ||
109 | + $log.warn(caller + ': duplicate ID:', id); | ||
110 | + return true; | ||
111 | + } | ||
112 | + return false; | ||
113 | + } | ||
114 | + | ||
105 | // API functions | 115 | // API functions |
106 | 116 | ||
107 | function addButton(id, gid, cb, tooltip) { | 117 | function addButton(id, gid, cb, tooltip) { |
118 | + if (dupId(id, 'addButton')) return null; | ||
119 | + | ||
108 | var bid = tbid + '-' + id, | 120 | var bid = tbid + '-' + id, |
109 | btn = bns.button(panel, bid, gid, cb, tooltip); | 121 | btn = bns.button(panel, bid, gid, cb, tooltip); |
122 | + | ||
123 | + items[id] = btn; | ||
110 | tbWidth += btn.width(); | 124 | tbWidth += btn.width(); |
111 | panel.width(tbWidth); | 125 | panel.width(tbWidth); |
112 | return btn; | 126 | return btn; |
113 | } | 127 | } |
114 | 128 | ||
115 | function addToggle(id, gid, initState, cb, tooltip) { | 129 | function addToggle(id, gid, initState, cb, tooltip) { |
130 | + if (dupId(id, 'addToggle')) return null; | ||
131 | + | ||
116 | var tid = tbid + '-' + id, | 132 | var tid = tbid + '-' + id, |
117 | tog = bns.toggle(panel, tid, gid, initState, cb, tooltip); | 133 | tog = bns.toggle(panel, tid, gid, initState, cb, tooltip); |
134 | + | ||
135 | + items[id] = tog; | ||
118 | tbWidth += tog.width(); | 136 | tbWidth += tog.width(); |
119 | panel.width(tbWidth); | 137 | panel.width(tbWidth); |
120 | return tog; | 138 | return tog; |
121 | } | 139 | } |
122 | 140 | ||
123 | function addRadioSet(id, rset) { | 141 | function addRadioSet(id, rset) { |
142 | + if (dupId(id, 'addRadioSet')) return null; | ||
143 | + | ||
124 | var rid = tbid + '-' + id, | 144 | var rid = tbid + '-' + id, |
125 | rad = bns.radioSet(panel, rid, rset); | 145 | rad = bns.radioSet(panel, rid, rset); |
146 | + | ||
147 | + items[id] = rad; | ||
126 | tbWidth += rad.width(); | 148 | tbWidth += rad.width(); |
127 | panel.width(tbWidth); | 149 | panel.width(tbWidth); |
128 | return rad; | 150 | return rad; | ... | ... |
... | @@ -18,8 +18,7 @@ | ... | @@ -18,8 +18,7 @@ |
18 | ONOS GUI -- Widget -- Toolbar Service - Unit Tests | 18 | ONOS GUI -- Widget -- Toolbar Service - Unit Tests |
19 | */ | 19 | */ |
20 | describe('factory: fw/widget/toolbar.js', function () { | 20 | describe('factory: fw/widget/toolbar.js', function () { |
21 | - var $log, fs, tbs, ps, bns, is, | 21 | + var $log, fs, tbs, ps, bns, is; |
22 | - d3Elem; | ||
23 | 22 | ||
24 | beforeEach(module('onosWidget', 'onosUtil', 'onosLayer', 'onosSvg')); | 23 | beforeEach(module('onosWidget', 'onosUtil', 'onosLayer', 'onosSvg')); |
25 | 24 | ||
... | @@ -33,20 +32,21 @@ describe('factory: fw/widget/toolbar.js', function () { | ... | @@ -33,20 +32,21 @@ describe('factory: fw/widget/toolbar.js', function () { |
33 | is = IconService; | 32 | is = IconService; |
34 | })); | 33 | })); |
35 | 34 | ||
36 | - // TODO: figure out solution for calling tests with new info instead of calling init | ||
37 | - | ||
38 | beforeEach(function () { | 35 | beforeEach(function () { |
39 | - d3Elem = d3.select('body').append('div').attr('id', 'testToolbar'); | 36 | + // panel service expects #floatpanels div into which panels are placed |
37 | + d3.select('body').append('div').attr('id', 'floatpanels'); | ||
40 | tbs.init(); | 38 | tbs.init(); |
41 | ps.init(); | 39 | ps.init(); |
42 | }); | 40 | }); |
43 | 41 | ||
44 | afterEach(function () { | 42 | afterEach(function () { |
45 | - d3.select('#testToolbar').remove(); | ||
46 | tbs.init(); | 43 | tbs.init(); |
47 | ps.init(); | 44 | ps.init(); |
45 | + d3.select('#floatpanels').remove(); | ||
48 | }); | 46 | }); |
49 | 47 | ||
48 | + function nullFunc() { } | ||
49 | + | ||
50 | it('should define ToolbarService', function () { | 50 | it('should define ToolbarService', function () { |
51 | expect(tbs).toBeDefined(); | 51 | expect(tbs).toBeDefined(); |
52 | }); | 52 | }); |
... | @@ -58,107 +58,96 @@ describe('factory: fw/widget/toolbar.js', function () { | ... | @@ -58,107 +58,96 @@ describe('factory: fw/widget/toolbar.js', function () { |
58 | ])).toBeTruthy(); | 58 | ])).toBeTruthy(); |
59 | }); | 59 | }); |
60 | 60 | ||
61 | - it('should warn if createToolbar id is invalid', function () { | 61 | + it('should warn when no id is given', function () { |
62 | spyOn($log, 'warn'); | 62 | spyOn($log, 'warn'); |
63 | expect(tbs.createToolbar()).toBeNull(); | 63 | expect(tbs.createToolbar()).toBeNull(); |
64 | expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + | 64 | expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + |
65 | 'no ID given: [undefined]'); | 65 | 'no ID given: [undefined]'); |
66 | - | ||
67 | - expect(tbs.createToolbar('test')).toBeTruthy(); | ||
68 | - expect(tbs.createToolbar('test')).toBeNull(); | ||
69 | - expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + | ||
70 | - 'duplicate ID given: [undefined]'); | ||
71 | }); | 66 | }); |
72 | 67 | ||
73 | - it('should create an unpopulated toolbar', function () { | 68 | + it('should warn when a duplicate id is given', function () { |
74 | spyOn($log, 'warn'); | 69 | spyOn($log, 'warn'); |
75 | expect(tbs.createToolbar('test')).toBeTruthy(); | 70 | expect(tbs.createToolbar('test')).toBeTruthy(); |
76 | - expect($log.warn).not.toHaveBeenCalled(); | 71 | + expect(tbs.createToolbar('test')).toBeNull(); |
72 | + expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + | ||
73 | + 'duplicate ID given: [test]'); | ||
77 | }); | 74 | }); |
78 | 75 | ||
79 | it('should verify the toolbar arrow div exists', function () { | 76 | it('should verify the toolbar arrow div exists', function () { |
80 | tbs.createToolbar('test'); | 77 | tbs.createToolbar('test'); |
81 | 78 | ||
82 | - var arrow = d3Elem.select('.tbarArrow'); | 79 | + // NOTE: toolbar service prefixes id with 'toolbar-' |
83 | - expect(arrow).toBeTruthy(); | 80 | + var tbar = d3.select('#toolbar-test'), |
84 | - expect(arrow.select('svg')).toBeTruthy(); | 81 | + arrow = tbar.select('.tbarArrow'); |
85 | - expect(arrow.select('svg').select('g') | 82 | + |
86 | - .classed('tableColSortAsc')).toBeTruthy(); | 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'); | ||
87 | }); | 87 | }); |
88 | 88 | ||
89 | + | ||
89 | it('should create a button', function () { | 90 | it('should create a button', function () { |
90 | spyOn($log, 'warn'); | 91 | spyOn($log, 'warn'); |
91 | - var toolbar = tbs.createToolbar('test'), | 92 | + var toolbar = tbs.createToolbar('foo'), |
92 | - btn = toolbar.addButton('btn0', 'gid', function () {}); | 93 | + btn = toolbar.addButton('btn0', 'gid'); |
93 | expect(btn).not.toBeNull(); | 94 | expect(btn).not.toBeNull(); |
94 | - expect(btn.id).toBe('tbar-test-btn0'); | 95 | + expect(btn.id).toBe('toolbar-foo-btn0'); |
95 | expect($log.warn).not.toHaveBeenCalled(); | 96 | expect($log.warn).not.toHaveBeenCalled(); |
96 | }); | 97 | }); |
97 | 98 | ||
98 | - it('should not create a button with a duplicate id', function () { | 99 | + it('should not create an item with a duplicate id', function () { |
99 | spyOn($log, 'warn'); | 100 | spyOn($log, 'warn'); |
100 | - var toolbar = tbs.createToolbar('test'), | 101 | + var toolbar = tbs.createToolbar('foo'), |
101 | - btn = toolbar.addButton('btn0', 'gid', function () {}), | 102 | + btn = toolbar.addButton('btn0', 'gid'), |
102 | - btn1 = toolbar.addButton('btn0', 'gid', function () {}); | 103 | + dup; |
103 | expect(btn).not.toBeNull(); | 104 | expect(btn).not.toBeNull(); |
104 | - expect(btn.id).toBe('tbar-test-btn0'); | 105 | + expect(btn.id).toBe('toolbar-foo-btn0'); |
105 | - expect($log.warn).toHaveBeenCalledWith('addButton: ID already exists'); | 106 | + |
106 | - expect(btn1).toBeNull(); | 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(); | ||
107 | }); | 118 | }); |
108 | 119 | ||
109 | it('should create a toggle', function () { | 120 | it('should create a toggle', function () { |
110 | spyOn($log, 'warn'); | 121 | spyOn($log, 'warn'); |
111 | - var toolbar = tbs.createToolbar('test'), | 122 | + var toolbar = tbs.createToolbar('foo'), |
112 | - tog = toolbar.addButton('tog0', 'gid', false, function () {}); | 123 | + tog = toolbar.addButton('tog0', 'gid'); |
113 | expect(tog).not.toBeNull(); | 124 | expect(tog).not.toBeNull(); |
114 | - expect(tog.id).toBe('tbar-test-tog0'); | 125 | + expect(tog.id).toBe('toolbar-foo-tog0'); |
115 | expect($log.warn).not.toHaveBeenCalled(); | 126 | expect($log.warn).not.toHaveBeenCalled(); |
116 | }); | 127 | }); |
117 | 128 | ||
118 | - it('should not create a toggle with a duplicate id', function () { | ||
119 | - spyOn($log, 'warn'); | ||
120 | - var toolbar = tbs.createToolbar('test'), | ||
121 | - tog = toolbar.addToggle('tog0', 'gid', false, function () {}), | ||
122 | - tog1 = toolbar.addToggle('tog0', 'gid', true, function () {}); | ||
123 | - expect(tog).not.toBeNull(); | ||
124 | - expect(tog.id).toBe('tbar-test-tog0'); | ||
125 | - expect($log.warn).toHaveBeenCalledWith('addToggle: ID already exists'); | ||
126 | - expect(tog1).toBeNull(); | ||
127 | - }); | ||
128 | - | ||
129 | - | ||
130 | it('should create a radio button set', function () { | 129 | it('should create a radio button set', function () { |
131 | spyOn($log, 'warn'); | 130 | spyOn($log, 'warn'); |
132 | - var toolbar = tbs.createToolbar('test'), | 131 | + var toolbar = tbs.createToolbar('foo'), |
133 | rset = [ | 132 | rset = [ |
134 | - { gid: 'crown', cb: function () {}, tooltip: 'nothing' }, | 133 | + { gid: 'crown', cb: nullFunc, tooltip: 'A Crown' }, |
135 | - { gid: 'bird', cb: function () {}, tooltip: 'nothing' } | 134 | + { gid: 'bird', cb: nullFunc, tooltip: 'A Bird' } |
136 | ], | 135 | ], |
137 | rad = toolbar.addRadioSet('rad0', rset); | 136 | rad = toolbar.addRadioSet('rad0', rset); |
138 | expect(rad).not.toBeNull(); | 137 | expect(rad).not.toBeNull(); |
139 | - expect(rad.rads[0].id).toBe('tbar-test-rad0-0'); | 138 | + expect(rad.selectedIndex()).toBe(0); |
140 | - expect(rad.rads[1].id).toBe('tbar-test-rad0-1'); | ||
141 | expect($log.warn).not.toHaveBeenCalled(); | 139 | expect($log.warn).not.toHaveBeenCalled(); |
142 | }); | 140 | }); |
143 | 141 | ||
144 | it('should create a separator div', function () { | 142 | it('should create a separator div', function () { |
145 | spyOn($log, 'warn'); | 143 | spyOn($log, 'warn'); |
146 | - var toolbar = tbs.createToolbar('test'), | 144 | + var toolbar = tbs.createToolbar('foo'); |
147 | - sep = toolbar.addSeparator(); | 145 | + var tbar = d3.select('#toolbar-foo'); |
148 | - expect(sep).not.toBeNull(); | ||
149 | - expect($log.warn).not.toHaveBeenCalled(); | ||
150 | 146 | ||
151 | - expect(d3Elem.select('.sep')).toBeTruthy(); | 147 | + toolbar.addSeparator(); |
152 | - expect(d3Elem.select('.sep').style('width')).toBe('2px'); | 148 | + expect($log.warn).not.toHaveBeenCalled(); |
153 | - }); | ||
154 | 149 | ||
155 | - it('should not append to a destroyed toolbar', function () { | 150 | + expect(tbar.select('.separator').size()).toBe(1); |
156 | - spyOn($log, 'warn'); | ||
157 | - var toolbar = tbs.createToolbar('test'); | ||
158 | - expect(toolbar).not.toBeNull(); | ||
159 | - tbs.destroyToolbar('tbar-test'); | ||
160 | - expect(toolbar.addButton('btn', 'gid', function () {})).toBeNull(); | ||
161 | - expect($log.warn).toHaveBeenCalledWith('Button cannot append to div'); | ||
162 | }); | 151 | }); |
163 | 152 | ||
164 | }); | 153 | }); | ... | ... |
-
Please register or login to post a comment