blob: 107d27d1e1812be7ff70941799575e85aff00b9c [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,
22 fs, is,
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080023 scope, compiled,
24 table, thead, tbody,
Simon Hunt3074fb22015-03-31 15:06:25 -070025 tableIconTdSize = 100,
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080026 bottomMargin = 200,
27 numTestElems = 4;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080028
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080029 var onosFixedHeaderTags = '<table ' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080030 'onos-fixed-header>' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080031 '<thead>' +
32 '<tr>' +
33 '<th></th>' +
34 '<th>Device ID </th>' +
35 '<th>H/W Version </th>' +
36 '<th>S/W Version </th>' +
37 '</tr>' +
38 '</thead>' +
39 '<tbody>' +
40 '<tr>' +
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080041 '<td class="table-icon">' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080042 '<div icon icon-id="{{dev._iconid_available}}">' +
43 '</div>' +
44 '</td>' +
45 '<td>Some ID</td>' +
46 '<td>Some HW</td>' +
47 '<td>Some Software</td>' +
48 '</tr>' +
49 '</tbody>' +
50 '</table>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080051
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080052 onosSortableHeaderTags = '<table ' +
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080053 'onos-sortable-header ' +
54 'sort-callback="sortCallback(urlSuffix)">' +
55 '<thead>' +
56 '<tr>' +
57 '<th colId="available"></th>' +
58 '<th colId="id" sortable>Device ID </th>' +
59 '<th colId="hw" sortable>H/W Version </th>' +
60 '<th colId="sw" sortable>S/W Version </th>' +
61 '</tr>' +
62 '</thead>' +
63 '<tbody>' +
64 '<tr>' +
65 '<td>' +
66 '<div icon icon-id="{{dev._iconid_available}}">' +
67 '</div>' +
68 '</td>' +
69 '<td>Some ID</td>' +
70 '<td>Some HW</td>' +
71 '<td>Some Software</td>' +
72 '</tr>' +
73 '</tbody>' +
74 '</table>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080075
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080076 beforeEach(module('onosWidget', 'onosUtil', 'onosSvg'));
77
Simon Hunt31bb01f2015-03-31 16:50:41 -070078 var mockWindow = {
79 innerWidth: 400,
80 innerHeight: 200,
81 navigator: {
82 userAgent: 'defaultUA'
83 },
84 on: function () {},
85 addEventListener: function () {}
86 };
87
88 beforeEach(function () {
89 module(function ($provide) {
90 $provide.value('$window', mockWindow);
91 });
92 });
93
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080094 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
95 FnService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080096 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080097 $compile = _$compile_;
98 $rootScope = _$rootScope_;
99 fs = FnService;
100 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800101 }));
102
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800103 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800104 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800105 });
106
Simon Huntc4ae8302015-01-26 14:22:48 -0800107 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800108 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800109 thead = null;
110 tbody = null;
Simon Huntc4ae8302015-01-26 14:22:48 -0800111 });
112
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800113 function compileTable() {
114 compiled = $compile(table);
115 compiled(scope);
116 scope.$digest();
117 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800118
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800119 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800120 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800121 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800122
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800123 thead = table.find('thead');
124 expect(thead).toBeDefined();
125 tbody = table.find('tbody');
126 expect(tbody).toBeDefined();
127 }
128
129 function verifyCssDisplay() {
130 var winHeight = fs.windowSize().height;
131
132 expect(thead.css('display')).toBe('block');
133 expect(tbody.css('display')).toBe('block');
134 expect(tbody.css('height')).toBe((winHeight - bottomMargin) + 'px');
135 expect(tbody.css('overflow')).toBe('auto');
136 }
137
138 function verifyColWidth() {
139 var winWidth = fs.windowSize().width,
140 colWidth, thElems, tdElem;
141
142 colWidth = Math.floor(winWidth / numTestElems);
143
144 thElems = thead.find('th');
145
146 angular.forEach(thElems, function (thElem, i) {
147 thElem = angular.element(thElems[i]);
148 tdElem = angular.element(tbody.find('td').eq(i));
149
150 if (tdElem.attr('class') === 'table-icon') {
151 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
152 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
153 } else {
154 expect(thElem.css('width')).toBe(colWidth + 'px');
155 expect(tdElem.css('width')).toBe(colWidth + 'px');
156 }
157 });
158 }
159
160 function verifyCallbacks(thElems) {
161 expect(scope.sortCallback).not.toHaveBeenCalled();
162
163 // first test header has no 'sortable' attr
164 thElems[0].click();
165 expect(scope.sortCallback).not.toHaveBeenCalled();
166
167 // the other headers have 'sortable'
168 for(var i = 1; i < numTestElems; i += 1) {
169 thElems[i].click();
170 expect(scope.sortCallback).toHaveBeenCalled();
171 }
172 }
173
174 function verifyIcons(thElems) {
175 var currentTh, div;
176 // make sure it has the correct icon after clicking
177 thElems[1].click();
178 currentTh = angular.element(thElems[1]);
179 div = currentTh.find('div');
180 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
181 'width="10" height="10" viewBox="0 0 50 50">' +
182 '<g class="icon tableColSortAsc">' +
183 '<rect width="50" height="50" rx="5"></rect>' +
184 '<use width="50" height="50" class="glyph" ' +
185 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
186 'xlink:href="#triangleUp">' +
187 '</use>' +
188 '</g></svg>');
189 thElems[1].click();
190 div = currentTh.find('div');
191 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
192 'width="10" height="10" viewBox="0 0 50 50">' +
193 '<g class="icon tableColSortDesc">' +
194 '<rect width="50" height="50" rx="5"></rect>' +
195 '<use width="50" height="50" class="glyph" ' +
196 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
197 'xlink:href="#triangleDown">' +
198 '</use>' +
199 '</g></svg>');
200
201 thElems[2].click();
202 div = currentTh.children();
203 // clicked on a new element, so the previous icon should have been deleted
204 expect(div.html()).toBeFalsy();
205
206 // the new element should have the ascending icon
207 currentTh = angular.element(thElems[2]);
208 div = currentTh.children();
209 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
210 'width="10" height="10" viewBox="0 0 50 50">' +
211 '<g class="icon tableColSortAsc">' +
212 '<rect width="50" height="50" rx="5"></rect>' +
213 '<use width="50" height="50" class="glyph" ' +
214 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
215 'xlink:href="#triangleUp">' +
216 '</use>' +
217 '</g></svg>');
218 }
219
220 it('should affirm that onos-fixed-header is working', function () {
221 table = angular.element(onosFixedHeaderTags);
222
223 compileTable();
224 verifyGivenTags('onos-fixed-header');
225
226 // table will not be fixed unless it receives the 'LastElement' event
227 scope.$emit('LastElement');
228 scope.$digest();
229
230 verifyCssDisplay();
231 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800232 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800233
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800234 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800235 var thElems;
236 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800237
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800238 compileTable();
239 verifyGivenTags('onos-sortable-header');
240 // ctrlCallback functionality is tested in device-spec
241 // only checking that it has been called correctly in the directive
242 scope.sortCallback = jasmine.createSpy('sortCallback');
243
244 thElems = thead.find('th');
245 verifyCallbacks(thElems);
246 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800247 });
248
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800249});