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