TouchUtils和ActivityInstrumentationTestCase2用户事件单元测试用例不工作



目前我在单元测试框架方面有点吃力。。。

我想做什么

  • 我需要在屏幕上模拟两次点击(100100)和(400400),持续时间相差很小
  • 我需要模拟长按在屏幕上点击,比如说(200,200)
  • 用户单击后,本机代码将运行并对位图执行像素操作
  • 此测试将针对多对点集运行,用于分析系统的运行时性能

这是我被卡住的地方

  • 我使用activityInstrumentationTestCase2和touchUtils来处理用户单击事件
  • TouchUtils.longClickView(InstrumentationTestCase测试,View v)运行良好;我能够正确地检测到长按事件,但测试用例甚至在我的UI线程中的计算/渲染完成之前就完成了;在这种情况下,如何阻止测试退出
  • 我如何模拟屏幕上特定位置的2/3用户点击?causeTouchUtils.clickView(InstrumentationTestCase测试,View v)只会模拟用户在屏幕中央点击。。。如何正确操作

这些是我尝试过的东西,但似乎我错过了一些东西:

  • TouchUtils.longClickView(InstrumentationTestCase测试,View v)运行良好。。。用于创建longClickView。。甚至我也能够通过引入ACTION_DOWN和ACTION_UP事件之间的时间延迟,在特定的屏幕位置创建longClickView()。。请参阅随附的代码
  • 我能够在特定的屏幕位置实现用户点击事件,但我遇到了一个奇怪的问题。。当我从测试用例中显示MotionEvent(100100)时。。该框架总是在Y事件中添加"-76"。。不确定为什么会出现这种偏差。。。我暂时通过在输入数据(100176)中添加76来解决这个问题。。有人面临类似的问题吗
  • 即使采用这种方法,时机也非常关键。。因为如果我在ACTION_DOWN和ACTION_UP之间放置更多延迟,则事件被检测为longClickPress。。。如果我少放一点。。。"第二个"单击事件(ACTION_DOWN+ACTION_UP)被检测为DoubleTapEvent

ACTION_UP和ACTION_DOWN的正确时序组合应该是什么。。对于单用户点击事件模拟????????

    @Test
    public void testClick(){
    List<Points> pointSequence = new ArrayList<Points>();
    Log.d(TAG, "FirClick Start Timing : " + SystemClock.uptimeMillis());
    pointSequence.add(new Points(100f,176f));
    pointSequence.add(new Points(100f,176f));       
    singleClickSimulation(pointSequence,false);
    }       
    private void singleClickSimulation(List<Points> pointSequence, Boolean addDelay) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    // NOTE : If I do not place this then the event is detected as doubleTap.
    eventTime += 100;
    Instrumentation inst = getInstrumentation();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
    inst.sendPointerSync(event);                
    //eventTime = SystemClock.uptimeMillis();
    pointSequence.remove(0);        
    //This delay I have added just to test; whether there is enough time for pixel manipulation or not, actually it would be used only at the end of the run of all the test cases for single user click
    if(addDelay){
    eventTime = SystemClock.uptimeMillis() + 3000;
    }
    eventTime += 25;
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
    inst.sendPointerSync(event);
    pointSequence.remove(0);
}

部分答案:

仍然是为了找到在创建用户点击事件时导致76px偏差的原因。

我已经能够正确地模拟50毫秒差异的单用户点击事件。我必须将2次点击间隔至少300ms;以便"SingleTapConfirmed"可以为GeastureDetector类自行启动。。

private void singleClickSimulation(List<Points> pointSequence,boolean addDelay) {
    long downTime = SystemClock.uptimeMillis();
    // event time MUST be retrieved only by this way!
    long eventTime = SystemClock.uptimeMillis();
            // This additional time was added between 2 successive clicks 
            // to make sure that "singleTapConfirmed" event can get fired for "GeastureDetector" class.
    eventTime +=400;
    downTime +=400;
    Instrumentation inst = getInstrumentation();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
    inst.sendPointerSync(event);                
    //eventTime = SystemClock.uptimeMillis();
    pointSequence.remove(0);        

            //50 ms timedelay between ACTION_DOWN and ACTION_UP works well      
    eventTime += 50;
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
    inst.sendPointerSync(event);
    pointSequence.remove(0);
}

我没有使用TouchUtils,而是使用了。

public class TestTouchUtils extends   ActivityInstrumentationTestCase2<TheActivity> {
    public void testTouchUtils() throws Throwable {
        final View button = getActivity().findViewById(R.id.button)
        getActivity().runOnUiThread( new Runnable() {
             public void run() {
                button.performClick();
             }
        });
    }
}

它不会更新仪器/设备上的UI,但会更新仪器内的变量。在我的例子中,按钮更新片段的适配器。如果我只使用,触摸工具。单击查看(此,按钮)设备上的UI已更新,但变量未更新。

所以我也和TouchUtils结合在一起。我不知道为什么,但它是有效的。

相关内容

最新更新