Robotium How to test with background Services like conversion?

梦想的初衷 提交于 2019-12-11 13:53:02

问题


I am using Robotium Recorder to test my app. My problem is in my app i am doing audio conversions in background which take some time based on how much time user is recording.

Example

If i am running a test which records for 10mins (Converts in background) each time like this 10 times it records so when my last recording that is 10th recording finishes robotium exits and kill the app.

**Problem:- For last recording my file still need to convert its converting in background (Service) but app is killed so converting failed. I cannot use solo.sleep(int) because i dont know how much time it will take to convert **


回答1:


You can use 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));



回答2:


Finally i got the solution

In testRun() function call isServiceRunning() function.

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

This method check for background services of your app and return boolean.

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;
}


来源:https://stackoverflow.com/questions/25830817/robotium-how-to-test-with-background-services-like-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!