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
This commit is contained in:
Seigo Nonaka
2016-06-16 16:59:30 +09:00
parent 2fdcf62495
commit c3b9f7dadd
7 changed files with 78 additions and 53 deletions

View File

@@ -666,11 +666,13 @@ TEST_F(FontCollectionItemizeTest, itemize_vs_sequence_but_no_base_char) {
std::vector<FontFamily*> families;
FontFamily* family1 = new FontFamily(VARIANT_DEFAULT);
family1->addFont(new MinikinFontForTest(kLatinFont));
MinikinAutoUnref<MinikinFont> font(MinikinFontForTest::createFromFile(kLatinFont));
family1->addFont(font.get());
families.push_back(family1);
FontFamily* family2 = new FontFamily(VARIANT_DEFAULT);
family2->addFont(new MinikinFontForTest(kVSTestFont));
MinikinAutoUnref<MinikinFont> 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<MinikinFont> 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<MinikinFont> 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<FontCollection> 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<FontCollection::Run> 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];

View File

@@ -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<uint32_t>& vsSet) {
void expectVSGlyphs(const FontCollection* fc, uint32_t codepoint, const std::set<uint32_t>& 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<FontFamily*> families({family});
FontCollection fc(families);
family->Unref();
MinikinAutoUnref<FontFamily> family(new FontFamily());
MinikinAutoUnref<MinikinFont> font(MinikinFontForTest::createFromFile(kVsTestFont));
family->addFont(font.get());
std::vector<FontFamily*> families({family.get()});
MinikinAutoUnref<FontCollection> fc(new FontCollection(families));
EXPECT_FALSE(fc.hasVariationSelector(0x82A6, 0));
expectVSGlyphs(fc, 0x82A6, std::set<uint32_t>({0xFE00, 0xE0100, 0xE0101, 0xE0102}));
EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0));
expectVSGlyphs(fc.get(), 0x82A6, std::set<uint32_t>({0xFE00, 0xE0100, 0xE0101, 0xE0102}));
EXPECT_FALSE(fc.hasVariationSelector(0x845B, 0));
expectVSGlyphs(fc, 0x845B, std::set<uint32_t>({0xFE01, 0xE0101, 0xE0102, 0xE0103}));
EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0));
expectVSGlyphs(fc.get(), 0x845B, std::set<uint32_t>({0xFE01, 0xE0101, 0xE0102, 0xE0103}));
EXPECT_FALSE(fc.hasVariationSelector(0x537F, 0));
expectVSGlyphs(fc, 0x537F, std::set<uint32_t>({}));
EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0));
expectVSGlyphs(fc.get(), 0x537F, std::set<uint32_t>({}));
EXPECT_FALSE(fc.hasVariationSelector(0x717D, 0));
expectVSGlyphs(fc, 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0));
expectVSGlyphs(fc.get(), 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
}
const char kEmojiXmlFile[] = kTestFontDir "emoji.xml";

View File

@@ -350,7 +350,8 @@ void expectVSGlyphs(FontFamily* family, uint32_t codepoint, const std::set<uint3
}
TEST_F(FontFamilyTest, hasVariationSelectorTest) {
MinikinAutoUnref<MinikinFontForTest> minikinFont(new MinikinFontForTest(kVsTestFont));
MinikinAutoUnref<MinikinFontForTest>
minikinFont(MinikinFontForTest::createFromFile(kVsTestFont));
MinikinAutoUnref<FontFamily> 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<MinikinFontForTest> minikinFont(new MinikinFontForTest(testCase.fontPath));
MinikinAutoUnref<MinikinFontForTest> minikinFont(
MinikinFontForTest::createFromFile(testCase.fontPath));
MinikinAutoUnref<FontFamily> family(new FontFamily);
family->addFont(minikinFont.get());
android::AutoMutex _l(gMinikinLock);

View File

@@ -37,34 +37,39 @@ public:
};
TEST_F(HbFontCacheTest, getHbFontLockedTest) {
MinikinAutoUnref<MinikinFontForTest> fontA(
MinikinFontForTest::createFromFile(kTestFontDir "Regular.ttf"));
MinikinAutoUnref<MinikinFontForTest> fontB(
MinikinFontForTest::createFromFile(kTestFontDir "Bold.ttf"));
MinikinAutoUnref<MinikinFontForTest> 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<MinikinFontForTest> 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));
}

View File

@@ -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<MinikinFontForTest>
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();
}

View File

@@ -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 */,

View File

@@ -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;
};