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 24 using android::String8; 25 26 // In this context, 'Axis' represents a particular field in the configuration, 27 // such as language or density. 28 29 static 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 36 static 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 47 TEST(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 55 TEST(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 71 TEST(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 simple_helper("hello\\nworld\\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 PSEUDO_BIDI); 94 } 95 96 TEST(Pseudolocales, SimpleICU) { 97 // Single-fragment messages 98 simple_helper("{placeholder}", "[{placeholder}]", PSEUDO_ACCENTED); 99 simple_helper("{USER} is offline", 100 "[{USER} one two]", PSEUDO_ACCENTED); 101 simple_helper("Copy from {path1} to {path2}", 102 "[ {path1} {path2} one two three]", PSEUDO_ACCENTED); 103 simple_helper("Today is {1,date} {1,time}", 104 "[ {1,date} {1,time} one two]", PSEUDO_ACCENTED); 105 106 // Multi-fragment messages 107 compound_helper("{USER}", " ", "is offline", 108 "[{USER} one two]", 109 PSEUDO_ACCENTED); 110 compound_helper("Copy from ", "{path1}", " to {path2}", 111 "[ {path1} {path2} one two three]", 112 PSEUDO_ACCENTED); 113 } 114 115 TEST(Pseudolocales, ICUBidi) { 116 // Single-fragment messages 117 simple_helper("{placeholder}", 118 "\xe2\x80\x8f\xE2\x80\xae{placeholder}\xE2\x80\xac\xe2\x80\x8f", 119 PSEUDO_BIDI); 120 simple_helper( 121 "{COUNT, plural, one {one} other {other}}", 122 "{COUNT, plural, " \ 123 "one {\xe2\x80\x8f\xE2\x80\xaeone\xE2\x80\xac\xe2\x80\x8f} " \ 124 "other {\xe2\x80\x8f\xE2\x80\xaeother\xE2\x80\xac\xe2\x80\x8f}}", 125 PSEUDO_BIDI 126 ); 127 } 128 129 TEST(Pseudolocales, Escaping) { 130 // Single-fragment messages 131 simple_helper("'{USER'} is offline", 132 "['{'} one two three]", PSEUDO_ACCENTED); 133 134 // Multi-fragment messages 135 compound_helper("'{USER}", " ", "''is offline", 136 "['{} '' one two three]", PSEUDO_ACCENTED); 137 } 138 139 TEST(Pseudolocales, PluralsAndSelects) { 140 simple_helper( 141 "{COUNT, plural, one {Delete a file} other {Delete {COUNT} files}}", 142 "[{COUNT, plural, one { one two} " \ 143 "other { {COUNT} one two}}]", 144 PSEUDO_ACCENTED 145 ); 146 simple_helper( 147 "Distance is {COUNT, plural, one {# mile} other {# miles}}", 148 "[ {COUNT, plural, one {# one two} " \ 149 "other {# one two}}]", 150 PSEUDO_ACCENTED 151 ); 152 simple_helper( 153 "{1, select, female {{1} added you} " \ 154 "male {{1} added you} other {{1} added you}}", 155 "[{1, select, female {{1} one two} " \ 156 "male {{1} one two} other {{1} one two}}]", 157 PSEUDO_ACCENTED 158 ); 159 160 compound_helper( 161 "{COUNT, plural, one {Delete a file} " \ 162 "other {Delete ", "{COUNT}", " files}}", 163 "[{COUNT, plural, one { one two} " \ 164 "other { {COUNT} one two}}]", 165 PSEUDO_ACCENTED 166 ); 167 } 168 169 TEST(Pseudolocales, NestedICU) { 170 simple_helper( 171 "{person, select, " \ 172 "female {" \ 173 "{num_circles, plural," \ 174 "=0{{person} didn't add you to any of her circles.}" \ 175 "=1{{person} added you to one of her circles.}" \ 176 "other{{person} added you to her # circles.}}}" \ 177 "male {" \ 178 "{num_circles, plural," \ 179 "=0{{person} didn't add you to any of his circles.}" \ 180 "=1{{person} added you to one of his circles.}" \ 181 "other{{person} added you to his # circles.}}}" \ 182 "other {" \ 183 "{num_circles, plural," \ 184 "=0{{person} didn't add you to any of their circles.}" \ 185 "=1{{person} added you to one of their circles.}" \ 186 "other{{person} added you to their # circles.}}}}", 187 "[{person, select, " \ 188 "female {" \ 189 "{num_circles, plural," \ 190 "=0{{person} ' ." \ 191 " one two three four five}" \ 192 "=1{{person} ." \ 193 " one two three four}" \ 194 "other{{person} # ." \ 195 " one two three four}}}" \ 196 "male {" \ 197 "{num_circles, plural," \ 198 "=0{{person} ' ." \ 199 " one two three four five}" \ 200 "=1{{person} ." \ 201 " one two three four}" \ 202 "other{{person} # ." \ 203 " one two three four}}}" \ 204 "other {{num_circles, plural," \ 205 "=0{{person} ' ." \ 206 " one two three four five}" \ 207 "=1{{person} ." \ 208 " one two three four}" \ 209 "other{{person} # ." \ 210 " one two three four}}}}]", 211 PSEUDO_ACCENTED 212 ); 213 } 214 215 TEST(Pseudolocales, RedefineMethod) { 216 Pseudolocalizer pseudo(PSEUDO_ACCENTED); 217 String16 result = pseudo.text(String16(String8("Hello, "))); 218 pseudo.setMethod(NO_PSEUDOLOCALIZATION); 219 result.append(pseudo.text(String16(String8("world!")))); 220 ASSERT_EQ(String8(", world!"), String8(result)); 221 } 222