001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import com.gargoylesoftware.htmlunit.WebTestCase;
041:
042: /**
043: * Tests for {@link HtmlOption}.
044: *
045: * @version $Revision: 2132 $
046: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
047: * @author Marc Guillemot
048: */
049: public class HtmlOptionTest extends WebTestCase {
050: /**
051: * Create an instance
052: * @param name The name of the test
053: */
054: public HtmlOptionTest(final String name) {
055: super (name);
056: }
057:
058: /**
059: * @throws Exception if the test fails
060: */
061: public void testSelect() throws Exception {
062: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
063: + "<form id='form1'><select name='select1' id='select1'>\n"
064: + "<option value='option1' id='option1'>Option1</option>\n"
065: + "<option value='option2' id='option2' selected='selected'>Option2</option>\n"
066: + "<option value='option3' id='option3'>Option3</option>\n"
067: + "</select>\n"
068: + "<input type='submit' name='button' value='foo'/>\n"
069: + "</form></body></html>";
070: final HtmlPage page = loadPage(htmlContent);
071:
072: final HtmlOption option1 = (HtmlOption) page
073: .getHtmlElementById("option1");
074: final HtmlOption option2 = (HtmlOption) page
075: .getHtmlElementById("option2");
076: final HtmlOption option3 = (HtmlOption) page
077: .getHtmlElementById("option3");
078:
079: assertFalse(option1.isSelected());
080: assertTrue(option2.isSelected());
081: assertFalse(option3.isSelected());
082:
083: option3.setSelected(true);
084:
085: assertFalse(option1.isSelected());
086: assertFalse(option2.isSelected());
087: assertTrue(option3.isSelected());
088:
089: option3.setSelected(false);
090:
091: assertFalse(option1.isSelected());
092: assertFalse(option2.isSelected());
093: assertFalse(option3.isSelected());
094: }
095:
096: /**
097: * @throws Exception if the test fails
098: */
099: public void testGetValue() throws Exception {
100: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
101: + "<form id='form1'><select name='select1' id='select1'>\n"
102: + "<option value='option1' id='option1'>Option1</option>\n"
103: + "<option id='option2' selected>Number Two</option>\n"
104: + "</select>\n"
105: + "<input type='submit' name='button' value='foo'/>\n"
106: + "</form></body></html>";
107:
108: final HtmlPage page = loadPage(htmlContent);
109:
110: final HtmlOption option1 = (HtmlOption) page
111: .getHtmlElementById("option1");
112: final HtmlOption option2 = (HtmlOption) page
113: .getHtmlElementById("option2");
114:
115: assertEquals("option1", option1.getValueAttribute());
116: assertEquals("Number Two", option2.getValueAttribute());
117:
118: }
119:
120: /**
121: * @throws Exception if the test fails
122: */
123: public void testGetValue_ContentsIsValue() throws Exception {
124: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
125: + "<form id='form1'>\n"
126: + "<select name='select1' id='select1'>\n"
127: + " <option id='option1'>Option1</option>\n"
128: + " <option id='option2' selected>Number Two</option>\n"
129: + " <option id='option3'>\n Number 3 with blanks </option>\n"
130: + "</select>\n"
131: + "<input type='submit' name='button' value='foo'/>\n"
132: + "</form></body></html>";
133:
134: final HtmlPage page = loadPage(htmlContent);
135:
136: final HtmlOption option1 = (HtmlOption) page
137: .getHtmlElementById("option1");
138: assertEquals("Option1", option1.getValueAttribute());
139:
140: final HtmlOption option2 = (HtmlOption) page
141: .getHtmlElementById("option2");
142: assertEquals("Number Two", option2.getValueAttribute());
143:
144: final HtmlOption option3 = (HtmlOption) page
145: .getHtmlElementById("option3");
146: assertEquals("Number 3 with blanks", option3
147: .getValueAttribute());
148: }
149:
150: /**
151: * @throws Exception if the test fails
152: */
153: public void testClick() throws Exception {
154:
155: final String htmlContent = "<html><body>\n"
156: + "<form id='form1'>\n"
157: + "<select name='select1' id='select1'>\n"
158: + " <option id='option1'>Option1</option>\n"
159: + " <option id='option2' selected>Number Two</option>\n"
160: + "</select>\n"
161: + "<input type='submit' name='button' value='foo'/>\n"
162: + "</form></body></html>";
163: final HtmlPage page = loadPage(htmlContent);
164:
165: final HtmlOption option1 = (HtmlOption) page
166: .getHtmlElementById("option1");
167: assertFalse(option1.isSelected());
168: option1.click();
169: assertTrue(option1.isSelected());
170: option1.click();
171: assertTrue(option1.isSelected());
172: }
173:
174: /**
175: * @throws Exception if the test fails
176: */
177: public void testAsText() throws Exception {
178: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
179: + "<form><select>\n"
180: + "<option id='option1'>option1</option>\n"
181: + "<option id='option2' label='Number Two'/>\n"
182: + "<option id='option3' label='overridden'>Number Three</option>\n"
183: + "<option id='option4'>Number 4</option>\n"
184: + "</select>\n" + "</form></body></html>";
185:
186: final HtmlPage page = loadPage(htmlContent);
187:
188: final HtmlOption option1 = (HtmlOption) page
189: .getHtmlElementById("option1");
190: final HtmlOption option2 = (HtmlOption) page
191: .getHtmlElementById("option2");
192: final HtmlOption option3 = (HtmlOption) page
193: .getHtmlElementById("option3");
194: final HtmlOption option4 = (HtmlOption) page
195: .getHtmlElementById("option4");
196:
197: assertEquals("option1", option1.asText());
198: assertEquals("Number Two", option2.asText());
199: assertEquals("overridden", option3.asText());
200: assertEquals("Number 4", option4.asText());
201: }
202: }
|