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.javascript.host;
039:
040: import com.gargoylesoftware.htmlunit.BrowserVersion;
041: import com.gargoylesoftware.htmlunit.WebTestCase;
042: import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
043: import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
044: import com.gargoylesoftware.htmlunit.html.HtmlLabel;
045: import com.gargoylesoftware.htmlunit.html.HtmlPage;
046:
047: /**
048: * Tests for {@link Label}.
049: *
050: * @version $Revision: 2132 $
051: * @author Ahmed Ashour
052: */
053: public class LabelTest extends WebTestCase {
054:
055: /**
056: * Create an instance
057: * @param name The name of the test
058: */
059: public LabelTest(final String name) {
060: super (name);
061: }
062:
063: /**
064: * @throws Exception if the test fails
065: */
066: public void testHtmlFor() throws Exception {
067: testHtmlFor(BrowserVersion.INTERNET_EXPLORER_7_0);
068: testHtmlFor(BrowserVersion.FIREFOX_2);
069: }
070:
071: private void testHtmlFor(final BrowserVersion browserVersion)
072: throws Exception {
073: final String html = "<html><head><title>First</title><script>\n"
074: + "function doTest() {\n"
075: + " document.getElementById('label1').htmlFor = 'checkbox1';\n"
076: + "}\n"
077: + "</script></head><body onload='doTest()'>\n"
078: + "<label id='label1'>My Label</label>\n"
079: + "<input type='checkbox' id='checkbox1'><br>\n"
080: + "</body></html>";
081:
082: final HtmlPage page = loadPage(html);
083: final HtmlLabel label = (HtmlLabel) page
084: .getHtmlElementById("label1");
085: final HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page
086: .getHtmlElementById("checkbox1");
087: assertFalse(checkbox.isChecked());
088: label.click();
089: assertTrue(checkbox.isChecked());
090: }
091:
092: /**
093: * Tests that clicking the label by javascript does not change 'htmlFor' attribute in FF!!
094: *
095: * @throws Exception if the test fails
096: */
097: public void testHtmlFor_click() throws Exception {
098: if (notYetImplemented()) {
099: return;
100: }
101: testHtmlFor_click(BrowserVersion.INTERNET_EXPLORER_7_0, true);
102: testHtmlFor_click(BrowserVersion.FIREFOX_2, false);
103: }
104:
105: /**
106: * @param changedByClick if 'label.click()' javascript causes the associated 'htmlFor' element to be changed.
107: */
108: private void testHtmlFor_click(final BrowserVersion browserVersion,
109: final boolean changedByClick) throws Exception {
110: final String html = "<html><head><title>First</title><script>\n"
111: + "function doTest() {\n"
112: + " document.getElementById('label1').htmlFor = 'checkbox1';\n"
113: + "}\n"
114: + "</script></head><body onload='doTest()'>\n"
115: + "<label id='label1'>My Label</label>\n"
116: + "<input type='checkbox' id='checkbox1'><br>\n"
117: + "<input type=button id='button1' value='Test' onclick='document.getElementById(\"label1\").click()'>\n"
118: + "</body></html>";
119:
120: final HtmlPage page = loadPage(html);
121: final HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page
122: .getHtmlElementById("checkbox1");
123: final HtmlButtonInput button = (HtmlButtonInput) page
124: .getHtmlElementById("button1");
125: assertFalse(checkbox.isChecked());
126: button.click();
127: assertTrue(checkbox.isChecked() == changedByClick);
128: }
129: }
|