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, |
| 25 | mockh2Height = '10px', |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 26 | tableIconTdSize = 100, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 27 | pdgTop = 101, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 28 | numTestElems = 4; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 29 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 30 | var onosFixedHeaderTags = '<table ' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 31 | 'onos-fixed-header>' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 32 | '<thead>' + |
| 33 | '<tr>' + |
| 34 | '<th></th>' + |
| 35 | '<th>Device ID </th>' + |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 36 | '<th col-width="100px">H/W Version </th>' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 37 | '<th>S/W Version </th>' + |
| 38 | '</tr>' + |
| 39 | '</thead>' + |
| 40 | '<tbody>' + |
| 41 | '<tr>' + |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 42 | '<td colspan="4">' + |
| 43 | 'No Devices found' + |
| 44 | '</td>' + |
| 45 | '</tr>' + |
| 46 | '<tr>' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 47 | '<td class="table-icon">' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 48 | '<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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 57 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 58 | onosSortableHeaderTags = '<table ' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 59 | 'onos-sortable-header ' + |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 60 | 'sort-callback="sortCallback(requestParams)">' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 61 | '<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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 81 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 82 | beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg')); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 83 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 84 | 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 Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 100 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 101 | FnService, MastService, IconService) { |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 102 | $log = _$log_; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 103 | $compile = _$compile_; |
| 104 | $rootScope = _$rootScope_; |
| 105 | fs = FnService; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 106 | mast = MastService; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 107 | is = IconService; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 108 | })); |
| 109 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 110 | beforeEach(function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 111 | scope = $rootScope.$new(); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 112 | }); |
| 113 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 114 | beforeEach(function () { |
| 115 | mockHeader = d3.select('body') |
| 116 | .append('h2') |
| 117 | .style('height', mockh2Height) |
| 118 | .html('Some Header'); |
| 119 | }); |
| 120 | |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 121 | afterEach(function () { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 122 | table = null; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 123 | thead = null; |
| 124 | tbody = null; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 125 | mockHeader.remove(); |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 126 | }); |
| 127 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 128 | function compileTable() { |
| 129 | compiled = $compile(table); |
| 130 | compiled(scope); |
| 131 | scope.$digest(); |
| 132 | } |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 133 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 134 | function verifyGivenTags(dirName) { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 135 | expect(table).toBeDefined(); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 136 | expect(table.attr(dirName)).toBe(''); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 137 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 138 | thead = table.find('thead'); |
| 139 | expect(thead).toBeDefined(); |
| 140 | tbody = table.find('tbody'); |
| 141 | expect(tbody).toBeDefined(); |
| 142 | } |
| 143 | |
| 144 | function verifyCssDisplay() { |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 145 | var tableHeight = fs.windowSize(pdgTop).height; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 146 | |
| 147 | expect(thead.css('display')).toBe('block'); |
| 148 | expect(tbody.css('display')).toBe('block'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 149 | expect(tbody.css('height')).toBe(tableHeight + 'px'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 150 | expect(tbody.css('overflow')).toBe('auto'); |
| 151 | } |
| 152 | |
| 153 | function verifyColWidth() { |
| 154 | var winWidth = fs.windowSize().width, |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 155 | colWidth, thElems, tr, tdElem; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 156 | |
| 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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 163 | tr = angular.element(tbody.find('tr').eq(1)); |
| 164 | tdElem = angular.element(tr.find('td').eq(i)); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 165 | var custWidth = thElem.attr('col-width'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 166 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 167 | 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 Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 171 | 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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 202 | '<g class="icon upArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 203 | '<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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 213 | '<g class="icon downArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 214 | '<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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 231 | '<g class="icon upArrow">' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 232 | '<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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 252 | }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 253 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 254 | it('should affirm that onos-sortable-header is working', function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 255 | var thElems; |
| 256 | table = angular.element(onosSortableHeaderTags); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 257 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 258 | 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 Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 267 | }); |
| 268 | |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 269 | }); |