Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 20 | describe('factory: fw/widget/table.js', function () { |
| 21 | var $log, $compile, $rootScope, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 22 | fs, mast, is, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 23 | scope, compiled, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 24 | table, thead, tbody, mockHeader, |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 25 | mockH2Height = 20, |
| 26 | mockMastHeight = 20, |
| 27 | mockHeaderHeight = mockH2Height + mockMastHeight, |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 28 | tableIconTdSize = 100, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 29 | numTestElems = 4; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 30 | |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 31 | 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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 58 | |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 59 | 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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 82 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 83 | beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg')); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 84 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 85 | 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 Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 101 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 102 | FnService, MastService, IconService) { |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 103 | $log = _$log_; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 104 | $compile = _$compile_; |
| 105 | $rootScope = _$rootScope_; |
| 106 | fs = FnService; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 107 | mast = MastService; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 108 | is = IconService; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 109 | })); |
| 110 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 111 | beforeEach(function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 112 | scope = $rootScope.$new(); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 113 | }); |
| 114 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 115 | beforeEach(function () { |
| 116 | mockHeader = d3.select('body') |
| 117 | .append('h2') |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 118 | .classed('tabular-header', true) |
| 119 | .style('height', mockHeaderHeight + 'px') |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 120 | .html('Some Header'); |
| 121 | }); |
| 122 | |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 123 | afterEach(function () { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 124 | table = null; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 125 | thead = null; |
| 126 | tbody = null; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 127 | mockHeader.remove(); |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 128 | }); |
| 129 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 130 | function compileTable() { |
| 131 | compiled = $compile(table); |
| 132 | compiled(scope); |
| 133 | scope.$digest(); |
| 134 | } |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 135 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 136 | function verifyGivenTags(dirName) { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 137 | expect(table).toBeDefined(); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 138 | expect(table.attr(dirName)).toBe(''); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 139 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 140 | thead = table.find('thead'); |
| 141 | expect(thead).toBeDefined(); |
| 142 | tbody = table.find('tbody'); |
| 143 | expect(tbody).toBeDefined(); |
| 144 | } |
| 145 | |
| 146 | function verifyCssDisplay() { |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 147 | 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 Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 150 | |
| 151 | expect(thead.css('display')).toBe('block'); |
| 152 | expect(tbody.css('display')).toBe('block'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 153 | expect(tbody.css('height')).toBe(tableHeight + 'px'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 154 | expect(tbody.css('overflow')).toBe('auto'); |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame^] | 155 | |
| 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 Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | function verifyColWidth() { |
| 161 | var winWidth = fs.windowSize().width, |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 162 | colWidth, thElems, tr, tdElem; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 163 | |
| 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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 170 | tr = angular.element(tbody.find('tr').eq(1)); |
| 171 | tdElem = angular.element(tr.find('td').eq(i)); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 172 | var custWidth = thElem.attr('col-width'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 173 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 174 | 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 Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 178 | 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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 209 | '<g class="icon upArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 210 | '<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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 220 | '<g class="icon downArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 221 | '<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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 238 | '<g class="icon upArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 239 | '<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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 259 | }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 260 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 261 | it('should affirm that onos-sortable-header is working', function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 262 | var thElems; |
| 263 | table = angular.element(onosSortableHeaderTags); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 264 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 265 | 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 Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 274 | }); |
| 275 | |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 276 | }); |