Home | History | Annotate | Download | only in automatic
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.storagemanager.automatic;
     18 
     19 import android.os.Bundle;
     20 import android.os.Environment;
     21 import com.android.storagemanager.deletionhelper.DeletionHelperSettings;
     22 import com.android.storagemanager.deletionhelper.DeletionType;
     23 import com.android.storagemanager.deletionhelper.DeletionType.LoadingStatus;
     24 import com.android.storagemanager.deletionhelper.DownloadsDeletionType;
     25 import com.android.storagemanager.deletionhelper.FetchDownloadsLoader.DownloadsResult;
     26 import com.android.storagemanager.testing.TestingConstants;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.robolectric.RobolectricTestRunner;
     31 import org.robolectric.RuntimeEnvironment;
     32 import org.robolectric.annotation.Config;
     33 
     34 import java.io.File;
     35 import java.io.FileWriter;
     36 import java.util.Set;
     37 
     38 import static com.google.common.truth.Truth.assertThat;
     39 import static org.mockito.Matchers.eq;
     40 import static org.mockito.Mockito.mock;
     41 import static org.mockito.Mockito.verify;
     42 
     43 @RunWith(RobolectricTestRunner.class)
     44 @Config(manifest= TestingConstants.MANIFEST, sdk=23)
     45 public class DownloadsDeletionTypeTest {
     46     private DownloadsDeletionType mDeletion;
     47     private File mDownloadsDirectory;
     48 
     49     @Before
     50     public void setUp() {
     51         mDeletion = new DownloadsDeletionType(RuntimeEnvironment.application, null);
     52         mDownloadsDirectory =
     53                 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
     54     }
     55 
     56     @Test
     57     public void testInitializeWithUncheckedFiles() throws Exception {
     58         File temp = new File(mDownloadsDirectory, "temp");
     59         File temp2 = new File(mDownloadsDirectory, "temp2");
     60         String[] filePaths = new String[2];
     61         filePaths[0] = temp.getPath();
     62         filePaths[1] = temp2.getPath();
     63         mDeletion = new DownloadsDeletionType(RuntimeEnvironment.application, filePaths);
     64 
     65         assertThat(mDeletion.isChecked(temp)).isFalse();
     66         assertThat(mDeletion.isChecked(temp2)).isFalse();
     67     }
     68 
     69     @Test
     70     public void testFetchDownloads() throws Exception {
     71         File temp = new File(mDownloadsDirectory, "temp");
     72         File temp2 = new File(mDownloadsDirectory, "temp2");
     73         DownloadsResult result = new DownloadsResult();
     74         result.files.add(temp);
     75         result.files.add(temp2);
     76 
     77         mDeletion.onLoadFinished(null, result);
     78         Set<File> fileSet = mDeletion.getFiles();
     79 
     80         assertThat(fileSet.contains(temp)).isTrue();
     81         assertThat(fileSet.contains(temp2)).isTrue();
     82     }
     83 
     84     @Test
     85     public void testSetChecked() throws Exception {
     86         File temp = new File(mDownloadsDirectory, "temp");
     87         DownloadsResult result = new DownloadsResult();
     88         result.files.add(temp);
     89 
     90         mDeletion.onLoadFinished(null, result);
     91 
     92         // Downloads files are default checked.
     93         assertThat(mDeletion.isChecked(temp)).isTrue();
     94         mDeletion.setFileChecked(temp, false);
     95 
     96         assertThat(mDeletion.isChecked(temp)).isFalse();
     97         mDeletion.setFileChecked(temp, true);
     98 
     99         assertThat(mDeletion.isChecked(temp)).isTrue();
    100     }
    101 
    102     @Test
    103     public void testUncheckedFilesDoNotCountForSize() throws Exception {
    104         File temp = new File(mDownloadsDirectory, "temp");
    105         FileWriter fileWriter = new FileWriter(temp);
    106         fileWriter.write("test");
    107         fileWriter.close();
    108         DownloadsResult result = new DownloadsResult();
    109         result.files.add(temp);
    110 
    111         mDeletion.onLoadFinished(null, result);
    112 
    113         // Downloads files are default checked.
    114         assertThat(mDeletion.isChecked(temp)).isTrue();
    115         assertThat(mDeletion.getFreeableBytes(DeletionHelperSettings.COUNT_CHECKED_ONLY))
    116                 .isEqualTo(4);
    117 
    118         mDeletion.setFileChecked(temp, false);
    119         assertThat(mDeletion.getFreeableBytes(DeletionHelperSettings.COUNT_CHECKED_ONLY))
    120                 .isEqualTo(0);
    121     }
    122 
    123     @Test
    124     public void testSaveAndRestoreRemembersUncheckedFiles() throws Exception {
    125         File temp = new File(mDownloadsDirectory, "temp");
    126         File temp2 = new File(mDownloadsDirectory, "temp2");
    127         DownloadsResult result = new DownloadsResult();
    128         result.files.add(temp);
    129         result.files.add(temp2);
    130         mDeletion.onLoadFinished(null, result);
    131 
    132         mDeletion.setFileChecked(temp, false);
    133         Bundle savedBundle = new Bundle();
    134         mDeletion.onSaveInstanceStateBundle(savedBundle);
    135         mDeletion = new DownloadsDeletionType(RuntimeEnvironment.application,
    136                 savedBundle.getStringArray(DownloadsDeletionType.EXTRA_UNCHECKED_DOWNLOADS));
    137 
    138         assertThat(mDeletion.isChecked(temp)).isFalse();
    139         assertThat(mDeletion.isChecked(temp2)).isTrue();
    140     }
    141 
    142     @Test
    143     public void testCallbackOnFileLoad() throws Exception {
    144         File temp = new File(mDownloadsDirectory, "temp");
    145         File temp2 = new File(mDownloadsDirectory, "temp2");
    146         DownloadsResult result = new DownloadsResult();
    147         result.files.add(temp);
    148         result.files.add(temp2);
    149         result.totalSize = 101L;
    150 
    151         DeletionType.FreeableChangedListener mockListener =
    152                 mock(DeletionType.FreeableChangedListener.class);
    153         mDeletion.registerFreeableChangedListener(mockListener);
    154 
    155         // Calls back immediately when we add a listener with its most current info.
    156         verify(mockListener).onFreeableChanged(eq(0), eq(0L));
    157 
    158         // Callback when the load finishes.
    159         mDeletion.onLoadFinished(null, result);
    160         verify(mockListener).onFreeableChanged(eq(2), eq(101L));
    161     }
    162 
    163     @Test
    164     public void testLoadingState_initiallyIncomplete() {
    165         // We should always be in the incomplete state when we start out
    166         assertThat(mDeletion.getLoadingStatus()).isEqualTo(LoadingStatus.LOADING);
    167     }
    168 
    169     @Test
    170     public void testLoadingState_completeEmptyOnNothingFound() {
    171         // We should be in EMPTY if nothing was found
    172         DownloadsResult result = new DownloadsResult();
    173         mDeletion.onLoadFinished(null, result);
    174         assertThat(mDeletion.isEmpty()).isTrue();
    175     }
    176 
    177     @Test
    178     public void testLoadingState_completeOnDeletableContentFound() {
    179         // We should be in COMPLETE if downloads were found
    180         DownloadsResult result = new DownloadsResult();
    181         File temp = new File(mDownloadsDirectory, "temp");
    182         File temp2 = new File(mDownloadsDirectory, "temp2");
    183         result.files.add(temp);
    184         result.files.add(temp2);
    185         mDeletion.onLoadFinished(null, result);
    186         assertThat(mDeletion.isComplete()).isTrue();
    187     }
    188 }
    189