summaryrefslogtreecommitdiff
path: root/tests/src/com/android/dialer/util/ExpirableCacheTest.java
blob: b81ad754f6ad29958785e3af95a337c1f3e5f4bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.util;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.LruCache;

import com.android.dialer.util.ExpirableCache.CachedValue;

/**
 * Unit tests for {@link ExpirableCache}.
 */
@SmallTest
public class ExpirableCacheTest extends AndroidTestCase {
    /** The object under test. */
    private ExpirableCache<String, Integer> mCache;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        LruCache<String, CachedValue<Integer>> lruCache =
            new LruCache<String, ExpirableCache.CachedValue<Integer>>(20);
        mCache = ExpirableCache.create(lruCache);
    }

    @Override
    protected void tearDown() throws Exception {
        mCache = null;
        super.tearDown();
    }

    public void testPut() {
        mCache.put("a", 1);
        mCache.put("b", 2);
        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
        assertEquals(2, mCache.getPossiblyExpired("b").intValue());
        mCache.put("a", 3);
        assertEquals(3, mCache.getPossiblyExpired("a").intValue());
    }

    public void testGet_NotExisting() {
        assertNull(mCache.getPossiblyExpired("a"));
        mCache.put("b", 1);
        assertNull(mCache.getPossiblyExpired("a"));
    }

    public void testGet_Expired() {
        mCache.put("a", 1);
        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
        mCache.expireAll();
        assertEquals(1, mCache.getPossiblyExpired("a").intValue());
    }

    public void testGetNotExpired_NotExisting() {
        assertNull(mCache.get("a"));
        mCache.put("b", 1);
        assertNull(mCache.get("a"));
    }

    public void testGetNotExpired_Expired() {
        mCache.put("a", 1);
        assertEquals(1, mCache.get("a").intValue());
        mCache.expireAll();
        assertNull(mCache.get("a"));
    }

    public void testGetCachedValue_NotExisting() {
        assertNull(mCache.getCachedValue("a"));
        mCache.put("b", 1);
        assertNull(mCache.getCachedValue("a"));
    }

    public void testGetCachedValue_Expired() {
        mCache.put("a", 1);
        assertFalse("Should not be expired", mCache.getCachedValue("a").isExpired());
        mCache.expireAll();
        assertTrue("Should be expired", mCache.getCachedValue("a").isExpired());
    }

    public void testGetChangedValue_PutAfterExpired() {
        mCache.put("a", 1);
        mCache.expireAll();
        mCache.put("a", 1);
        assertFalse("Should not be expired", mCache.getCachedValue("a").isExpired());
    }

    public void testComputingCache() {
        // Creates a cache in which all unknown values default to zero.
        mCache = ExpirableCache.create(
                new LruCache<String, ExpirableCache.CachedValue<Integer>>(10) {
                    @Override
                    protected CachedValue<Integer> create(String key) {
                        return mCache.newCachedValue(0);
                    }
                });

        // The first time we request a new value, we add it to the cache.
        CachedValue<Integer> cachedValue = mCache.getCachedValue("a");
        assertNotNull("Should have been created implicitly", cachedValue);
        assertEquals(0, cachedValue.getValue().intValue());
        assertFalse("Should not be expired", cachedValue.isExpired());

        // If we expire all the values, the implicitly created value will also be marked as expired.
        mCache.expireAll();
        CachedValue<Integer> expiredCachedValue = mCache.getCachedValue("a");
        assertNotNull("Should have been created implicitly", expiredCachedValue);
        assertEquals(0, expiredCachedValue.getValue().intValue());
        assertTrue("Should be expired", expiredCachedValue.isExpired());
    }
}