OLD | NEW |
---|---|
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 { | 17 namespace { |
20 | 18 |
21 // Mac-specific code for string size computations. This is a verbatim copy | 19 // 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. | 20 // of the old implementation that used to be in canvas_mac.mm. |
23 void CanvasMac_SizeStringInt(const base::string16& text, | 21 void CanvasMac_SizeStringInt(const base::string16& text, |
msw
2013/09/27 21:54:48
Is updating this important? Is it even worth keepi
jianli
2013/10/01 00:32:58
I do not know if it is worth to keep this test. I
| |
24 const FontList& font_list, | 22 const FontList& font_list, |
25 int* width, | 23 float* width, |
26 int* height, | 24 int* height, |
27 int line_height, | 25 int line_height, |
28 int flags) { | 26 int flags) { |
29 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; | 27 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; |
30 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; | 28 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; |
31 | 29 |
32 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont(); | 30 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont(); |
33 NSString* ns_string = base::SysUTF16ToNSString(text); | 31 NSString* ns_string = base::SysUTF16ToNSString(text); |
34 NSDictionary* attributes = | 32 NSDictionary* attributes = |
35 [NSDictionary dictionaryWithObject:native_font | 33 [NSDictionary dictionaryWithObject:native_font |
36 forKey:NSFontAttributeName]; | 34 forKey:NSFontAttributeName]; |
37 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | 35 NSSize string_size = [ns_string sizeWithAttributes:attributes]; |
38 *width = std::ceil(string_size.width); | 36 *width = string_size.width; |
39 *height = font_list.GetHeight(); | 37 *height = font_list.GetHeight(); |
40 } | 38 } |
41 | 39 |
42 } // namespace | 40 } // namespace |
43 | 41 |
44 class CanvasTestMac : public testing::Test { | 42 class CanvasTestMac : public testing::Test { |
45 protected: | 43 protected: |
46 // Compare the size returned by Canvas::SizeStringInt to the size generated | 44 // Compare the size returned by Canvas::SizeStringInt to the size generated |
47 // by the platform-specific version in CanvasMac_SizeStringInt. Will generate | 45 // by the platform-specific version in CanvasMac_SizeStringInt. Will generate |
48 // expectation failure on any mismatch. Only works for single-line text | 46 // expectation failure on any mismatch. Only works for single-line text |
49 // without specified line height, since that is all the platform | 47 // without specified line height, since that is all the platform |
50 // implementation supports. | 48 // implementation supports. |
51 void CompareSizes(const char* text) { | 49 void CompareSizes(const char* text) { |
52 const int kReallyLargeNumber = 12345678; | 50 const float kReallyLargeNumber = 12345678; |
53 FontList font_list(font_); | 51 FontList font_list(font_); |
54 base::string16 text16 = base::UTF8ToUTF16(text); | 52 base::string16 text16 = base::UTF8ToUTF16(text); |
55 | 53 |
56 int mac_width = kReallyLargeNumber; | 54 float mac_width = kReallyLargeNumber; |
57 int mac_height = kReallyLargeNumber; | 55 int mac_height = kReallyLargeNumber; |
58 CanvasMac_SizeStringInt(text16, font_list, &mac_width, &mac_height, 0, 0); | 56 CanvasMac_SizeStringInt(text16, font_list, &mac_width, &mac_height, 0, 0); |
59 | 57 |
60 int canvas_width = kReallyLargeNumber; | 58 float canvas_width = kReallyLargeNumber; |
61 int canvas_height = kReallyLargeNumber; | 59 float canvas_height = kReallyLargeNumber; |
62 Canvas::SizeStringInt( | 60 Canvas::SizeStringInt( |
63 text16, font_list, &canvas_width, &canvas_height, 0, 0); | 61 text16, font_list, &canvas_width, &canvas_height, 0, 0); |
64 | 62 |
65 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text; | 63 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text; |
66 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text; | 64 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text; |
67 EXPECT_EQ(mac_width, canvas_width) << " width for " << text; | 65 EXPECT_EQ(mac_width, canvas_width) << " width for " << text; |
68 EXPECT_EQ(mac_height, canvas_height) << " height for " << text; | 66 // mac_height is the truncated height. |
msw
2013/09/27 21:54:48
nit: Consider "CanvasMac_SizeStringInt returns a t
jianli
2013/10/01 00:32:58
Done.
| |
67 EXPECT_EQ(mac_height, | |
68 static_cast<int>(canvas_height)) << " height for " << text; | |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 Font font_; | 72 Font font_; |
73 }; | 73 }; |
74 | 74 |
75 // Tests that Canvas' SizeStringInt yields result consistent with a native | 75 // Tests that Canvas' SizeStringInt yields result consistent with a native |
76 // implementation. | 76 // implementation. |
77 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) { | 77 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) { |
78 CompareSizes(""); | 78 CompareSizes(""); |
79 CompareSizes("Foo"); | 79 CompareSizes("Foo"); |
80 CompareSizes("Longword"); | 80 CompareSizes("Longword"); |
81 CompareSizes("This is a complete sentence."); | 81 CompareSizes("This is a complete sentence."); |
82 } | 82 } |
83 | 83 |
84 } // namespace gfx | 84 } // namespace gfx |
OLD | NEW |