DictionaryFacilitatorLruCacheTests.java revision d6a8adcb044dd8b73a1c96776a835b411a978b46
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    public void testCacheSize() {
27        final DictionaryFacilitatorLruCache cache =
28                new DictionaryFacilitatorLruCache(getContext(), "");
29
30        assertEquals(0, cache.getCachedLocalesForTesting().size());
31        assertNotNull(cache.get(Locale.US));
32        assertEquals(1, cache.getCachedLocalesForTesting().size());
33        assertNotNull(cache.get(Locale.UK));
34        assertEquals(2, cache.getCachedLocalesForTesting().size());
35        assertNotNull(cache.get(Locale.FRENCH));
36        assertEquals(2, cache.getCachedLocalesForTesting().size());
37        cache.evictAll();
38        assertEquals(0, cache.getCachedLocalesForTesting().size());
39    }
40
41    public void testGetFacilitator() {
42        final DictionaryFacilitatorLruCache cache =
43                new DictionaryFacilitatorLruCache(getContext(), "");
44
45        final DictionaryFacilitator dictionaryFacilitatorEnUs = cache.get(Locale.US);
46        assertNotNull(dictionaryFacilitatorEnUs);
47        assertTrue(dictionaryFacilitatorEnUs.isForLocales(new Locale[] { Locale.US }));
48
49        final DictionaryFacilitator dictionaryFacilitatorFr = cache.get(Locale.FRENCH);
50        assertNotNull(dictionaryFacilitatorEnUs);
51        assertTrue(dictionaryFacilitatorFr.isForLocales(new Locale[] { Locale.FRENCH }));
52
53        final DictionaryFacilitator dictionaryFacilitatorDe = cache.get(Locale.GERMANY);
54        assertNotNull(dictionaryFacilitatorDe);
55        assertTrue(dictionaryFacilitatorDe.isForLocales(new Locale[] { Locale.GERMANY }));
56    }
57
58    public void testSetUseContactsDictionary() {
59        final DictionaryFacilitatorLruCache cache =
60                new DictionaryFacilitatorLruCache(getContext(), "");
61
62        assertNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
63        cache.setUseContactsDictionary(true /* useContactsDictionary */);
64        assertNotNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
65        assertNotNull(cache.get(Locale.FRENCH).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
66        assertNotNull(cache.get(Locale.GERMANY).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
67        cache.setUseContactsDictionary(false /* useContactsDictionary */);
68        assertNull(cache.get(Locale.GERMANY).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
69        assertNull(cache.get(Locale.US).getSubDictForTesting(Dictionary.TYPE_CONTACTS));
70    }
71}
72