blob: c0a317e0fba4ed943fd5d9305a96f208a82e86c5 [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,
25 tableIconTdSize = 33,
26 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
78 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
79 FnService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080080 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080081 $compile = _$compile_;
82 $rootScope = _$rootScope_;
83 fs = FnService;
84 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080085 }));
86
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080087 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080088 scope = $rootScope.$new();
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080089 });
90
Simon Huntc4ae8302015-01-26 14:22:48 -080091 afterEach(function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080092 table = null;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080093 thead = null;
94 tbody = null;
Simon Huntc4ae8302015-01-26 14:22:48 -080095 });
96
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080097 function compileTable() {
98 compiled = $compile(table);
99 compiled(scope);
100 scope.$digest();
101 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800102
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800103 function verifyGivenTags(dirName) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800104 expect(table).toBeDefined();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800105 expect(table.attr(dirName)).toBe('');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800106
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800107 thead = table.find('thead');
108 expect(thead).toBeDefined();
109 tbody = table.find('tbody');
110 expect(tbody).toBeDefined();
111 }
112
113 function verifyCssDisplay() {
114 var winHeight = fs.windowSize().height;
115
116 expect(thead.css('display')).toBe('block');
117 expect(tbody.css('display')).toBe('block');
118 expect(tbody.css('height')).toBe((winHeight - bottomMargin) + 'px');
119 expect(tbody.css('overflow')).toBe('auto');
120 }
121
122 function verifyColWidth() {
123 var winWidth = fs.windowSize().width,
124 colWidth, thElems, tdElem;
125
126 colWidth = Math.floor(winWidth / numTestElems);
127
128 thElems = thead.find('th');
129
130 angular.forEach(thElems, function (thElem, i) {
131 thElem = angular.element(thElems[i]);
132 tdElem = angular.element(tbody.find('td').eq(i));
133
134 if (tdElem.attr('class') === 'table-icon') {
135 expect(thElem.css('width')).toBe(tableIconTdSize + 'px');
136 expect(tdElem.css('width')).toBe(tableIconTdSize + 'px');
137 } else {
138 expect(thElem.css('width')).toBe(colWidth + 'px');
139 expect(tdElem.css('width')).toBe(colWidth + 'px');
140 }
141 });
142 }
143
144 function verifyCallbacks(thElems) {
145 expect(scope.sortCallback).not.toHaveBeenCalled();
146
147 // first test header has no 'sortable' attr
148 thElems[0].click();
149 expect(scope.sortCallback).not.toHaveBeenCalled();
150
151 // the other headers have 'sortable'
152 for(var i = 1; i < numTestElems; i += 1) {
153 thElems[i].click();
154 expect(scope.sortCallback).toHaveBeenCalled();
155 }
156 }
157
158 function verifyIcons(thElems) {
159 var currentTh, div;
160 // make sure it has the correct icon after clicking
161 thElems[1].click();
162 currentTh = angular.element(thElems[1]);
163 div = currentTh.find('div');
164 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
165 'width="10" height="10" viewBox="0 0 50 50">' +
166 '<g class="icon tableColSortAsc">' +
167 '<rect width="50" height="50" rx="5"></rect>' +
168 '<use width="50" height="50" class="glyph" ' +
169 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
170 'xlink:href="#triangleUp">' +
171 '</use>' +
172 '</g></svg>');
173 thElems[1].click();
174 div = currentTh.find('div');
175 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
176 'width="10" height="10" viewBox="0 0 50 50">' +
177 '<g class="icon tableColSortDesc">' +
178 '<rect width="50" height="50" rx="5"></rect>' +
179 '<use width="50" height="50" class="glyph" ' +
180 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
181 'xlink:href="#triangleDown">' +
182 '</use>' +
183 '</g></svg>');
184
185 thElems[2].click();
186 div = currentTh.children();
187 // clicked on a new element, so the previous icon should have been deleted
188 expect(div.html()).toBeFalsy();
189
190 // the new element should have the ascending icon
191 currentTh = angular.element(thElems[2]);
192 div = currentTh.children();
193 expect(div.html()).toBe('<svg class="embeddedIcon" ' +
194 'width="10" height="10" viewBox="0 0 50 50">' +
195 '<g class="icon tableColSortAsc">' +
196 '<rect width="50" height="50" rx="5"></rect>' +
197 '<use width="50" height="50" class="glyph" ' +
198 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
199 'xlink:href="#triangleUp">' +
200 '</use>' +
201 '</g></svg>');
202 }
203
204 it('should affirm that onos-fixed-header is working', function () {
205 table = angular.element(onosFixedHeaderTags);
206
207 compileTable();
208 verifyGivenTags('onos-fixed-header');
209
210 // table will not be fixed unless it receives the 'LastElement' event
211 scope.$emit('LastElement');
212 scope.$digest();
213
214 verifyCssDisplay();
215 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800216 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800217
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800218 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800219 var thElems;
220 table = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800221
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800222 compileTable();
223 verifyGivenTags('onos-sortable-header');
224 // ctrlCallback functionality is tested in device-spec
225 // only checking that it has been called correctly in the directive
226 scope.sortCallback = jasmine.createSpy('sortCallback');
227
228 thElems = thead.find('th');
229 verifyCallbacks(thElems);
230 verifyIcons(thElems);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800231 });
232
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800233});