DictionaryFacilitatorLruCacheTests.java revision 5f00fe09e9a611b647592188316e5999465df4d3
1/*
2 * Copyright (C) 2014 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.inputmethod.latin;
18
19import java.util.Locale;
20
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.LargeTest;
23
24@LargeTest
25public class DictionaryFacilitatorLruCacheTests extends AndroidTestCase {
26    static final int MAX_CACHE_SIZE = 2;
27    static final int MAX_CACHE_SIZE_LARGE = 5;
28
29    public void testCacheSize() {
30        final DictionaryFacilitatorLruCache cache =
31                new DictionaryFacilitatorLruCache(getContext(), MAX_CACHE_SIZE, "");
32
33        assertEquals(0, cache.getCachedLocalesForTesting().size());
34        assertNotNull(cache.get(Locale.US));
35        assertEquals(1, cache.getCachedLocalesForTesting().size());
36        assertNotNull(cache.get(Locale.UK));
37        assertEquals(2, cache.getCachedLocalesForTesting().size());
38        assertNotNull(cache.get(Locale.FRENCH));
39        assertEquals(2, cache.getCachedLocalesForTesting().size());
40        cache.evictAll();
41        assertEquals(0, cache.getCachedLocalesForTesting().size());
42    }
43
44    public void testGetFacilitator() {
45        testGetFacilitator(new DictionaryFacilitatorLruCache(getContext(), MAX_CACHE_SIZE, ""));
46        testGetFacilitator(new DictionaryFacilitatorLruCache(
47                getContext(), MAX_CACHE_SIZE_LARGE, ""));
48    }
49
50    private static void testGetFacilitator(final DictionaryFacilitatorLruCache cache) {
51        final DictionaryFacilitator dictionaryFacilitatorEnUs = cache.get(Locale.US);
52        assertNotNull(dictionaryFacilitatorEnUs);
53        assertTrue(dictionaryFacilitatorEnUs.isForLocales(new Locale[] { Locale.US }));
54
55        final DictionaryFacilitator dictionaryFacilitatorFr = cache.get(Locale.FRENCH);
56        assertNotNull(dictionaryFacilitatorEnUs);
57        assertTrue(dictionaryFacilitatorFr.isForLocales(new Locale[] { Locale.FRENCH }));
58
59        final DictionaryFacilitator dictionaryFacilitatorDe = cache.get(Locale.GERMANY);
60        assertNotNull(dictionaryFacilitatorDe);
61        assertTrue(dictionaryFacilitatorDe.isForLocales(new Locale[] { Locale.GERMANY }));
62    }
63
64    public void testSetUseContactsDictionary() {
65        testSetUseContactsDictionary(new DictionaryFacilitatorLruCache(
66                getContext(), MAX_CACHE_SIZE, ""));
67        testSetUseContactsDictionary(new DictionaryFacilitatorLruCache(
68                getContext(), MAX_CACHE_SIZE_LARGE, ""));
69    }
70
71    private static void testSetUseContactsDictionary(final DictionaryFacilitatorLruCache cache) {
72        assertNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
73        cache.setUseContactsDictionary(true /* useContactsDictionary */);
74        assertNotNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
75        assertNotNull(cache.get(Locale.FRENCH).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
76        assertNotNull(cache.get(Locale.GERMANY).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
77        cache.setUseContactsDictionary(false /* useContactsDictionary */);
78        assertNull(cache.get(Locale.GERMANY).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
79        assertNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
80    }
81}
82