blob: c43c3e629c8a4b0687287802d3a4172c6ce56566 [file] [log] [blame]
Jason Monkf6a3cf92016-02-29 13:01:08 -05001/*
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
Brett Chabot84151d92019-02-27 15:37:59 -080017import static junit.framework.Assert.assertEquals;
18
Jason Monk0c6e0992016-03-29 15:49:02 -040019import android.test.suitebuilder.annotation.SmallTest;
Jason Monkf6a3cf92016-02-29 13:01:08 -050020import android.view.View;
Jason Monkf6a3cf92016-02-29 13:01:08 -050021
Brett Chabot84151d92019-02-27 15:37:59 -080022import androidx.test.runner.AndroidJUnit4;
23
24import com.android.systemui.SysuiTestCase;
25import com.android.systemui.qs.TouchAnimator.Listener;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.mockito.Mockito;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040031
Jason Monk0c6e0992016-03-29 15:49:02 -040032@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040033@RunWith(AndroidJUnit4.class)
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040034public class TouchAnimatorTest extends SysuiTestCase {
Jason Monkf6a3cf92016-02-29 13:01:08 -050035
36 private Listener mTouchListener;
37 private View mTestView;
38
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040039 @Before
40 public void setUp() throws Exception {
Jason Monkf6a3cf92016-02-29 13:01:08 -050041 mTestView = new View(getContext());
42 mTouchListener = Mockito.mock(Listener.class);
43 }
44
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040045 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -050046 public void testSetValueFloat() {
47 TouchAnimator animator = new TouchAnimator.Builder()
48 .addFloat(mTestView, "x", 0, 50)
49 .build();
50
51 animator.setPosition(0);
52 assertEquals(0f, mTestView.getX());
53
54 animator.setPosition(.5f);
55 assertEquals(25f, mTestView.getX());
56
57 animator.setPosition(1);
58 assertEquals(50f, mTestView.getX());
59 }
60
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040061 @Test
Amin Shaikhd620def2018-02-27 16:52:53 -050062 public void testSetValueFloat_threeValues() {
63 TouchAnimator animator = new TouchAnimator.Builder()
64 .addFloat(mTestView, "x", 0, 20, 50)
65 .build();
66
67 animator.setPosition(0);
68 assertEquals(0f, mTestView.getX());
69
70 animator.setPosition(.25f);
71 assertEquals(10f, mTestView.getX());
72
73 animator.setPosition(.5f);
74 assertEquals(20f, mTestView.getX());
75
76 animator.setPosition(.75f);
77 assertEquals(35f, mTestView.getX());
78
79 animator.setPosition(1);
80 assertEquals(50f, mTestView.getX());
81 }
82
83 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -050084 public void testSetValueInt() {
85 TouchAnimator animator = new TouchAnimator.Builder()
86 .addInt(mTestView, "top", 0, 50)
87 .build();
88
89 animator.setPosition(0);
90 assertEquals(0, mTestView.getTop());
91
92 animator.setPosition(.5f);
93 assertEquals(25, mTestView.getTop());
94
95 animator.setPosition(1);
96 assertEquals(50, mTestView.getTop());
97 }
98
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040099 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -0500100 public void testStartDelay() {
101 TouchAnimator animator = new TouchAnimator.Builder()
102 .addFloat(mTestView, "x", 0, 50)
103 .setStartDelay(.5f)
104 .build();
105
106 animator.setPosition(0);
107 assertEquals(0f, mTestView.getX());
108
109 animator.setPosition(.5f);
110 assertEquals(0f, mTestView.getX());
111
112 animator.setPosition(.75f);
113 assertEquals(25f, mTestView.getX());
114
115 animator.setPosition(1);
116 assertEquals(50f, mTestView.getX());
117 }
118
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400119 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -0500120 public void testEndDelay() {
121 TouchAnimator animator = new TouchAnimator.Builder()
122 .addFloat(mTestView, "x", 0, 50)
123 .setEndDelay(.5f)
124 .build();
125
126 animator.setPosition(0);
127 assertEquals(0f, mTestView.getX());
128
129 animator.setPosition(.25f);
130 assertEquals(25f, mTestView.getX());
131
132 animator.setPosition(.5f);
133 assertEquals(50f, mTestView.getX());
134
135 animator.setPosition(1);
136 assertEquals(50f, mTestView.getX());
137 }
138
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400139 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -0500140 public void testOnAnimationAtStartCallback() {
141 TouchAnimator animator = new TouchAnimator.Builder()
142 .setListener(mTouchListener)
143 .build();
144
145 // Called on init.
146 animator.setPosition(0);
147 verifyOnAnimationAtStart(1);
148
149 // Not called from same state.
150 animator.setPosition(0);
151 verifyOnAnimationAtStart(1);
152
153 // Called after starting and moving back to start.
154 animator.setPosition(.5f);
155 animator.setPosition(0);
156 verifyOnAnimationAtStart(2);
157
158 // Called when move from end to end.
159 animator.setPosition(1);
160 animator.setPosition(0);
161 verifyOnAnimationAtStart(3);
162 }
163
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400164 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -0500165 public void testOnAnimationAtEndCallback() {
166 TouchAnimator animator = new TouchAnimator.Builder()
167 .setListener(mTouchListener)
168 .build();
169
170 // Called on init.
171 animator.setPosition(1);
172 verifyOnAnimationAtEnd(1);
173
174 // Not called from same state.
175 animator.setPosition(1);
176 verifyOnAnimationAtEnd(1);
177
178 // Called after starting and moving back to end.
179 animator.setPosition(.5f);
180 animator.setPosition(1);
181 verifyOnAnimationAtEnd(2);
182
183 // Called when move from end to end.
184 animator.setPosition(0);
185 animator.setPosition(1);
186 verifyOnAnimationAtEnd(3);
187 }
188
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -0400189 @Test
Jason Monkf6a3cf92016-02-29 13:01:08 -0500190 public void testOnAnimationStartedCallback() {
191 TouchAnimator animator = new TouchAnimator.Builder()
192 .setListener(mTouchListener)
193 .build();
194
195 // Called on init.
196 animator.setPosition(.5f);
197 verifyOnAnimationStarted(1);
198
199 // Not called from same state.
200 animator.setPosition(.6f);
201 verifyOnAnimationStarted(1);
202
203 // Called after going to end then moving again.
204 animator.setPosition(1);
205 animator.setPosition(.5f);
206 verifyOnAnimationStarted(2);
207
208 // Called after moving to start then moving again.
209 animator.setPosition(0);
210 animator.setPosition(.5f);
211 verifyOnAnimationStarted(3);
212 }
213
214 // TODO: Add test for interpolator.
215
216 private void verifyOnAnimationAtStart(int times) {
217 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtStart();
218 }
219
220 private void verifyOnAnimationAtEnd(int times) {
221 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtEnd();
222 }
223
224 private void verifyOnAnimationStarted(int times) {
225 Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationStarted();
226 }
227}