Home | History | Annotate | Download | only in makedict
      1 /*
      2  * Copyright (C) 2013 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.makedict;
     18 
     19 import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.DictBuffer;
     20 import com.android.inputmethod.latin.makedict.DictDecoder.DictionaryBufferFactory;
     21 import com.android.inputmethod.latin.makedict.DictDecoder.DictionaryBufferFromByteArrayFactory;
     22 import com.android.inputmethod.latin.makedict.DictDecoder.
     23         DictionaryBufferFromReadOnlyByteBufferFactory;
     24 import com.android.inputmethod.latin.makedict.DictDecoder.
     25         DictionaryBufferFromWritableByteBufferFactory;
     26 
     27 import android.test.AndroidTestCase;
     28 import android.util.Log;
     29 
     30 import java.io.File;
     31 import java.io.FileOutputStream;
     32 import java.io.IOException;
     33 
     34 /**
     35  * Unit tests for Ver2DictDecoder
     36  */
     37 public class Ver2DictDecoderTests extends AndroidTestCase {
     38     private static final String TAG = Ver2DictDecoderTests.class.getSimpleName();
     39 
     40     private final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
     41 
     42     // Utilities for testing
     43     public void writeDataToFile(final File file) {
     44         FileOutputStream outStream = null;
     45         try {
     46             outStream = new FileOutputStream(file);
     47             outStream.write(data);
     48         } catch (IOException e) {
     49             fail ("Can't write data to the test file");
     50         } finally {
     51             if (outStream != null) {
     52                 try {
     53                     outStream.close();
     54                 } catch (IOException e) {
     55                     Log.e(TAG, "Failed to close the output stream", e);
     56                 }
     57             }
     58         }
     59     }
     60 
     61     public void runTestOpenBuffer(final String testName, final DictionaryBufferFactory factory) {
     62         File testFile = null;
     63         try {
     64             testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir());
     65         } catch (IOException e) {
     66             Log.e(TAG, "IOException while the creating temporary file", e);
     67         }
     68 
     69         assertNotNull(testFile);
     70         final Ver2DictDecoder dictDecoder = new Ver2DictDecoder(testFile, 0, testFile.length(),
     71                 factory);
     72         try {
     73             dictDecoder.openDictBuffer();
     74         } catch (Exception e) {
     75             Log.e(TAG, "Failed to open the buffer", e);
     76         }
     77 
     78         writeDataToFile(testFile);
     79 
     80         try {
     81             dictDecoder.openDictBuffer();
     82         } catch (Exception e) {
     83             Log.e(TAG, "Raised the exception while opening buffer", e);
     84         }
     85 
     86         assertEquals(testFile.length(), dictDecoder.getDictBuffer().capacity());
     87     }
     88 
     89     public void testOpenBufferWithByteBuffer() {
     90         runTestOpenBuffer("testOpenBufferWithByteBuffer",
     91                 new DictionaryBufferFromReadOnlyByteBufferFactory());
     92     }
     93 
     94     public void testOpenBufferWithByteArray() {
     95         runTestOpenBuffer("testOpenBufferWithByteArray",
     96                 new DictionaryBufferFromByteArrayFactory());
     97     }
     98 
     99     public void testOpenBufferWithWritableByteBuffer() {
    100         runTestOpenBuffer("testOpenBufferWithWritableByteBuffer",
    101                 new DictionaryBufferFromWritableByteBufferFactory());
    102     }
    103 
    104     public void runTestGetBuffer(final String testName, final DictionaryBufferFactory factory) {
    105         File testFile = null;
    106         try {
    107             testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir());
    108         } catch (IOException e) {
    109             Log.e(TAG, "IOException while the creating temporary file", e);
    110         }
    111 
    112         final Ver2DictDecoder dictDecoder = new Ver2DictDecoder(testFile, 0, testFile.length(),
    113                 factory);
    114 
    115         // the default return value of getBuffer() must be null.
    116         assertNull("the default return value of getBuffer() is not null",
    117                 dictDecoder.getDictBuffer());
    118 
    119         writeDataToFile(testFile);
    120         assertTrue(testFile.exists());
    121         Log.d(TAG, "file length = " + testFile.length());
    122 
    123         DictBuffer dictBuffer = null;
    124         try {
    125             dictBuffer = dictDecoder.openAndGetDictBuffer();
    126         } catch (IOException e) {
    127             Log.e(TAG, "Failed to open and get the buffer", e);
    128         }
    129         assertNotNull("the buffer must not be null", dictBuffer);
    130 
    131         for (int i = 0; i < data.length; ++i) {
    132             assertEquals(data[i], dictBuffer.readUnsignedByte());
    133         }
    134     }
    135 
    136     public void testGetBufferWithByteBuffer() {
    137         runTestGetBuffer("testGetBufferWithByteBuffer",
    138                 new DictionaryBufferFromReadOnlyByteBufferFactory());
    139     }
    140 
    141     public void testGetBufferWithByteArray() {
    142         runTestGetBuffer("testGetBufferWithByteArray",
    143                 new DictionaryBufferFromByteArrayFactory());
    144     }
    145 
    146     public void testGetBufferWithWritableByteBuffer() {
    147         runTestGetBuffer("testGetBufferWithWritableByteBuffer",
    148                 new DictionaryBufferFromWritableByteBufferFactory());
    149     }
    150 }
    151