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

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

Issue 1919123002: RenderTextMac: Cache the SkTypeface on the TextRun [reland] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes, and a test, but somewhat orthogonal Created 4 years, 7 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
« no previous file with comments | « ui/gfx/render_text_mac.mm ('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 <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 229
230 private: 230 private:
231 const wchar_t* string_; 231 const wchar_t* string_;
232 const SkColor* buffer_; 232 const SkColor* buffer_;
233 int stride_; 233 int stride_;
234 int row_count_; 234 int row_count_;
235 235
236 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer); 236 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
237 }; 237 };
238 238
239 // Helper to run the same test expectations on all RenderText backends.
240 class RenderTextAllBackends {
241 public:
242 RenderTextAllBackends() : renderer_(&canvas_), current_(nullptr) {}
243
244 bool Advance() {
245 if (!current_) {
246 current_ = &render_text_harfbuzz_;
247 return true;
248 }
249 #if defined(OS_MACOSX)
250 if (current_ == &render_text_harfbuzz_) {
251 current_ = &render_text_mac_;
252 return true;
253 }
254 #endif
255
256 return false;
257 }
258
259 const char* GetName() const {
260 return current_ == &render_text_harfbuzz_ ? "Harfbuzz" : "Mac";
261 }
262
263 RenderText* operator->() {
264 return current_;
265 }
266
267 void DrawVisualText() {
268 test::RenderTextTestApi test_api(current_);
269 test_api.DrawVisualText(&renderer_);
270 }
271
272 void GetTextLogAndReset(std::vector<TestSkiaTextRenderer::TextLog>* log) {
273 renderer_.GetTextLogAndReset(log);
274 }
275
276 SkTypeface* GetTypeface() {
277 SkPaint& paint = test::RenderTextTestApi::GetRendererPaint(&renderer_);
278 return paint.getTypeface();
279 }
280
281 private:
282 Canvas canvas_;
283 TestSkiaTextRenderer renderer_;
284 RenderText* current_;
285
286 RenderTextHarfBuzz render_text_harfbuzz_;
287 #if defined(OS_MACOSX)
288 RenderTextMac render_text_mac_;
289 #endif
290
291 DISALLOW_COPY_AND_ASSIGN(RenderTextAllBackends);
292 };
293
239 } // namespace 294 } // namespace
240 295
241 using RenderTextTest = testing::Test; 296 using RenderTextTest = testing::Test;
242 297
243 TEST_F(RenderTextTest, DefaultStyles) { 298 TEST_F(RenderTextTest, DefaultStyles) {
244 // Check the default styles applied to new instances and adjusted text. 299 // Check the default styles applied to new instances and adjusted text.
245 std::unique_ptr<RenderText> render_text(RenderText::CreateInstance()); 300 std::unique_ptr<RenderText> render_text(RenderText::CreateInstance());
246 EXPECT_TRUE(render_text->text().empty()); 301 EXPECT_TRUE(render_text->text().empty());
247 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; 302 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
248 for (size_t i = 0; i < arraysize(cases); ++i) { 303 for (size_t i = 0; i < arraysize(cases); ++i) {
(...skipping 2927 matching lines...) Expand 10 before | Expand all | Expand 10 after
3176 render_text.SetDisplayRect(gfx::Rect(string_size)); 3231 render_text.SetDisplayRect(gfx::Rect(string_size));
3177 render_text.EnsureLayout(); 3232 render_text.EnsureLayout();
3178 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_); 3233 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
3179 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count)); 3234 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
3180 EXPECT_NE(0, glyph_count); 3235 EXPECT_NE(0, glyph_count);
3181 } 3236 }
3182 #endif 3237 #endif
3183 3238
3184 // Ensure color changes are picked up by the RenderText implementation. 3239 // Ensure color changes are picked up by the RenderText implementation.
3185 TEST_F(RenderTextTest, ColorChange) { 3240 TEST_F(RenderTextTest, ColorChange) {
3186 RenderTextHarfBuzz render_text_harfbuzz; 3241 RenderTextAllBackends backend;
3187 #if defined(OS_MACOSX)
3188 RenderTextMac render_text_mac;
3189 #endif
3190 3242
3191 RenderText* backend[] = { 3243 while (backend.Advance()) {
3192 &render_text_harfbuzz, 3244 SCOPED_TRACE(testing::Message() << "backend: " << backend.GetName());
3193 #if defined(OS_MACOSX) 3245 backend->SetText(ASCIIToUTF16("x"));
3194 &render_text_mac, 3246 backend.DrawVisualText();
3195 #endif
3196 };
3197
3198 Canvas canvas;
3199 TestSkiaTextRenderer renderer(&canvas);
3200
3201 for (size_t i = 0; i < arraysize(backend); ++i) {
3202 SCOPED_TRACE(testing::Message() << "backend: " << i);
3203 test::RenderTextTestApi test_api(backend[i]);
3204 backend[i]->SetText(ASCIIToUTF16("x"));
3205 test_api.DrawVisualText(&renderer);
3206 3247
3207 std::vector<TestSkiaTextRenderer::TextLog> text_log; 3248 std::vector<TestSkiaTextRenderer::TextLog> text_log;
3208 3249 backend.GetTextLogAndReset(&text_log);
3209 renderer.GetTextLogAndReset(&text_log);
3210 EXPECT_EQ(1u, text_log.size()); 3250 EXPECT_EQ(1u, text_log.size());
3211 EXPECT_EQ(SK_ColorBLACK, text_log[0].color); 3251 EXPECT_EQ(SK_ColorBLACK, text_log[0].color);
3212 3252
3213 backend[i]->SetColor(SK_ColorRED); 3253 backend->SetColor(SK_ColorRED);
3214 test_api.DrawVisualText(&renderer); 3254 backend.DrawVisualText();
3255 backend.GetTextLogAndReset(&text_log);
3215 3256
3216 renderer.GetTextLogAndReset(&text_log);
3217 EXPECT_EQ(1u, text_log.size()); 3257 EXPECT_EQ(1u, text_log.size());
3218 EXPECT_EQ(SK_ColorRED, text_log[0].color); 3258 EXPECT_EQ(SK_ColorRED, text_log[0].color);
3219 } 3259 }
3220 } 3260 }
3221 3261
3262 // Ensure style information propagates to the typeface on the text renderer.
3263 TEST_F(RenderTextTest, StylePropagated) {
3264 RenderTextAllBackends backend;
3265
3266 // Default-constructed fonts on Mac are system fonts. These can have all kinds
3267 // of weird weights and style, which are preserved by PlatformFontMac, but do
3268 // not map simply to a SkTypeface::Style (the full details in SkFontStyle is
3269 // needed). They also vary depending on the OS version, so set a known font.
3270 gfx::FontList font_list(gfx::Font("Arial", 10));
3271
3272 while (backend.Advance()) {
3273 SCOPED_TRACE(testing::Message() << "backend: " << backend.GetName());
3274 backend->SetText(ASCIIToUTF16("x"));
3275 backend->SetFontList(font_list);
3276
3277 backend.DrawVisualText();
3278 EXPECT_EQ(SkTypeface::kNormal, backend.GetTypeface()->style());
3279
3280 backend->SetStyle(TextStyle::BOLD, true);
3281 backend.DrawVisualText();
3282 EXPECT_EQ(SkTypeface::kBold, backend.GetTypeface()->style());
3283
3284 backend->SetStyle(TextStyle::ITALIC, true);
3285 backend.DrawVisualText();
3286 EXPECT_EQ(SkTypeface::kBoldItalic, backend.GetTypeface()->style());
3287
3288 backend->SetStyle(TextStyle::BOLD, false);
3289 backend.DrawVisualText();
3290 EXPECT_EQ(SkTypeface::kItalic, backend.GetTypeface()->style());
3291 }
3292 }
3293
3222 } // namespace gfx 3294 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698