Commit 7d8b5f28 authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Add tests to font service

Add testing for font service to test methods on FontLoader. FontLoader
will be extended to carry additional FontConfig IPC methods for font
fallback, querying render style for strike, and performing PPAPI
specific font functions. (See "FontService replacing Fonts Sandbox IPC"
design doc [1] and issue [2]).

Before doing that, let's ensure we have a framework to test these
methods. As the font service runs in a child utility process, we need a
way to ensure that it is set up for loading the custom FontConfig
configuration that we use for consistent font access in testing. To that
end, pass a command line feature enabled flag to the child process, and
adding while setting up the TestSuite.

Test basic matching, test failed matching, and test matching the
empty font name.

[1] https://docs.google.com/document/d/1dLBekS3RlcQFldeYZlzFGkuLyX53GBr_7Hbf4cUz1OE/
[2] crbug.com/839344

Bug: 849923
Change-Id: Ib3118d4a53b075725cbdc7fa89ba4f2b9eec8d5e
Reviewed-on: https://chromium-review.googlesource.com/1091754Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarThomas Anderson <thomasanderson@chromium.org>
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569387}
parent d9e4a995
......@@ -14,6 +14,12 @@
#include "base/path_service.h"
#include "base/strings/string_util.h"
namespace switches {
// Indicate to child processes that they should set up the FontConfig testing
// environment.
const char kFontConfigTestingEnvironment[] = "fontconfig-testing-environment";
}; // namespace switches
namespace base {
namespace {
......
......@@ -5,6 +5,10 @@
#ifndef BASE_TEST_FONTCONFIG_UTIL_LINUX_H_
#define BASE_TEST_FONTCONFIG_UTIL_LINUX_H_
namespace switches {
extern const char kFontConfigTestingEnvironment[];
};
namespace base {
// Initializes Fontconfig with a custom configuration suitable for tests.
......
......@@ -411,6 +411,11 @@ void TestSuite::Initialize() {
switches.erase(switches::kEnableFeatures);
switches.erase(switches::kDisableFeatures);
#if defined(OS_LINUX)
// Pass to child processes that we need the FontConfig testing environment.
switches[switches::kFontConfigTestingEnvironment] = "";
#endif
for (const auto& iter : switches)
new_command_line.AppendSwitchNative(iter.first, iter.second);
......
......@@ -4,6 +4,8 @@
import("//services/service_manager/public/cpp/service.gni")
import("//services/service_manager/public/service_manifest.gni")
import("//services/service_manager/public/tools/test/service_test.gni")
import("//testing/test.gni")
source_set("lib") {
sources = [
......@@ -13,6 +15,7 @@ source_set("lib") {
deps = [
"//base",
"//base/test:fontconfig_util_linux",
"//components/services/font/public/interfaces",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/system",
......@@ -41,3 +44,38 @@ service_manifest("manifest") {
name = "font_service"
source = "manifest.json"
}
service_test("font_service_unittests") {
sources = [
"font_loader_test.cc",
"font_loader_test.h",
]
catalog = ":font_service_unittests_catalog"
deps = [
"//base",
"//base/test:fontconfig_util_linux",
"//components/services/font/public/cpp",
"//components/services/font/public/interfaces",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/system",
"//services/service_manager/public/cpp",
"//services/service_manager/public/cpp:service_test_support",
"//skia",
]
data_deps = [
":font_service",
]
}
service_manifest("test_manifest") {
name = "font_service_unittests"
source = "test_manifest.json"
}
catalog("font_service_unittests_catalog") {
embedded_services = [ ":test_manifest" ]
standalone_services = [ ":manifest" ]
}
erg@chromium.org
drott@chromium.org
per-file manifest.json=set noparent
per-file manifest.json=file://ipc/SECURITY_OWNERS
per-file test_manifest.json=set noparent
per-file test_manifest.json=file://ipc/SECURITY_OWNERS
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/services/font/font_loader_test.h"
#include <utility>
#include "base/run_loop.h"
#include "base/test/fontconfig_util_linux.h"
#include "components/services/font/public/interfaces/constants.mojom.h"
#include "components/services/font/public/interfaces/font_service.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
#include "third_party/skia/include/core/SkFontStyle.h"
namespace {
bool IsInTestFontDirectory(const char* path) {
const char kTestFontsDir[] = "test_fonts";
return std::string(path).find(kTestFontsDir) != std::string::npos;
}
} // namespace
namespace font_service {
FontLoaderTest::FontLoaderTest() : ServiceTest("font_service_unittests") {}
FontLoaderTest::~FontLoaderTest() {}
void FontLoaderTest::SetUp() {
ServiceTest::SetUp();
font_loader_ = std::make_unique<FontLoader>(connector());
}
TEST_F(FontLoaderTest, BasicMatchingTest) {
SkFontStyle styles[] = {
SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
SkFontStyle::kUpright_Slant),
SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width,
SkFontStyle::kUpright_Slant),
SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width,
SkFontStyle::kItalic_Slant)};
// See kFontsConfTemplate[] in fontconfig_util_linux.cc for details of which
// fonts can be picked. Arial, Times New Roman and Courier New are aliased to
// Arimos, Tinos and Cousine ChromeOS open source alternatives when FontConfig
// is set up for testing.
std::vector<std::vector<std::string>> family_names_expected_names = {
{"Arial", "Arimo"},
{"Times New Roman", "Tinos"},
{"Courier New", "Cousine"}};
for (std::vector<std::string> request_family_name :
family_names_expected_names) {
for (auto& request_style : styles) {
SkFontConfigInterface::FontIdentity font_identity;
SkFontStyle result_style;
SkString result_family_name;
font_loader()->matchFamilyName(request_family_name[0].c_str(),
request_style, &font_identity,
&result_family_name, &result_style);
EXPECT_EQ(request_family_name[1],
std::string(result_family_name.c_str()));
EXPECT_TRUE(IsInTestFontDirectory(font_identity.fString.c_str()));
EXPECT_EQ(result_style, request_style);
}
}
}
TEST_F(FontLoaderTest, NotFoundTest) {
std::string request_family_name = {"IMPROBABLE_FONT_NAME"};
SkFontConfigInterface::FontIdentity font_identity;
SkFontStyle result_style;
SkString result_family_name;
font_loader()->matchFamilyName(request_family_name.c_str(), SkFontStyle(),
&font_identity, &result_family_name,
&result_style);
EXPECT_EQ("", std::string(result_family_name.c_str()));
EXPECT_EQ(0u, font_identity.fID);
EXPECT_EQ("", std::string(font_identity.fString.c_str()));
}
TEST_F(FontLoaderTest, EmptyFontName) {
std::string request_family_name = {""};
std::string kDefaultFontName = "DejaVu Sans";
SkFontConfigInterface::FontIdentity font_identity;
SkFontStyle result_style;
SkString result_family_name;
font_loader()->matchFamilyName(request_family_name.c_str(), SkFontStyle(),
&font_identity, &result_family_name,
&result_style);
EXPECT_EQ(kDefaultFontName, std::string(result_family_name.c_str()));
EXPECT_TRUE(IsInTestFontDirectory(font_identity.fString.c_str()));
}
} // namespace font_service
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SERVICES_FONT_FONT_LOADER_TEST_H_
#define COMPONENTS_SERVICES_FONT_FONT_LOADER_TEST_H_
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "components/services/font/public/cpp/font_loader.h"
#include "components/services/font/public/interfaces/font_service.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/service_manager/public/cpp/service_test.h"
namespace font_service {
class FontLoaderTest : public service_manager::test::ServiceTest {
public:
FontLoaderTest();
~FontLoaderTest() override;
// Overridden from service_manager::test::ServiceTest:
void SetUp() override;
protected:
FontLoader* font_loader() { return font_loader_.get(); }
private:
std::unique_ptr<FontLoader> font_loader_;
DISALLOW_COPY_AND_ASSIGN(FontLoaderTest);
};
} // namespace font_service
#endif // COMPONENTS_SERVICES_FONT_FONT_LOADER_TEST_H_
......@@ -6,8 +6,10 @@
#include <utility>
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/test/fontconfig_util_linux.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "services/service_manager/public/cpp/service_context.h"
......@@ -35,16 +37,31 @@ base::File GetFileForPath(const base::FilePath& path) {
return file;
}
bool FontConfigTestingEnvironmentEnabled() {
return base::CommandLine::ForCurrentProcess()->GetSwitches().count(
switches::kFontConfigTestingEnvironment);
}
} // namespace
namespace font_service {
FontServiceApp::FontServiceApp() {
// TODO(thomasanderson) https://crbug.com/831146: Remove this once a reland of
// CL https://chromium-review.googlesource.com/c/chromium/src/+/1009071 lands,
// which propagates the FONTCONFIG_FILE environment variable to subprocesses
// and thus ensures, child/utility processes receive the correct fontconfig
// testing environment.
if (FontConfigTestingEnvironmentEnabled())
base::SetUpFontconfig();
registry_.AddInterface(
base::Bind(&FontServiceApp::Create, base::Unretained(this)));
}
FontServiceApp::~FontServiceApp() {}
FontServiceApp::~FontServiceApp() {
if (FontConfigTestingEnvironmentEnabled())
base::TearDownFontconfig();
}
void FontServiceApp::OnStart() {}
......
......@@ -5,10 +5,7 @@
"interface_provider_specs": {
"service_manager:connector": {
"provides": {
"app": [ "font_service.mojom.FontService" ]
},
"requires": {
"*": [ "app" ]
"font_service": [ "font_service.mojom.FontService" ]
}
}
}
......
{
"name": "font_service_unittests",
"display_name": "Font Service Unittests",
"interface_provider_specs": {
"service_manager:connector": {
"requires": {
"font_service": [ "font_service" ]
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment