FileDocCategorySizeDatePackage
VelocityTest.javaAPI DocAndroid 5.1 API10632Thu Mar 12 22:22:12 GMT 2015android.view

VelocityTest

public class VelocityTest extends android.test.InstrumentationTestCase
Exercises {@link android.view.VelocityTracker} to compute correct velocity.
To launch this test, use :
./development/testrunner/runtest.py framework -c android.view.VelocityTest

Fields Summary
Constructors Summary
Methods Summary
private voidaddMotionEvent(VelocityTracker vt, int x, int y, long time, int action)

        MotionEvent me = MotionEvent.obtain(time, time, action, x, y, 0);
        vt.addMovement(me);
        me.recycle();
    
private voidassertEqualFuzzy(float expected, float actual, float threshold)
Float imprecision of the average computations and filtering (removing last MotionEvent for N > 3) implies that tests accepts some approximated values.

        boolean fuzzyEqual = actual >= expected - threshold && actual <= expected + threshold;
        Assert.assertTrue("Expected: <"+expected+"> but was: <"+actual+
                "> while accepting a variation of: <"+threshold+">", fuzzyEqual);
    
private voidassertGreater(float minExpected, float actual)

        Assert.assertTrue("Expected: minimum <"+minExpected+"> but was: <"+actual+">",
                actual > minExpected);
    
private voidassertLower(float maxExpected, float actual)

        Assert.assertTrue("Expected: maximum <"+maxExpected+"> but was: <"+actual+">",
                actual < maxExpected);
    
private voiddrag(VelocityTracker vt, int startX, int endX, int startY, int endY, int steps, long startime, int duration)
Simulate a drag by giving directly MotionEvents to the VelocityTracker using a linear interpolator

        drag(vt, startX, endX, startY, endY, steps, startime, duration, new LinearInterpolator());
    
private voiddrag(VelocityTracker vt, int startX, int endX, int startY, int endY, int steps, long startime, int duration, android.view.animation.Interpolator interpolator)
Simulate a drag by giving directly MotionEvents to the VelocityTracker using a given interpolator

        addMotionEvent(vt, startX, startY, startime, MotionEvent.ACTION_DOWN);
        float dt = duration / (float)steps;
        int distX = endX - startX;
        int distY = endY - startY;
        for (int i=1; i<steps-1; i++) {
            float ii = interpolator.getInterpolation(i / (float)steps);
            int x = (int) (startX + distX * ii);
            int y = (int) (startY + distY * ii);
            long time = startime + (int) (i * dt);
            addMotionEvent(vt, x, y, time, MotionEvent.ACTION_MOVE);
        }
        addMotionEvent(vt, endX, endY, startime + duration, MotionEvent.ACTION_UP);
    
public voidtestClear()
Test that {@link android.view.VelocityTracker}.clear() clears the previous values after a call to computeCurrentVelocity()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 300);
        vt.computeCurrentVelocity(1);
        assertFalse("Velocity should not be null", vt.getXVelocity() == 0.0f);
        assertFalse("Velocity should not be null", vt.getYVelocity() == 0.0f);
        vt.clear();
        vt.computeCurrentVelocity(1);
        assertEquals(0.0f, vt.getXVelocity());
        assertEquals(0.0f, vt.getYVelocity());
        vt.recycle();
    
public voidtestDragAcceleration()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 15, t, 400, new AccelerateInterpolator());
        vt.computeCurrentVelocity(1000);
        assertGreater(250.0f, vt.getXVelocity());
        assertGreater(250.0f, vt.getYVelocity());
        vt.recycle();
    
public voidtestDragDeceleration()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 15, t, 400, new DecelerateInterpolator());
        vt.computeCurrentVelocity(1000);
        assertLower(250.0f, vt.getXVelocity());
        assertLower(250.0f, vt.getYVelocity());
        vt.recycle();
    
public voidtestDragLinearHorizontal()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        // 100px in 400ms => 250px/s
        drag(vt, 100, 200, 200, 200, 15, t, 400);
        vt.computeCurrentVelocity(1000);
        assertEquals(0.0f, vt.getYVelocity());
        assertEqualFuzzy(250.0f, vt.getXVelocity(), 4f);
        vt.recycle();
    
public voidtestDragLinearVertical()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        // 100px in 400ms => 250px/s
        drag(vt, 200, 200, 100, 200, 15, t, 400);
        vt.computeCurrentVelocity(1000);
        assertEquals(0.0f, vt.getXVelocity());
        assertEqualFuzzy(250.0f, vt.getYVelocity(), 4f);
        vt.recycle();
    
public voidtestDragWith2Points()
Test dragging with two points only (velocity must be an exact value)

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        // 100px, 2 steps, 100ms => 1000px/s
        drag(vt, 100, 200, 100, 200, 2, t, 100);
        vt.computeCurrentVelocity(1000);
        assertEquals(1000.0f, vt.getXVelocity());
        assertEquals(1000.0f, vt.getYVelocity());
        vt.recycle();
    
public voidtestInitialCondiditions()

        VelocityTracker vt = VelocityTracker.obtain();
        assertNotNull(vt);
        vt.recycle();
    
public voidtestStabilityInNbPoints()
Velocity is independent of the number of points used during the same interval

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 400); // 10 steps over 400ms
        vt.computeCurrentVelocity(1);
        float firstX = vt.getXVelocity();
        float firstY = vt.getYVelocity();
        vt.clear();
        drag(vt, 100, 200, 100, 200, 20, t, 400); // 20 steps over 400ms
        vt.computeCurrentVelocity(1);
        float secondX = vt.getXVelocity();
        float secondY = vt.getYVelocity();
        assertEqualFuzzy(firstX, secondX, 0.1f);
        assertEqualFuzzy(firstY, secondY, 0.1f);
        vt.recycle();
    
public voidtestStabilityInSpace()
Velocity is independent of the position of the events, it only depends on their relative distance.

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 400);
        vt.computeCurrentVelocity(1);
        float firstX = vt.getXVelocity();
        float firstY = vt.getYVelocity();
        vt.clear();
        drag(vt, 200, 300, 200, 300, 10, t, 400); // 100px further
        vt.computeCurrentVelocity(1);
        float secondX = vt.getXVelocity();
        float secondY = vt.getYVelocity();
        assertEqualFuzzy(firstX, secondX, 0.1f);
        assertEqualFuzzy(firstY, secondY, 0.1f);
        vt.recycle();
    
public voidtestStabilityInTime()
Velocity is independent of the time when the events occurs, it only depends on delays between the events.

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 400);
        vt.computeCurrentVelocity(1);
        float firstX = vt.getXVelocity();
        float firstY = vt.getYVelocity();
        vt.clear();
        drag(vt, 100, 200, 100, 200, 10, t + 3600*1000, 400); // on hour later
        vt.computeCurrentVelocity(1);
        float secondX = vt.getXVelocity();
        float secondY = vt.getYVelocity();
        assertEqualFuzzy(firstX, secondX, 0.1f);
        assertEqualFuzzy(firstY, secondY, 0.1f);
        vt.recycle();
    
public voidtestStabilityOfComputation()
Test that calls to {@link android.view.VelocityTracker}.computeCurrentVelocity() will output same values when using the same data.

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 300);
        vt.computeCurrentVelocity(1);
        float firstX = vt.getXVelocity();
        float firstY = vt.getYVelocity();
        vt.computeCurrentVelocity(1);
        float secondX = vt.getXVelocity();
        float secondY = vt.getYVelocity();
        assertEquals(firstX, secondX);
        assertEquals(firstY, secondY);
        vt.recycle();
    
public voidtestStabilityOfUnits()
Test the units parameter of {@link android.view.VelocityTracker}.computeCurrentVelocity()

        long t = System.currentTimeMillis();
        VelocityTracker vt = VelocityTracker.obtain();
        drag(vt, 100, 200, 100, 200, 10, t, 300);
        vt.computeCurrentVelocity(1);
        float firstX = vt.getXVelocity();
        float firstY = vt.getYVelocity();
        vt.computeCurrentVelocity(1000);
        float secondX = vt.getXVelocity();
        float secondY = vt.getYVelocity();
        assertEqualFuzzy(firstX, secondX / 1000.0f, 0.1f);
        assertEqualFuzzy(firstY, secondY / 1000.0f, 0.1f);
        vt.recycle();