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.utils; 18 19 import android.content.res.Resources; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.text.TextUtils; 23 24 import com.android.inputmethod.latin.settings.SpacingAndPunctuations; 25 import com.android.inputmethod.latin.utils.LocaleUtils; 26 27 import java.util.Locale; 28 29 @SmallTest 30 public class CapsModeUtilsTests extends AndroidTestCase { 31 private static void onePathForCaps(final CharSequence cs, final int expectedResult, 32 final int mask, final SpacingAndPunctuations sp, final boolean hasSpaceBefore) { 33 final int oneTimeResult = expectedResult & mask; 34 assertEquals("After >" + cs + "<", oneTimeResult, 35 CapsModeUtils.getCapsMode(cs, mask, sp, hasSpaceBefore)); 36 } 37 38 private static void allPathsForCaps(final CharSequence cs, final int expectedResult, 39 final SpacingAndPunctuations sp, final boolean hasSpaceBefore) { 40 final int c = TextUtils.CAP_MODE_CHARACTERS; 41 final int w = TextUtils.CAP_MODE_WORDS; 42 final int s = TextUtils.CAP_MODE_SENTENCES; 43 onePathForCaps(cs, expectedResult, c | w | s, sp, hasSpaceBefore); 44 onePathForCaps(cs, expectedResult, w | s, sp, hasSpaceBefore); 45 onePathForCaps(cs, expectedResult, c | s, sp, hasSpaceBefore); 46 onePathForCaps(cs, expectedResult, c | w, sp, hasSpaceBefore); 47 onePathForCaps(cs, expectedResult, c, sp, hasSpaceBefore); 48 onePathForCaps(cs, expectedResult, w, sp, hasSpaceBefore); 49 onePathForCaps(cs, expectedResult, s, sp, hasSpaceBefore); 50 } 51 52 public void testGetCapsMode() { 53 final int c = TextUtils.CAP_MODE_CHARACTERS; 54 final int w = TextUtils.CAP_MODE_WORDS; 55 final int s = TextUtils.CAP_MODE_SENTENCES; 56 final RunInLocale<SpacingAndPunctuations> job = new RunInLocale<SpacingAndPunctuations>() { 57 @Override 58 protected SpacingAndPunctuations job(final Resources res) { 59 return new SpacingAndPunctuations(res); 60 } 61 }; 62 final Resources res = getContext().getResources(); 63 SpacingAndPunctuations sp = job.runInLocale(res, Locale.ENGLISH); 64 allPathsForCaps("", c | w | s, sp, false); 65 allPathsForCaps("Word", c, sp, false); 66 allPathsForCaps("Word.", c, sp, false); 67 allPathsForCaps("Word ", c | w, sp, false); 68 allPathsForCaps("Word. ", c | w | s, sp, false); 69 allPathsForCaps("Word..", c, sp, false); 70 allPathsForCaps("Word.. ", c | w | s, sp, false); 71 allPathsForCaps("Word... ", c | w | s, sp, false); 72 allPathsForCaps("Word ... ", c | w | s, sp, false); 73 allPathsForCaps("Word . ", c | w, sp, false); 74 allPathsForCaps("In the U.S ", c | w, sp, false); 75 allPathsForCaps("In the U.S. ", c | w, sp, false); 76 allPathsForCaps("Some stuff (e.g. ", c | w, sp, false); 77 allPathsForCaps("In the U.S.. ", c | w | s, sp, false); 78 allPathsForCaps("\"Word.\" ", c | w | s, sp, false); 79 allPathsForCaps("\"Word\". ", c | w | s, sp, false); 80 allPathsForCaps("\"Word\" ", c | w, sp, false); 81 82 // Test for phantom space 83 allPathsForCaps("Word", c | w, sp, true); 84 allPathsForCaps("Word.", c | w | s, sp, true); 85 86 // Tests after some whitespace 87 allPathsForCaps("Word\n", c | w | s, sp, false); 88 allPathsForCaps("Word\n", c | w | s, sp, true); 89 allPathsForCaps("Word\n ", c | w | s, sp, true); 90 allPathsForCaps("Word.\n", c | w | s, sp, false); 91 allPathsForCaps("Word.\n", c | w | s, sp, true); 92 allPathsForCaps("Word.\n ", c | w | s, sp, true); 93 94 sp = job.runInLocale(res, Locale.FRENCH); 95 allPathsForCaps("\"Word.\" ", c | w, sp, false); 96 allPathsForCaps("\"Word\". ", c | w | s, sp, false); 97 allPathsForCaps("\"Word\" ", c | w, sp, false); 98 99 // Test special case for German. German does not capitalize at the start of a 100 // line when the previous line starts with a comma. It does in other cases. 101 sp = job.runInLocale(res, Locale.GERMAN); 102 allPathsForCaps("Liebe Sara,\n", c | w, sp, false); 103 allPathsForCaps("Liebe Sara,\n", c | w, sp, true); 104 allPathsForCaps("Liebe Sara, \n ", c | w, sp, false); 105 allPathsForCaps("Liebe Sara \n ", c | w | s, sp, false); 106 allPathsForCaps("Liebe Sara.\n ", c | w | s, sp, false); 107 sp = job.runInLocale(res, Locale.ENGLISH); 108 allPathsForCaps("Liebe Sara,\n", c | w | s, sp, false); 109 allPathsForCaps("Liebe Sara,\n", c | w | s, sp, true); 110 allPathsForCaps("Liebe Sara, \n ", c | w | s, sp, false); 111 allPathsForCaps("Liebe Sara \n ", c | w | s, sp, false); 112 allPathsForCaps("Liebe Sara.\n ", c | w | s, sp, false); 113 114 // Test armenian period 115 sp = job.runInLocale(res, LocaleUtils.constructLocaleFromString("hy_AM")); 116 assertTrue("Period is not sentence separator in Armenian", 117 !sp.isSentenceSeparator('.')); 118 assertTrue("Sentence separator is Armenian period in Armenian", 119 sp.isSentenceSeparator(0x589)); 120 // No space : capitalize only if MODE_CHARACTERS 121 allPathsForCaps("Word", c, sp, false); 122 allPathsForCaps("Word.", c, sp, false); 123 // Space, but no armenian period : capitalize if MODE_WORDS but not SENTENCES 124 allPathsForCaps("Word. ", c | w, sp, false); 125 // Armenian period : capitalize if MODE_SENTENCES 126 allPathsForCaps("Word\u0589 ", c | w | s, sp, false); 127 } 128 } 129