Add unit test helper method to BinaryDictionary and Suggest

Bug: 3414081
Change-Id: Idee64010f2f423d3c7c548d0279c7bf287088762
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index 813f7d3..a7e95a0 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -20,6 +20,7 @@
 import android.content.res.AssetFileDescriptor;
 import android.util.Log;
 
+import java.io.File;
 import java.util.Arrays;
 
 /**
@@ -72,9 +73,40 @@
     public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
         synchronized (sInstance) {
             sInstance.closeInternal();
-            if (resId != 0) {
-                sInstance.loadDictionary(context, resId);
+            try {
+                final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
+                if (afd == null) {
+                    Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
+                    return null;
+                }
+                final String sourceDir = context.getApplicationInfo().sourceDir;
+                final File packagePath = new File(sourceDir);
+                // TODO: Come up with a way to handle a directory.
+                if (!packagePath.isFile()) {
+                    Log.e(TAG, "sourceDir is not a file: " + sourceDir);
+                    return null;
+                }
+                sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength());
                 sInstance.mDicTypeId = dicTypeId;
+            } catch (android.content.res.Resources.NotFoundException e) {
+                Log.e(TAG, "Could not find the resource. resId=" + resId);
+                return null;
+            }
+        }
+        return sInstance;
+    }
+
+    // For unit test
+    /* package */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
+            long length, int dicTypeId) {
+        synchronized (sInstance) {
+            sInstance.closeInternal();
+            if (dictionary.isFile()) {
+                sInstance.loadDictionary(dictionary.getAbsolutePath(), startOffset, length);
+                sInstance.mDicTypeId = dicTypeId;
+            } else {
+                Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath());
+                return null;
             }
         }
         return sInstance;
@@ -92,22 +124,11 @@
             int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
             int maxWordLength, int maxBigrams, int maxAlternatives);
 
-    private final void loadDictionary(Context context, int resId) {
-        try {
-            final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
-            if (afd == null) {
-                Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
-                return;
-            }
-            mNativeDict = openNative(context.getApplicationInfo().sourceDir,
-                    afd.getStartOffset(), afd.getLength(),
+    private final void loadDictionary(String path, long startOffset, long length) {
+        mNativeDict = openNative(path, startOffset, length,
                     TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
                     MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
-            mDictLength = afd.getLength();
-        } catch (android.content.res.Resources.NotFoundException e) {
-            Log.e(TAG, "Could not find the resource. resId=" + resId);
-            return;
-        }
+        mDictLength = length;
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 85a4903..c9e57d0 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -22,6 +22,7 @@
 import android.util.Log;
 import android.view.View;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Arrays;
 
@@ -109,6 +110,12 @@
         initPool();
     }
 
+    // For unit test
+    /* package */ Suggest(File dictionary, long startOffset, long length) {
+        mMainDict = BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN);
+        initPool();
+    }
+
     private void initPool() {
         for (int i = 0; i < mPrefMaxSuggestions; i++) {
             StringBuilder sb = new StringBuilder(getApproxMaxWordLength());