From 4e5639d921ed567a2007088279c49244866255f1 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 6 Jun 2017 16:23:28 -0700 Subject: [PATCH] Add additional test framework and tests for Italics. Overloaded paragraph text entry methods. Change-Id: I0ae9abd6130edd276b1144a0092e03e47373427d --- engine/src/flutter/src/font_collection.cc | 16 +++++ engine/src/flutter/src/font_collection.h | 17 +++-- engine/src/flutter/src/paragraph.cc | 4 +- engine/src/flutter/src/paragraph.h | 8 +++ engine/src/flutter/src/paragraph_builder.cc | 15 ++++ engine/src/flutter/src/paragraph_builder.h | 4 ++ .../tests/txt/font_collection_unittests.cc | 11 +++ .../flutter/tests/txt/paragraph_unittests.cc | 70 +++++++++++++++++-- 8 files changed, 132 insertions(+), 13 deletions(-) diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index 6763617d71..da24410388 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -17,9 +17,12 @@ #include "lib/txt/src/font_collection.h" #include +#include +#include #include "lib/ftl/logging.h" #include "lib/txt/src/font_skia.h" +#include "third_party/skia/include/core/SkString.h" #include "third_party/skia/include/ports/SkFontMgr.h" #include "third_party/skia/include/ports/SkFontMgr_directory.h" @@ -36,6 +39,19 @@ FontCollection::FontCollection() = default; FontCollection::~FontCollection() = default; +std::set FontCollection::GetFamilyNames(const std::string& dir) { + auto skia_font_manager = dir.length() != 0 + ? SkFontMgr_New_Custom_Directory(dir.c_str()) + : SkFontMgr::RefDefault(); + std::set names; + SkString str; + for (int i = 0; i < skia_font_manager->countFamilies(); i++) { + skia_font_manager->getFamilyName(i, &str); + names.insert(std::string{str.writable_str()}); + } + return names; +} + const std::string FontCollection::ProcessFamilyName(const std::string& family) { return family.length() == 0 ? DEFAULT_FAMILY_NAME : family; } diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index b78de954d8..43bdbec16f 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -20,13 +20,14 @@ #define DEFAULT_FAMILY_NAME "Roboto" #include +#include #include #include #include "lib/ftl/macros.h" -#include "lib/txt/tests/txt/render_test.h" #include "minikin/FontCollection.h" #include "minikin/FontFamily.h" +#include "third_party/gtest/include/gtest/gtest_prod.h" namespace txt { @@ -41,17 +42,21 @@ class FontCollection { std::shared_ptr GetMinikinFontCollectionForFamily( const std::string& family, const std::string& dir = ""); - // Temporarily public. + + // Provides a vector of all available family names. + static std::set GetFamilyNames(const std::string& dir = ""); + + private: + FRIEND_TEST(FontCollection, HasDefaultRegistrations); + FRIEND_TEST(FontCollection, GetMinikinFontCollections); + FRIEND_TEST(FontCollection, GetFamilyNames); + const std::string ProcessFamilyName(const std::string& family); - // For Testing. Temporarily public. static const std::string GetDefaultFamilyName() { return DEFAULT_FAMILY_NAME; }; - private: - friend RenderTest; - // TODO(chinmaygarde): Caches go here. FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection); }; diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 44caeada36..ce2edaa866 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -60,13 +60,13 @@ int GetWeight(const TextStyle& style) { return 2; case FontWeight::w300: return 3; - case FontWeight::w400: + case FontWeight::w400: // Normal. return 4; case FontWeight::w500: return 5; case FontWeight::w600: return 6; - case FontWeight::w700: + case FontWeight::w700: // Bold. return 7; case FontWeight::w800: return 8; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index eee7f43583..0ec43df129 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -24,6 +24,7 @@ #include "lib/txt/src/paragraph_constraints.h" #include "lib/txt/src/styled_runs.h" #include "minikin/LineBreaker.h" +#include "third_party/gtest/include/gtest/gtest_prod.h" #include "third_party/skia/include/core/SkTextBlob.h" class SkCanvas; @@ -43,6 +44,13 @@ class Paragraph { private: friend class ParagraphBuilder; + FRIEND_TEST(RenderTest, SimpleParagraph); + FRIEND_TEST(RenderTest, SimpleRedParagraph); + FRIEND_TEST(RenderTest, RainbowParagraph); + FRIEND_TEST(RenderTest, DefaultStyleParagraph); + FRIEND_TEST(RenderTest, BoldParagraph); + FRIEND_TEST(RenderTest, LinebreakParagraph); + FRIEND_TEST(RenderTest, ItalicsParagraph); std::vector text_; StyledRuns runs_; diff --git a/engine/src/flutter/src/paragraph_builder.cc b/engine/src/flutter/src/paragraph_builder.cc index a6db3492a8..4976b86126 100644 --- a/engine/src/flutter/src/paragraph_builder.cc +++ b/engine/src/flutter/src/paragraph_builder.cc @@ -15,6 +15,7 @@ */ #include "lib/txt/src/paragraph_builder.h" +#include "third_party/icu/source/common/unicode/unistr.h" namespace txt { @@ -46,6 +47,20 @@ void ParagraphBuilder::AddText(const std::u16string& text) { text_.insert(text_.end(), text.begin(), text.end()); } +void ParagraphBuilder::AddText(const std::string& text) { + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + AddText(u16_text); +} + +void ParagraphBuilder::AddText(const char* text) { + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + AddText(u16_text); +} + std::unique_ptr ParagraphBuilder::Build() { runs_.EndRunIfNeeded(text_.size()); std::unique_ptr paragraph = std::make_unique(); diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h index 8c6941af9b..71598af0b5 100644 --- a/engine/src/flutter/src/paragraph_builder.h +++ b/engine/src/flutter/src/paragraph_builder.h @@ -40,6 +40,10 @@ class ParagraphBuilder { void AddText(const std::u16string& text); + void AddText(const std::string& text); + + void AddText(const char* text); + std::unique_ptr Build(); private: diff --git a/engine/src/flutter/tests/txt/font_collection_unittests.cc b/engine/src/flutter/tests/txt/font_collection_unittests.cc index 1851f04af9..ab15710e96 100644 --- a/engine/src/flutter/tests/txt/font_collection_unittests.cc +++ b/engine/src/flutter/tests/txt/font_collection_unittests.cc @@ -20,6 +20,8 @@ #include "lib/ftl/logging.h" #include "lib/txt/tests/txt/utils.h" +namespace txt { + TEST(FontCollection, HasDefaultRegistrations) { std::string defaultFamilyName = txt::FontCollection::GetDefaultFamilyName(); @@ -61,3 +63,12 @@ TEST(FontCollection, GetMinikinFontCollections) { ASSERT_NE(collectionHomemadeApple, collectionRoboto); ASSERT_NE(collectionDef.get(), nullptr); } + +TEST(FontCollection, GetFamilyNames) { + std::set names = + txt::FontCollection::GetFamilyNames(txt::GetFontDir()); + + ASSERT_EQ(names.size(), 19ull); +} + +} // namespace txt \ No newline at end of file diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 9aa842ee28..11b816241b 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -17,13 +17,15 @@ #include "lib/ftl/logging.h" #include "lib/txt/src/font_style.h" #include "lib/txt/src/font_weight.h" +#include "lib/txt/src/paragraph.h" #include "lib/txt/tests/txt/utils.h" -#include "paragraph.h" #include "paragraph_builder.h" #include "render_test.h" #include "third_party/icu/source/common/unicode/unistr.h" #include "third_party/skia/include/core/SkColor.h" +namespace txt { + TEST_F(RenderTest, SimpleParagraph) { const char* text = "Hello World"; auto icu_text = icu::UnicodeString::fromUTF8(text); @@ -46,6 +48,10 @@ TEST_F(RenderTest, SimpleParagraph) { paragraph->Paint(GetCanvas(), 10.0, 15.0); + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } ASSERT_TRUE(Snapshot()); } @@ -72,6 +78,10 @@ TEST_F(RenderTest, SimpleRedParagraph) { paragraph->Paint(GetCanvas(), 10.0, 15.0); + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } ASSERT_TRUE(Snapshot()); } @@ -108,7 +118,7 @@ TEST_F(RenderTest, RainbowParagraph) { builder.AddText(u16_text1); txt::TextStyle text_style2; - text_style2.font_size = 30; + text_style2.font_size = 50; // Letter spacing not yet implemented text_style2.letter_spacing = 100; text_style2.font_weight = txt::FontWeight::w600; @@ -142,8 +152,12 @@ TEST_F(RenderTest, RainbowParagraph) { paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, txt::GetFontDir()); - paragraph->Paint(GetCanvas(), 10.0, 30.0); + paragraph->Paint(GetCanvas(), 10.0, 50.0); + u16_text1 += u16_text2 + u16_text3 + u16_text4; + for (size_t i = 0; i < u16_text1.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text1[i]); + } ASSERT_TRUE(Snapshot()); } @@ -170,6 +184,10 @@ TEST_F(RenderTest, DefaultStyleParagraph) { paragraph->Paint(GetCanvas(), 10.0, 15.0); + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } ASSERT_TRUE(Snapshot()); } @@ -186,7 +204,7 @@ TEST_F(RenderTest, BoldParagraph) { text_style.font_size = 60; // Letter spacing not yet implemented text_style.letter_spacing = 10; - text_style.font_weight = txt::FontWeight::w900; + text_style.font_weight = txt::FontWeight::w700; text_style.fake_bold = true; text_style.color = SK_ColorRED; builder.PushStyle(text_style); @@ -201,6 +219,10 @@ TEST_F(RenderTest, BoldParagraph) { paragraph->Paint(GetCanvas(), 10.0, 60.0); + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } ASSERT_TRUE(Snapshot()); } @@ -257,5 +279,43 @@ TEST_F(RenderTest, LinebreakParagraph) { paragraph->Paint(GetCanvas(), 5.0, 30.0); + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } ASSERT_TRUE(Snapshot()); -} \ No newline at end of file +} + +TEST_F(RenderTest, ItalicsParagraph) { + const char* text = "I am Italicized!"; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + txt::ParagraphBuilder builder(paragraph_style); + + txt::TextStyle text_style; + text_style.color = SK_ColorRED; + text_style.font_style = txt::FontStyle::italic; + text_style.font_size = 35; + builder.PushStyle(text_style); + + builder.AddText(u16_text); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, + txt::GetFontDir()); + + paragraph->Paint(GetCanvas(), 10.0, 35.0); + + ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); + for (size_t i = 0; i < u16_text.length(); i++) { + ASSERT_EQ(paragraph->text_[i], u16_text[i]); + } + ASSERT_TRUE(Snapshot()); +} + +} // namespace txt