1 /*
2  * Copyright (C) 2016 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 android.view.inputmethod.cts.util;
18 
19 import android.text.Selection;
20 import android.text.SpannableStringBuilder;
21 
22 public final class InputConnectionTestUtils {
23 
24     private static final String U1F427 = "\uD83D\uDC27";
25 
26     /**
27      * A utility function to generate test string for input method APIs.  There are several
28      * pre-defined meta characters that are useful for unit tests.
29      *
30      * <p>Pre-defined meta characters:</p>
31      * <dl>
32      *     <dl>{@code [}</dl><dd>The text selection starts from here.</dd>
33      *     <dl>{@code ]}</dl><dd>The text selection ends at here.</dd>
34      *     <dl>{@code <}</dl><dd>Represents a high surrogate character.</dd>
35      *     <dl>{@code >}</dl><dd>Represents a low surrogate character.</dd>
36      * </ul>
37      *
38      * <p>Examples: {@code "012[3<>67]89"} will be converted to {@ode "0123HL6789"}, where
39      * {@code "H"} and {@code "L"} indicate certain high and low surrogate characters, respectively,
40      * with selecting {@code "3HL67"}.</p>
41      *
42      * @param formatString
43      * @return A {@link CharSequence} object with text selection specified by the meta characters.
44      */
formatString(final String formatString)45     public static CharSequence formatString(final String formatString) {
46         final SpannableStringBuilder builder = new SpannableStringBuilder();
47         int selectionStart = -1;
48         int selectionEnd = -1;
49         for (int i = 0; i < formatString.length(); ++i) {
50             final Character c = formatString.charAt(i);
51             switch (c) {
52                 case '[':
53                     selectionStart = builder.length();
54                     break;
55                 case ']':
56                     selectionEnd = builder.length();
57                     break;
58                 case '<':
59                     builder.append(U1F427.charAt(0));  // High surrogate
60                     break;
61                 case '>':
62                     builder.append(U1F427.charAt(1));  // Low surrogate
63                     break;
64                 default:
65                     builder.append(c);
66                     break;
67             }
68         }
69         if (selectionStart < 0) {
70             throw new UnsupportedOperationException("Selection marker '[' must be specified.");
71         }
72         if (selectionEnd < 0) {
73             throw new UnsupportedOperationException("Selection marker ']' must be specified.");
74         }
75         Selection.setSelection(builder, selectionStart, selectionEnd);
76         return builder;
77     }
78 }
79