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

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: merge from master Created 5 years, 9 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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 << ", width " << width << ", stride_ " 195 << ", width " << width << ", stride_ "
196 << stride_; 196 << stride_;
197 for (int y = top; y < top + height; ++y) { 197 for (int y = top; y < top + height; ++y) {
198 for (int x = left; x < left + width; ++x) { 198 for (int x = left; x < left + width; ++x) {
199 SkColor buffer_color = buffer_[x + y * stride_]; 199 SkColor buffer_color = buffer_[x + y * stride_];
200 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y; 200 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y;
201 } 201 }
202 } 202 }
203 } 203 }
204 204
205 void EnsureSolidBorder(SkColor color, const Rect& rect) const {
206 {
207 SCOPED_TRACE("EnsureSolidBorder Top Side");
208 EnsureSolidRect(SK_ColorWHITE, 0, 0, stride_, rect.y());
209 }
210 {
211 SCOPED_TRACE("EnsureSolidBorder Bottom Side");
212 EnsureSolidRect(SK_ColorWHITE, 0, rect.y() + rect.height(), stride_,
msw 2015/03/26 22:55:45 rect.y() + rect.height() == rect.bottom()
dschuyler 2015/03/27 21:20:35 Done.
213 row_count_ - rect.y() - rect.height());
msw 2015/03/26 22:55:45 - rect.y() - rect.height() == - rect.bottom()
dschuyler 2015/03/27 21:20:35 Done.
214 }
215 {
216 SCOPED_TRACE("EnsureSolidBorder Left Side");
217 EnsureSolidRect(SK_ColorWHITE, 0, rect.y(), rect.x(), rect.height());
218 }
219 {
220 SCOPED_TRACE("EnsureSolidBorder Right Side");
221 EnsureSolidRect(SK_ColorWHITE, rect.x() + rect.width(), rect.y(),
msw 2015/03/26 22:55:45 rect.x() + rect.width() == rect.right()
dschuyler 2015/03/27 21:20:35 Done.
222 stride_ - rect.x() - rect.width(), rect.height());
msw 2015/03/26 22:55:45 - rect.x() - rect.width() == - rect.right()
dschuyler 2015/03/27 21:20:34 Done.
223 }
224 }
225
205 private: 226 private:
206 const wchar_t* string_; 227 const wchar_t* string_;
207 const SkColor* buffer_; 228 const SkColor* buffer_;
208 int stride_; 229 int stride_;
209 int row_count_; 230 int row_count_;
210 231
211 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer); 232 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
212 }; 233 };
213 234
214 } // namespace 235 } // namespace
(...skipping 2370 matching lines...) Expand 10 before | Expand all | Expand 10 after
2585 render_text->SetText(kString); 2606 render_text->SetText(kString);
2586 render_text->ApplyStyle(BOLD, true, Range(0, 3)); 2607 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2587 render_text->SetElideBehavior(ELIDE_TAIL); 2608 render_text->SetElideBehavior(ELIDE_TAIL);
2588 2609
2589 render_text->SetDisplayRect(Rect(0, 0, 500, 100)); 2610 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2590 EXPECT_EQ(kString, render_text->GetDisplayText()); 2611 EXPECT_EQ(kString, render_text->GetDisplayText());
2591 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); 2612 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2592 EXPECT_EQ(kString, render_text->GetDisplayText()); 2613 EXPECT_EQ(kString, render_text->GetDisplayText());
2593 } 2614 }
2594 2615
2616 TEST_F(RenderTextTest, VerticalAlignement) {
msw 2015/03/26 22:55:45 nit: "Alignment"
dschuyler 2015/03/27 21:20:35 Done.
2617 SCOPED_TRACE("VerticalAlignement");
msw 2015/03/26 22:55:45 What's the use of this SCOPED_TRACE? The failing t
dschuyler 2015/03/27 21:20:34 Done.
2618 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2619 const wchar_t* string = L"g Hello g";
2620 render_text->SetText(WideToUTF16(string));
2621 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2622 const Size kCanvasSize(300, 50);
2623 const int kTestSize = 10;
2624 const Size string_size = render_text->GetStringSize();
2625 const int kCenterBegin = (kCanvasSize.width() - string_size.width()) / 2;
2626 const int kRightBegin = kCanvasSize.width() - string_size.width() - kTestSize;
2627 const int kMiddleBegin = (kCanvasSize.height() - string_size.height()) / 2;
2628 const int kBottomBegin =
2629 kCanvasSize.height() - string_size.height() - kTestSize;
2630
2631 struct Tests {
2632 HorizontalAlignment horizontal;
2633 VerticalAlignment vertical;
2634 int x;
2635 int y;
2636 } const kTests[] = {
2637 {ALIGN_LEFT, VALIGN_TOP, kTestSize, kTestSize},
msw 2015/03/26 22:55:45 nit: outdent all the these lines by 2 spaces
dschuyler 2015/03/27 21:20:35 Afaik, it's an outstanding bug in cl format. I.e.
2638 {ALIGN_LEFT, VALIGN_MIDDLE, kTestSize, kMiddleBegin},
2639 {ALIGN_LEFT, VALIGN_BOTTOM, kTestSize, kBottomBegin},
2640 {ALIGN_CENTER, VALIGN_TOP, kCenterBegin, kTestSize},
2641 {ALIGN_CENTER, VALIGN_MIDDLE, kCenterBegin, kMiddleBegin},
2642 {ALIGN_CENTER, VALIGN_BOTTOM, kCenterBegin, kBottomBegin},
2643 {ALIGN_RIGHT, VALIGN_TOP, kRightBegin, kTestSize},
2644 {ALIGN_RIGHT, VALIGN_MIDDLE, kRightBegin, kMiddleBegin},
2645 {ALIGN_RIGHT, VALIGN_BOTTOM, kRightBegin, kBottomBegin},
2646 };
2647
2648 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
msw 2015/03/26 22:55:45 I think painting the text and checking the painted
dschuyler 2015/03/27 21:20:34 I put in some tests for the get alignment offset.
msw 2015/03/30 19:19:12 How many ms do the pixel tests take on the bots? I
dschuyler 2015/03/31 18:38:14 Done.
2649 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2650 scoped_ptr<Canvas> canvas(
2651 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2652 render_text->SetColor(SK_ColorBLACK);
2653 Rect bounds = Rect(kTestSize, kTestSize, kCanvasSize.width() - kTestSize * 2,
2654 kCanvasSize.height() - kTestSize * 2);
2655 render_text->SetDisplayRect(bounds);
2656 // Allow the RenderText to paint outside of its display rect.
2657 render_text->set_clip_to_display_rect(false);
2658 const uint32* buffer =
2659 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2660 ASSERT_NE(nullptr, buffer);
2661 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2662 kCanvasSize.height());
2663 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2664 for (const auto& test : kTests) {
2665 surface->getCanvas()->clear(SK_ColorWHITE);
2666 render_text->SetHorizontalAlignment(test.horizontal);
2667 render_text->SetVerticalAlignment(test.vertical);
2668 Rect expected_rect(test.x, test.y, string_size.width(),
2669 string_size.height());
2670 expected_rect.Inset(-1, -1);
2671 render_text->Draw(canvas.get());
2672 rect_buffer.EnsureSolidBorder(SK_ColorWHITE, expected_rect);
2673 }
2674 }
2675
2595 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 2676 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2596 #if !defined(OS_WIN) 2677 #if !defined(OS_WIN)
2597 // Ensure that RenderText examines all of the fonts in its FontList before 2678 // Ensure that RenderText examines all of the fonts in its FontList before
2598 // falling back to other fonts. 2679 // falling back to other fonts.
2599 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) { 2680 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2600 // Double-check that the requested fonts are present. 2681 // Double-check that the requested fonts are present.
2601 FontList font_list("Arial, Symbol, 12px"); 2682 FontList font_list("Arial, Symbol, 12px");
2602 const std::vector<Font>& fonts = font_list.GetFonts(); 2683 const std::vector<Font>& fonts = font_list.GetFonts();
2603 ASSERT_EQ(2u, fonts.size()); 2684 ASSERT_EQ(2u, fonts.size());
2604 ASSERT_EQ("arial", 2685 ASSERT_EQ("arial",
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2692 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); 2773 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6));
2693 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); 2774 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8));
2694 render_text->SetStyle(BOLD, true); 2775 render_text->SetStyle(BOLD, true);
2695 render_text->SetDisplayRect( 2776 render_text->SetDisplayRect(
2696 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); 2777 Rect(kTestSize, kTestSize, string_size.width(), string_size.height()));
2697 // Allow the RenderText to paint outside of its display rect. 2778 // Allow the RenderText to paint outside of its display rect.
2698 render_text->set_clip_to_display_rect(false); 2779 render_text->set_clip_to_display_rect(false);
2699 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); 2780 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2700 2781
2701 render_text->Draw(canvas.get()); 2782 render_text->Draw(canvas.get());
2702 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2703 const uint32* buffer = 2783 const uint32* buffer =
2704 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); 2784 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2705 ASSERT_NE(nullptr, buffer); 2785 ASSERT_NE(nullptr, buffer);
2706 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), 2786 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2707 kCanvasSize.height()); 2787 kCanvasSize.height());
2788 // TODO(dschuyler): After platform issued are resolved below, change to
msw 2015/03/26 22:55:45 nit: "issues"
dschuyler 2015/03/27 21:20:34 Done.
2789 // using EnsureSolidBorder() rather than EnsureSolidRect().
2708 { 2790 {
2709 #if !defined(OS_CHROMEOS) 2791 #if !defined(OS_CHROMEOS)
2710 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. 2792 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
2711 SCOPED_TRACE("TextDoesntClip Top Side"); 2793 SCOPED_TRACE("TextDoesntClip Top Side");
2712 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), 2794 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2713 kTestSize); 2795 kTestSize);
2714 #endif 2796 #endif
2715 } 2797 }
2716 { 2798 {
2717 SCOPED_TRACE("TextDoesntClip Bottom Side"); 2799 SCOPED_TRACE("TextDoesntClip Bottom Side");
(...skipping 29 matching lines...) Expand all
2747 kTestSize + string_size.width(), kTestSize, 2829 kTestSize + string_size.width(), kTestSize,
2748 kTestSize, string_size.height()); 2830 kTestSize, string_size.height());
2749 #endif 2831 #endif
2750 } 2832 }
2751 } 2833 }
2752 } 2834 }
2753 2835
2754 // Ensure that the text will clip to the display rect. Draws to a canvas and 2836 // Ensure that the text will clip to the display rect. Draws to a canvas and
2755 // checks whether any pixel beyond the bounding rectangle is colored. 2837 // checks whether any pixel beyond the bounding rectangle is colored.
2756 TEST_F(RenderTextTest, TextDoesClip) { 2838 TEST_F(RenderTextTest, TextDoesClip) {
2839 SCOPED_TRACE("TextDoesClip");
msw 2015/03/26 22:55:45 Ditto, what's the use of this SCOPED_TRACE? The fa
2757 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; 2840 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"};
2758 const Size kCanvasSize(300, 50); 2841 const Size kCanvasSize(300, 50);
2759 const int kTestSize = 10; 2842 const int kTestSize = 10;
2760 2843
2761 skia::RefPtr<SkSurface> surface = skia::AdoptRef( 2844 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2762 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); 2845 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2763 scoped_ptr<Canvas> canvas( 2846 scoped_ptr<Canvas> canvas(
2764 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); 2847 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2765 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 2848 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2766 render_text->SetHorizontalAlignment(ALIGN_LEFT); 2849 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2767 render_text->SetColor(SK_ColorBLACK); 2850 render_text->SetColor(SK_ColorBLACK);
2768 2851
2769 for (auto string : kTestStrings) { 2852 for (auto string : kTestStrings) {
2770 surface->getCanvas()->clear(SK_ColorWHITE); 2853 surface->getCanvas()->clear(SK_ColorWHITE);
2771 render_text->SetText(WideToUTF16(string)); 2854 render_text->SetText(WideToUTF16(string));
2772 const Size string_size = render_text->GetStringSize(); 2855 const Size string_size = render_text->GetStringSize();
2773 int fake_width = string_size.width() / 2; 2856 int fake_width = string_size.width() / 2;
2774 int fake_height = string_size.height() / 2; 2857 int fake_height = string_size.height() / 2;
2775 render_text->SetDisplayRect( 2858 const Rect bounds(kTestSize, kTestSize, fake_width, fake_height);
2776 Rect(kTestSize, kTestSize, fake_width, fake_height)); 2859 render_text->SetDisplayRect(bounds);
2777 render_text->set_clip_to_display_rect(true); 2860 render_text->set_clip_to_display_rect(true);
2861 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2778 render_text->Draw(canvas.get()); 2862 render_text->Draw(canvas.get());
2779 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2780 const uint32* buffer = 2863 const uint32* buffer =
2781 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); 2864 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2782 ASSERT_NE(nullptr, buffer); 2865 ASSERT_NE(nullptr, buffer);
2783 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), 2866 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2784 kCanvasSize.height()); 2867 kCanvasSize.height());
2785 { 2868 rect_buffer.EnsureSolidBorder(SK_ColorWHITE, bounds);
2786 SCOPED_TRACE("TextDoesClip Top Side");
2787 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2788 kTestSize);
2789 }
2790
2791 {
2792 SCOPED_TRACE("TextDoesClip Bottom Side");
2793 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height,
2794 kCanvasSize.width(), kTestSize);
2795 }
2796 {
2797 SCOPED_TRACE("TextDoesClip Left Side");
2798 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2799 fake_height);
2800 }
2801 {
2802 SCOPED_TRACE("TextDoesClip Right Side");
2803 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width,
2804 kTestSize, kTestSize, fake_height);
2805 }
2806 } 2869 }
2807 } 2870 }
2808 2871
2809 #if defined(OS_MACOSX) 2872 #if defined(OS_MACOSX)
2810 TEST_F(RenderTextTest, Mac_ElidedText) { 2873 TEST_F(RenderTextTest, Mac_ElidedText) {
2811 RenderTextMac render_text; 2874 RenderTextMac render_text;
2812 base::string16 text(ASCIIToUTF16("This is an example.")); 2875 base::string16 text(ASCIIToUTF16("This is an example."));
2813 render_text.SetText(text); 2876 render_text.SetText(text);
2814 gfx::Size string_size = render_text.GetStringSize(); 2877 gfx::Size string_size = render_text.GetStringSize();
2815 render_text.SetDisplayRect(gfx::Rect(string_size)); 2878 render_text.SetDisplayRect(gfx::Rect(string_size));
2816 render_text.EnsureLayout(); 2879 render_text.EnsureLayout();
2817 // NOTE: Character and glyph counts are only comparable for simple text. 2880 // NOTE: Character and glyph counts are only comparable for simple text.
2818 EXPECT_EQ(text.size(), 2881 EXPECT_EQ(text.size(),
2819 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_))); 2882 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_)));
2820 2883
2821 render_text.SetElideBehavior(ELIDE_TAIL); 2884 render_text.SetElideBehavior(ELIDE_TAIL);
2822 string_size.set_width(string_size.width() / 2); 2885 string_size.set_width(string_size.width() / 2);
2823 render_text.SetDisplayRect(gfx::Rect(string_size)); 2886 render_text.SetDisplayRect(gfx::Rect(string_size));
2824 render_text.EnsureLayout(); 2887 render_text.EnsureLayout();
2825 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_); 2888 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
2826 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count)); 2889 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
2827 EXPECT_NE(0, glyph_count); 2890 EXPECT_NE(0, glyph_count);
2828 } 2891 }
2829 #endif 2892 #endif
2830 2893
2831 } // namespace gfx 2894 } // namespace gfx
OLDNEW
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text.cc ('k') | ui/gfx/text_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698