1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.Collections; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 46 import com.gargoylesoftware.htmlunit.MockWebConnection; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 import com.gargoylesoftware.htmlunit.html.HtmlPage; 50 51 57 public class StyleTest extends WebTestCase { 58 59 63 public StyleTest( final String name ) { 64 super(name); 65 } 66 67 68 71 public void testStyle_OneCssAttribute() throws Exception { 72 final WebClient client = new WebClient(); 73 final MockWebConnection webConnection = new MockWebConnection( client ); 74 75 final String firstContent 76 = "<html><head><title>First</title><script>\n" 77 + "function doTest() {\n" 78 + " var style = document.getElementById('div1').style;\n" 79 + " alert(style.color);\n" 80 + " style.color = 'pink';\n" 81 + " alert(style.color);\n" 82 + "}\n</script></head>" 83 + "<body onload='doTest()'><div id='div1' style='color: black'>foo</div></body></html>"; 84 85 webConnection.setResponse( 86 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 87 client.setWebConnection( webConnection ); 88 89 final List collectedAlerts = new ArrayList(); 90 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 91 92 final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST); 93 94 final List expectedAlerts = Arrays.asList( new String[]{"black", "pink"} ); 95 assertEquals( expectedAlerts, collectedAlerts ); 96 97 assertEquals("color: pink; ", page.getHtmlElementById("div1").getAttributeValue("style") ); 98 } 99 100 101 104 public void testStyle_MultipleCssAttributes() throws Exception { 105 final WebClient client = new WebClient(); 106 final MockWebConnection webConnection = new MockWebConnection( client ); 107 108 final String firstContent 109 = "<html><head><title>First</title><script>\n" 110 + "function doTest() {\n" 111 + " var style = document.getElementById('div1').style;\n" 112 + " alert(style.color);\n" 113 + " style.color = 'pink';\n" 114 + " alert(style.color);\n" 115 + "}\n</script></head>" 116 + "<body onload='doTest()'>" 117 + "<div id='div1' style='color: black;background:blue;foo:bar'>foo</div></body></html>"; 118 119 webConnection.setResponse( 120 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 121 client.setWebConnection( webConnection ); 122 123 final List collectedAlerts = new ArrayList(); 124 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 125 126 final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST); 127 128 final List expectedAlerts = Arrays.asList( new String[]{"black", "pink"} ); 129 assertEquals( expectedAlerts, collectedAlerts ); 130 131 assertEquals( 132 "background: blue; color: pink; foo: bar; ", 133 page.getHtmlElementById("div1").getAttributeValue("style") ); 134 } 135 136 139 public void testStyle_OneUndefinedCssAttribute() throws Exception { 140 final WebClient client = new WebClient(); 141 final MockWebConnection webConnection = new MockWebConnection( client ); 142 143 final String firstContent 144 = "<html><head><title>First</title><script>\n" 145 + "function doTest() {\n" 146 + " var style = document.getElementById('div1').style;\n" 147 + " alert(document.getElementById('nonexistingid'));\n" 148 + " alert(style.color);\n" 149 + " style.color = 'pink';\n" 150 + " alert(style.color);\n" 151 + "}\n</script></head>" 152 + "<body onload='doTest()'><div id='div1'>foo</div></body></html>"; 153 154 webConnection.setResponse( 155 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 156 client.setWebConnection( webConnection ); 157 158 final List collectedAlerts = new ArrayList(); 159 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 160 161 final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST); 162 163 final List expectedAlerts = Arrays.asList( new String[]{"null", "", "pink"} ); 164 assertEquals( expectedAlerts, collectedAlerts ); 165 166 assertEquals("color: pink; ", page.getHtmlElementById("div1").getAttributeValue("style") ); 167 } 168 } 169 | Popular Tags |