blob: a152ed6d00cf673d9a4de5190b2d3c218c707b85 [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"
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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028static ::testing::AssertionResult simpleHelper(const char* input,
29 const char* expected,
Adam Lesinski393b5f02015-12-17 13:03:11 -080030 Pseudolocalizer::Method method) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 Pseudolocalizer pseudo(method);
32 std::string result = pseudo.start() + pseudo.text(input) + pseudo.end();
33 if (result != expected) {
34 return ::testing::AssertionFailure() << expected << " != " << result;
35 }
36 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080037}
38
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039static ::testing::AssertionResult compoundHelper(
40 const char* in1, const char* in2, const char* in3, const char* expected,
41 Pseudolocalizer::Method method) {
42 Pseudolocalizer pseudo(method);
43 std::string result = pseudo.start() + pseudo.text(in1) + pseudo.text(in2) +
44 pseudo.text(in3) + pseudo.end();
45 if (result != expected) {
46 return ::testing::AssertionFailure() << expected << " != " << result;
47 }
48 return ::testing::AssertionSuccess();
Adam Lesinski393b5f02015-12-17 13:03:11 -080049}
50
51TEST(PseudolocalizerTest, NoPseudolocalization) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 EXPECT_TRUE(simpleHelper("", "", Pseudolocalizer::Method::kNone));
53 EXPECT_TRUE(simpleHelper("Hello, world", "Hello, world",
54 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 EXPECT_TRUE(compoundHelper("Hello,", " world", "", "Hello, world",
57 Pseudolocalizer::Method::kNone));
Adam Lesinski393b5f02015-12-17 13:03:11 -080058}
59
60TEST(PseudolocalizerTest, PlaintextAccent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 EXPECT_TRUE(simpleHelper("", "[]", Pseudolocalizer::Method::kAccent));
62 EXPECT_TRUE(simpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]",
63 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 EXPECT_TRUE(simpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]",
66 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 EXPECT_TRUE(simpleHelper("Battery %1d%%", "[βåţţéŕý »%1d«%% one two]",
69 Pseudolocalizer::Method::kAccent));
70 EXPECT_TRUE(
71 simpleHelper("^1 %", "[^1 % one]", Pseudolocalizer::Method::kAccent));
72 EXPECT_TRUE(
73 compoundHelper("", "", "", "[]", Pseudolocalizer::Method::kAccent));
74 EXPECT_TRUE(compoundHelper("Hello,", " world", "", "[Ĥéļļö, ŵöŕļð one two]",
75 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -080076}
77
78TEST(PseudolocalizerTest, PlaintextBidi) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 EXPECT_TRUE(simpleHelper("", "", Pseudolocalizer::Method::kBidi));
80 EXPECT_TRUE(simpleHelper(
81 "word", "\xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f",
82 Pseudolocalizer::Method::kBidi));
83 EXPECT_TRUE(simpleHelper(
84 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
85 Pseudolocalizer::Method::kBidi));
86 EXPECT_TRUE(simpleHelper(
87 " word ", " \xe2\x80\x8f\xE2\x80\xaeword\xE2\x80\xac\xe2\x80\x8f ",
88 Pseudolocalizer::Method::kBidi));
89 EXPECT_TRUE(
90 simpleHelper("hello\n world\n",
91 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n"
92 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n",
93 Pseudolocalizer::Method::kBidi));
94 EXPECT_TRUE(compoundHelper(
95 "hello", "\n ", " world\n",
96 "\xe2\x80\x8f\xE2\x80\xaehello\xE2\x80\xac\xe2\x80\x8f\n"
97 " \xe2\x80\x8f\xE2\x80\xaeworld\xE2\x80\xac\xe2\x80\x8f\n",
98 Pseudolocalizer::Method::kBidi));
Adam Lesinski393b5f02015-12-17 13:03:11 -080099}
100
101TEST(PseudolocalizerTest, SimpleICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // Single-fragment messages
103 EXPECT_TRUE(simpleHelper("{placeholder}", "[»{placeholder}«]",
104 Pseudolocalizer::Method::kAccent));
105 EXPECT_TRUE(simpleHelper("{USER} is offline", "[»{USER}« îš öƒƒļîñé one two]",
106 Pseudolocalizer::Method::kAccent));
107 EXPECT_TRUE(simpleHelper("Copy from {path1} to {path2}",
108 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
109 Pseudolocalizer::Method::kAccent));
110 EXPECT_TRUE(simpleHelper("Today is {1,date} {1,time}",
111 "[Ţöðåý îš »{1,date}« »{1,time}« one two]",
112 Pseudolocalizer::Method::kAccent));
113
114 // Multi-fragment messages
115 EXPECT_TRUE(compoundHelper("{USER}", " ", "is offline",
116 "[»{USER}« îš öƒƒļîñé one two]",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800117 Pseudolocalizer::Method::kAccent));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 EXPECT_TRUE(compoundHelper("Copy from ", "{path1}", " to {path2}",
Adam Lesinski393b5f02015-12-17 13:03:11 -0800119 "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]",
120 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800121}
122
123TEST(PseudolocalizerTest, ICUBidi) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 // Single-fragment messages
125 EXPECT_TRUE(simpleHelper(
126 "{placeholder}",
127 "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f",
128 Pseudolocalizer::Method::kBidi));
129 EXPECT_TRUE(simpleHelper(
130 "{COUNT, plural, one {one} other {other}}",
131 "{COUNT, plural, "
132 "one {\xe2\x80\x8f\xE2\x80\xaeone\xE2\x80\xac\xe2\x80\x8f} "
133 "other {\xe2\x80\x8f\xE2\x80\xaeother\xE2\x80\xac\xe2\x80\x8f}}",
134 Pseudolocalizer::Method::kBidi));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800135}
136
137TEST(PseudolocalizerTest, Escaping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 // Single-fragment messages
139 EXPECT_TRUE(simpleHelper("'{USER'} is offline",
140 "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]",
141 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 // Multi-fragment messages
144 EXPECT_TRUE(compoundHelper("'{USER}", " ", "''is offline",
145 "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]",
146 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800147}
148
149TEST(PseudolocalizerTest, PluralsAndSelects) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 EXPECT_TRUE(simpleHelper(
151 "{COUNT, plural, one {Delete a file} other {Delete {COUNT} files}}",
152 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
153 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
154 Pseudolocalizer::Method::kAccent));
155
156 EXPECT_TRUE(
157 simpleHelper("Distance is {COUNT, plural, one {# mile} other {# miles}}",
158 "[Ðîšţåñçé îš {COUNT, plural, one {# ḿîļé one two} "
159 "other {# ḿîļéš one two}}]",
160 Pseudolocalizer::Method::kAccent));
161
162 EXPECT_TRUE(simpleHelper(
163 "{1, select, female {{1} added you} "
164 "male {{1} added you} other {{1} added you}}",
165 "[{1, select, female {»{1}« åððéð ýöû one two} "
166 "male {»{1}« åððéð ýöû one two} other {»{1}« åððéð ýöû one two}}]",
167 Pseudolocalizer::Method::kAccent));
168
169 EXPECT_TRUE(
170 compoundHelper("{COUNT, plural, one {Delete a file} "
171 "other {Delete ",
172 "{COUNT}", " files}}",
173 "[{COUNT, plural, one {Ðéļéţé å ƒîļé one two} "
Adam Lesinski393b5f02015-12-17 13:03:11 -0800174 "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800176}
177
178TEST(PseudolocalizerTest, NestedICU) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 EXPECT_TRUE(
180 simpleHelper("{person, select, "
181 "female {"
182 "{num_circles, plural,"
183 "=0{{person} didn't add you to any of her circles.}"
184 "=1{{person} added you to one of her circles.}"
185 "other{{person} added you to her # circles.}}}"
186 "male {"
187 "{num_circles, plural,"
188 "=0{{person} didn't add you to any of his circles.}"
189 "=1{{person} added you to one of his circles.}"
190 "other{{person} added you to his # circles.}}}"
191 "other {"
192 "{num_circles, plural,"
193 "=0{{person} didn't add you to any of their circles.}"
194 "=1{{person} added you to one of their circles.}"
195 "other{{person} added you to their # circles.}}}}",
196 "[{person, select, "
197 "female {"
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 "male {"
206 "{num_circles, plural,"
207 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ĥîš çîŕçļéš."
208 " one two three four five}"
209 "=1{»{person}« åððéð ýöû ţö öñé öƒ ĥîš çîŕçļéš."
210 " one two three four}"
211 "other{»{person}« åððéð ýöû ţö ĥîš # çîŕçļéš."
212 " one two three four}}}"
213 "other {{num_circles, plural,"
214 "=0{»{person}« ðîðñ'ţ åðð ýöû ţö åñý öƒ ţĥéîŕ çîŕçļéš."
215 " one two three four five}"
216 "=1{»{person}« åððéð ýöû ţö öñé öƒ ţĥéîŕ çîŕçļéš."
217 " one two three four}"
218 "other{»{person}« åððéð ýöû ţö ţĥéîŕ # çîŕçļéš."
219 " one two three four}}}}]",
220 Pseudolocalizer::Method::kAccent));
Adam Lesinski393b5f02015-12-17 13:03:11 -0800221}
222
223TEST(PseudolocalizerTest, RedefineMethod) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 Pseudolocalizer pseudo(Pseudolocalizer::Method::kAccent);
225 std::string result = pseudo.text("Hello, ");
226 pseudo.setMethod(Pseudolocalizer::Method::kNone);
227 result += pseudo.text("world!");
228 ASSERT_EQ(StringPiece("Ĥéļļö, world!"), result);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800229}
230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231} // namespace aapt