1 /* 2 * Copyright (C) 2017 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.deletionhelper; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.graphics.Bitmap; 27 import android.graphics.drawable.BitmapDrawable; 28 import android.support.v7.preference.PreferenceManager; 29 30 import com.android.storagemanager.testing.StorageManagerRobolectricTestRunner; 31 import com.android.storagemanager.testing.TestingConstants; 32 import com.android.storagemanager.utils.IconProvider; 33 34 import org.junit.Before; 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.rules.TemporaryFolder; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.annotation.Config; 43 44 import java.io.File; 45 46 @RunWith(StorageManagerRobolectricTestRunner.class) 47 @Config(manifest = TestingConstants.MANIFEST, sdk = TestingConstants.SDK_VERSION) 48 public class DownloadsDeletionPreferenceGroupTest { 49 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); 50 51 @Mock private IconProvider mIconProvider; 52 53 private Context mContext; 54 private DownloadsDeletionPreferenceGroup mGroup; 55 private DownloadsDeletionType mType; 56 private File mTempDir; 57 58 @Before 59 public void setUp() throws Exception { 60 MockitoAnnotations.initMocks(this); 61 mContext = RuntimeEnvironment.application; 62 63 mGroup = spy(new DownloadsDeletionPreferenceGroup(mContext)); 64 final PreferenceManager preferenceManager = mock(PreferenceManager.class); 65 when(mGroup.getPreferenceManager()).thenReturn(preferenceManager); 66 mType = new DownloadsDeletionType(mContext, new String[0]); 67 68 mTempDir = temporaryFolder.newFolder(); 69 70 mGroup.registerDeletionService(mType); 71 mGroup.injectIconProvider(mIconProvider); 72 } 73 74 @Test 75 public void thumbnailsArePopulated() { 76 FetchDownloadsLoader.DownloadsResult result = new FetchDownloadsLoader.DownloadsResult(); 77 File imageFile = new File(mTempDir, "test.bmp"); 78 result.files.add(imageFile); 79 result.thumbnails.put(imageFile, Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565)); 80 File nonImageFile = new File(mTempDir, "test.txt"); 81 result.files.add(nonImageFile); 82 83 mType.onLoadFinished(null, result); 84 85 assertThat(mGroup.getPreferenceCount()).isEqualTo(2); 86 assertThat(mGroup.getPreference(0).getIcon() instanceof BitmapDrawable).isTrue(); 87 assertThat(mGroup.getPreference(1).getIcon() instanceof BitmapDrawable).isFalse(); 88 } 89 } 90