diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index ba95ca2cae830..09ff677814e17 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -560,7 +560,7 @@ class RuntimeController : public PlatformConfigurationClient { void RequestDartDeferredLibrary(intptr_t loading_unit_id) override; const fml::WeakPtr& GetIOManager() const { return io_manager_; } - DartVM* GetDartVM() const { return vm_; } + virtual DartVM* GetDartVM() const { return vm_; } const fml::RefPtr& GetIsolateSnapshot() const { return isolate_snapshot_; diff --git a/shell/common/BUILD.gn b/shell/common/BUILD.gn index c5b89c720d6ad..ef390e5d0649e 100644 --- a/shell/common/BUILD.gn +++ b/shell/common/BUILD.gn @@ -256,6 +256,7 @@ if (enable_unittests) { "//flutter/common/graphics", "//flutter/shell/profiling:profiling_unittests", "//flutter/shell/version", + "//flutter/testing:fixture_test", "//third_party/googletest:gmock", ] diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 1d194e4fc7341..c25511da130e0 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -43,6 +43,7 @@ Engine::Engine( Settings settings, std::unique_ptr animator, fml::WeakPtr io_manager, + const std::shared_ptr& font_collection, std::unique_ptr runtime_controller) : delegate_(delegate), settings_(std::move(settings)), @@ -50,6 +51,7 @@ Engine::Engine( runtime_controller_(std::move(runtime_controller)), activity_running_(true), have_surface_(false), + font_collection_(font_collection), image_decoder_(task_runners, image_decoder_task_runner, io_manager), task_runners_(std::move(task_runners)), weak_factory_(this) { @@ -75,6 +77,7 @@ Engine::Engine(Delegate& delegate, settings, std::move(animator), io_manager, + std::make_shared(), nullptr) { runtime_controller_ = std::make_unique( *this, // runtime delegate @@ -111,6 +114,7 @@ std::unique_ptr Engine::Spawn( /*settings=*/settings, /*animator=*/std::move(animator), /*io_manager=*/runtime_controller_->GetIOManager(), + /*font_collection=*/font_collection_, /*runtime_controller=*/nullptr); result->runtime_controller_ = runtime_controller_->Spawn( *result, // runtime delegate @@ -132,7 +136,7 @@ fml::WeakPtr Engine::GetWeakPtr() const { void Engine::SetupDefaultFontManager() { TRACE_EVENT0("flutter", "Engine::SetupDefaultFontManager"); - font_collection_.SetupDefaultFontManager(); + font_collection_->SetupDefaultFontManager(); } std::shared_ptr Engine::GetAssetManager() { @@ -152,10 +156,10 @@ bool Engine::UpdateAssetManager( } // Using libTXT as the text engine. - font_collection_.RegisterFonts(asset_manager_); + font_collection_->RegisterFonts(asset_manager_); if (settings_.use_test_fonts) { - font_collection_.RegisterTestFonts(); + font_collection_->RegisterTestFonts(); } return true; @@ -492,7 +496,7 @@ void Engine::SetNeedsReportTimings(bool needs_reporting) { } FontCollection& Engine::GetFontCollection() { - return font_collection_; + return *font_collection_; } void Engine::DoDispatchPacket(std::unique_ptr packet, diff --git a/shell/common/engine.h b/shell/common/engine.h index cfcab91e302c7..a5581611e5c31 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -297,6 +297,7 @@ class Engine final : public RuntimeDelegate, Settings settings, std::unique_ptr animator, fml::WeakPtr io_manager, + const std::shared_ptr& font_collection, std::unique_ptr runtime_controller); //---------------------------------------------------------------------------- @@ -888,7 +889,7 @@ class Engine final : public RuntimeDelegate, std::shared_ptr asset_manager_; bool activity_running_; bool have_surface_; - FontCollection font_collection_; + std::shared_ptr font_collection_; ImageDecoder image_decoder_; TaskRunners task_runners_; size_t hint_freed_bytes_since_last_idle_ = 0; diff --git a/shell/common/engine_unittests.cc b/shell/common/engine_unittests.cc index 92e1ab47f912a..e2a9996c6b4a6 100644 --- a/shell/common/engine_unittests.cc +++ b/shell/common/engine_unittests.cc @@ -8,6 +8,7 @@ #include "flutter/runtime/dart_vm_lifecycle.h" #include "flutter/shell/common/thread_host.h" +#include "flutter/testing/fixture_test.h" #include "flutter/testing/testing.h" #include "gmock/gmock.h" #include "rapidjson/document.h" @@ -67,6 +68,7 @@ class MockRuntimeController : public RuntimeController { MOCK_METHOD1(DispatchPlatformMessage, bool(fml::RefPtr)); MOCK_METHOD3(LoadDartDeferredLibraryError, void(intptr_t, const std::string, bool)); + MOCK_CONST_METHOD0(GetDartVM, DartVM*()); }; fml::RefPtr MakePlatformMessage( @@ -95,7 +97,7 @@ fml::RefPtr MakePlatformMessage( return message; } -class EngineTest : public ::testing::Test { +class EngineTest : public testing::FixtureTest { public: EngineTest() : thread_host_("EngineTest", @@ -120,6 +122,7 @@ class EngineTest : public ::testing::Test { protected: void SetUp() override { + settings_ = CreateSettingsForFixture(); dispatcher_maker_ = [](PointerDataDispatcher::Delegate&) { return nullptr; }; @@ -147,6 +150,7 @@ TEST_F(EngineTest, Create) { /*settings=*/settings_, /*animator=*/std::move(animator_), /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), /*runtime_controller=*/std::move(runtime_controller_)); EXPECT_TRUE(engine); }); @@ -167,6 +171,7 @@ TEST_F(EngineTest, DispatchPlatformMessageUnknown) { /*settings=*/settings_, /*animator=*/std::move(animator_), /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), /*runtime_controller=*/std::move(mock_runtime_controller)); fml::RefPtr response = @@ -192,6 +197,7 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRoute) { /*settings=*/settings_, /*animator=*/std::move(animator_), /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), /*runtime_controller=*/std::move(mock_runtime_controller)); fml::RefPtr response = @@ -224,6 +230,7 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRouteIgnored) { /*settings=*/settings_, /*animator=*/std::move(animator_), /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), /*runtime_controller=*/std::move(mock_runtime_controller)); fml::RefPtr response = @@ -239,6 +246,32 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRouteIgnored) { }); } +TEST_F(EngineTest, SpawnSharesFontLibrary) { + PostUITaskSync([this] { + MockRuntimeDelegate client; + auto mock_runtime_controller = + std::make_unique(client, task_runners_); + auto vm_ref = DartVMRef::Create(settings_); + EXPECT_CALL(*mock_runtime_controller, GetDartVM()) + .WillRepeatedly(::testing::Return(vm_ref.get())); + auto engine = std::make_unique( + /*delegate=*/delegate_, + /*dispatcher_maker=*/dispatcher_maker_, + /*image_decoder_task_runner=*/image_decoder_task_runner_, + /*task_runners=*/task_runners_, + /*settings=*/settings_, + /*animator=*/std::move(animator_), + /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), + /*runtime_controller=*/std::move(mock_runtime_controller)); + + auto spawn = + engine->Spawn(delegate_, dispatcher_maker_, settings_, nullptr); + EXPECT_TRUE(spawn != nullptr); + EXPECT_EQ(&engine->GetFontCollection(), &spawn->GetFontCollection()); + }); +} + TEST_F(EngineTest, PassesLoadDartDeferredLibraryErrorToRuntime) { PostUITaskSync([this] { intptr_t error_id = 123; @@ -259,6 +292,7 @@ TEST_F(EngineTest, PassesLoadDartDeferredLibraryErrorToRuntime) { /*settings=*/settings_, /*animator=*/std::move(animator_), /*io_manager=*/io_manager_, + /*font_collection=*/std::make_shared(), /*runtime_controller=*/std::move(mock_runtime_controller)); engine->LoadDartDeferredLibraryError(error_id, error_message, true);