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