• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package com.android.launcher3.folder;
17 
18 import static com.android.launcher3.folder.PreviewBackground.ACCEPT_SCALE_FACTOR;
19 import static com.android.launcher3.folder.PreviewBackground.CONSUMPTION_ANIMATION_DURATION;
20 import static com.android.launcher3.folder.PreviewBackground.HOVER_ANIMATION_DURATION;
21 import static com.android.launcher3.folder.PreviewBackground.HOVER_SCALE;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 
27 import android.view.animation.AccelerateDecelerateInterpolator;
28 import android.view.animation.PathInterpolator;
29 
30 import androidx.test.annotation.UiThreadTest;
31 import androidx.test.filters.SmallTest;
32 import androidx.test.platform.app.InstrumentationRegistry;
33 
34 import com.android.launcher3.CellLayout;
35 import com.android.launcher3.util.LauncherMultivalentJUnit;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 
43 @SmallTest
44 @UiThreadTest
45 @RunWith(LauncherMultivalentJUnit.class)
46 public class PreviewBackgroundTest {
47 
48     private static final float REST_SCALE = 1f;
49     private static final float EPSILON = 0.00001f;
50 
51     @Mock
52     CellLayout mCellLayout;
53 
54     private final PreviewBackground mPreviewBackground =
55             new PreviewBackground(InstrumentationRegistry.getInstrumentation().getContext());
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mPreviewBackground.mScale = REST_SCALE;
61         mPreviewBackground.mIsAccepting = false;
62         mPreviewBackground.mIsHovered = false;
63         mPreviewBackground.mIsHoveredOrAnimating = false;
64         mPreviewBackground.invalidate();
65     }
66 
67     @Test
testAnimateScale_restToHovered()68     public void testAnimateScale_restToHovered() {
69         mPreviewBackground.setHovered(true);
70         runAnimationToFraction(1f);
71 
72         assertEquals("Scale not changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
73         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
74                 HOVER_ANIMATION_DURATION);
75         assertTrue("Wrong interpolator used.",
76                 mPreviewBackground.mScaleAnimator.getInterpolator() instanceof PathInterpolator);
77         endAnimation();
78         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
79                 EPSILON);
80     }
81 
82     @Test
testAnimateScale_restToNotHovered()83     public void testAnimateScale_restToNotHovered() {
84         mPreviewBackground.setHovered(false);
85 
86         assertEquals("Scale changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
87         assertNull("Animator not null.", mPreviewBackground.mScaleAnimator);
88         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
89                 EPSILON);
90     }
91 
92     @Test
testAnimateScale_hoveredToHovered()93     public void testAnimateScale_hoveredToHovered() {
94         mPreviewBackground.mScale = HOVER_SCALE;
95         mPreviewBackground.mIsHovered = true;
96         mPreviewBackground.mIsHoveredOrAnimating = true;
97         mPreviewBackground.invalidate();
98 
99         mPreviewBackground.setHovered(true);
100 
101         assertEquals("Scale changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
102         assertNull("Animator not null.", mPreviewBackground.mScaleAnimator);
103         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
104                 EPSILON);
105     }
106 
107     @Test
testAnimateScale_hoveredToRest()108     public void testAnimateScale_hoveredToRest() {
109         mPreviewBackground.mScale = HOVER_SCALE;
110         mPreviewBackground.mIsHovered = true;
111         mPreviewBackground.mIsHoveredOrAnimating = true;
112         mPreviewBackground.invalidate();
113 
114         mPreviewBackground.setHovered(false);
115         runAnimationToFraction(1f);
116 
117         assertEquals("Scale not changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
118         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
119                 HOVER_ANIMATION_DURATION);
120         assertTrue("Wrong interpolator used.",
121                 mPreviewBackground.mScaleAnimator.getInterpolator() instanceof PathInterpolator);
122         endAnimation();
123         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
124                 EPSILON);
125     }
126 
127     @Test
testAnimateScale_restToAccept()128     public void testAnimateScale_restToAccept() {
129         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
130         runAnimationToFraction(1f);
131 
132         assertEquals("Scale changed.", mPreviewBackground.mScale, ACCEPT_SCALE_FACTOR, EPSILON);
133         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
134                 CONSUMPTION_ANIMATION_DURATION);
135         assertTrue("Wrong interpolator used.",
136                 mPreviewBackground.mScaleAnimator.getInterpolator()
137                         instanceof AccelerateDecelerateInterpolator);
138         endAnimation();
139         assertEquals("Scale progress not 1.", mPreviewBackground.getAcceptScaleProgress(), 1,
140                 EPSILON);
141     }
142 
143     @Test
testAnimateScale_restToRest()144     public void testAnimateScale_restToRest() {
145         mPreviewBackground.animateToRest();
146 
147         assertEquals("Scale changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
148         assertNull("Animator not null.", mPreviewBackground.mScaleAnimator);
149         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
150                 EPSILON);
151     }
152 
153     @Test
testAnimateScale_acceptToRest()154     public void testAnimateScale_acceptToRest() {
155         mPreviewBackground.mScale = ACCEPT_SCALE_FACTOR;
156         mPreviewBackground.mIsAccepting = true;
157         mPreviewBackground.invalidate();
158 
159         mPreviewBackground.animateToRest();
160         runAnimationToFraction(1f);
161 
162         assertEquals("Scale not changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
163         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
164                 CONSUMPTION_ANIMATION_DURATION);
165         assertTrue("Wrong interpolator used.",
166                 mPreviewBackground.mScaleAnimator.getInterpolator()
167                         instanceof AccelerateDecelerateInterpolator);
168         endAnimation();
169         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
170                 EPSILON);
171     }
172 
173     @Test
testAnimateScale_acceptToHover()174     public void testAnimateScale_acceptToHover() {
175         mPreviewBackground.mScale = ACCEPT_SCALE_FACTOR;
176         mPreviewBackground.mIsAccepting = true;
177         mPreviewBackground.invalidate();
178 
179         mPreviewBackground.mIsAccepting = false;
180         mPreviewBackground.setHovered(true);
181         runAnimationToFraction(1f);
182 
183         assertEquals("Scale not changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
184         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
185                 HOVER_ANIMATION_DURATION);
186         assertTrue("Wrong interpolator used.",
187                 mPreviewBackground.mScaleAnimator.getInterpolator() instanceof PathInterpolator);
188         endAnimation();
189         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
190                 EPSILON);
191     }
192 
193     @Test
testAnimateScale_hoverToAccept()194     public void testAnimateScale_hoverToAccept() {
195         mPreviewBackground.mScale = HOVER_SCALE;
196         mPreviewBackground.mIsHovered = true;
197         mPreviewBackground.mIsHoveredOrAnimating = true;
198         mPreviewBackground.invalidate();
199 
200         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
201         runAnimationToFraction(1f);
202 
203         assertEquals("Scale not changed.", mPreviewBackground.mScale, ACCEPT_SCALE_FACTOR, EPSILON);
204         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
205                 CONSUMPTION_ANIMATION_DURATION);
206         assertTrue("Wrong interpolator used.",
207                 mPreviewBackground.mScaleAnimator.getInterpolator()
208                         instanceof AccelerateDecelerateInterpolator);
209         mPreviewBackground.mIsHovered = false;
210         endAnimation();
211         assertEquals("Scale progress not 1.", mPreviewBackground.getAcceptScaleProgress(), 1,
212                 EPSILON);
213     }
214 
215     @Test
testAnimateScale_midwayToHoverToAccept()216     public void testAnimateScale_midwayToHoverToAccept() {
217         mPreviewBackground.setHovered(true);
218         runAnimationToFraction(0.5f);
219         assertTrue("Scale not changed.",
220                 mPreviewBackground.mScale > REST_SCALE && mPreviewBackground.mScale < HOVER_SCALE);
221         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
222                 EPSILON);
223 
224         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
225         runAnimationToFraction(1f);
226 
227         assertEquals("Scale not changed.", mPreviewBackground.mScale, ACCEPT_SCALE_FACTOR, EPSILON);
228         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
229                 CONSUMPTION_ANIMATION_DURATION);
230         assertTrue("Wrong interpolator used.",
231                 mPreviewBackground.mScaleAnimator.getInterpolator()
232                         instanceof AccelerateDecelerateInterpolator);
233         mPreviewBackground.mIsHovered = false;
234         endAnimation();
235         assertEquals("Scale progress not 1.", mPreviewBackground.getAcceptScaleProgress(), 1,
236                 EPSILON);
237         assertNull("Animator not null.", mPreviewBackground.mScaleAnimator);
238     }
239 
240     @Test
testAnimateScale_partWayToAcceptToHover()241     public void testAnimateScale_partWayToAcceptToHover() {
242         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
243         runAnimationToFraction(0.25f);
244         assertTrue("Scale not changed part way.", mPreviewBackground.mScale > REST_SCALE
245                 && mPreviewBackground.mScale < ACCEPT_SCALE_FACTOR);
246 
247         mPreviewBackground.mIsAccepting = false;
248         mPreviewBackground.setHovered(true);
249         runAnimationToFraction(1f);
250 
251         assertEquals("Scale not changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
252         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
253                 HOVER_ANIMATION_DURATION);
254         assertTrue("Wrong interpolator used.",
255                 mPreviewBackground.mScaleAnimator.getInterpolator() instanceof PathInterpolator);
256         endAnimation();
257         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
258                 EPSILON);
259     }
260 
261     @Test
testAnimateScale_midwayToAcceptEqualsHover()262     public void testAnimateScale_midwayToAcceptEqualsHover() {
263         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
264         runAnimationToFraction(0.5f);
265         assertEquals("Scale not changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
266         mPreviewBackground.mIsAccepting = false;
267 
268         mPreviewBackground.setHovered(true);
269 
270         assertEquals("Scale changed.", mPreviewBackground.mScale, HOVER_SCALE, EPSILON);
271         assertNull("Animator not null.", mPreviewBackground.mScaleAnimator);
272         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
273                 EPSILON);
274     }
275 
276     @Test
testAnimateScale_midwayToHoverToRest()277     public void testAnimateScale_midwayToHoverToRest() {
278         mPreviewBackground.setHovered(true);
279         runAnimationToFraction(0.5f);
280         assertTrue("Scale not changed midway.",
281                 mPreviewBackground.mScale > REST_SCALE && mPreviewBackground.mScale < HOVER_SCALE);
282 
283         mPreviewBackground.mIsHovered = false;
284         mPreviewBackground.animateToRest();
285         runAnimationToFraction(1f);
286 
287         assertEquals("Scale not changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
288         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
289                 HOVER_ANIMATION_DURATION);
290         assertTrue("Wrong interpolator used.",
291                 mPreviewBackground.mScaleAnimator.getInterpolator() instanceof PathInterpolator);
292         endAnimation();
293         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
294                 EPSILON);
295     }
296 
297     @Test
testAnimateScale_midwayToAcceptToRest()298     public void testAnimateScale_midwayToAcceptToRest() {
299         mPreviewBackground.animateToAccept(mCellLayout, 0, 0);
300         runAnimationToFraction(0.5f);
301         assertTrue("Scale not changed.", mPreviewBackground.mScale > REST_SCALE
302                 && mPreviewBackground.mScale < ACCEPT_SCALE_FACTOR);
303 
304         mPreviewBackground.animateToRest();
305         runAnimationToFraction(1f);
306 
307         assertEquals("Scale not changed.", mPreviewBackground.mScale, REST_SCALE, EPSILON);
308         assertEquals("Duration not correct.", mPreviewBackground.mScaleAnimator.getDuration(),
309                 CONSUMPTION_ANIMATION_DURATION);
310         assertTrue("Wrong interpolator used.",
311                 mPreviewBackground.mScaleAnimator.getInterpolator()
312                         instanceof AccelerateDecelerateInterpolator);
313         endAnimation();
314         assertEquals("Scale progress not 0.", mPreviewBackground.getAcceptScaleProgress(), 0,
315                 EPSILON);
316     }
317 
runAnimationToFraction(float animationFraction)318     private void runAnimationToFraction(float animationFraction) {
319         mPreviewBackground.mScaleAnimator.setCurrentFraction(animationFraction);
320     }
321 
endAnimation()322     private void endAnimation() {
323         mPreviewBackground.mScaleAnimator.end();
324     }
325 }
326