1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/gmock/include/gmock/gmock.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/blink/renderer/core/css/resolver/font_style_resolver.h"
8 #include "third_party/blink/renderer/core/css/threaded/multi_threaded_test_util.h"
9 #include "third_party/blink/renderer/platform/fonts/font.h"
10 #include "third_party/blink/renderer/platform/fonts/font_custom_platform_data.h"
11 #include "third_party/blink/renderer/platform/fonts/font_description.h"
12 #include "third_party/blink/renderer/platform/fonts/font_selector.h"
13 #include "third_party/blink/renderer/platform/fonts/shaping/caching_word_shape_iterator.h"
14 #include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_shaper.h"
15 #include "third_party/blink/renderer/platform/fonts/text_run_paint_info.h"
16 #include "third_party/blink/renderer/platform/graphics/test/mock_paint_canvas.h"
17 #include "third_party/blink/renderer/platform/language.h"
18 #include "third_party/blink/renderer/platform/testing/font_test_helpers.h"
19 #include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
20 #include "third_party/blink/renderer/platform/text/text_direction.h"
21 
22 using testing::_;
23 using testing::Return;
24 
25 using blink::test::CreateTestFont;
26 
27 namespace blink {
28 
TSAN_TEST(TextRendererThreadedTest,MeasureText)29 TSAN_TEST(TextRendererThreadedTest, MeasureText) {
30   RunOnThreads([]() {
31     String text = "measure this";
32 
33     FontDescription font_description;
34     font_description.SetComputedSize(12.0);
35     font_description.SetLocale(LayoutLocale::Get("en"));
36     ASSERT_EQ(USCRIPT_LATIN, font_description.GetScript());
37     font_description.SetGenericFamily(FontDescription::kStandardFamily);
38 
39     Font font = Font(font_description);
40 
41     const SimpleFontData* font_data = font.PrimaryFont();
42     ASSERT_TRUE(font_data);
43 
44     TextRun text_run(
45         text, 0, 0,
46         TextRun::kAllowTrailingExpansion | TextRun::kForbidLeadingExpansion,
47         TextDirection::kLtr, false);
48     text_run.SetNormalizeSpace(true);
49     FloatRect text_bounds = font.SelectionRectForText(
50         text_run, FloatPoint(), font.GetFontDescription().ComputedSize(), 0,
51         -1);
52 
53     // X direction.
54     EXPECT_EQ(78, font.Width(text_run));
55     EXPECT_EQ(0, text_bounds.X());
56     EXPECT_EQ(78, text_bounds.MaxX());
57 
58     // Y direction.
59     const FontMetrics& font_metrics = font_data->GetFontMetrics();
60     EXPECT_EQ(11, font_metrics.FloatAscent());
61     EXPECT_EQ(3, font_metrics.FloatDescent());
62     EXPECT_EQ(0, text_bounds.Y());
63     EXPECT_EQ(12, text_bounds.MaxY());
64   });
65 }
66 
TSAN_TEST(TextRendererThreadedTest,DrawText)67 TSAN_TEST(TextRendererThreadedTest, DrawText) {
68   callbacks_per_thread_ = 50;
69   RunOnThreads([]() {
70     String text = "draw this";
71 
72     FontDescription font_description;
73     font_description.SetComputedSize(12.0);
74     font_description.SetLocale(LayoutLocale::Get("en"));
75     ASSERT_EQ(USCRIPT_LATIN, font_description.GetScript());
76     font_description.SetGenericFamily(FontDescription::kStandardFamily);
77 
78     Font font = Font(font_description);
79 
80     FloatPoint location(0, 0);
81     TextRun text_run(text, 0, 0, TextRun::kAllowTrailingExpansion,
82                      TextDirection::kLtr, false);
83     text_run.SetNormalizeSpace(true);
84 
85     TextRunPaintInfo text_run_paint_info(text_run);
86 
87     MockPaintCanvas mpc;
88     PaintFlags flags;
89 
90     EXPECT_CALL(mpc, getSaveCount()).WillOnce(Return(17));
91     EXPECT_CALL(mpc, drawTextBlob(_, 0, 0, _)).Times(1);
92     EXPECT_CALL(mpc, restoreToCount(17)).WillOnce(Return());
93 
94     font.DrawBidiText(&mpc, text_run_paint_info, location,
95                       Font::kUseFallbackIfFontNotReady, 1.0, flags);
96   });
97 }
98 
99 }  // namespace blink
100