blob: 29927cd31b10e5620af0ef7f939c015db05b4c9c [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 Cole7c445752015-02-17 14:49:46 -080042 '<td class="table-icon">' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080043 '<div icon icon-id="{{dev._iconid_available}}">' +
44 '</div>' +
45 '</td>' +
46 '<td>Some ID</td>' +
47 '<td>Some HW</td>' +
48 '<td>Some Software</td>' +
49 '</tr>' +
50 '</tbody>' +
51 '</table>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080052
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080053 onosSortableHeaderTags = '<table ' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080054 'onos-sortable-header ' +
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070055 'sort-callback="sortCallback(requestParams)">' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080056 '<thead>' +
57 '<tr>' +
58 '<th colId="available"></th>' +
59 '<th colId="id" sortable>Device ID </th>' +
60 '<th colId="hw" sortable>H/W Version </th>' +
61 '<th colId="sw" sortable>S/W Version </th>' +
62 '</tr>' +
63 '</thead>' +
64 '<tbody>' +
65 '<tr>' +
66 '<td>' +
67 '<div icon icon-id="{{dev._iconid_available}}">' +
68 '</div>' +
69 '</td>' +
70 '<td>Some ID</td>' +
71 '<td>Some HW</td>' +
72 '<td>Some Software</td>' +
73 '</tr>' +
74 '</tbody>' +
75 '</table>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080076
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070077 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080078
Simon Hunt31bb01f2015-03-31 16:50:41 -070079 var mockWindow = {
80 innerWidth: 400,
81 innerHeight: 200,
82 navigator: {
83 userAgent: 'defaultUA'
84 },
85 on: function () {},
86 addEventListener: function () {}
87 };
88
89 beforeEach(function () {
90 module(function ($provide) {
91 $provide.value('$window', mockWindow);
92 });
93 });
94
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080095 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070096 FnService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080097 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080098 $compile = _$compile_;
99 $rootScope = _$rootScope_;
100 fs = FnService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700101 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800102 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800103 }));
104
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800105 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800106 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800107 });
108
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700109 beforeEach(function () {
110 mockHeader = d3.select('body')
111 .append('h2')
112 .style('height', mockh2Height)
113 .html('Some Header');
114 });
115
Simon Huntc4ae8302015-01-26 14:22:48 -0800116 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800117 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800118 thead = null;
119 tbody = null;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700120 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800121 });
122
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800123 function compileTable() {
124 compiled = $compile(table);
125 compiled(scope);
126 scope.$digest();
127 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800128
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800129 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800130 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800131 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800132
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800133 thead = table.find('thead');
134 expect(thead).toBeDefined();
135 tbody = table.find('tbody');
136 expect(tbody).toBeDefined();
137 }
138
139 function verifyCssDisplay() {
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700140 var tableHeight = fs.windowSize(pdgTop).height;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800141
142 expect(thead.css('display')).toBe('block');
143 expect(tbody.css('display')).toBe('block');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700144 expect(tbody.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800145 expect(tbody.css('overflow')).toBe('auto');
146 }
147
148 function verifyColWidth() {
149 var winWidth = fs.windowSize().width,
150 colWidth, thElems, tdElem;
151
152 colWidth = Math.floor(winWidth / numTestElems);
153
154 thElems = thead.find('th');
155
156 angular.forEach(thElems, function (thElem, i) {
157 thElem = angular.element(thElems[i]);
158 tdElem = angular.element(tbody.find('td').eq(i));
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700159 var custWidth = thElem.attr('col-width');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800160
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700161 if (custWidth) {
162 expect(thElem.css('width')).toBe(custWidth);
163 expect(tdElem.css('width')).toBe(custWidth);
164 } else if (tdElem.attr('class') === 'table-icon') {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800165 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
166 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
167 } else {
168 expect(thElem.css('width')).toBe(colWidth + 'px');
169 expect(tdElem.css('width')).toBe(colWidth + 'px');
170 }
171 });
172 }
173
174 function verifyCallbacks(thElems) {
175 expect(scope.sortCallback).not.toHaveBeenCalled();
176
177 // first test header has no 'sortable' attr
178 thElems[0].click();
179 expect(scope.sortCallback).not.toHaveBeenCalled();
180
181 // the other headers have 'sortable'
182 for(var i = 1; i < numTestElems; i += 1) {
183 thElems[i].click();
184 expect(scope.sortCallback).toHaveBeenCalled();
185 }
186 }
187
188 function verifyIcons(thElems) {
189 var currentTh, div;
190 // make sure it has the correct icon after clicking
191 thElems[1].click();
192 currentTh = angular.element(thElems[1]);
193 div = currentTh.find('div');
194 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
195 'width="10" height="10" viewBox="0 0 50 50">' +
196 '<g class="icon tableColSortAsc">' +
197 '<rect width="50" height="50" rx="5"></rect>' +
198 '<use width="50" height="50" class="glyph" ' +
199 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
200 'xlink:href="#triangleUp">' +
201 '</use>' +
202 '</g></svg>');
203 thElems[1].click();
204 div = currentTh.find('div');
205 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
206 'width="10" height="10" viewBox="0 0 50 50">' +
207 '<g class="icon tableColSortDesc">' +
208 '<rect width="50" height="50" rx="5"></rect>' +
209 '<use width="50" height="50" class="glyph" ' +
210 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
211 'xlink:href="#triangleDown">' +
212 '</use>' +
213 '</g></svg>');
214
215 thElems[2].click();
216 div = currentTh.children();
217 // clicked on a new element, so the previous icon should have been deleted
218 expect(div.html()).toBeFalsy();
219
220 // the new element should have the ascending icon
221 currentTh = angular.element(thElems[2]);
222 div = currentTh.children();
223 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
224 'width="10" height="10" viewBox="0 0 50 50">' +
225 '<g class="icon tableColSortAsc">' +
226 '<rect width="50" height="50" rx="5"></rect>' +
227 '<use width="50" height="50" class="glyph" ' +
228 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
229 'xlink:href="#triangleUp">' +
230 '</use>' +
231 '</g></svg>');
232 }
233
234 it('should affirm that onos-fixed-header is working', function () {
235 table = angular.element(onosFixedHeaderTags);
236
237 compileTable();
238 verifyGivenTags('onos-fixed-header');
239
240 // table will not be fixed unless it receives the 'LastElement' event
241 scope.$emit('LastElement');
242 scope.$digest();
243
244 verifyCssDisplay();
245 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800246 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800247
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800248 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800249 var thElems;
250 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800251
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800252 compileTable();
253 verifyGivenTags('onos-sortable-header');
254 // ctrlCallback functionality is tested in device-spec
255 // only checking that it has been called correctly in the directive
256 scope.sortCallback = jasmine.createSpy('sortCallback');
257
258 thElems = thead.find('th');
259 verifyCallbacks(thElems);
260 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800261 });
262
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800263});