blob: c6aea882b7ca59a04d3074e612bb6effd23dcc87 [file] [log] [blame]
Jason Monke009a8e2018-05-21 13:36:21 -04001/*
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
15package com.android.server.slice;
16
17import static org.junit.Assert.assertTrue;
18import static org.mockito.Mockito.mock;
19import static org.mockito.Mockito.never;
20import static org.mockito.Mockito.times;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.support.test.filters.SmallTest;
25import android.testing.AndroidTestingRunner;
26import android.testing.TestableLooper.RunWithLooper;
27
28import com.android.server.UiServiceTestCase;
29import com.android.server.slice.SliceManagerService.PackageMatchingCache;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.util.function.Supplier;
35
36@SmallTest
37@RunWith(AndroidTestingRunner.class)
38@RunWithLooper
39public 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}