方法含义:执行action后,判断timeout时间内,是否出现Event。
方法返回值:布尔型,若有Event发生,则返回true,否则返回false。
EventCondition是一个抽象类,可用通过 android.support.test.uiautomator.Until中提供的两个静态方法,来获取实体对象。
public static EventCondition newWindow()
public static EventCondition scrollFinished(final Direction direction)
查看源码,可发现newWindow()是指发生window状态变化,或内容变化所引发的Event。
而scrollFinished,则是当当滑动到某一方向尽头时所引发的Event。
还是用一个示例来看一下效果。先用newWindow为EventCondition.
核心代码如下:
public class WaitConditionTest extends UIDeviceTest {
@Test
public void test1(){
String packageName="com.breakloop.salaryshow";
String activityName=".MainActivity";
boolean result1=mDevice.performActionAndWait(new Runnable() {
@Override
public void run() {
Log.i(TAG, "run: new window appeared in 5 s");
}
}, Until.newWindow(), 5000);
Log.i(TAG, "test1: result = "+result1);
Utils.startAPP(mDevice,packageName,activityName);
mDevice.waitForWindowUpdate(packageName,5000);
Utils.closeAPP(mDevice,packageName);
}
}
执行效果如下:
执行结果如下:
11-21 23:50:29.221 I/TestRunner: run started: 1 tests
11-21 23:50:29.236 I/TestRunner: started: test1(com.breakloop.u2demo.uidevice.WaitConditionTest)
11-21 23:50:29.238 I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
11-21 23:50:29.239 I/com.breakloop.u2demo.uidevice.WaitConditionTest: run: new window appeared in 5 s
11-21 23:50:34.684 I/com.breakloop.u2demo.uidevice.WaitConditionTest: test1: result = false
11-21 23:50:34.705 I/TestRunner: finished: test1(com.breakloop.u2demo.uidevice.WaitConditionTest)
由执行结果可见,在5秒后,输出了“result = false”,说明5秒内未有新窗口事件产生。原因在于,打开APP这个操作,发生在5秒之后。
我们修改代码,将
Utils.startAPP(mDevice,packageName,activityName);
放置在performActionAndWait之前,查看执行结果。
11-22 00:28:43.961 I/TestRunner: started: test1(com.breakloop.u2demo.uidevice.WaitConditionTest)
11-22 00:28:43.963 I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
11-22 00:28:44.322 I/com.breakloop.u2demo.uidevice.WaitConditionTest: run: new window appeared in 5 s
11-22 00:28:44.411 I/com.breakloop.u2demo.uidevice.WaitConditionTest: test1: result = true
11-22 00:28:50.912 I/TestRunner: finished: test1(com.breakloop.u2demo.uidevice.WaitConditionTest)
可以发现,“result = true”,说明有newWindow事件发生。并且,“result = true”紧接着“new window appeared in 5 s”输出,说明并没有等待发生。这与scrollFinished不同。
下面,我们再以scrollFinished为触发条件,看一下效果。
我们在第二个fragment中,添加一个下拉动作。
实例代码如下:
@Test
public void test2(){
String packageName="com.breakloop.salaryshow";
String activityName=".MainActivity";
Utils.startAPP(mDevice,packageName,activityName);
mDevice.waitForWindowUpdate(packageName,2000);
int h=mDevice.getDisplayHeight();
int w=mDevice.getDisplayWidth();
int left=200;
int right=w-200;
int up=200;
int down=h-200;
int step=10;
mDevice.swipe(right, h/2, left, h/2, step);
mDevice.waitForWindowUpdate(packageName,1000);
mDevice.swipe(w/2, down, w/2, up, step);
boolean result1=mDevice.performActionAndWait(new Runnable() {
@Override
public void run() {
Log.i(TAG, "run: scroll Down");
}
},Until.scrollFinished(Direction.DOWN),2000);
Log.i(TAG, "test1: result = "+result1);
mDevice.waitForWindowUpdate(packageName,1000);
Utils.closeAPP(mDevice,packageName);
}
效果图如下:
结果如下:
10:34:55.344 I/TestRunner: started: test2(com.breakloop.u2demo.uidevice.WaitConditionTest)
10:34:55.351 I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
10:35:01.800 I/com.breakloop.u2demo.uidevice.WaitConditionTest: run: scroll Down
10:35:06.801 I/com.breakloop.u2demo.uidevice.WaitConditionTest: test1: result = true
10:35:08.408 I/TestRunner: finished: test2(com.breakloop.u2demo.uidevice.WaitConditionTest)
由结果可见,在action完成后,scroll down事件发生了,但仍等待了timeout(5秒)的时间。这一点与newWindow事件不同。
对于performActionAndWait方法,不同的事件,效果也不同。若newWindow事件若发生,程序将不再等待,继续执行后续动作。而若scroll事件发生,程序将等待timeout,然后在继续后续动作。
测试机型为华为Mate8。不知其它机型是否有同样的效果。童鞋可尝试,望分享。