1 /*
2  * Copyright (C) 2017 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 <gtest/gtest.h>
18 
19 #include "../util/FontTestUtils.h"
20 #include "../util/MinikinFontForTest.h"
21 #include "HbFontCache.h"
22 #include "MinikinInternal.h"
23 #include "minikin/FontCollection.h"
24 #include "minikin/Layout.h"
25 
26 namespace minikin {
27 
28 typedef std::pair<std::string, int> TestParam;
29 
30 class FontFamilyHarfBuzzCompatibilityTest : public ::testing::TestWithParam<TestParam> {};
31 
TEST_P(FontFamilyHarfBuzzCompatibilityTest,CoverageTest)32 TEST_P(FontFamilyHarfBuzzCompatibilityTest, CoverageTest) {
33     const std::string& fontPath = GetParam().first;
34     int ttcIndex = GetParam().second;
35 
36     std::shared_ptr<MinikinFont> font(new MinikinFontForTest(fontPath, ttcIndex));
37     std::shared_ptr<FontFamily> family =
38             std::make_shared<FontFamily>(std::vector<Font>({Font(font, FontStyle())}));
39 
40     android::AutoMutex _l(gMinikinLock);
41     hb_font_t* hbFont = getHbFontLocked(font.get());
42 
43     for (uint32_t codePoint = 0; codePoint < MAX_UNICODE_CODE_POINT; ++codePoint) {
44         uint32_t unusedGlyph;
45         EXPECT_EQ(family->hasGlyph(codePoint, 0 /* variation selector */),
46                 static_cast<bool>(hb_font_get_glyph(hbFont, codePoint, 0 /* variation selector */,
47                         &unusedGlyph)));
48     }
49 
50     for (uint32_t vs = VS1; vs < VS256; ++vs) {
51         // Move to variation selectors supplements after variation selectors.
52         if (vs == VS16 + 1) {
53             vs = VS17;
54         }
55         for (uint32_t codePoint = 0; codePoint < MAX_UNICODE_CODE_POINT; ++codePoint) {
56             uint32_t unusedGlyph;
57             ASSERT_EQ(family->hasGlyph(codePoint, vs),
58                     static_cast<bool>(hb_font_get_glyph(hbFont, codePoint, vs, &unusedGlyph)))
59                 << "Inconsistent Result: " << fontPath << "#" << ttcIndex
60                 << ": U+" << std::hex << codePoint << " U+" << std::hex << vs
61                 << " Minikin: " << family->hasGlyph(codePoint, vs)
62                 << " HarfBuzz: "
63                 << static_cast<bool>(hb_font_get_glyph(hbFont, codePoint, vs, &unusedGlyph));
64 
65         }
66     }
67     hb_font_destroy(hbFont);
68 }
69 
70 INSTANTIATE_TEST_CASE_P(FontFamilyTest,
71         FontFamilyHarfBuzzCompatibilityTest,
72         ::testing::Values(
73                 TestParam("/system/fonts/NotoSansCJK-Regular.ttc", 0),
74                 TestParam("/system/fonts/NotoColorEmoji.ttf", 0)));
75 }  // namespace minikin
76