Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.bubbles.animation; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | import android.content.res.Resources; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 22 | import android.graphics.Point; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 23 | import android.graphics.PointF; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 24 | import android.testing.AndroidTestingRunner; |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.widget.FrameLayout; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 27 | |
| 28 | import androidx.dynamicanimation.animation.DynamicAnimation; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 29 | import androidx.test.filters.SmallTest; |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 30 | |
| 31 | import com.android.systemui.R; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.mockito.Mockito; |
| 37 | import org.mockito.Spy; |
| 38 | |
| 39 | @SmallTest |
| 40 | @RunWith(AndroidTestingRunner.class) |
| 41 | public class ExpandedAnimationControllerTest extends PhysicsAnimationLayoutTestCase { |
| 42 | |
| 43 | @Spy |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 44 | private ExpandedAnimationController mExpandedController = |
| 45 | new ExpandedAnimationController(new Point(500, 1000) /* displaySize */); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 46 | |
| 47 | private int mStackOffset; |
| 48 | private float mBubblePadding; |
| 49 | private float mBubbleSize; |
| 50 | |
| 51 | private PointF mExpansionPoint; |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
| 55 | super.setUp(); |
| 56 | addOneMoreThanRenderLimitBubbles(); |
| 57 | mLayout.setController(mExpandedController); |
| 58 | Resources res = mLayout.getResources(); |
| 59 | mStackOffset = res.getDimensionPixelSize(R.dimen.bubble_stack_offset); |
| 60 | mBubblePadding = res.getDimensionPixelSize(R.dimen.bubble_padding); |
| 61 | mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 62 | |
| 63 | mExpansionPoint = new PointF(100, 100); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testExpansionAndCollapse() throws InterruptedException { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 68 | Runnable afterExpand = Mockito.mock(Runnable.class); |
| 69 | mExpandedController.expandFromStack(mExpansionPoint, afterExpand); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 70 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
| 71 | |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 72 | testBubblesInCorrectExpandedPositions(); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 73 | Mockito.verify(afterExpand).run(); |
| 74 | |
| 75 | Runnable afterCollapse = Mockito.mock(Runnable.class); |
| 76 | mExpandedController.collapseBackToStack(afterCollapse); |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 77 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
| 78 | |
| 79 | testStackedAtPosition(mExpansionPoint.x, mExpansionPoint.y, -1); |
| 80 | Mockito.verify(afterExpand).run(); |
| 81 | } |
| 82 | |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 83 | @Test |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 84 | public void testOnChildAdded() throws InterruptedException { |
| 85 | expand(); |
| 86 | |
| 87 | // Add another new view and wait for its animation. |
| 88 | final View newView = new FrameLayout(getContext()); |
| 89 | mLayout.addView(newView, 0); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 90 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 91 | |
| 92 | testBubblesInCorrectExpandedPositions(); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testOnChildRemoved() throws InterruptedException { |
| 97 | expand(); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 98 | |
| 99 | // Remove some views and see if the remaining child views still pass the expansion test. |
| 100 | mLayout.removeView(mViews.get(0)); |
| 101 | mLayout.removeView(mViews.get(3)); |
| 102 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 103 | testBubblesInCorrectExpandedPositions(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testBubbleDraggedNotDismissedSnapsBack() throws InterruptedException { |
| 108 | expand(); |
| 109 | |
| 110 | final View draggedBubble = mViews.get(0); |
| 111 | mExpandedController.prepareForBubbleDrag(draggedBubble); |
| 112 | mExpandedController.dragBubbleOut(draggedBubble, 500f, 500f); |
| 113 | |
| 114 | assertEquals(500f, draggedBubble.getTranslationX(), 1f); |
| 115 | assertEquals(500f, draggedBubble.getTranslationY(), 1f); |
| 116 | |
| 117 | // Snap it back and make sure it made it back correctly. |
| 118 | mExpandedController.snapBubbleBack(draggedBubble, 0f, 0f); |
| 119 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
| 120 | testBubblesInCorrectExpandedPositions(); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testBubbleDismissed() throws InterruptedException { |
| 125 | expand(); |
| 126 | |
| 127 | final View draggedBubble = mViews.get(0); |
| 128 | mExpandedController.prepareForBubbleDrag(draggedBubble); |
| 129 | mExpandedController.dragBubbleOut(draggedBubble, 500f, 500f); |
| 130 | |
| 131 | assertEquals(500f, draggedBubble.getTranslationX(), 1f); |
| 132 | assertEquals(500f, draggedBubble.getTranslationY(), 1f); |
| 133 | |
| 134 | // Snap it back and make sure it made it back correctly. |
| 135 | mExpandedController.prepareForDismissalWithVelocity(draggedBubble, 0f, 0f); |
| 136 | mLayout.removeView(draggedBubble); |
| 137 | waitForLayoutMessageQueue(); |
| 138 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
| 139 | |
| 140 | assertEquals(-1, mLayout.indexOfChild(draggedBubble)); |
| 141 | testBubblesInCorrectExpandedPositions(); |
| 142 | } |
| 143 | |
| 144 | /** Expand the stack and wait for animations to finish. */ |
| 145 | private void expand() throws InterruptedException { |
| 146 | mExpandedController.expandFromStack(mExpansionPoint, Mockito.mock(Runnable.class)); |
| 147 | waitForPropertyAnimations(DynamicAnimation.TRANSLATION_X, DynamicAnimation.TRANSLATION_Y); |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 150 | /** Check that children are in the correct positions for being stacked. */ |
| 151 | private void testStackedAtPosition(float x, float y, int offsetMultiplier) { |
| 152 | // Make sure the rest of the stack moved again, including the first bubble not moving, and |
| 153 | // is stacked to the right now that we're on the right side of the screen. |
| 154 | for (int i = 0; i < mLayout.getChildCount(); i++) { |
| 155 | assertEquals(x + i * offsetMultiplier * mStackOffset, |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 156 | mLayout.getChildAt(i).getTranslationX(), 2f); |
| 157 | assertEquals(y, mLayout.getChildAt(i).getTranslationY(), 2f); |
| 158 | |
| 159 | if (i < mMaxRenderedBubbles) { |
| 160 | assertEquals(1f, mLayout.getChildAt(i).getAlpha(), .01f); |
| 161 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /** Check that children are in the correct positions for being expanded. */ |
Joshua Tsuji | 442b627 | 2019-02-08 13:23:43 -0500 | [diff] [blame] | 166 | private void testBubblesInCorrectExpandedPositions() { |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 167 | // Check all the visible bubbles to see if they're in the right place. |
| 168 | for (int i = 0; i < Math.min(mLayout.getChildCount(), mMaxRenderedBubbles); i++) { |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 169 | assertEquals(mBubblePadding + (i * (mBubbleSize + mBubblePadding)), |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 170 | mLayout.getChildAt(i).getTranslationX(), |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 171 | 2f); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 172 | assertEquals(mExpandedController.getExpandedY(), |
Joshua Tsuji | 1575e6b | 2019-01-30 13:43:28 -0500 | [diff] [blame] | 173 | mLayout.getChildAt(i).getTranslationY(), 2f); |
| 174 | |
| 175 | if (i < mMaxRenderedBubbles) { |
| 176 | assertEquals(1f, mLayout.getChildAt(i).getAlpha(), .01f); |
| 177 | } |
Joshua Tsuji | b1a796b | 2019-01-16 15:43:12 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |