001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.verify;
003:
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import com.canoo.webtest.engine.StepExecutionException;
010: import com.canoo.webtest.engine.StepFailedException;
011: import com.canoo.webtest.self.ThrowAssert;
012: import com.canoo.webtest.steps.Step;
013: import com.gargoylesoftware.htmlunit.html.HtmlForm;
014: import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
015:
016: /**
017: * @author Denis N. Antonioli
018: */
019: public class VerifyRadioButtonTest extends AbstractVerifyFormTest {
020: private class VerifyRadioButtonTestStub extends VerifyRadioButton {
021: private int fListSize;
022: private boolean fShouldBeChecked;
023:
024: VerifyRadioButtonTestStub(final int size) {
025: this (size, true);
026: }
027:
028: VerifyRadioButtonTestStub(final int size, final boolean checked) {
029: fListSize = size;
030: fShouldBeChecked = checked;
031: }
032:
033: protected HtmlForm findForm() {
034: return (HtmlForm) getDummyPage().createHtmlElement("form");
035: }
036:
037: protected List findFields(final HtmlForm form) {
038: final List result = new ArrayList(fListSize);
039: for (int i = 0; i < fListSize; i++) {
040: result.add(getDummyNode());
041: }
042: return result;
043: }
044:
045: private HtmlRadioButtonInput getDummyNode() {
046: final Map attributes = new HashMap();
047: if (fShouldBeChecked) {
048: attributes.put("checked", String
049: .valueOf(fShouldBeChecked));
050: }
051: return new HtmlRadioButtonInput(getDummyPage(), attributes);
052: }
053: }
054:
055: protected Step createStep() {
056: return new VerifyRadioButton();
057: }
058:
059: public void testNoRadioButtonFound() throws Exception {
060: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
061: 0);
062: step.setName("dummy");
063: assertThrowsExceptionOnExecute(step, StepFailedException.class);
064: }
065:
066: public void testTooManyRadioButtonsFound() throws Exception {
067: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
068: 2);
069: step.setName("dummy");
070: step.setChecked("true");
071: executeStep(step);
072: }
073:
074: public void testMultipleRadioButtonsWithBadIndex() throws Exception {
075: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
076: 2);
077: step.setName("dummy");
078: step.setFieldIndex("blah");
079: assertThrowsExceptionOnExecute(step,
080: StepExecutionException.class);
081: }
082:
083: public void testMultipleRadioButtonsWithIndex() throws Exception {
084: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
085: 2);
086: step.setName("dummy");
087: step.setChecked("true");
088: step.setFieldIndex("1");
089: executeStep(step);
090: }
091:
092: public void testNotChecked() throws Exception {
093: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
094: 1, false);
095: step.setName("X");
096: step.setChecked("false");
097: executeStep(step);
098: // runs without exception
099: }
100:
101: public void testNotCheckedButExpected() throws Exception {
102: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
103: 1, false);
104: step.setName("X");
105: step.setChecked("true");
106: assertThrowsExceptionOnExecute(step, StepFailedException.class);
107: }
108:
109: public void testChecked() throws Exception {
110: VerifyRadioButton step = new VerifyRadioButtonTest.VerifyRadioButtonTestStub(
111: 1, true);
112: step.setName("X");
113: step.setChecked("true");
114: executeStep(step);
115: // runs without exception
116: }
117:
118: public void testCheckedButNotExpected() throws Exception {
119: VerifyRadioButtonTestStub step = new VerifyRadioButtonTestStub(
120: 1, true);
121: step.setName("X");
122: step.setChecked("false");
123: assertThrowsExceptionOnExecute(step, StepFailedException.class);
124: }
125:
126: private void assertThrowsExceptionOnExecute(
127: final VerifyRadioButton step, Class throwable) {
128: ThrowAssert.assertThrows(throwable,
129: getExecuteStepTestBlock(step));
130: }
131: }
|