1 /* 2 * Copyright (C) 2012 The Android Open Source Project 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 * 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view.accessibility.cts; 18 19 import android.os.Message; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.view.accessibility.AccessibilityEvent; 23 import android.view.accessibility.AccessibilityRecord; 24 25 import junit.framework.TestCase; 26 27 import java.lang.reflect.Field; 28 import java.lang.reflect.Modifier; 29 import java.util.Iterator; 30 import java.util.List; 31 32 /** 33 * Class for testing {@link AccessibilityRecord}. 34 */ 35 public class AccessibilityRecordTest extends AndroidTestCase { 36 37 /** The number of properties of the {@link AccessibilityEvent} class. */ 38 private static final int NON_STATIC_FIELD_COUNT = 22; 39 40 /** 41 * Test that no new fields have been added without updating the 42 * marshaling tests. Note that the marshaling tests are in 43 * AccessibilityEventTests since it is a super class that is 44 * responsible for marshaling and unmarshaling. 45 */ 46 @SmallTest 47 public void testNoNewFieldsAddedWithoutUpdadingMarshallTests() { 48 assertNoNewNonStaticFieldsAdded(AccessibilityRecord.class, NON_STATIC_FIELD_COUNT); 49 } 50 51 /** 52 * Tests the cloning obtain method. 53 */ 54 @SmallTest 55 public void testObtain() { 56 AccessibilityRecord originalRecord = AccessibilityRecord.obtain(); 57 fullyPopulateAccessibilityRecord(originalRecord); 58 AccessibilityRecord cloneRecord = AccessibilityRecord.obtain(originalRecord); 59 assertEqualAccessibilityRecord(originalRecord, cloneRecord); 60 } 61 62 /** 63 * Tests if {@link AccessibilityRecord}s are properly recycled. 64 */ 65 @SmallTest 66 public void testRecycle() { 67 // obtain and populate an event 68 AccessibilityRecord populatedRecord = AccessibilityRecord.obtain(); 69 fullyPopulateAccessibilityRecord(populatedRecord); 70 71 // recycle and obtain the same recycled instance 72 populatedRecord.recycle(); 73 AccessibilityRecord recycledRecord = AccessibilityRecord.obtain(); 74 75 // check expectations 76 assertAccessibilityRecordCleared(recycledRecord); 77 } 78 79 /** 80 * Asserts that an {@link AccessibilityRecord} is cleared. 81 */ 82 static void assertAccessibilityRecordCleared(AccessibilityRecord record) { 83 TestCase.assertEquals("addedCount not properly recycled", -1, record.getAddedCount()); 84 TestCase.assertNull("beforeText not properly recycled", record.getBeforeText()); 85 TestCase.assertFalse("checked not properly recycled", record.isChecked()); 86 TestCase.assertNull("className not properly recycled", record.getClassName()); 87 TestCase.assertNull("contentDescription not properly recycled", 88 record.getContentDescription()); 89 TestCase.assertEquals("currentItemIndex not properly recycled", -1, 90 record.getCurrentItemIndex()); 91 TestCase.assertFalse("enabled not properly recycled", record.isEnabled()); 92 TestCase.assertEquals("fromIndex not properly recycled", -1, record.getFromIndex()); 93 TestCase.assertFalse("fullScreen not properly recycled", record.isFullScreen()); 94 TestCase.assertEquals("itemCount not properly recycled", -1, record.getItemCount()); 95 TestCase.assertNull("parcelableData not properly recycled", record.getParcelableData()); 96 TestCase.assertFalse("password not properly recycled", record.isPassword()); 97 TestCase.assertEquals("removedCount not properly recycled", -1, record.getRemovedCount()); 98 TestCase.assertTrue("text not properly recycled", record.getText().isEmpty()); 99 TestCase.assertFalse("scrollable not properly recycled", record.isScrollable()); 100 TestCase.assertSame("maxScrollX not properly recycled", -1, record.getMaxScrollX()); 101 TestCase.assertSame("maxScrollY not properly recycled", -1, record.getMaxScrollY()); 102 TestCase.assertSame("scrollX not properly recycled", -1, record.getScrollX()); 103 TestCase.assertSame("scrollY not properly recycled", -1, record.getScrollY()); 104 TestCase.assertSame("toIndex not properly recycled", -1, record.getToIndex()); 105 } 106 107 /** 108 * Fully populates the {@link AccessibilityRecord}. 109 * 110 * @param record The record to populate. 111 */ 112 static void fullyPopulateAccessibilityRecord(AccessibilityRecord record) { 113 record.setAddedCount(1); 114 record.setBeforeText("BeforeText"); 115 record.setChecked(true); 116 record.setClassName("foo.bar.baz.Class"); 117 record.setContentDescription("ContentDescription"); 118 record.setCurrentItemIndex(1); 119 record.setEnabled(true); 120 record.setFromIndex(1); 121 record.setFullScreen(true); 122 record.setItemCount(1); 123 record.setParcelableData(Message.obtain(null, 1, 2, 3)); 124 record.setPassword(true); 125 record.setRemovedCount(1); 126 record.getText().add("Foo"); 127 record.setMaxScrollX(1); 128 record.setMaxScrollY(1); 129 record.setScrollX(1); 130 record.setScrollY(1); 131 record.setToIndex(1); 132 record.setScrollable(true); 133 } 134 135 static void assertEqualAccessibilityRecord(AccessibilityRecord expectedRecord, 136 AccessibilityRecord receivedRecord) { 137 assertEquals("addedCount has incorrect value", expectedRecord.getAddedCount(), 138 receivedRecord.getAddedCount()); 139 assertEquals("beforeText has incorrect value", expectedRecord.getBeforeText(), 140 receivedRecord.getBeforeText()); 141 assertEquals("checked has incorrect value", expectedRecord.isChecked(), 142 receivedRecord.isChecked()); 143 assertEquals("className has incorrect value", expectedRecord.getClassName(), 144 receivedRecord.getClassName()); 145 assertEquals("contentDescription has incorrect value", 146 expectedRecord.getContentDescription(), receivedRecord.getContentDescription()); 147 assertEquals("currentItemIndex has incorrect value", expectedRecord.getCurrentItemIndex(), 148 receivedRecord.getCurrentItemIndex()); 149 assertEquals("enabled has incorrect value", expectedRecord.isEnabled(), 150 receivedRecord.isEnabled()); 151 assertEquals("fromIndex has incorrect value", expectedRecord.getFromIndex(), 152 receivedRecord.getFromIndex()); 153 assertEquals("fullScreen has incorrect value", expectedRecord.isFullScreen(), 154 receivedRecord.isFullScreen()); 155 assertEquals("itemCount has incorrect value", expectedRecord.getItemCount(), 156 receivedRecord.getItemCount()); 157 assertEquals("password has incorrect value", expectedRecord.isPassword(), 158 receivedRecord.isPassword()); 159 assertEquals("removedCount has incorrect value", expectedRecord.getRemovedCount(), 160 receivedRecord.getRemovedCount()); 161 assertEqualsText(expectedRecord.getText(), receivedRecord.getText()); 162 assertSame("maxScrollX has incorect value", expectedRecord.getMaxScrollX(), 163 receivedRecord.getMaxScrollX()); 164 assertSame("maxScrollY has incorect value", expectedRecord.getMaxScrollY(), 165 receivedRecord.getMaxScrollY()); 166 assertSame("scrollX has incorect value", expectedRecord.getScrollX(), 167 receivedRecord.getScrollX()); 168 assertSame("scrollY has incorect value", expectedRecord.getScrollY(), 169 receivedRecord.getScrollY()); 170 assertSame("toIndex has incorect value", expectedRecord.getToIndex(), 171 receivedRecord.getToIndex()); 172 assertSame("scrollable has incorect value", expectedRecord.isScrollable(), 173 receivedRecord.isScrollable()); 174 assertSame("parcelableData has incorect value", 175 ((Message) expectedRecord.getParcelableData()).what, 176 ((Message) receivedRecord.getParcelableData()).what); 177 } 178 179 /** 180 * Compares the text of the <code>expectedEvent</code> and 181 * <code>receivedEvent</code> by comparing the string representation of the 182 * corresponding {@link CharSequence}s. 183 */ 184 static void assertEqualsText(List<CharSequence> expectedText, 185 List<CharSequence> receivedText ) { 186 String message = "text has incorrect value"; 187 188 TestCase.assertEquals(message, expectedText.size(), receivedText.size()); 189 190 Iterator<CharSequence> expectedTextIterator = expectedText.iterator(); 191 Iterator<CharSequence> receivedTextIterator = receivedText.iterator(); 192 193 for (int i = 0; i < expectedText.size(); i++) { 194 // compare the string representation 195 TestCase.assertEquals(message, expectedTextIterator.next().toString(), 196 receivedTextIterator.next().toString()); 197 } 198 } 199 200 /** 201 * Asserts that no new fields have been added, so we are testing marshaling 202 * of all such. 203 */ 204 static void assertNoNewNonStaticFieldsAdded(Class<?> clazz, int expectedCount) { 205 int nonStaticFieldCount = 0; 206 207 while (clazz != Object.class) { 208 for (Field field : clazz.getDeclaredFields()) { 209 if ((field.getModifiers() & Modifier.STATIC) == 0) { 210 nonStaticFieldCount++; 211 } 212 } 213 clazz = clazz.getSuperclass(); 214 } 215 216 String message = "New fields have been added, so add code to test marshaling them."; 217 assertEquals(message, expectedCount, nonStaticFieldCount); 218 } 219 } 220