Home | History | Annotate | Download | only in utils
      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 
     17 package com.android.inputmethod.latin.utils;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.LargeTest;
     21 
     22 import com.android.inputmethod.latin.BinaryDictionary;
     23 import com.android.inputmethod.latin.makedict.DictionaryHeader;
     24 import com.android.inputmethod.latin.makedict.FormatSpec;
     25 
     26 import java.io.File;
     27 import java.io.IOException;
     28 import java.util.HashMap;
     29 import java.util.Locale;
     30 import java.util.Map;
     31 import java.util.concurrent.TimeUnit;
     32 
     33 @LargeTest
     34 public class BinaryDictionaryUtilsTests extends AndroidTestCase {
     35     private static final String TEST_DICT_FILE_EXTENSION = ".testDict";
     36     private static final String TEST_LOCALE = "test";
     37 
     38     private File createEmptyDictionaryAndGetFile(final String dictId,
     39             final int formatVersion) throws IOException {
     40         if (formatVersion == FormatSpec.VERSION4) {
     41             return createEmptyVer4DictionaryAndGetFile(dictId);
     42         } else {
     43             throw new IOException("Dictionary format version " + formatVersion
     44                     + " is not supported.");
     45         }
     46     }
     47 
     48     private File createEmptyVer4DictionaryAndGetFile(final String dictId) throws IOException {
     49         final File file = getDictFile(dictId);
     50         FileUtils.deleteRecursively(file);
     51         Map<String, String> attributeMap = new HashMap<>();
     52         attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, dictId);
     53         attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY,
     54                 String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
     55         attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY,
     56                 DictionaryHeader.ATTRIBUTE_VALUE_TRUE);
     57         attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY,
     58                 DictionaryHeader.ATTRIBUTE_VALUE_TRUE);
     59         if (BinaryDictionaryUtils.createEmptyDictFile(file.getAbsolutePath(), FormatSpec.VERSION4,
     60                 LocaleUtils.constructLocaleFromString(TEST_LOCALE), attributeMap)) {
     61             return file;
     62         } else {
     63             throw new IOException("Empty dictionary " + file.getAbsolutePath()
     64                     + " cannot be created.");
     65         }
     66     }
     67 
     68     private File getDictFile(final String dictId) {
     69         return new File(getContext().getCacheDir(), dictId + TEST_DICT_FILE_EXTENSION);
     70     }
     71 
     72     public void testRenameDictionary() {
     73         final int formatVersion = FormatSpec.VERSION4;
     74         File dictFile0 = null;
     75         try {
     76             dictFile0 = createEmptyDictionaryAndGetFile("MoveFromDictionary", formatVersion);
     77         } catch (IOException e) {
     78             fail("IOException while writing an initial dictionary : " + e);
     79         }
     80         final File dictFile1 = getDictFile("MoveToDictionary");
     81         FileUtils.deleteRecursively(dictFile1);
     82         assertTrue(BinaryDictionaryUtils.renameDict(dictFile0, dictFile1));
     83         assertFalse(dictFile0.exists());
     84         assertTrue(dictFile1.exists());
     85         BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile1.getAbsolutePath(),
     86                 0 /* offset */, dictFile1.length(), true /* useFullEditDistance */,
     87                 Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
     88         assertTrue(binaryDictionary.isValidDictionary());
     89         assertTrue(binaryDictionary.getFormatVersion() == formatVersion);
     90         binaryDictionary.close();
     91     }
     92 }
     93