PageRenderTime 35ms CodeModel.GetById 23ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/AccessCheck/src/com/android/accessibility/AccessibilityValidatorTest.java

http://eyes-free.googlecode.com/
Java | 113 lines | 70 code | 15 blank | 28 comment | 5 complexity | 3e153e1a3f2d7b484be4d57a009b1535 MD5 | raw file
  1/*
  2 * Copyright 2010 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.android.accessibility;
 18
 19import junit.framework.TestCase;
 20
 21import org.xml.sax.InputSource;
 22
 23import java.io.StringReader;
 24import java.util.ArrayList;
 25import java.util.logging.Logger;
 26
 27/**
 28 * Unit tests for the AccessibilityValidator object.
 29 * 
 30 * @author dtseng@google.com (David Tseng)
 31 */
 32public class AccessibilityValidatorTest extends TestCase {
 33    private AccessibilityValidator mObjectUnderTest;
 34    private String mMockFile = "";
 35    private String mAndroidSdkPath;
 36    private static final String ANDROID_FILE_PATH_PROPERTY = "androidSdkPath";
 37    private static final Logger sLogger =
 38            Logger.getLogger("android.accessibility.test");
 39
 40    @Override
 41    protected void setUp() throws Exception {
 42        super.setUp();
 43        mAndroidSdkPath = System.getProperty(ANDROID_FILE_PATH_PROPERTY);
 44        sLogger.info(mAndroidSdkPath + " set as android sdk path");
 45    }
 46
 47    /* Positive unit tests */
 48    public void testRun_validationOnValidPathAndValidXmlFile() {
 49        mMockFile = "<html>well formed</html>";
 50        assertTrue(mockValidateFiles("./"));
 51        assertTrue(mockValidateFiles("/tmp"));
 52    }
 53
 54    public void testRun_onXmlLayoutFileWithNoValidationErrors() {
 55        mMockFile = "<ImageView anroid=\"value1\" "
 56                + "androidAi=\"value2\" contentDescription=\"hi\" />";
 57        assertTrue(mockValidateFiles("./"));
 58        assertTrue(mObjectUnderTest.getTotalValidationErrors() == 0);
 59    }
 60
 61    /* negative unit tests */
 62    public void testRun_onBadFilePath() {
 63        assertFalse(mockValidateFiles(""));
 64        assertFalse(mockValidateFiles(null));
 65    }
 66
 67    public void testRun_onBadXmlLayoutFile() {
 68        mMockFile = "Not well formed";
 69        assertFalse(mockValidateFiles("~/"));
 70    }
 71
 72    public void testRun_onXmlLayoutFileWithValidationErrors() {
 73        mMockFile = "<ImageView anroid=\"value1\" androidAi=\"value2\" />";
 74        assertTrue(mockValidateFiles("./"));
 75        assertTrue(mObjectUnderTest.getTotalValidationErrors() == 1);
 76    }
 77
 78    public void testRun_onXmlLayoutWithTwoValidationErrors() {
 79        mMockFile = "<html>"
 80                + "<ImageView anroid=\"value1\" androidAi=\"value2\" />"
 81                + "<ImageView asdf=\"a\" />"
 82                + "<ImageView contentDescription=\"a\" />" + "</html>";
 83        assertTrue(mockValidateFiles("./"));
 84        assertTrue(mObjectUnderTest.getTotalValidationErrors() == 2);
 85    }
 86
 87    /**
 88     * returns whether run has no general errors. General errors are caught
 89     * exceptions. We also look for NullPointerException and
 90     * IllegalArgumentExceptions as uncaught exceptions. We want the program to
 91     * crash in these cases.
 92     */
 93    private boolean mockValidateFiles(String filePath) {
 94        try {
 95            mObjectUnderTest = new AccessibilityValidator(filePath,
 96                    mAndroidSdkPath);
 97        } catch (IllegalArgumentException iAException) {
 98            return false;
 99        } catch (NullPointerException npExcepption) {
100            return false;
101        }
102
103        mObjectUnderTest.setLayoutFiles(new ArrayList<InputSource>());
104        mObjectUnderTest.getLayoutFiles().add(
105                new InputSource(new StringReader(mMockFile)));
106        mObjectUnderTest.validateFiles();
107        for (String msg : mObjectUnderTest.getGeneralErrors()) {
108            sLogger.info("Caught error: " + msg);
109        }
110
111        return mObjectUnderTest.getGeneralErrors().size() == 0;
112    }
113}