blob: 184d29db860fba845924b1808f679220bf7617d3 [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 Coleb5f2b152015-04-07 14:58:09 -070022 fs, 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 Coleb5f2b152015-04-07 14:58:09 -0700102 FnService, 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 Coleb5f2b152015-04-07 14:58:09 -0700107 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800108 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800109 }));
110
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800111 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800112 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800113 });
114
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700115 beforeEach(function () {
116 mockHeader = d3.select('body')
117 .append('h2')
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700118 .classed('tabular-header', true)
119 .style('height', mockHeaderHeight + 'px')
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700120 .html('Some Header');
121 });
122
Simon Huntc4ae8302015-01-26 14:22:48 -0800123 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800124 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800125 thead = null;
126 tbody = null;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700127 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800128 });
129
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800130 function compileTable() {
131 compiled = $compile(table);
132 compiled(scope);
133 scope.$digest();
134 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800135
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800136 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800137 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800138 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800139
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800140 thead = table.find('thead');
141 expect(thead).toBeDefined();
142 tbody = table.find('tbody');
143 expect(tbody).toBeDefined();
144 }
145
146 function verifyCssDisplay() {
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700147 var padding = 21, // bottom table constant 12 + mastPadding(?) 9
148 tableHeight = fs.windowSize(mockHeaderHeight).height -
149 (fs.noPx(table.find('thead').css('height')) + padding);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800150
151 expect(thead.css('display')).toBe('block');
152 expect(tbody.css('display')).toBe('block');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700153 expect(tbody.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800154 expect(tbody.css('overflow')).toBe('auto');
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700155
156 // TODO: investigate why math for calculating the height works better
157 // in the browser window (thead height is 0 in this test?)
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800158 }
159
160 function verifyColWidth() {
161 var winWidth = fs.windowSize().width,
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700162 colWidth, thElems, tr, tdElem;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800163
164 colWidth = Math.floor(winWidth / numTestElems);
165
166 thElems = thead.find('th');
167
168 angular.forEach(thElems, function (thElem, i) {
169 thElem = angular.element(thElems[i]);
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700170 tr = angular.element(tbody.find('tr').eq(1));
171 tdElem = angular.element(tr.find('td').eq(i));
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700172 var custWidth = thElem.attr('col-width');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800173
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700174 if (custWidth) {
175 expect(thElem.css('width')).toBe(custWidth);
176 expect(tdElem.css('width')).toBe(custWidth);
177 } else if (tdElem.attr('class') === 'table-icon') {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800178 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
179 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
180 } else {
181 expect(thElem.css('width')).toBe(colWidth + 'px');
182 expect(tdElem.css('width')).toBe(colWidth + 'px');
183 }
184 });
185 }
186
187 function verifyCallbacks(thElems) {
188 expect(scope.sortCallback).not.toHaveBeenCalled();
189
190 // first test header has no 'sortable' attr
191 thElems[0].click();
192 expect(scope.sortCallback).not.toHaveBeenCalled();
193
194 // the other headers have 'sortable'
195 for(var i = 1; i < numTestElems; i += 1) {
196 thElems[i].click();
197 expect(scope.sortCallback).toHaveBeenCalled();
198 }
199 }
200
201 function verifyIcons(thElems) {
202 var currentTh, div;
203 // make sure it has the correct icon after clicking
204 thElems[1].click();
205 currentTh = angular.element(thElems[1]);
206 div = currentTh.find('div');
207 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
208 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700209 '<g class="icon upArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800210 '<rect width="50" height="50" rx="5"></rect>' +
211 '<use width="50" height="50" class="glyph" ' +
212 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
213 'xlink:href="#triangleUp">' +
214 '</use>' +
215 '</g></svg>');
216 thElems[1].click();
217 div = currentTh.find('div');
218 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
219 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700220 '<g class="icon downArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800221 '<rect width="50" height="50" rx="5"></rect>' +
222 '<use width="50" height="50" class="glyph" ' +
223 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
224 'xlink:href="#triangleDown">' +
225 '</use>' +
226 '</g></svg>');
227
228 thElems[2].click();
229 div = currentTh.children();
230 // clicked on a new element, so the previous icon should have been deleted
231 expect(div.html()).toBeFalsy();
232
233 // the new element should have the ascending icon
234 currentTh = angular.element(thElems[2]);
235 div = currentTh.children();
236 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
237 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700238 '<g class="icon upArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800239 '<rect width="50" height="50" rx="5"></rect>' +
240 '<use width="50" height="50" class="glyph" ' +
241 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
242 'xlink:href="#triangleUp">' +
243 '</use>' +
244 '</g></svg>');
245 }
246
247 it('should affirm that onos-fixed-header is working', function () {
248 table = angular.element(onosFixedHeaderTags);
249
250 compileTable();
251 verifyGivenTags('onos-fixed-header');
252
253 // table will not be fixed unless it receives the 'LastElement' event
254 scope.$emit('LastElement');
255 scope.$digest();
256
257 verifyCssDisplay();
258 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800259 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800260
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800261 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800262 var thElems;
263 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800264
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800265 compileTable();
266 verifyGivenTags('onos-sortable-header');
267 // ctrlCallback functionality is tested in device-spec
268 // only checking that it has been called correctly in the directive
269 scope.sortCallback = jasmine.createSpy('sortCallback');
270
271 thElems = thead.find('th');
272 verifyCallbacks(thElems);
273 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800274 });
275
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800276});