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, |
| 22 | fs, is, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 23 | scope, compiled, |
| 24 | table, thead, tbody, |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 25 | tableIconTdSize = 100, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 26 | bottomMargin = 200, |
| 27 | numTestElems = 4; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 28 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 29 | var onosFixedHeaderTags = '<table ' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 30 | 'onos-fixed-header>' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 31 | '<thead>' + |
| 32 | '<tr>' + |
| 33 | '<th></th>' + |
| 34 | '<th>Device ID </th>' + |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 35 | '<th col-width="100px">H/W Version </th>' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 36 | '<th>S/W Version </th>' + |
| 37 | '</tr>' + |
| 38 | '</thead>' + |
| 39 | '<tbody>' + |
| 40 | '<tr>' + |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 41 | '<td class="table-icon">' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 42 | '<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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 51 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 52 | onosSortableHeaderTags = '<table ' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 53 | 'onos-sortable-header ' + |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 54 | 'sort-callback="sortCallback(requestParams)">' + |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 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 Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 75 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 76 | beforeEach(module('onosWidget', 'onosUtil', 'onosSvg')); |
| 77 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 78 | 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 Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 94 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, |
| 95 | FnService, IconService) { |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 96 | $log = _$log_; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 97 | $compile = _$compile_; |
| 98 | $rootScope = _$rootScope_; |
| 99 | fs = FnService; |
| 100 | is = IconService; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 101 | })); |
| 102 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 103 | beforeEach(function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 104 | scope = $rootScope.$new(); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 105 | }); |
| 106 | |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 107 | afterEach(function () { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 108 | table = null; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 109 | thead = null; |
| 110 | tbody = null; |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 111 | }); |
| 112 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 113 | function compileTable() { |
| 114 | compiled = $compile(table); |
| 115 | compiled(scope); |
| 116 | scope.$digest(); |
| 117 | } |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 118 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 119 | function verifyGivenTags(dirName) { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 120 | expect(table).toBeDefined(); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 121 | expect(table.attr(dirName)).toBe(''); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 122 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 123 | 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)); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 149 | var custWidth = thElem.attr('col-width'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 150 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 151 | if (custWidth) { |
| 152 | expect(thElem.css('width')).toBe(custWidth); |
| 153 | expect(tdElem.css('width')).toBe(custWidth); |
| 154 | } else if (tdElem.attr('class') === 'table-icon') { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 155 | expect(thElem.css('width')).toBe(tableIconTdSize + 'px'); |
| 156 | expect(tdElem.css('width')).toBe(tableIconTdSize + 'px'); |
| 157 | } else { |
| 158 | expect(thElem.css('width')).toBe(colWidth + 'px'); |
| 159 | expect(tdElem.css('width')).toBe(colWidth + 'px'); |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | function verifyCallbacks(thElems) { |
| 165 | expect(scope.sortCallback).not.toHaveBeenCalled(); |
| 166 | |
| 167 | // first test header has no 'sortable' attr |
| 168 | thElems[0].click(); |
| 169 | expect(scope.sortCallback).not.toHaveBeenCalled(); |
| 170 | |
| 171 | // the other headers have 'sortable' |
| 172 | for(var i = 1; i < numTestElems; i += 1) { |
| 173 | thElems[i].click(); |
| 174 | expect(scope.sortCallback).toHaveBeenCalled(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | function verifyIcons(thElems) { |
| 179 | var currentTh, div; |
| 180 | // make sure it has the correct icon after clicking |
| 181 | thElems[1].click(); |
| 182 | currentTh = angular.element(thElems[1]); |
| 183 | div = currentTh.find('div'); |
| 184 | expect(div.html()).toBe('<svg class="embeddedIcon" ' + |
| 185 | 'width="10" height="10" viewBox="0 0 50 50">' + |
| 186 | '<g class="icon tableColSortAsc">' + |
| 187 | '<rect width="50" height="50" rx="5"></rect>' + |
| 188 | '<use width="50" height="50" class="glyph" ' + |
| 189 | 'xmlns:xlink="http://www.w3.org/1999/xlink" ' + |
| 190 | 'xlink:href="#triangleUp">' + |
| 191 | '</use>' + |
| 192 | '</g></svg>'); |
| 193 | thElems[1].click(); |
| 194 | div = currentTh.find('div'); |
| 195 | expect(div.html()).toBe('<svg class="embeddedIcon" ' + |
| 196 | 'width="10" height="10" viewBox="0 0 50 50">' + |
| 197 | '<g class="icon tableColSortDesc">' + |
| 198 | '<rect width="50" height="50" rx="5"></rect>' + |
| 199 | '<use width="50" height="50" class="glyph" ' + |
| 200 | 'xmlns:xlink="http://www.w3.org/1999/xlink" ' + |
| 201 | 'xlink:href="#triangleDown">' + |
| 202 | '</use>' + |
| 203 | '</g></svg>'); |
| 204 | |
| 205 | thElems[2].click(); |
| 206 | div = currentTh.children(); |
| 207 | // clicked on a new element, so the previous icon should have been deleted |
| 208 | expect(div.html()).toBeFalsy(); |
| 209 | |
| 210 | // the new element should have the ascending icon |
| 211 | currentTh = angular.element(thElems[2]); |
| 212 | div = currentTh.children(); |
| 213 | expect(div.html()).toBe('<svg class="embeddedIcon" ' + |
| 214 | 'width="10" height="10" viewBox="0 0 50 50">' + |
| 215 | '<g class="icon tableColSortAsc">' + |
| 216 | '<rect width="50" height="50" rx="5"></rect>' + |
| 217 | '<use width="50" height="50" class="glyph" ' + |
| 218 | 'xmlns:xlink="http://www.w3.org/1999/xlink" ' + |
| 219 | 'xlink:href="#triangleUp">' + |
| 220 | '</use>' + |
| 221 | '</g></svg>'); |
| 222 | } |
| 223 | |
| 224 | it('should affirm that onos-fixed-header is working', function () { |
| 225 | table = angular.element(onosFixedHeaderTags); |
| 226 | |
| 227 | compileTable(); |
| 228 | verifyGivenTags('onos-fixed-header'); |
| 229 | |
| 230 | // table will not be fixed unless it receives the 'LastElement' event |
| 231 | scope.$emit('LastElement'); |
| 232 | scope.$digest(); |
| 233 | |
| 234 | verifyCssDisplay(); |
| 235 | verifyColWidth(); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 236 | }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 237 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 238 | it('should affirm that onos-sortable-header is working', function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 239 | var thElems; |
| 240 | table = angular.element(onosSortableHeaderTags); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 241 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 242 | compileTable(); |
| 243 | verifyGivenTags('onos-sortable-header'); |
| 244 | // ctrlCallback functionality is tested in device-spec |
| 245 | // only checking that it has been called correctly in the directive |
| 246 | scope.sortCallback = jasmine.createSpy('sortCallback'); |
| 247 | |
| 248 | thElems = thead.find('th'); |
| 249 | verifyCallbacks(thElems); |
| 250 | verifyIcons(thElems); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 251 | }); |
| 252 | |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 253 | }); |