1 /* 2 * Copyright (C) 2010 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.os.Parcel; 21 import android.platform.test.annotations.Presubmit; 22 import android.test.suitebuilder.annotation.SmallTest; 23 import android.text.TextUtils; 24 import android.view.accessibility.AccessibilityEvent; 25 import android.view.accessibility.AccessibilityNodeInfo; 26 import android.view.accessibility.AccessibilityRecord; 27 28 import junit.framework.TestCase; 29 30 /** 31 * Class for testing {@link AccessibilityEvent}. 32 */ 33 @Presubmit 34 public class AccessibilityEventTest extends TestCase { 35 /** 36 * Tests whether accessibility events are correctly written and 37 * read from a parcel (version 1). 38 */ 39 @SmallTest 40 public void testMarshaling() throws Exception { 41 // fully populate the event to marshal 42 AccessibilityEvent sentEvent = AccessibilityEvent.obtain(); 43 fullyPopulateAccessibilityEvent(sentEvent); 44 45 // marshal and unmarshal the event 46 Parcel parcel = Parcel.obtain(); 47 sentEvent.writeToParcel(parcel, 0); 48 parcel.setDataPosition(0); 49 AccessibilityEvent receivedEvent = AccessibilityEvent.CREATOR.createFromParcel(parcel); 50 51 // make sure all fields properly marshaled 52 assertEqualsAccessiblityEvent(sentEvent, receivedEvent); 53 54 parcel.recycle(); 55 } 56 57 /** 58 * Tests if {@link AccessibilityEvent} are properly reused. 59 */ 60 @SmallTest 61 public void testReuse() { 62 AccessibilityEvent firstEvent = AccessibilityEvent.obtain(); 63 firstEvent.recycle(); 64 AccessibilityEvent secondEvent = AccessibilityEvent.obtain(); 65 assertSame("AccessibilityEvent not properly reused", firstEvent, secondEvent); 66 } 67 68 /** 69 * Tests if {@link AccessibilityEvent} are properly recycled. 70 */ 71 @SmallTest 72 public void testRecycle() { 73 // obtain and populate an event 74 AccessibilityEvent populatedEvent = AccessibilityEvent.obtain(); 75 fullyPopulateAccessibilityEvent(populatedEvent); 76 77 // recycle and obtain the same recycled instance 78 populatedEvent.recycle(); 79 AccessibilityEvent recycledEvent = AccessibilityEvent.obtain(); 80 81 // check expectations 82 assertAccessibilityEventCleared(recycledEvent); 83 } 84 85 /** 86 * Tests whether the event types are correctly converted to strings. 87 */ 88 @SmallTest 89 public void testEventTypeToString() { 90 assertEquals("TYPE_NOTIFICATION_STATE_CHANGED", AccessibilityEvent.eventTypeToString( 91 AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)); 92 assertEquals("TYPE_TOUCH_EXPLORATION_GESTURE_END", AccessibilityEvent.eventTypeToString( 93 AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END)); 94 assertEquals("TYPE_TOUCH_EXPLORATION_GESTURE_START", AccessibilityEvent.eventTypeToString( 95 AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START)); 96 assertEquals("TYPE_VIEW_CLICKED", AccessibilityEvent.eventTypeToString( 97 AccessibilityEvent.TYPE_VIEW_CLICKED)); 98 assertEquals("TYPE_VIEW_FOCUSED", AccessibilityEvent.eventTypeToString( 99 AccessibilityEvent.TYPE_VIEW_FOCUSED)); 100 assertEquals("TYPE_VIEW_HOVER_ENTER", 101 AccessibilityEvent.eventTypeToString(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)); 102 assertEquals("TYPE_VIEW_HOVER_EXIT", AccessibilityEvent.eventTypeToString( 103 AccessibilityEvent.TYPE_VIEW_HOVER_EXIT)); 104 assertEquals("TYPE_VIEW_LONG_CLICKED", AccessibilityEvent.eventTypeToString( 105 AccessibilityEvent.TYPE_VIEW_LONG_CLICKED)); 106 assertEquals("TYPE_VIEW_CONTEXT_CLICKED", AccessibilityEvent.eventTypeToString( 107 AccessibilityEvent.TYPE_VIEW_CONTEXT_CLICKED)); 108 assertEquals("TYPE_VIEW_SCROLLED", AccessibilityEvent.eventTypeToString( 109 AccessibilityEvent.TYPE_VIEW_SCROLLED)); 110 assertEquals("TYPE_VIEW_SELECTED", AccessibilityEvent.eventTypeToString( 111 AccessibilityEvent.TYPE_VIEW_SELECTED)); 112 assertEquals("TYPE_VIEW_TEXT_CHANGED", AccessibilityEvent.eventTypeToString( 113 AccessibilityEvent .TYPE_VIEW_TEXT_CHANGED)); 114 assertEquals("TYPE_VIEW_TEXT_SELECTION_CHANGED", AccessibilityEvent.eventTypeToString( 115 AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED)); 116 assertEquals("TYPE_WINDOW_CONTENT_CHANGED", AccessibilityEvent.eventTypeToString( 117 AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)); 118 assertEquals("TYPE_WINDOW_STATE_CHANGED", AccessibilityEvent.eventTypeToString( 119 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)); 120 } 121 122 /** 123 * Tests whether the event describes its contents consistently. 124 */ 125 @SmallTest 126 public void testDescribeContents() { 127 AccessibilityEvent event = AccessibilityEvent.obtain(); 128 assertSame("Accessibility events always return 0 for this method.", 0, 129 event.describeContents()); 130 fullyPopulateAccessibilityEvent(event); 131 assertSame("Accessibility events always return 0 for this method.", 0, 132 event.describeContents()); 133 } 134 135 /** 136 * Tests whether accessibility events are correctly written and 137 * read from a parcel (version 2). 138 */ 139 @SmallTest 140 public void testMarshaling2() { 141 AccessibilityEvent marshaledEvent = AccessibilityEvent.obtain(); 142 fullyPopulateAccessibilityEvent(marshaledEvent); 143 Parcel parcel = Parcel.obtain(); 144 marshaledEvent.writeToParcel(parcel, 0); 145 parcel.setDataPosition(0); 146 AccessibilityEvent unmarshaledEvent = AccessibilityEvent.obtain(); 147 unmarshaledEvent.initFromParcel(parcel); 148 assertEqualsAccessiblityEvent(marshaledEvent, unmarshaledEvent); 149 150 parcel.recycle(); 151 } 152 153 /** 154 * While CharSequence is immutable, some classes implementing it are mutable. Make sure they 155 * can't change the object by changing the objects backing CharSequence 156 */ 157 @SmallTest 158 public void testChangeTextAfterSetting_shouldNotAffectEvent() { 159 final String originalText = "Cassowary"; 160 final String newText = "Hornbill"; 161 AccessibilityEvent event = AccessibilityEvent.obtain(); 162 StringBuffer updatingString = new StringBuffer(originalText); 163 event.setBeforeText(updatingString); 164 event.setContentDescription(updatingString); 165 166 updatingString.delete(0, updatingString.length()); 167 updatingString.append(newText); 168 169 assertTrue(TextUtils.equals(originalText, event.getBeforeText())); 170 assertTrue(TextUtils.equals(originalText, event.getContentDescription())); 171 } 172 173 /** 174 * Fully populates the {@link AccessibilityEvent} to marshal. 175 * 176 * @param sentEvent The event to populate. 177 */ 178 private void fullyPopulateAccessibilityEvent(AccessibilityEvent sentEvent) { 179 sentEvent.setAddedCount(1); 180 sentEvent.setBeforeText("BeforeText"); 181 sentEvent.setChecked(true); 182 sentEvent.setClassName("foo.bar.baz.Class"); 183 sentEvent.setContentDescription("ContentDescription"); 184 sentEvent.setCurrentItemIndex(1); 185 sentEvent.setEnabled(true); 186 sentEvent.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED); 187 sentEvent.setEventTime(1000); 188 sentEvent.setFromIndex(1); 189 sentEvent.setFullScreen(true); 190 sentEvent.setItemCount(1); 191 sentEvent.setPackageName("foo.bar.baz"); 192 sentEvent.setParcelableData(Message.obtain(null, 1, 2, 3)); 193 sentEvent.setPassword(true); 194 sentEvent.setRemovedCount(1); 195 sentEvent.getText().add("Foo"); 196 sentEvent.setMaxScrollX(1); 197 sentEvent.setMaxScrollY(1); 198 sentEvent.setScrollX(1); 199 sentEvent.setScrollY(1); 200 sentEvent.setScrollDeltaX(3); 201 sentEvent.setScrollDeltaY(3); 202 sentEvent.setToIndex(1); 203 sentEvent.setScrollable(true); 204 sentEvent.setAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS); 205 sentEvent.setMovementGranularity(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE); 206 207 AccessibilityRecord record = AccessibilityRecord.obtain(); 208 AccessibilityRecordTest.fullyPopulateAccessibilityRecord(record); 209 sentEvent.appendRecord(record); 210 } 211 212 213 /** 214 * Compares all properties of the <code>expectedEvent</code> and the 215 * <code>receviedEvent</code> to verify that the received event is the one 216 * that is expected. 217 */ 218 private static void assertEqualsAccessiblityEvent(AccessibilityEvent expectedEvent, 219 AccessibilityEvent receivedEvent) { 220 assertEquals("addedCount has incorrect value", expectedEvent.getAddedCount(), receivedEvent 221 .getAddedCount()); 222 assertEquals("beforeText has incorrect value", expectedEvent.getBeforeText(), receivedEvent 223 .getBeforeText()); 224 assertEquals("checked has incorrect value", expectedEvent.isChecked(), receivedEvent 225 .isChecked()); 226 assertEquals("className has incorrect value", expectedEvent.getClassName(), receivedEvent 227 .getClassName()); 228 assertEquals("contentDescription has incorrect value", expectedEvent 229 .getContentDescription(), receivedEvent.getContentDescription()); 230 assertEquals("currentItemIndex has incorrect value", expectedEvent.getCurrentItemIndex(), 231 receivedEvent.getCurrentItemIndex()); 232 assertEquals("enabled has incorrect value", expectedEvent.isEnabled(), receivedEvent 233 .isEnabled()); 234 assertEquals("eventType has incorrect value", expectedEvent.getEventType(), receivedEvent 235 .getEventType()); 236 assertEquals("fromIndex has incorrect value", expectedEvent.getFromIndex(), receivedEvent 237 .getFromIndex()); 238 assertEquals("fullScreen has incorrect value", expectedEvent.isFullScreen(), receivedEvent 239 .isFullScreen()); 240 assertEquals("itemCount has incorrect value", expectedEvent.getItemCount(), receivedEvent 241 .getItemCount()); 242 assertEquals("password has incorrect value", expectedEvent.isPassword(), receivedEvent 243 .isPassword()); 244 assertEquals("removedCount has incorrect value", expectedEvent.getRemovedCount(), 245 receivedEvent.getRemovedCount()); 246 AccessibilityRecordTest.assertEqualsText(expectedEvent.getText(), receivedEvent.getText()); 247 assertEquals("must have one record", expectedEvent.getRecordCount(), 248 receivedEvent.getRecordCount()); 249 assertSame("maxScrollX has incorect value", expectedEvent.getMaxScrollX(), 250 receivedEvent.getMaxScrollX()); 251 assertSame("maxScrollY has incorect value", expectedEvent.getMaxScrollY(), 252 receivedEvent.getMaxScrollY()); 253 assertSame("scrollX has incorect value", expectedEvent.getScrollX(), 254 receivedEvent.getScrollX()); 255 assertSame("scrollY has incorect value", expectedEvent.getScrollY(), 256 receivedEvent.getScrollY()); 257 assertSame("scrollDeltaX has incorect value", expectedEvent.getScrollDeltaX(), 258 receivedEvent.getScrollDeltaX()); 259 assertSame("scrollDeltaY has incorect value", expectedEvent.getScrollDeltaY(), 260 receivedEvent.getScrollDeltaY()); 261 assertSame("toIndex has incorect value", expectedEvent.getToIndex(), 262 receivedEvent.getToIndex()); 263 assertSame("scrollable has incorect value", expectedEvent.isScrollable(), 264 receivedEvent.isScrollable()); 265 assertSame("granularity has incorect value", expectedEvent.getMovementGranularity(), 266 receivedEvent.getMovementGranularity()); 267 assertSame("action has incorect value", expectedEvent.getAction(), 268 receivedEvent.getAction()); 269 assertSame("windowChangeTypes has incorect value", expectedEvent.getWindowChanges(), 270 receivedEvent.getWindowChanges()); 271 272 assertSame("parcelableData has incorect value", 273 ((Message) expectedEvent.getParcelableData()).what, 274 ((Message) receivedEvent.getParcelableData()).what); 275 276 AccessibilityRecord receivedRecord = receivedEvent.getRecord(0); 277 AccessibilityRecordTest.assertEqualAccessibilityRecord(expectedEvent, receivedRecord); 278 } 279 280 /** 281 * Asserts that an {@link AccessibilityEvent} is cleared. 282 * 283 * @param event The event to check. 284 */ 285 private static void assertAccessibilityEventCleared(AccessibilityEvent event) { 286 AccessibilityRecordTest.assertAccessibilityRecordCleared(event); 287 TestCase.assertEquals("eventTime not properly recycled", 0, event.getEventTime()); 288 TestCase.assertEquals("eventType not properly recycled", 0, event.getEventType()); 289 TestCase.assertNull("packageName not properly recycled", event.getPackageName()); 290 } 291 } 292