blob: e3570ae42a1917c8fe3ef63b0a602eec10243214 [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,
25 mockh2Height = '10px',
Simon Hunt3074fb22015-03-31 15:06:25 -070026 tableIconTdSize = 100,
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070027 pdgTop = 101,
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080028 numTestElems = 4;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080029
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080030 var onosFixedHeaderTags = '<table ' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080031 'onos-fixed-header>' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080032 '<thead>' +
33 '<tr>' +
34 '<th></th>' +
35 '<th>Device ID </th>' +
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070036 '<th col-width="100px">H/W Version </th>' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080037 '<th>S/W Version </th>' +
38 '</tr>' +
39 '</thead>' +
40 '<tbody>' +
41 '<tr>' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070042 '<td colspan="4">' +
43 'No Devices found' +
44 '</td>' +
45 '</tr>' +
46 '<tr>' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080047 '<td class="table-icon">' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080048 '<div icon icon-id="{{dev._iconid_available}}">' +
49 '</div>' +
50 '</td>' +
51 '<td>Some ID</td>' +
52 '<td>Some HW</td>' +
53 '<td>Some Software</td>' +
54 '</tr>' +
55 '</tbody>' +
56 '</table>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080057
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080058 onosSortableHeaderTags = '<table ' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080059 'onos-sortable-header ' +
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070060 'sort-callback="sortCallback(requestParams)">' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080061 '<thead>' +
62 '<tr>' +
63 '<th colId="available"></th>' +
64 '<th colId="id" sortable>Device ID </th>' +
65 '<th colId="hw" sortable>H/W Version </th>' +
66 '<th colId="sw" sortable>S/W Version </th>' +
67 '</tr>' +
68 '</thead>' +
69 '<tbody>' +
70 '<tr>' +
71 '<td>' +
72 '<div icon icon-id="{{dev._iconid_available}}">' +
73 '</div>' +
74 '</td>' +
75 '<td>Some ID</td>' +
76 '<td>Some HW</td>' +
77 '<td>Some Software</td>' +
78 '</tr>' +
79 '</tbody>' +
80 '</table>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080081
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070082 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080083
Simon Hunt31bb01f2015-03-31 16:50:41 -070084 var mockWindow = {
85 innerWidth: 400,
86 innerHeight: 200,
87 navigator: {
88 userAgent: 'defaultUA'
89 },
90 on: function () {},
91 addEventListener: function () {}
92 };
93
94 beforeEach(function () {
95 module(function ($provide) {
96 $provide.value('$window', mockWindow);
97 });
98 });
99
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800100 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700101 FnService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800102 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800103 $compile = _$compile_;
104 $rootScope = _$rootScope_;
105 fs = FnService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700106 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800107 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800108 }));
109
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800110 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800111 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800112 });
113
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700114 beforeEach(function () {
115 mockHeader = d3.select('body')
116 .append('h2')
117 .style('height', mockh2Height)
118 .html('Some Header');
119 });
120
Simon Huntc4ae8302015-01-26 14:22:48 -0800121 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800122 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800123 thead = null;
124 tbody = null;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700125 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800126 });
127
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800128 function compileTable() {
129 compiled = $compile(table);
130 compiled(scope);
131 scope.$digest();
132 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800133
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800134 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800135 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800136 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800137
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800138 thead = table.find('thead');
139 expect(thead).toBeDefined();
140 tbody = table.find('tbody');
141 expect(tbody).toBeDefined();
142 }
143
144 function verifyCssDisplay() {
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700145 var tableHeight = fs.windowSize(pdgTop).height;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800146
147 expect(thead.css('display')).toBe('block');
148 expect(tbody.css('display')).toBe('block');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700149 expect(tbody.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800150 expect(tbody.css('overflow')).toBe('auto');
151 }
152
153 function verifyColWidth() {
154 var winWidth = fs.windowSize().width,
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700155 colWidth, thElems, tr, tdElem;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800156
157 colWidth = Math.floor(winWidth / numTestElems);
158
159 thElems = thead.find('th');
160
161 angular.forEach(thElems, function (thElem, i) {
162 thElem = angular.element(thElems[i]);
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700163 tr = angular.element(tbody.find('tr').eq(1));
164 tdElem = angular.element(tr.find('td').eq(i));
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700165 var custWidth = thElem.attr('col-width');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800166
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700167 if (custWidth) {
168 expect(thElem.css('width')).toBe(custWidth);
169 expect(tdElem.css('width')).toBe(custWidth);
170 } else if (tdElem.attr('class') === 'table-icon') {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800171 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
172 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
173 } else {
174 expect(thElem.css('width')).toBe(colWidth + 'px');
175 expect(tdElem.css('width')).toBe(colWidth + 'px');
176 }
177 });
178 }
179
180 function verifyCallbacks(thElems) {
181 expect(scope.sortCallback).not.toHaveBeenCalled();
182
183 // first test header has no 'sortable' attr
184 thElems[0].click();
185 expect(scope.sortCallback).not.toHaveBeenCalled();
186
187 // the other headers have 'sortable'
188 for(var i = 1; i < numTestElems; i += 1) {
189 thElems[i].click();
190 expect(scope.sortCallback).toHaveBeenCalled();
191 }
192 }
193
194 function verifyIcons(thElems) {
195 var currentTh, div;
196 // make sure it has the correct icon after clicking
197 thElems[1].click();
198 currentTh = angular.element(thElems[1]);
199 div = currentTh.find('div');
200 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
201 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700202 '<g class="icon upArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800203 '<rect width="50" height="50" rx="5"></rect>' +
204 '<use width="50" height="50" class="glyph" ' +
205 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
206 'xlink:href="#triangleUp">' +
207 '</use>' +
208 '</g></svg>');
209 thElems[1].click();
210 div = currentTh.find('div');
211 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
212 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700213 '<g class="icon downArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800214 '<rect width="50" height="50" rx="5"></rect>' +
215 '<use width="50" height="50" class="glyph" ' +
216 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
217 'xlink:href="#triangleDown">' +
218 '</use>' +
219 '</g></svg>');
220
221 thElems[2].click();
222 div = currentTh.children();
223 // clicked on a new element, so the previous icon should have been deleted
224 expect(div.html()).toBeFalsy();
225
226 // the new element should have the ascending icon
227 currentTh = angular.element(thElems[2]);
228 div = currentTh.children();
229 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
230 'width="10" height="10" viewBox="0 0 50 50">' +
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700231 '<g class="icon upArrow">' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800232 '<rect width="50" height="50" rx="5"></rect>' +
233 '<use width="50" height="50" class="glyph" ' +
234 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
235 'xlink:href="#triangleUp">' +
236 '</use>' +
237 '</g></svg>');
238 }
239
240 it('should affirm that onos-fixed-header is working', function () {
241 table = angular.element(onosFixedHeaderTags);
242
243 compileTable();
244 verifyGivenTags('onos-fixed-header');
245
246 // table will not be fixed unless it receives the 'LastElement' event
247 scope.$emit('LastElement');
248 scope.$digest();
249
250 verifyCssDisplay();
251 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800252 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800253
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800254 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800255 var thElems;
256 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800257
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800258 compileTable();
259 verifyGivenTags('onos-sortable-header');
260 // ctrlCallback functionality is tested in device-spec
261 // only checking that it has been called correctly in the directive
262 scope.sortCallback = jasmine.createSpy('sortCallback');
263
264 thElems = thead.find('th');
265 verifyCallbacks(thElems);
266 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800267 });
268
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800269});