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