TouchAnimatorTest.java revision fc2b64e3e80d44dabd6c32eb8cd69893d39476a9
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 testSetValueInt() {
60        TouchAnimator animator = new TouchAnimator.Builder()
61                .addInt(mTestView, "top", 0, 50)
62                .build();
63
64        animator.setPosition(0);
65        assertEquals(0, mTestView.getTop());
66
67        animator.setPosition(.5f);
68        assertEquals(25, mTestView.getTop());
69
70        animator.setPosition(1);
71        assertEquals(50, mTestView.getTop());
72    }
73
74    @Test
75    public void testStartDelay() {
76        TouchAnimator animator = new TouchAnimator.Builder()
77                .addFloat(mTestView, "x", 0, 50)
78                .setStartDelay(.5f)
79                .build();
80
81        animator.setPosition(0);
82        assertEquals(0f, mTestView.getX());
83
84        animator.setPosition(.5f);
85        assertEquals(0f, mTestView.getX());
86
87        animator.setPosition(.75f);
88        assertEquals(25f, mTestView.getX());
89
90        animator.setPosition(1);
91        assertEquals(50f, mTestView.getX());
92    }
93
94    @Test
95    public void testEndDelay() {
96        TouchAnimator animator = new TouchAnimator.Builder()
97                .addFloat(mTestView, "x", 0, 50)
98                .setEndDelay(.5f)
99                .build();
100
101        animator.setPosition(0);
102        assertEquals(0f, mTestView.getX());
103
104        animator.setPosition(.25f);
105        assertEquals(25f, mTestView.getX());
106
107        animator.setPosition(.5f);
108        assertEquals(50f, mTestView.getX());
109
110        animator.setPosition(1);
111        assertEquals(50f, mTestView.getX());
112    }
113
114    @Test
115    public void testOnAnimationAtStartCallback() {
116        TouchAnimator animator = new TouchAnimator.Builder()
117                .setListener(mTouchListener)
118                .build();
119
120        // Called on init.
121        animator.setPosition(0);
122        verifyOnAnimationAtStart(1);
123
124        // Not called from same state.
125        animator.setPosition(0);
126        verifyOnAnimationAtStart(1);
127
128        // Called after starting and moving back to start.
129        animator.setPosition(.5f);
130        animator.setPosition(0);
131        verifyOnAnimationAtStart(2);
132
133        // Called when move from end to end.
134        animator.setPosition(1);
135        animator.setPosition(0);
136        verifyOnAnimationAtStart(3);
137    }
138
139    @Test
140    public void testOnAnimationAtEndCallback() {
141        TouchAnimator animator = new TouchAnimator.Builder()
142                .setListener(mTouchListener)
143                .build();
144
145        // Called on init.
146        animator.setPosition(1);
147        verifyOnAnimationAtEnd(1);
148
149        // Not called from same state.
150        animator.setPosition(1);
151        verifyOnAnimationAtEnd(1);
152
153        // Called after starting and moving back to end.
154        animator.setPosition(.5f);
155        animator.setPosition(1);
156        verifyOnAnimationAtEnd(2);
157
158        // Called when move from end to end.
159        animator.setPosition(0);
160        animator.setPosition(1);
161        verifyOnAnimationAtEnd(3);
162    }
163
164    @Test
165    public void testOnAnimationStartedCallback() {
166        TouchAnimator animator = new TouchAnimator.Builder()
167                .setListener(mTouchListener)
168                .build();
169
170        // Called on init.
171        animator.setPosition(.5f);
172        verifyOnAnimationStarted(1);
173
174        // Not called from same state.
175        animator.setPosition(.6f);
176        verifyOnAnimationStarted(1);
177
178        // Called after going to end then moving again.
179        animator.setPosition(1);
180        animator.setPosition(.5f);
181        verifyOnAnimationStarted(2);
182
183        // Called after moving to start then moving again.
184        animator.setPosition(0);
185        animator.setPosition(.5f);
186        verifyOnAnimationStarted(3);
187    }
188
189    // TODO: Add test for interpolator.
190
191    private void verifyOnAnimationAtStart(int times) {
192        Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtStart();
193    }
194
195    private void verifyOnAnimationAtEnd(int times) {
196        Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationAtEnd();
197    }
198
199    private void verifyOnAnimationStarted(int times) {
200        Mockito.verify(mTouchListener, Mockito.times(times)).onAnimationStarted();
201    }
202}
203