Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 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 | |
| 23 | namespace aapt { |
| 24 | |
| 25 | // In this context, 'Axis' represents a particular field in the configuration, |
| 26 | // such as language or density. |
| 27 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 28 | static ::testing::AssertionResult simpleHelper(const char* input, |
| 29 | const char* expected, |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 30 | Pseudolocalizer::Method method) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 31 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | static ::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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(PseudolocalizerTest, NoPseudolocalization) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 52 | EXPECT_TRUE(simpleHelper("", "", Pseudolocalizer::Method::kNone)); |
| 53 | EXPECT_TRUE(simpleHelper("Hello, world", "Hello, world", |
| 54 | Pseudolocalizer::Method::kNone)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 56 | EXPECT_TRUE(compoundHelper("Hello,", " world", "", "Hello, world", |
| 57 | Pseudolocalizer::Method::kNone)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(PseudolocalizerTest, PlaintextAccent) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 61 | EXPECT_TRUE(simpleHelper("", "[]", Pseudolocalizer::Method::kAccent)); |
| 62 | EXPECT_TRUE(simpleHelper("Hello, world", "[Ĥéļļö, ŵöŕļð one two]", |
| 63 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 65 | EXPECT_TRUE(simpleHelper("Hello, %1d", "[Ĥéļļö, »%1d« one two]", |
| 66 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 67 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 68 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST(PseudolocalizerTest, PlaintextBidi) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 79 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST(PseudolocalizerTest, SimpleICU) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 102 | // 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 117 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 118 | EXPECT_TRUE(compoundHelper("Copy from ", "{path1}", " to {path2}", |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 119 | "[Çöþý ƒŕöḿ »{path1}« ţö »{path2}« one two three]", |
| 120 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST(PseudolocalizerTest, ICUBidi) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 124 | // 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST(PseudolocalizerTest, Escaping) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 138 | // Single-fragment messages |
| 139 | EXPECT_TRUE(simpleHelper("'{USER'} is offline", |
| 140 | "['{ÛŠÉŔ'} îš öƒƒļîñé one two three]", |
| 141 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 142 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 143 | // Multi-fragment messages |
| 144 | EXPECT_TRUE(compoundHelper("'{USER}", " ", "''is offline", |
| 145 | "['{ÛŠÉŔ} ''îš öƒƒļîñé one two three]", |
| 146 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST(PseudolocalizerTest, PluralsAndSelects) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 150 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 174 | "other {Ðéļéţé »{COUNT}« ƒîļéš one two}}]", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 175 | Pseudolocalizer::Method::kAccent)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST(PseudolocalizerTest, NestedICU) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 179 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | TEST(PseudolocalizerTest, RedefineMethod) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 224 | 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 Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 231 | } // namespace aapt |