From 8586a43424bc9e53dd7a01c5bf2214ba317da43b Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 24 May 2013 12:23:55 -0700 Subject: [PATCH] Make unit test independent from test device locale Bug: 9112465 Change-Id: I4fe39bebf130799397485dbe6d23199a29a8641e --- .../internal/KeySpecParserSplitTests.java | 17 ++++++++++++++--- .../keyboard/internal/KeySpecParserTests.java | 19 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserSplitTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserSplitTests.java index 7a87f3722..36d946670 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserSplitTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserSplitTests.java @@ -17,10 +17,13 @@ package com.android.inputmethod.keyboard.internal; import android.app.Instrumentation; +import android.content.Context; +import android.content.res.Resources; import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.MediumTest; import com.android.inputmethod.latin.CollectionUtils; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import java.lang.reflect.Field; import java.util.ArrayList; @@ -29,15 +32,23 @@ import java.util.Locale; @MediumTest public class KeySpecParserSplitTests extends InstrumentationTestCase { - private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); + private static final Locale TEST_LOCALE = Locale.ENGLISH; + final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); @Override protected void setUp() throws Exception { super.setUp(); final Instrumentation instrumentation = getInstrumentation(); - mTextsSet.setLanguage(Locale.ENGLISH.getLanguage()); - mTextsSet.loadStringResources(instrumentation.getTargetContext()); + final Context targetContext = instrumentation.getTargetContext(); + mTextsSet.setLanguage(TEST_LOCALE.getLanguage()); + new RunInLocale() { + @Override + protected Void job(final Resources res) { + mTextsSet.loadStringResources(targetContext); + return null; + } + }.runInLocale(targetContext.getResources(), TEST_LOCALE); final String[] testResourceNames = getAllResourceIdNames( com.android.inputmethod.latin.tests.R.string.class); mTextsSet.loadStringResourcesInternal(instrumentation.getContext(), diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java index 861abe831..b55158d3e 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java @@ -20,18 +20,22 @@ import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UN import static com.android.inputmethod.latin.Constants.CODE_OUTPUT_TEXT; import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; +import android.content.Context; +import android.content.res.Resources; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import com.android.inputmethod.latin.Constants; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import java.util.Arrays; import java.util.Locale; @SmallTest public class KeySpecParserTests extends AndroidTestCase { - private final KeyboardCodesSet mCodesSet = new KeyboardCodesSet(); - private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); + private final static Locale TEST_LOCALE = Locale.ENGLISH; + final KeyboardCodesSet mCodesSet = new KeyboardCodesSet(); + final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); private static final String CODE_SETTINGS = "!code/key_settings"; private static final String ICON_SETTINGS = "!icon/settings_key"; @@ -48,10 +52,17 @@ public class KeySpecParserTests extends AndroidTestCase { protected void setUp() throws Exception { super.setUp(); - final String language = Locale.ENGLISH.getLanguage(); + final String language = TEST_LOCALE.getLanguage(); mCodesSet.setLanguage(language); mTextsSet.setLanguage(language); - mTextsSet.loadStringResources(getContext()); + final Context context = getContext(); + new RunInLocale() { + @Override + protected Void job(final Resources res) { + mTextsSet.loadStringResources(context); + return null; + } + }.runInLocale(context.getResources(), TEST_LOCALE); mCodeSettings = KeySpecParser.parseCode( CODE_SETTINGS, mCodesSet, CODE_UNSPECIFIED);