/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app; import android.app.DownloadManager.Query; import android.app.DownloadManager.Request; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.os.ParcelFileDescriptor; import android.os.StatFs; import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; /** * Integration tests of the DownloadManager API. */ public class DownloadManagerStressTest extends DownloadManagerBaseTest { private static final String TAG = "DownloadManagerStressTest"; private final static String CACHE_DIR = Environment.getDownloadCacheDirectory().getAbsolutePath(); /** * {@inheritDoc} */ @Override public void setUp() throws Exception { super.setUp(); setWiFiStateOn(true); removeAllCurrentDownloads(); } /** * {@inheritDoc} */ @Override public void tearDown() throws Exception { super.tearDown(); setWiFiStateOn(true); removeAllCurrentDownloads(); if (mReceiver != null) { mContext.unregisterReceiver(mReceiver); mReceiver = null; } } /** * Attempts to download several files simultaneously */ @LargeTest public void testMultipleDownloads() throws Exception { // need to be sure all current downloads have stopped first removeAllCurrentDownloads(); int NUM_FILES = 10; int MAX_FILE_SIZE = 10 * 1024; // 10 kb Random r = new LoggingRng(); for (int i=0; i= remainderSizeBlocks) { fo.write(buffer); fs.restat(CACHE_DIR); } } catch (IOException e) { Log.e(LOG_TAG, "error filling file: ", e); throw e; } finally { if (fo != null) { fo.close(); } } } // /cache should now be almost full. long spaceAvailable = fs.getAvailableBlocks() * blockSize; Log.i(TAG, "BEFORE download, available space in /cache: " + spaceAvailable); assertTrue(DOWNLOAD_FILE_SIZE > spaceAvailable); // try to download 1MB file into /cache - and it should succeed byte[] blobData = generateData(DOWNLOAD_FILE_SIZE, DataType.TEXT); long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE); verifyAndCleanupSingleFileDownload(dlRequest, blobData); } finally { if (outFile != null) { outFile.delete(); } } } }