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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertSame;
22 
23 import android.accessibility.cts.common.AccessibilityDumpOnFailureRule;
24 import android.os.Message;
25 import android.platform.test.annotations.Presubmit;
26 import android.view.accessibility.AccessibilityRecord;
27 
28 import androidx.test.filters.SmallTest;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import junit.framework.TestCase;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 import java.util.Iterator;
38 import java.util.List;
39 
40 /**
41  * Class for testing {@link AccessibilityRecord}.
42  */
43 @Presubmit
44 @RunWith(AndroidJUnit4.class)
45 public class AccessibilityRecordTest {
46 
47     @Rule
48     public final AccessibilityDumpOnFailureRule mDumpOnFailureRule =
49             new AccessibilityDumpOnFailureRule();
50 
51     /**
52      * Tests the cloning obtain method.
53      */
54     @SmallTest
55     @Test
testObtain()56     public void testObtain() {
57         AccessibilityRecord originalRecord = AccessibilityRecord.obtain();
58         fullyPopulateAccessibilityRecord(originalRecord);
59         AccessibilityRecord cloneRecord = AccessibilityRecord.obtain(originalRecord);
60         assertEqualAccessibilityRecord(originalRecord, cloneRecord);
61     }
62 
63    /**
64     * Tests if {@link AccessibilityRecord}s are properly recycled.
65     */
66    @SmallTest
67    @Test
testRecycle()68    public void testRecycle() {
69        // obtain and populate an event
70        AccessibilityRecord populatedRecord = AccessibilityRecord.obtain();
71        fullyPopulateAccessibilityRecord(populatedRecord);
72 
73        // recycle and obtain the same recycled instance
74        populatedRecord.recycle();
75        AccessibilityRecord recycledRecord = AccessibilityRecord.obtain();
76 
77        // check expectations
78        assertAccessibilityRecordCleared(recycledRecord);
79    }
80 
81    /**
82     * Asserts that an {@link AccessibilityRecord} is cleared.
83     */
assertAccessibilityRecordCleared(AccessibilityRecord record)84    static void assertAccessibilityRecordCleared(AccessibilityRecord record) {
85        TestCase.assertEquals("addedCount not properly recycled", -1, record.getAddedCount());
86        TestCase.assertNull("beforeText not properly recycled", record.getBeforeText());
87        TestCase.assertFalse("checked not properly recycled", record.isChecked());
88        TestCase.assertNull("className not properly recycled", record.getClassName());
89        TestCase.assertNull("contentDescription not properly recycled",
90                record.getContentDescription());
91        TestCase.assertEquals("currentItemIndex not properly recycled", -1,
92                record.getCurrentItemIndex());
93        TestCase.assertFalse("enabled not properly recycled", record.isEnabled());
94        TestCase.assertEquals("fromIndex not properly recycled", -1, record.getFromIndex());
95        TestCase.assertFalse("fullScreen not properly recycled", record.isFullScreen());
96        TestCase.assertEquals("itemCount not properly recycled", -1, record.getItemCount());
97        TestCase.assertNull("parcelableData not properly recycled", record.getParcelableData());
98        TestCase.assertFalse("password not properly recycled", record.isPassword());
99        TestCase.assertEquals("removedCount not properly recycled", -1, record.getRemovedCount());
100        TestCase.assertTrue("text not properly recycled", record.getText().isEmpty());
101        TestCase.assertFalse("scrollable not properly recycled", record.isScrollable());
102        TestCase.assertSame("maxScrollX not properly recycled", 0, record.getMaxScrollX());
103        TestCase.assertSame("maxScrollY not properly recycled", 0, record.getMaxScrollY());
104        TestCase.assertSame("scrollX not properly recycled", 0, record.getScrollX());
105        TestCase.assertSame("scrollY not properly recycled", 0, record.getScrollY());
106        TestCase.assertSame("toIndex not properly recycled", -1, record.getToIndex());
107    }
108 
109     /**
110      * Fully populates the {@link AccessibilityRecord}.
111      *
112      * @param record The record to populate.
113      */
fullyPopulateAccessibilityRecord(AccessibilityRecord record)114     static void fullyPopulateAccessibilityRecord(AccessibilityRecord record) {
115         record.setAddedCount(1);
116         record.setBeforeText("BeforeText");
117         record.setChecked(true);
118         record.setClassName("foo.bar.baz.Class");
119         record.setContentDescription("ContentDescription");
120         record.setCurrentItemIndex(1);
121         record.setEnabled(true);
122         record.setFromIndex(1);
123         record.setFullScreen(true);
124         record.setItemCount(1);
125         record.setParcelableData(Message.obtain(null, 1, 2, 3));
126         record.setPassword(true);
127         record.setRemovedCount(1);
128         record.getText().add("Foo");
129         record.setMaxScrollX(1);
130         record.setMaxScrollY(1);
131         record.setScrollX(1);
132         record.setScrollY(1);
133         record.setToIndex(1);
134         record.setScrollable(true);
135     }
136 
assertEqualAccessibilityRecord(AccessibilityRecord expectedRecord, AccessibilityRecord receivedRecord)137     static void assertEqualAccessibilityRecord(AccessibilityRecord expectedRecord,
138             AccessibilityRecord receivedRecord) {
139         assertEquals("addedCount has incorrect value", expectedRecord.getAddedCount(),
140                 receivedRecord.getAddedCount());
141         assertEquals("beforeText has incorrect value", expectedRecord.getBeforeText(),
142                 receivedRecord.getBeforeText());
143         assertEquals("checked has incorrect value", expectedRecord.isChecked(),
144                 receivedRecord.isChecked());
145         assertEquals("className has incorrect value", expectedRecord.getClassName(),
146                 receivedRecord.getClassName());
147         assertEquals("contentDescription has incorrect value",
148                 expectedRecord.getContentDescription(), receivedRecord.getContentDescription());
149         assertEquals("currentItemIndex has incorrect value", expectedRecord.getCurrentItemIndex(),
150                 receivedRecord.getCurrentItemIndex());
151         assertEquals("enabled has incorrect value", expectedRecord.isEnabled(),
152                 receivedRecord.isEnabled());
153         assertEquals("fromIndex has incorrect value", expectedRecord.getFromIndex(),
154                 receivedRecord.getFromIndex());
155         assertEquals("fullScreen has incorrect value", expectedRecord.isFullScreen(),
156                 receivedRecord.isFullScreen());
157         assertEquals("itemCount has incorrect value", expectedRecord.getItemCount(),
158                 receivedRecord.getItemCount());
159         assertEquals("password has incorrect value", expectedRecord.isPassword(),
160                 receivedRecord.isPassword());
161         assertEquals("removedCount has incorrect value", expectedRecord.getRemovedCount(),
162                 receivedRecord.getRemovedCount());
163         assertEqualsText(expectedRecord.getText(), receivedRecord.getText());
164         assertSame("maxScrollX has incorrect value", expectedRecord.getMaxScrollX(),
165                 receivedRecord.getMaxScrollX());
166         assertSame("maxScrollY has incorrect value", expectedRecord.getMaxScrollY(),
167                 receivedRecord.getMaxScrollY());
168         assertSame("scrollX has incorrect value", expectedRecord.getScrollX(),
169                 receivedRecord.getScrollX());
170         assertSame("scrollY has incorrect value", expectedRecord.getScrollY(),
171                 receivedRecord.getScrollY());
172         assertSame("toIndex has incorrect value", expectedRecord.getToIndex(),
173                 receivedRecord.getToIndex());
174         assertSame("scrollable has incorrect value", expectedRecord.isScrollable(),
175                 receivedRecord.isScrollable());
176 
177         assertFalse("one of the parcelableData is null",
178                 expectedRecord.getParcelableData() == null
179                         ^ receivedRecord.getParcelableData() == null);
180         if (expectedRecord.getParcelableData() != null
181                 && receivedRecord.getParcelableData() != null) {
182             assertSame("parcelableData has incorrect value",
183                     ((Message) expectedRecord.getParcelableData()).what,
184                     ((Message) receivedRecord.getParcelableData()).what);
185         }
186     }
187 
188     /**
189      * Compares the text of the <code>expectedEvent</code> and
190      * <code>receivedEvent</code> by comparing the string representation of the
191      * corresponding {@link CharSequence}s.
192      */
assertEqualsText(List<CharSequence> expectedText, List<CharSequence> receivedText )193     static void assertEqualsText(List<CharSequence> expectedText,
194             List<CharSequence> receivedText ) {
195         String message = "text has incorrect value";
196 
197         TestCase.assertEquals(message, expectedText.size(), receivedText.size());
198 
199         Iterator<CharSequence> expectedTextIterator = expectedText.iterator();
200         Iterator<CharSequence> receivedTextIterator = receivedText.iterator();
201 
202         for (int i = 0; i < expectedText.size(); i++) {
203             // compare the string representation
204             TestCase.assertEquals(message, expectedTextIterator.next().toString(),
205                     receivedTextIterator.next().toString());
206         }
207     }
208 }
209