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

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

Issue 915383003: Fix alignment format in multiline mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text_harfbuzz.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkSurface.h" 17 #include "third_party/skia/include/core/SkSurface.h"
17 #include "ui/gfx/break_list.h" 18 #include "ui/gfx/break_list.h"
18 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/color_utils.h" 20 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/font.h" 21 #include "ui/gfx/font.h"
21 #include "ui/gfx/range/range.h" 22 #include "ui/gfx/range/range.h"
(...skipping 2114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 // by symbols, therefore the character should be changed. 2137 // by symbols, therefore the character should be changed.
2137 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText()); 2138 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2138 2139
2139 // Setting multiline will fix this, the newline characters will be back 2140 // Setting multiline will fix this, the newline characters will be back
2140 // to the original text. 2141 // to the original text.
2141 render_text.SetMultiline(true); 2142 render_text.SetMultiline(true);
2142 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText()); 2143 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2143 } 2144 }
2144 } 2145 }
2145 2146
2147 // Ensure RTL alignment works.
msw 2015/02/23 19:30:07 nit: Is this really specific to RTL or just multi-
oshima 2015/02/23 23:06:52 I added RTL case for assertion purpose, but update
2148 TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
2149 const struct {
2150 const wchar_t* const text;
2151 const gfx::HorizontalAlignment alignment;
2152 } kTestStrings[] = {
2153 { L"abcdefghij\nhijkl", gfx::ALIGN_LEFT},
msw 2015/02/23 19:30:07 nit: add spaces before closing curly braces throug
msw 2015/02/23 19:30:07 nit: maybe add an english, 2nd line longer case? (
oshima 2015/02/23 23:06:52 Done.
oshima 2015/02/23 23:06:52 Done.
2154 // hebrew, 2nd line shorter
2155 { L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2156 gfx::ALIGN_RIGHT},
2157 // hebrew, 2nd line longer
2158 { L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2159 gfx::ALIGN_RIGHT},
2160 // arabic, 2nd line shorter
2161 { L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2162 gfx::ALIGN_RIGHT},
2163 // arabic, 2nd line longer
2164 { L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2165 gfx::ALIGN_RIGHT},
2166 };
2167 const int kGlyphSize = 5;
2168 RenderTextHarfBuzz render_text;
2169 render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
2170 render_text.set_glyph_width_for_test(kGlyphSize);
2171 render_text.SetDisplayRect(Rect(100, 1000));
2172 render_text.SetMultiline(true);
2173
2174 Canvas canvas;
2175 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2176 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
2177 kTestStrings[i].text));
2178 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2179 render_text.Draw(&canvas);
2180 ASSERT_LE(2u, render_text.lines().size());
2181 if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
2182 EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
2183 EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
2184 } else {
2185 std::vector<base::string16> lines;
2186 base::SplitString(base::WideToUTF16(kTestStrings[i].text), '\n', &lines);
2187 ASSERT_EQ(2u, lines.size());
2188 int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
2189 EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
2190 render_text.GetAlignmentOffset(1).x());
2191 }
2192 }
2193 }
2194
2146 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) { 2195 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
2147 const wchar_t* kTestStrings[] = { 2196 const wchar_t* kTestStrings[] = {
2148 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n", 2197 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2149 }; 2198 };
2150 2199
2151 RenderTextHarfBuzz render_text; 2200 RenderTextHarfBuzz render_text;
2152 render_text.SetDisplayRect(Rect(200, 1000)); 2201 render_text.SetDisplayRect(Rect(200, 1000));
2153 Canvas canvas; 2202 Canvas canvas;
2154 2203
2155 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 2204 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
2552 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string; 2601 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string;
2553 for (int x = 1; x < kTestWidth; ++x) { 2602 for (int x = 1; x < kTestWidth; ++x) {
2554 color = buffer[width + x + y * kCanvasSize.width()]; 2603 color = buffer[width + x + y * kCanvasSize.width()];
2555 EXPECT_EQ(SK_ColorWHITE, color) << string; 2604 EXPECT_EQ(SK_ColorWHITE, color) << string;
2556 } 2605 }
2557 } 2606 }
2558 } 2607 }
2559 } 2608 }
2560 2609
2561 } // namespace gfx 2610 } // namespace gfx
OLDNEW
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text_harfbuzz.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698