| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.DimensionPickerTest'); |
| 16 |
goog.setTestOnly('goog.ui.DimensionPickerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.events.KeyCodes'); |
| 21 |
goog.require('goog.math.Size'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.testing.ui.rendererasserts'); |
| 24 |
goog.require('goog.ui.DimensionPicker'); |
| 25 |
goog.require('goog.ui.DimensionPickerRenderer'); |
| 26 |
|
| 27 |
var picker; |
| 28 |
var render; |
| 29 |
var decorate; |
| 30 |
|
| 31 |
function setUpPage() { |
| 32 |
render = goog.dom.getElement('render'); |
| 33 |
decorate = goog.dom.getElement('decorate'); |
| 34 |
} |
| 35 |
|
| 36 |
function setUp() { |
| 37 |
picker = new goog.ui.DimensionPicker(); |
| 38 |
goog.dom.removeChildren(render); |
| 39 |
goog.dom.removeChildren(decorate); |
| 40 |
} |
| 41 |
|
| 42 |
function tearDown() { |
| 43 |
picker.dispose(); |
| 44 |
} |
| 45 |
|
| 46 |
function testConstructor() { |
| 47 |
assertNotNull('Should have successful construction', picker); |
| 48 |
assertNull('Should not be in document', picker.getElement()); |
| 49 |
} |
| 50 |
|
| 51 |
function testRender() { |
| 52 |
picker.render(render); |
| 53 |
|
| 54 |
assertEquals('Should create 1 child', 1, render.childNodes.length); |
| 55 |
assertEquals( |
| 56 |
'Should be a div', String(goog.dom.TagName.DIV), render.firstChild.tagName); |
| 57 |
} |
| 58 |
|
| 59 |
function testDecorate() { |
| 60 |
picker.decorate(decorate); |
| 61 |
|
| 62 |
assertNotEquals( |
| 63 |
'Should add several children', decorate.firstChild, decorate.lastChild); |
| 64 |
} |
| 65 |
|
| 66 |
function testHighlightedSize() { |
| 67 |
picker.render(render); |
| 68 |
|
| 69 |
var size = picker.getValue(); |
| 70 |
assertEquals('Should have 1 column highlighted initially.', 1, size.width); |
| 71 |
assertEquals('Should have 1 row highlighted initially.', 1, size.height); |
| 72 |
|
| 73 |
picker.setValue(1, 2); |
| 74 |
size = picker.getValue(); |
| 75 |
assertEquals('Should have 1 column highlighted.', 1, size.width); |
| 76 |
assertEquals('Should have 2 rows highlighted.', 2, size.height); |
| 77 |
|
| 78 |
picker.setValue(new goog.math.Size(3, 4)); |
| 79 |
size = picker.getValue(); |
| 80 |
assertEquals('Should have 3 columns highlighted.', 3, size.width); |
| 81 |
assertEquals('Should have 4 rows highlighted.', 4, size.height); |
| 82 |
|
| 83 |
picker.setValue(new goog.math.Size(-3, 0)); |
| 84 |
size = picker.getValue(); |
| 85 |
assertEquals( |
| 86 |
'Should have 1 column highlighted when passed a negative ' + |
| 87 |
'column value.', |
| 88 |
1, size.width); |
| 89 |
assertEquals( |
| 90 |
'Should have 1 row highlighted when passed 0 as the row ' + |
| 91 |
'value.', |
| 92 |
1, size.height); |
| 93 |
|
| 94 |
picker.setValue(picker.maxColumns + 10, picker.maxRows + 2); |
| 95 |
size = picker.getValue(); |
| 96 |
assertEquals( |
| 97 |
'Column value should be decreased to match max columns ' + |
| 98 |
'if it is too high.', |
| 99 |
picker.maxColumns, size.width); |
| 100 |
assertEquals( |
| 101 |
'Row value should be decreased to match max rows ' + |
| 102 |
'if it is too high.', |
| 103 |
picker.maxRows, size.height); |
| 104 |
} |
| 105 |
|
| 106 |
function testSizeShown() { |
| 107 |
picker.render(render); |
| 108 |
|
| 109 |
var size = picker.getSize(); |
| 110 |
assertEquals('Should have 5 columns visible', 5, size.width); |
| 111 |
assertEquals('Should have 5 rows visible', 5, size.height); |
| 112 |
|
| 113 |
picker.setValue(4, 4); |
| 114 |
size = picker.getSize(); |
| 115 |
assertEquals('Should have 5 columns visible', 5, size.width); |
| 116 |
assertEquals('Should have 5 rows visible', 5, size.height); |
| 117 |
|
| 118 |
picker.setValue(12, 13); |
| 119 |
size = picker.getSize(); |
| 120 |
assertEquals('Should have 13 columns visible', 13, size.width); |
| 121 |
assertEquals('Should have 14 rows visible', 14, size.height); |
| 122 |
|
| 123 |
picker.setValue(20, 20); |
| 124 |
size = picker.getSize(); |
| 125 |
assertEquals('Should have 20 columns visible', 20, size.width); |
| 126 |
assertEquals('Should have 20 rows visible', 20, size.height); |
| 127 |
|
| 128 |
picker.setValue(2, 3); |
| 129 |
size = picker.getSize(); |
| 130 |
assertEquals('Should have 5 columns visible', 5, size.width); |
| 131 |
assertEquals('Should have 5 rows visible', 5, size.height); |
| 132 |
} |
| 133 |
|
| 134 |
function testHandleMove() { |
| 135 |
picker.render(render); |
| 136 |
var renderer = picker.getRenderer(); |
| 137 |
var mouseMoveElem = renderer.getMouseMoveElement(picker); |
| 138 |
|
| 139 |
picker.rightToLeft_ = false; |
| 140 |
var e = { |
| 141 |
target: mouseMoveElem, |
| 142 |
offsetX: 18, // Each grid square currently a magic 18px. |
| 143 |
offsetY: 36 |
| 144 |
}; |
| 145 |
|
| 146 |
picker.handleMouseMove(e); |
| 147 |
var size = picker.getValue(); |
| 148 |
assertEquals('Should have 1 column highlighted', 1, size.width); |
| 149 |
assertEquals('Should have 2 rows highlighted', 2, size.height); |
| 150 |
|
| 151 |
picker.rightToLeft_ = true; |
| 152 |
|
| 153 |
picker.handleMouseMove(e); |
| 154 |
var size = picker.getValue(); |
| 155 |
// In RTL we pick from the right side of the picker, so an offsetX of 0 |
| 156 |
// would actually mean select all columns. |
| 157 |
assertEquals( |
| 158 |
'Should have columns to the right of the mouse highlighted', |
| 159 |
Math.ceil((mouseMoveElem.offsetWidth - e.offsetX) / 18), size.width); |
| 160 |
assertEquals('Should have 2 rows highlighted', 2, size.height); |
| 161 |
} |
| 162 |
|
| 163 |
function testHandleKeyboardEvents() { |
| 164 |
picker.render(render); |
| 165 |
|
| 166 |
picker.rightToLeft_ = false; |
| 167 |
|
| 168 |
var result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.DOWN}); |
| 169 |
var size = picker.getValue(); |
| 170 |
assertEquals('Should have 1 column highlighted', 1, size.width); |
| 171 |
assertEquals('Should have 2 rows highlighted', 2, size.height); |
| 172 |
assertTrue('Should handle DOWN key event', result); |
| 173 |
|
| 174 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT}); |
| 175 |
size = picker.getValue(); |
| 176 |
assertEquals('Should have 2 column highlighted', 2, size.width); |
| 177 |
assertEquals('Should have 2 rows highlighted', 2, size.height); |
| 178 |
assertTrue('Should handle RIGHT key event', result); |
| 179 |
|
| 180 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.UP}); |
| 181 |
size = picker.getValue(); |
| 182 |
assertEquals('Should have 2 column highlighted', 2, size.width); |
| 183 |
assertEquals('Should have 1 rows highlighted', 1, size.height); |
| 184 |
assertTrue('Should handle UP key event', result); |
| 185 |
|
| 186 |
// Pressing UP when there is only 1 row should be handled but has no |
| 187 |
// effect. |
| 188 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.UP}); |
| 189 |
size = picker.getValue(); |
| 190 |
assertEquals('Should have 2 column highlighted', 2, size.width); |
| 191 |
assertEquals('Should have 1 rows highlighted', 1, size.height); |
| 192 |
assertTrue('Should handle UP key event', result); |
| 193 |
|
| 194 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT}); |
| 195 |
size = picker.getValue(); |
| 196 |
assertEquals('Should have 2 column highlighted', 1, size.width); |
| 197 |
assertEquals('Should have 1 rows highlighted', 1, size.height); |
| 198 |
assertTrue('Should handle LEFT key event', result); |
| 199 |
|
| 200 |
// Pressing LEFT when there is only 1 row should not be handled |
| 201 |
// allowing SubMenu to close. |
| 202 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT}); |
| 203 |
assertFalse( |
| 204 |
'Should not handle LEFT key event when there is only one column', result); |
| 205 |
|
| 206 |
picker.rightToLeft_ = true; |
| 207 |
|
| 208 |
// In RTL the roles of the LEFT and RIGHT keys are swapped. |
| 209 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT}); |
| 210 |
size = picker.getValue(); |
| 211 |
assertEquals('Should have 2 column highlighted', 2, size.width); |
| 212 |
assertEquals('Should have 2 rows highlighted', 1, size.height); |
| 213 |
assertTrue('Should handle LEFT key event', result); |
| 214 |
|
| 215 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT}); |
| 216 |
size = picker.getValue(); |
| 217 |
assertEquals('Should have 2 column highlighted', 1, size.width); |
| 218 |
assertEquals('Should have 1 rows highlighted', 1, size.height); |
| 219 |
assertTrue('Should handle RIGHT key event', result); |
| 220 |
|
| 221 |
result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT}); |
| 222 |
assertFalse( |
| 223 |
'Should not handle RIGHT key event when there is only one column', |
| 224 |
result); |
| 225 |
} |
| 226 |
|
| 227 |
function testDispose() { |
| 228 |
var element = picker.getElement(); |
| 229 |
picker.render(render); |
| 230 |
picker.dispose(); |
| 231 |
assertTrue('Picker should have been disposed of', picker.isDisposed()); |
| 232 |
assertNull( |
| 233 |
'Picker element reference should have been nulled out', |
| 234 |
picker.getElement()); |
| 235 |
} |
| 236 |
|
| 237 |
function testRendererDoesntCallGetCssClassInConstructor() { |
| 238 |
goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor( |
| 239 |
goog.ui.DimensionPickerRenderer); |
| 240 |
} |
| 241 |
|
| 242 |
function testSetAriaLabel() { |
| 243 |
assertNull( |
| 244 |
'Picker must not have aria label by default', picker.getAriaLabel()); |
| 245 |
picker.setAriaLabel('My picker'); |
| 246 |
picker.render(render); |
| 247 |
var element = picker.getElementStrict(); |
| 248 |
assertNotNull('Element must not be null', element); |
| 249 |
assertEquals( |
| 250 |
'Picker element must have expected aria-label', 'My picker', |
| 251 |
element.getAttribute('aria-label')); |
| 252 |
assertTrue(goog.dom.isFocusableTabIndex(element)); |
| 253 |
picker.setAriaLabel('My new picker'); |
| 254 |
assertEquals( |
| 255 |
'Picker element must have updated aria-label', 'My new picker', |
| 256 |
element.getAttribute('aria-label')); |
| 257 |
} |