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 15package com.android.systemui.qs; 16 17import android.support.test.runner.AndroidJUnit4; 18import android.test.suitebuilder.annotation.SmallTest; 19import android.view.View; 20import com.android.systemui.qs.TouchAnimator.Listener; 21import com.android.systemui.SysuiTestCase; 22import org.junit.Before; 23import org.junit.runner.RunWith; 24import org.junit.Test; 25import org.mockito.Mockito; 26 27import static junit.framework.Assert.assertEquals; 28 29@SmallTest 30@RunWith(AndroidJUnit4.class) 31public class TouchAnimatorTest extends SysuiTestCase { 32 33 private Listener mTouchListener; 34 private View mTestView; 35 36 @Before 37 public void setUp() throws Exception { 38 mTestView = new View(getContext()); 39 mTouchListener = Mockito.mock(Listener.class); 40 } 41 42 @Test 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 58 @Test 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 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 96 @Test 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 116 @Test 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 136 @Test 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 161 @Test 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 186 @Test 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} 225