From c3b9f7dadde2701885950c0d6aa0f6f739b5cecb Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Thu, 16 Jun 2016 16:59:30 +0900 Subject: [PATCH] Fix test utilities This fixes following three memory leaks in test utilities. There is no problem in production code and this CL doesn't affect any production behaviors. - SkTypeface leaks due to forget calling SkSafeUnref in dtor. - MinikinFontForTest leaks during constructing FontCollection. - FontCollection leaks due to unnecessary AddRef. Change-Id: I22e1e0307f1b2499296acb1aacc3ef66076a36e9 --- .../unittest/FontCollectionItemizeTest.cpp | 23 ++++++----- .../tests/unittest/FontCollectionTest.cpp | 32 +++++++-------- .../flutter/tests/unittest/FontFamilyTest.cpp | 6 ++- .../tests/unittest/HbFontCacheTest.cpp | 39 +++++++++++-------- .../src/flutter/tests/util/FontTestUtils.cpp | 6 ++- .../flutter/tests/util/MinikinFontForTest.cpp | 16 +++++--- .../flutter/tests/util/MinikinFontForTest.h | 9 ++++- 7 files changed, 78 insertions(+), 53 deletions(-) diff --git a/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp b/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp index 8ae703ea12..b28820b1f8 100644 --- a/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp +++ b/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp @@ -666,11 +666,13 @@ TEST_F(FontCollectionItemizeTest, itemize_vs_sequence_but_no_base_char) { std::vector families; FontFamily* family1 = new FontFamily(VARIANT_DEFAULT); - family1->addFont(new MinikinFontForTest(kLatinFont)); + MinikinAutoUnref font(MinikinFontForTest::createFromFile(kLatinFont)); + family1->addFont(font.get()); families.push_back(family1); FontFamily* family2 = new FontFamily(VARIANT_DEFAULT); - family2->addFont(new MinikinFontForTest(kVSTestFont)); + MinikinAutoUnref font2(MinikinFontForTest::createFromFile(kVSTestFont)); + family2->addFont(font2.get()); families.push_back(family2); FontCollection collection(families); @@ -794,8 +796,9 @@ TEST_F(FontCollectionItemizeTest, itemize_LanguageScore) { // Prepare first font which doesn't supports U+9AA8 FontFamily* firstFamily = new FontFamily( FontStyle::registerLanguageList("und"), 0 /* variant */); - MinikinFont* firstFamilyMinikinFont = new MinikinFontForTest(kNoGlyphFont); - firstFamily->addFont(firstFamilyMinikinFont); + MinikinAutoUnref firstFamilyMinikinFont( + MinikinFontForTest::createFromFile(kNoGlyphFont)); + firstFamily->addFont(firstFamilyMinikinFont.get()); families.push_back(firstFamily); // Prepare font families @@ -806,12 +809,12 @@ TEST_F(FontCollectionItemizeTest, itemize_LanguageScore) { for (size_t i = 0; i < testCase.fontLanguages.size(); ++i) { FontFamily* family = new FontFamily( FontStyle::registerLanguageList(testCase.fontLanguages[i]), 0 /* variant */); - MinikinFont* minikin_font = new MinikinFontForTest(kJAFont); - family->addFont(minikin_font); + MinikinAutoUnref minikin_font(MinikinFontForTest::createFromFile(kJAFont)); + family->addFont(minikin_font.get()); families.push_back(family); - fontLangIdxMap.insert(std::make_pair(minikin_font, i)); + fontLangIdxMap.insert(std::make_pair(minikin_font.get(), i)); } - FontCollection collection(families); + MinikinAutoUnref collection(new FontCollection(families)); for (auto family : families) { family->Unref(); } @@ -820,13 +823,13 @@ TEST_F(FontCollectionItemizeTest, itemize_LanguageScore) { const FontStyle style = FontStyle( FontStyle::registerLanguageList(testCase.userPreferredLanguages)); std::vector runs; - itemize(&collection, "U+9AA8", style, &runs); + itemize(collection.get(), "U+9AA8", style, &runs); ASSERT_EQ(1U, runs.size()); ASSERT_NE(nullptr, runs[0].fakedFont.font); // First family doesn't support U+9AA8 and others support it, so the first font should not // be selected. - EXPECT_NE(firstFamilyMinikinFont, runs[0].fakedFont.font); + EXPECT_NE(firstFamilyMinikinFont.get(), runs[0].fakedFont.font); // Lookup used font family by MinikinFont*. const int usedLangIndex = fontLangIdxMap[runs[0].fakedFont.font]; diff --git a/engine/src/flutter/tests/unittest/FontCollectionTest.cpp b/engine/src/flutter/tests/unittest/FontCollectionTest.cpp index a22067a5b0..3f365be083 100644 --- a/engine/src/flutter/tests/unittest/FontCollectionTest.cpp +++ b/engine/src/flutter/tests/unittest/FontCollectionTest.cpp @@ -40,40 +40,40 @@ namespace minikin { // U+717D U+E0103 (VS20) const char kVsTestFont[] = kTestFontDir "/VarioationSelectorTest-Regular.ttf"; -void expectVSGlyphs(const FontCollection& fc, uint32_t codepoint, const std::set& vsSet) { +void expectVSGlyphs(const FontCollection* fc, uint32_t codepoint, const std::set& vsSet) { for (uint32_t vs = 0xFE00; vs <= 0xE01EF; ++vs) { // Move to variation selectors supplements after variation selectors. if (vs == 0xFF00) { vs = 0xE0100; } if (vsSet.find(vs) == vsSet.end()) { - EXPECT_FALSE(fc.hasVariationSelector(codepoint, vs)) + EXPECT_FALSE(fc->hasVariationSelector(codepoint, vs)) << "Glyph for U+" << std::hex << codepoint << " U+" << vs; } else { - EXPECT_TRUE(fc.hasVariationSelector(codepoint, vs)) + EXPECT_TRUE(fc->hasVariationSelector(codepoint, vs)) << "Glyph for U+" << std::hex << codepoint << " U+" << vs; } } } TEST(FontCollectionTest, hasVariationSelectorTest) { - FontFamily* family = new FontFamily(); - family->addFont(new MinikinFontForTest(kVsTestFont)); - std::vector families({family}); - FontCollection fc(families); - family->Unref(); + MinikinAutoUnref family(new FontFamily()); + MinikinAutoUnref font(MinikinFontForTest::createFromFile(kVsTestFont)); + family->addFont(font.get()); + std::vector families({family.get()}); + MinikinAutoUnref fc(new FontCollection(families)); - EXPECT_FALSE(fc.hasVariationSelector(0x82A6, 0)); - expectVSGlyphs(fc, 0x82A6, std::set({0xFE00, 0xE0100, 0xE0101, 0xE0102})); + EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0)); + expectVSGlyphs(fc.get(), 0x82A6, std::set({0xFE00, 0xE0100, 0xE0101, 0xE0102})); - EXPECT_FALSE(fc.hasVariationSelector(0x845B, 0)); - expectVSGlyphs(fc, 0x845B, std::set({0xFE01, 0xE0101, 0xE0102, 0xE0103})); + EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0)); + expectVSGlyphs(fc.get(), 0x845B, std::set({0xFE01, 0xE0101, 0xE0102, 0xE0103})); - EXPECT_FALSE(fc.hasVariationSelector(0x537F, 0)); - expectVSGlyphs(fc, 0x537F, std::set({})); + EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0)); + expectVSGlyphs(fc.get(), 0x537F, std::set({})); - EXPECT_FALSE(fc.hasVariationSelector(0x717D, 0)); - expectVSGlyphs(fc, 0x717D, std::set({0xFE02, 0xE0102, 0xE0103})); + EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0)); + expectVSGlyphs(fc.get(), 0x717D, std::set({0xFE02, 0xE0102, 0xE0103})); } const char kEmojiXmlFile[] = kTestFontDir "emoji.xml"; diff --git a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp index 384703f8c4..69fef23da8 100644 --- a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp +++ b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp @@ -350,7 +350,8 @@ void expectVSGlyphs(FontFamily* family, uint32_t codepoint, const std::set minikinFont(new MinikinFontForTest(kVsTestFont)); + MinikinAutoUnref + minikinFont(MinikinFontForTest::createFromFile(kVsTestFont)); MinikinAutoUnref family(new FontFamily); family->addFont(minikinFont.get()); @@ -403,7 +404,8 @@ TEST_F(FontFamilyTest, hasVSTableTest) { "Font " + testCase.fontPath + " should have a variation sequence table." : "Font " + testCase.fontPath + " shouldn't have a variation sequence table."); - MinikinAutoUnref minikinFont(new MinikinFontForTest(testCase.fontPath)); + MinikinAutoUnref minikinFont( + MinikinFontForTest::createFromFile(testCase.fontPath)); MinikinAutoUnref family(new FontFamily); family->addFont(minikinFont.get()); android::AutoMutex _l(gMinikinLock); diff --git a/engine/src/flutter/tests/unittest/HbFontCacheTest.cpp b/engine/src/flutter/tests/unittest/HbFontCacheTest.cpp index 82adc4f768..aa4cb34c05 100644 --- a/engine/src/flutter/tests/unittest/HbFontCacheTest.cpp +++ b/engine/src/flutter/tests/unittest/HbFontCacheTest.cpp @@ -37,34 +37,39 @@ public: }; TEST_F(HbFontCacheTest, getHbFontLockedTest) { + MinikinAutoUnref fontA( + MinikinFontForTest::createFromFile(kTestFontDir "Regular.ttf")); + + MinikinAutoUnref fontB( + MinikinFontForTest::createFromFile(kTestFontDir "Bold.ttf")); + + MinikinAutoUnref fontC( + MinikinFontForTest::createFromFile(kTestFontDir "BoldItalic.ttf")); + android::AutoMutex _l(gMinikinLock); - - MinikinFontForTest fontA(kTestFontDir "Regular.ttf"); - MinikinFontForTest fontB(kTestFontDir "Bold.ttf"); - MinikinFontForTest fontC(kTestFontDir "BoldItalic.ttf"); - // Never return NULL. - EXPECT_NE(nullptr, getHbFontLocked(&fontA)); - EXPECT_NE(nullptr, getHbFontLocked(&fontB)); - EXPECT_NE(nullptr, getHbFontLocked(&fontC)); + EXPECT_NE(nullptr, getHbFontLocked(fontA.get())); + EXPECT_NE(nullptr, getHbFontLocked(fontB.get())); + EXPECT_NE(nullptr, getHbFontLocked(fontC.get())); EXPECT_NE(nullptr, getHbFontLocked(nullptr)); // Must return same object if same font object is passed. - EXPECT_EQ(getHbFontLocked(&fontA), getHbFontLocked(&fontA)); - EXPECT_EQ(getHbFontLocked(&fontB), getHbFontLocked(&fontB)); - EXPECT_EQ(getHbFontLocked(&fontC), getHbFontLocked(&fontC)); + EXPECT_EQ(getHbFontLocked(fontA.get()), getHbFontLocked(fontA.get())); + EXPECT_EQ(getHbFontLocked(fontB.get()), getHbFontLocked(fontB.get())); + EXPECT_EQ(getHbFontLocked(fontC.get()), getHbFontLocked(fontC.get())); // Different object must be returned if the passed minikinFont has different ID. - EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontB)); - EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontC)); + EXPECT_NE(getHbFontLocked(fontA.get()), getHbFontLocked(fontB.get())); + EXPECT_NE(getHbFontLocked(fontA.get()), getHbFontLocked(fontC.get())); } TEST_F(HbFontCacheTest, purgeCacheTest) { - android::AutoMutex _l(gMinikinLock); - MinikinFontForTest minikinFont(kTestFontDir "Regular.ttf"); + MinikinAutoUnref minikinFont( + MinikinFontForTest::createFromFile(kTestFontDir "Regular.ttf")); - hb_font_t* font = getHbFontLocked(&minikinFont); + android::AutoMutex _l(gMinikinLock); + hb_font_t* font = getHbFontLocked(minikinFont.get()); ASSERT_NE(nullptr, font); // Set user data to identify the font object. @@ -78,7 +83,7 @@ TEST_F(HbFontCacheTest, purgeCacheTest) { // By checking user data, confirm that the object after purge is different from previously // created one. Do not compare the returned pointer here since memory allocator may assign // same region for new object. - font = getHbFontLocked(&minikinFont); + font = getHbFontLocked(minikinFont.get()); EXPECT_EQ(nullptr, hb_font_get_user_data(font, &key)); } diff --git a/engine/src/flutter/tests/util/FontTestUtils.cpp b/engine/src/flutter/tests/util/FontTestUtils.cpp index 76d3ea20a2..eaf3610df2 100644 --- a/engine/src/flutter/tests/util/FontTestUtils.cpp +++ b/engine/src/flutter/tests/util/FontTestUtils.cpp @@ -68,14 +68,16 @@ FontCollection* getFontCollection(const char* fontDir, const char* fontXml) { LOG_ALWAYS_FATAL_IF(access(fontPath.c_str(), R_OK) != 0, "%s is not found", fontPath.c_str()); - family->addFont(new MinikinFontForTest(fontPath), FontStyle(weight, italic)); + MinikinAutoUnref + minikinFont(MinikinFontForTest::createFromFile(fontPath)); + + family->addFont(minikinFont.get(), FontStyle(weight, italic)); } families.push_back(family); } xmlFreeDoc(doc); FontCollection* collection = new FontCollection(families); - collection->Ref(); for (size_t i = 0; i < families.size(); ++i) { families[i]->Unref(); } diff --git a/engine/src/flutter/tests/util/MinikinFontForTest.cpp b/engine/src/flutter/tests/util/MinikinFontForTest.cpp index ae8ef72bcf..93e3e66d2d 100644 --- a/engine/src/flutter/tests/util/MinikinFontForTest.cpp +++ b/engine/src/flutter/tests/util/MinikinFontForTest.cpp @@ -24,17 +24,23 @@ namespace minikin { -MinikinFontForTest::MinikinFontForTest(const std::string& font_path) : - MinikinFontForTest(font_path, SkTypeface::CreateFromFile(font_path.c_str())) { +// static +MinikinFontForTest* MinikinFontForTest::createFromFile(const std::string& font_path) { + SkTypeface* typeface = SkTypeface::CreateFromFile(font_path.c_str()); + MinikinFontForTest* font = new MinikinFontForTest(font_path, typeface); + SkSafeUnref(typeface); + return font; } MinikinFontForTest::MinikinFontForTest(const std::string& font_path, SkTypeface* typeface) : - MinikinFont(typeface->uniqueID()), - mTypeface(typeface), - mFontPath(font_path) { + MinikinFont(typeface->uniqueID()), + mTypeface(typeface), + mFontPath(font_path) { + SkSafeRef(mTypeface); } MinikinFontForTest::~MinikinFontForTest() { + SkSafeUnref(mTypeface); } float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */, diff --git a/engine/src/flutter/tests/util/MinikinFontForTest.h b/engine/src/flutter/tests/util/MinikinFontForTest.h index 56f63adaff..bfd8421525 100644 --- a/engine/src/flutter/tests/util/MinikinFontForTest.h +++ b/engine/src/flutter/tests/util/MinikinFontForTest.h @@ -25,10 +25,13 @@ namespace minikin { class MinikinFontForTest : public MinikinFont { public: - explicit MinikinFontForTest(const std::string& font_path); MinikinFontForTest(const std::string& font_path, SkTypeface* typeface); ~MinikinFontForTest(); + // Helper function for creating MinikinFontForTest instance from font file. + // Calller need to unref returned object. + static MinikinFontForTest* createFromFile(const std::string& font_path); + // MinikinFont overrides. float GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint &paint) const; void GetBounds(MinikinRect* bounds, uint32_t glyph_id, @@ -37,6 +40,10 @@ public: const std::string& fontPath() const { return mFontPath; } private: + MinikinFontForTest() = delete; + MinikinFontForTest(const MinikinFontForTest&) = delete; + MinikinFontForTest& operator=(MinikinFontForTest&) = delete; + SkTypeface *mTypeface; const std::string mFontPath; };