Add option to desktop test shells to use an embedded font for consistent unit tests. (flutter/engine#3301)

* This allows the tests to add their own FLX files but still use consistent fonts.
* The test fonts are only embedded on the desktop test shells. The option is not available on mobile platforms.
* Right now, all fonts will resolve to the test font. If we want tests to be able to use the fonts they embed in FLX files but use the test font for platform fallbacks, we will need to add font selector fallbacks. I can do this in an another patch. So far, there are no users of this functionality.
This commit is contained in:
Chinmay Garde
2016-12-09 14:47:49 -08:00
committed by GitHub
parent 51a86fd1fd
commit 6f3487f5f6
10 changed files with 1366 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ struct Settings {
bool trace_startup = false;
bool endless_trace_buffer = false;
bool enable_dart_profiling = false;
bool use_test_fonts = false;
std::string aot_snapshot_path;
std::string aot_isolate_snapshot_file_name;
std::string aot_vm_isolate_snapshot_file_name;

View File

@@ -60,6 +60,10 @@ source_set("runtime") {
"runtime_init.h",
"start_up.cc",
"start_up.h",
"test_font_data.cc",
"test_font_data.h",
"test_font_selector.cc",
"test_font_selector.h",
]
deps = [
@@ -79,6 +83,14 @@ source_set("runtime") {
":embedded_resources_cc",
]
defines = []
if (current_toolchain == host_toolchain) {
# Though the test font data is small, we dont want to add to the binary size
# on the device. We only add the same on the host test shells.
defines += ["EMBED_TEST_FONT_DATA=1"]
}
if (flutter_runtime_mode != "release") {
deps += [
"//lib/tonic/debugger",

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
// Copyright 2016 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 FLUTTER_RUNTIME_TEST_FONT_DATA_H_
#define FLUTTER_RUNTIME_TEST_FONT_DATA_H_
#include <memory>
#include "third_party/skia/include/core/SkStream.h"
namespace blink {
std::unique_ptr<SkStreamAsset> GetTestFontData();
} // namespace blink
#endif // FLUTTER_RUNTIME_TEST_FONT_DATA_H_

View File

@@ -0,0 +1,50 @@
// Copyright 2016 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 "flutter/runtime/test_font_selector.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/runtime/test_font_data.h"
#include "flutter/sky/engine/platform/fonts/SimpleFontData.h"
#include "third_party/skia/include/core/SkTypeface.h"
namespace blink {
void TestFontSelector::Install() {
auto font_selector = adoptRef(new TestFontSelector());
UIDartState::Current()->set_font_selector(font_selector);
}
TestFontSelector::TestFontSelector() = default;
TestFontSelector::~TestFontSelector() = default;
PassRefPtr<FontData> TestFontSelector::getFontData(
const FontDescription&,
const AtomicString& familyName) {
if (test_font_data_ != nullptr) {
return test_font_data_;
}
auto typeface = SkTypeface::MakeFromStream(GetTestFontData().get());
FontPlatformData platform_data(typeface, "Ahem", 14.0, false, false,
FontOrientation::Horizontal, false);
test_font_data_ =
SimpleFontData::create(platform_data, CustomFontData::create());
return test_font_data_;
}
void TestFontSelector::willUseFontData(const FontDescription&,
const AtomicString& familyName,
UChar32) {}
unsigned TestFontSelector::version() const {
return 0;
}
void TestFontSelector::fontCacheInvalidated() {}
} // namespace blink

View File

@@ -0,0 +1,41 @@
// Copyright 2016 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 FLUTTER_RUNTIME_TEST_FONT_SELECTOR_H_
#define FLUTTER_RUNTIME_TEST_FONT_SELECTOR_H_
#include "flutter/sky/engine/platform/fonts/FontSelector.h"
#include "flutter/sky/engine/wtf/RefPtr.h"
#include "lib/ftl/macros.h"
namespace blink {
class TestFontSelector : public FontSelector {
public:
static void Install();
~TestFontSelector() override;
PassRefPtr<FontData> getFontData(const FontDescription&,
const AtomicString& familyName) override;
void willUseFontData(const FontDescription&,
const AtomicString& familyName,
UChar32) override;
unsigned version() const override;
void fontCacheInvalidated() override;
private:
WTF::RefPtr<FontData> test_font_data_;
TestFontSelector();
FTL_DISALLOW_COPY_AND_ASSIGN(TestFontSelector);
};
} // namespace blink
#endif // FLUTTER_RUNTIME_TEST_FONT_SELECTOR_H_

View File

@@ -6,17 +6,20 @@
#include <sys/stat.h>
#include <unistd.h>
#include <memory>
#include <utility>
#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/assets/unzipper_provider.h"
#include "flutter/assets/zip_asset_store.h"
#include "flutter/common/settings.h"
#include "flutter/common/threads.h"
#include "flutter/glue/trace_event.h"
#include "flutter/runtime/asset_font_selector.h"
#include "flutter/runtime/dart_controller.h"
#include "flutter/runtime/dart_init.h"
#include "flutter/runtime/runtime_init.h"
#include "flutter/runtime/test_font_selector.h"
#include "flutter/shell/common/animator.h"
#include "flutter/shell/common/platform_view.h"
#include "flutter/sky/engine/public/web/Sky.h"
@@ -305,8 +308,11 @@ void Engine::ConfigureRuntime(const std::string& script_uri) {
}
void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
if (asset_store_)
if (blink::Settings::Get().use_test_fonts) {
blink::TestFontSelector::Install();
} else if (asset_store_) {
blink::AssetFontSelector::Install(asset_store_);
}
}
void Engine::DidCreateSecondaryIsolate(Dart_Isolate isolate) {}

View File

@@ -95,21 +95,17 @@ class Engine : public blink::RuntimeDelegate {
ftl::WeakPtr<PlatformView> platform_view_;
std::unique_ptr<Animator> animator_;
std::unique_ptr<blink::RuntimeController> runtime_;
ftl::RefPtr<blink::PlatformMessage> pending_push_route_message_;
blink::ViewportMetrics viewport_metrics_;
std::string language_code_;
std::string country_code_;
bool semantics_enabled_ = false;
// TODO(abarth): Unify these two behind a common interface.
ftl::RefPtr<blink::ZipAssetStore> asset_store_;
std::unique_ptr<blink::DirectoryAssetBundle> directory_asset_bundle_;
// TODO(eseidel): This should move into an AnimatorStateMachine.
bool activity_running_;
bool have_surface_;
ftl::WeakPtrFactory<Engine> weak_factory_;
FTL_DISALLOW_COPY_AND_ASSIGN(Engine);

View File

@@ -178,6 +178,9 @@ void Shell::InitStandalone(std::string icu_data_path,
settings.temp_directory_path =
command_line.GetSwitchValueASCII(FlagForSwitch(Switch::CacheDirPath));
settings.use_test_fonts =
command_line.HasSwitch(FlagForSwitch(Switch::UseTestFonts));
if (command_line.HasSwitch(FlagForSwitch(Switch::DartFlags))) {
std::stringstream stream(
command_line.GetSwitchValueNative(FlagForSwitch(Switch::DartFlags)));

View File

@@ -73,6 +73,13 @@ DEF_SWITCH(TraceStartup,
"trace-startup",
"Trace early application lifecycle. Automatically switches to an "
"endless trace buffer.")
DEF_SWITCH(UseTestFonts,
"use-test-fonts",
"Running tests that layout and measure text will not yield "
"consistent results across various platforms. Enabling this option "
"will make font resolution default to the Ahem test font on all "
"platforms (See https://www.w3.org/Style/CSS/Test/Fonts/Ahem/). "
"This option is only available on the desktop test shells.")
DEF_SWITCHES_END
void PrintUsage(const std::string& executable_name);