Phil Weaver | bb2f28a | 2017-12-22 09:44:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertFalse; |
| 21 | import static junit.framework.Assert.assertTrue; |
| 22 | |
| 23 | import android.os.Parcel; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | |
Phil Weaver | 9e418b8 | 2018-04-03 11:02:52 -0700 | [diff] [blame^] | 29 | import java.lang.reflect.Field; |
| 30 | import java.lang.reflect.Modifier; |
| 31 | |
Phil Weaver | bb2f28a | 2017-12-22 09:44:28 -0800 | [diff] [blame] | 32 | /** |
| 33 | * AccessibilityEvent is public, so CTS covers it pretty well. Verifying hidden methods here. |
| 34 | */ |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | public class AccessibilityEventTest { |
Phil Weaver | 9e418b8 | 2018-04-03 11:02:52 -0700 | [diff] [blame^] | 37 | // The number of fields tested in the corresponding CTS AccessibilityEventTest: |
| 38 | // See the CTS tests for AccessibilityRecord: |
| 39 | // fullyPopulateAccessibilityEvent, assertEqualsAccessiblityEvent, |
| 40 | // and assertAccessibilityEventCleared |
| 41 | |
| 42 | /** The number of properties of the {@link AccessibilityEvent} class. */ |
| 43 | private static final int A11Y_EVENT_NON_STATIC_FIELD_COUNT = 32; |
| 44 | |
| 45 | // The number of fields tested in the corresponding CTS AccessibilityRecordTest: |
| 46 | // assertAccessibilityRecordCleared, fullyPopulateAccessibilityRecord, |
| 47 | // and assertEqualAccessibilityRecord |
| 48 | |
| 49 | /** The number of properties of the {@link AccessibilityRecord} class. */ |
| 50 | private static final int A11Y_RECORD_NON_STATIC_FIELD_COUNT = 24; |
| 51 | |
Phil Weaver | bb2f28a | 2017-12-22 09:44:28 -0800 | [diff] [blame] | 52 | @Test |
| 53 | public void testImportantForAccessibiity_getSetWorkAcrossParceling() { |
| 54 | AccessibilityEvent event = AccessibilityEvent.obtain(); |
| 55 | event.setImportantForAccessibility(true); |
| 56 | assertTrue(copyEventViaParcel(event).isImportantForAccessibility()); |
| 57 | |
| 58 | event.setImportantForAccessibility(false); |
| 59 | assertFalse(copyEventViaParcel(event).isImportantForAccessibility()); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testSouceNodeId_getSetWorkAcrossParceling() { |
| 64 | final long sourceNodeId = 0x1234567890ABCDEFL; |
| 65 | AccessibilityEvent event = AccessibilityEvent.obtain(); |
| 66 | event.setSourceNodeId(sourceNodeId); |
| 67 | assertEquals(sourceNodeId, copyEventViaParcel(event).getSourceNodeId()); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testWindowChanges_getSetWorkAcrossParceling() { |
| 72 | final int windowChanges = AccessibilityEvent.WINDOWS_CHANGE_TITLE |
| 73 | | AccessibilityEvent.WINDOWS_CHANGE_ACTIVE |
| 74 | | AccessibilityEvent.WINDOWS_CHANGE_FOCUSED; |
| 75 | AccessibilityEvent event = AccessibilityEvent.obtain(); |
| 76 | event.setWindowChanges(windowChanges); |
| 77 | assertEquals(windowChanges, copyEventViaParcel(event).getWindowChanges()); |
| 78 | } |
| 79 | |
Phil Weaver | 9e418b8 | 2018-04-03 11:02:52 -0700 | [diff] [blame^] | 80 | @Test |
| 81 | public void dontForgetToUpdateA11yRecordCtsParcelingTestWhenYouAddNewFields() { |
| 82 | AccessibilityEventTest.assertNoNewNonStaticFieldsAdded( |
| 83 | AccessibilityRecord.class, A11Y_RECORD_NON_STATIC_FIELD_COUNT); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void dontForgetToUpdateA11yEventCtsParcelingTestWhenYouAddNewFields() { |
| 88 | AccessibilityEventTest.assertNoNewNonStaticFieldsAdded( |
| 89 | AccessibilityEvent.class, A11Y_EVENT_NON_STATIC_FIELD_COUNT); |
| 90 | } |
| 91 | |
Phil Weaver | bb2f28a | 2017-12-22 09:44:28 -0800 | [diff] [blame] | 92 | private AccessibilityEvent copyEventViaParcel(AccessibilityEvent event) { |
| 93 | Parcel parcel = Parcel.obtain(); |
| 94 | event.writeToParcel(parcel, 0); |
| 95 | parcel.setDataPosition(0); |
| 96 | return AccessibilityEvent.CREATOR.createFromParcel(parcel); |
| 97 | } |
Phil Weaver | 9e418b8 | 2018-04-03 11:02:52 -0700 | [diff] [blame^] | 98 | |
| 99 | /** |
| 100 | * Asserts that no new fields have been added, so we are testing marshaling |
| 101 | * of all such. |
| 102 | */ |
| 103 | static void assertNoNewNonStaticFieldsAdded(Class<?> clazz, int expectedCount) { |
| 104 | int nonStaticFieldCount = 0; |
| 105 | |
| 106 | while (clazz != Object.class) { |
| 107 | for (Field field : clazz.getDeclaredFields()) { |
| 108 | if ((field.getModifiers() & Modifier.STATIC) == 0) { |
| 109 | nonStaticFieldCount++; |
| 110 | } |
| 111 | } |
| 112 | clazz = clazz.getSuperclass(); |
| 113 | } |
| 114 | |
| 115 | String message = "New fields have been added, so add code to test marshaling them."; |
| 116 | assertEquals(message, expectedCount, nonStaticFieldCount); |
| 117 | } |
Phil Weaver | bb2f28a | 2017-12-22 09:44:28 -0800 | [diff] [blame] | 118 | } |