blob: 4f8f0c0ff0e12dacd725313d19695f0ed99a29fe [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 -- Table Service - Unit Tests
19 */
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080020describe('factory: fw/widget/table.js', function () {
21 var $log, $compile, $rootScope,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070022 fs, ts, mast, is,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070023 scope,
24 containerDiv,
25 headerDiv, bodyDiv,
26 header, body,
27 mockHeader,
28 mockHeaderHeight = 40;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080029
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070030 var onosFixedHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070031 '<div class="summary-list" onos-fixed-header>' +
32 '<div class="table-header">' +
33 '<table>' +
34 '<tr>' +
35 '<td colId="type" class="table-icon"></td>' +
36 '<td colId="id">Host ID </td>' +
37 '<td colId="mac" sortable>MAC Address </td>' +
38 '<td colId="location" col-width="110px">Location </td>' +
39 '</tr>' +
40 '</table>' +
41 '</div>' +
42
43 '<div class="table-body">' +
44 '<table>' +
45 '<tr class="ignore-width">' +
46 '<td class="not-picked"></td>' +
47 '</tr>' +
48 '<tr>' +
49 '<td class="table-icon">Some Icon</td>' +
50 '<td>Some ID</td>' +
51 '<td>Some MAC Address</td>' +
52 '<td>Some Location</td>' +
53 '</tr>' +
54 '</table>' +
55 '</div>' +
56 '</div>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080057
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070058 onosSortableHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070059 '<div onos-sortable-header ' +
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070060 'sort-callback="sortCallback(requestParams)">' +
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070061 '<table>' +
62 '<tr>' +
63 '<td colId="type"></td>' +
64 '<td colId="id" sortable>Host ID </td>' +
65 '<td colId="mac" sortable>MAC Address </td>' +
66 '<td colId="location" sortable>Location </td>' +
67 '</tr>' +
68 '</table>' +
69 '</div>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080070
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070071 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080072
Simon Hunt31bb01f2015-03-31 16:50:41 -070073 var mockWindow = {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070074 innerWidth: 600,
75 innerHeight: 400,
Simon Hunt31bb01f2015-03-31 16:50:41 -070076 navigator: {
77 userAgent: 'defaultUA'
78 },
79 on: function () {},
80 addEventListener: function () {}
81 };
82
83 beforeEach(function () {
84 module(function ($provide) {
85 $provide.value('$window', mockWindow);
86 });
87 });
88
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080089 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070090 FnService, TableService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080091 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080092 $compile = _$compile_;
93 $rootScope = _$rootScope_;
94 fs = FnService;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070095 ts = TableService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070096 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080097 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080098 }));
99
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800100 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800101 scope = $rootScope.$new();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700102 scope.tableData = [];
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800103 });
104
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700105 // Note: dummy header so that d3 doesn't trip up.
106 // $compile has to be used on the directive tag element, so it can't
107 // be included in the tag strings declared above.
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700108 beforeEach(function () {
109 mockHeader = d3.select('body')
110 .append('h2')
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700111 .classed('tabular-header', true)
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700112 .style({
113 height: mockHeaderHeight + 'px',
114 margin: 0,
115 padding: 0
116 })
117 .text('Some Header');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700118 });
119
Simon Huntc4ae8302015-01-26 14:22:48 -0800120 afterEach(function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700121 containerDiv = undefined;
122 headerDiv = undefined;
123 bodyDiv = undefined;
124 header = undefined;
125 body = undefined;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700126 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800127 });
128
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700129 function populateTableData() {
130 scope.tableData = [
131 {
132 type: 'endstation',
133 id: '1234',
134 mac: '00:00:03',
135 location: 'USA'
136 }
137 ];
138 }
139
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700140 it('should define TableBuilderService', function () {
141 expect(ts).toBeDefined();
142 });
143
144 it('should define api functions', function () {
145 expect(fs.areFunctions(ts, [
146 'resetSortIcons'
147 ])).toBeTruthy();
148 });
149
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700150 function compile(elem) {
151 var compiled = $compile(elem);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800152 compiled(scope);
153 scope.$digest();
154 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800155
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700156 function selectTables() {
157 expect(containerDiv.find('div').length).toBe(2);
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800158
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700159 headerDiv = angular.element(containerDiv[0].querySelector('.table-header'));
160 expect(headerDiv.length).toBe(1);
161
162 bodyDiv = angular.element(containerDiv[0].querySelector('.table-body'));
163 expect(bodyDiv.length).toBe(1);
164
165 header = headerDiv.find('table');
166 expect(header.length).toBe(1);
167
168 body = bodyDiv.find('table');
169 expect(body.length).toBe(1);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800170 }
171
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700172 function verifyGivenTags(dirName, div) {
173 expect(div).toBeDefined();
174 expect(div.attr(dirName)).toBe('');
175 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800176
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700177 function verifyDefaultSize() {
178 expect(header.css('width')).toBe('570px');
179 expect(body.css('width')).toBe('570px');
180 }
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700181
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700182 function verifyHeight() {
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -0700183 var padding = 22,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700184 mastHeight = 36,
185 tableHeight = (mockWindow.innerHeight - mockHeaderHeight) -
186 (fs.noPx(headerDiv.css('height')) + mastHeight + padding);
187
188 expect(bodyDiv.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800189 }
190
191 function verifyColWidth() {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700192 var hdrs = header.find('td'),
193 cols = body.find('td');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800194
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700195 expect(angular.element(hdrs[0]).css('width')).toBe('33px');
196 expect(angular.element(hdrs[3]).css('width')).toBe('110px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800197
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700198 expect(angular.element(cols[1]).css('width')).toBe('33px');
199 expect(angular.element(cols[4]).css('width')).toBe('110px');
200 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800201
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700202 function verifyCallbacks(h) {
203 expect(scope.sortCallback).not.toHaveBeenCalled();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800204
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700205 h[0].click();
206 expect(scope.sortCallback).not.toHaveBeenCalled();
207
208 h[1].click();
209 expect(scope.sortCallback).toHaveBeenCalledWith({
210 sortCol: 'id',
211 sortDir: 'asc'
212 });
213 h[1].click();
214 expect(scope.sortCallback).toHaveBeenCalledWith({
215 sortCol: 'id',
216 sortDir: 'desc'
217 });
218 h[1].click();
219 expect(scope.sortCallback).toHaveBeenCalledWith({
220 sortCol: 'id',
221 sortDir: 'asc'
222 });
223
224 h[2].click();
225 expect(scope.sortCallback).toHaveBeenCalledWith({
226 sortCol: 'mac',
227 sortDir: 'asc'
228 });
229 h[2].click();
230 expect(scope.sortCallback).toHaveBeenCalledWith({
231 sortCol: 'mac',
232 sortDir: 'desc'
233 });
234 h[2].click();
235 expect(scope.sortCallback).toHaveBeenCalledWith({
236 sortCol: 'mac',
237 sortDir: 'asc'
238 });
239
240 h[3].click();
241 expect(scope.sortCallback).toHaveBeenCalledWith({
242 sortCol: 'location',
243 sortDir: 'asc'
244 });
245 h[3].click();
246 expect(scope.sortCallback).toHaveBeenCalledWith({
247 sortCol: 'location',
248 sortDir: 'desc'
249 });
250 h[3].click();
251 expect(scope.sortCallback).toHaveBeenCalledWith({
252 sortCol: 'location',
253 sortDir: 'asc'
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800254 });
255 }
256
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700257 function verifyIcons(h) {
258 var currH, div;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800259
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700260 h[1].click();
261 currH = angular.element(h[1]);
262 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700263 expect(div.html()).toBe(
264 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
265 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
266 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
267 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
268 '</use></g></svg>'
269 );
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700270 h[1].click();
271 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700272 expect(div.html()).toBe(
273 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
274 '50 50"><g class="icon downArrow"><rect width="50" height="50" ' +
275 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
276 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleDown">' +
277 '</use></g></svg>'
278 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800279
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700280 h[2].click();
281 div = currH.children();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800282 // clicked on a new element, so the previous icon should have been deleted
283 expect(div.html()).toBeFalsy();
284
285 // the new element should have the ascending icon
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700286 currH = angular.element(h[2]);
287 div = currH.children();
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700288 expect(div.html()).toBe(
289 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
290 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
291 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
292 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
293 '</use></g></svg>'
294 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800295 }
296
297 it('should affirm that onos-fixed-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700298 containerDiv = angular.element(onosFixedHeaderTags);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800299
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700300 compile(containerDiv);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800301
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700302 verifyGivenTags('onos-fixed-header', containerDiv);
303 selectTables();
304 verifyDefaultSize();
305
306 populateTableData();
307
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800308 scope.$emit('LastElement');
309 scope.$digest();
310
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700311 verifyHeight();
312 verifyColWidth();
313
314 mockWindow.innerHeight = 300;
315 scope.$digest();
316 verifyHeight();
317
318 mockWindow.innerWidth = 500;
319 scope.$digest();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800320 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800321 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800322
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800323 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700324 headerDiv = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800325
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700326 compile(headerDiv);
327 verifyGivenTags('onos-sortable-header', headerDiv);
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700328
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800329 scope.sortCallback = jasmine.createSpy('sortCallback');
330
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700331 header = headerDiv.find('td');
332 verifyCallbacks(header);
333 verifyIcons(header);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800334 });
335
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700336 // Note: testing resetSortIcons isn't feasible because due to the nature of
337 // directive compilation: they are jQuery elements, not d3 elements,
338 // so the function doesn't work.
339
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800340});