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