blob: 995788fc042bc7812e724de3bc7e98852d93d9df [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole093739a2015-01-23 10:22:50 -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 -- Table Service - Unit Tests
19 */
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070020
Matteo Scandolo209c6c62016-05-21 10:08:57 -070021describe('factory: fw/widget/table.js', function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080022 var $log, $compile, $rootScope,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070023 fs, ts, mast, is,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070024 scope,
25 containerDiv,
26 headerDiv, bodyDiv,
27 header, body,
28 mockHeader,
29 mockHeaderHeight = 40;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080030
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070031 var onosFixedHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070032 '<div class="summary-list" onos-fixed-header>' +
33 '<div class="table-header">' +
34 '<table>' +
35 '<tr>' +
36 '<td colId="type" class="table-icon"></td>' +
37 '<td colId="id">Host ID </td>' +
38 '<td colId="mac" sortable>MAC Address </td>' +
39 '<td colId="location" col-width="110px">Location </td>' +
40 '</tr>' +
41 '</table>' +
42 '</div>' +
43
44 '<div class="table-body">' +
45 '<table>' +
46 '<tr class="ignore-width">' +
47 '<td class="not-picked"></td>' +
48 '</tr>' +
49 '<tr>' +
50 '<td class="table-icon">Some Icon</td>' +
51 '<td>Some ID</td>' +
52 '<td>Some MAC Address</td>' +
53 '<td>Some Location</td>' +
54 '</tr>' +
55 '</table>' +
56 '</div>' +
57 '</div>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080058
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070059 onosSortableHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070060 '<div onos-sortable-header ' +
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070061 'sort-callback="sortCallback(requestParams)">' +
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070062 '<table>' +
63 '<tr>' +
64 '<td colId="type"></td>' +
65 '<td colId="id" sortable>Host ID </td>' +
66 '<td colId="mac" sortable>MAC Address </td>' +
67 '<td colId="location" sortable>Location </td>' +
68 '</tr>' +
69 '</table>' +
70 '</div>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080071
Matteo Scandolo209c6c62016-05-21 10:08:57 -070072 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg', 'onosRemote', 'onosLayer'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080073
Simon Hunt31bb01f2015-03-31 16:50:41 -070074 var mockWindow = {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070075 innerWidth: 600,
76 innerHeight: 400,
Simon Hunt31bb01f2015-03-31 16:50:41 -070077 navigator: {
78 userAgent: 'defaultUA'
79 },
80 on: function () {},
81 addEventListener: function () {}
82 };
83
84 beforeEach(function () {
85 module(function ($provide) {
86 $provide.value('$window', mockWindow);
87 });
88 });
89
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080090 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Matteo Scandolo209c6c62016-05-21 10:08:57 -070091 FnService, TableBuilderService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080092 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080093 $compile = _$compile_;
94 $rootScope = _$rootScope_;
95 fs = FnService;
Matteo Scandolo209c6c62016-05-21 10:08:57 -070096 ts = TableBuilderService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070097 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080098 is = IconService;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080099 scope = $rootScope.$new();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700100 scope.tableData = [];
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700101 }));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800102
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700103 // Note: dummy header so that d3 doesn't trip up.
104 // $compile has to be used on the directive tag element, so it can't
105 // be included in the tag strings declared above.
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700106 beforeEach(function () {
107 mockHeader = d3.select('body')
108 .append('h2')
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700109 .classed('tabular-header', true)
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700110 .style({
111 height: mockHeaderHeight + 'px',
112 margin: 0,
113 padding: 0
114 })
115 .text('Some Header');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700116 });
117
Simon Huntc4ae8302015-01-26 14:22:48 -0800118 afterEach(function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700119 containerDiv = undefined;
120 headerDiv = undefined;
121 bodyDiv = undefined;
122 header = undefined;
123 body = undefined;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700124 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800125 });
126
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700127 function populateTableData() {
128 scope.tableData = [
129 {
130 type: 'endstation',
131 id: '1234',
132 mac: '00:00:03',
133 location: 'USA'
134 }
135 ];
136 }
137
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700138 it('should define TableBuilderService', function () {
139 expect(ts).toBeDefined();
140 });
141
142 it('should define api functions', function () {
143 expect(fs.areFunctions(ts, [
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700144 'buildTable'
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700145 ])).toBeTruthy();
146 });
147
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700148 function compile(elem) {
149 var compiled = $compile(elem);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800150 compiled(scope);
151 scope.$digest();
152 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800153
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700154 function selectTables() {
155 expect(containerDiv.find('div').length).toBe(2);
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800156
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700157 headerDiv = angular.element(containerDiv[0].querySelector('.table-header'));
158 expect(headerDiv.length).toBe(1);
159
160 bodyDiv = angular.element(containerDiv[0].querySelector('.table-body'));
161 expect(bodyDiv.length).toBe(1);
162
163 header = headerDiv.find('table');
164 expect(header.length).toBe(1);
165
166 body = bodyDiv.find('table');
167 expect(body.length).toBe(1);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800168 }
169
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700170 function verifyGivenTags(dirName, div) {
171 expect(div).toBeDefined();
172 expect(div.attr(dirName)).toBe('');
173 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800174
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700175 function verifyDefaultSize() {
176 expect(header.css('width')).toBe('570px');
177 expect(body.css('width')).toBe('570px');
178 }
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700179
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700180 function verifyHeight() {
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -0700181 var padding = 22,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700182 mastHeight = 36,
183 tableHeight = (mockWindow.innerHeight - mockHeaderHeight) -
184 (fs.noPx(headerDiv.css('height')) + mastHeight + padding);
185
186 expect(bodyDiv.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800187 }
188
189 function verifyColWidth() {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700190 var hdrs = header.find('td'),
191 cols = body.find('td');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800192
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700193 expect(angular.element(hdrs[0]).css('width')).toBe('33px');
194 expect(angular.element(hdrs[3]).css('width')).toBe('110px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800195
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700196 expect(angular.element(cols[1]).css('width')).toBe('33px');
197 expect(angular.element(cols[4]).css('width')).toBe('110px');
198 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800199
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700200 function verifyCallbacks(h) {
201 expect(scope.sortCallback).not.toHaveBeenCalled();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800202
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700203 h[0].click();
204 expect(scope.sortCallback).not.toHaveBeenCalled();
205
206 h[1].click();
207 expect(scope.sortCallback).toHaveBeenCalledWith({
208 sortCol: 'id',
209 sortDir: 'asc'
210 });
211 h[1].click();
212 expect(scope.sortCallback).toHaveBeenCalledWith({
213 sortCol: 'id',
214 sortDir: 'desc'
215 });
216 h[1].click();
217 expect(scope.sortCallback).toHaveBeenCalledWith({
218 sortCol: 'id',
219 sortDir: 'asc'
220 });
221
222 h[2].click();
223 expect(scope.sortCallback).toHaveBeenCalledWith({
224 sortCol: 'mac',
225 sortDir: 'asc'
226 });
227 h[2].click();
228 expect(scope.sortCallback).toHaveBeenCalledWith({
229 sortCol: 'mac',
230 sortDir: 'desc'
231 });
232 h[2].click();
233 expect(scope.sortCallback).toHaveBeenCalledWith({
234 sortCol: 'mac',
235 sortDir: 'asc'
236 });
237
238 h[3].click();
239 expect(scope.sortCallback).toHaveBeenCalledWith({
240 sortCol: 'location',
241 sortDir: 'asc'
242 });
243 h[3].click();
244 expect(scope.sortCallback).toHaveBeenCalledWith({
245 sortCol: 'location',
246 sortDir: 'desc'
247 });
248 h[3].click();
249 expect(scope.sortCallback).toHaveBeenCalledWith({
250 sortCol: 'location',
251 sortDir: 'asc'
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800252 });
253 }
254
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700255 function verifyIcons(h) {
256 var currH, div;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800257
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700258 h[1].click();
259 currH = angular.element(h[1]);
260 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700261 expect(div.html()).toBe(
262 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
263 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
264 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
265 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
266 '</use></g></svg>'
267 );
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700268 h[1].click();
269 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700270 expect(div.html()).toBe(
271 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
272 '50 50"><g class="icon downArrow"><rect width="50" height="50" ' +
273 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
274 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleDown">' +
275 '</use></g></svg>'
276 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800277
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700278 h[2].click();
279 div = currH.children();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800280 // clicked on a new element, so the previous icon should have been deleted
281 expect(div.html()).toBeFalsy();
282
283 // the new element should have the ascending icon
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700284 currH = angular.element(h[2]);
285 div = currH.children();
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700286 expect(div.html()).toBe(
287 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
288 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
289 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
290 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
291 '</use></g></svg>'
292 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800293 }
294
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700295 xit('should affirm that onos-fixed-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700296 containerDiv = angular.element(onosFixedHeaderTags);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800297
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700298 compile(containerDiv);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800299
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700300 verifyGivenTags('onos-fixed-header', containerDiv);
301 selectTables();
302 verifyDefaultSize();
303
304 populateTableData();
305
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800306 scope.$emit('LastElement');
307 scope.$digest();
308
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700309 verifyHeight();
310 verifyColWidth();
311
312 mockWindow.innerHeight = 300;
313 scope.$digest();
314 verifyHeight();
315
316 mockWindow.innerWidth = 500;
317 scope.$digest();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800318 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800319 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800320
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700321 xit('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700322 headerDiv = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800323
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700324 compile(headerDiv);
325 verifyGivenTags('onos-sortable-header', headerDiv);
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700326
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800327 scope.sortCallback = jasmine.createSpy('sortCallback');
328
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700329 header = headerDiv.find('td');
330 verifyCallbacks(header);
331 verifyIcons(header);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800332 });
333
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700334 // Note: testing resetSortIcons isn't feasible because due to the nature of
335 // directive compilation: they are jQuery elements, not d3 elements,
336 // so the function doesn't work.
337
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800338});