I have a app which will download a file from web. The download action is triggered by clicking a button and then it will start a new thread to download this file.
What I want to do now is to test if the download is succesfull or not.
So I wrote the following test code :
private void testGenerateFile() throws Exception {
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
((EditText)mActivity.findViewById(R.id.edit_server)).setText("http://192.168.120.248/phonebook.xml");
click(R.id.ok);
}
});
} catch (Throwable e) {
}
}
But after executing it, the file hasn’t been downloaded. But if I add Thread.sleep(2000) at the end, the file is generated. But I think this is not a proper solution. Can you help me? Thanks.
If you know the location where the file is downloaded to, and the size of the file, you could periodically check (till a predefined duration) for the file being created and downloaded. You could assume that the file is successfully downloaded if the file size meets the original size.