Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 1013543006: [RenderText] Adding vertical alignment options (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Testing adjustment to vertical alignment test Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/i18n/break_iterator.h" 10 #include "base/i18n/break_iterator.h"
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 int left, 187 int left,
188 int top, 188 int top,
189 int width, 189 int width,
190 int height) const { 190 int height) const {
191 ASSERT_LT(top, row_count_) << string_; 191 ASSERT_LT(top, row_count_) << string_;
192 ASSERT_LE(top + height, row_count_) << string_; 192 ASSERT_LE(top + height, row_count_) << string_;
193 ASSERT_LT(left, stride_) << string_; 193 ASSERT_LT(left, stride_) << string_;
194 ASSERT_LE(left + width, stride_) << string_ << ", left " << left 194 ASSERT_LE(left + width, stride_) << string_ << ", left " << left
195 << ", width " << width << ", stride_ " 195 << ", width " << width << ", stride_ "
196 << stride_; 196 << stride_;
197 // Only report the first N failures.
198 uint32_t fail_limit = 5;
197 for (int y = top; y < top + height; ++y) { 199 for (int y = top; y < top + height; ++y) {
198 for (int x = left; x < left + width; ++x) { 200 for (int x = left; x < left + width; ++x) {
199 SkColor buffer_color = buffer_[x + y * stride_]; 201 SkColor buffer_color = buffer_[x + y * stride_];
202 if (color != buffer_color && !(--fail_limit)) {
203 EXPECT_TRUE(fail_limit) << "fail_limit reached";
204 return;
205 }
200 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y; 206 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y;
201 } 207 }
202 } 208 }
203 } 209 }
204 210
211 void EnsureSolidBorder(SkColor color, const Rect& rect) const {
212 {
213 SCOPED_TRACE("EnsureSolidBorder Top Side");
214 EnsureSolidRect(SK_ColorWHITE, 0, 0, stride_, rect.y());
215 }
216 {
217 SCOPED_TRACE("EnsureSolidBorder Bottom Side");
218 EnsureSolidRect(SK_ColorWHITE, 0, rect.bottom(), stride_,
219 row_count_ - rect.bottom());
220 }
221 {
222 SCOPED_TRACE("EnsureSolidBorder Left Side");
223 EnsureSolidRect(SK_ColorWHITE, 0, rect.y(), rect.x(), rect.height());
224 }
225 {
226 SCOPED_TRACE("EnsureSolidBorder Right Side");
227 EnsureSolidRect(SK_ColorWHITE, rect.right(), rect.y(),
228 stride_ - rect.right(), rect.height());
229 }
230 }
231
205 private: 232 private:
206 const wchar_t* string_; 233 const wchar_t* string_;
207 const SkColor* buffer_; 234 const SkColor* buffer_;
208 int stride_; 235 int stride_;
209 int row_count_; 236 int row_count_;
210 237
211 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer); 238 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
212 }; 239 };
213 240
214 } // namespace 241 } // namespace
(...skipping 2434 matching lines...) Expand 10 before | Expand all | Expand 10 after
2649 render_text->SetText(kString); 2676 render_text->SetText(kString);
2650 render_text->ApplyStyle(BOLD, true, Range(0, 3)); 2677 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2651 render_text->SetElideBehavior(ELIDE_TAIL); 2678 render_text->SetElideBehavior(ELIDE_TAIL);
2652 2679
2653 render_text->SetDisplayRect(Rect(0, 0, 500, 100)); 2680 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2654 EXPECT_EQ(kString, render_text->GetDisplayText()); 2681 EXPECT_EQ(kString, render_text->GetDisplayText());
2655 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); 2682 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2656 EXPECT_EQ(kString, render_text->GetDisplayText()); 2683 EXPECT_EQ(kString, render_text->GetDisplayText());
2657 } 2684 }
2658 2685
2686 // TODO(dschuyler): Alignment tests disabled on Mac because RenderTextMac
2687 // does not implement this yet.
2688 #if !defined(OS_MACOSX)
2689 TEST_F(RenderTextTest, VerticalAlignment) {
2690 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2691 const wchar_t* string = L"g Hello g";
2692 render_text->SetText(WideToUTF16(string));
2693 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2694 const Size kCanvasSize(171, 50);
2695 const int kTestSize = 10;
2696 const Size string_size(render_text->GetContentWidth(),
2697 render_text->GetStringSize().height());
2698 // The + 1 accounts for legacy behavior in GetAlignmentOffset().
2699 const int kCenterBegin = (kCanvasSize.width() + 1 - string_size.width()) / 2;
2700 const int kRightBegin = kCanvasSize.width() - string_size.width() - kTestSize;
2701 // TODO(dschuyler): Determine where the need for the -1 below originates from.
2702 const int kMiddleBegin =
2703 ((kCanvasSize.height()) / 2) - 1 +
2704 (render_text->GetBaseline() - render_text->GetDisplayTextBaseline());
2705 const int kBottomBegin =
2706 kCanvasSize.height() - string_size.height() - kTestSize;
2707
2708 struct Tests {
2709 HorizontalAlignment horizontal;
2710 VerticalAlignment vertical;
2711 int x;
2712 int y;
2713 } const kTests[] = {
2714 {ALIGN_LEFT, VALIGN_TOP, kTestSize, kTestSize},
2715 {ALIGN_LEFT, VALIGN_MIDDLE, kTestSize, kMiddleBegin},
2716 {ALIGN_LEFT, VALIGN_BOTTOM, kTestSize, kBottomBegin},
2717 {ALIGN_CENTER, VALIGN_TOP, kCenterBegin, kTestSize},
2718 {ALIGN_CENTER, VALIGN_MIDDLE, kCenterBegin, kMiddleBegin},
2719 {ALIGN_CENTER, VALIGN_BOTTOM, kCenterBegin, kBottomBegin},
2720 {ALIGN_RIGHT, VALIGN_TOP, kRightBegin, kTestSize},
2721 {ALIGN_RIGHT, VALIGN_MIDDLE, kRightBegin, kMiddleBegin},
2722 {ALIGN_RIGHT, VALIGN_BOTTOM, kRightBegin, kBottomBegin},
2723 };
2724
2725 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2726 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2727 scoped_ptr<Canvas> canvas(
2728 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2729 render_text->SetColor(SK_ColorBLACK);
2730 Rect bounds = Rect(kTestSize, kTestSize, kCanvasSize.width() - kTestSize * 2,
2731 kCanvasSize.height() - kTestSize * 2);
2732 render_text->SetDisplayRect(bounds);
2733 // Allow the RenderText to paint outside of its display rect.
2734 render_text->set_clip_to_display_rect(false);
2735 const uint32* buffer =
2736 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2737 ASSERT_NE(nullptr, buffer);
2738 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2739 kCanvasSize.height());
2740 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2741 ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height());
2742 for (const auto& test : kTests) {
2743 SCOPED_TRACE(base::StringPrintf("VerticalAlignement H %i, V %i",
2744 test.horizontal, test.vertical));
2745
2746 surface->getCanvas()->clear(SK_ColorWHITE);
2747 render_text->SetHorizontalAlignment(test.horizontal);
2748 render_text->SetVerticalAlignment(test.vertical);
2749 EXPECT_EQ(test.x - kTestSize, render_text->GetAlignmentOffset(0).x());
2750 EXPECT_EQ(test.y - kTestSize, render_text->GetAlignmentOffset(0).y());
2751 Rect expected_rect(test.x, test.y, string_size.width(),
2752 string_size.height());
2753 expected_rect.Inset(-1, -1);
2754 render_text->Draw(canvas.get());
2755 rect_buffer.EnsureSolidBorder(SK_ColorWHITE, expected_rect);
2756 }
2757 }
2758 #endif // !defined(OS_MACOSX)
2759
2659 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 2760 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2660 #if !defined(OS_WIN) 2761 #if !defined(OS_WIN)
2661 // Ensure that RenderText examines all of the fonts in its FontList before 2762 // Ensure that RenderText examines all of the fonts in its FontList before
2662 // falling back to other fonts. 2763 // falling back to other fonts.
2663 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) { 2764 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2664 // Double-check that the requested fonts are present. 2765 // Double-check that the requested fonts are present.
2665 FontList font_list("Arial, Symbol, 12px"); 2766 FontList font_list("Arial, Symbol, 12px");
2666 const std::vector<Font>& fonts = font_list.GetFonts(); 2767 const std::vector<Font>& fonts = font_list.GetFonts();
2667 ASSERT_EQ(2u, fonts.size()); 2768 ASSERT_EQ(2u, fonts.size());
2668 ASSERT_EQ("arial", 2769 ASSERT_EQ("arial",
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); 2844 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2744 scoped_ptr<Canvas> canvas( 2845 scoped_ptr<Canvas> canvas(
2745 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); 2846 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2746 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 2847 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2747 render_text->SetHorizontalAlignment(ALIGN_LEFT); 2848 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2748 render_text->SetColor(SK_ColorBLACK); 2849 render_text->SetColor(SK_ColorBLACK);
2749 2850
2750 for (auto string : kTestStrings) { 2851 for (auto string : kTestStrings) {
2751 surface->getCanvas()->clear(SK_ColorWHITE); 2852 surface->getCanvas()->clear(SK_ColorWHITE);
2752 render_text->SetText(WideToUTF16(string)); 2853 render_text->SetText(WideToUTF16(string));
2753 const Size string_size = render_text->GetStringSize(); 2854 const Size string_size(render_text->GetContentWidth(),
msw 2015/03/30 19:19:12 I don't think this is correct, the cursor shouldn'
dschuyler 2015/03/31 18:38:14 The value of cursor_enabled_ defaults to true. It
msw 2015/04/01 15:07:17 Ah, you should probably disable the cursor for the
2855 render_text->GetStringSize().height());
2754 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); 2856 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
2755 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); 2857 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4));
2756 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); 2858 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6));
2757 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); 2859 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8));
2758 render_text->SetStyle(BOLD, true); 2860 render_text->SetStyle(BOLD, true);
2759 render_text->SetDisplayRect( 2861 render_text->SetDisplayRect(
2760 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); 2862 Rect(kTestSize, kTestSize, string_size.width(), string_size.height()));
2761 // Allow the RenderText to paint outside of its display rect. 2863 // Allow the RenderText to paint outside of its display rect.
2762 render_text->set_clip_to_display_rect(false); 2864 render_text->set_clip_to_display_rect(false);
2763 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); 2865 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2866 ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height());
2764 2867
2765 render_text->Draw(canvas.get()); 2868 render_text->Draw(canvas.get());
2766 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2767 const uint32* buffer = 2869 const uint32* buffer =
2768 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); 2870 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2769 ASSERT_NE(nullptr, buffer); 2871 ASSERT_NE(nullptr, buffer);
2770 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), 2872 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2771 kCanvasSize.height()); 2873 kCanvasSize.height());
2874 // TODO(dschuyler): After platform issues are resolved below, change to
2875 // using EnsureSolidBorder() rather than EnsureSolidRect().
2772 { 2876 {
2773 #if !defined(OS_CHROMEOS) 2877 #if !defined(OS_CHROMEOS)
2774 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. 2878 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
2775 SCOPED_TRACE("TextDoesntClip Top Side"); 2879 SCOPED_TRACE("TextDoesntClip Top Side");
2776 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), 2880 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2777 kTestSize); 2881 kTestSize);
2778 #endif 2882 #endif
2779 } 2883 }
2780 { 2884 {
2781 SCOPED_TRACE("TextDoesntClip Bottom Side"); 2885 SCOPED_TRACE("TextDoesntClip Bottom Side");
(...skipping 29 matching lines...) Expand all
2811 kTestSize + string_size.width(), kTestSize, 2915 kTestSize + string_size.width(), kTestSize,
2812 kTestSize, string_size.height()); 2916 kTestSize, string_size.height());
2813 #endif 2917 #endif
2814 } 2918 }
2815 } 2919 }
2816 } 2920 }
2817 2921
2818 // Ensure that the text will clip to the display rect. Draws to a canvas and 2922 // Ensure that the text will clip to the display rect. Draws to a canvas and
2819 // checks whether any pixel beyond the bounding rectangle is colored. 2923 // checks whether any pixel beyond the bounding rectangle is colored.
2820 TEST_F(RenderTextTest, TextDoesClip) { 2924 TEST_F(RenderTextTest, TextDoesClip) {
2925 SCOPED_TRACE("TextDoesClip");
msw 2015/03/30 19:19:12 Remove this scoped trace.
dschuyler 2015/03/31 18:38:14 Done.
2821 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; 2926 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"};
2822 const Size kCanvasSize(300, 50); 2927 const Size kCanvasSize(300, 50);
2823 const int kTestSize = 10; 2928 const int kTestSize = 10;
2824 2929
2825 skia::RefPtr<SkSurface> surface = skia::AdoptRef( 2930 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2826 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); 2931 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2827 scoped_ptr<Canvas> canvas( 2932 scoped_ptr<Canvas> canvas(
2828 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); 2933 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2829 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 2934 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2830 render_text->SetHorizontalAlignment(ALIGN_LEFT); 2935 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2831 render_text->SetColor(SK_ColorBLACK); 2936 render_text->SetColor(SK_ColorBLACK);
2832 2937
2833 for (auto string : kTestStrings) { 2938 for (auto string : kTestStrings) {
2834 surface->getCanvas()->clear(SK_ColorWHITE); 2939 surface->getCanvas()->clear(SK_ColorWHITE);
2835 render_text->SetText(WideToUTF16(string)); 2940 render_text->SetText(WideToUTF16(string));
2836 const Size string_size = render_text->GetStringSize(); 2941 const Size string_size(render_text->GetContentWidth(),
msw 2015/03/30 19:19:12 Ditto.
dschuyler 2015/03/31 18:38:14 Acknowledged.
2942 render_text->GetStringSize().height());
2837 int fake_width = string_size.width() / 2; 2943 int fake_width = string_size.width() / 2;
2838 int fake_height = string_size.height() / 2; 2944 int fake_height = string_size.height() / 2;
2839 render_text->SetDisplayRect( 2945 const Rect bounds(kTestSize, kTestSize, fake_width, fake_height);
2840 Rect(kTestSize, kTestSize, fake_width, fake_height)); 2946 render_text->SetDisplayRect(bounds);
2841 render_text->set_clip_to_display_rect(true); 2947 render_text->set_clip_to_display_rect(true);
2948 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2949 ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height());
2842 render_text->Draw(canvas.get()); 2950 render_text->Draw(canvas.get());
2843 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2844 const uint32* buffer = 2951 const uint32* buffer =
2845 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); 2952 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2846 ASSERT_NE(nullptr, buffer); 2953 ASSERT_NE(nullptr, buffer);
2847 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), 2954 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2848 kCanvasSize.height()); 2955 kCanvasSize.height());
2849 { 2956 rect_buffer.EnsureSolidBorder(SK_ColorWHITE, bounds);
2850 SCOPED_TRACE("TextDoesClip Top Side");
2851 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2852 kTestSize);
2853 }
2854
2855 {
2856 SCOPED_TRACE("TextDoesClip Bottom Side");
2857 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height,
2858 kCanvasSize.width(), kTestSize);
2859 }
2860 {
2861 SCOPED_TRACE("TextDoesClip Left Side");
2862 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2863 fake_height);
2864 }
2865 {
2866 SCOPED_TRACE("TextDoesClip Right Side");
2867 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width,
2868 kTestSize, kTestSize, fake_height);
2869 }
2870 } 2957 }
2871 } 2958 }
2872 2959
2873 #if defined(OS_MACOSX) 2960 #if defined(OS_MACOSX)
2874 TEST_F(RenderTextTest, Mac_ElidedText) { 2961 TEST_F(RenderTextTest, Mac_ElidedText) {
2875 RenderTextMac render_text; 2962 RenderTextMac render_text;
2876 base::string16 text(ASCIIToUTF16("This is an example.")); 2963 base::string16 text(ASCIIToUTF16("This is an example."));
2877 render_text.SetText(text); 2964 render_text.SetText(text);
2878 gfx::Size string_size = render_text.GetStringSize(); 2965 gfx::Size string_size = render_text.GetStringSize();
2879 render_text.SetDisplayRect(gfx::Rect(string_size)); 2966 render_text.SetDisplayRect(gfx::Rect(string_size));
2880 render_text.EnsureLayout(); 2967 render_text.EnsureLayout();
2881 // NOTE: Character and glyph counts are only comparable for simple text. 2968 // NOTE: Character and glyph counts are only comparable for simple text.
2882 EXPECT_EQ(text.size(), 2969 EXPECT_EQ(text.size(),
2883 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_))); 2970 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_)));
2884 2971
2885 render_text.SetElideBehavior(ELIDE_TAIL); 2972 render_text.SetElideBehavior(ELIDE_TAIL);
2886 string_size.set_width(string_size.width() / 2); 2973 string_size.set_width(string_size.width() / 2);
2887 render_text.SetDisplayRect(gfx::Rect(string_size)); 2974 render_text.SetDisplayRect(gfx::Rect(string_size));
2888 render_text.EnsureLayout(); 2975 render_text.EnsureLayout();
2889 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_); 2976 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
2890 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count)); 2977 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
2891 EXPECT_NE(0, glyph_count); 2978 EXPECT_NE(0, glyph_count);
2892 } 2979 }
2893 #endif 2980 #endif
2894 2981
2895 } // namespace gfx 2982 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text.cc ('k') | ui/gfx/text_constants.h » ('j') | ui/gfx/text_constants.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698