diff options
Diffstat (limited to 'tests/src/org/kelar/inputmethod/compat')
3 files changed, 580 insertions, 0 deletions
diff --git a/tests/src/org/kelar/inputmethod/compat/LocaleSpanCompatUtilsTests.java b/tests/src/org/kelar/inputmethod/compat/LocaleSpanCompatUtilsTests.java new file mode 100644 index 000000000..44aae0282 --- /dev/null +++ b/tests/src/org/kelar/inputmethod/compat/LocaleSpanCompatUtilsTests.java @@ -0,0 +1,222 @@ +/* + * 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 org.kelar.inputmethod.compat; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import android.graphics.Typeface; +import android.os.Build; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.style.StyleSpan; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Locale; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class LocaleSpanCompatUtilsTests { + @Test + public void testInstantiatable() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { + // LocaleSpan isn't yet available. + return; + } + assertTrue(LocaleSpanCompatUtils.isLocaleSpanAvailable()); + final Object japaneseLocaleSpan = LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE); + assertNotNull(japaneseLocaleSpan); + assertEquals(Locale.JAPANESE, + LocaleSpanCompatUtils.getLocaleFromLocaleSpan(japaneseLocaleSpan)); + } + + private static void assertLocaleSpan(final Spanned spanned, final int index, + final int expectedStart, final int expectedEnd, + final Locale expectedLocale, final int expectedSpanFlags) { + final Object span = spanned.getSpans(0, spanned.length(), Object.class)[index]; + assertEquals(expectedLocale, LocaleSpanCompatUtils.getLocaleFromLocaleSpan(span)); + assertEquals(expectedStart, spanned.getSpanStart(span)); + assertEquals(expectedEnd, spanned.getSpanEnd(span)); + assertEquals(expectedSpanFlags, spanned.getSpanFlags(span)); + } + + private static void assertSpanEquals(final Object expectedSpan, final Spanned spanned, + final int index) { + final Object[] spans = spanned.getSpans(0, spanned.length(), Object.class); + assertEquals(expectedSpan, spans[index]); + } + + private static void assertSpanCount(final int expectedCount, final Spanned spanned) { + final Object[] spans = spanned.getSpans(0, spanned.length(), Object.class); + assertEquals(expectedCount, spans.length); + } + + @Test + public void testUpdateLocaleSpan() { + if (!LocaleSpanCompatUtils.isLocaleSpanAvailable()) { + return; + } + + // Test if the simplest case works. + { + final SpannableString text = new SpannableString("0123456789"); + LocaleSpanCompatUtils.updateLocaleSpan(text, 1, 5, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 5, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if only LocaleSpans are updated. + { + final SpannableString text = new SpannableString("0123456789"); + final StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); + text.setSpan(styleSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 1, 5, Locale.JAPANESE); + assertSpanCount(2, text); + assertSpanEquals(styleSpan, text, 0); + assertLocaleSpan(text, 1, 1, 5, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if two jointed spans are merged into one span. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 3, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 3, 5, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 5, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if two overlapped spans are merged into one span. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 4, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 3, 5, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 5, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if three overlapped spans are merged into one span. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 4, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 5, 6, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 2, 8, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 8, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if disjoint spans remain disjoint. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 3, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 5, 6, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 8, 9, Locale.JAPANESE); + assertSpanCount(3, text); + assertLocaleSpan(text, 0, 1, 3, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 1, 5, 6, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 2, 8, 9, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if existing span flags are preserved during merge. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 5, + Spanned.SPAN_INCLUSIVE_INCLUSIVE | Spanned.SPAN_INTERMEDIATE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 3, 4, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 5, Locale.JAPANESE, + Spanned.SPAN_INCLUSIVE_INCLUSIVE | Spanned.SPAN_INTERMEDIATE); + } + + // Test if existing span flags are preserved even when partially overlapped (leading edge). + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 1, 5, + Spanned.SPAN_INCLUSIVE_INCLUSIVE | Spanned.SPAN_INTERMEDIATE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 3, 7, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 7, Locale.JAPANESE, + Spanned.SPAN_INCLUSIVE_EXCLUSIVE | Spanned.SPAN_INTERMEDIATE); + } + + // Test if existing span flags are preserved even when partially overlapped (trailing edge). + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.JAPANESE), 3, 7, + Spanned.SPAN_INCLUSIVE_INCLUSIVE | Spanned.SPAN_INTERMEDIATE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 1, 5, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 7, Locale.JAPANESE, + Spanned.SPAN_EXCLUSIVE_INCLUSIVE | Spanned.SPAN_INTERMEDIATE); + } + + // Test if existing locale span will be removed when the locale doesn't match. + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.ENGLISH), 3, 5, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 1, 7, Locale.JAPANESE); + assertSpanCount(1, text); + assertLocaleSpan(text, 0, 1, 7, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if existing locale span will be removed when the locale doesn't match. (case 2) + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.ENGLISH), 3, 7, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 5, 6, Locale.JAPANESE); + assertSpanCount(3, text); + assertLocaleSpan(text, 0, 3, 5, Locale.ENGLISH, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 1, 6, 7, Locale.ENGLISH, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 2, 5, 6, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if existing locale span will be removed when the locale doesn't match. (case 3) + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.ENGLISH), 3, 7, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 2, 5, Locale.JAPANESE); + assertSpanCount(2, text); + assertLocaleSpan(text, 0, 5, 7, Locale.ENGLISH, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 1, 2, 5, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + // Test if existing locale span will be removed when the locale doesn't match. (case 3) + { + final SpannableString text = new SpannableString("0123456789"); + text.setSpan(LocaleSpanCompatUtils.newLocaleSpan(Locale.ENGLISH), 3, 7, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + LocaleSpanCompatUtils.updateLocaleSpan(text, 5, 8, Locale.JAPANESE); + assertSpanCount(2, text); + assertLocaleSpan(text, 0, 3, 5, Locale.ENGLISH, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + assertLocaleSpan(text, 1, 5, 8, Locale.JAPANESE, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } +} diff --git a/tests/src/org/kelar/inputmethod/compat/SuggestionSpanUtilsTest.java b/tests/src/org/kelar/inputmethod/compat/SuggestionSpanUtilsTest.java new file mode 100644 index 000000000..d9fe690ac --- /dev/null +++ b/tests/src/org/kelar/inputmethod/compat/SuggestionSpanUtilsTest.java @@ -0,0 +1,264 @@ +/* + * 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 org.kelar.inputmethod.compat; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +import android.text.Spanned; +import android.text.TextUtils; +import android.text.style.SuggestionSpan; + +import androidx.test.InstrumentationRegistry; +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.kelar.inputmethod.latin.SuggestedWords; +import org.kelar.inputmethod.latin.SuggestedWords.SuggestedWordInfo; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Locale; + +import javax.annotation.Nullable; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class SuggestionSpanUtilsTest { + + private Context getContext() { + return InstrumentationRegistry.getTargetContext(); + } + + /** + * Helper method to create a placeholder {@link SuggestedWordInfo}. + * + * @param kindAndFlags the kind and flags to be used to create {@link SuggestedWordInfo}. + * @param word the word to be used to create {@link SuggestedWordInfo}. + * @return a new instance of {@link SuggestedWordInfo}. + */ + private static SuggestedWordInfo createWordInfo(final String word, final int kindAndFlags) { + return new SuggestedWordInfo(word, "" /* prevWordsContext */, 1 /* score */, kindAndFlags, + null /* sourceDict */, + SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, + SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); + } + + private static void assertNotSuggestionSpan(final String expectedText, + final CharSequence actualText) { + assertTrue(TextUtils.equals(expectedText, actualText)); + if (!(actualText instanceof Spanned)) { + return; + } + final Spanned spanned = (Spanned)actualText; + final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(), + SuggestionSpan.class); + assertEquals(0, suggestionSpans.length); + } + + private static void assertSuggestionSpan(final String expectedText, + final int reuiredSuggestionSpanFlags, final int requiredSpanFlags, + final String[] expectedSuggestions, @Nullable final Locale expectedLocale, + final CharSequence actualText) { + assertTrue(TextUtils.equals(expectedText, actualText)); + assertTrue(actualText instanceof Spanned); + final Spanned spanned = (Spanned)actualText; + final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(), + SuggestionSpan.class); + assertEquals(1, suggestionSpans.length); + final SuggestionSpan suggestionSpan = suggestionSpans[0]; + if (reuiredSuggestionSpanFlags != 0) { + assertTrue((suggestionSpan.getFlags() & reuiredSuggestionSpanFlags) != 0); + } + if (requiredSpanFlags != 0) { + assertTrue((spanned.getSpanFlags(suggestionSpan) & requiredSpanFlags) != 0); + } + if (expectedSuggestions != null) { + final String[] actualSuggestions = suggestionSpan.getSuggestions(); + assertEquals(expectedSuggestions.length, actualSuggestions.length); + for (int i = 0; i < expectedSuggestions.length; ++i) { + assertEquals(expectedSuggestions[i], actualSuggestions[i]); + } + } + // CAVEAT: SuggestionSpan#getLocale() returns String rather than Locale object. + assertEquals(expectedLocale.toString(), suggestionSpan.getLocale()); + } + + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) + @Test + public void testGetTextWithAutoCorrectionIndicatorUnderline() { + final String ORIGINAL_TEXT = "Hey!"; + final Locale NONNULL_LOCALE = new Locale("en", "GB"); + final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( + getContext(), ORIGINAL_TEXT, NONNULL_LOCALE); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { + assertNotSuggestionSpan(ORIGINAL_TEXT, text); + return; + } + assertSuggestionSpan(ORIGINAL_TEXT, + SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */, + Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */, + new String[]{}, NONNULL_LOCALE, text); + } + + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) + @Test + public void testGetTextWithAutoCorrectionIndicatorUnderlineRootLocale() { + final String ORIGINAL_TEXT = "Hey!"; + final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( + getContext(), ORIGINAL_TEXT, Locale.ROOT); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { + assertNotSuggestionSpan(ORIGINAL_TEXT, text); + return; + } + assertSuggestionSpan(ORIGINAL_TEXT, + SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */, + Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */, + new String[]{}, Locale.ROOT, text); + } + + @Test + public void testGetTextWithSuggestionSpan() { + final SuggestedWordInfo prediction1 = + createWordInfo("Quality", SuggestedWordInfo.KIND_PREDICTION); + final SuggestedWordInfo prediction2 = + createWordInfo("Speed", SuggestedWordInfo.KIND_PREDICTION); + final SuggestedWordInfo prediction3 = + createWordInfo("Price", SuggestedWordInfo.KIND_PREDICTION); + + final SuggestedWordInfo typed = + createWordInfo("Hey", SuggestedWordInfo.KIND_TYPED); + + final SuggestedWordInfo[] corrections = + new SuggestedWordInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE * 2]; + for (int i = 0; i < corrections.length; ++i) { + corrections[i] = createWordInfo("correction" + i, SuggestedWordInfo.KIND_CORRECTION); + } + + final Locale NONNULL_LOCALE = new Locale("en", "GB"); + + // SuggestionSpan will not be attached when {@link SuggestedWords#INPUT_STYLE_PREDICTION} + // is specified. + { + final SuggestedWords predictedWords = new SuggestedWords( + new ArrayList<>(Arrays.asList(prediction1, prediction2, prediction3)), + null /* rawSuggestions */, + null /* typedWord */, + false /* typedWordValid */, + false /* willAutoCorrect */, + false /* isObsoleteSuggestions */, + SuggestedWords.INPUT_STYLE_PREDICTION, + SuggestedWords.NOT_A_SEQUENCE_NUMBER); + final String PICKED_WORD = prediction2.mWord; + // Note that the framework uses the context locale as a fallback locale. + assertNotSuggestionSpan( + PICKED_WORD, + SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, + predictedWords, NONNULL_LOCALE)); + } + + final ArrayList<SuggestedWordInfo> suggestedWordList = new ArrayList<>(); + suggestedWordList.add(typed); + suggestedWordList.add(prediction1); + suggestedWordList.add(prediction2); + suggestedWordList.add(prediction3); + suggestedWordList.addAll(Arrays.asList(corrections)); + final SuggestedWords typedAndCollectedWords = new SuggestedWords( + suggestedWordList, + null /* rawSuggestions */, + null /* typedWord */, + false /* typedWordValid */, + false /* willAutoCorrect */, + false /* isObsoleteSuggestions */, + SuggestedWords.INPUT_STYLE_TYPING, + SuggestedWords.NOT_A_SEQUENCE_NUMBER); + + for (final SuggestedWordInfo pickedWord : suggestedWordList) { + final String PICKED_WORD = pickedWord.mWord; + + final ArrayList<String> expectedSuggestions = new ArrayList<>(); + for (SuggestedWordInfo suggestedWordInfo : suggestedWordList) { + if (expectedSuggestions.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) { + break; + } + if (suggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) { + // Currently predictions are not filled into SuggestionSpan. + continue; + } + final String suggestedWord = suggestedWordInfo.mWord; + if (TextUtils.equals(PICKED_WORD, suggestedWord)) { + // Typed word itself is not added to SuggestionSpan. + continue; + } + expectedSuggestions.add(suggestedWord); + } + + // non-null locale + assertSuggestionSpan( + PICKED_WORD, + 0 /* reuiredSuggestionSpanFlags */, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */, + expectedSuggestions.toArray(new String[expectedSuggestions.size()]), + NONNULL_LOCALE, + SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, + typedAndCollectedWords, NONNULL_LOCALE)); + + // root locale + assertSuggestionSpan( + PICKED_WORD, + 0 /* reuiredSuggestionSpanFlags */, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */, + expectedSuggestions.toArray(new String[expectedSuggestions.size()]), + Locale.ROOT, + SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, + typedAndCollectedWords, Locale.ROOT)); + } + } + + @Test + public void testFindFirstLocaleFromSuggestionSpans() { + final String[] suggestions = new String[] {"Quality", "Speed", "Price"}; + final SuggestionSpan nullLocaleSpan = new SuggestionSpan((Locale)null, suggestions, 0); + final SuggestionSpan emptyLocaleSpan = new SuggestionSpan(new Locale(""), suggestions, 0); + final SuggestionSpan enUsLocaleSpan = new SuggestionSpan(Locale.US, suggestions, 0); + final SuggestionSpan jaJpLocaleSpan = new SuggestionSpan(Locale.JAPAN, suggestions, 0); + + assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {})); + + assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {emptyLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {enUsLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, enUsLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, emptyLocaleSpan, enUsLocaleSpan})); + + assertEquals(Locale.JAPAN, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, jaJpLocaleSpan, enUsLocaleSpan})); + } +} diff --git a/tests/src/org/kelar/inputmethod/compat/TextInfoCompatUtilsTests.java b/tests/src/org/kelar/inputmethod/compat/TextInfoCompatUtilsTests.java new file mode 100644 index 000000000..9b2ab94ee --- /dev/null +++ b/tests/src/org/kelar/inputmethod/compat/TextInfoCompatUtilsTests.java @@ -0,0 +1,94 @@ +/* + * 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 org.kelar.inputmethod.compat; + +import static org.junit.Assert.assertTrue; + +import android.graphics.Typeface; +import android.os.Parcel; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.TextUtils; +import android.text.style.StyleSpan; +import android.text.style.URLSpan; +import android.view.textservice.TextInfo; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Arrays; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class TextInfoCompatUtilsTests { + final private static String TEST_TEXT = "0123456789"; + final private static int TEST_COOKIE = 0x1234; + final private static int TEST_SEQUENCE_NUMBER = 0x4321; + final private static int TEST_CHAR_SEQUENCE_START = 1; + final private static int TEST_CHAR_SEQUENCE_END = 6; + final private static StyleSpan TEST_STYLE_SPAN = new StyleSpan(Typeface.BOLD); + final private static int TEST_STYLE_SPAN_START = 4; + final private static int TEST_STYLE_SPAN_END = 5; + final private static int TEST_STYLE_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_INCLUSIVE; + final private static URLSpan TEST_URL_SPAN_URL = new URLSpan("http://example.com"); + final private static int TEST_URL_SPAN_START = 3; + final private static int TEST_URL_SPAN_END = 7; + final private static int TEST_URL_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; + + @Test + public void testGetCharSequence() { + final SpannableString text = new SpannableString(TEST_TEXT); + text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END, + TEST_STYLE_SPAN_FLAGS); + text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END, + TEST_URL_SPAN_FLAGS); + + final TextInfo textInfo = TextInfoCompatUtils.newInstance(text, + TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE, + TEST_SEQUENCE_NUMBER); + final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START, + TEST_CHAR_SEQUENCE_END); + final CharSequence actualCharSequence = + TextInfoCompatUtils.getCharSequenceOrString(textInfo); + + // This should be valid even if TextInfo#getCharSequence is not supported. + assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence)); + + if (TextInfoCompatUtils.isCharSequenceSupported()) { + // This is valid only if TextInfo#getCharSequence is supported. + assertTrue("should be Spanned", actualCharSequence instanceof Spanned); + assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence))); + } + } + + private static byte[] marshall(final CharSequence cahrSequence) { + Parcel parcel = null; + try { + parcel = Parcel.obtain(); + TextUtils.writeToParcel(cahrSequence, parcel, 0); + return parcel.marshall(); + } finally { + if (parcel != null) { + parcel.recycle(); + parcel = null; + } + } + } +} |