blob: 693f42d1ad76df2175bf615a4bc13ffaf440ed1d [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 Cole7c445752015-02-17 14:49:46 -080023 scope, compiled,
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070024 table, thead, tbody, mockHeader,
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070025 mockH2Height = 20,
26 mockMastHeight = 20,
27 mockHeaderHeight = mockH2Height + mockMastHeight,
Simon Hunt3074fb22015-03-31 15:06:25 -070028 tableIconTdSize = 100,
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080029 numTestElems = 4;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080030
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070031 var onosFixedHeaderTags =
32 '<table onos-fixed-header>' +
33 '<thead style="height:27px;">' +
34 '<tr>' +
35 '<th></th>' +
36 '<th>Device ID </th>' +
37 '<th col-width="100px">H/W Version </th>' +
38 '<th>S/W Version </th>' +
39 '</tr>' +
40 '</thead>' +
41 '<tbody>' +
42 '<tr>' +
43 '<td colspan="4">' +
44 'No Devices found' +
45 '</td>' +
46 '</tr>' +
47 '<tr>' +
48 '<td class="table-icon">' +
49 '<div icon icon-id="{{dev._iconid_available}}">' +
50 '</div>' +
51 '</td>' +
52 '<td>Some ID</td>' +
53 '<td>Some HW</td>' +
54 '<td>Some Software</td>' +
55 '</tr>' +
56 '</tbody>' +
57 '</table>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080058
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070059 onosSortableHeaderTags =
60 '<table onos-sortable-header ' +
61 'sort-callback="sortCallback(requestParams)">' +
62 '<thead>' +
63 '<tr>' +
64 '<th colId="available"></th>' +
65 '<th colId="id" sortable>Device ID </th>' +
66 '<th colId="hw" sortable>H/W Version </th>' +
67 '<th colId="sw" sortable>S/W Version </th>' +
68 '</tr>' +
69 '</thead>' +
70 '<tbody>' +
71 '<tr>' +
72 '<td>' +
73 '<div icon icon-id="{{dev._iconid_available}}">' +
74 '</div>' +
75 '</td>' +
76 '<td>Some ID</td>' +
77 '<td>Some HW</td>' +
78 '<td>Some Software</td>' +
79 '</tr>' +
80 '</tbody>' +
81 '</table>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080082
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070083 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080084
Simon Hunt31bb01f2015-03-31 16:50:41 -070085 var mockWindow = {
86 innerWidth: 400,
87 innerHeight: 200,
88 navigator: {
89 userAgent: 'defaultUA'
90 },
91 on: function () {},
92 addEventListener: function () {}
93 };
94
95 beforeEach(function () {
96 module(function ($provide) {
97 $provide.value('$window', mockWindow);
98 });
99 });
100
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800101 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700102 FnService, TableService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800103 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800104 $compile = _$compile_;
105 $rootScope = _$rootScope_;
106 fs = FnService;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700107 ts = TableService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700108 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800109 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800110 }));
111
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800112 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800113 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800114 });
115
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700116 beforeEach(function () {
117 mockHeader = d3.select('body')
118 .append('h2')
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700119 .classed('tabular-header', true)
120 .style('height', mockHeaderHeight + 'px')
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700121 .html('Some Header');
122 });
123
Simon Huntc4ae8302015-01-26 14:22:48 -0800124 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800125 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800126 thead = null;
127 tbody = null;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700128 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800129 });
130
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700131 it('should define TableBuilderService', function () {
132 expect(ts).toBeDefined();
133 });
134
135 it('should define api functions', function () {
136 expect(fs.areFunctions(ts, [
137 'resetSortIcons'
138 ])).toBeTruthy();
139 });
140
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800141 function compileTable() {
142 compiled = $compile(table);
143 compiled(scope);
144 scope.$digest();
145 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800146
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800147 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800148 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800149 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800150
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800151 thead = table.find('thead');
152 expect(thead).toBeDefined();
153 tbody = table.find('tbody');
154 expect(tbody).toBeDefined();
155 }
156
157 function verifyCssDisplay() {
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700158 var padding = 21, // bottom table constant 12 + mastPadding(?) 9
159 tableHeight = fs.windowSize(mockHeaderHeight).height -
160 (fs.noPx(table.find('thead').css('height')) + padding);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800161
162 expect(thead.css('display')).toBe('block');
163 expect(tbody.css('display')).toBe('block');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700164 expect(tbody.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800165 expect(tbody.css('overflow')).toBe('auto');
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700166
167 // TODO: investigate why math for calculating the height works better
168 // in the browser window (thead height is 0 in this test?)
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800169 }
170
171 function verifyColWidth() {
172 var winWidth = fs.windowSize().width,
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700173 colWidth, thElems, tr, tdElem;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800174
175 colWidth = Math.floor(winWidth / numTestElems);
176
177 thElems = thead.find('th');
178
179 angular.forEach(thElems, function (thElem, i) {
180 thElem = angular.element(thElems[i]);
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700181 tr = angular.element(tbody.find('tr').eq(1));
182 tdElem = angular.element(tr.find('td').eq(i));
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700183 var custWidth = thElem.attr('col-width');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800184
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700185 if (custWidth) {
186 expect(thElem.css('width')).toBe(custWidth);
187 expect(tdElem.css('width')).toBe(custWidth);
188 } else if (tdElem.attr('class') === 'table-icon') {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800189 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
190 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
191 } else {
192 expect(thElem.css('width')).toBe(colWidth + 'px');
193 expect(tdElem.css('width')).toBe(colWidth + 'px');
194 }
195 });
196 }
197
198 function verifyCallbacks(thElems) {
199 expect(scope.sortCallback).not.toHaveBeenCalled();
200
201 // first test header has no 'sortable' attr
202 thElems[0].click();
203 expect(scope.sortCallback).not.toHaveBeenCalled();
204
205 // the other headers have 'sortable'
206 for(var i = 1; i < numTestElems; i += 1) {
207 thElems[i].click();
208 expect(scope.sortCallback).toHaveBeenCalled();
209 }
210 }
211
212 function verifyIcons(thElems) {
213 var currentTh, div;
214 // make sure it has the correct icon after clicking
215 thElems[1].click();
216 currentTh = angular.element(thElems[1]);
217 div = currentTh.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700218 expect(div.html()).toBe(
219 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
220 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
221 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
222 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
223 '</use></g></svg>'
224 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800225 thElems[1].click();
226 div = currentTh.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700227 expect(div.html()).toBe(
228 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
229 '50 50"><g class="icon downArrow"><rect width="50" height="50" ' +
230 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
231 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleDown">' +
232 '</use></g></svg>'
233 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800234
235 thElems[2].click();
236 div = currentTh.children();
237 // clicked on a new element, so the previous icon should have been deleted
238 expect(div.html()).toBeFalsy();
239
240 // the new element should have the ascending icon
241 currentTh = angular.element(thElems[2]);
242 div = currentTh.children();
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700243 expect(div.html()).toBe(
244 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
245 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
246 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
247 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
248 '</use></g></svg>'
249 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800250 }
251
252 it('should affirm that onos-fixed-header is working', function () {
253 table = angular.element(onosFixedHeaderTags);
254
255 compileTable();
256 verifyGivenTags('onos-fixed-header');
257
258 // table will not be fixed unless it receives the 'LastElement' event
259 scope.$emit('LastElement');
260 scope.$digest();
261
262 verifyCssDisplay();
263 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800264 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800265
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800266 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800267 var thElems;
268 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800269
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800270 compileTable();
271 verifyGivenTags('onos-sortable-header');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700272
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800273 scope.sortCallback = jasmine.createSpy('sortCallback');
274
275 thElems = thead.find('th');
276 verifyCallbacks(thElems);
277 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800278 });
279
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700280 // Note: testing resetSortIcons isn't feasible because due to the nature of
281 // directive compilation: they are jQuery elements, not d3 elements,
282 // so the function doesn't work.
283
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800284});