/*
* Copyright (c) 2002-2008 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.html;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.WebTestCase;
/**
* Tests for {@link HtmlInput}.
*
* @version $Revision$
* @author Mike Bowler
* @author Marc Guillemot
* @author Ahmed Ashour
*/
public final class HtmlInputTest extends WebTestCase {
/**
* Tests that selecting one radio button will deselect all the others.
* @exception Exception If the test fails
*/
@Test
public void testRadioButtonsAreMutuallyExclusive() throws Exception {
final String htmlContent
= "
foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) form.getFirstByXPath(
"//input[@type='radio' and @name='foo' and @value='2']");
final HtmlSubmitInput pushButton = form.getInputByName("button");
radioButton.setChecked(true);
// Test that only one value for the radio button is being passed back to the server
final HtmlPage secondPage = (HtmlPage) pushButton.click();
assertEquals("url", URL_GARGOYLE + "?foo=2&button=foo", secondPage.getWebResponse().getRequestUrl());
assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
assertNotNull(secondPage);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSetChecked_CheckBox() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlCheckBoxInput checkbox = form.getInputByName("foo");
Assert.assertFalse("Initial state", checkbox.isChecked());
checkbox.setChecked(true);
assertTrue("After setSelected(true)", checkbox.isChecked());
checkbox.setChecked(false);
Assert.assertFalse("After setSelected(false)", checkbox.isChecked());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testGetChecked_RadioButton() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final List radioButtons = form.getRadioButtonsByName("radio1");
assertEquals(2, radioButtons.size());
assertFalse(radioButtons.get(0).isChecked());
assertTrue(radioButtons.get(1).isChecked());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testOnChangeHandler() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(htmlContent, collectedAlerts);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlTextInput input = form.getInputByName("text1");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
input.setValueAttribute("foo");
assertEquals(new String[] {"changed"}, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testCheckboxDefaultValue() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlCheckBoxInput input = form.getInputByName("checkbox1");
assertEquals("on", input.getValueAttribute());
}
/**
* Tests that clicking a radio button will select it.
* @exception Exception If the test fails
*/
@Test
public void testClickRadioButton() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) form.getFirstByXPath(
"//input[@type='radio' and @name='foo' and @value='2']");
Assert.assertFalse("Should not be checked before click", radioButton.isChecked());
radioButton.click();
assertTrue("Should be checked after click", radioButton.isChecked());
}
/**
* Tests that default type of input is text.
* @exception Exception If the test fails
*/
@Test
public void testInputNoType() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
assertEquals("text", form.getInputByName("foo").getTypeAttribute());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testOnChangeHandlerNotFiredOnLoad() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final List collectedAlerts = new ArrayList();
loadPage(htmlContent, collectedAlerts);
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testBadInputType() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "";
final String[] expectedAlerts = {"text"};
createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
final List collectedAlerts = new ArrayList();
loadPage(htmlContent, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testOnchangeNull() throws Exception {
final String html =
"\n"
+ "\n"
+ "\n"
+ " \n"
+ "";
final String[] expectedAlerts = {"\nfunction handler() {\n}\n", "null"};
final List collectedAlerts = new ArrayList();
loadPage(html, collectedAlerts);
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* Tests that clicking a radio button will select it.
* @exception Exception If the test fails
*/
@Test
public void testSelect() throws Exception {
final String content
= "foo\n"
+ "\n"
+ "\n"
+ "";
createTestPageForRealBrowserIfNeeded(content, new String[] {});
loadPage(content);
}
}