[英]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 我想做什么
Here's where I am stuck 这就是我被困住的地方
These are the things I have tried and seems I'm missing out something: 这些是我尝试过的事情,似乎缺少了一些东西:
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.