use of android.view.MotionEvent.PointerCoords in project robolectric by robolectric.
the class MotionEventTest method testTransformShouldApplyMatrixToPointsAndPreserveRawPosition.
@Test
public void testTransformShouldApplyMatrixToPointsAndPreserveRawPosition() {
// Generate some points on a circle.
// Each point 'i' is a point on a circle of radius ROTATION centered at (3,2) at an angle
// of ARC * i degrees clockwise relative to the Y axis.
// The geometrical representation is irrelevant to the test, it's just easy to generate
// and check rotation. We set the orientation to the same angle.
// Coordinate system: down is increasing Y, right is increasing X.
final float pi180 = (float) (Math.PI / 180);
final float radius = 10;
final float arc = 36;
final float rotation = arc * 2;
final int pointerCount = 11;
final int[] pointerIds = new int[pointerCount];
final PointerCoords[] pointerCoords = new PointerCoords[pointerCount];
for (int i = 0; i < pointerCount; i++) {
final PointerCoords c = new PointerCoords();
final float angle = (float) (i * arc * pi180);
pointerIds[i] = i;
pointerCoords[i] = c;
c.x = (float) (Math.sin(angle) * radius + 3);
c.y = (float) (-Math.cos(angle) * radius + 2);
c.orientation = angle;
}
final MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, pointerCount, pointerIds, pointerCoords, 0, 0, 0, 0, 0, 0, 0);
final float originalRawX = 0 + 3;
final float originalRawY = -radius + 2;
// Check original raw X and Y assumption.
assertThat(event).rawX().isWithin(TOLERANCE).of(originalRawX);
assertThat(event).rawY().isWithin(TOLERANCE).of(originalRawY);
// Now translate the motion event so the circle's origin is at (0,0).
event.offsetLocation(-3, -2);
// Offsetting the location should preserve the raw X and Y of the first point.
assertThat(event).rawX().isWithin(TOLERANCE).of(originalRawX);
assertThat(event).rawY().isWithin(TOLERANCE).of(originalRawY);
// Apply a rotation about the origin by ROTATION degrees clockwise.
Matrix matrix = new Matrix();
matrix.setRotate(rotation);
event.transform(matrix);
// Check the points.
for (int i = 0; i < pointerCount; i++) {
final PointerCoords c = pointerCoords[i];
event.getPointerCoords(i, c);
final float angle = (float) ((i * arc + rotation) * pi180);
assertThat(event).pointerCoords(i).x().isWithin(TOLERANCE).of((float) (Math.sin(angle) * radius));
assertThat(event).pointerCoords(i).y().isWithin(TOLERANCE).of(-(float) Math.cos(angle) * radius);
assertThat(Math.tan(c.orientation)).isWithin(0.1f).of(Math.tan(angle));
}
// Applying the transformation should preserve the raw X and Y of the first point.
assertThat(event).rawX().isWithin(TOLERANCE).of(originalRawX);
assertThat(event).rawY().isWithin(TOLERANCE).of(originalRawY);
}
use of android.view.MotionEvent.PointerCoords in project robolectric by robolectric.
the class MotionEventTest method testGetCurrentDataWithTwoPointers.
@Test
public void testGetCurrentDataWithTwoPointers() {
PointerCoords coords0 = PointerCoordsBuilder.newBuilder().setCoords(10.0f, 20.0f).setPressure(1.2f).setSize(2.0f).setTool(1.2f, 1.4f).build();
PointerCoords coords1 = PointerCoordsBuilder.newBuilder().setCoords(30.0f, 40.0f).setPressure(1.4f).setSize(3.0f).setTouch(2.2f, 0.6f).build();
PointerProperties properties0 = PointerPropertiesBuilder.newBuilder().setId(0).setToolType(MotionEvent.TOOL_TYPE_FINGER).build();
PointerProperties properties1 = PointerPropertiesBuilder.newBuilder().setId(1).setToolType(MotionEvent.TOOL_TYPE_FINGER).build();
motionEventDynamic = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, new PointerProperties[] { properties0, properties1 }, new PointerCoords[] { coords0, coords1 }, 0, 0, 1.0f, 1.0f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
// We expect to have data for two pointers
assertThat(motionEventDynamic).pointerId(0).isEqualTo(0);
assertThat(motionEventDynamic).pointerId(1).isEqualTo(1);
assertThat(motionEventDynamic).hasPointerCount(2);
assertThat(motionEventDynamic).hasFlags(0);
MotionEventEqualitySubject.assertThat(motionEventDynamic).pointerCoords(0).isEqualToWithinTolerance(coords0, TOLERANCE);
MotionEventEqualitySubject.assertThat(motionEventDynamic).pointerCoords(1).isEqualToWithinTolerance(coords1, TOLERANCE);
assertThat(motionEventDynamic).pointerProperties(0).isEqualTo(properties0);
assertThat(motionEventDynamic).pointerProperties(1).isEqualTo(properties1);
}
use of android.view.MotionEvent.PointerCoords in project robolectric by robolectric.
the class MotionEventTest method testObtainFromPropertyArrays.
@Test
public void testObtainFromPropertyArrays() {
PointerCoords coords0 = PointerCoordsBuilder.newBuilder().setCoords(X_3F, Y_4F).setPressure(PRESSURE_1F).setSize(SIZE_1F).setTool(1.2f, 1.4f).build();
PointerCoords coords1 = PointerCoordsBuilder.newBuilder().setCoords(X_3F + 1.0f, Y_4F - 2.0f).setPressure(PRESSURE_1F + 0.2f).setSize(SIZE_1F + 0.5f).setTouch(2.2f, 0.6f).build();
PointerProperties properties0 = PointerPropertiesBuilder.newBuilder().setId(0).setToolType(MotionEvent.TOOL_TYPE_FINGER).build();
PointerProperties properties1 = PointerPropertiesBuilder.newBuilder().setId(1).setToolType(MotionEvent.TOOL_TYPE_FINGER).build();
motionEventDynamic = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, new PointerProperties[] { properties0, properties1 }, new PointerCoords[] { coords0, coords1 }, META_STATE, 0, X_PRECISION_3F, Y_PRECISION_4F, DEVICE_ID_1, EDGE_FLAGS, InputDevice.SOURCE_TOUCHSCREEN, 0);
// We expect to have data for two pointers
assertThat(motionEventDynamic).hasPointerCount(2);
assertThat(motionEventDynamic).hasFlags(0);
assertThat(motionEventDynamic).pointerId(0).isEqualTo(0);
assertThat(motionEventDynamic).pointerId(1).isEqualTo(1);
MotionEventEqualitySubject.assertThat(motionEventDynamic).pointerCoords(0).isEqualToWithinTolerance(coords0, TOLERANCE);
MotionEventEqualitySubject.assertThat(motionEventDynamic).pointerCoords(1).isEqualToWithinTolerance(coords1, TOLERANCE);
assertThat(motionEventDynamic).pointerProperties(0).isEqualTo(properties0);
assertThat(motionEventDynamic).pointerProperties(1).isEqualTo(properties1);
}
use of android.view.MotionEvent.PointerCoords in project robolectric by robolectric.
the class ShadowMotionEvent method nativeAddBatch.
@Implementation(minSdk = LOLLIPOP)
@HiddenApi
protected static void nativeAddBatch(long nativePtr, long eventTimeNanos, PointerCoords[] pointerCoordsObjArray, int metaState) {
NativeInput.MotionEvent event = getNativeMotionEvent(nativePtr);
int pointerCount = event.getPointerCount();
validatePointerCoordsObjArray(pointerCoordsObjArray, pointerCount);
NativeInput.PointerCoords[] rawPointerCoords = new NativeInput.PointerCoords[pointerCount];
for (int i = 0; i < pointerCount; i++) {
PointerCoords pointerCoordsObj = pointerCoordsObjArray[i];
checkNotNull(pointerCoordsObj);
rawPointerCoords[i] = pointerCoordsToNative(pointerCoordsObj, event.getXOffset(), event.getYOffset());
}
event.addSample(eventTimeNanos, rawPointerCoords);
event.setMetaState(event.getMetaState() | metaState);
}
use of android.view.MotionEvent.PointerCoords in project robotium by RobotiumTech.
the class Rotator method generateRotateGesture.
public void generateRotateGesture(int size, PointF center1, PointF center2) {
double incrementFactor = 0;
float startX1 = center1.x;
float startY1 = center1.y;
float startX2 = center2.x;
float startY2 = center2.y;
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
// pointer 1
float x1 = startX1;
float y1 = startY1;
// pointer 2
float x2 = startX2;
float y2 = startY2;
PointerCoords[] pointerCoords = new PointerCoords[2];
PointerCoords pc1 = new PointerCoords();
PointerCoords pc2 = new PointerCoords();
pc1.x = x1;
pc1.y = y1;
pc1.pressure = 1;
pc1.size = 1;
pc2.x = x2;
pc2.y = y2;
pc2.pressure = 1;
pc2.size = 1;
pointerCoords[0] = pc1;
pointerCoords[1] = pc2;
PointerProperties[] pointerProperties = new PointerProperties[2];
PointerProperties pp1 = new PointerProperties();
PointerProperties pp2 = new PointerProperties();
pp1.id = 0;
pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
pp2.id = 1;
pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;
pointerProperties[0] = pp1;
pointerProperties[1] = pp2;
MotionEvent event;
// send the initial touches
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, // metaState, buttonState
0, // metaState, buttonState
0, // x precision
1, // y precision
1, // deviceId, edgeFlags
0, // deviceId, edgeFlags
0, InputDevice.SOURCE_TOUCHSCREEN, // source, flags
0);
_instrument.sendPointerSync(event);
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_DOWN + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
_instrument.sendPointerSync(event);
switch(size) {
case 0:
{
incrementFactor = 0.01;
}
break;
case 1:
{
incrementFactor = 0.1;
}
break;
}
for (double i = 0; i < Math.PI; i += incrementFactor) {
eventTime += EVENT_TIME_INTERVAL_MS;
pointerCoords[0].x += Math.cos(i);
pointerCoords[0].y += Math.sin(i);
pointerCoords[1].x += Math.cos(i + Math.PI);
pointerCoords[1].y += Math.sin(i + Math.PI);
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
_instrument.sendPointerSync(event);
}
// and remove them fingers from the screen
eventTime += EVENT_TIME_INTERVAL_MS;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_UP + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
_instrument.sendPointerSync(event);
eventTime += EVENT_TIME_INTERVAL_MS;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 1, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
_instrument.sendPointerSync(event);
}
Aggregations