Skip to content

Commit

Permalink
Add option to desktop test shells to use an embedded font for consist…
Browse files Browse the repository at this point in the history
…ent unit tests. (flutter#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.
  • Loading branch information
chinmaygarde authored Dec 9, 2016
1 parent 9eeed70 commit e133b4f
Show file tree
Hide file tree
Showing 11 changed files with 1,371 additions and 6 deletions.
1 change: 1 addition & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
Expand Down
1,228 changes: 1,228 additions & 0 deletions runtime/test_font_data.cc

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions runtime/test_font_data.h
Original file line number Diff line number Diff line change
@@ -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_
50 changes: 50 additions & 0 deletions runtime/test_font_selector.cc
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions runtime/test_font_selector.h
Original file line number Diff line number Diff line change
@@ -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_
8 changes: 7 additions & 1 deletion shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {}
Expand Down
4 changes: 0 additions & 4 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
7 changes: 7 additions & 0 deletions shell/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion travis/licenses.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

0 comments on commit e133b4f

Please sign in to comment.