aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2014-04-07 22:32:06 +0900
committerJean Chalard <jchalard@google.com>2014-04-10 17:32:05 +0900
commit4370ff0998d3240cfda7745d08edbdd11703b984 (patch)
treebe63baa9809d7b3541c55ec4f11c6af2e5f1cff5
parenta4ac18551f6bf6ca7ee84139e8cea3656a4a8b86 (diff)
downloadlatinime-4370ff0998d3240cfda7745d08edbdd11703b984.tar.gz
latinime-4370ff0998d3240cfda7745d08edbdd11703b984.tar.xz
latinime-4370ff0998d3240cfda7745d08edbdd11703b984.zip
Fix some flaky tests.
In tests, we create many instances of LatinIME, but we never destroy them. That means we never close the dictionaries nor the handlers. This change calls onDestroy, which closes all dictionaries, and adds some code to finish the handlers. Change-Id: I942517a2a940c54256b08763f6b38f5b55809f55
-rw-r--r--java/src/com/android/inputmethod/compat/LooperCompatUtils.java42
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java5
-rw-r--r--java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java11
-rw-r--r--java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java7
-rw-r--r--tests/src/com/android/inputmethod/latin/InputTestsBase.java5
5 files changed, 70 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/compat/LooperCompatUtils.java b/java/src/com/android/inputmethod/compat/LooperCompatUtils.java
new file mode 100644
index 000000000..d647dbbd3
--- /dev/null
+++ b/java/src/com/android/inputmethod/compat/LooperCompatUtils.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2014 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.compat;
+
+import android.os.Looper;
+
+import java.lang.reflect.Method;
+
+/**
+ * Helper to call Looper#quitSafely, which was introduced in API
+ * level 18 (Build.VERSION_CODES.JELLY_BEAN_MR2).
+ *
+ * In unit tests, we create lots of instances of LatinIME, which means we need to clean up
+ * some Loopers lest we leak file descriptors. In normal use on a device though, this is never
+ * necessary (although it does not hurt).
+ */
+public final class LooperCompatUtils {
+ private static final Method METHOD_quitSafely = CompatUtils.getMethod(
+ Looper.class, "quitSafely");
+
+ public static void quitSafely(final Looper looper) {
+ if (null != METHOD_quitSafely) {
+ CompatUtils.invoke(looper, null /* default return value */, METHOD_quitSafely);
+ } else {
+ looper.quit();
+ }
+ }
+}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 6d36af77a..1f15a9759 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -635,6 +635,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
super.onDestroy();
}
+ @UsedForTesting
+ public void recycle() {
+ mInputLogic.recycle();
+ }
+
@Override
public void onConfigurationChanged(final Configuration conf) {
// If orientation changed while predicting, commit the change
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 7cf8c5e49..0754b1fa9 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -148,6 +148,17 @@ public final class InputLogic {
mInputLogicHandler.reset();
}
+ // Normally this class just gets out of scope after the process ends, but in unit tests, we
+ // create several instances of LatinIME in the same process, which results in several
+ // instances of InputLogic. This cleans up the associated handler so that tests don't leak
+ // handlers.
+ public void recycle() {
+ final InputLogicHandler inputLogicHandler = mInputLogicHandler;
+ mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
+ inputLogicHandler.destroy();
+ mSuggest.mDictionaryFacilitator.closeDictionaries();
+ }
+
/**
* React to a string input.
*
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
index e3b8ab465..64bba681f 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
@@ -20,6 +20,7 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
+import com.android.inputmethod.compat.LooperCompatUtils;
import com.android.inputmethod.latin.InputPointers;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.Suggest;
@@ -80,6 +81,12 @@ class InputLogicHandler implements Handler.Callback {
mNonUIThreadHandler.removeCallbacksAndMessages(null);
}
+ // In unit tests, we create several instances of LatinIME, which results in several instances
+ // of InputLogicHandler. To avoid these handlers lingering, we call this.
+ public void destroy() {
+ LooperCompatUtils.quitSafely(mNonUIThreadHandler.getLooper());
+ }
+
/**
* Handle a message.
* @see android.os.Handler.Callback#handleMessage(android.os.Message)
diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
index 1383ff903..e5f111ab6 100644
--- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java
+++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
@@ -213,13 +213,18 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
@Override
protected void tearDown() throws Exception {
+ mLatinIME.onFinishInputView(true);
+ mLatinIME.onFinishInput();
+ runMessages();
mLatinIME.mHandler.removeAllMessages();
setBooleanPreference(Settings.PREF_BIGRAM_PREDICTIONS, mPreviousBigramPredictionSettings,
true /* defaultValue */);
setStringPreference(Settings.PREF_AUTO_CORRECTION_THRESHOLD, mPreviousAutoCorrectSetting,
DEFAULT_AUTO_CORRECTION_THRESHOLD);
setDebugMode(false);
+ mLatinIME.recycle();
super.tearDown();
+ mLatinIME = null;
}
// We need to run the messages added to the handler from LatinIME. The only way to do