PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/htmlunit-2.8/src/test/java/com/gargoylesoftware/htmlunit/html/DomTextTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 336 lines | 211 code | 50 blank | 75 comment | 4 complexity | cf452d526b92516d5daf609a9aa720bd MD5 | raw file
  1. /*
  2. * Copyright (c) 2002-2010 Gargoyle Software Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.gargoylesoftware.htmlunit.html;
  16. import static org.junit.Assert.assertSame;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import com.gargoylesoftware.htmlunit.BrowserRunner;
  22. import com.gargoylesoftware.htmlunit.MockWebConnection;
  23. import com.gargoylesoftware.htmlunit.TextUtil;
  24. import com.gargoylesoftware.htmlunit.WebClient;
  25. import com.gargoylesoftware.htmlunit.WebTestCase;
  26. /**
  27. * Tests for {@link DomText}.
  28. *
  29. * @version $Revision: 5569 $
  30. * @author Marc Guillemot
  31. * @author Ahmed Ashour
  32. * @author Rodney Gitzel
  33. * @author Sudhan Moghe
  34. * @author Philip Graf
  35. */
  36. @RunWith(BrowserRunner.class)
  37. public class DomTextTest extends WebTestCase {
  38. /**
  39. * Test the clean up of   in strings.
  40. * @throws Exception if the test fails
  41. */
  42. @Test
  43. public void asText_nbsp() throws Exception {
  44. testPlainText("a b c d  e", "a b c d e");
  45. testPlainText("a b c d   e", "a b c d e");
  46. testPlainText(" a ", " a ");
  47. testPlainText("  a ", " a ");
  48. testPlainText(" a  ", " a ");
  49. }
  50. /**
  51. * Test font formats, as per bug #1731042.
  52. * See http://sourceforge.net/tracker/index.php?func=detail&aid=1731042&group_id=47038&atid=448266.
  53. *
  54. * @throws Exception if the test fails
  55. */
  56. @Test
  57. public void asText_fontFormat() throws Exception {
  58. testAsText("a <b>b</b> c", "a b c");
  59. testAsText("a <b>b</b>c", "a bc");
  60. testAsText("a<b>b</b> c", "ab c");
  61. testAsText("a<b>b</b>c", "abc");
  62. // italics and teletype should work the same way
  63. testAsText("a <i>b</i> c", "a b c");
  64. testAsText("a <i>b</i>c", "a bc");
  65. testAsText("a<i>b</i> c", "ab c");
  66. testAsText("a<i>b</i>c", "abc");
  67. testAsText("a <tt>b</tt> c", "a b c");
  68. testAsText("a <tt>b</tt>c", "a bc");
  69. testAsText("a<tt>b</tt> c", "ab c");
  70. testAsText("a<tt>b</tt>c", "abc");
  71. testAsText("a <font>b</font> c", "a b c");
  72. testAsText("a<font>b</font> c", "ab c");
  73. testAsText("a <font>b</font>c", "a bc");
  74. testAsText("a<font>b</font>c", "abc");
  75. testAsText("a <span>b</span> c", "a b c");
  76. testAsText("a<span>b</span> c", "ab c");
  77. testAsText("a <span>b</span>c", "a bc");
  78. testAsText("a<span>b</span>c", "abc");
  79. testAsText("a<b><font><i>b</i></font></b>c", "abc");
  80. testAsText("a<b><font> <i>b</i></font></b>c", "a bc");
  81. }
  82. /**
  83. * This test once tested regression for bug #1731042 but the expectations have been changed
  84. * as asText() should now use new lines when appropriate.
  85. * @throws Exception if the test fails
  86. */
  87. @Test
  88. public void asText_regression() throws Exception {
  89. String expected = "a" + LINE_SEPARATOR + "b" + LINE_SEPARATOR + "c";
  90. testAsText("a<ul><li>b</ul>c", expected);
  91. testAsText("a<p>b<br>c", expected);
  92. testAsText("a<table><tr><td>b</td></tr></table>c", expected);
  93. testAsText("a<div>b</div>c", expected);
  94. expected = "a" + LINE_SEPARATOR + "b" + LINE_SEPARATOR + "b" + LINE_SEPARATOR + "c";
  95. testAsText("a<table><tr><td> b </td></tr>\n<tr><td> b </td></tr></table>c", expected);
  96. }
  97. /**
  98. * Checks the HtmlTable* objects themselves.
  99. * @throws Exception if the test fails
  100. */
  101. @Test
  102. public void asText_table_elements() throws Exception {
  103. final String html = "<table id='table'><tr id='row'><td id='cell'> b </td></tr>\n</table>\n";
  104. final String content = "<html><body><span id='foo'>" + html + "</span></body></html>";
  105. final HtmlPage page = loadPage(content);
  106. assertEquals("b", page.<HtmlElement>getHtmlElementById("cell").asText());
  107. assertEquals("b", page.<HtmlElement>getHtmlElementById("row").asText());
  108. assertEquals("b", page.<HtmlElement>getHtmlElementById("table").asText());
  109. }
  110. private void testPlainText(final String html, final String expectedText) throws Exception {
  111. final String content = "<html><body><span id='foo'>" + html + "</span></body></html>";
  112. final HtmlPage page = loadPage(content);
  113. assertEquals(expectedText, page.asText());
  114. final HtmlElement elt = page.getHtmlElementById("foo");
  115. assertEquals(expectedText, elt.asText());
  116. final DomNode node = elt.getFirstChild();
  117. assertEquals(expectedText, node.asText());
  118. }
  119. private void testAsText(final String html, final String expectedText) throws Exception {
  120. final String content = "<html><body><span id='foo'>" + html + "</span></body></html>";
  121. final HtmlPage page = loadPage(content);
  122. final HtmlElement elt = page.getHtmlElementById("foo");
  123. assertEquals(expectedText, elt.asText());
  124. }
  125. /**
  126. * @throws Exception if the test fails
  127. */
  128. @Test
  129. public void asXml() throws Exception {
  130. final String unicodeString = "\u064A\u0627 \u0644\u064A\u064A\u0644";
  131. final String html = "<html>\n"
  132. + "<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>\n"
  133. + "<body><span id='foo'>" + unicodeString + "</span></body></html>";
  134. final int[] expectedValues = {1610, 1575, 32, 1604, 1610, 1610, 1604};
  135. final WebClient client = getWebClient();
  136. final MockWebConnection webConnection = new MockWebConnection();
  137. webConnection.setDefaultResponse(TextUtil.stringToByteArray(html, "UTF-8"), 200, "OK", "text/html");
  138. client.setWebConnection(webConnection);
  139. final HtmlPage page = client.getPage(getDefaultUrl());
  140. final String xml = page.<HtmlElement>getHtmlElementById("foo").getFirstChild().asXml().trim();
  141. assertEquals(expectedValues.length, xml.length());
  142. int index = 0;
  143. for (final int expectedValue : expectedValues) {
  144. assertEquals(expectedValue, xml.codePointAt(index++));
  145. }
  146. }
  147. /**
  148. * @throws Exception if the test fails
  149. */
  150. @Test
  151. public void splitText() throws Exception {
  152. final String html
  153. = "<html><head></head><body>\n"
  154. + "<br><div id='tag'></div><br></body></html>";
  155. final HtmlPage page = loadPage(html);
  156. final DomNode divNode = page.getDocumentElement().getElementById("tag");
  157. final DomText node = new DomText(page, "test split");
  158. divNode.insertBefore(node);
  159. final DomNode previousSibling = node.getPreviousSibling();
  160. final DomNode nextSibling = node.getNextSibling();
  161. final DomNode parent = node.getParentNode();
  162. // position among parent's children
  163. final int position = readPositionAmongParentChildren(node);
  164. final DomText newNode = node.splitText(5);
  165. assertSame("new node previous sibling", node, newNode.getPreviousSibling());
  166. assertSame("previous sibling", previousSibling, node.getPreviousSibling());
  167. assertSame("new node next sibling", nextSibling, newNode.getNextSibling());
  168. assertSame("next sibling", newNode, node.getNextSibling());
  169. assertSame("parent", parent, newNode.getParentNode());
  170. assertSame(node, previousSibling.getNextSibling());
  171. assertSame(newNode, nextSibling.getPreviousSibling());
  172. assertEquals(position + 1, readPositionAmongParentChildren(newNode));
  173. }
  174. /**
  175. * @throws Exception if the test fails
  176. */
  177. @Test
  178. public void splitLastDomText() throws Exception {
  179. final String content
  180. = "<html><head></head><body>\n"
  181. + "<br><div id='tag'></div><br></body></html>";
  182. final HtmlPage page = loadPage(content);
  183. final DomNode divNode = page.getDocumentElement().getElementById("tag");
  184. final DomText firstNode = new DomText(page, "test split");
  185. divNode.appendChild(firstNode);
  186. assertNull(firstNode.getPreviousSibling());
  187. final DomText secondNode = firstNode.splitText(5);
  188. final DomText thirdNode = new DomText(page, "test split");
  189. divNode.appendChild(thirdNode);
  190. assertSame(secondNode, firstNode.getNextSibling());
  191. assertNull(firstNode.getPreviousSibling());
  192. assertSame(firstNode, secondNode.getPreviousSibling());
  193. assertSame(thirdNode, secondNode.getNextSibling());
  194. assertSame(secondNode, thirdNode.getPreviousSibling());
  195. assertNull(thirdNode.getNextSibling());
  196. assertSame(divNode, secondNode.getParentNode());
  197. assertSame(divNode, thirdNode.getParentNode());
  198. assertEquals(0, readPositionAmongParentChildren(firstNode));
  199. assertEquals(1, readPositionAmongParentChildren(secondNode));
  200. assertEquals(2, readPositionAmongParentChildren(thirdNode));
  201. }
  202. /**
  203. * Reads the position of the node among the children of its parent
  204. * @param node the node to look at
  205. * @return the position
  206. */
  207. private int readPositionAmongParentChildren(final DomNode node) {
  208. int i = 0;
  209. for (final DomNode child : node.getParentNode().getChildren()) {
  210. if (child == node) {
  211. return i;
  212. }
  213. i++;
  214. }
  215. return -1;
  216. }
  217. /**
  218. * @throws Exception if the test fails
  219. */
  220. @Test
  221. public void splitText2() throws Exception {
  222. final String html = "<html><head><title>foo</title><script>\n"
  223. + " function test() {\n"
  224. + " var div = document.getElementById('myDiv');\n"
  225. + " div.appendChild(document.createElement('a'));\n"
  226. + " var text = document.createTextNode('123456');\n"
  227. + " div.appendChild(text);\n"
  228. + " div.appendChild(document.createElement('hr'));\n"
  229. + " alert(div.childNodes.length);\n"
  230. + " text.splitText(3);\n"
  231. + " alert(div.childNodes.length);\n"
  232. + " alert(div.childNodes.item(2).nodeValue);\n"
  233. + " }\n"
  234. + "</script></head><body onload='test()'>\n"
  235. + " <div id='myDiv'></div>\n"
  236. + "</body></html>";
  237. final String[] expectedAlerts = {"3", "4", "456"};
  238. final List<String> collectedAlerts = new ArrayList<String>();
  239. loadPage(html, collectedAlerts);
  240. assertEquals(expectedAlerts, collectedAlerts);
  241. }
  242. /**
  243. * @throws Exception if an error occurs
  244. */
  245. @Test
  246. public void setTextContent() throws Exception {
  247. final String html = "<html><body><span id='s'>abc</span></body></html>";
  248. final HtmlPage page = loadPage(html);
  249. final DomText text = (DomText) page.getElementById("s").getFirstChild();
  250. assertEquals("abc", text.getTextContent());
  251. text.setTextContent("xyz");
  252. assertEquals("xyz", text.getTextContent());
  253. assertEquals("xyz", page.asText());
  254. }
  255. /**
  256. * Tests if {@code getCanonicalXPath()} returns the correct XPath for a text
  257. * node without other text node siblings.
  258. * @throws Exception if an error occurs
  259. */
  260. @Test
  261. public void getCanonicalXPath_withoutTextSiblings() throws Exception {
  262. final String html = "<html><body><span id='s'>abc</span></body></html>";
  263. final HtmlPage page = loadPage(html);
  264. final DomText text = (DomText) page.getElementById("s").getFirstChild();
  265. assertEquals("/html/body/span/text()", text.getCanonicalXPath());
  266. assertEquals(text, page.getFirstByXPath(text.getCanonicalXPath()));
  267. }
  268. /**
  269. * Tests if {@code getCanonicalXPath()} returns the correct XPath for a text
  270. * node with other text node siblings.
  271. * @throws Exception if an error occurs
  272. */
  273. @Test
  274. public void getCanonicalXPath_withTextSiblings() throws Exception {
  275. final String html = "<html><body><span id='s'>abc<br/>def</span></body></html>";
  276. final HtmlPage page = loadPage(html);
  277. final DomText text1 = (DomText) page.getElementById("s").getFirstChild();
  278. assertEquals("abc", text1.getData());
  279. assertEquals("/html/body/span/text()[1]", text1.getCanonicalXPath());
  280. assertEquals(text1, page.getFirstByXPath(text1.getCanonicalXPath()));
  281. final DomText text2 = (DomText) page.getElementById("s").getChildNodes().get(2);
  282. assertEquals("def", text2.getData());
  283. assertEquals("/html/body/span/text()[2]", text2.getCanonicalXPath());
  284. assertEquals(text2, page.getFirstByXPath(text2.getCanonicalXPath()));
  285. }
  286. }