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 Ver3DictDecoder 36 */ 37 public class Ver3DictDecoderTests extends AndroidTestCase { 38 private static final String TAG = Ver3DictDecoderTests.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 @SuppressWarnings("null") 62 public void runTestOpenBuffer(final String testName, final DictionaryBufferFactory factory) { 63 File testFile = null; 64 try { 65 testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir()); 66 } catch (IOException e) { 67 Log.e(TAG, "IOException while the creating temporary file", e); 68 } 69 70 assertNotNull(testFile); 71 final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile, 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 @SuppressWarnings("null") 105 public void runTestGetBuffer(final String testName, final DictionaryBufferFactory factory) { 106 File testFile = null; 107 try { 108 testFile = File.createTempFile(testName, ".tmp", getContext().getCacheDir()); 109 } catch (IOException e) { 110 Log.e(TAG, "IOException while the creating temporary file", e); 111 } 112 113 final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile, 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