2017-03-04 6 views
1

私はこの問題を一日中処理しています。問題は、Instrumental Espressoテストで何かをドラッグしようとすると、次のエラーが表示されることです。Android Espresso Drag and Dropエラーイベントを注入する

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

私はカスタムメソッド

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は問題が目でエスプレッソ

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; 
    } 

の情報源から取られた

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

このメソッドを呼び出すと、この問題が発生しましたこの時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; 
      } 
     } 
    } 

この問題の解決方法はわかりません。

私は何か助けや助言に感謝します。

答えて

0

Androidがスワイプオブジェクトを管理する方法はあまり明確ではありません。 何かをドラッグしようとすると、アンドロイドはあなたの指に続くその物の画像を生成し、あなたにドラッグのアイデアを与えます。 問題は、そのエフェクトを生成するシステムのコンポーネントがアプリケーションからではなく、別のアプリケーションから来ているため、Androidはドラッグ中に別の「アプリ」に触れていると考えています。 誤ってシステムキーボードに触れると同じ問題が発生します。

解決策は、システム証明書を使用してapkに署名し、マニフェストにINJECT_EVENTS権限を追加することです。

関連する問題