| 1 |
// Copyright 2009 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.FormPostTest'); |
| 16 |
goog.setTestOnly('goog.ui.FormPostTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.object'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.ui.FormPost'); |
| 24 |
goog.require('goog.userAgent.product'); |
| 25 |
goog.require('goog.userAgent.product.isVersion'); |
| 26 |
|
| 27 |
var TARGET = 'target'; |
| 28 |
var ACTION_URL = 'http://url/'; |
| 29 |
var formPost; |
| 30 |
var parameters; |
| 31 |
var submits; |
| 32 |
var originalCreateDom = goog.ui.FormPost.prototype.createDom; |
| 33 |
|
| 34 |
function isChrome7or8() { |
| 35 |
// Temporarily disabled in Chrome 7 & 8. See b/3176768 |
| 36 |
if (goog.userAgent.product.CHROME && |
| 37 |
goog.userAgent.product.isVersion('7.0') && |
| 38 |
!goog.userAgent.product.isVersion('8.0')) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
function setUp() { |
| 46 |
formPost = new goog.ui.FormPost(); |
| 47 |
submits = 0; |
| 48 |
|
| 49 |
// Replace the form's submit method with a fake. |
| 50 |
goog.ui.FormPost.prototype.createDom = function() { |
| 51 |
originalCreateDom.apply(this, arguments); |
| 52 |
|
| 53 |
this.getElement().submit = function() { submits++ }; |
| 54 |
}; |
| 55 |
parameters = {'foo': 'bar', 'baz': 1, 'array': [0, 'yes']}; |
| 56 |
} |
| 57 |
|
| 58 |
function tearDown() { |
| 59 |
formPost.dispose(); |
| 60 |
goog.ui.FormPost.prototype.createDom = originalCreateDom; |
| 61 |
} |
| 62 |
|
| 63 |
function testPost() { |
| 64 |
formPost.post(parameters, ACTION_URL, TARGET); |
| 65 |
expectUrlAndParameters_(ACTION_URL, TARGET, parameters); |
| 66 |
} |
| 67 |
|
| 68 |
function testPostWithDefaults() { |
| 69 |
// Temporarily disabled in Chrome 7. See See b/3176768 |
| 70 |
if (isChrome7or8) { |
| 71 |
return; |
| 72 |
} |
| 73 |
formPost = new goog.ui.FormPost(); |
| 74 |
formPost.post(parameters); |
| 75 |
expectUrlAndParameters_('', '', parameters); |
| 76 |
} |
| 77 |
|
| 78 |
function expectUrlAndParameters_(url, target, parameters) { |
| 79 |
var form = formPost.getElement(); |
| 80 |
assertEquals( |
| 81 |
'element must be a form', String(goog.dom.TagName.FORM), form.tagName); |
| 82 |
assertEquals('form must be hidden', 'none', form.style.display); |
| 83 |
assertEquals('form method must be POST', 'POST', form.method.toUpperCase()); |
| 84 |
assertEquals('submits', 1, submits); |
| 85 |
assertEquals('action attribute', url, form.action); |
| 86 |
assertEquals('target attribute', target, form.target); |
| 87 |
var inputs = |
| 88 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.INPUT, null, form); |
| 89 |
var formValues = {}; |
| 90 |
for (var i = 0, input = inputs[i]; input = inputs[i]; i++) { |
| 91 |
if (goog.isArray(formValues[input.name])) { |
| 92 |
formValues[input.name].push(input.value); |
| 93 |
} else if (input.name in formValues) { |
| 94 |
formValues[input.name] = [formValues[input.name], input.value]; |
| 95 |
} else { |
| 96 |
formValues[input.name] = input.value; |
| 97 |
} |
| 98 |
} |
| 99 |
var expected = goog.object.map(parameters, valueToString); |
| 100 |
assertObjectEquals('form values must match', expected, formValues); |
| 101 |
} |
| 102 |
|
| 103 |
function valueToString(value) { |
| 104 |
if (goog.isArray(value)) { |
| 105 |
return goog.array.map(value, valueToString); |
| 106 |
} |
| 107 |
return String(value); |
| 108 |
} |