1/*
2 * Copyright (C) 2011 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
17package com.android.dialer.util;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21import android.util.LruCache;
22
23import com.android.dialer.util.ExpirableCache.CachedValue;
24
25/**
26 * Unit tests for {@link ExpirableCache}.
27 */
28@SmallTest
29public class ExpirableCacheTest extends AndroidTestCase {
30    /** The object under test. */
31    private ExpirableCache<String, Integer> mCache;
32
33    @Override
34    protected void setUp() throws Exception {
35        super.setUp();
36        LruCache<String, CachedValue<Integer>> lruCache =
37            new LruCache<String, ExpirableCache.CachedValue<Integer>>(20);
38        mCache = ExpirableCache.create(lruCache);
39    }
40
41    @Override
42    protected void tearDown() throws Exception {
43        mCache = null;
44        super.tearDown();
45    }
46
47    public void testPut() {
48        mCache.put("a", 1);
49        mCache.put("b", 2);
50        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
51        assertEquals(2, mCache.getPossiblyExpired("b").intValue());
52        mCache.put("a", 3);
53        assertEquals(3, mCache.getPossiblyExpired("a").intValue());
54    }
55
56    public void testGet_NotExisting() {
57        assertNull(mCache.getPossiblyExpired("a"));
58        mCache.put("b", 1);
59        assertNull(mCache.getPossiblyExpired("a"));
60    }
61
62    public void testGet_Expired() {
63        mCache.put("a", 1);
64        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
65        mCache.expireAll();
66        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
67    }
68
69    public void testGetNotExpired_NotExisting() {
70        assertNull(mCache.get("a"));
71        mCache.put("b", 1);
72        assertNull(mCache.get("a"));
73    }
74
75    public void testGetNotExpired_Expired() {
76        mCache.put("a", 1);
77        assertEquals(1, mCache.get("a").intValue());
78        mCache.expireAll();
79        assertNull(mCache.get("a"));
80    }
81
82    public void testGetCachedValue_NotExisting() {
83        assertNull(mCache.getCachedValue("a"));
84        mCache.put("b", 1);
85        assertNull(mCache.getCachedValue("a"));
86    }
87
88    public void testGetCachedValue_Expired() {
89        mCache.put("a", 1);
90        assertFalse("Should not be expired", mCache.getCachedValue("a").isExpired());
91        mCache.expireAll();
92        assertTrue("Should be expired", mCache.getCachedValue("a").isExpired());
93    }
94
95    public void testGetChangedValue_PutAfterExpired() {
96        mCache.put("a", 1);
97        mCache.expireAll();
98        mCache.put("a", 1);
99        assertFalse("Should not be expired", mCache.getCachedValue("a").isExpired());
100    }
101
102    public void testComputingCache() {
103        // Creates a cache in which all unknown values default to zero.
104        mCache = ExpirableCache.create(
105                new LruCache<String, ExpirableCache.CachedValue<Integer>>(10) {
106                    @Override
107                    protected CachedValue<Integer> create(String key) {
108                        return mCache.newCachedValue(0);
109                    }
110                });
111
112        // The first time we request a new value, we add it to the cache.
113        CachedValue<Integer> cachedValue = mCache.getCachedValue("a");
114        assertNotNull("Should have been created implicitly", cachedValue);
115        assertEquals(0, cachedValue.getValue().intValue());
116        assertFalse("Should not be expired", cachedValue.isExpired());
117
118        // If we expire all the values, the implicitly created value will also be marked as expired.
119        mCache.expireAll();
120        CachedValue<Integer> expiredCachedValue = mCache.getCachedValue("a");
121        assertNotNull("Should have been created implicitly", expiredCachedValue);
122        assertEquals(0, expiredCachedValue.getValue().intValue());
123        assertTrue("Should be expired", expiredCachedValue.isExpired());
124    }
125}
126