1 /* 2 * Copyright (C) 2023 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 package com.android.quickstep; 17 18 import static junit.framework.Assert.assertFalse; 19 import static junit.framework.Assert.assertTrue; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.res.Resources; 30 31 import androidx.test.filters.SmallTest; 32 33 import com.android.launcher3.R; 34 import com.android.quickstep.util.TaskKeyCache; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 import java.util.concurrent.Executor; 42 43 @SmallTest 44 public class TaskThumbnailCacheTest { 45 @Mock 46 private Context mContext; 47 48 @Mock 49 private Resources mResource; 50 51 @Mock 52 private TaskKeyCache mTaskKeyCache; 53 54 @Before setup()55 public void setup() throws NoSuchFieldException { 56 MockitoAnnotations.initMocks(this); 57 when(mContext.getResources()).thenReturn(mResource); 58 } 59 60 @Test increaseCacheSize()61 public void increaseCacheSize() { 62 // Mock a cache size increase from 3 to 8 63 when(mTaskKeyCache.getMaxSize()).thenReturn(3); 64 when(mResource.getInteger((R.integer.recentsThumbnailCacheSize))).thenReturn(8); 65 TaskThumbnailCache thumbnailCache = new TaskThumbnailCache(mContext, mock(Executor.class), 66 mTaskKeyCache); 67 68 // Preload is needed when increasing size 69 assertTrue(thumbnailCache.updateCacheSizeAndRemoveExcess()); 70 verify(mTaskKeyCache, times(1)).updateCacheSizeAndRemoveExcess(8); 71 } 72 73 @Test decreaseCacheSize()74 public void decreaseCacheSize() { 75 // Mock a cache size decrease from 8 to 3 76 when(mTaskKeyCache.getMaxSize()).thenReturn(8); 77 when(mResource.getInteger((R.integer.recentsThumbnailCacheSize))).thenReturn(3); 78 TaskThumbnailCache thumbnailCache = new TaskThumbnailCache(mContext, mock(Executor.class), 79 mTaskKeyCache); 80 // Preload is not needed when decreasing size 81 assertFalse(thumbnailCache.updateCacheSizeAndRemoveExcess()); 82 verify(mTaskKeyCache, times(1)).updateCacheSizeAndRemoveExcess(3); 83 } 84 85 @Test keepSameCacheSize()86 public void keepSameCacheSize() { 87 when(mTaskKeyCache.getMaxSize()).thenReturn(3); 88 when(mResource.getInteger((R.integer.recentsThumbnailCacheSize))).thenReturn(3); 89 TaskThumbnailCache thumbnailCache = new TaskThumbnailCache(mContext, mock(Executor.class), 90 mTaskKeyCache); 91 // Preload is not needed when it has the same cache size 92 assertFalse(thumbnailCache.updateCacheSizeAndRemoveExcess()); 93 verify(mTaskKeyCache, never()).updateCacheSizeAndRemoveExcess(anyInt()); 94 } 95 } 96