• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 androidx.emoji2.bundled.util;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 /**
23  * Utility class used to create strings with emojis during tests.
24  */
25 public class TestString {
26 
27     private static final List<Integer> EMPTY_LIST = new ArrayList<>();
28 
29     private static final String EXTRA = "ab";
30     private final List<Integer> mCodePoints;
31     private String mString;
32     private final String mValue;
33     private boolean mHasSuffix;
34     private boolean mHasPrefix;
35 
TestString(int... codePoints)36     public TestString(int... codePoints) {
37         if (codePoints.length == 0) {
38             mCodePoints = EMPTY_LIST;
39         } else {
40             mCodePoints = new ArrayList<>();
41             append(codePoints);
42         }
43         mValue = null;
44     }
45 
TestString(Emoji.EmojiMapping emojiMapping)46     public TestString(Emoji.EmojiMapping emojiMapping) {
47         this(emojiMapping.codepoints());
48     }
49 
TestString(String string)50     public TestString(String string) {
51         mCodePoints = EMPTY_LIST;
52         mValue = string;
53     }
54 
append(int... codePoints)55     public TestString append(int... codePoints) {
56         for (int i = 0; i < codePoints.length; i++) {
57             mCodePoints.add(codePoints[i]);
58         }
59         return this;
60     }
61 
prepend(int... codePoints)62     public TestString prepend(int... codePoints) {
63         for (int i = codePoints.length - 1; i >= 0; i--) {
64             mCodePoints.add(0, codePoints[i]);
65         }
66         return this;
67     }
68 
append(Emoji.EmojiMapping emojiMapping)69     public TestString append(Emoji.EmojiMapping emojiMapping) {
70         return append(emojiMapping.codepoints());
71     }
72 
withSuffix()73     public TestString withSuffix() {
74         mHasSuffix = true;
75         return this;
76     }
77 
withPrefix()78     public TestString withPrefix() {
79         mHasPrefix = true;
80         return this;
81     }
82 
83     @SuppressWarnings("ForLoopReplaceableByForEach")
84     @Override
toString()85     public String toString() {
86         StringBuilder builder = new StringBuilder();
87         if (mHasPrefix) {
88             builder.append(EXTRA);
89         }
90 
91         for (int index = 0; index < mCodePoints.size(); index++) {
92             builder.append(Character.toChars(mCodePoints.get(index)));
93         }
94 
95         if (mValue != null) {
96             builder.append(mValue);
97         }
98 
99         if (mHasSuffix) {
100             builder.append(EXTRA);
101         }
102         mString = builder.toString();
103         return mString;
104     }
105 
emojiStartIndex()106     public int emojiStartIndex() {
107         if (mHasPrefix) return EXTRA.length();
108         return 0;
109     }
110 
emojiEndIndex()111     public int emojiEndIndex() {
112         if (mHasSuffix) return mString.lastIndexOf(EXTRA);
113         return mString.length();
114     }
115 }
116