Commit 0c3c4e42 authored by Christopher Grant's avatar Christopher Grant Committed by Commit Bot

RenderText: Split RenderTextTestApi out into gfx test support

This lets additional non-RenderText tests leverage the RenderTextTestApi
class. Put the class to immediate use in a VR UI test.

BUG=

Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_vr;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Idf4d65a8e1b44430da72fe4bd5e3df6efb54719d
Reviewed-on: https://chromium-review.googlesource.com/881521
Commit-Queue: Christopher Grant <cjgrant@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532533}
parent 1f54e7ce
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "chrome/browser/vr/model/color_scheme.h" #include "chrome/browser/vr/model/color_scheme.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/render_text.h" #include "ui/gfx/render_text.h"
#include "ui/gfx/render_text_test_api.h"
#include "ui/gfx/text_utils.h" #include "ui/gfx/text_utils.h"
namespace vr { namespace vr {
...@@ -71,7 +72,7 @@ TEST(OmniboxFormatting, MultiLine) { ...@@ -71,7 +72,7 @@ TEST(OmniboxFormatting, MultiLine) {
struct ElisionTestcase { struct ElisionTestcase {
std::string url_string; std::string url_string;
std::string reference_url_string; std::string field_size_string;
bool has_offset; bool has_offset;
bool fade_left; bool fade_left;
bool fade_right; bool fade_right;
...@@ -80,18 +81,14 @@ struct ElisionTestcase { ...@@ -80,18 +81,14 @@ struct ElisionTestcase {
class ElisionTest : public ::testing::TestWithParam<ElisionTestcase> {}; class ElisionTest : public ::testing::TestWithParam<ElisionTestcase> {};
TEST_P(ElisionTest, ProperOffsetAndFading) { TEST_P(ElisionTest, ProperOffsetAndFading) {
// Use the reference URL to compute a text field width into which the actual constexpr int kCharacterWidth = 10;
// URL will be rendered. constexpr int kMinPathWidth = 3 * kCharacterWidth;
GURL reference_gurl(base::UTF8ToUTF16(GetParam().reference_url_string));
ASSERT_TRUE(reference_gurl.is_valid());
const base::string16 reference_text = url_formatter::FormatUrl(
reference_gurl, GetVrFormatUrlTypes(), net::UnescapeRule::NORMAL, nullptr,
nullptr, nullptr);
gfx::FontList font_list; gfx::FontList font_list;
auto field_width = gfx::GetStringWidth(reference_text, font_list); auto field_width = GetParam().field_size_string.size() * kCharacterWidth;
gfx::Rect field(field_width, font_list.GetHeight()); gfx::Rect field(field_width, font_list.GetHeight());
// Format the actual test URL. // Format the test URL.
GURL gurl(base::UTF8ToUTF16(GetParam().url_string)); GURL gurl(base::UTF8ToUTF16(GetParam().url_string));
ASSERT_TRUE(gurl.is_valid()); ASSERT_TRUE(gurl.is_valid());
url::Parsed parsed; url::Parsed parsed;
...@@ -107,10 +104,12 @@ TEST_P(ElisionTest, ProperOffsetAndFading) { ...@@ -107,10 +104,12 @@ TEST_P(ElisionTest, ProperOffsetAndFading) {
render_text->SetDisplayRect(field); render_text->SetDisplayRect(field);
render_text->SetCursorEnabled(false); render_text->SetCursorEnabled(false);
auto min_path_width = // Use a fixed glyph width for testing to avoid having to measure text.
gfx::GetStringWidth(base::UTF8ToUTF16("aaa"), font_list); gfx::test::RenderTextTestApi render_text_test_api(render_text.get());
render_text_test_api.SetGlyphWidth(kCharacterWidth);
ElisionParameters result = ElisionParameters result =
GetElisionParameters(gurl, parsed, render_text.get(), min_path_width); GetElisionParameters(gurl, parsed, render_text.get(), kMinPathWidth);
EXPECT_EQ(result.offset != 0, GetParam().has_offset); EXPECT_EQ(result.offset != 0, GetParam().has_offset);
EXPECT_EQ(result.fade_left, GetParam().fade_left); EXPECT_EQ(result.fade_left, GetParam().fade_left);
...@@ -119,22 +118,22 @@ TEST_P(ElisionTest, ProperOffsetAndFading) { ...@@ -119,22 +118,22 @@ TEST_P(ElisionTest, ProperOffsetAndFading) {
const std::vector<ElisionTestcase> elision_test_cases = { const std::vector<ElisionTestcase> elision_test_cases = {
// URL exactly matches the field width. // URL exactly matches the field width.
{"http://abc.com", "http://abc.com", kNoOffset, false, false}, {"http://abc.com", "abc.com", kNoOffset, false, false},
{"http://abc.com/aaa", "http://abc.com/aaa", kNoOffset, false, false}, {"http://abc.com/aaa", "abc.com/aaa", kNoOffset, false, false},
// URL is narrower than the field. // URL is narrower than the field.
{"http://abc.com/a", "http://abc.com/aaa", kNoOffset, false, false}, {"http://abc.com/a", "abc.com/aaa", kNoOffset, false, false},
// A really long path should not interfere with a short domain. // A really long path should not interfere with a short domain.
{"http://abc.com/aaaaaaaaaaaaaaaaaaaaaaaaa", "http://abc.com/aaa", {"http://abc.com/aaaaaaaaaaaaaaaaaaaaaaaaa", "abc.com/aaa", kNoOffset,
kNoOffset, false, true}, false, true},
// A long domain should be offset and fade on the left. // A long domain should be offset and fade on the left.
{"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com", "http://abc.com/aaa", {"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com", "abc.com/aaa", kHasOffset, true,
kHasOffset, true, false}, false},
// A long domain with a tiny path should preserve the path. // A long domain with a tiny path should preserve the path.
{"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com/a", "http://abc.com/aaa", {"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com/a", "abc.com/aaa", kHasOffset,
kHasOffset, true, false}, true, false},
// A long domain and path should see fading at both ends. // A long domain and path should see fading at both ends.
{"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaa", {"http://aaaaaaaaaaaaaaaaaaaaaaaa.abc.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"http://abc.com/aaa", kHasOffset, true, true}, "abc.com/aaa", kHasOffset, true, true},
// A file URL should always fade to the right. // A file URL should always fade to the right.
{"file://filer/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "file://filer/aaa", {"file://filer/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "file://filer/aaa",
kNoOffset, false, true}, kNoOffset, false, true},
......
...@@ -624,6 +624,12 @@ static_library("test_support") { ...@@ -624,6 +624,12 @@ static_library("test_support") {
"//ui/gfx/geometry", "//ui/gfx/geometry",
] ]
if (!is_ios) {
sources += [ "render_text_test_api.h" ]
deps += [ "//third_party:freetype_harfbuzz" ]
}
if (is_linux) { if (is_linux) {
deps += [ "//third_party/fontconfig" ] deps += [ "//third_party/fontconfig" ]
} }
......
...@@ -1723,4 +1723,18 @@ Range RenderText::ExpandRangeToWordBoundary(const Range& range) const { ...@@ -1723,4 +1723,18 @@ Range RenderText::ExpandRangeToWordBoundary(const Range& range) const {
: Range(range_min, range_max); : Range(range_min, range_max);
} }
internal::TextRunList* RenderText::GetRunList() {
NOTREACHED();
return nullptr;
}
const internal::TextRunList* RenderText::GetRunList() const {
NOTREACHED();
return nullptr;
}
void RenderText::SetGlyphWidthForTest(float test_width) {
NOTREACHED();
}
} // namespace gfx } // namespace gfx
...@@ -50,6 +50,8 @@ class Font; ...@@ -50,6 +50,8 @@ class Font;
namespace internal { namespace internal {
class TextRunList;
// Internal helper class used by derived classes to draw text through Skia. // Internal helper class used by derived classes to draw text through Skia.
class GFX_EXPORT SkiaTextRenderer { class GFX_EXPORT SkiaTextRenderer {
public: public:
...@@ -701,11 +703,20 @@ class GFX_EXPORT RenderText { ...@@ -701,11 +703,20 @@ class GFX_EXPORT RenderText {
// range. Maintains directionality of |range|. // range. Maintains directionality of |range|.
Range ExpandRangeToWordBoundary(const Range& range) const; Range ExpandRangeToWordBoundary(const Range& range) const;
// Returns an implementation-specific run list, if implemented.
virtual internal::TextRunList* GetRunList();
virtual const internal::TextRunList* GetRunList() const;
// Returns the decorated text corresponding to |range|. Returns false if the // Returns the decorated text corresponding to |range|. Returns false if the
// text cannot be retrieved, e.g. if the text is obscured. // text cannot be retrieved, e.g. if the text is obscured.
virtual bool GetDecoratedTextForRange(const Range& range, virtual bool GetDecoratedTextForRange(const Range& range,
DecoratedText* decorated_text) = 0; DecoratedText* decorated_text) = 0;
// Specify the width of a glyph for test. The width of glyphs is very
// platform-dependent and environment-dependent. Otherwise multiline text
// will become really flaky.
virtual void SetGlyphWidthForTest(float test_width);
// Logical UTF-16 string data to be drawn. // Logical UTF-16 string data to be drawn.
base::string16 text_; base::string16 text_;
......
...@@ -1739,6 +1739,8 @@ void RenderTextHarfBuzz::EnsureLayoutRunList() { ...@@ -1739,6 +1739,8 @@ void RenderTextHarfBuzz::EnsureLayoutRunList() {
} }
} }
// Returns the current run list, |display_run_list_| if the text is elided, or
// |layout_run_list_| otherwise.
internal::TextRunList* RenderTextHarfBuzz::GetRunList() { internal::TextRunList* RenderTextHarfBuzz::GetRunList() {
DCHECK(!update_layout_run_list_); DCHECK(!update_layout_run_list_);
DCHECK(!update_display_run_list_); DCHECK(!update_display_run_list_);
...@@ -1787,4 +1789,8 @@ bool RenderTextHarfBuzz::GetDecoratedTextForRange( ...@@ -1787,4 +1789,8 @@ bool RenderTextHarfBuzz::GetDecoratedTextForRange(
return true; return true;
} }
void RenderTextHarfBuzz::SetGlyphWidthForTest(float test_width) {
glyph_width_for_test_ = test_width;
}
} // namespace gfx } // namespace gfx
...@@ -193,13 +193,6 @@ class GFX_EXPORT RenderTextHarfBuzz : public RenderText { ...@@ -193,13 +193,6 @@ class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
friend class test::RenderTextTestApi; friend class test::RenderTextTestApi;
friend class RenderTextHarfBuzzTest; friend class RenderTextHarfBuzzTest;
// Specify the width of a glyph for test. The width of glyphs is very
// platform-dependent and environment-dependent. Otherwise multiline test
// will become really flaky.
void set_glyph_width_for_test(float test_width) {
glyph_width_for_test_ = test_width;
}
// Return the run index that contains the argument; or the length of the // Return the run index that contains the argument; or the length of the
// |runs_| vector if argument exceeds the text length or width. // |runs_| vector if argument exceeds the text length or width.
size_t GetRunContainingCaret(const SelectionModel& caret); size_t GetRunContainingCaret(const SelectionModel& caret);
...@@ -245,14 +238,12 @@ class GFX_EXPORT RenderTextHarfBuzz : public RenderText { ...@@ -245,14 +238,12 @@ class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
// Makes sure that text runs for layout text are shaped. // Makes sure that text runs for layout text are shaped.
void EnsureLayoutRunList(); void EnsureLayoutRunList();
// Returns the current run list, |display_run_list_| if the text is
// elided, or |layout_run_list_| otherwise.
internal::TextRunList* GetRunList();
const internal::TextRunList* GetRunList() const;
// RenderText: // RenderText:
internal::TextRunList* GetRunList() override;
const internal::TextRunList* GetRunList() const override;
bool GetDecoratedTextForRange(const Range& range, bool GetDecoratedTextForRange(const Range& range,
DecoratedText* decorated_text) override; DecoratedText* decorated_text) override;
void SetGlyphWidthForTest(float test_width) override;
// Text run list for |layout_text_| and |display_text_|. // Text run list for |layout_text_| and |display_text_|.
// |display_run_list_| is created only when the text is elided. // |display_run_list_| is created only when the text is elided.
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "ui/gfx/break_list.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/render_text.h"
#include "ui/gfx/selection_model.h"
namespace gfx {
namespace test {
class RenderTextTestApi {
public:
RenderTextTestApi(RenderText* render_text) : render_text_(render_text) {}
static cc::PaintFlags& GetRendererPaint(
internal::SkiaTextRenderer* renderer) {
return renderer->flags_;
}
// Callers must ensure that the associated RenderText object is a
// RenderTextHarfBuzz instance.
const internal::TextRunList* GetHarfBuzzRunList() const {
return render_text_->GetRunList();
}
void DrawVisualText(internal::SkiaTextRenderer* renderer) {
render_text_->EnsureLayout();
render_text_->DrawVisualText(renderer);
}
const BreakList<SkColor>& colors() const { return render_text_->colors(); }
const BreakList<BaselineStyle>& baselines() const {
return render_text_->baselines();
}
const BreakList<Font::Weight>& weights() const {
return render_text_->weights();
}
const std::vector<BreakList<bool>>& styles() const {
return render_text_->styles();
}
const std::vector<internal::Line>& lines() const {
return render_text_->lines();
}
SelectionModel EdgeSelectionModel(VisualCursorDirection direction) {
return render_text_->EdgeSelectionModel(direction);
}
size_t TextIndexToDisplayIndex(size_t index) {
return render_text_->TextIndexToDisplayIndex(index);
}
size_t DisplayIndexToTextIndex(size_t index) {
return render_text_->DisplayIndexToTextIndex(index);
}
void EnsureLayout() { render_text_->EnsureLayout(); }
Vector2d GetAlignmentOffset(size_t line_number) {
return render_text_->GetAlignmentOffset(line_number);
}
int GetDisplayTextBaseline() {
return render_text_->GetDisplayTextBaseline();
}
// Callers must ensure that the underlying RenderText object is a
// RenderTextHarfBuzz instance.
void SetGlyphWidth(float test_width) {
render_text_->SetGlyphWidthForTest(test_width);
}
private:
RenderText* render_text_;
DISALLOW_COPY_AND_ASSIGN(RenderTextTestApi);
};
} // namespace test
} // namespace gfx
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "ui/gfx/range/range.h" #include "ui/gfx/range/range.h"
#include "ui/gfx/range/range_f.h" #include "ui/gfx/range/range_f.h"
#include "ui/gfx/render_text_harfbuzz.h" #include "ui/gfx/render_text_harfbuzz.h"
#include "ui/gfx/render_text_test_api.h"
#include "ui/gfx/switches.h" #include "ui/gfx/switches.h"
#include "ui/gfx/text_utils.h" #include "ui/gfx/text_utils.h"
...@@ -58,77 +59,6 @@ using base::UTF8ToUTF16; ...@@ -58,77 +59,6 @@ using base::UTF8ToUTF16;
using base::WideToUTF16; using base::WideToUTF16;
namespace gfx { namespace gfx {
namespace test {
class RenderTextTestApi {
public:
RenderTextTestApi(RenderText* render_text) : render_text_(render_text) {}
static cc::PaintFlags& GetRendererPaint(
internal::SkiaTextRenderer* renderer) {
return renderer->flags_;
}
// Callers should ensure that the associated RenderText object is a
// RenderTextHarfBuzz instance.
const internal::TextRunList* GetHarfBuzzRunList() const {
RenderTextHarfBuzz* render_text =
static_cast<RenderTextHarfBuzz*>(render_text_);
return render_text->GetRunList();
}
void DrawVisualText(internal::SkiaTextRenderer* renderer) {
render_text_->EnsureLayout();
render_text_->DrawVisualText(renderer);
}
const BreakList<SkColor>& colors() const { return render_text_->colors(); }
const BreakList<BaselineStyle>& baselines() const {
return render_text_->baselines();
}
const BreakList<Font::Weight>& weights() const {
return render_text_->weights();
}
const std::vector<BreakList<bool>>& styles() const {
return render_text_->styles();
}
const std::vector<internal::Line>& lines() const {
return render_text_->lines();
}
SelectionModel EdgeSelectionModel(VisualCursorDirection direction) {
return render_text_->EdgeSelectionModel(direction);
}
size_t TextIndexToDisplayIndex(size_t index) {
return render_text_->TextIndexToDisplayIndex(index);
}
size_t DisplayIndexToTextIndex(size_t index) {
return render_text_->DisplayIndexToTextIndex(index);
}
void EnsureLayout() { render_text_->EnsureLayout(); }
Vector2d GetAlignmentOffset(size_t line_number) {
return render_text_->GetAlignmentOffset(line_number);
}
int GetDisplayTextBaseline() {
return render_text_->GetDisplayTextBaseline();
}
private:
RenderText* render_text_;
DISALLOW_COPY_AND_ASSIGN(RenderTextTestApi);
};
} // namespace test
namespace { namespace {
...@@ -615,7 +545,7 @@ class RenderTextHarfBuzzTest : public RenderTextTest { ...@@ -615,7 +545,7 @@ class RenderTextHarfBuzzTest : public RenderTextTest {
protected: protected:
void SetGlyphWidth(float test_width) { void SetGlyphWidth(float test_width) {
GetRenderTextHarfBuzz()->set_glyph_width_for_test(test_width); test_api()->SetGlyphWidth(test_width);
} }
bool ShapeRunWithFont(const base::string16& text, bool ShapeRunWithFont(const base::string16& text,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment