PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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