Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 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.support.test.filters.SmallTest; |
| 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper.RunWithLooper; |
| 27 | |
| 28 | import com.android.server.UiServiceTestCase; |
| 29 | import com.android.server.slice.SliceManagerService.PackageMatchingCache; |
| 30 | |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.util.function.Supplier; |
| 35 | |
| 36 | @SmallTest |
| 37 | @RunWith(AndroidTestingRunner.class) |
| 38 | @RunWithLooper |
| 39 | public class PackageMatchingCacheTest extends UiServiceTestCase { |
| 40 | |
| 41 | private final Supplier<String> supplier = mock(Supplier.class); |
| 42 | private final PackageMatchingCache cache = new PackageMatchingCache(supplier); |
| 43 | |
| 44 | @Test |
| 45 | public void testNulls() { |
| 46 | // Doesn't get for a null input |
| 47 | cache.matches(null); |
| 48 | verify(supplier, never()).get(); |
| 49 | |
| 50 | // Gets once valid input in sent. |
| 51 | cache.matches(""); |
| 52 | verify(supplier).get(); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testCaching() { |
| 57 | when(supplier.get()).thenReturn("ret.pkg"); |
| 58 | |
| 59 | assertTrue(cache.matches("ret.pkg")); |
| 60 | assertTrue(cache.matches("ret.pkg")); |
| 61 | assertTrue(cache.matches("ret.pkg")); |
| 62 | |
| 63 | verify(supplier, times(1)).get(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testGetOnFailure() { |
| 68 | when(supplier.get()).thenReturn("ret.pkg"); |
| 69 | assertTrue(cache.matches("ret.pkg")); |
| 70 | |
| 71 | when(supplier.get()).thenReturn("other.pkg"); |
| 72 | assertTrue(cache.matches("other.pkg")); |
| 73 | verify(supplier, times(2)).get(); |
| 74 | } |
| 75 | } |