From 482348524544b0c7ebfe856151e233d3d2cb5092 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 15 Jun 2017 12:13:14 -0700 Subject: [PATCH] Restructure Building of tests and FontCollection Change-Id: Ifd7d9cd505b119684ccb42c998204d41ee7ef93e --- engine/src/flutter/src/font_collection.cc | 15 +++++--- engine/src/flutter/src/font_collection.h | 9 +++-- engine/src/flutter/src/paragraph.cc | 8 ++-- engine/src/flutter/tests/BUILD.gn | 22 +++++++++++ .../tests/txt/font_collection_unittests.cc | 37 +++++++++---------- 5 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 engine/src/flutter/tests/BUILD.gn diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index e7b55875bd..05b5d6b66f 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -28,14 +28,18 @@ namespace txt { -FontCollection& FontCollection::GetDefaultFontCollection() { +FontCollection& FontCollection::GetFontCollection(std::string dir) { static FontCollection* collection = nullptr; static std::once_flag once; - std::call_once(once, []() { collection = new FontCollection(); }); + std::call_once(once, [dir]() { collection = new FontCollection(dir); }); return *collection; } -FontCollection::FontCollection() { +FontCollection& FontCollection::GetDefaultFontCollection() { + return GetFontCollection(""); +} + +FontCollection::FontCollection(std::string dir) { #ifdef DIRECTORY_FONT_MANAGER_AVAILABLE skia_font_manager_ = dir.length() != 0 ? SkFontMgr_New_Custom_Directory(dir.c_str()) @@ -47,7 +51,7 @@ FontCollection::FontCollection() { FontCollection::~FontCollection() = default; -std::set FontCollection::GetFamilyNames(const std::string& dir) { +std::set FontCollection::GetFamilyNames() { std::set names; SkString str; for (int i = 0; i < skia_font_manager_->countFamilies(); i++) { @@ -72,8 +76,7 @@ const std::string FontCollection::ProcessFamilyName(const std::string& family) { } std::shared_ptr -FontCollection::GetMinikinFontCollectionForFamily(const std::string& family, - const std::string& dir) { +FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { FTL_DCHECK(skia_font_manager_ != nullptr); // Ask Skia to resolve a font style set for a font family name. diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index 531ef23e48..836e22e8ff 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -37,12 +37,13 @@ class FontCollection { public: static FontCollection& GetDefaultFontCollection(); + static FontCollection& GetFontCollection(std::string dir = ""); + std::shared_ptr GetMinikinFontCollectionForFamily( - const std::string& family, - const std::string& dir = ""); + const std::string& family); // Provides a set of all available family names. - std::set GetFamilyNames(const std::string& dir = ""); + std::set GetFamilyNames(); private: sk_sp skia_font_manager_; @@ -57,7 +58,7 @@ class FontCollection { return DEFAULT_FAMILY_NAME; }; - FontCollection(); + FontCollection(std::string dir = ""); ~FontCollection(); diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index fd4ed256c3..8f45e9f6b6 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -121,8 +121,8 @@ void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) { for (size_t i = 0; i < runs_.size(); ++i) { auto run = runs_.GetRun(i); auto collection = - FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily(run.style.font_family, rootdir); + FontCollection::GetFontCollection(rootdir) + .GetMinikinFontCollectionForFamily(run.style.font_family); GetFontAndMinikinPaint(run.style, &font, &paint); breaker_.addStyleRun(&paint, collection, font, run.start, run.end, false); } @@ -154,8 +154,8 @@ void Paragraph::Layout(const ParagraphConstraints& constraints, for (size_t run_index = 0; run_index < runs_.size(); ++run_index) { auto run = runs_.GetRun(run_index); auto collection = - FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily(run.style.font_family, rootdir); + FontCollection::GetFontCollection(rootdir) + .GetMinikinFontCollectionForFamily(run.style.font_family); GetFontAndMinikinPaint(run.style, &font, &minikin_paint); GetPaint(run.style, &paint); diff --git a/engine/src/flutter/tests/BUILD.gn b/engine/src/flutter/tests/BUILD.gn new file mode 100644 index 0000000000..98defad50b --- /dev/null +++ b/engine/src/flutter/tests/BUILD.gn @@ -0,0 +1,22 @@ +# 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. + +group("tests") { + testonly = true + + deps = [ + "txt", + "unittest", + ] +} \ No newline at end of file diff --git a/engine/src/flutter/tests/txt/font_collection_unittests.cc b/engine/src/flutter/tests/txt/font_collection_unittests.cc index 4a069a3661..f501649a1c 100644 --- a/engine/src/flutter/tests/txt/font_collection_unittests.cc +++ b/engine/src/flutter/tests/txt/font_collection_unittests.cc @@ -25,33 +25,31 @@ namespace txt { TEST(FontCollection, HasDefaultRegistrations) { std::string defaultFamilyName = txt::FontCollection::GetDefaultFamilyName(); - auto collection = - txt::FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily("", txt::GetFontDir()); - ASSERT_EQ( - defaultFamilyName, - txt::FontCollection::GetDefaultFontCollection().ProcessFamilyName("")); + auto collection = txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .GetMinikinFontCollectionForFamily(""); + ASSERT_EQ(defaultFamilyName, + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .ProcessFamilyName("")); ASSERT_NE(defaultFamilyName, - txt::FontCollection::GetDefaultFontCollection().ProcessFamilyName( - "NotARealFont!")); + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .ProcessFamilyName("NotARealFont!")); ASSERT_EQ("NotARealFont!", - txt::FontCollection::GetDefaultFontCollection().ProcessFamilyName( - "NotARealFont!")); + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .ProcessFamilyName("NotARealFont!")); ASSERT_NE(collection.get(), nullptr); } TEST(FontCollection, GetMinikinFontCollections) { std::string defaultFamilyName = txt::FontCollection::GetDefaultFamilyName(); - auto collectionDef = - txt::FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily("", txt::GetFontDir()); + auto collectionDef = txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .GetMinikinFontCollectionForFamily(""); auto collectionRoboto = - txt::FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily("Roboto", txt::GetFontDir()); - auto collectionHomemadeApple = txt::FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily( - "Homemade Apple", txt::GetFontDir()); + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .GetMinikinFontCollectionForFamily("Roboto"); + auto collectionHomemadeApple = + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .GetMinikinFontCollectionForFamily("Homemade Apple"); for (size_t base = 0; base < 50; base++) { for (size_t variation = 0; variation < 50; variation++) { ASSERT_EQ(collectionDef->hasVariationSelector(base, variation), @@ -66,7 +64,8 @@ TEST(FontCollection, GetMinikinFontCollections) { TEST(FontCollection, GetFamilyNames) { std::set names = - txt::FontCollection::GetFamilyNames(txt::GetFontDir()); + txt::FontCollection::GetFontCollection(txt::GetFontDir()) + .GetFamilyNames(); ASSERT_EQ(names.size(), 19ull);