From d6bf0fc362f9ee42293edfd016db9b751395096e Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:49:03 -0700 Subject: [PATCH] setupDefultFontManager correctly clear out cache (flutter/engine#42178) fixes https://github.com/flutter/flutter/issues/123483 Not entirely sure if this really fix the flake as I can't reproduce locally. My guess to the flake is that the setDefaultfontmgr should only be called once during the life time of the app. The setDefaultfontmgr can be remove at the call site, since it is already called during setup [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- engine/src/flutter/third_party/txt/BUILD.gn | 8 +++- .../txt/src/txt/font_collection.cc | 1 + .../txt/tests/font_collection_tests.cc | 43 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/third_party/txt/tests/font_collection_tests.cc diff --git a/engine/src/flutter/third_party/txt/BUILD.gn b/engine/src/flutter/third_party/txt/BUILD.gn index 6abab7cba0..1f57ef313a 100644 --- a/engine/src/flutter/third_party/txt/BUILD.gn +++ b/engine/src/flutter/third_party/txt/BUILD.gn @@ -141,11 +141,17 @@ if (enable_unittests) { executable("txt_unittests") { testonly = true - sources = [ "tests/txt_run_all_unittests.cc" ] + sources = [ + "tests/font_collection_tests.cc", + "tests/txt_run_all_unittests.cc", + ] + + public_configs = [ ":txt_config" ] configs += [ ":allow_posix_names" ] deps = [ + ":txt", ":txt_fixtures", "//flutter/fml", "//flutter/testing:testing_lib", diff --git a/engine/src/flutter/third_party/txt/src/txt/font_collection.cc b/engine/src/flutter/third_party/txt/src/txt/font_collection.cc index 9d1e8b51be..5cef503cb8 100644 --- a/engine/src/flutter/third_party/txt/src/txt/font_collection.cc +++ b/engine/src/flutter/third_party/txt/src/txt/font_collection.cc @@ -46,6 +46,7 @@ size_t FontCollection::GetFontManagersCount() const { void FontCollection::SetupDefaultFontManager( uint32_t font_initialization_data) { default_font_manager_ = GetDefaultFontManager(font_initialization_data); + skt_collection_.reset(); } void FontCollection::SetDefaultFontManager(sk_sp font_manager) { diff --git a/engine/src/flutter/third_party/txt/tests/font_collection_tests.cc b/engine/src/flutter/third_party/txt/tests/font_collection_tests.cc new file mode 100644 index 0000000000..051d378de1 --- /dev/null +++ b/engine/src/flutter/third_party/txt/tests/font_collection_tests.cc @@ -0,0 +1,43 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" + +#include + +#include "txt/font_collection.h" + +namespace txt { +namespace testing { + +class FontCollectionTests : public ::testing::Test { + public: + FontCollectionTests() {} + + void SetUp() override {} +}; + +TEST_F(FontCollectionTests, SettingUpDefaultFontManagerClearsCache) { + FontCollection font_collection; + sk_sp sk_font_collection = + font_collection.CreateSktFontCollection(); + ASSERT_EQ(sk_font_collection->getFallbackManager().get(), nullptr); + font_collection.SetupDefaultFontManager(0); + sk_font_collection = font_collection.CreateSktFontCollection(); + ASSERT_NE(sk_font_collection->getFallbackManager().get(), nullptr); +} +} // namespace testing +} // namespace txt