1 /*
2  * Copyright (C) 2008 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.widget.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertSame;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.Manifest;
25 import android.app.Activity;
26 import android.util.AttributeSet;
27 import android.util.Xml;
28 import android.view.View;
29 import android.view.ViewGroup.LayoutParams;
30 import android.view.animation.AccelerateInterpolator;
31 import android.view.animation.Animation;
32 import android.view.animation.AnimationSet;
33 import android.widget.RelativeLayout;
34 import android.widget.ViewAnimator;
35 
36 import androidx.test.annotation.UiThreadTest;
37 import androidx.test.filters.MediumTest;
38 import androidx.test.rule.ActivityTestRule;
39 import androidx.test.runner.AndroidJUnit4;
40 
41 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
42 
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.xmlpull.v1.XmlPullParser;
48 
49 @MediumTest
50 @RunWith(AndroidJUnit4.class)
51 public class ViewAnimatorTest {
52     private Activity mActivity;
53     private ViewAnimator mViewAnimator;
54     private AttributeSet mAttributeSet;
55 
56     @Rule(order = 0)
57     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
58             androidx.test.platform.app.InstrumentationRegistry
59                     .getInstrumentation().getUiAutomation(),
60             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
61 
62     @Rule(order = 1)
63     public ActivityTestRule<ViewAnimatorCtsActivity> mActivityRule =
64             new ActivityTestRule<>(ViewAnimatorCtsActivity.class);
65 
66     @Before
setup()67     public void setup() {
68         mActivity = mActivityRule.getActivity();
69 
70         XmlPullParser parser = mActivity.getResources().getXml(R.layout.viewanimator_layout);
71         mAttributeSet = Xml.asAttributeSet(parser);
72         mViewAnimator = new ViewAnimator(mActivity, mAttributeSet);
73     }
74 
75     @Test
testConstructor()76     public void testConstructor() {
77         new ViewAnimator(mActivity);
78         new ViewAnimator(mActivity, mAttributeSet);
79     }
80 
81     @Test
testAccessInAnimation()82     public void testAccessInAnimation() {
83         AnimationSet expected = new AnimationSet(mActivity, mAttributeSet);
84         assertNull(mViewAnimator.getInAnimation());
85 
86         mViewAnimator.setInAnimation(expected);
87         assertSame(expected, mViewAnimator.getInAnimation());
88 
89         // input null as param
90         mViewAnimator.setInAnimation(null);
91         assertNull(mViewAnimator.getInAnimation());
92 
93         mViewAnimator.setInAnimation(mActivity, R.anim.anim_alpha);
94         Animation animation = mViewAnimator.getInAnimation();
95         assertTrue(animation.getInterpolator() instanceof AccelerateInterpolator);
96         assertEquals(500, animation.getDuration());
97     }
98 
99     @UiThreadTest
100     @Test
testShowNext()101     public void testShowNext() {
102         final View v1 = mActivity.findViewById(R.id.ok);
103         final View v2 = mActivity.findViewById(R.id.cancel);
104         final View v3 = mActivity.findViewById(R.id.label);
105         final View v4 = mActivity.findViewById(R.id.entry);
106         final RelativeLayout parent = (RelativeLayout) v1.getParent();
107 
108         parent.removeView(v1);
109         parent.removeView(v2);
110         parent.removeView(v3);
111         parent.removeView(v4);
112         assertEquals(0, mViewAnimator.getChildCount());
113 
114         mViewAnimator.addView(v1);
115         mViewAnimator.addView(v2);
116         mViewAnimator.addView(v3);
117         mViewAnimator.addView(v4);
118         assertEquals(4, mViewAnimator.getChildCount());
119 
120         int current = 0;
121 
122         mViewAnimator.setDisplayedChild(current);
123         assertEquals(current, mViewAnimator.getDisplayedChild());
124 
125         mViewAnimator.showNext();
126         assertEquals(1, mViewAnimator.getDisplayedChild());
127 
128         mViewAnimator.showNext();
129         assertEquals(2, mViewAnimator.getDisplayedChild());
130 
131         mViewAnimator.showNext();
132         assertEquals(3, mViewAnimator.getDisplayedChild());
133 
134         mViewAnimator.removeAllViews();
135         assertEquals(0, mViewAnimator.getChildCount());
136     }
137 
138     @Test
testSetAnimateFirstView()139     public void testSetAnimateFirstView() {
140         mViewAnimator.setAnimateFirstView(true);
141         mViewAnimator.setAnimateFirstView(false);
142 
143         // TODO: how to check
144     }
145 
146     @UiThreadTest
147     @Test
testAccessDisplayedChild()148     public void testAccessDisplayedChild() {
149         final View v1 = mActivity.findViewById(R.id.ok);
150         final View v2 = mActivity.findViewById(R.id.cancel);
151         final RelativeLayout parent = (RelativeLayout) v1.getParent();
152 
153         parent.removeView(v1);
154         parent.removeView(v2);
155         assertEquals(0, mViewAnimator.getChildCount());
156 
157         mViewAnimator.addView(v1);
158         assertEquals(1, mViewAnimator.getChildCount());
159 
160         mViewAnimator.addView(v2);
161         assertEquals(2, mViewAnimator.getChildCount());
162 
163         mViewAnimator.setDisplayedChild(0);
164         assertEquals(0, mViewAnimator.getDisplayedChild());
165 
166         // set a negative value, then switch to getChildCount()-1.
167         mViewAnimator.setDisplayedChild(-1);
168         assertEquals(1, mViewAnimator.getDisplayedChild());
169 
170         // set larger than ChildCount, then switch to 0.
171         mViewAnimator.setDisplayedChild(2);
172         assertEquals(0, mViewAnimator.getDisplayedChild());
173 
174         mViewAnimator.setDisplayedChild(1);
175         assertEquals(1, mViewAnimator.getDisplayedChild());
176 
177         mViewAnimator.removeAllViews();
178         assertEquals(0, mViewAnimator.getChildCount());
179     }
180 
181     @UiThreadTest
182     @Test
testAccessDisplayedChildBoundary()183     public void testAccessDisplayedChildBoundary() {
184         final View v1 = mActivity.findViewById(R.id.ok);
185         final View v2 = mActivity.findViewById(R.id.cancel);
186         final RelativeLayout parent = (RelativeLayout) v1.getParent();
187 
188         parent.removeView(v1);
189         parent.removeView(v2);
190         assertEquals(0, mViewAnimator.getChildCount());
191 
192         mViewAnimator.addView(v1);
193         assertEquals(1, mViewAnimator.getChildCount());
194 
195         mViewAnimator.addView(v2);
196         assertEquals(2, mViewAnimator.getChildCount());
197 
198         int index = -1;
199         mViewAnimator.setDisplayedChild(index);
200         assertEquals(1, mViewAnimator.getDisplayedChild());
201 
202         index = 2;
203         mViewAnimator.setDisplayedChild(index);
204         assertEquals(0, mViewAnimator.getDisplayedChild());
205 
206         mViewAnimator.removeAllViews();
207         assertEquals(0, mViewAnimator.getChildCount());
208     }
209 
210     @UiThreadTest
211     @Test
testGetBaseline()212     public void testGetBaseline() {
213         final View v1 = mActivity.findViewById(R.id.ok);
214         final View v2 = mActivity.findViewById(R.id.cancel);
215         final RelativeLayout parent = (RelativeLayout) v1.getParent();
216 
217         parent.removeView(v1);
218         parent.removeView(v2);
219         assertEquals(0, mViewAnimator.getChildCount());
220 
221         mViewAnimator.addView(v1);
222         mViewAnimator.addView(v2);
223         assertEquals(2, mViewAnimator.getChildCount());
224 
225         int expected = v1.getBaseline();
226         mViewAnimator.setDisplayedChild(0);
227         assertEquals(expected, mViewAnimator.getBaseline());
228 
229         expected = v2.getBaseline();
230         mViewAnimator.setDisplayedChild(1);
231         assertEquals(expected, mViewAnimator.getBaseline());
232 
233         mViewAnimator.removeAllViews();
234         assertEquals(0, mViewAnimator.getChildCount());
235     }
236 
237     @UiThreadTest
238     @Test
testShowPrevious()239     public void testShowPrevious() {
240         final View v1 = mActivity.findViewById(R.id.ok);
241         final View v2 = mActivity.findViewById(R.id.cancel);
242         final View v3 = mActivity.findViewById(R.id.label);
243         final View v4 = mActivity.findViewById(R.id.entry);
244         final RelativeLayout parent = (RelativeLayout) v1.getParent();
245 
246         parent.removeView(v1);
247         parent.removeView(v2);
248         parent.removeView(v3);
249         parent.removeView(v4);
250         assertEquals(0, mViewAnimator.getChildCount());
251 
252         mViewAnimator.addView(v1);
253         mViewAnimator.addView(v2);
254         mViewAnimator.addView(v3);
255         mViewAnimator.addView(v4);
256         assertEquals(4, mViewAnimator.getChildCount());
257 
258         int current = 3;
259 
260         // set DisplayedChild by {@link mViewAnimator#setDisplayedChild(int)}
261         mViewAnimator.setDisplayedChild(current);
262         assertEquals(current, mViewAnimator.getDisplayedChild());
263 
264         mViewAnimator.showPrevious();
265         assertEquals(2, mViewAnimator.getDisplayedChild());
266 
267         mViewAnimator.showPrevious();
268         assertEquals(1, mViewAnimator.getDisplayedChild());
269 
270         mViewAnimator.showPrevious();
271         assertEquals(0, mViewAnimator.getDisplayedChild());
272 
273         mViewAnimator.removeAllViews();
274         assertEquals(0, mViewAnimator.getChildCount());
275     }
276 
277     @UiThreadTest
278     @Test
testGetCurrentView()279     public void testGetCurrentView() {
280         final View v = mActivity.findViewById(R.id.label);
281         final RelativeLayout parent = (RelativeLayout) v.getParent();
282 
283         parent.removeView(v);
284         assertEquals(0, mViewAnimator.getChildCount());
285 
286         mViewAnimator.addView(v);
287         assertEquals(1, mViewAnimator.getChildCount());
288 
289         int current = 0;
290         mViewAnimator.setDisplayedChild(current);
291         assertSame(v, mViewAnimator.getCurrentView());
292 
293         mViewAnimator.removeAllViews();
294         assertEquals(0, mViewAnimator.getChildCount());
295     }
296 
297     @UiThreadTest
298     @Test
testAddView()299     public void testAddView() {
300         final View v1 = mActivity.findViewById(R.id.ok);
301         final View v2 = mActivity.findViewById(R.id.cancel);
302         final RelativeLayout parent = (RelativeLayout) v1.getParent();
303 
304         parent.removeView(v1);
305         parent.removeView(v2);
306         assertEquals(0, mViewAnimator.getChildCount());
307 
308         LayoutParams p =
309             new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
310         mViewAnimator.addView(v1, 0, p);
311         assertEquals(1, mViewAnimator.getChildCount());
312         assertEquals(0, mViewAnimator.indexOfChild(v1));
313 
314         mViewAnimator.addView(v2, 1, p);
315         assertEquals(2, mViewAnimator.getChildCount());
316         assertEquals(1, mViewAnimator.indexOfChild(v2));
317 
318         mViewAnimator.removeAllViews();
319         assertEquals(0, mViewAnimator.getChildCount());
320     }
321 
322     @Test
testAccessOutAnimation()323     public void testAccessOutAnimation() {
324         AnimationSet expected = new AnimationSet(mActivity, mAttributeSet);
325         assertNull(mViewAnimator.getOutAnimation());
326 
327         mViewAnimator.setOutAnimation(expected);
328         assertSame(expected, mViewAnimator.getOutAnimation());
329 
330         mViewAnimator.setOutAnimation(null);
331         assertNull(mViewAnimator.getOutAnimation());
332 
333         mViewAnimator.setOutAnimation(mActivity, R.anim.anim_alpha);
334         Animation animation = mViewAnimator.getOutAnimation();
335         assertTrue(animation.getInterpolator() instanceof AccelerateInterpolator);
336         assertEquals(500, animation.getDuration());
337     }
338 
339     @UiThreadTest
340     @Test
testRemoveViews()341     public void testRemoveViews() {
342         final View v1 = mActivity.findViewById(R.id.ok);
343         final View v2 = mActivity.findViewById(R.id.cancel);
344         final View v3 = mActivity.findViewById(R.id.label);
345         final View v4 = mActivity.findViewById(R.id.entry);
346         final RelativeLayout parent = (RelativeLayout) v1.getParent();
347 
348         parent.removeView(v1);
349         parent.removeView(v2);
350         parent.removeView(v3);
351         parent.removeView(v4);
352         assertEquals(0, mViewAnimator.getChildCount());
353 
354         mViewAnimator.addView(v1);
355         mViewAnimator.addView(v2);
356         mViewAnimator.addView(v3);
357         mViewAnimator.addView(v4);
358         assertEquals(4, mViewAnimator.getChildCount());
359 
360         mViewAnimator.removeViewAt(3);
361         assertEquals(3, mViewAnimator.getChildCount());
362         assertSame(v1, mViewAnimator.getChildAt(0));
363         assertSame(v2, mViewAnimator.getChildAt(1));
364         assertSame(v3, mViewAnimator.getChildAt(2));
365 
366         mViewAnimator.removeView(v3);
367         assertEquals(2, mViewAnimator.getChildCount());
368         assertSame(v1, mViewAnimator.getChildAt(0));
369         assertSame(v2, mViewAnimator.getChildAt(1));
370 
371         mViewAnimator.removeViewInLayout(v2);
372         assertEquals(1, mViewAnimator.getChildCount());
373         assertSame(v1, mViewAnimator.getChildAt(0));
374 
375         mViewAnimator.addView(v2);
376         mViewAnimator.addView(v3);
377         mViewAnimator.addView(v4);
378         assertEquals(4, mViewAnimator.getChildCount());
379 
380         mViewAnimator.removeViews(0, 2);
381         assertEquals(2, mViewAnimator.getChildCount());
382 
383         // the remain views should be v3, v4
384         assertSame(v3, mViewAnimator.getChildAt(0));
385         assertSame(v4, mViewAnimator.getChildAt(1));
386 
387         mViewAnimator.removeViewsInLayout(1, 1);
388         assertEquals(1, mViewAnimator.getChildCount());
389 
390         // the remain view should be v3
391         assertSame(v3, mViewAnimator.getChildAt(0));
392 
393         mViewAnimator.removeAllViews();
394         assertEquals(0, mViewAnimator.getChildCount());
395     }
396 }
397