1 /*
2  * Copyright (C) 2016 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.assertNotNull;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 
27 import android.accessibility.cts.common.AccessibilityDumpOnFailureRule;
28 import android.graphics.Rect;
29 import android.graphics.Region;
30 import android.os.Parcel;
31 import android.platform.test.annotations.Presubmit;
32 import android.text.TextUtils;
33 import android.view.Display;
34 import android.view.accessibility.AccessibilityWindowInfo;
35 
36 import androidx.test.filters.SmallTest;
37 import androidx.test.runner.AndroidJUnit4;
38 
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 /**
44  * Class for testing {@link AccessibilityWindowInfo}.
45  */
46 @Presubmit
47 @RunWith(AndroidJUnit4.class)
48 public class AccessibilityWindowInfoTest {
49 
50     @Rule
51     public final AccessibilityDumpOnFailureRule mDumpOnFailureRule =
52             new AccessibilityDumpOnFailureRule();
53 
54     @SmallTest
55     @Test
testObtain()56     public void testObtain() {
57         AccessibilityWindowInfo w1 = AccessibilityWindowInfo.obtain();
58         assertNotNull(w1);
59 
60         AccessibilityWindowInfo w2 = AccessibilityWindowInfo.obtain(w1);
61         assertNotSame(w1, w2);
62         assertTrue(areWindowsEqual(w1, w2));
63     }
64 
65     @SmallTest
66     @Test
testParceling()67     public void testParceling() {
68         Parcel parcel = Parcel.obtain();
69         AccessibilityWindowInfo w1 = AccessibilityWindowInfo.obtain();
70         w1.writeToParcel(parcel, 0);
71         parcel.setDataPosition(0);
72         AccessibilityWindowInfo w2 = AccessibilityWindowInfo.CREATOR.createFromParcel(parcel);
73         assertNotSame(w1, w2);
74         assertTrue(w2.toString(), areWindowsEqual(w1, w2));
75         parcel.recycle();
76     }
77 
78     @SmallTest
79     @Test
testDefaultValues()80     public void testDefaultValues() {
81         AccessibilityWindowInfo w = AccessibilityWindowInfo.obtain();
82         assertEquals(0, w.getChildCount());
83         assertEquals(-1, w.getType());
84         assertEquals(-1, w.getLayer());
85         assertEquals(-1, w.getId());
86         assertEquals(0, w.describeContents());
87         assertEquals(Display.INVALID_DISPLAY, w.getDisplayId());
88         assertNull(w.getParent());
89         assertNull(w.getRoot());
90         assertFalse(w.isAccessibilityFocused());
91         assertFalse(w.isActive());
92         assertFalse(w.isFocused());
93         assertNull(w.getTitle());
94 
95         Rect rect = new Rect();
96         w.getBoundsInScreen(rect);
97         assertTrue(rect.isEmpty());
98 
99         Region region = new Region();
100         w.getRegionInScreen(region);
101         assertTrue(region.isEmpty());
102 
103         try {
104             w.getChild(0);
105             fail("Expected IndexOutOfBoundsException");
106         } catch (IndexOutOfBoundsException e) {
107             // Expected.
108         }
109     }
110 
111     @SmallTest
112     @Test
testRecycle()113     public void testRecycle() {
114         AccessibilityWindowInfo w = AccessibilityWindowInfo.obtain();
115         w.recycle();
116 
117         try {
118             w.recycle();
119             fail("Expected IllegalStateException");
120         } catch (IllegalStateException e) {
121             // Expected.
122         }
123     }
124 
areWindowsEqual(AccessibilityWindowInfo w1, AccessibilityWindowInfo w2)125     private boolean areWindowsEqual(AccessibilityWindowInfo w1, AccessibilityWindowInfo w2) {
126         boolean equality = w1.equals(w2);
127         equality &= TextUtils.equals(w1.getTitle(), w2.getTitle());
128         equality &= w1.isAccessibilityFocused() == w2.isAccessibilityFocused();
129         equality &= w1.isActive() == w2.isActive();
130         equality &= w1.getType() == w2.getType();
131         equality &= w1.getLayer() == w2.getLayer();
132         equality &= w1.getDisplayId() == w2.getDisplayId();
133         Rect bounds1 = new Rect();
134         Rect bounds2 = new Rect();
135         w1.getBoundsInScreen(bounds1);
136         w2.getBoundsInScreen(bounds2);
137         equality &= bounds1.equals(bounds2);
138         Region regions1 = new Region();
139         Region regions2 = new Region();
140         w1.getRegionInScreen(regions1);
141         w2.getRegionInScreen(regions2);
142         equality &= regions1.equals(regions2);
143         return equality;
144     }
145 }
146