提交 e133b4fb 编写于 作者: C Chinmay Garde 提交者: GitHub

Add option to desktop test shells to use an embedded font for consistent unit tests. (#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.
上级 9eeed701
......@@ -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;
......
......@@ -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",
......
此差异已折叠。
// 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_
// 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
// 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_
......@@ -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) {}
......
......@@ -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);
......
......@@ -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)));
......
......@@ -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);
......
......@@ -14229,6 +14229,10 @@ FILE: ../../../flutter/runtime/runtime_init.cc
FILE: ../../../flutter/runtime/runtime_init.h
FILE: ../../../flutter/runtime/start_up.cc
FILE: ../../../flutter/runtime/start_up.h
FILE: ../../../flutter/runtime/test_font_data.cc
FILE: ../../../flutter/runtime/test_font_data.h
FILE: ../../../flutter/runtime/test_font_selector.cc
FILE: ../../../flutter/runtime/test_font_selector.h
FILE: ../../../flutter/shell/common/diagnostic/diagnostic_server.cc
FILE: ../../../flutter/shell/common/diagnostic/diagnostic_server.dart
FILE: ../../../flutter/shell/common/diagnostic/diagnostic_server.h
......@@ -72012,4 +72016,4 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
====================================================================================================
Total license count: 688
31619 of 31619 ██████████ 100% (0 missing licenses) Done.
31623 of 31623 ██████████ 100% (0 missing licenses) Done.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册