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.accessibilityservice.cts; 18 19 import android.accessibilityservice.AccessibilityServiceInfo; 20 import android.os.Parcel; 21 import android.platform.test.annotations.Presubmit; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import android.view.accessibility.AccessibilityEvent; 25 26 /** 27 * Class for testing {@link AccessibilityServiceInfo}. 28 */ 29 @Presubmit 30 public class AccessibilityServiceInfoTest extends AndroidTestCase { 31 32 @MediumTest 33 public void testMarshalling() throws Exception { 34 35 // fully populate the service info to marshal 36 AccessibilityServiceInfo sentInfo = new AccessibilityServiceInfo(); 37 fullyPopulateSentAccessibilityServiceInfo(sentInfo); 38 39 // marshal and unmarshal the service info 40 Parcel parcel = Parcel.obtain(); 41 sentInfo.writeToParcel(parcel, 0); 42 parcel.setDataPosition(0); 43 AccessibilityServiceInfo receivedInfo = AccessibilityServiceInfo.CREATOR 44 .createFromParcel(parcel); 45 46 // make sure all fields properly marshaled 47 assertAllFieldsProperlyMarshalled(sentInfo, receivedInfo); 48 } 49 50 /** 51 * Tests whether the service info describes its contents consistently. 52 */ 53 @MediumTest 54 public void testDescribeContents() { 55 AccessibilityServiceInfo info = new AccessibilityServiceInfo(); 56 assertSame("Accessibility service info always return 0 for this method.", 0, 57 info.describeContents()); 58 fullyPopulateSentAccessibilityServiceInfo(info); 59 assertSame("Accessibility service infos always return 0 for this method.", 0, 60 info.describeContents()); 61 } 62 63 /** 64 * Tests whether a feedback type is correctly transformed to a string. 65 */ 66 @MediumTest 67 public void testFeedbackTypeToString() { 68 assertEquals("[FEEDBACK_AUDIBLE]", AccessibilityServiceInfo.feedbackTypeToString( 69 AccessibilityServiceInfo.FEEDBACK_AUDIBLE)); 70 assertEquals("[FEEDBACK_GENERIC]", AccessibilityServiceInfo.feedbackTypeToString( 71 AccessibilityServiceInfo.FEEDBACK_GENERIC)); 72 assertEquals("[FEEDBACK_HAPTIC]", AccessibilityServiceInfo.feedbackTypeToString( 73 AccessibilityServiceInfo.FEEDBACK_HAPTIC)); 74 assertEquals("[FEEDBACK_SPOKEN]", AccessibilityServiceInfo.feedbackTypeToString( 75 AccessibilityServiceInfo.FEEDBACK_SPOKEN)); 76 assertEquals("[FEEDBACK_VISUAL]", AccessibilityServiceInfo.feedbackTypeToString( 77 AccessibilityServiceInfo.FEEDBACK_VISUAL)); 78 assertEquals("[FEEDBACK_BRAILLE]", AccessibilityServiceInfo.feedbackTypeToString( 79 AccessibilityServiceInfo.FEEDBACK_BRAILLE)); 80 assertEquals("[FEEDBACK_SPOKEN, FEEDBACK_HAPTIC, FEEDBACK_AUDIBLE, FEEDBACK_VISUAL," 81 + " FEEDBACK_GENERIC, FEEDBACK_BRAILLE]", 82 AccessibilityServiceInfo.feedbackTypeToString( 83 AccessibilityServiceInfo.FEEDBACK_ALL_MASK)); 84 } 85 86 /** 87 * Tests whether a flag is correctly transformed to a string. 88 */ 89 @MediumTest 90 public void testFlagToString() { 91 assertEquals("DEFAULT", AccessibilityServiceInfo.flagToString( 92 AccessibilityServiceInfo.DEFAULT)); 93 assertEquals("FLAG_INCLUDE_NOT_IMPORTANT_VIEWS", AccessibilityServiceInfo.flagToString( 94 AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS)); 95 assertEquals("FLAG_REPORT_VIEW_IDS", AccessibilityServiceInfo.flagToString( 96 AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS)); 97 assertEquals("FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY", AccessibilityServiceInfo 98 .flagToString(AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY)); 99 assertEquals("FLAG_REQUEST_FILTER_KEY_EVENTS", AccessibilityServiceInfo.flagToString( 100 AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS)); 101 assertEquals("FLAG_REQUEST_TOUCH_EXPLORATION_MODE", AccessibilityServiceInfo.flagToString( 102 AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE)); 103 } 104 105 /** 106 * Fully populates the {@link AccessibilityServiceInfo} to marshal. 107 * 108 * @param sentInfo The service info to populate. 109 */ 110 private void fullyPopulateSentAccessibilityServiceInfo(AccessibilityServiceInfo sentInfo) { 111 sentInfo.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED; 112 sentInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN; 113 sentInfo.flags = AccessibilityServiceInfo.DEFAULT; 114 sentInfo.notificationTimeout = 1000; 115 sentInfo.packageNames = new String[] { 116 "foo.bar.baz" 117 }; 118 } 119 120 /** 121 * Compares all properties of the <code>sentInfo</code> and the 122 * <code>receviedInfo</code> to make sure marshaling is correctly 123 * implemented. 124 */ 125 private void assertAllFieldsProperlyMarshalled(AccessibilityServiceInfo sentInfo, 126 AccessibilityServiceInfo receivedInfo) { 127 assertEquals("eventTypes not marshalled properly", sentInfo.eventTypes, 128 receivedInfo.eventTypes); 129 assertEquals("feedbackType not marshalled properly", sentInfo.feedbackType, 130 receivedInfo.feedbackType); 131 assertEquals("flags not marshalled properly", sentInfo.flags, receivedInfo.flags); 132 assertEquals("notificationTimeout not marshalled properly", sentInfo.notificationTimeout, 133 receivedInfo.notificationTimeout); 134 assertEquals("packageNames not marshalled properly", sentInfo.packageNames.length, 135 receivedInfo.packageNames.length); 136 assertEquals("packageNames not marshalled properly", sentInfo.packageNames[0], 137 receivedInfo.packageNames[0]); 138 } 139 } 140