1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.server.slice; 16 17 import static org.junit.Assert.assertTrue; 18 import static org.mockito.Mockito.mock; 19 import static org.mockito.Mockito.never; 20 import static org.mockito.Mockito.times; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.testing.AndroidTestingRunner; 25 import android.testing.TestableLooper.RunWithLooper; 26 27 import androidx.test.filters.SmallTest; 28 29 import com.android.server.UiServiceTestCase; 30 import com.android.server.slice.SliceManagerService.PackageMatchingCache; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.function.Supplier; 36 37 @SmallTest 38 @RunWith(AndroidTestingRunner.class) 39 @RunWithLooper 40 public class PackageMatchingCacheTest extends UiServiceTestCase { 41 42 private final Supplier<String> supplier = mock(Supplier.class); 43 private final PackageMatchingCache cache = new PackageMatchingCache(supplier); 44 45 @Test testNulls()46 public void testNulls() { 47 // Doesn't get for a null input 48 cache.matches(null); 49 verify(supplier, never()).get(); 50 51 // Gets once valid input in sent. 52 cache.matches(""); 53 verify(supplier).get(); 54 } 55 56 @Test testCaching()57 public void testCaching() { 58 when(supplier.get()).thenReturn("ret.pkg"); 59 60 assertTrue(cache.matches("ret.pkg")); 61 assertTrue(cache.matches("ret.pkg")); 62 assertTrue(cache.matches("ret.pkg")); 63 64 verify(supplier, times(1)).get(); 65 } 66 67 @Test testGetOnFailure()68 public void testGetOnFailure() { 69 when(supplier.get()).thenReturn("ret.pkg"); 70 assertTrue(cache.matches("ret.pkg")); 71 72 when(supplier.get()).thenReturn("other.pkg"); 73 assertTrue(cache.matches("other.pkg")); 74 verify(supplier, times(2)).get(); 75 } 76 } 77