2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 import java.util.Random;
22 import android.app.DownloadManager.Query;
23 import android.app.DownloadManager.Request;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.ParcelFileDescriptor;
27 import android.util.Log;
30 public class DownloadManagerStressTest extends DownloadManagerBaseTest {
31 private static String LOG_TAG = "android.net.DownloadManagerStressTest";
37 public void setUp() throws Exception {
41 removeAllCurrentDownloads();
45 * Attempts to downloading thousands of files simultaneously
47 public void testDownloadThousands() throws Exception {
49 int MAX_FILE_SIZE = 3000;
50 long[] reqs = new long[NUM_FILES];
52 // need to be sure all current downloads have stopped first
53 MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();
56 Random r = new LoggingRng();
57 for (int i = 0; i < NUM_FILES; ++i) {
58 int size = r.nextInt(MAX_FILE_SIZE);
59 byte[] blobData = generateData(size, DataType.TEXT);
61 Uri uri = getServerUri(DEFAULT_FILENAME);
62 Request request = new Request(uri);
63 request.setTitle(String.format("%s--%d", DEFAULT_FILENAME, i));
65 // Prepare the mock server with a standard response
66 enqueueResponse(HTTP_OK, blobData);
68 Log.i(LOG_TAG, "issuing request: " + i);
69 long reqId = mDownloadManager.enqueue(request);
73 // wait for the download to complete or timeout
74 waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME,
75 MAX_WAIT_FOR_LARGE_DOWNLOAD_TIME);
76 cursor = mDownloadManager.query(new Query());
77 assertEquals(NUM_FILES, cursor.getCount());
78 Log.i(LOG_TAG, "Verified number of downloads in download manager is what we expect.");
79 while (cursor.moveToNext()) {
80 int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
81 String filename = cursor.getString(cursor.getColumnIndex(
82 DownloadManager.COLUMN_URI));
83 String errorString = String.format("File %s failed to download successfully. " +
84 "Status code: %d", filename, status);
85 assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
87 Log.i(LOG_TAG, "Verified each download was successful.");
88 assertEquals(NUM_FILES, receiver.numDownloadsCompleted());
89 Log.i(LOG_TAG, "Verified number of completed downloads in our receiver.");
91 // Verify that for each request, we can open the downloaded file
92 for (int i = 0; i < NUM_FILES; ++i) {
93 ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(reqs[i]);
96 Log.i(LOG_TAG, "Verified we can open each file.");
101 mContext.unregisterReceiver(receiver);
102 removeAllCurrentDownloads();
107 * Tests trying to download a large file (50M bytes).
109 public void testDownloadLargeFile() throws Exception {
110 long fileSize = 50000000L; // note: kept relatively small to not exceed /cache dir size
111 File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
112 MultipleDownloadsCompletedReceiver receiver = registerNewMultipleDownloadsReceiver();
115 long dlRequest = doStandardEnqueue(largeFile);
117 // wait for the download to complete
118 waitForDownloadOrTimeout(dlRequest);
120 ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest);
121 verifyFileContents(pfd, largeFile);
122 verifyFileSize(pfd, largeFile.length());
124 assertEquals(1, receiver.numDownloadsCompleted());
125 mContext.unregisterReceiver(receiver);
126 } catch (Exception e) {
134 * Tests trying to download a large file (~600M bytes) when there's not enough space in cache
136 public void testInsufficientSpace() throws Exception {
137 // @TODO: Rework this to fill up cache partition with a dynamically calculated size
138 long fileSize = 600000000L;
139 File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
141 Cursor cursor = null;
143 long dlRequest = doStandardEnqueue(largeFile);
145 // wait for the download to complete
146 waitForDownloadOrTimeout(dlRequest);
148 cursor = getCursor(dlRequest);
149 verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
150 verifyInt(cursor, DownloadManager.COLUMN_REASON,
151 DownloadManager.ERROR_INSUFFICIENT_SPACE);
153 if (cursor != null) {