Home | History | Annotate | Download | only in deletionhelper
      1 /*
      2  * Copyright (C) 2016 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.storagemanager.deletionhelper;
     18 
     19 import com.android.storagemanager.deletionhelper.FetchDownloadsLoader.DownloadsResult;
     20 
     21 import org.junit.Rule;
     22 import org.junit.Test;
     23 import org.junit.runner.RunWith;
     24 import org.junit.rules.TemporaryFolder;
     25 import org.junit.runners.JUnit4;
     26 
     27 import java.io.File;
     28 import java.io.FileWriter;
     29 import java.io.IOException;
     30 
     31 import static org.junit.Assert.assertNotNull;
     32 import static org.junit.Assert.assertEquals;
     33 import static org.junit.Assert.assertTrue;
     34 
     35 @RunWith(JUnit4.class)
     36 public class FetchDownloadsLoaderTest {
     37     @Rule
     38     public TemporaryFolder temporaryFolder = new TemporaryFolder();
     39 
     40     @Test
     41     public void testEmptyDirectory() throws Exception {
     42         DownloadsResult result =
     43                 FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot());
     44         assertNotNull(result);
     45         assertEquals(0, result.totalSize);
     46         assertEquals(0, result.files.size());
     47     }
     48 
     49     @Test
     50     public void testFilesInDirectory() throws Exception {
     51         makeClearableFile();
     52         makeClearableFile();
     53 
     54         DownloadsResult result =
     55                 FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot());
     56         assertNotNull(result);
     57         assertEquals(0, result.totalSize);
     58         assertEquals(2, result.files.size());
     59     }
     60 
     61     @Test
     62     public void testNestedDirectories() throws Exception {
     63         File tempDir = temporaryFolder.newFolder();
     64 
     65         File testFile = File.createTempFile("test", null, tempDir);
     66         testFile.setLastModified(0);
     67         testFile.deleteOnExit();
     68         DownloadsResult result =
     69                 FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot());
     70         assertNotNull(result);
     71         assertEquals(0, result.totalSize);
     72         assertEquals(1, result.files.size());
     73     }
     74 
     75     @Test
     76     public void testSumFileSizes() throws Exception {
     77         File first = temporaryFolder.newFile();
     78         FileWriter fileWriter = new FileWriter(first);
     79         fileWriter.write("test");
     80         fileWriter.close();
     81         // Writing to the file changes the last modified, so we need to reset it to have it be
     82         // counted.
     83         first.setLastModified(0);
     84 
     85         File second = temporaryFolder.newFile();
     86         fileWriter = new FileWriter(second);
     87         fileWriter.write("test2");
     88         fileWriter.close();
     89         second.setLastModified(0);
     90 
     91         DownloadsResult result =
     92                 FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot());
     93         assertNotNull(result);
     94         assertEquals(9, result.totalSize);
     95         assertEquals(2, result.files.size());
     96     }
     97 
     98     @Test
     99     public void testFetchesThumbnailsForImages() throws Exception {
    100         File imageFile = temporaryFolder.newFile("test.gif");
    101         imageFile.setLastModified(0);
    102         String smallestValidGif = "GIF89a\u0001\u0001;";
    103         FileWriter fileWriter = new FileWriter(imageFile);
    104         fileWriter.write(smallestValidGif);
    105         fileWriter.close();
    106 
    107         DownloadsResult result =
    108                 FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot());
    109         assertNotNull(result);
    110         assertEquals(9, result.totalSize);
    111         assertEquals(1, result.files.size());
    112         assertTrue(result.thumbnails.containsKey(imageFile));
    113     }
    114 
    115     private File makeClearableFile() throws IOException {
    116         File clearableFile = temporaryFolder.newFile();
    117         clearableFile.setLastModified(0);
    118         return clearableFile;
    119     }
    120 
    121 }
    122