Robotium如何使用后台测试服务如转换?

时间:2014-09-14 06:50:15

标签: android unit-testing testing robotium

我正在使用Robotium Recorder来测试我的应用。我的问题是在我的应用程序中,我在后台进行音频转换,根据用户录制的时间需要一些时间。

实施例

如果我正在运行一个记录10分钟(在后台转换)的测试,每次这样10次记录,那么当我最后一次记录第10次记录完成robotium退出并杀死应用程序时。

**问题: - 对于最后一次录制,我的文件仍需要在后台(服务)转换其转换,但应用程序被终止,因此转换失败。我不能使用solo.sleep(int),因为我不知道转换需要多长时间**

2 个答案:

答案 0 :(得分:1)

您可以使用solo.waitForCondition()

final int TIMEOUT = 5000;
Assert.assertTrue(solo.waitForCondition(new Condition() {
    @Override
    public boolean isSatisfied() {
        // return true if the file has been converted.
    }
}, TIMEOUT));

答案 1 :(得分:0)

最后我得到了解决方案

testRun()函数调用isServiceRunning()函数中。

public void testRun() {
    .
    .
    if(isSurviceRunning()) {
        solo.sleep(time);
    }
}

此方法检查应用的后台服务并返回布尔值。

public boolean isServiceRunning() {      
ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if ("com.pakage.name.yourcls.your_service_name".equals(service.service.getClassName())) {
        Log.d("Running Service is->", "" + service.service.getClassName());
        return true;
    } else {
        Log.d("false", "" + service.service.getClassName());
    }
}
return false;
}
相关问题