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