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