summaryrefslogtreecommitdiff
path: root/tests/src/de/danoeh/antennapod/test/HttpDownloaderTest.java
blob: 470a08881484b70457ef44e070d92b143c200667 (plain)
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
package de.danoeh.antennapod.test;

import java.io.File;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.service.download.Downloader;
import de.danoeh.antennapod.service.download.DownloaderCallback;
import de.danoeh.antennapod.service.download.HttpDownloader;

import android.test.AndroidTestCase;
import android.util.Log;

public class HttpDownloaderTest extends AndroidTestCase {
	private static final String TAG = "HttpDownloaderTest";
	private static final String DOWNLOAD_DIR = "testdownloads";
	
	private static boolean successful = true;
	private static ExecutorService es;
	
	private static DownloaderCallback downloaderCallback = new DownloaderCallback() {
		
		@Override
		public void onDownloadCompleted(Downloader downloader) {
			DownloadStatus status = downloader.getStatus();
			if (status != null) {
				final String downloadUrl = status.getFeedFile().getDownload_url();
				final String fileUrl = status.getFeedFile().getFile_url();
				new File(fileUrl).delete();
				if (status.isSuccessful()) {
					Log.i(TAG, "Download successful: " + downloadUrl);
				} else {
					Log.e(TAG, "Download not successful: " + status.toString());
					successful = false;
				}
			} else {
				Log.wtf(TAG, "Status was null");
				successful = false;
			}
			if (successful == false) {
				es.shutdownNow();
			}
		}
	};
	
	public void testDownload() throws InterruptedException {
		es = Executors.newFixedThreadPool(5);
		int i = 0;
		for (String url : TestDownloads.urls) {
			Feed feed = new Feed(url, new Date());
			String fileUrl = new File(getContext().getExternalFilesDir(DOWNLOAD_DIR).getAbsolutePath(), Integer.toString(i)).getAbsolutePath();
			File file = new File(fileUrl);
			Log.d(TAG, "Deleting file: " + file.delete());
			feed.setFile_url(fileUrl);
			DownloadStatus status = new DownloadStatus(feed, Integer.toString(i));
			Downloader downloader = new HttpDownloader(downloaderCallback, status);
			es.submit(downloader);
			i++;
		}
		Log.i(TAG, "Awaiting termination");
		es.shutdown();
		es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
		assertTrue(successful);
	}
	
}