1 38 package com.gargoylesoftware.htmlunit.javascript; 39 40 import java.io.IOException; 41 42 import com.gargoylesoftware.htmlunit.MockWebConnection; 43 import com.gargoylesoftware.htmlunit.WebClient; 44 import com.gargoylesoftware.htmlunit.WebTestCase; 45 import com.gargoylesoftware.htmlunit.html.HtmlElement; 46 import com.gargoylesoftware.htmlunit.html.HtmlPage; 47 48 49 56 public class AttributeCaseTest extends WebTestCase { 57 58 private static final String ATTRIBUTE_NAME = "randomAttribute"; 59 private static final String ATTRIBUTE_VALUE = "someValue"; 60 private static final String ATTRIBUTE_VALUE_NEW = "newValue"; 61 62 private HtmlElement element_; 63 private HtmlPage page_; 64 65 66 71 public AttributeCaseTest(final String name) { 72 super(name); 73 } 74 75 79 public void testGetAttributeLowerCase() throws IOException { 80 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 81 assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 82 } 83 84 88 public void testGetAttributeMixedCase() throws IOException { 89 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 90 assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME)); 91 } 92 93 97 public void testGetAttributeUpperCase() throws IOException { 98 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 99 assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME.toUpperCase())); 100 } 101 102 106 public void testSetAttributeLowerCase() throws IOException { 107 setupSetAttributeTest(ATTRIBUTE_NAME.toLowerCase(), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW); 108 assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 109 } 110 111 115 public void testSetAttributeMixedCase() throws IOException { 116 setupSetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW); 117 assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 118 } 119 120 124 public void testSetAttributeUpperCase() throws IOException { 125 setupSetAttributeTest(ATTRIBUTE_NAME.toUpperCase(), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW); 126 assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 127 } 128 129 133 public void testRemoveAttributeLowerCase() throws IOException { 134 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 135 element_.removeAttribute(ATTRIBUTE_NAME.toLowerCase()); 136 assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 137 } 138 139 143 public void testRemoveAttributeMixedCase() throws IOException { 144 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 145 element_.removeAttribute(ATTRIBUTE_NAME); 146 assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 147 } 148 149 153 public void testRemoveAttributeUpperCase() throws IOException { 154 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 155 element_.removeAttribute(ATTRIBUTE_NAME.toUpperCase()); 156 assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase())); 157 } 158 159 163 public void testIsAttributeDefinedLowerCase() throws IOException { 164 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 165 assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME.toLowerCase())); 166 } 167 168 172 public void testIsAttributeDefinedMixedCase() throws IOException { 173 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 174 assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME)); 175 } 176 177 181 public void testIsAttributeDefinedUpperCase() throws IOException { 182 setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE); 183 assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME.toUpperCase())); 184 } 185 186 187 private void setupAttributeTest(final String content, final String elementId) throws IOException { 188 189 final WebClient client = new WebClient(); 190 final MockWebConnection webConnection = new MockWebConnection(client); 191 192 webConnection.setDefaultResponse(content); 193 client.setWebConnection(webConnection); 194 195 page_ = (HtmlPage) client.getPage(URL_GARGOYLE); 196 197 element_ = page_.getHtmlElementById(elementId); 198 } 199 200 201 private void setupGetAttributeTest(final String attributeName, final String attributeValue) throws IOException { 202 203 final String elementId = "p-id"; 204 final String content = "<html><head><title>AttributeCaseTest</title></head><body>\n" 205 + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n" 206 + "</body></html>"; 207 208 setupAttributeTest(content, elementId); 209 } 210 211 212 private void setupSetAttributeTest( 213 final String attributeName, final String attributeValue, 214 final String newAttributeValue) 215 throws IOException { 216 217 final String elementId = "p-id"; 218 final String content 219 = "<html><head><title>AttributeCaseTest</title></head><body>\n" 220 + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n" 221 + "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n" 222 + " document.getElementById(\"" + elementId + "\").setAttribute(\"" + attributeName + "\", \"" + 223 newAttributeValue + "\");\n" 224 + "\n// -->\n</script>\n" 225 + "</body></html>"; 226 227 setupAttributeTest(content, elementId); 228 } 229 } 230 | Popular Tags |