FontFamilyTest.cpp 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <minikin/FontFamily.h>
20 21 22

#include <cutils/log.h>

S
Seigo Nonaka 已提交
23
#include "FontLanguageListCache.h"
24
#include "ICUTestBase.h"
25 26 27 28 29
#include "MinikinFontForTest.h"
#include "MinikinInternal.h"

namespace android {

30 31 32 33
typedef ICUTestBase FontLanguagesTest;
typedef ICUTestBase FontLanguageTest;

static FontLanguages createFontLanguages(const std::string& input) {
34
    AutoMutex _l(gMinikinLock);
35 36 37 38 39
    uint32_t langId = FontLanguageListCache::getId(input);
    return FontLanguageListCache::getById(langId);
}

static FontLanguage createFontLanguage(const std::string& input) {
40
    AutoMutex _l(gMinikinLock);
41 42 43 44 45 46 47 48 49 50
    uint32_t langId = FontLanguageListCache::getId(input);
    return FontLanguageListCache::getById(langId)[0];
}

TEST_F(FontLanguageTest, basicTests) {
    FontLanguage defaultLang;
    FontLanguage emptyLang("", 0);
    FontLanguage english = createFontLanguage("en");
    FontLanguage french = createFontLanguage("fr");
    FontLanguage und = createFontLanguage("und");
S
Seigo Nonaka 已提交
51
    FontLanguage undZsye = createFontLanguage("und-Zsye");
52 53 54 55 56 57 58 59 60 61 62 63

    EXPECT_EQ(english, english);
    EXPECT_EQ(french, french);

    EXPECT_TRUE(defaultLang != defaultLang);
    EXPECT_TRUE(emptyLang != emptyLang);
    EXPECT_TRUE(defaultLang != emptyLang);
    EXPECT_TRUE(defaultLang != und);
    EXPECT_TRUE(emptyLang != und);
    EXPECT_TRUE(english != defaultLang);
    EXPECT_TRUE(english != emptyLang);
    EXPECT_TRUE(english != french);
S
Seigo Nonaka 已提交
64 65
    EXPECT_TRUE(english != undZsye);
    EXPECT_TRUE(und != undZsye);
66 67 68 69 70 71 72 73
    EXPECT_TRUE(english != und);

    EXPECT_TRUE(defaultLang.isUnsupported());
    EXPECT_TRUE(emptyLang.isUnsupported());

    EXPECT_FALSE(english.isUnsupported());
    EXPECT_FALSE(french.isUnsupported());
    EXPECT_FALSE(und.isUnsupported());
S
Seigo Nonaka 已提交
74
    EXPECT_FALSE(undZsye.isUnsupported());
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

TEST_F(FontLanguageTest, getStringTest) {
    EXPECT_EQ("en-Latn", createFontLanguage("en").getString());
    EXPECT_EQ("en-Latn", createFontLanguage("en-Latn").getString());

    // Capitalized language code or lowercased script should be normalized.
    EXPECT_EQ("en-Latn", createFontLanguage("EN-LATN").getString());
    EXPECT_EQ("en-Latn", createFontLanguage("EN-latn").getString());
    EXPECT_EQ("en-Latn", createFontLanguage("en-latn").getString());

    // Invalid script should be kept.
    EXPECT_EQ("en-Xyzt", createFontLanguage("en-xyzt").getString());

    EXPECT_EQ("en-Latn", createFontLanguage("en-Latn-US").getString());
    EXPECT_EQ("ja-Jpan", createFontLanguage("ja").getString());
    EXPECT_EQ("und", createFontLanguage("und").getString());
    EXPECT_EQ("und", createFontLanguage("UND").getString());
    EXPECT_EQ("und", createFontLanguage("Und").getString());
S
Seigo Nonaka 已提交
94 95 96
    EXPECT_EQ("und-Zsye", createFontLanguage("und-Zsye").getString());
    EXPECT_EQ("und-Zsye", createFontLanguage("Und-ZSYE").getString());
    EXPECT_EQ("und-Zsye", createFontLanguage("Und-zsye").getString());
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    EXPECT_EQ("de-Latn", createFontLanguage("de-1901").getString());

    // This is not a necessary desired behavior, just known behavior.
    EXPECT_EQ("en-Latn", createFontLanguage("und-Abcdefgh").getString());
}

TEST_F(FontLanguageTest, ScriptEqualTest) {
    EXPECT_TRUE(createFontLanguage("en").isEqualScript(createFontLanguage("en")));
    EXPECT_TRUE(createFontLanguage("en-Latn").isEqualScript(createFontLanguage("en")));
    EXPECT_TRUE(createFontLanguage("jp-Latn").isEqualScript(createFontLanguage("en-Latn")));
    EXPECT_TRUE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Jpan")));

    EXPECT_FALSE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Hira")));
    EXPECT_FALSE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Hani")));
}

TEST_F(FontLanguageTest, ScriptMatchTest) {
    const bool SUPPORTED = true;
    const bool NOT_SUPPORTED = false;

    struct TestCase {
        const std::string baseScript;
        const std::string requestedScript;
        bool isSupported;
    } testCases[] = {
        // Same scripts
        { "en-Latn", "Latn", SUPPORTED },
        { "ja-Jpan", "Jpan", SUPPORTED },
        { "ja-Hira", "Hira", SUPPORTED },
        { "ja-Kana", "Kana", SUPPORTED },
        { "ja-Hrkt", "Hrkt", SUPPORTED },
        { "zh-Hans", "Hans", SUPPORTED },
        { "zh-Hant", "Hant", SUPPORTED },
        { "zh-Hani", "Hani", SUPPORTED },
        { "ko-Kore", "Kore", SUPPORTED },
        { "ko-Hang", "Hang", SUPPORTED },

        // Japanese supports Hiragana, Katakanara, etc.
        { "ja-Jpan", "Hira", SUPPORTED },
        { "ja-Jpan", "Kana", SUPPORTED },
        { "ja-Jpan", "Hrkt", SUPPORTED },
        { "ja-Hrkt", "Hira", SUPPORTED },
        { "ja-Hrkt", "Kana", SUPPORTED },

        // Chinese supports Han.
        { "zh-Hans", "Hani", SUPPORTED },
        { "zh-Hant", "Hani", SUPPORTED },

        // Korean supports Hangul.
        { "ko-Kore", "Hang", SUPPORTED },

        // Different scripts
        { "ja-Jpan", "Latn", NOT_SUPPORTED },
        { "en-Latn", "Jpan", NOT_SUPPORTED },
        { "ja-Jpan", "Hant", NOT_SUPPORTED },
        { "zh-Hant", "Jpan", NOT_SUPPORTED },
        { "ja-Jpan", "Hans", NOT_SUPPORTED },
        { "zh-Hans", "Jpan", NOT_SUPPORTED },
        { "ja-Jpan", "Kore", NOT_SUPPORTED },
        { "ko-Kore", "Jpan", NOT_SUPPORTED },
        { "zh-Hans", "Hant", NOT_SUPPORTED },
        { "zh-Hant", "Hans", NOT_SUPPORTED },
        { "zh-Hans", "Kore", NOT_SUPPORTED },
        { "ko-Kore", "Hans", NOT_SUPPORTED },
        { "zh-Hant", "Kore", NOT_SUPPORTED },
        { "ko-Kore", "Hant", NOT_SUPPORTED },

        // Hiragana doesn't support Japanese, etc.
        { "ja-Hira", "Jpan", NOT_SUPPORTED },
        { "ja-Kana", "Jpan", NOT_SUPPORTED },
        { "ja-Hrkt", "Jpan", NOT_SUPPORTED },
        { "ja-Hani", "Jpan", NOT_SUPPORTED },
        { "ja-Hira", "Hrkt", NOT_SUPPORTED },
        { "ja-Kana", "Hrkt", NOT_SUPPORTED },
        { "ja-Hani", "Hrkt", NOT_SUPPORTED },
        { "ja-Hani", "Hira", NOT_SUPPORTED },
        { "ja-Hani", "Kana", NOT_SUPPORTED },

        // Kanji doesn't support Chinese, etc.
        { "zh-Hani", "Hant", NOT_SUPPORTED },
        { "zh-Hani", "Hans", NOT_SUPPORTED },

        // Hangul doesn't support Korean, etc.
        { "ko-Hang", "Kore", NOT_SUPPORTED },
        { "ko-Hani", "Kore", NOT_SUPPORTED },
        { "ko-Hani", "Hang", NOT_SUPPORTED },
        { "ko-Hang", "Hani", NOT_SUPPORTED },
    };

    for (auto testCase : testCases) {
        hb_script_t script = hb_script_from_iso15924_tag(
                HB_TAG(testCase.requestedScript[0], testCase.requestedScript[1],
                       testCase.requestedScript[2], testCase.requestedScript[3]));
        if (testCase.isSupported) {
            EXPECT_TRUE(
                    createFontLanguage(testCase.baseScript).supportsHbScript(script))
                    << testCase.baseScript << " should support " << testCase.requestedScript;
        } else {
            EXPECT_FALSE(
                    createFontLanguage(testCase.baseScript).supportsHbScript(script))
                    << testCase.baseScript << " shouldn't support " << testCase.requestedScript;
        }
    }
}

TEST_F(FontLanguagesTest, basicTests) {
204 205 206
    FontLanguages emptyLangs;
    EXPECT_EQ(0u, emptyLangs.size());

207 208
    FontLanguage english = createFontLanguage("en");
    FontLanguages singletonLangs = createFontLanguages("en");
209 210 211
    EXPECT_EQ(1u, singletonLangs.size());
    EXPECT_EQ(english, singletonLangs[0]);

212 213
    FontLanguage french = createFontLanguage("fr");
    FontLanguages twoLangs = createFontLanguages("en,fr");
214 215 216 217 218
    EXPECT_EQ(2u, twoLangs.size());
    EXPECT_EQ(english, twoLangs[0]);
    EXPECT_EQ(french, twoLangs[1]);
}

219 220
TEST_F(FontLanguagesTest, unsupportedLanguageTests) {
    FontLanguage unsupportedLang = createFontLanguage("abcd");
221 222
    ASSERT_TRUE(unsupportedLang.isUnsupported());

223
    FontLanguages oneUnsupported = createFontLanguages("abcd-example");
224 225 226
    EXPECT_EQ(1u, oneUnsupported.size());
    EXPECT_TRUE(oneUnsupported[0].isUnsupported());

227
    FontLanguages twoUnsupporteds = createFontLanguages("abcd-example,abcd-example");
228 229 230
    EXPECT_EQ(1u, twoUnsupporteds.size());
    EXPECT_TRUE(twoUnsupporteds[0].isUnsupported());

231 232
    FontLanguage english = createFontLanguage("en");
    FontLanguages firstUnsupported = createFontLanguages("abcd-example,en");
233 234 235
    EXPECT_EQ(1u, firstUnsupported.size());
    EXPECT_EQ(english, firstUnsupported[0]);

236
    FontLanguages lastUnsupported = createFontLanguages("en,abcd-example");
237 238 239 240
    EXPECT_EQ(1u, lastUnsupported.size());
    EXPECT_EQ(english, lastUnsupported[0]);
}

241 242 243 244
TEST_F(FontLanguagesTest, repeatedLanguageTests) {
    FontLanguage english = createFontLanguage("en");
    FontLanguage french = createFontLanguage("fr");
    FontLanguage englishInLatn = createFontLanguage("en-Latn");
245 246
    ASSERT_TRUE(english == englishInLatn);

247
    FontLanguages langs = createFontLanguages("en,en-Latn");
248 249
    EXPECT_EQ(1u, langs.size());
    EXPECT_EQ(english, langs[0]);
250 251 252 253 254 255 256 257 258 259 260

    // Country codes are ignored.
    FontLanguages fr = createFontLanguages("fr,fr-CA,fr-FR");
    EXPECT_EQ(1u, fr.size());
    EXPECT_EQ(french, fr[0]);

    // The order should be kept.
    langs = createFontLanguages("en,fr,en-Latn");
    EXPECT_EQ(2u, langs.size());
    EXPECT_EQ(english, langs[0]);
    EXPECT_EQ(french, langs[1]);
261 262
}

263
TEST_F(FontLanguagesTest, undEmojiTests) {
S
Seigo Nonaka 已提交
264
    FontLanguage emoji = createFontLanguage("und-Zsye");
265 266
    EXPECT_TRUE(emoji.hasEmojiFlag());

267
    FontLanguage und = createFontLanguage("und");
268 269 270
    EXPECT_FALSE(und.hasEmojiFlag());
    EXPECT_FALSE(emoji == und);

271
    FontLanguage undExample = createFontLanguage("und-example");
272 273 274 275
    EXPECT_FALSE(undExample.hasEmojiFlag());
    EXPECT_FALSE(emoji == undExample);
}

276
TEST_F(FontLanguagesTest, registerLanguageListTest) {
S
Seigo Nonaka 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    EXPECT_EQ(0UL, FontStyle::registerLanguageList(""));
    EXPECT_NE(0UL, FontStyle::registerLanguageList("en"));
    EXPECT_NE(0UL, FontStyle::registerLanguageList("jp"));
    EXPECT_NE(0UL, FontStyle::registerLanguageList("en,zh-Hans"));

    EXPECT_EQ(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("en"));
    EXPECT_NE(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("jp"));

    EXPECT_EQ(FontStyle::registerLanguageList("en,zh-Hans"),
              FontStyle::registerLanguageList("en,zh-Hans"));
    EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
              FontStyle::registerLanguageList("zh-Hans,en"));
    EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
              FontStyle::registerLanguageList("jp"));
    EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
              FontStyle::registerLanguageList("en"));
    EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
              FontStyle::registerLanguageList("en,zh-Hant"));
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
// The test font has following glyphs.
// U+82A6
// U+82A6 U+FE00 (VS1)
// U+82A6 U+E0100 (VS17)
// U+82A6 U+E0101 (VS18)
// U+82A6 U+E0102 (VS19)
// U+845B
// U+845B U+FE00 (VS2)
// U+845B U+E0101 (VS18)
// U+845B U+E0102 (VS19)
// U+845B U+E0103 (VS20)
// U+537F
// U+717D U+FE02 (VS3)
// U+717D U+E0102 (VS19)
// U+717D U+E0103 (VS20)
312
const char kVsTestFont[] = kTestFontDir "VarioationSelectorTest-Regular.ttf";
313

