| 1 |
// Copyright 2006 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.dom.formsTest'); |
| 16 |
goog.setTestOnly('goog.dom.formsTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.forms'); |
| 20 |
goog.require('goog.testing.PropertyReplacer'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
|
| 24 |
var stubs = new goog.testing.PropertyReplacer(); |
| 25 |
|
| 26 |
function tearDown() { |
| 27 |
stubs.reset(); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Sets up a mocked version of goog.window.openBlank. |
| 33 |
* @param {!Object} mockForm A mocked form Object to return on |
| 34 |
* createElement('form'). |
| 35 |
*/ |
| 36 |
function mockWindowOpen(mockForm) { |
| 37 |
var windowOpen = function() { |
| 38 |
return { |
| 39 |
document: { |
| 40 |
createElement: function(name) { |
| 41 |
if (name == 'form') { |
| 42 |
return mockForm; |
| 43 |
} |
| 44 |
return {}; |
| 45 |
} |
| 46 |
} |
| 47 |
}; |
| 48 |
}; |
| 49 |
stubs.setPath('goog.window.openBlank', windowOpen); |
| 50 |
} |
| 51 |
|
| 52 |
function testSubmitFormInNewWindowWithSubmitButton() { |
| 53 |
var expectedForm = [ |
| 54 |
{name: 'in1', value: 'foo', type: 'hidden'}, |
| 55 |
{name: 'in2', value: 'bar', type: 'hidden'}, |
| 56 |
{name: 'in2', value: 'baaz', type: 'hidden'}, |
| 57 |
{name: 'in3', value: '', type: 'hidden'}, |
| 58 |
{name: 'pass', value: 'bar', type: 'hidden'}, |
| 59 |
{name: 'textarea', value: 'foo bar baz', type: 'hidden'}, |
| 60 |
{name: 'select1', value: '1', type: 'hidden'}, |
| 61 |
{name: 'select2', value: 'a', type: 'hidden'}, |
| 62 |
{name: 'select2', value: 'c', type: 'hidden'}, |
| 63 |
{name: 'select3', value: '', type: 'hidden'}, |
| 64 |
{name: 'checkbox1', value: 'on', type: 'hidden'}, |
| 65 |
{name: 'radio', value: 'X', type: 'hidden'}, |
| 66 |
{name: 'radio2', value: 'Y', type: 'hidden'}, |
| 67 |
{name: 'submit', value: 'submitb', type: 'hidden'} |
| 68 |
]; |
| 69 |
|
| 70 |
var formElements = []; |
| 71 |
var mockForm = {}; |
| 72 |
|
| 73 |
var appendChild = HTMLFormElement.prototype.appendChild; |
| 74 |
var submit = HTMLFormElement.prototype.submit; |
| 75 |
|
| 76 |
HTMLFormElement.prototype.appendChild = function(child) { |
| 77 |
formElements.push(child); |
| 78 |
}; |
| 79 |
HTMLFormElement.prototype.submit = function() { |
| 80 |
assertArrayEquals(expectedForm, formElements); |
| 81 |
assertEquals('https://foo.xyz/baz', mockForm.action); |
| 82 |
assertEquals('get', mockForm.method); |
| 83 |
}; |
| 84 |
mockWindowOpen(mockForm); |
| 85 |
|
| 86 |
var formEl = goog.dom.getElement('testform1'); |
| 87 |
var submitEl = goog.dom.getElement('submitb'); |
| 88 |
var result = goog.dom.forms.submitFormInNewWindow(formEl, submitEl); |
| 89 |
assertTrue(result); |
| 90 |
HTMLFormElement.prototype.appendChild = appendChild; |
| 91 |
HTMLFormElement.prototype.submit = submit; |
| 92 |
} |
| 93 |
|
| 94 |
function testSubmitFormInNewWindowWithSubmitInput() { |
| 95 |
var expectedForm = [ |
| 96 |
{name: 'in1', value: 'foo', type: 'hidden'}, |
| 97 |
{name: 'in2', value: 'bar', type: 'hidden'}, |
| 98 |
{name: 'in2', value: 'baaz', type: 'hidden'}, |
| 99 |
{name: 'in3', value: '', type: 'hidden'}, |
| 100 |
{name: 'pass', value: 'bar', type: 'hidden'}, |
| 101 |
{name: 'textarea', value: 'foo bar baz', type: 'hidden'}, |
| 102 |
{name: 'select1', value: '1', type: 'hidden'}, |
| 103 |
{name: 'select2', value: 'a', type: 'hidden'}, |
| 104 |
{name: 'select2', value: 'c', type: 'hidden'}, |
| 105 |
{name: 'select3', value: '', type: 'hidden'}, |
| 106 |
{name: 'checkbox1', value: 'on', type: 'hidden'}, |
| 107 |
{name: 'radio', value: 'X', type: 'hidden'}, |
| 108 |
{name: 'radio2', value: 'Y', type: 'hidden'}, |
| 109 |
{name: 'submit', value: 'submitv', type: 'hidden'} |
| 110 |
]; |
| 111 |
|
| 112 |
var formElements = []; |
| 113 |
var mockForm = {}; |
| 114 |
|
| 115 |
var appendChild = HTMLFormElement.prototype.appendChild; |
| 116 |
var submit = HTMLFormElement.prototype.submit; |
| 117 |
HTMLFormElement.prototype.appendChild = function(child) { |
| 118 |
formElements.push(child); |
| 119 |
}; |
| 120 |
HTMLFormElement.prototype.submit = function() { |
| 121 |
assertArrayEquals(expectedForm, formElements); |
| 122 |
assertEquals('https://foo.xyz/baz', mockForm.action); |
| 123 |
assertEquals('get', mockForm.method); |
| 124 |
}; |
| 125 |
mockWindowOpen(mockForm); |
| 126 |
|
| 127 |
|
| 128 |
var formEl = goog.dom.getElement('testform1'); |
| 129 |
var submitEl = goog.dom.getElement('submit'); |
| 130 |
var result = goog.dom.forms.submitFormInNewWindow(formEl, submitEl); |
| 131 |
assertTrue(result); |
| 132 |
HTMLFormElement.prototype.appendChild = appendChild; |
| 133 |
HTMLFormElement.prototype.submit = submit; |
| 134 |
} |
| 135 |
|
| 136 |
function testSubmitFormInNewWindowWithoutSubmitButton() { |
| 137 |
var expectedForm = [ |
| 138 |
{name: 'in1', value: 'foo', type: 'hidden'}, |
| 139 |
{name: 'in2', value: 'bar', type: 'hidden'}, |
| 140 |
{name: 'in2', value: 'baaz', type: 'hidden'}, |
| 141 |
{name: 'in3', value: '', type: 'hidden'}, |
| 142 |
{name: 'pass', value: 'bar', type: 'hidden'}, |
| 143 |
{name: 'textarea', value: 'foo bar baz', type: 'hidden'}, |
| 144 |
{name: 'select1', value: '1', type: 'hidden'}, |
| 145 |
{name: 'select2', value: 'a', type: 'hidden'}, |
| 146 |
{name: 'select2', value: 'c', type: 'hidden'}, |
| 147 |
{name: 'select3', value: '', type: 'hidden'}, |
| 148 |
{name: 'checkbox1', value: 'on', type: 'hidden'}, |
| 149 |
{name: 'radio', value: 'X', type: 'hidden'}, |
| 150 |
{name: 'radio2', value: 'Y', type: 'hidden'} |
| 151 |
]; |
| 152 |
|
| 153 |
var formElements = []; |
| 154 |
var mockForm = {}; |
| 155 |
|
| 156 |
var appendChild = HTMLFormElement.prototype.appendChild; |
| 157 |
var submit = HTMLFormElement.prototype.submit; |
| 158 |
|
| 159 |
HTMLFormElement.prototype.appendChild = function(child) { |
| 160 |
formElements.push(child); |
| 161 |
}; |
| 162 |
HTMLFormElement.prototype.submit = function() { |
| 163 |
assertArrayEquals(expectedForm, formElements); |
| 164 |
assertEquals('https://foo.bar/baz', mockForm.action); |
| 165 |
assertEquals('get', mockForm.method); |
| 166 |
}; |
| 167 |
mockWindowOpen(mockForm); |
| 168 |
|
| 169 |
var formEl = goog.dom.getElement('testform1'); |
| 170 |
var result = goog.dom.forms.submitFormInNewWindow(formEl); |
| 171 |
assertTrue(result); |
| 172 |
HTMLFormElement.prototype.appendChild = appendChild; |
| 173 |
HTMLFormElement.prototype.submit = submit; |
| 174 |
} |
| 175 |
|
| 176 |
function testSubmitFormInNewWindowError() { |
| 177 |
var formEl = goog.dom.getElement('testform1'); |
| 178 |
var resetEl = goog.dom.getElement('reset'); |
| 179 |
|
| 180 |
assertThrows( |
| 181 |
'Non-submit type elements cannot be used to submit form.', |
| 182 |
function() { goog.dom.forms.submitFormInNewWindow(formEl, resetEl); }); |
| 183 |
} |
| 184 |
|
| 185 |
function testSubmitFormDataInNewWindow() { |
| 186 |
var expectedForm = [ |
| 187 |
{name: 'in1', value: 'foo', type: 'hidden'}, |
| 188 |
{name: 'in2', value: 'bar', type: 'hidden'}, |
| 189 |
{name: 'in2', value: 'baaz', type: 'hidden'}, |
| 190 |
{name: 'in3', value: '', type: 'hidden'}, |
| 191 |
{name: 'pass', value: 'bar', type: 'hidden'}, |
| 192 |
{name: 'textarea', value: 'foo bar baz', type: 'hidden'}, |
| 193 |
{name: 'select1', value: '1', type: 'hidden'}, |
| 194 |
{name: 'select2', value: 'a', type: 'hidden'}, |
| 195 |
{name: 'select2', value: 'c', type: 'hidden'}, |
| 196 |
{name: 'select3', value: '', type: 'hidden'}, |
| 197 |
{name: 'checkbox1', value: 'on', type: 'hidden'}, |
| 198 |
{name: 'radio', value: 'X', type: 'hidden'}, |
| 199 |
{name: 'radio2', value: 'Y', type: 'hidden'} |
| 200 |
]; |
| 201 |
|
| 202 |
var formElements = []; |
| 203 |
var mockForm = {}; |
| 204 |
|
| 205 |
var appendChild = HTMLFormElement.prototype.appendChild; |
| 206 |
var submit = HTMLFormElement.prototype.submit; |
| 207 |
HTMLFormElement.prototype.appendChild = function(child) { |
| 208 |
formElements.push(child); |
| 209 |
}; |
| 210 |
HTMLFormElement.prototype.submit = function() { |
| 211 |
assertArrayEquals(expectedForm, formElements); |
| 212 |
assertEquals('https://foo.bar/baz', mockForm.action); |
| 213 |
assertEquals('get', mockForm.method); |
| 214 |
}; |
| 215 |
mockWindowOpen(mockForm); |
| 216 |
|
| 217 |
var formEl = goog.dom.getElement('testform1'); |
| 218 |
var formData = goog.dom.forms.getFormDataMap(formEl); |
| 219 |
var result = goog.dom.forms.submitFormDataInNewWindow( |
| 220 |
formEl.action, formEl.method, formData); |
| 221 |
assertTrue(result); |
| 222 |
HTMLFormElement.prototype.appendChild = appendChild; |
| 223 |
HTMLFormElement.prototype.submit = submit; |
| 224 |
} |
| 225 |
|
| 226 |
function testGetFormDataString() { |
| 227 |
var el = goog.dom.getElement('testform1'); |
| 228 |
var result = goog.dom.forms.getFormDataString(el); |
| 229 |
assertEquals( |
| 230 |
'in1=foo&in2=bar&in2=baaz&in3=&pass=bar&textarea=foo%20bar%20baz&' + |
| 231 |
'select1=1&select2=a&select2=c&select3=&checkbox1=on&radio=X&radio2=Y', |
| 232 |
result); |
| 233 |
} |
| 234 |
|
| 235 |
function testGetFormDataMap() { |
| 236 |
var el = goog.dom.getElement('testform1'); |
| 237 |
var result = goog.dom.forms.getFormDataMap(el); |
| 238 |
|
| 239 |
assertArrayEquals(['foo'], result.get('in1')); |
| 240 |
assertArrayEquals(['bar', 'baaz'], result.get('in2')); |
| 241 |
assertArrayEquals(['1'], result.get('select1')); |
| 242 |
assertArrayEquals(['a', 'c'], result.get('select2')); |
| 243 |
assertArrayEquals(['on'], result.get('checkbox1')); |
| 244 |
assertUndefined(result.get('select6')); |
| 245 |
assertUndefined(result.get('checkbox2')); |
| 246 |
assertArrayEquals(['X'], result.get('radio')); |
| 247 |
assertArrayEquals(['Y'], result.get('radio2')); |
| 248 |
} |
| 249 |
|
| 250 |
function testHasFileInput() { |
| 251 |
var el = goog.dom.getElement('testform1'); |
| 252 |
assertFalse(goog.dom.forms.hasFileInput(el)); |
| 253 |
el = goog.dom.getElement('testform2'); |
| 254 |
assertTrue(goog.dom.forms.hasFileInput(el)); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
function testGetValueOnAtypicalValueElements() { |
| 259 |
var el = goog.dom.getElement('testdiv1'); |
| 260 |
var result = goog.dom.forms.getValue(el); |
| 261 |
assertNull(result); |
| 262 |
var el = goog.dom.getElement('testfieldset1'); |
| 263 |
var result = goog.dom.forms.getValue(el); |
| 264 |
assertNull(result); |
| 265 |
var el = goog.dom.getElement('testlegend1'); |
| 266 |
var result = goog.dom.forms.getValue(el); |
| 267 |
assertNull(result); |
| 268 |
} |
| 269 |
|
| 270 |
function testHasValueInput() { |
| 271 |
var el = goog.dom.getElement('in1'); |
| 272 |
var result = goog.dom.forms.hasValue(el); |
| 273 |
assertTrue(result); |
| 274 |
} |
| 275 |
|
| 276 |
function testGetValueByNameForNonExistentElement() { |
| 277 |
var form = goog.dom.getElement('testform1'); |
| 278 |
var result = goog.dom.forms.getValueByName(form, 'non_existent'); |
| 279 |
assertNull(result); |
| 280 |
} |
| 281 |
|
| 282 |
function testHasValueByNameInput() { |
| 283 |
var form = goog.dom.getElement('testform1'); |
| 284 |
var result = goog.dom.forms.hasValueByName(form, 'in1'); |
| 285 |
assertTrue(result); |
| 286 |
} |
| 287 |
|
| 288 |
function testHasValueInputEmpty() { |
| 289 |
var el = goog.dom.getElement('in3'); |
| 290 |
var result = goog.dom.forms.hasValue(el); |
| 291 |
assertFalse(result); |
| 292 |
} |
| 293 |
|
| 294 |
function testHasValueByNameEmpty() { |
| 295 |
var form = goog.dom.getElement('testform1'); |
| 296 |
var result = goog.dom.forms.hasValueByName(form, 'in3'); |
| 297 |
assertFalse(result); |
| 298 |
} |
| 299 |
|
| 300 |
function testHasValueRadio() { |
| 301 |
var el = goog.dom.getElement('radio1'); |
| 302 |
var result = goog.dom.forms.hasValue(el); |
| 303 |
assertTrue(result); |
| 304 |
} |
| 305 |
|
| 306 |
function testHasValueByNameRadio() { |
| 307 |
var form = goog.dom.getElement('testform1'); |
| 308 |
var result = goog.dom.forms.hasValueByName(form, 'radio'); |
| 309 |
assertTrue(result); |
| 310 |
} |
| 311 |
|
| 312 |
function testHasValueRadioNotChecked() { |
| 313 |
var el = goog.dom.getElement('radio2'); |
| 314 |
var result = goog.dom.forms.hasValue(el); |
| 315 |
assertFalse(result); |
| 316 |
} |
| 317 |
|
| 318 |
function testHasValueByNameRadioNotChecked() { |
| 319 |
var form = goog.dom.getElement('testform3'); |
| 320 |
var result = goog.dom.forms.hasValueByName(form, 'radio3'); |
| 321 |
assertFalse(result); |
| 322 |
} |
| 323 |
|
| 324 |
function testHasValueSelectSingle() { |
| 325 |
var el = goog.dom.getElement('select1'); |
| 326 |
var result = goog.dom.forms.hasValue(el); |
| 327 |
assertTrue(result); |
| 328 |
} |
| 329 |
|
| 330 |
function testHasValueByNameSelectSingle() { |
| 331 |
var form = goog.dom.getElement('testform1'); |
| 332 |
var result = goog.dom.forms.hasValueByName(form, 'select1'); |
| 333 |
assertTrue(result); |
| 334 |
} |
| 335 |
|
| 336 |
function testHasValueSelectMultiple() { |
| 337 |
var el = goog.dom.getElement('select2'); |
| 338 |
var result = goog.dom.forms.hasValue(el); |
| 339 |
assertTrue(result); |
| 340 |
} |
| 341 |
|
| 342 |
function testHasValueByNameSelectMultiple() { |
| 343 |
var form = goog.dom.getElement('testform1'); |
| 344 |
var result = goog.dom.forms.hasValueByName(form, 'select2'); |
| 345 |
assertTrue(result); |
| 346 |
} |
| 347 |
|
| 348 |
function testHasValueSelectNotSelected() { |
| 349 |
// select without value |
| 350 |
var el = goog.dom.getElement('select3'); |
| 351 |
var result = goog.dom.forms.hasValue(el); |
| 352 |
assertFalse(result); |
| 353 |
} |
| 354 |
|
| 355 |
function testHasValueByNameSelectNotSelected() { |
| 356 |
var form = goog.dom.getElement('testform1'); |
| 357 |
var result = goog.dom.forms.hasValueByName(form, 'select3'); |
| 358 |
assertFalse(result); |
| 359 |
} |
| 360 |
|
| 361 |
function testHasValueSelectMultipleNotSelected() { |
| 362 |
var el = goog.dom.getElement('select6'); |
| 363 |
var result = goog.dom.forms.hasValue(el); |
| 364 |
assertFalse(result); |
| 365 |
} |
| 366 |
|
| 367 |
function testHasValueByNameSelectMultipleNotSelected() { |
| 368 |
var form = goog.dom.getElement('testform3'); |
| 369 |
var result = goog.dom.forms.hasValueByName(form, 'select6'); |
| 370 |
assertFalse(result); |
| 371 |
} |
| 372 |
|
| 373 |
// TODO(user): make this a meaningful selenium test |
| 374 |
function testSetDisabledFalse() {} |
| 375 |
function testSetDisabledTrue() {} |
| 376 |
|
| 377 |
// TODO(user): make this a meaningful selenium test |
| 378 |
function testFocusAndSelect() { |
| 379 |
var el = goog.dom.getElement('in1'); |
| 380 |
goog.dom.forms.focusAndSelect(el); |
| 381 |
} |
| 382 |
|
| 383 |
function testGetValueInput() { |
| 384 |
var el = goog.dom.getElement('in1'); |
| 385 |
var result = goog.dom.forms.getValue(el); |
| 386 |
assertEquals('foo', result); |
| 387 |
} |
| 388 |
|
| 389 |
function testSetValueInput() { |
| 390 |
var el = goog.dom.getElement('in3'); |
| 391 |
goog.dom.forms.setValue(el, 'foo'); |
| 392 |
assertEquals('foo', goog.dom.forms.getValue(el)); |
| 393 |
|
| 394 |
goog.dom.forms.setValue(el, 3500); |
| 395 |
assertEquals('3500', goog.dom.forms.getValue(el)); |
| 396 |
|
| 397 |
goog.dom.forms.setValue(el, 0); |
| 398 |
assertEquals('0', goog.dom.forms.getValue(el)); |
| 399 |
|
| 400 |
goog.dom.forms.setValue(el, null); |
| 401 |
assertEquals('', goog.dom.forms.getValue(el)); |
| 402 |
|
| 403 |
goog.dom.forms.setValue(el, undefined); |
| 404 |
assertEquals('', goog.dom.forms.getValue(el)); |
| 405 |
|
| 406 |
goog.dom.forms.setValue(el, false); |
| 407 |
assertEquals('false', goog.dom.forms.getValue(el)); |
| 408 |
|
| 409 |
goog.dom.forms.setValue(el, {}); |
| 410 |
assertEquals({}.toString(), goog.dom.forms.getValue(el)); |
| 411 |
|
| 412 |
goog.dom.forms.setValue(el, {toString: function() { return 'test'; }}); |
| 413 |
assertEquals('test', goog.dom.forms.getValue(el)); |
| 414 |
|
| 415 |
// unset |
| 416 |
goog.dom.forms.setValue(el); |
| 417 |
assertEquals('', goog.dom.forms.getValue(el)); |
| 418 |
} |
| 419 |
|
| 420 |
function testGetValueMeter() { |
| 421 |
var el = goog.dom.createDom('meter', {'min': 0, 'max': 3, 'value': 2.3}); |
| 422 |
assertEquals(2.3, goog.dom.forms.getValue(el)); |
| 423 |
} |
| 424 |
|
| 425 |
function testSetValueMeter() { |
| 426 |
var el = goog.dom.createDom('meter', {'min': 1, 'max': 5, 'value': 3}); |
| 427 |
|
| 428 |
assertEquals(3, goog.dom.forms.getValue(el)); |
| 429 |
|
| 430 |
goog.dom.forms.setValue(el, 2); |
| 431 |
assertEquals(2, goog.dom.forms.getValue(el)); |
| 432 |
} |
| 433 |
|
| 434 |
function testGetValuePassword() { |
| 435 |
var el = goog.dom.getElement('pass'); |
| 436 |
var result = goog.dom.forms.getValue(el); |
| 437 |
assertEquals('bar', result); |
| 438 |
} |
| 439 |
|
| 440 |
function testGetValueByNamePassword() { |
| 441 |
var form = goog.dom.getElement('testform1'); |
| 442 |
var result = goog.dom.forms.getValueByName(form, 'pass'); |
| 443 |
assertEquals('bar', result); |
| 444 |
} |
| 445 |
|
| 446 |
function testGetValueTextarea() { |
| 447 |
var el = goog.dom.getElement('textarea1'); |
| 448 |
var result = goog.dom.forms.getValue(el); |
| 449 |
assertEquals('foo bar baz', result); |
| 450 |
} |
| 451 |
|
| 452 |
function testGetValueByNameTextarea() { |
| 453 |
var form = goog.dom.getElement('testform1'); |
| 454 |
var result = goog.dom.forms.getValueByName(form, 'textarea1'); |
| 455 |
assertEquals('foo bar baz', result); |
| 456 |
} |
| 457 |
|
| 458 |
function testSetValueTextarea() { |
| 459 |
var el = goog.dom.getElement('textarea2'); |
| 460 |
goog.dom.forms.setValue(el, 'foo bar baz'); |
| 461 |
var result = goog.dom.forms.getValue(el); |
| 462 |
assertEquals('foo bar baz', result); |
| 463 |
} |
| 464 |
|
| 465 |
function testGetValueSelectSingle() { |
| 466 |
var el = goog.dom.getElement('select1'); |
| 467 |
var result = goog.dom.forms.getValue(el); |
| 468 |
assertEquals('1', result); |
| 469 |
} |
| 470 |
|
| 471 |
function testGetValueByNameSelectSingle() { |
| 472 |
var form = goog.dom.getElement('testform1'); |
| 473 |
var result = goog.dom.forms.getValueByName(form, 'select1'); |
| 474 |
assertEquals('1', result); |
| 475 |
} |
| 476 |
|
| 477 |
function testSetValueSelectSingle() { |
| 478 |
var el = goog.dom.getElement('select4'); |
| 479 |
goog.dom.forms.setValue(el, '2'); |
| 480 |
var result = goog.dom.forms.getValue(el); |
| 481 |
assertEquals('2', result); |
| 482 |
// unset |
| 483 |
goog.dom.forms.setValue(el); |
| 484 |
var result = goog.dom.forms.getValue(el); |
| 485 |
assertNull(result); |
| 486 |
} |
| 487 |
|
| 488 |
function testSetValueSelectSingleEmptyString() { |
| 489 |
var el = goog.dom.getElement('select7'); |
| 490 |
// unset |
| 491 |
goog.dom.forms.setValue(el); |
| 492 |
var result = goog.dom.forms.getValue(el); |
| 493 |
assertNull(result); |
| 494 |
goog.dom.forms.setValue(el, ''); |
| 495 |
result = goog.dom.forms.getValue(el); |
| 496 |
assertEquals('', result); |
| 497 |
} |
| 498 |
|
| 499 |
function testGetValueSelectMultiple() { |
| 500 |
var el = goog.dom.getElement('select2'); |
| 501 |
var result = goog.dom.forms.getValue(el); |
| 502 |
assertArrayEquals(['a', 'c'], result); |
| 503 |
} |
| 504 |
|
| 505 |
function testGetValueSelectMultipleNotSelected() { |
| 506 |
var el = goog.dom.getElement('select6'); |
| 507 |
var result = goog.dom.forms.getValue(el); |
| 508 |
assertNull(result); |
| 509 |
} |
| 510 |
|
| 511 |
function testGetValueByNameSelectMultiple() { |
| 512 |
var form = goog.dom.getElement('testform1'); |
| 513 |
var result = goog.dom.forms.getValueByName(form, 'select2'); |
| 514 |
assertArrayEquals(['a', 'c'], result); |
| 515 |
} |
| 516 |
|
| 517 |
function testSetValueSelectMultiple() { |
| 518 |
var el = goog.dom.getElement('select5'); |
| 519 |
goog.dom.forms.setValue(el, ['a', 'c']); |
| 520 |
var result = goog.dom.forms.getValue(el); |
| 521 |
assertArrayEquals(['a', 'c'], result); |
| 522 |
|
| 523 |
goog.dom.forms.setValue(el, 'a'); |
| 524 |
var result = goog.dom.forms.getValue(el); |
| 525 |
assertArrayEquals(['a'], result); |
| 526 |
|
| 527 |
// unset |
| 528 |
goog.dom.forms.setValue(el); |
| 529 |
var result = goog.dom.forms.getValue(el); |
| 530 |
assertNull(result); |
| 531 |
} |
| 532 |
|
| 533 |
function testGetValueCheckbox() { |
| 534 |
var el = goog.dom.getElement('checkbox1'); |
| 535 |
var result = goog.dom.forms.getValue(el); |
| 536 |
assertEquals('on', result); |
| 537 |
var el = goog.dom.getElement('checkbox2'); |
| 538 |
var result = goog.dom.forms.getValue(el); |
| 539 |
assertNull(result); |
| 540 |
} |
| 541 |
|
| 542 |
function testGetValueByNameCheckbox() { |
| 543 |
var form = goog.dom.getElement('testform1'); |
| 544 |
var result = goog.dom.forms.getValueByName(form, 'checkbox1'); |
| 545 |
assertEquals('on', result); |
| 546 |
result = goog.dom.forms.getValueByName(form, 'checkbox2'); |
| 547 |
assertNull(result); |
| 548 |
} |
| 549 |
|
| 550 |
function testGetValueRadio() { |
| 551 |
var el = goog.dom.getElement('radio1'); |
| 552 |
var result = goog.dom.forms.getValue(el); |
| 553 |
assertEquals('X', result); |
| 554 |
var el = goog.dom.getElement('radio2'); |
| 555 |
var result = goog.dom.forms.getValue(el); |
| 556 |
assertNull(result); |
| 557 |
} |
| 558 |
|
| 559 |
function testGetValueByNameRadio() { |
| 560 |
var form = goog.dom.getElement('testform1'); |
| 561 |
var result = goog.dom.forms.getValueByName(form, 'radio'); |
| 562 |
assertEquals('X', result); |
| 563 |
|
| 564 |
result = goog.dom.forms.getValueByName(form, 'radio2'); |
| 565 |
assertEquals('Y', result); |
| 566 |
} |
| 567 |
|
| 568 |
function testGetValueButton() { |
| 569 |
var el = goog.dom.getElement('button'); |
| 570 |
var result = goog.dom.forms.getValue(el); |
| 571 |
assertEquals('button', result); |
| 572 |
} |
| 573 |
|
| 574 |
function testGetValueSubmit() { |
| 575 |
var el = goog.dom.getElement('submit'); |
| 576 |
var result = goog.dom.forms.getValue(el); |
| 577 |
assertEquals('submitv', result); |
| 578 |
} |
| 579 |
|
| 580 |
function testGetValueReset() { |
| 581 |
var el = goog.dom.getElement('reset'); |
| 582 |
var result = goog.dom.forms.getValue(el); |
| 583 |
assertEquals('reset', result); |
| 584 |
} |
| 585 |
|
| 586 |
function testGetFormDataHelperAndNonInputElements() { |
| 587 |
var el = goog.dom.getElement('testform4'); |
| 588 |
goog.dom.forms.getFormDataHelper_(el, {}, goog.nullFunction); |
| 589 |
} |