/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. package com.android.accessibility;
  17. import junit.framework.TestCase;
  18. import org.xml.sax.InputSource;
  19. import java.io.StringReader;
  20. import java.util.ArrayList;
  21. import java.util.logging.Logger;
  22. /**
  23. * Unit tests for the AccessibilityValidator object.
  24. *
  25. * @author dtseng@google.com (David Tseng)
  26. */
  27. public class AccessibilityValidatorTest extends TestCase {
  28. private AccessibilityValidator mObjectUnderTest;
  29. private String mMockFile = "";
  30. private String mAndroidSdkPath;
  31. private static final String ANDROID_FILE_PATH_PROPERTY = "androidSdkPath";
  32. private static final Logger sLogger =
  33. Logger.getLogger("android.accessibility.test");
  34. @Override
  35. protected void setUp() throws Exception {
  36. super.setUp();
  37. mAndroidSdkPath = System.getProperty(ANDROID_FILE_PATH_PROPERTY);
  38. sLogger.info(mAndroidSdkPath + " set as android sdk path");
  39. }
  40. /* Positive unit tests */
  41. public void testRun_validationOnValidPathAndValidXmlFile() {
  42. mMockFile = "<html>well formed</html>";
  43. assertTrue(mockValidateFiles("./"));
  44. assertTrue(mockValidateFiles("/tmp"));
  45. }
  46. public void testRun_onXmlLayoutFileWithNoValidationErrors() {
  47. mMockFile = "<ImageView anroid=\"value1\" "
  48. + "androidAi=\"value2\" contentDescription=\"hi\" />";
  49. assertTrue(mockValidateFiles("./"));
  50. assertTrue(mObjectUnderTest.getTotalValidationErrors() == 0);
  51. }
  52. /* negative unit tests */
  53. public void testRun_onBadFilePath() {
  54. assertFalse(mockValidateFiles(""));
  55. assertFalse(mockValidateFiles(null));
  56. }
  57. public void testRun_onBadXmlLayoutFile() {
  58. mMockFile = "Not well formed";
  59. assertFalse(mockValidateFiles("~/"));
  60. }
  61. public void testRun_onXmlLayoutFileWithValidationErrors() {
  62. mMockFile = "<ImageView anroid=\"value1\" androidAi=\"value2\" />";
  63. assertTrue(mockValidateFiles("./"));
  64. assertTrue(mObjectUnderTest.getTotalValidationErrors() == 1);
  65. }
  66. public void testRun_onXmlLayoutWithTwoValidationErrors() {
  67. mMockFile = "<html>"
  68. + "<ImageView anroid=\"value1\" androidAi=\"value2\" />"
  69. + "<ImageView asdf=\"a\" />"
  70. + "<ImageView contentDescription=\"a\" />" + "</html>";
  71. assertTrue(mockValidateFiles("./"));
  72. assertTrue(mObjectUnderTest.getTotalValidationErrors() == 2);
  73. }
  74. /**
  75. * returns whether run has no general errors. General errors are caught
  76. * exceptions. We also look for NullPointerException and
  77. * IllegalArgumentExceptions as uncaught exceptions. We want the program to
  78. * crash in these cases.
  79. */
  80. private boolean mockValidateFiles(String filePath) {
  81. try {
  82. mObjectUnderTest = new AccessibilityValidator(filePath,
  83. mAndroidSdkPath);
  84. } catch (IllegalArgumentException iAException) {
  85. return false;
  86. } catch (NullPointerException npExcepption) {
  87. return false;
  88. }
  89. mObjectUnderTest.setLayoutFiles(new ArrayList<InputSource>());
  90. mObjectUnderTest.getLayoutFiles().add(
  91. new InputSource(new StringReader(mMockFile)));
  92. mObjectUnderTest.validateFiles();
  93. for (String msg : mObjectUnderTest.getGeneralErrors()) {
  94. sLogger.info("Caught error: " + msg);
  95. }
  96. return mObjectUnderTest.getGeneralErrors().size() == 0;
  97. }
  98. }