简体   繁体   English

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

[英]TouchUtils and ActivityInstrumentationTestCase2 user event Unit test case not working

Currently I am struggling a little with the Unit test framework... 目前,我在单元测试框架方面有些挣扎...

what I am trying to do 我想做什么

  • I need to simulate 2 clicks on the screen (100,100) and (400,400) at a small duration difference. 我需要在屏幕上以较小的持续时间差异模拟两次点击(100,100)和(400,400)。
  • I need to simulate a longPressClick on the screen Let's say ( 200, 200 ) 我需要模拟屏幕上的longPressClick,假设(200,200)
  • Upon User click the native code runs and performs pixel manipulation on Bitmap. 用户单击后,本机代码将运行并在位图上执行像素操作。
  • This test is going to run for multiple pair-sets of points, for analysing the runtime performance of the system 该测试将针对多个点对运行,以分析系统的运行时性能。

Here's where I am stuck 这就是我被困住的地方

  • I am using activityInstrumentationTestCase2 and touchUtils for the user click events. 我正在为用户点击事件使用activityInstrumentationTestCase2和touchUtils。
  • TouchUtils.longClickView(InstrumentationTestCase test, View v) works fine ; TouchUtils.longClickView(InstrumentationTestCase test,View v)工作正常; I'm able to detect the long pressing event propely , But test-case finishes even before the calculation / rendering is complete in my UI thread ; 我能够适当地检测到长按事件,但是测试用例甚至在UI线程中的计算/渲染完成之前就已经完成; how do I stop the test from exiting in this case ? 在这种情况下,如何停止测试?
  • How do i simulate 2 / 3 user clicks @ particular location on screen ? 我如何模拟2/3用户在屏幕上的特定位置单击? cause TouchUtils.clickView(InstrumentationTestCase test, View v) would only simulate the user click in the center of the screen ... How to do it properly ? 原因TouchUtils.clickView(InstrumentationTestCase test,View v)只会模拟用户在屏幕中央单击的情况。如何正确执行操作?

These are the things I have tried and seems I'm missing out something: 这些是我尝试过的事情,似乎缺少了一些东西:

  • TouchUtils.longClickView(InstrumentationTestCase test, View v) works fine...for creating longClickView .. Even I was able to create longClickView() at particular screen location by introducing the timedelay between ACTION_DOWN and ACTION_UP event.. please refer to the code attached TouchUtils.longClickView(InstrumentationTestCase test,View v)可以正常工作...用于创建longClickView。甚至我也可以通过在ACTION_DOWN和ACTION_UP事件之间引入时间延迟来在特定屏幕位置创建longClickView(。),请参阅所附代码
  • I was able to achieve the user clicking event at particular screen location , But I faced a strange issue .. When I am displatching the MotionEvent(100,100) from the test-case.. The framework would always add "-76" in the Y event .. not sure why there was this deviation ... I worked around the issue by adding 76 to my input data (100,176) for time being .. did anyone face a similar issue ? 我能够在特定的屏幕位置实现用户单击事件,但是我遇到一个奇怪的问题..当我从测试用例中分发MotionEvent(100,100)时。该框架将始终在Y中添加“ -76”事件..不知道为什么会出现这种偏差...我暂时通过在输入数据中添加76(100,176)来解决该问题。有人遇到过类似的问题吗?
  • Even seems with this approach timing is very critical .. as If i place more delay between ACTION_DOWN and ACTION_UP , the event is detected as longClickPress ... and if I put a little less ... the "second" single click events ( ACTION_DOWN + ACTION_UP ) gets detected as DoubleTapEvent .. 甚至似乎采用这种方法的时间也是非常关键的..如果如果我在ACTION_DOWN和ACTION_UP之间放置更多的延迟,则该事件被检测为longClickPress ...,如果我放置的时间少一点,则该“第二”单击事件(ACTION_DOWN + ACTION_UP)被检测为DoubleTapEvent ..

What should be the right timing combination for ACTION_UP and ACTION_DOWN .. for a single user click event simulation .. ???????? 对于单个用户单击事件模拟,什么是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);
}

Partial Answer : 部分答案:

still to find what's causing the 76px deviation when creating the user click events. 仍在查找创建用户点击事件时导致76px偏差的原因。

I have been able to simuate the single user click event with 50ms difference propely. 我已经能够正确模拟50ms差异的单用户单击事件。 And I had to space 2 clicks by atleast 300ms ; 而且我必须间隔至少300毫秒2次点击; so that "SingleTapConfirmed" can fire on its own for the GeastureDetector class.. 这样“ 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);
}

instead of using TouchUtils, I used. 我没有使用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();
             }
        });
    }
}

It will not update UI on instrument/device, but it will update the variable within the instrument. 它不会更新仪器/设备上的用户界面,但会更新仪器中的变量。 In my case, the button update the adapter of a fragment. 就我而言,该按钮将更新片段的适配器。 If I only use, TouchUtils.clickView(this, button) the UI on device is updated, but the variable not updated. 如果仅使用,设备上的UI的TouchUtils.clickView(此按钮)将更新,但变量未更新。

So I combine with TouchUtils too. 因此,我也与TouchUtils结合使用。 I don't know why, but it is worked. 我不知道为什么,但是可以用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ActivityInstrumentationTestCase2的android actionbar单元测试 - android actionbar unit test using ActivityInstrumentationTestCase2 使用ActivityInstrumentationTestCase2进行Android单元测试时,如果先前的测试用例导致启动另一个Activity,则getActivity()会挂起 - Android unit test with ActivityInstrumentationTestCase2, getActivity() hangs when previous test case causes another Activity to be launched 运行Android单元测试以使用ActivityInstrumentationTestCase2测试FragmentActivity时发生java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError when running android unit test to test FragmentActivity with ActivityInstrumentationTestCase2 发送触摸到ActivityInstrumentationTestCase2测试时如何解决INJECT_EVENT权限异常 - How to fix INJECT_EVENT permission exception when sending touches to an ActivityInstrumentationTestCase2 test 活动转换后,ActivityInstrumentationTestCase2无法设置新测试 - ActivityInstrumentationTestCase2 cannot setup new test after activity transition java.lang.RuntimeException android ActivityInstrumentationTestCase2测试 - java.lang.RuntimeException android ActivityInstrumentationTestCase2 test 如何使用ActivityInstrumentationTestCase2防止在每次测试期间启动活动 - How to prevent activity to be launched during each test using ActivityInstrumentationTestCase2 java.lang.NoClassDefFoundError:android / test / ActivityInstrumentationTestCase2 - java.lang.NoClassDefFoundError: android/test/ActivityInstrumentationTestCase2 使用ActivityInstrumentationTestCase2似乎无法测试ListView内容 - Using ActivityInstrumentationTestCase2 cannot seem to test ListView contents 是否可以使用支持测试库摆脱ActivityInstrumentationTestCase2? - Is it possible to get rid of ActivityInstrumentationTestCase2 using Support Test Library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM