1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package instrumentationTest.de.test.antennapod.service.download;
import java.io.File;
import android.test.InstrumentationTestCase;
import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.service.download.*;
import android.test.AndroidTestCase;
import android.util.Log;
public class HttpDownloaderTest extends InstrumentationTestCase {
private static final String TAG = "HttpDownloaderTest";
private static final String DOWNLOAD_DIR = "testdownloads";
private static boolean successful = true;
private File destDir;
public HttpDownloaderTest() {
super();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
File[] contents = destDir.listFiles();
for (File f : contents) {
assertTrue(f.delete());
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
destDir = getInstrumentation().getTargetContext().getExternalFilesDir(DOWNLOAD_DIR);
assertNotNull(destDir);
assertTrue(destDir.exists());
}
private FeedFileImpl setupFeedFile(String downloadUrl, String title) {
FeedFileImpl feedfile = new FeedFileImpl(downloadUrl);
String fileUrl = new File(destDir, title).getAbsolutePath();
File file = new File(fileUrl);
Log.d(TAG, "Deleting file: " + file.delete());
feedfile.setFile_url(fileUrl);
return feedfile;
}
private void download(String url, String title, boolean expectedResult) {
FeedFile feedFile = setupFeedFile(url, title);
DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt());
Downloader downloader = new HttpDownloader(request);
downloader.call();
DownloadStatus status = downloader.getResult();
assertNotNull(status);
assertTrue(status.isSuccessful() == expectedResult);
assertTrue(status.isDone());
// the file should not exist if the download has failed
assertTrue(new File(feedFile.getFile_url()).exists() == expectedResult);
}
public void testPassingHttp() {
download("http://httpbin.org/status/200", "test200", true);
}
public void testPassingHttps() {
download("https://httpbin.org/status/200", "test200", true);
}
public void testRedirect() {
download("http://httpbin.org/redirect/4", "testRedirect", true);
}
public void testRelativeRedirect() {
download("http://httpbin.org/relative-redirect/4", "testRelativeRedirect", true);
}
public void testGzip() {
download("http://httpbin.org/gzip", "testGzip", true);
}
public void test404() {
download("http://httpbin.org/status/404", "test404", false);
}
public void testCancel() {
final String url = "http://httpbin.org/delay/3";
FeedFileImpl feedFile = setupFeedFile(url, "delay");
final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
Thread t = new Thread() {
@Override
public void run() {
downloader.call();
}
};
t.start();
downloader.cancel();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
DownloadStatus result = downloader.getResult();
assertTrue(result.isDone());
assertFalse(result.isSuccessful());
assertTrue(result.isCancelled());
assertFalse(new File(feedFile.getFile_url()).exists());
}
private static class FeedFileImpl extends FeedFile {
public FeedFileImpl(String download_url) {
super(null, download_url, false);
}
@Override
public String getHumanReadableIdentifier() {
return download_url;
}
@Override
public int getTypeAsInt() {
return 0;
}
}
}
|