Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.app.DownloadManager.Request;
      4 import static org.assertj.core.api.Assertions.assertThat;
      5 import static org.robolectric.Shadows.shadowOf;
      6 import static org.robolectric.shadows.ShadowDownloadManager.ShadowRequest;
      7 
      8 import android.app.DownloadManager;
      9 import android.database.Cursor;
     10 import android.net.Uri;
     11 import android.util.Pair;
     12 import java.util.List;
     13 import org.junit.Test;
     14 import org.junit.runner.RunWith;
     15 import org.robolectric.RobolectricTestRunner;
     16 
     17 @RunWith(RobolectricTestRunner.class)
     18 public class ShadowDownloadManagerTest {
     19 
     20   private final Uri uri = Uri.parse("http://example.com/foo.mp4");
     21   private final Uri destination = Uri.parse("file:///storage/foo.mp4");
     22   private final Request request = new Request(uri);
     23   private final ShadowRequest shadow = shadowOf(request);
     24 
     25   @Test
     26   public void request_shouldGetUri() throws Exception {
     27     assertThat(shadow.getUri().toString()).isEqualTo("http://example.com/foo.mp4");
     28   }
     29 
     30   @Test
     31   public void request_shouldGetDestinationUri() throws Exception {
     32     request.setDestinationUri(Uri.parse("/storage/media/foo.mp4"));
     33     assertThat(shadow.getDestination().toString()).isEqualTo("/storage/media/foo.mp4");
     34   }
     35 
     36   @Test
     37   public void request_shouldGetTitle() throws Exception {
     38     request.setTitle("Title");
     39     assertThat(shadow.getTitle()).isEqualTo("Title");
     40   }
     41 
     42   @Test
     43   public void request_shouldGetDescription() throws Exception {
     44     request.setDescription("Description");
     45     assertThat(shadow.getDescription()).isEqualTo("Description");
     46   }
     47 
     48   @Test
     49   public void request_shouldGetMimeType() throws Exception {
     50     request.setMimeType("application/json");
     51     assertThat(shadow.getMimeType()).isEqualTo("application/json");
     52   }
     53 
     54   @Test
     55   public void request_shouldGetRequestHeaders() throws Exception {
     56     request.addRequestHeader("Authorization", "Bearer token");
     57     List<Pair<String, String>> headers = shadow.getRequestHeaders();
     58     assertThat(headers).isNotEmpty().hasSize(1);
     59     assertThat(headers.get(0).first).isEqualTo("Authorization");
     60     assertThat(headers.get(0).second).isEqualTo("Bearer token");
     61   }
     62 
     63   @Test
     64   public void request_shouldGetNotificationVisibility() throws Exception {
     65     request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
     66     assertThat(shadow.getNotificationVisibility()).isEqualTo(Request.VISIBILITY_VISIBLE);
     67   }
     68 
     69   @Test
     70   public void request_shouldGetAllowedNetworkTypes() throws Exception {
     71     request.setAllowedNetworkTypes(Request.NETWORK_BLUETOOTH);
     72     assertThat(shadow.getAllowedNetworkTypes()).isEqualTo(Request.NETWORK_BLUETOOTH);
     73   }
     74 
     75   @Test
     76   public void request_shouldGetAllowedOverRoaming() throws Exception {
     77     request.setAllowedOverRoaming(true);
     78     assertThat(shadow.getAllowedOverRoaming()).isTrue();
     79   }
     80 
     81   @Test
     82   public void request_shouldGetAllowedOverMetered() throws Exception {
     83     request.setAllowedOverMetered(true);
     84     assertThat(shadow.getAllowedOverMetered()).isTrue();
     85   }
     86 
     87   @Test
     88   public void request_shouldGetVisibleInDownloadsUi() throws Exception {
     89     request.setVisibleInDownloadsUi(true);
     90     assertThat(shadow.getVisibleInDownloadsUi()).isTrue();
     91   }
     92 
     93   @Test
     94   public void enqueue_shouldAddRequest() {
     95     ShadowDownloadManager manager = new ShadowDownloadManager();
     96     long id = manager.enqueue(request);
     97 
     98     assertThat(manager.getRequestCount()).isEqualTo(1);
     99     assertThat(manager.getRequest(id)).isEqualTo(request);
    100   }
    101 
    102   @Test
    103   public void query_shouldReturnCursor() throws Exception {
    104     ShadowDownloadManager manager = new ShadowDownloadManager();
    105     long id = manager.enqueue(request);
    106 
    107     Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id));
    108     assertThat(cursor.getCount()).isEqualTo(1);
    109     assertThat(cursor.moveToNext()).isTrue();
    110   }
    111 
    112   @Test
    113   public void query_shouldReturnColumnIndexes() throws Exception {
    114     ShadowDownloadManager manager = new ShadowDownloadManager();
    115     long id = manager.enqueue(request.setDestinationUri(destination));
    116     Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id));
    117 
    118     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_URI)).isNotNegative();
    119     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)).isNotNegative();
    120     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)).isNotNegative();
    121     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION)).isNotNegative();
    122     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)).isNotNegative();
    123     assertThat(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)).isNotNegative();
    124   }
    125 
    126   @Test
    127   public void query_shouldReturnColumnValues() throws Exception {
    128     ShadowDownloadManager manager = new ShadowDownloadManager();
    129     long id = manager.enqueue(request.setDestinationUri(destination));
    130     Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id));
    131 
    132     cursor.moveToNext();
    133     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString());
    134     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString());
    135   }
    136 
    137   @Test
    138   public void query_shouldHandleEmptyIds() {
    139     ShadowDownloadManager manager = new ShadowDownloadManager();
    140     assertThat(manager.query(new DownloadManager.Query())).isNotNull();
    141   }
    142 
    143   @Test
    144   public void query_shouldReturnAll() {
    145     ShadowDownloadManager manager = new ShadowDownloadManager();
    146     long firstId = manager.enqueue(request.setDestinationUri(destination));
    147     Uri secondUri = Uri.parse("http://example.com/foo2.mp4");
    148     Uri secondDestination = Uri.parse("file:///storage/foo2.mp4");
    149     Request secondRequest = new Request(secondUri);
    150     long secondId = manager.enqueue(secondRequest.setDestinationUri(secondDestination));
    151     Cursor cursor = manager.query(new DownloadManager.Query());
    152 
    153     cursor.moveToNext();
    154     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString());
    155     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString());
    156 
    157     cursor.moveToNext();
    158     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(secondUri.toString());
    159     assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(secondDestination.toString());
    160   }
    161 
    162 }
    163