gpt4 book ai didi

java - Android Espresso 拖放错误注入(inject)事件

转载 作者:行者123 更新时间:2023-11-30 00:40:56 28 4
gpt4 key购买 nike

我已经处理这个问题一整天了。问题是当我尝试在 Instrumental Espresso 测试中拖动某些东西时出现以下错误。

Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

当我调用这个方法时会发生这种情况

 onView(withId(R.id.any_id)).perform(CustomViewActions.touchAndDrag(200, 200));

自定义方法

 public static ViewAction touchAndDrag(final float x, final float y, final long delay) {
return new ViewAction() {

@Override
public void perform(UiController uiController, final View view) {
// Get view absolute position
sendLinearSwipe(uiController,coordinatesClickOn,coordinatesMoveTo,precision,2000);

};

}

swipeLinaer 取自 Espresso 的源代码

  private static Swiper.Status sendLinearSwipe(UiController uiController, float[] startCoordinates,
float[] endCoordinates, float[] precision, int duration) {
checkNotNull(uiController);
checkNotNull(startCoordinates);
checkNotNull(endCoordinates);
checkNotNull(precision);

float[][] steps = interpolate(startCoordinates, endCoordinates, SWIPE_EVENT_COUNT);
final int delayBetweenMovements = duration / steps.length;

MotionEvent downEvent = MotionEvents.sendDown(uiController, startCoordinates, precision).down;
try {
for (int i = 0; i < steps.length; i++) {
if (!MotionEvents.sendMovement(uiController, downEvent, steps[i])) {
Log.e(TAG, "Injection of move event as part of the swipe failed. Sending cancel event.");
MotionEvents.sendCancel(uiController, downEvent);
return Swiper.Status.FAILURE;
}

long desiredTime = downEvent.getDownTime() + delayBetweenMovements * i;
long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
if (timeUntilDesired > 10) {
uiController.loopMainThreadForAtLeast(timeUntilDesired);
}
}

if (!MotionEvents.sendUp(uiController, downEvent, endCoordinates)) {
Log.e(TAG, "Injection of up event as part of the swipe failed. Sending cancel event.");
MotionEvents.sendCancel(uiController, downEvent);
return Swiper.Status.FAILURE;
}
} finally {
downEvent.recycle();
}
return Swiper.Status.SUCCESS;
}

问题是这不起作用 ONLY IF TOUCH 监听器已设置

像那样

    anyView.setOnTouchListener(new MyTouchListener());

所以设置什么样的监听器并不重要,但设置它的事实会导致错误。

 private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}

我不知道如何解决这个问题。

如果有任何帮助或建议,我将不胜感激。

最佳答案

Android 管理滑动对象的方式不是很清楚。当您尝试拖动某物时,Android 会生成一张跟随您手指的物体图片,让您产生拖动的想法。问题是产生这种效果的系统组件不是来自您的应用程序,而是来自另一个应用程序,因此 Android 认为在拖动过程中您正在触摸另一个“应用程序”。当您不小心触摸系统键盘时会出现同样的问题。

解决方案是使用系统证书对您的 apk 进行签名,并在您的 list 上添加 INJECT_EVENTS 权限。

关于java - Android Espresso 拖放错误注入(inject)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592946/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com