forms_test.js 18 KB

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