1 /* 2 * Copyright (C) 2018 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.settingslib.utils; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static junit.framework.Assert.assertTrue; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 29 import android.content.Context; 30 import android.graphics.drawable.Drawable; 31 import android.graphics.drawable.Icon; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class IconCacheTest { 41 private Icon mIcon; 42 private Context mContext; 43 private IconCache mIconCache; 44 45 @Before 46 public void setUp() { 47 mContext = spy(RuntimeEnvironment.application); 48 mIcon = mock(Icon.class); 49 Drawable drawable = mock(Drawable.class); 50 doReturn(drawable).when(mIcon).loadDrawable(mContext); 51 mIconCache = new IconCache(mContext); 52 } 53 54 @Test 55 public void testGetIcon_iconisNull() { 56 assertThat(mIconCache.getIcon(null)).isNull(); 57 } 58 59 @Test 60 public void testGetIcon_iconAlreadyLoaded() { 61 mIconCache.getIcon(mIcon); 62 verify(mIcon, times(1)).loadDrawable(mContext); 63 mIconCache.getIcon(mIcon); 64 verify(mIcon, times(1)).loadDrawable(mContext); 65 } 66 67 @Test 68 public void testGetIcon_iconLoadedFirstTime() { 69 mIconCache.getIcon(mIcon); 70 assertTrue(mIconCache.mMap.containsKey(mIcon)); 71 } 72 } 73