From a8835b2d8f92c93e9d3725378443a80d1a9f45f2 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Wed, 28 Apr 2021 18:42:19 -0700 Subject: [PATCH] Tests: Convert LibGfx font handling test to be LibTest based. --- Userland/Tests/LibGfx/CMakeLists.txt | 2 + Userland/Tests/LibGfx/TestFontHandling.cpp | 169 ++++++++++++++++++ Userland/Tests/LibGfx/font.cpp | 196 --------------------- 3 files changed, 171 insertions(+), 196 deletions(-) create mode 100644 Userland/Tests/LibGfx/TestFontHandling.cpp delete mode 100644 Userland/Tests/LibGfx/font.cpp diff --git a/Userland/Tests/LibGfx/CMakeLists.txt b/Userland/Tests/LibGfx/CMakeLists.txt index 640ac2f0e0..daeb330270 100644 --- a/Userland/Tests/LibGfx/CMakeLists.txt +++ b/Userland/Tests/LibGfx/CMakeLists.txt @@ -1,6 +1,7 @@ file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp") list(REMOVE_ITEM CMD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/BenchmarkGfxPainter.cpp) list(REMOVE_ITEM CMD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/TestImageDecoder.cpp) +list(REMOVE_ITEM CMD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/TestFontHandling.cpp) # FIXME These tests do not use LibTest foreach(CMD_SRC ${CMD_SOURCES}) @@ -10,5 +11,6 @@ foreach(CMD_SRC ${CMD_SOURCES}) install(TARGETS ${CMD_NAME} RUNTIME DESTINATION usr/Tests/LibGfx) endforeach() +serenity_test(TestFontHandling.cpp LibGfx LIBS LibGUI) serenity_test(TestImageDecoder.cpp LibGfx LIBS LibGUI) serenity_test(BenchmarkGfxPainter.cpp LibGfx LIBS LibGUI) diff --git a/Userland/Tests/LibGfx/TestFontHandling.cpp b/Userland/Tests/LibGfx/TestFontHandling.cpp new file mode 100644 index 0000000000..82e209bce0 --- /dev/null +++ b/Userland/Tests/LibGfx/TestFontHandling.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2021, Brian Gianforcaro + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +TEST_CASE(test_fontdatabase_get_by_name) +{ + const char* name = "Liza 10 400"; + auto& font_database = Gfx::FontDatabase::the(); + EXPECT(!font_database.get_by_name(name)->name().is_null()); +} + +TEST_CASE(test_fontdatabase_get) +{ + auto& font_database = Gfx::FontDatabase::the(); + EXPECT(!font_database.get("Liza", 10, 400)->name().is_null()); +} + +TEST_CASE(test_fontdatabase_for_each_font) +{ + auto& font_database = Gfx::FontDatabase::the(); + font_database.for_each_font([&](const Gfx::Font& font) { + EXPECT(!font.name().is_null()); + EXPECT(!font.qualified_name().is_null()); + EXPECT(!font.family().is_null()); + EXPECT(font.glyph_count() > 0); + }); +} + +TEST_CASE(test_default_font) +{ + EXPECT(!Gfx::FontDatabase::default_font().name().is_null()); +} + +TEST_CASE(test_default_fixed_width_font) +{ + EXPECT(!Gfx::FontDatabase::default_fixed_width_font().name().is_null()); +} + +TEST_CASE(test_default_bold_fixed_width_font) +{ + EXPECT(!Gfx::FontDatabase::default_bold_fixed_width_font().name().is_null()); +} + +TEST_CASE(test_default_bold_font) +{ + EXPECT(!Gfx::FontDatabase::default_bold_font().name().is_null()); +} + +TEST_CASE(test_clone) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + auto new_font = font->clone(); + EXPECT(!new_font->name().is_null()); + EXPECT(!new_font->qualified_name().is_null()); + EXPECT(!new_font->family().is_null()); + EXPECT(new_font->glyph_count() > 0); +} + +TEST_CASE(test_set_name) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + const char* name = "my newly created font"; + font->set_name(name); + + EXPECT(!font->name().is_null()); + EXPECT(font->name().contains(name)); +} + +TEST_CASE(test_set_family) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + const char* family = "my newly created font family"; + font->set_family(family); + + EXPECT(!font->family().is_null()); + EXPECT(font->family().contains(family)); +} + +TEST_CASE(test_set_glyph_width) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + size_t ch = 123; + font->set_glyph_width(ch, glyph_width); + + EXPECT(font->glyph_width(ch) == glyph_width); +} + +TEST_CASE(test_set_glyph_spacing) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + u8 glyph_spacing = 8; + font->set_glyph_spacing(glyph_spacing); + + EXPECT(font->glyph_spacing() == glyph_spacing); +} + +TEST_CASE(test_set_type) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + auto type = Gfx::FontTypes::Default; + font->set_type(type); + + EXPECT(font->type() == type); +} + +TEST_CASE(test_width) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + EXPECT(font->width("A") == glyph_width); +} + +TEST_CASE(test_glyph_or_emoji_width) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + font->set_type(Gfx::FontTypes::Default); + + EXPECT(font->glyph_or_emoji_width(0)); +} + +TEST_CASE(test_load_from_file) +{ + auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font"); + EXPECT(!font->name().is_null()); +} + +TEST_CASE(test_write_to_file) +{ + u8 glyph_height = 1; + u8 glyph_width = 1; + auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); + + char path[] = "/tmp/new.font.XXXXXX"; + EXPECT(mkstemp(path) != -1); + EXPECT(font->write_to_file(path)); + unlink(path); +} diff --git a/Userland/Tests/LibGfx/font.cpp b/Userland/Tests/LibGfx/font.cpp deleted file mode 100644 index 842e402459..0000000000 --- a/Userland/Tests/LibGfx/font.cpp +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (c) 2020, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include -#include - -static void test_fontdatabase_get_by_name() -{ - const char* name = "Liza 10 400"; - auto& font_database = Gfx::FontDatabase::the(); - assert(!font_database.get_by_name(name)->name().is_null()); -} - -static void test_fontdatabase_get() -{ - auto& font_database = Gfx::FontDatabase::the(); - assert(!font_database.get("Liza", 10, 400)->name().is_null()); -} - -static void test_fontdatabase_for_each_font() -{ - auto& font_database = Gfx::FontDatabase::the(); - font_database.for_each_font([&](const Gfx::Font& font) { - assert(!font.name().is_null()); - assert(!font.qualified_name().is_null()); - assert(!font.family().is_null()); - assert(font.glyph_count() > 0); - }); -} - -static void test_default_font() -{ - assert(!Gfx::FontDatabase::default_font().name().is_null()); -} - -static void test_default_fixed_width_font() -{ - assert(!Gfx::FontDatabase::default_fixed_width_font().name().is_null()); -} - -static void test_default_bold_fixed_width_font() -{ - assert(!Gfx::FontDatabase::default_bold_fixed_width_font().name().is_null()); -} - -static void test_default_bold_font() -{ - assert(!Gfx::FontDatabase::default_bold_font().name().is_null()); -} - -static void test_clone() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - auto new_font = font->clone(); - assert(!new_font->name().is_null()); - assert(!new_font->qualified_name().is_null()); - assert(!new_font->family().is_null()); - assert(new_font->glyph_count() > 0); -} - -static void test_set_name() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - const char* name = "my newly created font"; - font->set_name(name); - - assert(!font->name().is_null()); - assert(font->name().contains(name)); -} - -static void test_set_family() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - const char* family = "my newly created font family"; - font->set_family(family); - - assert(!font->family().is_null()); - assert(font->family().contains(family)); -} - -static void test_set_glyph_width() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - size_t ch = 123; - font->set_glyph_width(ch, glyph_width); - - assert(font->glyph_width(ch) == glyph_width); -} -static void test_set_glyph_spacing() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - u8 glyph_spacing = 8; - font->set_glyph_spacing(glyph_spacing); - - assert(font->glyph_spacing() == glyph_spacing); -} - -static void test_set_type() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - auto type = Gfx::FontTypes::Default; - font->set_type(type); - - assert(font->type() == type); -} - -static void test_width() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - assert(font->width("A") == glyph_width); -} - -static void test_glyph_or_emoji_width() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - font->set_type(Gfx::FontTypes::Default); - - assert(font->glyph_or_emoji_width(0)); -} - -static void test_load_from_file() -{ - auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font"); - assert(!font->name().is_null()); -} - -static void test_write_to_file() -{ - u8 glyph_height = 1; - u8 glyph_width = 1; - auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); - - char path[] = "/tmp/new.font.XXXXXX"; - assert(mkstemp(path) != -1); - assert(font->write_to_file(path)); - unlink(path); -} - -int main(int, char**) -{ -#define RUNTEST(x) \ - { \ - printf("Running " #x " ...\n"); \ - x(); \ - printf("Success!\n"); \ - } - RUNTEST(test_fontdatabase_get); - RUNTEST(test_fontdatabase_get_by_name); - RUNTEST(test_fontdatabase_for_each_font); - RUNTEST(test_default_font); - RUNTEST(test_default_fixed_width_font); - RUNTEST(test_default_bold_fixed_width_font); - RUNTEST(test_default_bold_font); - RUNTEST(test_clone); - RUNTEST(test_set_name); - RUNTEST(test_set_family); - RUNTEST(test_set_type); - RUNTEST(test_set_glyph_width); - RUNTEST(test_set_glyph_spacing); - RUNTEST(test_width); - RUNTEST(test_glyph_or_emoji_width); - RUNTEST(test_load_from_file); - RUNTEST(test_write_to_file); - printf("PASS\n"); - - return 0; -}