/talkback_preics/src/com/google/android/marvin/talkback/formatter/WebContentHandlerTest.java

http://eyes-free.googlecode.com/ · Java · 135 lines · 77 code · 24 blank · 34 comment · 0 complexity · 1307912c904cf91770f3a83b9b057056 MD5 · raw file

  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. package com.google.android.marvin.talkback.formatter;
  3. import org.xml.sax.SAXException;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import javax.xml.parsers.SAXParser;
  8. import javax.xml.parsers.SAXParserFactory;
  9. import junit.framework.TestCase;
  10. /**
  11. * @author credo@google.com (Tim Credo)
  12. */
  13. public class WebContentHandlerTest extends TestCase {
  14. private static final String XML_TEMPLATE =
  15. "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><div>%s</div>";
  16. HashMap<String, String> htmlInputMap;
  17. HashMap<String, String> htmlRoleMap;
  18. HashMap<String, String> htmlTagMap;
  19. WebContentHandler mHandler;
  20. SAXParser mParser;
  21. @Override
  22. protected void setUp() throws Exception {
  23. super.setUp();
  24. SAXParserFactory factory = SAXParserFactory.newInstance();
  25. mParser = factory.newSAXParser();
  26. HashMap<String, String> htmlInputMap = new HashMap<String, String>();
  27. htmlInputMap.put("button", "Button");
  28. htmlInputMap.put("text", "Edit text");
  29. htmlInputMap.put("checkbox", "Check box");
  30. htmlInputMap.put("radio", "Radio button");
  31. HashMap<String, String> htmlRoleMap = new HashMap<String, String>();
  32. htmlRoleMap.put("checkbox", "Check box");
  33. htmlRoleMap.put("link", "Link");
  34. HashMap<String, String> htmlTagMap = new HashMap<String, String>();
  35. htmlTagMap.put("a", "Link");
  36. htmlTagMap.put("button", "Button");
  37. htmlTagMap.put("h2", "Heading two");
  38. htmlTagMap.put("textarea", "Text area");
  39. mHandler = new WebContentHandler(htmlInputMap, htmlRoleMap, htmlTagMap);
  40. }
  41. /**
  42. * A helper function to make it easy to parse strings with the handler.
  43. */
  44. public void parse(String xmlString) throws SAXException, IOException {
  45. String xml = String.format(XML_TEMPLATE,xmlString);
  46. mParser.parse(new ByteArrayInputStream(xml.getBytes()), mHandler);
  47. }
  48. /**
  49. * Parsing an unknown tag should cause the handler to ignore it when
  50. * producing output.
  51. */
  52. public void testUnknownTag() throws Exception {
  53. parse("<unknownTag>Foo</unknownTag>");
  54. assertEquals("Foo", mHandler.getOutput());
  55. }
  56. /**
  57. * Content should have a space inserted if necessary to preserve separation
  58. * after tags are removed.
  59. */
  60. public void testFixWhitespace() throws Exception {
  61. parse("<tag>Hello,</tag><tag>world!</tag>");
  62. assertEquals("Hello, world!", mHandler.getOutput());
  63. parse("<tag>Hello,</tag> <tag>world!</tag>");
  64. assertEquals("Hello, world!", mHandler.getOutput());
  65. }
  66. /**
  67. * Input type should be spoken after the content of the element.
  68. */
  69. public void testInputType() throws Exception {
  70. parse("<input type=\"button\"/>");
  71. assertEquals("Button", mHandler.getOutput());
  72. }
  73. /**
  74. * ARIA roles should be spoken after the content of the element.
  75. */
  76. public void testAriaRole() throws Exception {
  77. parse("<span role=\"link\">Foo</span>");
  78. assertEquals("Foo Link", mHandler.getOutput());
  79. }
  80. /**
  81. * Tags appearing in the tag map should be described to the user.
  82. */
  83. public void testTagMap() throws Exception {
  84. parse("<h2>Foo</h2> <a>Bar</a>");
  85. assertEquals("Foo Heading two Bar Link", mHandler.getOutput());
  86. }
  87. /**
  88. * For most input types, the value of the element should be spoken. There
  89. * are two exceptions which are tested below.
  90. */
  91. public void testInputValue() throws Exception {
  92. parse("<input type=\"button\" value=\"Search\"/>");
  93. assertEquals("Search Button", mHandler.getOutput());
  94. parse("<textarea value=\"Content here\"/>");
  95. assertEquals("Content here Text area", mHandler.getOutput());
  96. }
  97. /**
  98. * The value attribute of a checkbox input element should not be spoken.
  99. */
  100. public void testCheckboxValue() throws Exception {
  101. parse("<input type=\"checkbox\" value=\"do not speak\"/>");
  102. assertEquals("Check box", mHandler.getOutput());
  103. }
  104. /**
  105. * The value attribute of a radio input element should not be spoken.
  106. */
  107. public void testRadioValue() throws Exception {
  108. parse("<input type=\"radio\" value=\"do not speak\"/>");
  109. assertEquals("Radio button", mHandler.getOutput());
  110. }
  111. }