From 8e9df884aa367dc580b0bc9a470640dcccec65a1 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Fri, 25 Oct 2013 12:32:30 -0700 Subject: [PATCH] Add a test for ver4 native binary dictionary. Bug: 10197478 Bug: 11073222 Change-Id: I6daac6c4b048c65682c25afe6ca0fad83cc9720f --- .../latin/makedict/FormatSpec.java | 2 +- .../latin/Ver4BinaryDictionaryTests.java | 93 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/src/com/android/inputmethod/latin/Ver4BinaryDictionaryTests.java diff --git a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java index b99aca2c8..5aee135ad 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java +++ b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java @@ -264,7 +264,7 @@ public final class FormatSpec { static final int PTNODE_SHORTCUT_LIST_SIZE_SIZE = 2; // These values are used only by version 4 or later. - static final String TRIE_FILE_EXTENSION = ".trie"; + public static final String TRIE_FILE_EXTENSION = ".trie"; static final String FREQ_FILE_EXTENSION = ".freq"; static final String UNIGRAM_TIMESTAMP_FILE_EXTENSION = ".timestamp"; // tat = Terminal Address Table diff --git a/tests/src/com/android/inputmethod/latin/Ver4BinaryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/Ver4BinaryDictionaryTests.java new file mode 100644 index 000000000..25fb1c2a8 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/Ver4BinaryDictionaryTests.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2013 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.inputmethod.latin; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.LargeTest; +import android.util.Log; + +import com.android.inputmethod.latin.makedict.DictEncoder; +import com.android.inputmethod.latin.makedict.FormatSpec; +import com.android.inputmethod.latin.makedict.FusionDictionary; +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; +import com.android.inputmethod.latin.makedict.Ver4DictEncoder; +import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions; +import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Locale; + +@LargeTest +public class Ver4BinaryDictionaryTests extends AndroidTestCase { + private static final String TAG = Ver4BinaryDictionaryTests.class.getSimpleName(); + private static final String TEST_LOCALE = "test"; + private static final FormatSpec.FormatOptions FORMAT_OPTIONS = + new FormatSpec.FormatOptions(4, true /* supportsDynamicUpdate */); + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + // TODO: remove after native code support dictionary creation. + private DictionaryOptions getDictionaryOptions(final String id, final String version) { + final DictionaryOptions options = new DictionaryOptions(new HashMap(), + false /* germanUmlautProcessing */, false /* frenchLigatureProcessing */); + options.mAttributes.put("version", version); + options.mAttributes.put("dictionary", id); + return options; + } + + // TODO: remove after native code support dictionary creation. + private File getTrieFile(final String id, final String version) { + return new File(getContext().getCacheDir() + "/" + id + "." + version, + TEST_LOCALE + "." + version + FormatSpec.TRIE_FILE_EXTENSION); + } + + public void testIsValidDictionary() { + final String dictVersion = Long.toString(System.currentTimeMillis()); + final File trieFile = getTrieFile(TEST_LOCALE, dictVersion); + + BinaryDictionary binaryDictionary = new BinaryDictionary(trieFile.getAbsolutePath(), + 0 /* offset */, trieFile.length(), true /* useFullEditDistance */, + Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); + assertFalse(binaryDictionary.isValidDictionary()); + + final FusionDictionary dict = new FusionDictionary(new PtNodeArray(), + getDictionaryOptions(TEST_LOCALE, dictVersion)); + DictEncoder encoder = new Ver4DictEncoder(getContext().getCacheDir()); + try { + encoder.writeDictionary(dict, FORMAT_OPTIONS); + } catch (IOException e) { + Log.e(TAG, "IOException while writing dictionary", e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "Unsupported format", e); + } + + binaryDictionary = new BinaryDictionary(trieFile.getAbsolutePath(), + 0 /* offset */, trieFile.length(), true /* useFullEditDistance */, + Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); + assertTrue(binaryDictionary.isValidDictionary()); + } +}