TouchAnimatorTests.java revision f6a3cf929102fd74757affdbd09053144e29a3c7
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.view.View; 18import com.android.systemui.SysuiTestCase; 19import com.android.systemui.qs.TouchAnimator.Listener; 20import org.mockito.Mockito; 21 22public class TouchAnimatorTests extends SysuiTestCase { 23 24 private Listener mTouchListener; 25 private View mTestView; 26 27 @Override 28 protected void setUp() throws Exception { 29 super.setUp(); 30 31 mTestView = new View(getContext()); 32 mTouchListener = Mockito.mock(Listener.class); 33 } 34 35 public void testSetValueFloat() { 36 TouchAnimator animator = new TouchAnimator.Builder() 37 .addFloat(mTestView, "x", 0, 50) 38 .build(); 39 40 animator.setPosition(0); 41 assertEquals(0f, mTestView.getX()); 42 43 animator.setPosition(.5f); 44 assertEquals(25f, mTestView.getX()); 45 46 animator.setPosition(1); 47 assertEquals(50f, mTestView.getX()); 48 } 49 50 public void testSetValueInt() { 51 TouchAnimator animator = new TouchAnimator.Builder() 52 .addInt(mTestView, "top", 0, 50) 53 .build(); 54 55 animator.setPosition(0); 56 assertEquals(0, mTestView.getTop()); 57 58 animator.setPosition(.5f); 59 assertEquals(25, mTestView.getTop()); 60 61 animator.setPosition(1); 62 assertEquals(50, mTestView.getTop()); 63 } 64 65 public void testStartDelay() { 66 TouchAnimator animator = new TouchAnimator.Builder() 67 .addFloat(mTestView, "x", 0, 50) 68 .setStartDelay(.5f) 69 .build(); 70 71 animator.setPosition(0); 72 assertEquals(0f, mTestView.getX()); 73 74 animator.setPosition(.5f); 75 assertEquals(0f, mTestView.getX()); 76 77 animator.setPosition(.75f); 78 assertEquals(25f, mTestView.getX()); 79 80 animator.setPosition(1); 81 assertEquals(50f, mTestView.getX()); 82 } 83 84 public void testEndDelay() { 85 TouchAnimator animator = new TouchAnimator.Builder() 86 .addFloat(mTestView, "x", 0, 50) 87 .setEndDelay(.5f) 88 .build(); 89 90 animator.setPosition(0); 91 assertEquals(0f, mTestView.getX()); 92 93 animator.setPosition(.25f); 94 assertEquals(25f, mTestView.getX()); 95 96 animator.setPosition(.5f); 97 assertEquals(50f, mTestView.getX()); 98 99 animator.setPosition(1); 100 assertEquals(50f, mTestView.getX()); 101 } 102 103 public void testOnAnimationAtStartCallback() { 104 TouchAnimator animator = new TouchAnimator.Builder() 105 .setListener(mTouchListener) 106 .build(); 107 108 // Called on init. 109 animator.setPosition(0); 110 verifyOnAnimationAtStart(1); 111 112 // Not called from same state. 113 animator.setPosition(0); 114 verifyOnAnimationAtStart(1); 115 116 // Called after starting and moving back to start. 117 animator.setPosition(.5f); 118 animator.setPosition(0); 119 verifyOnAnimationAtStart(2); 120 121 // Called when move from end to end. 122 animator.setPosition(1); 123 animator.setPosition(0); 124 verifyOnAnimationAtStart(3); 125 } 126 127 public void testOnAnimationAtEndCallback() { 128 TouchAnimator animator = new TouchAnimator.Builder() 129 .setListener(mTouchListener) 130 .build(); 131 132 // Called on init. 133 animator.setPosition(1); 134 verifyOnAnimationAtEnd(1); 135 136 // Not called from same state. 137 animator.setPosition(1); 138 verifyOnAnimationAtEnd(1); 139 140 // Called after starting and moving back to end. 141 animator.setPosition(.5f); 142 animator.setPosition(1); 143 verifyOnAnimationAtEnd(2); 144 145 // Called when move from end to end. 146 animator.setPosition(0); 147 animator.setPosition(1); 148 verifyOnAnimationAtEnd(3); 149 } 150 151 public void testOnAnimationStartedCallback() { 152 TouchAnimator animator = new TouchAnimator.Builder() 153 .setListener(mTouchListener) 154 .build(); 155 156 // Called on init. 157 animator.setPosition(.5f); 158 verifyOnAnimationStarted(1); 159 160 // Not called from same state. 161 animator.setPosition(.6f); 162 verifyOnAnimationStarted(1); 163 164 // Called after going to end then moving again. 165 animator.setPosition(1); 166 animator.setPosition(.5f); 167 verifyOnAnimationStarted(2); 168 169 // Called after moving to start then moving again. 170 animator.setPosition(0); 171 animator.setPosition(.5f); 172 verifyOnAnimationStarted(3); 173 } 174 175 // TODO: Add test for interpolator. 176 177 private void verifyOnAnimationAtStart(int times) { 178 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtStart(); 179 } 180 181 private void verifyOnAnimationAtEnd(int times) { 182 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtEnd(); 183 } 184 185 private void verifyOnAnimationStarted(int times) { 186 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationStarted(); 187 } 188} 189