Home | History | Annotate | Download | only in downloads
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.android.providers.downloads;
     18 
     19 import android.app.DownloadManager;
     20 import android.database.Cursor;
     21 import android.net.Uri;
     22 import android.os.ParcelFileDescriptor;
     23 
     24 import java.io.FileInputStream;
     25 import java.io.InputStream;
     26 import java.net.MalformedURLException;
     27 
     28 /**
     29  * Code common to tests that use the download manager public API.
     30  */
     31 public abstract class AbstractPublicApiTest extends AbstractDownloadManagerFunctionalTest {
     32 
     33     class Download {
     34         final long mId;
     35 
     36         private Download(long downloadId) {
     37             this.mId = downloadId;
     38         }
     39 
     40         public int getStatus() {
     41             return (int) getLongField(DownloadManager.COLUMN_STATUS);
     42         }
     43 
     44         String getStringField(String field) {
     45             Cursor cursor = mManager.query(new DownloadManager.Query().setFilterById(mId));
     46             try {
     47                 assertEquals(1, cursor.getCount());
     48                 cursor.moveToFirst();
     49                 return cursor.getString(cursor.getColumnIndexOrThrow(field));
     50             } finally {
     51                 cursor.close();
     52             }
     53         }
     54 
     55         long getLongField(String field) {
     56             Cursor cursor = mManager.query(new DownloadManager.Query().setFilterById(mId));
     57             try {
     58                 assertEquals(1, cursor.getCount());
     59                 cursor.moveToFirst();
     60                 return cursor.getLong(cursor.getColumnIndexOrThrow(field));
     61             } finally {
     62                 cursor.close();
     63             }
     64         }
     65 
     66         String getContents() throws Exception {
     67             ParcelFileDescriptor downloadedFile = mManager.openDownloadedFile(mId);
     68             assertTrue("Invalid file descriptor: " + downloadedFile,
     69                        downloadedFile.getFileDescriptor().valid());
     70             InputStream stream = new FileInputStream(downloadedFile.getFileDescriptor());
     71             try {
     72                 return readStream(stream);
     73             } finally {
     74                 stream.close();
     75             }
     76         }
     77 
     78         void runUntilStatus(int status) throws Exception {
     79             runService();
     80             assertEquals(status, getStatus());
     81         }
     82     }
     83 
     84     protected static final String PACKAGE_NAME = "my.package.name";
     85     protected static final String REQUEST_PATH = "/path";
     86 
     87     protected DownloadManager mManager;
     88 
     89     public AbstractPublicApiTest(FakeSystemFacade systemFacade) {
     90         super(systemFacade);
     91     }
     92 
     93     @Override
     94     protected void setUp() throws Exception {
     95         super.setUp();
     96         mManager = new DownloadManager(mResolver, PACKAGE_NAME);
     97     }
     98 
     99     protected DownloadManager.Request getRequest() throws MalformedURLException {
    100         return getRequest(getServerUri(REQUEST_PATH));
    101     }
    102 
    103     protected DownloadManager.Request getRequest(String path) {
    104         return new DownloadManager.Request(Uri.parse(path));
    105     }
    106 
    107     protected Download enqueueRequest(DownloadManager.Request request) {
    108         return new Download(mManager.enqueue(request));
    109     }
    110 }
    111