blob: 25b94d9f8094a5b94e36a6919027e16a8f79e92f [file] [log] [blame]
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view.cts;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetClass;
import dalvik.annotation.TestTargetNew;
import dalvik.annotation.TestTargets;
import android.test.AndroidTestCase;
import android.view.MotionEvent;
import android.view.VelocityTracker;
/**
* Test {@link VelocityTracker}.
*/
@TestTargetClass(VelocityTracker.class)
public class VelocityTrackerTest extends AndroidTestCase {
private static final float ERROR_TOLERANCE = 0.0001f;
@Override
protected void setUp() throws Exception {
super.setUp();
}
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "obtain",
args = {}
)
public void testObtain() {
VelocityTracker vt = VelocityTracker.obtain();
assertNotNull(vt);
vt.recycle();
}
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "recycle",
args = {}
)
public void testRecycle() {
VelocityTracker vt = VelocityTracker.obtain();
assertNotNull(vt);
vt.recycle();
VelocityTracker vt2 = VelocityTracker.obtain();
assertEquals(vt, vt2);
}
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "computeCurrentVelocity",
args = {int.class}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "getXVelocity",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "getYVelocity",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "addMovement",
args = {android.view.MotionEvent.class}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "clear",
args = {}
)
})
public void testComputeCurrentVelocity() {
// XVelocity & YVelocity calculated by the original algorithm from android
float XVelocity;
float YVelocity;
VelocityTracker vt = VelocityTracker.obtain();
assertNotNull(vt);
MotionEvent me = MotionEvent.obtain(0L, 10L, MotionEvent.ACTION_MOVE, .0f, .0f, 0);
vt.clear();
me.addBatch(20L, 20, 20, .0f, .0f, 0);
vt.addMovement(me);
vt.computeCurrentVelocity(1);
XVelocity = 2.0f;
YVelocity = 2.0f;
assertEquals(XVelocity, vt.getXVelocity(), ERROR_TOLERANCE);
assertEquals(YVelocity, vt.getYVelocity(), ERROR_TOLERANCE);
vt.computeCurrentVelocity(10);
XVelocity = 20.0f;
YVelocity = 20.0f;
assertEquals(XVelocity, vt.getXVelocity(), ERROR_TOLERANCE);
assertEquals(YVelocity, vt.getYVelocity(), ERROR_TOLERANCE);
for (int i = 3; i < 10; i++) {
me.addBatch((long)i * 10, (float)i * 10, (float)i * 10, .0f, .0f, 0);
}
vt.clear();
vt.addMovement(me);
vt.computeCurrentVelocity(1);
XVelocity = 1.1479408f;
YVelocity = 1.1479408f;
assertEquals(XVelocity, vt.getXVelocity(), ERROR_TOLERANCE);
assertEquals(YVelocity, vt.getYVelocity(), ERROR_TOLERANCE);
vt.clear();
me.addBatch(100L, 100, 100, .0f, .0f, 0);
vt.addMovement(me);
vt.computeCurrentVelocity(1);
XVelocity = 1.1284428f;
YVelocity = 1.1284428f;
assertEquals(XVelocity, vt.getXVelocity(), ERROR_TOLERANCE);
assertEquals(YVelocity, vt.getYVelocity(), ERROR_TOLERANCE);
me.recycle();
me = MotionEvent.obtain(0L, 110L, MotionEvent.ACTION_UP, 100f, 100f, 0);
vt.addMovement(me);
vt.computeCurrentVelocity(1);
XVelocity = 1.1284428f;
YVelocity = 1.1284428f;
assertEquals(XVelocity, vt.getXVelocity(), ERROR_TOLERANCE);
assertEquals(YVelocity, vt.getYVelocity(), ERROR_TOLERANCE);
me.recycle();
vt.recycle();
}
}