Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs; |
| 16 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 17 | import android.support.test.runner.AndroidJUnit4; |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 18 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 19 | import android.view.View; |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 20 | import com.android.systemui.qs.TouchAnimator.Listener; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 21 | import com.android.systemui.SysuiTestCase; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.Test; |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 25 | import org.mockito.Mockito; |
| 26 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 27 | import static junit.framework.Assert.assertEquals; |
| 28 | |
Jason Monk | 0c6e099 | 2016-03-29 15:49:02 -0400 | [diff] [blame] | 29 | @SmallTest |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 30 | @RunWith(AndroidJUnit4.class) |
Geoffrey Pitsch | fc2b64e | 2016-09-02 09:05:25 -0400 | [diff] [blame] | 31 | public class TouchAnimatorTest extends SysuiTestCase { |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 32 | |
| 33 | private Listener mTouchListener; |
| 34 | private View mTestView; |
| 35 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 36 | @Before |
| 37 | public void setUp() throws Exception { |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 38 | mTestView = new View(getContext()); |
| 39 | mTouchListener = Mockito.mock(Listener.class); |
| 40 | } |
| 41 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 42 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 43 | public void testSetValueFloat() { |
| 44 | TouchAnimator animator = new TouchAnimator.Builder() |
| 45 | .addFloat(mTestView, "x", 0, 50) |
| 46 | .build(); |
| 47 | |
| 48 | animator.setPosition(0); |
| 49 | assertEquals(0f, mTestView.getX()); |
| 50 | |
| 51 | animator.setPosition(.5f); |
| 52 | assertEquals(25f, mTestView.getX()); |
| 53 | |
| 54 | animator.setPosition(1); |
| 55 | assertEquals(50f, mTestView.getX()); |
| 56 | } |
| 57 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 58 | @Test |
Amin Shaikh | d620def | 2018-02-27 16:52:53 -0500 | [diff] [blame] | 59 | public void testSetValueFloat_threeValues() { |
| 60 | TouchAnimator animator = new TouchAnimator.Builder() |
| 61 | .addFloat(mTestView, "x", 0, 20, 50) |
| 62 | .build(); |
| 63 | |
| 64 | animator.setPosition(0); |
| 65 | assertEquals(0f, mTestView.getX()); |
| 66 | |
| 67 | animator.setPosition(.25f); |
| 68 | assertEquals(10f, mTestView.getX()); |
| 69 | |
| 70 | animator.setPosition(.5f); |
| 71 | assertEquals(20f, mTestView.getX()); |
| 72 | |
| 73 | animator.setPosition(.75f); |
| 74 | assertEquals(35f, mTestView.getX()); |
| 75 | |
| 76 | animator.setPosition(1); |
| 77 | assertEquals(50f, mTestView.getX()); |
| 78 | } |
| 79 | |
| 80 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 81 | public void testSetValueInt() { |
| 82 | TouchAnimator animator = new TouchAnimator.Builder() |
| 83 | .addInt(mTestView, "top", 0, 50) |
| 84 | .build(); |
| 85 | |
| 86 | animator.setPosition(0); |
| 87 | assertEquals(0, mTestView.getTop()); |
| 88 | |
| 89 | animator.setPosition(.5f); |
| 90 | assertEquals(25, mTestView.getTop()); |
| 91 | |
| 92 | animator.setPosition(1); |
| 93 | assertEquals(50, mTestView.getTop()); |
| 94 | } |
| 95 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 96 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 97 | public void testStartDelay() { |
| 98 | TouchAnimator animator = new TouchAnimator.Builder() |
| 99 | .addFloat(mTestView, "x", 0, 50) |
| 100 | .setStartDelay(.5f) |
| 101 | .build(); |
| 102 | |
| 103 | animator.setPosition(0); |
| 104 | assertEquals(0f, mTestView.getX()); |
| 105 | |
| 106 | animator.setPosition(.5f); |
| 107 | assertEquals(0f, mTestView.getX()); |
| 108 | |
| 109 | animator.setPosition(.75f); |
| 110 | assertEquals(25f, mTestView.getX()); |
| 111 | |
| 112 | animator.setPosition(1); |
| 113 | assertEquals(50f, mTestView.getX()); |
| 114 | } |
| 115 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 116 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 117 | public void testEndDelay() { |
| 118 | TouchAnimator animator = new TouchAnimator.Builder() |
| 119 | .addFloat(mTestView, "x", 0, 50) |
| 120 | .setEndDelay(.5f) |
| 121 | .build(); |
| 122 | |
| 123 | animator.setPosition(0); |
| 124 | assertEquals(0f, mTestView.getX()); |
| 125 | |
| 126 | animator.setPosition(.25f); |
| 127 | assertEquals(25f, mTestView.getX()); |
| 128 | |
| 129 | animator.setPosition(.5f); |
| 130 | assertEquals(50f, mTestView.getX()); |
| 131 | |
| 132 | animator.setPosition(1); |
| 133 | assertEquals(50f, mTestView.getX()); |
| 134 | } |
| 135 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 136 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 137 | public void testOnAnimationAtStartCallback() { |
| 138 | TouchAnimator animator = new TouchAnimator.Builder() |
| 139 | .setListener(mTouchListener) |
| 140 | .build(); |
| 141 | |
| 142 | // Called on init. |
| 143 | animator.setPosition(0); |
| 144 | verifyOnAnimationAtStart(1); |
| 145 | |
| 146 | // Not called from same state. |
| 147 | animator.setPosition(0); |
| 148 | verifyOnAnimationAtStart(1); |
| 149 | |
| 150 | // Called after starting and moving back to start. |
| 151 | animator.setPosition(.5f); |
| 152 | animator.setPosition(0); |
| 153 | verifyOnAnimationAtStart(2); |
| 154 | |
| 155 | // Called when move from end to end. |
| 156 | animator.setPosition(1); |
| 157 | animator.setPosition(0); |
| 158 | verifyOnAnimationAtStart(3); |
| 159 | } |
| 160 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 161 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 162 | public void testOnAnimationAtEndCallback() { |
| 163 | TouchAnimator animator = new TouchAnimator.Builder() |
| 164 | .setListener(mTouchListener) |
| 165 | .build(); |
| 166 | |
| 167 | // Called on init. |
| 168 | animator.setPosition(1); |
| 169 | verifyOnAnimationAtEnd(1); |
| 170 | |
| 171 | // Not called from same state. |
| 172 | animator.setPosition(1); |
| 173 | verifyOnAnimationAtEnd(1); |
| 174 | |
| 175 | // Called after starting and moving back to end. |
| 176 | animator.setPosition(.5f); |
| 177 | animator.setPosition(1); |
| 178 | verifyOnAnimationAtEnd(2); |
| 179 | |
| 180 | // Called when move from end to end. |
| 181 | animator.setPosition(0); |
| 182 | animator.setPosition(1); |
| 183 | verifyOnAnimationAtEnd(3); |
| 184 | } |
| 185 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 186 | @Test |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 187 | public void testOnAnimationStartedCallback() { |
| 188 | TouchAnimator animator = new TouchAnimator.Builder() |
| 189 | .setListener(mTouchListener) |
| 190 | .build(); |
| 191 | |
| 192 | // Called on init. |
| 193 | animator.setPosition(.5f); |
| 194 | verifyOnAnimationStarted(1); |
| 195 | |
| 196 | // Not called from same state. |
| 197 | animator.setPosition(.6f); |
| 198 | verifyOnAnimationStarted(1); |
| 199 | |
| 200 | // Called after going to end then moving again. |
| 201 | animator.setPosition(1); |
| 202 | animator.setPosition(.5f); |
| 203 | verifyOnAnimationStarted(2); |
| 204 | |
| 205 | // Called after moving to start then moving again. |
| 206 | animator.setPosition(0); |
| 207 | animator.setPosition(.5f); |
| 208 | verifyOnAnimationStarted(3); |
| 209 | } |
| 210 | |
| 211 | // TODO: Add test for interpolator. |
| 212 | |
| 213 | private void verifyOnAnimationAtStart(int times) { |
| 214 | Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtStart(); |
| 215 | } |
| 216 | |
| 217 | private void verifyOnAnimationAtEnd(int times) { |
| 218 | Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtEnd(); |
| 219 | } |
| 220 | |
| 221 | private void verifyOnAnimationStarted(int times) { |
| 222 | Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationStarted(); |
| 223 | } |
| 224 | } |