diff options
Diffstat (limited to 'tests/src')
4 files changed, 185 insertions, 61 deletions
diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index eb4f706cc..ead134b73 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -204,17 +204,16 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> { // view and only delegates to the parts of the code that care. So we don't include them here // to keep these tests as pinpoint as possible and avoid bringing it too many dependencies, // but keep them in mind if something breaks. Commenting them out as is should work. - //mLatinIME.onPressKey(codePoint); - for (final Key key : mKeyboard.mKeys) { - if (key.mCode == codePoint) { - final int x = key.mX + key.mWidth / 2; - final int y = key.mY + key.mHeight / 2; - mLatinIME.onCodeInput(codePoint, x, y); - return; - } + //mLatinIME.onPressKey(codePoint, 0 /* repeatCount */, true /* isSinglePointer */); + final Key key = mKeyboard.getKey(codePoint); + if (key != null) { + final int x = key.mX + key.mWidth / 2; + final int y = key.mY + key.mHeight / 2; + mLatinIME.onCodeInput(codePoint, x, y); + return; } mLatinIME.onCodeInput(codePoint, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); - //mLatinIME.onReleaseKey(codePoint, false); + //mLatinIME.onReleaseKey(codePoint, false /* withSliding */); } protected void type(final String stringToType) { diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java index ef4ed3396..e4b5ad279 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java @@ -27,15 +27,13 @@ import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; -import com.android.inputmethod.latin.utils.ByteArrayWrapper; import com.android.inputmethod.latin.utils.CollectionUtils; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -120,31 +118,13 @@ public class BinaryDictIOTests extends AndroidTestCase { /** * Makes new buffer according to BUFFER_TYPE. */ - private FusionDictionaryBufferInterface getBuffer(final File file, final int bufferType) { - FileInputStream inStream = null; - try { - inStream = new FileInputStream(file); - if (bufferType == USE_BYTE_ARRAY) { - final byte[] array = new byte[(int)file.length()]; - inStream.read(array); - return new ByteArrayWrapper(array); - } else if (bufferType == USE_BYTE_BUFFER){ - final ByteBuffer buffer = inStream.getChannel().map( - FileChannel.MapMode.READ_ONLY, 0, file.length()); - return new BinaryDictInputOutput.ByteBufferWrapper(buffer); - } - } catch (IOException e) { - Log.e(TAG, "IOException while making buffer", e); - } finally { - if (inStream != null) { - try { - inStream.close(); - } catch (IOException e) { - Log.e(TAG, "IOException while closing stream", e); - } - } + private void getBuffer(final BinaryDictReader reader, final int bufferType) + throws FileNotFoundException, IOException { + if (bufferType == USE_BYTE_BUFFER) { + reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory()); + } else if (bufferType == USE_BYTE_ARRAY) { + reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory()); } - return null; } /** @@ -285,13 +265,14 @@ public class BinaryDictIOTests extends AndroidTestCase { final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap, final int bufferType) { long now, diff = -1; - final FusionDictionaryBufferInterface buffer = getBuffer(file, bufferType); - assertNotNull(buffer); + final BinaryDictReader reader = new BinaryDictReader(file); FusionDictionary dict = null; try { + getBuffer(reader, bufferType); + assertNotNull(reader.getBuffer()); now = System.currentTimeMillis(); - dict = BinaryDictInputOutput.readDictionaryBinary(buffer, null); + dict = BinaryDictInputOutput.readDictionaryBinary(reader, null); diff = System.currentTimeMillis() - now; } catch (IOException e) { Log.e(TAG, "IOException while reading dictionary", e); @@ -421,11 +402,12 @@ public class BinaryDictIOTests extends AndroidTestCase { final Map<Integer, Integer> resultFreqs = CollectionUtils.newTreeMap(); long now = -1, diff = -1; - final FusionDictionaryBufferInterface buffer = getBuffer(file, bufferType); - assertNotNull("Can't get buffer.", buffer); + final BinaryDictReader reader = new BinaryDictReader(file); try { + getBuffer(reader, bufferType); + assertNotNull("Can't get buffer.", reader.getBuffer()); now = System.currentTimeMillis(); - BinaryDictIOUtils.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs, + BinaryDictIOUtils.readUnigramsAndBigramsBinary(reader, resultWords, resultFreqs, resultBigrams); diff = System.currentTimeMillis() - now; } catch (IOException e) { @@ -562,7 +544,16 @@ public class BinaryDictIOTests extends AndroidTestCase { addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE); - final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); + final BinaryDictReader reader = new BinaryDictReader(file); + FusionDictionaryBufferInterface buffer = null; + try { + buffer = reader.openAndGetBuffer( + new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory()); + } catch (IOException e) { + // ignore + Log.e(TAG, "IOException while opening the buffer", e); + } + assertNotNull("Can't get the buffer", buffer); try { // too long word @@ -614,7 +605,16 @@ public class BinaryDictIOTests extends AndroidTestCase { addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE); - final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); + final BinaryDictReader reader = new BinaryDictReader(file); + FusionDictionaryBufferInterface buffer = null; + try { + buffer = reader.openAndGetBuffer( + new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory()); + } catch (IOException e) { + // ignore + Log.e(TAG, "IOException while opening the buffer", e); + } + assertNotNull("Can't get the buffer", buffer); try { MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java new file mode 100644 index 000000000..5f6950a4e --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictReaderTests.java @@ -0,0 +1,138 @@ +/* + * 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.makedict; + +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; +import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFactory; +import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory; +import com.android.inputmethod.latin.makedict.BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory; + +import android.test.AndroidTestCase; +import android.util.Log; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Unit tests for BinaryDictReader + */ +public class BinaryDictReaderTests extends AndroidTestCase { + private static final String TAG = BinaryDictReaderTests.class.getSimpleName(); + + private final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + // Utilities for testing + public void writeDataToFile(final File file) { + FileOutputStream outStream = null; + try { + outStream = new FileOutputStream(file); + outStream.write(data); + } catch (IOException e) { + fail ("Can't write data to the test file"); + } finally { + if (outStream != null) { + try { + outStream.close(); + } catch (IOException e) { + Log.e(TAG, "Failed to close the output stream", e); + } + } + } + } + + @SuppressWarnings("null") + public void runTestOpenBuffer(final String testName, + final FusionDictionaryBufferFactory factory) { + File testFile = null; + try { + testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir()); + } catch (IOException e) { + Log.e(TAG, "IOException while the creating temporary file", e); + } + + assertNotNull(testFile); + final BinaryDictReader reader = new BinaryDictReader(testFile); + try { + reader.openBuffer(factory); + } catch (Exception e) { + Log.e(TAG, "Failed to open the buffer", e); + } + + writeDataToFile(testFile); + + try { + reader.openBuffer(factory); + } catch (Exception e) { + Log.e(TAG, "Raised the exception while opening buffer", e); + } + + assertEquals(testFile.length(), reader.getBuffer().capacity()); + } + + public void testOpenBufferWithByteBuffer() { + runTestOpenBuffer("testOpenBufferWithByteBuffer", + new FusionDictionaryBufferFromByteBufferFactory()); + } + + public void testOpenBufferWithByteArray() { + runTestOpenBuffer("testOpenBufferWithByteArray", + new FusionDictionaryBufferFromByteArrayFactory()); + } + + @SuppressWarnings("null") + public void runTestGetBuffer(final String testName, + final FusionDictionaryBufferFactory factory) { + File testFile = null; + try { + testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir()); + } catch (IOException e) { + Log.e(TAG, "IOException while the creating temporary file", e); + } + + final BinaryDictReader reader = new BinaryDictReader(testFile); + + // the default return value of getBuffer() must be null. + assertNull("the default return value of getBuffer() is not null", reader.getBuffer()); + + writeDataToFile(testFile); + assertTrue(testFile.exists()); + Log.d(TAG, "file length = " + testFile.length()); + + FusionDictionaryBufferInterface buffer = null; + try { + buffer = reader.openAndGetBuffer(factory); + } catch (IOException e) { + Log.e(TAG, "Failed to open and get the buffer", e); + } + assertNotNull("the buffer must not be null", buffer); + + for (int i = 0; i < data.length; ++i) { + assertEquals(data[i], buffer.readUnsignedByte()); + } + } + + public void testGetBufferWithByteBuffer() { + runTestGetBuffer("testGetBufferWithByteBuffer", + new FusionDictionaryBufferFromByteBufferFactory()); + } + + public void testGetBufferWithByteArray() { + runTestGetBuffer("testGetBufferWithByteArray", + new FusionDictionaryBufferFromByteArrayFactory()); + } +} diff --git a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java index b6798392f..ce62bf21a 100644 --- a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java @@ -21,16 +21,15 @@ import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; +import com.android.inputmethod.latin.makedict.BinaryDictReader; import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList; -import com.android.inputmethod.latin.utils.ByteArrayWrapper; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -147,27 +146,15 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase } private void readDictFromFile(final File file, final OnAddWordListener listener) { - FileInputStream inStream = null; - + final BinaryDictReader reader = new BinaryDictReader(file); try { - inStream = new FileInputStream(file); - final byte[] buffer = new byte[(int)file.length()]; - inStream.read(buffer); - - UserHistoryDictIOUtils.readDictionaryBinary(new ByteArrayWrapper(buffer), listener); + reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory()); } catch (FileNotFoundException e) { Log.e(TAG, "file not found", e); } catch (IOException e) { Log.e(TAG, "IOException", e); - } finally { - if (inStream != null) { - try { - inStream.close(); - } catch (IOException e) { - // do nothing - } - } } + UserHistoryDictIOUtils.readDictionaryBinary(reader, listener); } public void testGenerateFusionDictionary() { |