1 /*
2  * Copyright (C) 2019 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.systemui.shared.recents.model;
18 
19 
20 import static junit.framework.TestCase.assertEquals;
21 import static junit.framework.TestCase.assertNull;
22 
23 import android.test.suitebuilder.annotation.SmallTest;
24 
25 import com.android.systemui.SysuiTestCase;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 @SmallTest
33 @RunWith(JUnit4.class)
34 public class TaskKeyLruCacheTest extends SysuiTestCase {
35     private static int sCacheSize = 3;
36     private static int sIdTask1 = 1;
37     private static int sIdTask2 = 2;
38     private static int sIdTask3 = 3;
39     private static int sIdUser1 = 1;
40 
41     TaskKeyCache<Integer> mCache = new TaskKeyLruCache<>(sCacheSize, null);
42     Task.TaskKey mKey1;
43     Task.TaskKey mKey2;
44     Task.TaskKey mKey3;
45 
46     @Before
setup()47     public void setup() {
48         mKey1 = new Task.TaskKey(sIdTask1, 0, null, null, sIdUser1, System.currentTimeMillis());
49         mKey2 = new Task.TaskKey(sIdTask2, 0, null, null, sIdUser1, System.currentTimeMillis());
50         mKey3 = new Task.TaskKey(sIdTask3, 0, null, null, sIdUser1, System.currentTimeMillis());
51     }
52 
53     @Test
addSingleItem_get_success()54     public void addSingleItem_get_success() {
55         mCache.put(mKey1, 1);
56 
57         assertEquals(1, (int) mCache.get(mKey1));
58     }
59 
60     @Test
addSingleItem_getUninsertedItem_returnsNull()61     public void addSingleItem_getUninsertedItem_returnsNull() {
62         mCache.put(mKey1, 1);
63 
64         assertNull(mCache.get(mKey2));
65     }
66 
67     @Test
emptyCache_get_returnsNull()68     public void emptyCache_get_returnsNull() {
69         assertNull(mCache.get(mKey1));
70     }
71 
72     @Test
updateItem_get_returnsSecond()73     public void updateItem_get_returnsSecond() {
74         mCache.put(mKey1, 1);
75         mCache.put(mKey1, 2);
76 
77         assertEquals(2, (int) mCache.get(mKey1));
78         assertEquals(1, mCache.mKeys.size());
79     }
80 
81     @Test
fillCache_put_evictsOldest()82     public void fillCache_put_evictsOldest() {
83         mCache.put(mKey1, 1);
84         mCache.put(mKey2, 2);
85         mCache.put(mKey3, 3);
86         Task.TaskKey key4 = new Task.TaskKey(sIdTask3 + 1, 0,
87                 null, null, sIdUser1, System.currentTimeMillis());
88         mCache.put(key4, 4);
89 
90         assertNull(mCache.get(mKey1));
91         assertEquals(3, mCache.mKeys.size());
92         assertEquals(mKey2, mCache.mKeys.valueAt(0));
93     }
94 
95     @Test
fillCache_remove_success()96     public void fillCache_remove_success() {
97         mCache.put(mKey1, 1);
98         mCache.put(mKey2, 2);
99         mCache.put(mKey3, 3);
100 
101         mCache.remove(mKey2);
102 
103         assertNull(mCache.get(mKey2));
104         assertEquals(2, mCache.mKeys.size());
105     }
106 }
107