Bri Prebilic Cole

GUI -- Fixed warning about undefined panel, added topoPanel destroy, added unit tests for topoPanel.

Change-Id: Ic7ecc2771aa64bb0db19fe3b8005dc3932b237c5
...@@ -44,9 +44,14 @@ ...@@ -44,9 +44,14 @@
44 // Panel API 44 // Panel API
45 function createTopoPanel(id, opts) { 45 function createTopoPanel(id, opts) {
46 var p = ps.createPanel(id, opts), 46 var p = ps.createPanel(id, opts),
47 + pid = id,
47 header, body, footer; 48 header, body, footer;
48 p.classed(pCls, true); 49 p.classed(pCls, true);
49 50
51 + function panel() {
52 + return p;
53 + }
54 +
50 function hAppend(x) { 55 function hAppend(x) {
51 return header.append(x); 56 return header.append(x);
52 } 57 }
...@@ -71,6 +76,10 @@ ...@@ -71,6 +76,10 @@
71 footer = p.el().select('.footer'); 76 footer = p.el().select('.footer');
72 } 77 }
73 78
79 + function destroy() {
80 + ps.destroyPanel(pid);
81 + }
82 +
74 // fromTop is how many pixels from the top of the page the panel is 83 // fromTop is how many pixels from the top of the page the panel is
75 // max is the max height of the panel in pixels 84 // max is the max height of the panel in pixels
76 // only adjusts if the body content would be 10px or larger 85 // only adjusts if the body content would be 10px or larger
...@@ -83,10 +92,7 @@ ...@@ -83,10 +92,7 @@
83 $log.warn('adjustHeight: height from top of page not given'); 92 $log.warn('adjustHeight: height from top of page not given');
84 return null; 93 return null;
85 } else if (!body || !p) { 94 } else if (!body || !p) {
86 - // if we have reached this function without setting a panel 95 + $log.warn('adjustHeight: panel contents are not defined');
87 - // sometimes the d3 tick function calls detail panel's 'up'
88 - // on reload when body isn't defined yet.
89 - $log.warn('adjustHeight: panel is not defined');
90 return null; 96 return null;
91 } 97 }
92 98
...@@ -119,8 +125,9 @@ ...@@ -119,8 +125,9 @@
119 } 125 }
120 126
121 return { 127 return {
122 - panel: p, 128 + panel: panel,
123 setup: setup, 129 setup: setup,
130 + destroy: destroy,
124 appendHeader: hAppend, 131 appendHeader: hAppend,
125 appendBody: bAppend, 132 appendBody: bAppend,
126 appendFooter: fAppend, 133 appendFooter: fAppend,
...@@ -317,7 +324,7 @@ ...@@ -317,7 +324,7 @@
317 324
318 function toggleSummary(x) { 325 function toggleSummary(x) {
319 var kev = (x === 'keyev'), 326 var kev = (x === 'keyev'),
320 - on = kev ? !summary.panel.isVisible() : !!x, 327 + on = kev ? !summary.panel().isVisible() : !!x,
321 verb = on ? 'Show' : 'Hide'; 328 verb = on ? 'Show' : 'Hide';
322 329
323 if (on) { 330 if (on) {
...@@ -336,10 +343,10 @@ ...@@ -336,10 +343,10 @@
336 343
337 function showSummaryPanel() { 344 function showSummaryPanel() {
338 function _show() { 345 function _show() {
339 - summary.panel.show(); 346 + summary.panel().show();
340 summary.adjustHeight(64, 226); 347 summary.adjustHeight(64, 226);
341 } 348 }
342 - if (detail.panel.isVisible()) { 349 + if (detail.panel().isVisible()) {
343 detail.down(_show); 350 detail.down(_show);
344 } else { 351 } else {
345 _show(); 352 _show();
...@@ -349,19 +356,20 @@ ...@@ -349,19 +356,20 @@
349 function hideSummaryPanel() { 356 function hideSummaryPanel() {
350 // instruct server to stop sending summary data 357 // instruct server to stop sending summary data
351 wss.sendEvent("cancelSummary"); 358 wss.sendEvent("cancelSummary");
352 - summary.panel.hide(detail.up); 359 + summary.panel().hide(detail.up);
353 } 360 }
354 361
355 function showDetailPanel() { 362 function showDetailPanel() {
356 - if (summary.panel.isVisible()) { 363 + if (summary.panel().isVisible()) {
357 - detail.down(detail.panel.show); 364 + detail.down(detail.panel().show);
358 } else { 365 } else {
359 - detail.up(detail.panel.show); 366 + detail.up(detail.panel().show);
360 } 367 }
368 + detail.adjustHeight(detail.ypos.current);
361 } 369 }
362 370
363 function hideDetailPanel() { 371 function hideDetailPanel() {
364 - detail.panel.hide(); 372 + detail.panel().hide();
365 } 373 }
366 374
367 // ========================== 375 // ==========================
...@@ -377,7 +385,7 @@ ...@@ -377,7 +385,7 @@
377 yp = d.ypos; 385 yp = d.ypos;
378 if (yp.current !== y) { 386 if (yp.current !== y) {
379 yp.current = y; 387 yp.current = y;
380 - d.panel.el().transition().duration(300) 388 + d.panel().el().transition().duration(300)
381 .each('end', endCb) 389 .each('end', endCb)
382 .style('top', yp.current + 'px'); 390 .style('top', yp.current + 'px');
383 } else { 391 } else {
...@@ -385,15 +393,8 @@ ...@@ -385,15 +393,8 @@
385 } 393 }
386 }; 394 };
387 395
388 - // d.up is being called on the tick function for some reason 396 + d.down = function (cb) { d._move(d.ypos.down, cb); };
389 - d.down = function (cb) { 397 + d.up = function (cb) { d._move(d.ypos.up, cb); };
390 - d._move(d.ypos.down, cb);
391 - detail.adjustHeight(d.ypos.current);
392 - };
393 - d.up = function (cb) {
394 - d._move(d.ypos.up, cb);
395 - detail.adjustHeight(d.ypos.current);
396 - };
397 } 398 }
398 399
399 function toggleUseDetailsFlag(x) { 400 function toggleUseDetailsFlag(x) {
...@@ -424,9 +425,11 @@ ...@@ -424,9 +425,11 @@
424 } 425 }
425 426
426 function destroyPanels() { 427 function destroyPanels() {
427 - ps.destroyPanel(idSum); 428 + summary.destroy();
428 - ps.destroyPanel(idDet); 429 + summary = null;
429 - summary.panel = detail.panel = null; 430 +
431 + detail.destroy();
432 + detail = null;
430 haveDetails = false; 433 haveDetails = false;
431 } 434 }
432 435
...@@ -464,8 +467,8 @@ ...@@ -464,8 +467,8 @@
464 467
465 hideSummaryPanel: hideSummaryPanel, 468 hideSummaryPanel: hideSummaryPanel,
466 469
467 - detailVisible: function () { return detail.panel.isVisible(); }, 470 + detailVisible: function () { return detail.panel().isVisible(); },
468 - summaryVisible: function () { return summary.panel.isVisible(); } 471 + summaryVisible: function () { return summary.panel().isVisible(); }
469 }; 472 };
470 }]); 473 }]);
471 }()); 474 }());
......
...@@ -18,19 +18,41 @@ ...@@ -18,19 +18,41 @@
18 ONOS GUI -- Topo View -- Topo Panel Service - Unit Tests 18 ONOS GUI -- Topo View -- Topo Panel Service - Unit Tests
19 */ 19 */
20 describe('factory: view/topo/topoPanel.js', function() { 20 describe('factory: view/topo/topoPanel.js', function() {
21 - var $log, fs, tps, bns; 21 + var $log, fs, tps, bns, ps, panelLayer;
22 +
23 + var mockWindow = {
24 + innerWidth: 300,
25 + innerHeight: 100,
26 + navigator: {
27 + userAgent: 'defaultUA'
28 + },
29 + on: function () {},
30 + addEventListener: function () {}
31 + };
22 32
23 beforeEach(module('ovTopo', 'onosUtil', 'onosLayer', 'ngRoute', 'onosNav', 33 beforeEach(module('ovTopo', 'onosUtil', 'onosLayer', 'ngRoute', 'onosNav',
24 'onosWidget')); 34 'onosWidget'));
25 35
36 + beforeEach(function () {
37 + module(function ($provide) {
38 + $provide.value('$window', mockWindow);
39 + });
40 + });
41 +
26 beforeEach(inject(function (_$log_, FnService, 42 beforeEach(inject(function (_$log_, FnService,
27 - TopoPanelService, ButtonService) { 43 + TopoPanelService, ButtonService, PanelService) {
28 $log = _$log_; 44 $log = _$log_;
29 fs = FnService; 45 fs = FnService;
30 tps = TopoPanelService; 46 tps = TopoPanelService;
31 bns = ButtonService; 47 bns = ButtonService;
48 + ps = PanelService;
49 + panelLayer = d3.select('body').append('div').attr('id', 'floatpanels');
32 })); 50 }));
33 51
52 + afterEach(function () {
53 + panelLayer.remove();
54 + });
55 +
34 it('should define TopoPanelService', function () { 56 it('should define TopoPanelService', function () {
35 expect(tps).toBeDefined(); 57 expect(tps).toBeDefined();
36 }); 58 });
...@@ -39,20 +61,99 @@ describe('factory: view/topo/topoPanel.js', function() { ...@@ -39,20 +61,99 @@ describe('factory: view/topo/topoPanel.js', function() {
39 expect(fs.areFunctions(tps, [ 61 expect(fs.areFunctions(tps, [
40 'initPanels', 62 'initPanels',
41 'destroyPanels', 63 'destroyPanels',
64 + 'createTopoPanel',
65 +
42 'showSummary', 66 'showSummary',
43 'toggleSummary', 67 'toggleSummary',
68 +
44 'toggleUseDetailsFlag', 69 'toggleUseDetailsFlag',
45 'displaySingle', 70 'displaySingle',
46 'displayMulti', 71 'displayMulti',
47 - 'addAction',
48 'displayLink', 72 'displayLink',
49 'displayNothing', 73 'displayNothing',
50 'displaySomething', 74 'displaySomething',
75 + 'addAction',
76 +
51 'hideSummaryPanel', 77 'hideSummaryPanel',
78 +
52 'detailVisible', 79 'detailVisible',
53 'summaryVisible' 80 'summaryVisible'
54 ])).toBeTruthy(); 81 ])).toBeTruthy();
55 }); 82 });
56 83
84 + // === topoPanel api ------------------
85 +
86 + it('should define topoPanel api functions', function () {
87 + var panel = tps.createTopoPanel('foo');
88 + expect(fs.areFunctions(panel, [
89 + 'panel', 'setup', 'destroy',
90 + 'appendHeader', 'appendBody', 'appendFooter',
91 + 'adjustHeight'
92 + ])).toBeTruthy();
93 + panel.destroy();
94 + });
95 +
96 + it('should allow you to get panel', function () {
97 + var panel = tps.createTopoPanel('foo');
98 + expect(panel.panel()).toBeTruthy();
99 + panel.destroy();
100 + });
101 +
102 + it('should set up panel', function () {
103 + var p = tps.createTopoPanel('foo'),
104 + h, b, f;
105 + p.setup();
106 + expect(p.panel().el().selectAll('div').size()).toBe(3);
107 +
108 + h = p.panel().el().select('.header');
109 + expect(h.empty()).toBe(false);
110 + b = p.panel().el().select('.body');
111 + expect(b.empty()).toBe(false);
112 + f = p.panel().el().select('.footer');
113 + expect(f.empty()).toBe(false);
114 + p.destroy();
115 + });
116 +
117 + it('should destroy panel', function () {
118 + spyOn(ps, 'destroyPanel').and.callThrough();
119 + var p = tps.createTopoPanel('foo');
120 + p.destroy();
121 + expect(ps.destroyPanel).toHaveBeenCalledWith('foo');
122 + });
123 +
124 + it('should append to panel', function () {
125 + var p = tps.createTopoPanel('foo');
126 + p.setup();
127 + p.appendHeader('div').attr('id', 'header-div');
128 + expect(p.panel().el().select('#header-div').empty()).toBe(false);
129 + p.appendBody('p').attr('id', 'body-paragraph');
130 + expect(p.panel().el().select('#body-paragraph').empty()).toBe(false);
131 + p.appendFooter('svg').attr('id', 'footer-svg');
132 + expect(p.panel().el().select('#footer-svg').empty()).toBe(false);
133 + p.destroy();
134 + });
135 +
136 + it('should warn if fromTop not given, adjustHeight', function () {
137 + spyOn($log, 'warn');
138 + var p = tps.createTopoPanel('foo');
139 + p.adjustHeight();
140 + expect($log.warn).toHaveBeenCalledWith(
141 + 'adjustHeight: height from top of page not given'
142 + );
143 + p.destroy();
144 + });
145 +
146 + it('should warn if panel is not setup/defined, adjustHeight', function () {
147 + spyOn($log, 'warn');
148 + var p = tps.createTopoPanel('foo');
149 + p.adjustHeight(50);
150 + expect($log.warn).toHaveBeenCalledWith(
151 + 'adjustHeight: panel contents are not defined'
152 + );
153 + p.destroy();
154 + });
155 +
156 + // TODO: test adjustHeight height adjustment
157 +
57 // TODO: more tests... 158 // TODO: more tests...
58 }); 159 });
......