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.net.Uri;
     20 import android.provider.Downloads;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import libcore.io.IoUtils;
     25 
     26 import java.io.File;
     27 
     28 /**
     29  * This test exercises methods in the {@Helpers} utility class.
     30  */
     31 @SmallTest
     32 public class HelpersTest extends AndroidTestCase {
     33 
     34     @Override
     35     protected void tearDown() throws Exception {
     36         IoUtils.deleteContents(getContext().getFilesDir());
     37         IoUtils.deleteContents(getContext().getCacheDir());
     38 
     39         super.tearDown();
     40     }
     41 
     42     public void testGenerateSaveFile() throws Exception {
     43         final File expected = new File(getContext().getFilesDir(), "file.mp4");
     44         final String actual = Helpers.generateSaveFile(getContext(),
     45                 "http://example.com/file.txt", null, null, null,
     46                 "video/mp4", Downloads.Impl.DESTINATION_CACHE_PARTITION);
     47         assertEquals(expected.getAbsolutePath(), actual);
     48     }
     49 
     50     public void testGenerateSaveFileDupes() throws Exception {
     51         final File expected1 = new File(getContext().getFilesDir(), "file.txt");
     52         final String actual1 = Helpers.generateSaveFile(getContext(), "http://example.com/file.txt",
     53                 null, null, null, null, Downloads.Impl.DESTINATION_CACHE_PARTITION);
     54 
     55         final File expected2 = new File(getContext().getFilesDir(), "file-1.txt");
     56         final String actual2 = Helpers.generateSaveFile(getContext(), "http://example.com/file.txt",
     57                 null, null, null, null, Downloads.Impl.DESTINATION_CACHE_PARTITION);
     58 
     59         assertEquals(expected1.getAbsolutePath(), actual1);
     60         assertEquals(expected2.getAbsolutePath(), actual2);
     61     }
     62 
     63     public void testGenerateSaveFileNoExtension() throws Exception {
     64         final File expected = new File(getContext().getFilesDir(), "file.mp4");
     65         final String actual = Helpers.generateSaveFile(getContext(),
     66                 "http://example.com/file", null, null, null,
     67                 "video/mp4", Downloads.Impl.DESTINATION_CACHE_PARTITION);
     68         assertEquals(expected.getAbsolutePath(), actual);
     69     }
     70 
     71     public void testGenerateSaveFileHint() throws Exception {
     72         final File expected = new File(getContext().getFilesDir(), "meow");
     73         final String hint = Uri.fromFile(expected).toString();
     74 
     75         // Test that we never change requested filename.
     76         final String actual = Helpers.generateSaveFile(getContext(), "url", hint,
     77                 "dispo", "locat", "video/mp4", Downloads.Impl.DESTINATION_FILE_URI);
     78         assertEquals(expected.getAbsolutePath(), actual);
     79     }
     80 
     81     public void testGenerateSaveFileDisposition() throws Exception {
     82         final File expected = new File(getContext().getFilesDir(), "real.mp4");
     83         final String actual = Helpers.generateSaveFile(getContext(),
     84                 "http://example.com/file.txt", null, "attachment; filename=\"subdir/real.pdf\"",
     85                 null, "video/mp4", Downloads.Impl.DESTINATION_CACHE_PARTITION);
     86         assertEquals(expected.getAbsolutePath(), actual);
     87     }
     88 }
     89