blob: ae4581a805038642712d721208da1d5a9a03b8c0 [file] [log] [blame]
Joshua Tsujib1a796b2019-01-16 15:43:12 -08001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.bubbles.animation;
18
19import static org.junit.Assert.assertEquals;
Joshua Tsuji4accf5982019-04-22 17:36:11 -040020import static org.junit.Assert.assertNotEquals;
21import static org.mockito.Mockito.verify;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080022
Lyn Hanf4730312019-06-18 11:18:58 -070023import android.content.res.Configuration;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080024import android.content.res.Resources;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080025import android.graphics.Point;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080026import android.graphics.PointF;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080027import android.testing.AndroidTestingRunner;
Joshua Tsuji442b6272019-02-08 13:23:43 -050028import android.view.View;
29import android.widget.FrameLayout;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080030
31import androidx.dynamicanimation.animation.DynamicAnimation;
Brett Chabot84151d92019-02-27 15:37:59 -080032import androidx.test.filters.SmallTest;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080033
34import com.android.systemui.R;
35
36import org.junit.Before;
Joshua Tsuji15e72622019-05-09 21:31:29 -040037import org.junit.Ignore;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080038import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Mockito;
41import org.mockito.Spy;
42
43@SmallTest
44@RunWith(AndroidTestingRunner.class)
45public class ExpandedAnimationControllerTest extends PhysicsAnimationLayoutTestCase {
46
Lyn Han522e9ff2019-05-17 13:26:13 -070047 private int mDisplayWidth = 500;
48 private int mDisplayHeight = 1000;
Lyn Han5390ce32019-05-31 07:25:50 -070049 private int mExpandedViewPadding = 10;
Lyn Hanf4730312019-06-18 11:18:58 -070050 private int mOrientation = Configuration.ORIENTATION_PORTRAIT;
Lyn Han1b4f25e2019-06-11 13:56:34 -070051 private float mLauncherGridDiff = 30f;
Lyn Han522e9ff2019-05-17 13:26:13 -070052
Joshua Tsujib1a796b2019-01-16 15:43:12 -080053 @Spy
Mady Mellor44ee2fe2019-01-30 17:51:16 -080054 private ExpandedAnimationController mExpandedController =
Lyn Han6f6b3ae2019-05-16 14:17:30 -070055 new ExpandedAnimationController(
Lyn Han522e9ff2019-05-17 13:26:13 -070056 new Point(mDisplayWidth, mDisplayHeight) /* displaySize */,
Lyn Hanf4730312019-06-18 11:18:58 -070057 mExpandedViewPadding, mOrientation);
Lyn Han5390ce32019-05-31 07:25:50 -070058
Joshua Tsujib1a796b2019-01-16 15:43:12 -080059 private int mStackOffset;
Lyn Han5390ce32019-05-31 07:25:50 -070060 private float mBubblePaddingTop;
Joshua Tsujib1a796b2019-01-16 15:43:12 -080061 private float mBubbleSize;
62
63 private PointF mExpansionPoint;
64
65 @Before
66 public void setUp() throws Exception {
67 super.setUp();
Joshua Tsujicb97a112019-05-29 16:20:41 -040068 addOneMoreThanBubbleLimitBubbles();
Joshua Tsujic36ee6f2019-05-28 17:00:16 -040069 mLayout.setActiveController(mExpandedController);
Lyn Han522e9ff2019-05-17 13:26:13 -070070
Joshua Tsujib1a796b2019-01-16 15:43:12 -080071 Resources res = mLayout.getResources();
72 mStackOffset = res.getDimensionPixelSize(R.dimen.bubble_stack_offset);
Lyn Han5390ce32019-05-31 07:25:50 -070073 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
Joshua Tsujib1a796b2019-01-16 15:43:12 -080074 mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size);
Joshua Tsuji1575e6b2019-01-30 13:43:28 -050075 mExpansionPoint = new PointF(100, 100);
Joshua Tsujib1a796b2019-01-16 15:43:12 -080076 }
77
78 @Test
Joshua Tsujic5fab412019-09-12 15:16:40 -040079 @Ignore
Joshua Tsujib1a796b2019-01-16 15:43:12 -080080 public void testExpansionAndCollapse() throws InterruptedException {
Joshua Tsujib1a796b2019-01-16 15:43:12 -080081 Runnable afterExpand = Mockito.mock(Runnable.class);
Joshua Tsujif49ee142019-05-29 16:32:01 -040082 mExpandedController.expandFromStack(afterExpand);
Joshua Tsujib1a796b2019-01-16 15:43:12 -080083 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
84
Joshua Tsuji442b6272019-02-08 13:23:43 -050085 testBubblesInCorrectExpandedPositions();
Joshua Tsuji4accf5982019-04-22 17:36:11 -040086 verify(afterExpand).run();
Joshua Tsujib1a796b2019-01-16 15:43:12 -080087
88 Runnable afterCollapse = Mockito.mock(Runnable.class);
Joshua Tsujif49ee142019-05-29 16:32:01 -040089 mExpandedController.collapseBackToStack(mExpansionPoint, afterCollapse);
Joshua Tsujib1a796b2019-01-16 15:43:12 -080090 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
91
92 testStackedAtPosition(mExpansionPoint.x, mExpansionPoint.y, -1);
Joshua Tsuji4accf5982019-04-22 17:36:11 -040093 verify(afterExpand).run();
Joshua Tsujib1a796b2019-01-16 15:43:12 -080094 }
95
Joshua Tsuji1575e6b2019-01-30 13:43:28 -050096 @Test
Joshua Tsujic5fab412019-09-12 15:16:40 -040097 @Ignore
Joshua Tsuji442b6272019-02-08 13:23:43 -050098 public void testOnChildAdded() throws InterruptedException {
99 expand();
100
101 // Add another new view and wait for its animation.
102 final View newView = new FrameLayout(getContext());
103 mLayout.addView(newView, 0);
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500104 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500105
106 testBubblesInCorrectExpandedPositions();
107 }
108
109 @Test
Joshua Tsujic5fab412019-09-12 15:16:40 -0400110 @Ignore
Joshua Tsuji442b6272019-02-08 13:23:43 -0500111 public void testOnChildRemoved() throws InterruptedException {
112 expand();
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500113
114 // Remove some views and see if the remaining child views still pass the expansion test.
115 mLayout.removeView(mViews.get(0));
116 mLayout.removeView(mViews.get(3));
117 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
Joshua Tsuji442b6272019-02-08 13:23:43 -0500118 testBubblesInCorrectExpandedPositions();
119 }
120
121 @Test
Joshua Tsujic5fab412019-09-12 15:16:40 -0400122 @Ignore
Joshua Tsuji442b6272019-02-08 13:23:43 -0500123 public void testBubbleDraggedNotDismissedSnapsBack() throws InterruptedException {
124 expand();
125
126 final View draggedBubble = mViews.get(0);
127 mExpandedController.prepareForBubbleDrag(draggedBubble);
128 mExpandedController.dragBubbleOut(draggedBubble, 500f, 500f);
129
130 assertEquals(500f, draggedBubble.getTranslationX(), 1f);
131 assertEquals(500f, draggedBubble.getTranslationY(), 1f);
132
133 // Snap it back and make sure it made it back correctly.
134 mExpandedController.snapBubbleBack(draggedBubble, 0f, 0f);
135 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
136 testBubblesInCorrectExpandedPositions();
137 }
138
139 @Test
Joshua Tsujic5fab412019-09-12 15:16:40 -0400140 @Ignore
Joshua Tsuji442b6272019-02-08 13:23:43 -0500141 public void testBubbleDismissed() throws InterruptedException {
142 expand();
143
144 final View draggedBubble = mViews.get(0);
145 mExpandedController.prepareForBubbleDrag(draggedBubble);
146 mExpandedController.dragBubbleOut(draggedBubble, 500f, 500f);
147
148 assertEquals(500f, draggedBubble.getTranslationX(), 1f);
149 assertEquals(500f, draggedBubble.getTranslationY(), 1f);
150
Joshua Tsuji442b6272019-02-08 13:23:43 -0500151 mLayout.removeView(draggedBubble);
152 waitForLayoutMessageQueue();
153 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
154
155 assertEquals(-1, mLayout.indexOfChild(draggedBubble));
156 testBubblesInCorrectExpandedPositions();
157 }
158
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400159 @Test
Joshua Tsuji16a4abf2019-05-22 13:03:58 -0400160 @Ignore("Flaky")
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400161 public void testMagnetToDismiss_dismiss() throws InterruptedException {
162 expand();
163
164 final View draggedOutView = mViews.get(0);
165 final Runnable after = Mockito.mock(Runnable.class);
166
167 mExpandedController.prepareForBubbleDrag(draggedOutView);
168 mExpandedController.dragBubbleOut(draggedOutView, 25, 25);
169
170 // Magnet to dismiss, verify the bubble is at the dismiss target and the callback was
171 // called.
172 mExpandedController.magnetBubbleToDismiss(
173 mViews.get(0), 100 /* velX */, 100 /* velY */, 1000 /* destY */, after);
174 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
175 verify(after).run();
176 assertEquals(1000, mViews.get(0).getTranslationY(), .1f);
177
178 // Dismiss the now-magneted bubble, verify that the callback was called.
179 final Runnable afterDismiss = Mockito.mock(Runnable.class);
Joshua Tsujif49ee142019-05-29 16:32:01 -0400180 mExpandedController.dismissDraggedOutBubble(draggedOutView, afterDismiss);
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400181 waitForPropertyAnimations(DynamicAnimation.ALPHA);
182 verify(after).run();
183
184 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
185
Lyn Han5390ce32019-05-31 07:25:50 -0700186 assertEquals(mBubblePaddingTop, mViews.get(1).getTranslationX(), 1f);
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400187 }
188
189 @Test
Joshua Tsuji15e72622019-05-09 21:31:29 -0400190 @Ignore("Flaky")
Joshua Tsuji4accf5982019-04-22 17:36:11 -0400191 public void testMagnetToDismiss_demagnetizeThenDrag() throws InterruptedException {
192 expand();
193
194 final View draggedOutView = mViews.get(0);
195 final Runnable after = Mockito.mock(Runnable.class);
196
197 mExpandedController.prepareForBubbleDrag(draggedOutView);
198 mExpandedController.dragBubbleOut(draggedOutView, 25, 25);
199
200 // Magnet to dismiss, verify the bubble is at the dismiss target and the callback was
201 // called.
202 mExpandedController.magnetBubbleToDismiss(
203 draggedOutView, 100 /* velX */, 100 /* velY */, 1000 /* destY */, after);
204 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
205 verify(after).run();
206 assertEquals(1000, mViews.get(0).getTranslationY(), .1f);
207
208 // Demagnetize the bubble towards (25, 25).
209 mExpandedController.demagnetizeBubbleTo(25 /* x */, 25 /* y */, 100, 100);
210
211 // Start dragging towards (20, 20).
212 mExpandedController.dragBubbleOut(draggedOutView, 20, 20);
213
214 // Since we just demagnetized, the bubble shouldn't be at (20, 20), it should be animating
215 // towards it.
216 assertNotEquals(20, draggedOutView.getTranslationX());
217 assertNotEquals(20, draggedOutView.getTranslationY());
218 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
219
220 // Waiting for the animations should result in the bubble ending at (20, 20) since the
221 // animation end value was updated.
222 assertEquals(20, draggedOutView.getTranslationX(), 1f);
223 assertEquals(20, draggedOutView.getTranslationY(), 1f);
224
225 // Drag to (30, 30).
226 mExpandedController.dragBubbleOut(draggedOutView, 30, 30);
227
228 // It should go there instantly since the animations finished.
229 assertEquals(30, draggedOutView.getTranslationX(), 1f);
230 assertEquals(30, draggedOutView.getTranslationY(), 1f);
231 }
232
Joshua Tsuji442b6272019-02-08 13:23:43 -0500233 /** Expand the stack and wait for animations to finish. */
234 private void expand() throws InterruptedException {
Joshua Tsujif49ee142019-05-29 16:32:01 -0400235 mExpandedController.expandFromStack(Mockito.mock(Runnable.class));
Joshua Tsuji442b6272019-02-08 13:23:43 -0500236 waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y);
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500237 }
238
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800239 /** Check that children are in the correct positions for being stacked. */
240 private void testStackedAtPosition(float x, float y, int offsetMultiplier) {
241 // Make sure the rest of the stack moved again, including the first bubble not moving, and
242 // is stacked to the right now that we're on the right side of the screen.
243 for (int i = 0; i < mLayout.getChildCount(); i++) {
244 assertEquals(x + i * offsetMultiplier * mStackOffset,
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500245 mLayout.getChildAt(i).getTranslationX(), 2f);
246 assertEquals(y, mLayout.getChildAt(i).getTranslationY(), 2f);
Joshua Tsujicb97a112019-05-29 16:20:41 -0400247 assertEquals(1f, mLayout.getChildAt(i).getAlpha(), .01f);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800248 }
249 }
250
251 /** Check that children are in the correct positions for being expanded. */
Joshua Tsuji442b6272019-02-08 13:23:43 -0500252 private void testBubblesInCorrectExpandedPositions() {
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500253 // Check all the visible bubbles to see if they're in the right place.
Joshua Tsujicb97a112019-05-29 16:20:41 -0400254 for (int i = 0; i < mLayout.getChildCount(); i++) {
Lyn Han522e9ff2019-05-17 13:26:13 -0700255 assertEquals(getBubbleLeft(i),
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500256 mLayout.getChildAt(i).getTranslationX(),
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800257 2f);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800258 assertEquals(mExpandedController.getExpandedY(),
Joshua Tsuji1575e6b2019-01-30 13:43:28 -0500259 mLayout.getChildAt(i).getTranslationY(), 2f);
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800260 }
261 }
Lyn Han522e9ff2019-05-17 13:26:13 -0700262
263 /**
264 * @param index Bubble index in row.
265 * @return Bubble left x from left edge of screen.
266 */
267 public float getBubbleLeft(int index) {
Lyn Han5390ce32019-05-31 07:25:50 -0700268 final float bubbleLeft = index * (mBubbleSize + getSpaceBetweenBubbles());
269 return getRowLeft() + bubbleLeft;
Lyn Han522e9ff2019-05-17 13:26:13 -0700270 }
271
272 private float getRowLeft() {
273 if (mLayout == null) {
274 return 0;
275 }
276 int bubbleCount = mLayout.getChildCount();
Lyn Han5390ce32019-05-31 07:25:50 -0700277 final float totalBubbleWidth = bubbleCount * mBubbleSize;
278 final float totalGapWidth = (bubbleCount - 1) * getSpaceBetweenBubbles();
279 final float rowWidth = totalGapWidth + totalBubbleWidth;
Lyn Han522e9ff2019-05-17 13:26:13 -0700280
Lyn Han5390ce32019-05-31 07:25:50 -0700281 final float centerScreen = mDisplayWidth / 2f;
282 final float halfRow = rowWidth / 2f;
283 final float rowLeft = centerScreen - halfRow;
Lyn Han522e9ff2019-05-17 13:26:13 -0700284
Lyn Han5390ce32019-05-31 07:25:50 -0700285 return rowLeft;
286 }
287
288 /**
289 * @return Space between bubbles in row above expanded view.
290 */
291 private float getSpaceBetweenBubbles() {
Lyn Han1b4f25e2019-06-11 13:56:34 -0700292 final float rowMargins = (mExpandedViewPadding + mLauncherGridDiff) * 2;
Lyn Han5390ce32019-05-31 07:25:50 -0700293 final float maxRowWidth = mDisplayWidth - rowMargins;
294
Joshua Tsuji61b38f52019-05-31 16:20:22 -0400295 final float totalBubbleWidth = mMaxBubbles * mBubbleSize;
Lyn Han5390ce32019-05-31 07:25:50 -0700296 final float totalGapWidth = maxRowWidth - totalBubbleWidth;
297
Joshua Tsuji61b38f52019-05-31 16:20:22 -0400298 final int gapCount = mMaxBubbles - 1;
Lyn Han5390ce32019-05-31 07:25:50 -0700299 final float gapWidth = totalGapWidth / gapCount;
300 return gapWidth;
Lyn Han522e9ff2019-05-17 13:26:13 -0700301 }
Joshua Tsujib1a796b2019-01-16 15:43:12 -0800302}