blob: 65d2472ef2a9f810d234459efdeaa9158ef50589 [file] [log] [blame]
Adam Lesinski393b5f02015-12-17 13:03:11 -08001/*
2 * Copyright (C) 2015 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#include "compile/Pseudolocalizer.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "test/Test.h"
20#include "util/Util.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080021
Adam Lesinskid5083f62017-01-16 15:07:21 -080022using android::StringPiece;
23
Adam Lesinski393b5f02015-12-17 13:03:11 -080024namespace aapt {
25
26// In this context, 'Axis' represents a particular field in the configuration,
27// such as language or density.
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029static ::testing::AssertionResult SimpleHelper(const char* input,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 const char* expected,
Adam Lesinski393b5f02015-12-17 13:03:11 -080031 Pseudolocalizer::Method method) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 Pseudolocalizer pseudo(method);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 std::string result = pseudo.Start() + pseudo.Text(input) + pseudo.End();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 if (result != expected) {
35 return ::testing::AssertionFailure() << expected << " != " << result;
36 }
37 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080038}
39
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040static ::testing::AssertionResult CompoundHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 const char* in1, const char* in2, const char* in3, const char* expected,
42 Pseudolocalizer::Method method) {
43 Pseudolocalizer pseudo(method);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 std::string result = pseudo.Start() + pseudo.Text(in1) + pseudo.Text(in2) +
45 pseudo.Text(in3) + pseudo.End();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 if (result != expected) {
47 return ::testing::AssertionFailure() << expected << " != " << result;
48 }
49 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080050}
51
52TEST(PseudolocalizerTest, NoPseudolocalization) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kNone));
54 EXPECT_TRUE(SimpleHelper("Hello, world", "Hello, world",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "Hello, world",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080059}
60
61TEST(PseudolocalizerTest, PlaintextAccent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 EXPECT_TRUE(SimpleHelper("", "[]", Pseudolocalizer::Method::kAccent));
63 EXPECT_TRUE(SimpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 EXPECT_TRUE(SimpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 EXPECT_TRUE(SimpleHelper("Battery %1d%%", "[βåţţéŕý »%1d«%% one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 Pseudolocalizer::Method::kAccent));
71 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 SimpleHelper("^1 %", "[^1 % one]", Pseudolocalizer::Method::kAccent));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 CompoundHelper("", "", "", "[]", Pseudolocalizer::Method::kAccent));
75 EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "[Ĥéļļö, ŵöŕļð one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080077}
78
79TEST(PseudolocalizerTest, PlaintextBidi) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kBidi));
81 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 "word", "\xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f",
83 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
86 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
89 Pseudolocalizer::Method::kBidi));
90 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 SimpleHelper("hello\n world\n",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n"
93 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n",
94 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 EXPECT_TRUE(CompoundHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 "hello", "\n ", " world\n",
97 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n"
98 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n",
99 Pseudolocalizer::Method::kBidi));
Igor Viarheichyk4fb65162017-07-06 15:23:51 -0700100 EXPECT_TRUE(
101 SimpleHelper("hello\\nworld\\n",
102 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\\n"
103 "\xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\\n",
104 Pseudolocalizer::Method::kBidi));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800105}
106
107TEST(PseudolocalizerTest, SimpleICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 EXPECT_TRUE(SimpleHelper("{placeholder}", "[»{placeholder}«]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 EXPECT_TRUE(SimpleHelper("{USER} is offline", "[»{USER}« îš öƒƒļîñé one two]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 EXPECT_TRUE(SimpleHelper("Copy from {path1} to {path2}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
115 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 EXPECT_TRUE(SimpleHelper("Today is {1,date} {1,time}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 "[Ţöðåý îš »{1,date}« »{1,time}« one two]",
118 Pseudolocalizer::Method::kAccent));
119
120 // Multi-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 EXPECT_TRUE(CompoundHelper("{USER}", " ", "is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 "[»{USER}« îš öƒƒļîñé one two]",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800123 Pseudolocalizer::Method::kAccent));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 EXPECT_TRUE(CompoundHelper("Copy from ", "{path1}", " to {path2}",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800125 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
126 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800127}
128
129TEST(PseudolocalizerTest, ICUBidi) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 "{placeholder}",
133 "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f",
134 Pseudolocalizer::Method::kBidi));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 "{COUNT, plural, one {one} other {other}}",
137 "{COUNT, plural, "
138 "one {\xe2\x80\x8f\xE2\x80\xaeone\xE2\x80\xac\xe2\x80\x8f} "
139 "other {\xe2\x80\x8f\xE2\x80\xaeother\xE2\x80\xac\xe2\x80\x8f}}",
140 Pseudolocalizer::Method::kBidi));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800141}
142
143TEST(PseudolocalizerTest, Escaping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 // Single-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 EXPECT_TRUE(SimpleHelper("'{USER'} is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]",
147 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 // Multi-fragment messages
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 EXPECT_TRUE(CompoundHelper("'{USER}", " ", "''is offline",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]",
152 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800153}
154
155TEST(PseudolocalizerTest, PluralsAndSelects) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 "{COUNT, plural, one {Delete a file} other {Delete {COUNT} files}}",
158 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
159 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
160 Pseudolocalizer::Method::kAccent));
161
162 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 SimpleHelper("Distance is {COUNT, plural, one {# mile} other {# miles}}",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 "[Ðîšţåñçé îš {COUNT, plural, one {# ḿîļé one two} "
165 "other {# ḿîļéš one two}}]",
166 Pseudolocalizer::Method::kAccent));
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 EXPECT_TRUE(SimpleHelper(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 "{1, select, female {{1} added you} "
170 "male {{1} added you} other {{1} added you}}",
171 "[{1, select, female {»{1}« åððéð ýöû one two} "
172 "male {»{1}« åððéð ýöû one two} other {»{1}« åððéð ýöû one two}}]",
173 Pseudolocalizer::Method::kAccent));
174
175 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 CompoundHelper("{COUNT, plural, one {Delete a file} "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 "other {Delete ",
178 "{COUNT}", " files}}",
179 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
Adam Lesinski393b5f02015-12-17 13:03:11 -0800180 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800182}
183
184TEST(PseudolocalizerTest, NestedICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 EXPECT_TRUE(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 SimpleHelper("{person, select, "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 "female {"
188 "{num_circles, plural,"
189 "=0{{person} didn't add you to any of her circles.}"
190 "=1{{person} added you to one of her circles.}"
191 "other{{person} added you to her # circles.}}}"
192 "male {"
193 "{num_circles, plural,"
194 "=0{{person} didn't add you to any of his circles.}"
195 "=1{{person} added you to one of his circles.}"
196 "other{{person} added you to his # circles.}}}"
197 "other {"
198 "{num_circles, plural,"
199 "=0{{person} didn't add you to any of their circles.}"
200 "=1{{person} added you to one of their circles.}"
201 "other{{person} added you to their # circles.}}}}",
202 "[{person, select, "
203 "female {"
204 "{num_circles, plural,"
205 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥéŕ çîŕçļéš."
206 " one two three four five}"
207 "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥéŕ çîŕçļéš."
208 " one two three four}"
209 "other{»{person}« åððéð ýöû ţö ĥéŕ # çîŕçļéš."
210 " one two three four}}}"
211 "male {"
212 "{num_circles, plural,"
213 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥîš çîŕçļéš."
214 " one two three four five}"
215 "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥîš çîŕçļéš."
216 " one two three four}"
217 "other{»{person}« åððéð ýöû ţö ĥîš # çîŕçļéš."
218 " one two three four}}}"
219 "other {{num_circles, plural,"
220 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ţĥéîŕ çîŕçļéš."
221 " one two three four five}"
222 "=1{»{person}« åððéð ýöû ţö öñé öƒ ţĥéîŕ çîŕçļéš."
223 " one two three four}"
224 "other{»{person}« åððéð ýöû ţö ţĥéîŕ # çîŕçļéš."
225 " one two three four}}}}]",
226 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800227}
228
229TEST(PseudolocalizerTest, RedefineMethod) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 Pseudolocalizer pseudo(Pseudolocalizer::Method::kAccent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 std::string result = pseudo.Text("Hello, ");
232 pseudo.SetMethod(Pseudolocalizer::Method::kNone);
233 result += pseudo.Text("world!");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 ASSERT_EQ(StringPiece("Ĥéļļö, world!"), result);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800235}
236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237} // namespace aapt