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

Side by Side Diff: ui/gfx/canvas_unittest_mac.mm

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More fixes per feedback plus new tests Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/canvas.h" 5 #include "ui/gfx/canvas.h"
6 6
7 #include <cmath>
8
9 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
10 8
11 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
12 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/font.h" 12 #include "ui/gfx/font.h"
15 #include "ui/gfx/font_list.h" 13 #include "ui/gfx/font_list.h"
16 14
17 namespace gfx { 15 namespace gfx {
18 16
19 namespace {
20
21 // Mac-specific code for string size computations. This is a verbatim copy
22 // of the old implementation that used to be in canvas_mac.mm.
23 void CanvasMac_SizeStringInt(const base::string16& text,
Alexei Svitkine (slow) 2013/10/01 19:04:55 I'd rather you keep this as a standalone function.
msw 2013/10/01 19:14:53 Why do we care? What's so great about this impleme
Alexei Svitkine (slow) 2013/10/01 19:19:50 I'm okay with changing the signature / simplifying
jianli 2013/10/01 21:34:28 Added back the helper function with simplification
24 const FontList& font_list,
25 int* width,
26 int* height,
27 int line_height,
28 int flags) {
29 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented.";
30 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented.";
31
32 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont();
33 NSString* ns_string = base::SysUTF16ToNSString(text);
34 NSDictionary* attributes =
35 [NSDictionary dictionaryWithObject:native_font
36 forKey:NSFontAttributeName];
37 NSSize string_size = [ns_string sizeWithAttributes:attributes];
38 *width = std::ceil(string_size.width);
39 *height = font_list.GetHeight();
40 }
41
42 } // namespace
43
44 class CanvasTestMac : public testing::Test { 17 class CanvasTestMac : public testing::Test {
45 protected: 18 protected:
46 // Compare the size returned by Canvas::SizeStringInt to the size generated 19 // Compare the size returned by Canvas::SizeStringToFit to the size generated
47 // by the platform-specific version in CanvasMac_SizeStringInt. Will generate 20 // by the platform-specific version in CanvasMac_SizeStringToFit. Will
48 // expectation failure on any mismatch. Only works for single-line text 21 // generate expectation failure on any mismatch. Only works for single-line
49 // without specified line height, since that is all the platform 22 // text without specified line height, since that is all the platform
50 // implementation supports. 23 // implementation supports.
51 void CompareSizes(const char* text) { 24 void CompareSizes(const char* text) {
52 const int kReallyLargeNumber = 12345678; 25 const float kReallyLargeNumber = 12345678;
53 FontList font_list(font_); 26 FontList font_list(font_);
54 base::string16 text16 = base::UTF8ToUTF16(text); 27 base::string16 text16 = base::UTF8ToUTF16(text);
55 28
56 int mac_width = kReallyLargeNumber; 29 // Compute the size via Mac-specific code.
57 int mac_height = kReallyLargeNumber; 30 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont();
58 CanvasMac_SizeStringInt(text16, font_list, &mac_width, &mac_height, 0, 0); 31 NSString* ns_string = base::SysUTF16ToNSString(text16);
32 NSDictionary* attributes =
33 [NSDictionary dictionaryWithObject:native_font
34 forKey:NSFontAttributeName];
35 float mac_width = [ns_string sizeWithAttributes:attributes].width;
36 int mac_height = font_list.GetHeight();
59 37
60 int canvas_width = kReallyLargeNumber; 38 // Compute the size via Canvas function.
61 int canvas_height = kReallyLargeNumber; 39 float canvas_width = kReallyLargeNumber;
62 Canvas::SizeStringInt( 40 float canvas_height = kReallyLargeNumber;
41 Canvas::SizeStringToFit(
63 text16, font_list, &canvas_width, &canvas_height, 0, 0); 42 text16, font_list, &canvas_width, &canvas_height, 0, 0);
64 43
65 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text; 44 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text;
66 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text; 45 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text;
67 EXPECT_EQ(mac_width, canvas_width) << " width for " << text; 46 EXPECT_EQ(mac_width, canvas_width) << " width for " << text;
68 EXPECT_EQ(mac_height, canvas_height) << " height for " << text; 47 // FontList::GetHeight returns a truncated height.
48 EXPECT_EQ(mac_height,
49 static_cast<int>(canvas_height)) << " height for " << text;
69 } 50 }
70 51
71 private: 52 private:
72 Font font_; 53 Font font_;
73 }; 54 };
74 55
75 // Tests that Canvas' SizeStringInt yields result consistent with a native 56 // Tests that Canvas' SizeStringToFit yields result consistent with a native
76 // implementation. 57 // implementation.
77 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) { 58 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) {
78 CompareSizes(""); 59 CompareSizes("");
79 CompareSizes("Foo"); 60 CompareSizes("Foo");
80 CompareSizes("Longword"); 61 CompareSizes("Longword");
81 CompareSizes("This is a complete sentence."); 62 CompareSizes("This is a complete sentence.");
82 } 63 }
83 64
65 TEST_F(CanvasTestMac, FractionalWidth) {
66 const float kReallyLargeNumber = 12345678;
67 float width = kReallyLargeNumber;
68 float height = kReallyLargeNumber;
69
70 Font font;
msw 2013/10/01 19:14:53 nit: Just construct a FontList here instead of con
jianli 2013/10/01 21:34:28 Done.
71 Canvas::SizeStringToFit(
72 base::UTF8ToUTF16("Test"), FontList(font), &width, &height, 0, 0);
73
74 EXPECT_GT(width - static_cast<int>(width), 0);
75
Alexei Svitkine (slow) 2013/10/01 19:04:55 Nit: Remove empty line.
jianli 2013/10/01 21:34:28 Done.
76 }
77
84 } // namespace gfx 78 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas_unittest.cc ('k') | ui/gfx/font.h » ('j') | ui/gfx/font.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698