314
class FontFamilyTest : public ICUTestBase {
315 316
public:
    virtual void SetUp() override {
317
        ICUTestBase::SetUp();
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        if (access(kVsTestFont, R_OK) != 0) {
            FAIL() << "Unable to read " << kVsTestFont << ". "
                   << "Please prepare the test data directory. "
                   << "For more details, please see how_to_run.txt.";
        }
    }
};

// Asserts that the font family has glyphs for and only for specified codepoint
// and variationSelector pairs.
void expectVSGlyphs(FontFamily* family, uint32_t codepoint, const std::set<uint32_t>& vs) {
    for (uint32_t i = 0xFE00; i <= 0xE01EF; ++i) {
        // Move to variation selectors supplements after variation selectors.
        if (i == 0xFF00) {
            i = 0xE0100;
        }
        if (vs.find(i) == vs.end()) {
            EXPECT_FALSE(family->hasVariationSelector(codepoint, i))
                    << "Glyph for U+" << std::hex << codepoint << " U+" << i;
        } else {
            EXPECT_TRUE(family->hasVariationSelector(codepoint, i))
                    << "Glyph for U+" << std::hex << codepoint << " U+" << i;
        }

    }
}

TEST_F(FontFamilyTest, hasVariationSelectorTest) {
    MinikinFontForTest minikinFont(kVsTestFont);
    FontFamily family;
    family.addFont(&minikinFont);

    AutoMutex _l(gMinikinLock);

    const uint32_t kVS1 = 0xFE00;
    const uint32_t kVS2 = 0xFE01;
    const uint32_t kVS3 = 0xFE02;
    const uint32_t kVS17 = 0xE0100;
    const uint32_t kVS18 = 0xE0101;
    const uint32_t kVS19 = 0xE0102;
    const uint32_t kVS20 = 0xE0103;

    const uint32_t kSupportedChar1 = 0x82A6;
    EXPECT_TRUE(family.getCoverage()->get(kSupportedChar1));
    expectVSGlyphs(&family, kSupportedChar1, std::set<uint32_t>({kVS1, kVS17, kVS18, kVS19}));

    const uint32_t kSupportedChar2 = 0x845B;
    EXPECT_TRUE(family.getCoverage()->get(kSupportedChar2));
    expectVSGlyphs(&family, kSupportedChar2, std::set<uint32_t>({kVS2, kVS18, kVS19, kVS20}));

    const uint32_t kNoVsSupportedChar = 0x537F;
    EXPECT_TRUE(family.getCoverage()->get(kNoVsSupportedChar));
    expectVSGlyphs(&family, kNoVsSupportedChar, std::set<uint32_t>());

    const uint32_t kVsOnlySupportedChar = 0x717D;
    EXPECT_FALSE(family.getCoverage()->get(kVsOnlySupportedChar));
    expectVSGlyphs(&family, kVsOnlySupportedChar, std::set<uint32_t>({kVS3, kVS19, kVS20}));

    const uint32_t kNotSupportedChar = 0x845C;
    EXPECT_FALSE(family.getCoverage()->get(kNotSupportedChar));
    expectVSGlyphs(&family, kNotSupportedChar, std::set<uint32_t>());
}

TEST_F(FontFamilyTest, hasVariationSelectorWorksAfterpurgeHbFontCache) {
    MinikinFontForTest minikinFont(kVsTestFont);
    FontFamily family;
    family.addFont(&minikinFont);

    const uint32_t kVS1 = 0xFE00;
    const uint32_t kSupportedChar1 = 0x82A6;

    AutoMutex _l(gMinikinLock);
    EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1));

    family.purgeHbFontCache();
    EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1));
}
}  // namespace android