Pseudolocalizer_test.cpp revision d5083f6f6b9bc76bbe64052bcec639eee752a321
1/*
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"
18
19#include "test/Test.h"
20#include "util/Util.h"
21
22using android::StringPiece;
23
24namespace aapt {
25
26// In this context, 'Axis' represents a particular field in the configuration,
27// such as language or density.
28
29static ::testing::AssertionResult SimpleHelper(const char* input,
30                                               const char* expected,
31                                               Pseudolocalizer::Method method) {
32  Pseudolocalizer pseudo(method);
33  std::string result = pseudo.Start() + pseudo.Text(input) + pseudo.End();
34  if (result != expected) {
35    return ::testing::AssertionFailure() << expected << " != " << result;
36  }
37  return ::testing::AssertionSuccess();
38}
39
40static ::testing::AssertionResult CompoundHelper(
41    const char* in1, const char* in2, const char* in3, const char* expected,
42    Pseudolocalizer::Method method) {
43  Pseudolocalizer pseudo(method);
44  std::string result = pseudo.Start() + pseudo.Text(in1) + pseudo.Text(in2) +
45                       pseudo.Text(in3) + pseudo.End();
46  if (result != expected) {
47    return ::testing::AssertionFailure() << expected << " != " << result;
48  }
49  return ::testing::AssertionSuccess();
50}
51
52TEST(PseudolocalizerTest, NoPseudolocalization) {
53  EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kNone));
54  EXPECT_TRUE(SimpleHelper("Hello, world", "Hello, world",
55                           Pseudolocalizer::Method::kNone));
56
57  EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "Hello, world",
58                             Pseudolocalizer::Method::kNone));
59}
60
61TEST(PseudolocalizerTest, PlaintextAccent) {
62  EXPECT_TRUE(SimpleHelper("", "[]", Pseudolocalizer::Method::kAccent));
63  EXPECT_TRUE(SimpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]",
64                           Pseudolocalizer::Method::kAccent));
65
66  EXPECT_TRUE(SimpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]",
67                           Pseudolocalizer::Method::kAccent));
68
69  EXPECT_TRUE(SimpleHelper("Battery %1d%%", "[βåţţéŕý »%1d«%% one two]",
70                           Pseudolocalizer::Method::kAccent));
71  EXPECT_TRUE(
72      SimpleHelper("^1 %", "[^1 % one]", Pseudolocalizer::Method::kAccent));
73  EXPECT_TRUE(
74      CompoundHelper("", "", "", "[]", Pseudolocalizer::Method::kAccent));
75  EXPECT_TRUE(CompoundHelper("Hello,", " world", "", "[Ĥéļļö, ŵöŕļð one two]",
76                             Pseudolocalizer::Method::kAccent));
77}
78
79TEST(PseudolocalizerTest, PlaintextBidi) {
80  EXPECT_TRUE(SimpleHelper("", "", Pseudolocalizer::Method::kBidi));
81  EXPECT_TRUE(SimpleHelper(
82      "word", "\xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f",
83      Pseudolocalizer::Method::kBidi));
84  EXPECT_TRUE(SimpleHelper(
85      "  word  ", "  \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f  ",
86      Pseudolocalizer::Method::kBidi));
87  EXPECT_TRUE(SimpleHelper(
88      "  word  ", "  \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f  ",
89      Pseudolocalizer::Method::kBidi));
90  EXPECT_TRUE(
91      SimpleHelper("hello\n  world\n",
92                   "\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));
95  EXPECT_TRUE(CompoundHelper(
96      "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));
100}
101
102TEST(PseudolocalizerTest, SimpleICU) {
103  // Single-fragment messages
104  EXPECT_TRUE(SimpleHelper("{placeholder}", "[»{placeholder}«]",
105                           Pseudolocalizer::Method::kAccent));
106  EXPECT_TRUE(SimpleHelper("{USER} is offline", "[»{USER}« îš öƒƒļîñé one two]",
107                           Pseudolocalizer::Method::kAccent));
108  EXPECT_TRUE(SimpleHelper("Copy from {path1} to {path2}",
109                           "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
110                           Pseudolocalizer::Method::kAccent));
111  EXPECT_TRUE(SimpleHelper("Today is {1,date} {1,time}",
112                           "[Ţöðåý îš »{1,date}« »{1,time}« one two]",
113                           Pseudolocalizer::Method::kAccent));
114
115  // Multi-fragment messages
116  EXPECT_TRUE(CompoundHelper("{USER}", " ", "is offline",
117                             "[»{USER}« îš öƒƒļîñé one two]",
118                             Pseudolocalizer::Method::kAccent));
119  EXPECT_TRUE(CompoundHelper("Copy from ", "{path1}", " to {path2}",
120                             "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
121                             Pseudolocalizer::Method::kAccent));
122}
123
124TEST(PseudolocalizerTest, ICUBidi) {
125  // Single-fragment messages
126  EXPECT_TRUE(SimpleHelper(
127      "{placeholder}",
128      "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f",
129      Pseudolocalizer::Method::kBidi));
130  EXPECT_TRUE(SimpleHelper(
131      "{COUNT, plural, one {one} other {other}}",
132      "{COUNT, plural, "
133      "one {\xe2\x80\x8f\xE2\x80\xaeone\xE2\x80\xac\xe2\x80\x8f} "
134      "other {\xe2\x80\x8f\xE2\x80\xaeother\xE2\x80\xac\xe2\x80\x8f}}",
135      Pseudolocalizer::Method::kBidi));
136}
137
138TEST(PseudolocalizerTest, Escaping) {
139  // Single-fragment messages
140  EXPECT_TRUE(SimpleHelper("'{USER'} is offline",
141                           "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]",
142                           Pseudolocalizer::Method::kAccent));
143
144  // Multi-fragment messages
145  EXPECT_TRUE(CompoundHelper("'{USER}", " ", "''is offline",
146                             "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]",
147                             Pseudolocalizer::Method::kAccent));
148}
149
150TEST(PseudolocalizerTest, PluralsAndSelects) {
151  EXPECT_TRUE(SimpleHelper(
152      "{COUNT, plural, one {Delete a file} other {Delete {COUNT} files}}",
153      "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
154      "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
155      Pseudolocalizer::Method::kAccent));
156
157  EXPECT_TRUE(
158      SimpleHelper("Distance is {COUNT, plural, one {# mile} other {# miles}}",
159                   "[Ðîšţåñçé îš {COUNT, plural, one {# ḿîļé one two} "
160                   "other {# ḿîļéš one two}}]",
161                   Pseudolocalizer::Method::kAccent));
162
163  EXPECT_TRUE(SimpleHelper(
164      "{1, select, female {{1} added you} "
165      "male {{1} added you} other {{1} added you}}",
166      "[{1, select, female {»{1}« åððéð ýöû one two} "
167      "male {»{1}« åððéð ýöû one two} other {»{1}« åððéð ýöû one two}}]",
168      Pseudolocalizer::Method::kAccent));
169
170  EXPECT_TRUE(
171      CompoundHelper("{COUNT, plural, one {Delete a file} "
172                     "other {Delete ",
173                     "{COUNT}", " files}}",
174                     "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
175                     "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
176                     Pseudolocalizer::Method::kAccent));
177}
178
179TEST(PseudolocalizerTest, NestedICU) {
180  EXPECT_TRUE(
181      SimpleHelper("{person, select, "
182                   "female {"
183                   "{num_circles, plural,"
184                   "=0{{person} didn't add you to any of her circles.}"
185                   "=1{{person} added you to one of her circles.}"
186                   "other{{person} added you to her # circles.}}}"
187                   "male {"
188                   "{num_circles, plural,"
189                   "=0{{person} didn't add you to any of his circles.}"
190                   "=1{{person} added you to one of his circles.}"
191                   "other{{person} added you to his # circles.}}}"
192                   "other {"
193                   "{num_circles, plural,"
194                   "=0{{person} didn't add you to any of their circles.}"
195                   "=1{{person} added you to one of their circles.}"
196                   "other{{person} added you to their # circles.}}}}",
197                   "[{person, select, "
198                   "female {"
199                   "{num_circles, plural,"
200                   "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥéŕ çîŕçļéš."
201                   " one two three four five}"
202                   "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥéŕ çîŕçļéš."
203                   " one two three four}"
204                   "other{»{person}« åððéð ýöû ţö ĥéŕ # çîŕçļéš."
205                   " one two three four}}}"
206                   "male {"
207                   "{num_circles, plural,"
208                   "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥîš çîŕçļéš."
209                   " one two three four five}"
210                   "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥîš çîŕçļéš."
211                   " one two three four}"
212                   "other{»{person}« åððéð ýöû ţö ĥîš # çîŕçļéš."
213                   " one two three four}}}"
214                   "other {{num_circles, plural,"
215                   "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ţĥéîŕ çîŕçļéš."
216                   " one two three four five}"
217                   "=1{»{person}« åððéð ýöû ţö öñé öƒ ţĥéîŕ çîŕçļéš."
218                   " one two three four}"
219                   "other{»{person}« åððéð ýöû ţö ţĥéîŕ # çîŕçļéš."
220                   " one two three four}}}}]",
221                   Pseudolocalizer::Method::kAccent));
222}
223
224TEST(PseudolocalizerTest, RedefineMethod) {
225  Pseudolocalizer pseudo(Pseudolocalizer::Method::kAccent);
226  std::string result = pseudo.Text("Hello, ");
227  pseudo.SetMethod(Pseudolocalizer::Method::kNone);
228  result += pseudo.Text("world!");
229  ASSERT_EQ(StringPiece("Ĥéļļö, world!"), result);
230}
231
232}  // namespace aapt
233