PseudolocaleGenerator_test.cpp revision 58a20a6482a56a262fd83a617482641e3a981db1
1/* 2 * Copyright (C) 2016 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/PseudolocaleGenerator.h" 18#include "test/Test.h" 19#include "util/Util.h" 20 21#include <androidfw/ResourceTypes.h> 22 23namespace aapt { 24 25TEST(PseudolocaleGeneratorTest, PseudolocalizeStyledString) { 26 StringPool pool; 27 StyleString originalStyle; 28 originalStyle.str = "Hello world!"; 29 originalStyle.spans = { Span{ "b", 2, 3 }, Span{ "b", 6, 7 }, Span{ "i", 1, 10 } }; 30 31 std::unique_ptr<StyledString> newString = pseudolocalizeStyledString( 32 util::make_unique<StyledString>(pool.makeRef(originalStyle)).get(), 33 Pseudolocalizer::Method::kNone, &pool); 34 35 EXPECT_EQ(originalStyle.str, *newString->value->str); 36 ASSERT_EQ(originalStyle.spans.size(), newString->value->spans.size()); 37 38 EXPECT_EQ(std::string("He").size(), newString->value->spans[0].firstChar); 39 EXPECT_EQ(std::string("Hel").size(), newString->value->spans[0].lastChar); 40 EXPECT_EQ(std::string("b"), *newString->value->spans[0].name); 41 42 EXPECT_EQ(std::string("Hello ").size(), newString->value->spans[1].firstChar); 43 EXPECT_EQ(std::string("Hello w").size(), newString->value->spans[1].lastChar); 44 EXPECT_EQ(std::string("b"), *newString->value->spans[1].name); 45 46 EXPECT_EQ(std::string("H").size(), newString->value->spans[2].firstChar); 47 EXPECT_EQ(std::string("Hello worl").size(), newString->value->spans[2].lastChar); 48 EXPECT_EQ(std::string("i"), *newString->value->spans[2].name); 49 50 originalStyle.spans.push_back(Span{ "em", 0, 11u }); 51 52 newString = pseudolocalizeStyledString( 53 util::make_unique<StyledString>(pool.makeRef(originalStyle)).get(), 54 Pseudolocalizer::Method::kAccent, &pool); 55 56 EXPECT_EQ(std::string("[Ĥéļļö ŵöŕļð¡ one two]"), *newString->value->str); 57 ASSERT_EQ(originalStyle.spans.size(), newString->value->spans.size()); 58 59 EXPECT_EQ(std::string("[Ĥé").size(), newString->value->spans[0].firstChar); 60 EXPECT_EQ(std::string("[Ĥéļ").size(), newString->value->spans[0].lastChar); 61 62 EXPECT_EQ(std::string("[Ĥéļļö ").size(), newString->value->spans[1].firstChar); 63 EXPECT_EQ(std::string("[Ĥéļļö ŵ").size(), newString->value->spans[1].lastChar); 64 65 EXPECT_EQ(std::string("[Ĥ").size(), newString->value->spans[2].firstChar); 66 EXPECT_EQ(std::string("[Ĥéļļö ŵöŕļ").size(), newString->value->spans[2].lastChar); 67 68 EXPECT_EQ(std::string("[").size(), newString->value->spans[3].firstChar); 69 EXPECT_EQ(std::string("[Ĥéļļö ŵöŕļð").size(), newString->value->spans[3].lastChar); 70} 71 72TEST(PseudolocaleGeneratorTest, PseudolocalizeOnlyDefaultConfigs) { 73 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() 74 .addString("android:string/one", "one") 75 .addString("android:string/two", ResourceId{}, test::parseConfigOrDie("en"), "two") 76 .addString("android:string/three", "three") 77 .addString("android:string/three", ResourceId{}, test::parseConfigOrDie("en-rXA"), 78 "three") 79 .addString("android:string/four", "four") 80 .build(); 81 82 String* val = test::getValue<String>(table.get(), "android:string/four"); 83 ASSERT_NE(nullptr, val); 84 val->setTranslateable(false); 85 86 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); 87 PseudolocaleGenerator generator; 88 ASSERT_TRUE(generator.consume(context.get(), table.get())); 89 90 // Normal pseudolocalization should take place. 91 ASSERT_NE(nullptr, test::getValueForConfig<String>(table.get(), "android:string/one", 92 test::parseConfigOrDie("en-rXA"))); 93 ASSERT_NE(nullptr, test::getValueForConfig<String>(table.get(), "android:string/one", 94 test::parseConfigOrDie("ar-rXB"))); 95 96 // No default config for android:string/two, so no pseudlocales should exist. 97 ASSERT_EQ(nullptr, test::getValueForConfig<String>(table.get(), "android:string/two", 98 test::parseConfigOrDie("en-rXA"))); 99 ASSERT_EQ(nullptr, test::getValueForConfig<String>(table.get(), "android:string/two", 100 test::parseConfigOrDie("ar-rXB"))); 101 102 103 // Check that we didn't override manual pseudolocalization. 104 val = test::getValueForConfig<String>(table.get(), "android:string/three", 105 test::parseConfigOrDie("en-rXA")); 106 ASSERT_NE(nullptr, val); 107 EXPECT_EQ(std::string("three"), *val->value); 108 109 ASSERT_NE(nullptr, test::getValueForConfig<String>(table.get(), "android:string/three", 110 test::parseConfigOrDie("ar-rXB"))); 111 112 // Check that four's translateable marker was honored. 113 ASSERT_EQ(nullptr, test::getValueForConfig<String>(table.get(), "android:string/four", 114 test::parseConfigOrDie("en-rXA"))); 115 ASSERT_EQ(nullptr, test::getValueForConfig<String>(table.get(), "android:string/four", 116 test::parseConfigOrDie("ar-rXB"))); 117 118} 119 120} // namespace aapt 121 122