OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #import "ui/ios/NSString+CrStringDrawing.h" | |
5 | |
6 #include "base/mac/scoped_nsobject.h" | |
7 #include "base/strings/stringprintf.h" | |
8 #include "base/strings/sys_string_conversions.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "testing/platform_test.h" | |
11 | |
12 namespace { | |
13 | |
14 typedef PlatformTest NSStringCrStringDrawing; | |
15 | |
16 // These tests verify that the category methods return the same values as the | |
17 // deprecated methods, so ignore warnings about using deprecated methods. | |
18 #pragma clang diagnostic push | |
19 #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
20 | |
21 // Verifies that |cr_boundingSizeWithSize| returns the same size as the | |
22 // deprecated |sizeWithFont:constrainedToSize| for most values. | |
23 // Note that the methods return different values in a few cases (so they are not | |
24 // included in the test cases): | |
25 // - the constrained size.width is less than a character. | |
26 // - the constrained size.height is less than the font height. | |
27 // - the string is empty. | |
28 TEST_F(NSStringCrStringDrawing, BoundingSizeWithSize) { | |
29 NSArray* fonts = @[ | |
30 [UIFont systemFontOfSize:16], | |
31 [UIFont boldSystemFontOfSize:10], | |
32 [UIFont fontWithName:@"Helvetica" size:12.0], | |
33 ]; | |
34 NSArray* strings = @[ | |
35 @"Test", | |
36 @"multi word test", | |
37 @"你好", | |
38 @"★ This is a test string that is very long.", | |
39 ]; | |
40 NSArray* sizes = @[ | |
41 [NSValue valueWithCGSize:CGSizeMake(20, 100)], | |
42 [NSValue valueWithCGSize:CGSizeMake(100, 100)], | |
43 [NSValue valueWithCGSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)], | |
44 ]; | |
45 for (UIFont* font in fonts) { | |
46 for (NSString* string in strings) { | |
47 for (NSValue* sizeValue in sizes) { | |
48 CGSize test_size = [sizeValue CGSizeValue]; | |
49 std::string test_tag = base::StringPrintf( | |
50 "for string '%s' with font %s and size %s", | |
51 base::SysNSStringToUTF8(string).c_str(), | |
52 base::SysNSStringToUTF8([font description]).c_str(), | |
53 base::SysNSStringToUTF8(NSStringFromCGSize(test_size)).c_str()); | |
54 | |
55 CGSize size_with_font = | |
56 [string sizeWithFont:font constrainedToSize:test_size]; | |
57 CGSize bounding_size = | |
58 [string cr_boundingSizeWithSize:test_size font:font]; | |
59 EXPECT_EQ(size_with_font.width, bounding_size.width) << test_tag; | |
60 EXPECT_EQ(size_with_font.height, bounding_size.height) << test_tag; | |
61 } | |
62 } | |
63 } | |
64 } | |
65 | |
66 TEST_F(NSStringCrStringDrawing, SizeWithFont) { | |
67 NSArray* fonts = @[ | |
68 [NSNull null], | |
69 [UIFont systemFontOfSize:16], | |
70 [UIFont boldSystemFontOfSize:10], | |
71 [UIFont fontWithName:@"Helvetica" size:12.0], | |
72 ]; | |
73 for (UIFont* font in fonts) { | |
74 if ([font isEqual:[NSNull null]]) | |
75 font = nil; | |
76 std::string font_tag = "with font "; | |
77 font_tag.append( | |
78 base::SysNSStringToUTF8(font ? [font description] : @"nil")); | |
79 EXPECT_EQ([@"" sizeWithFont:font].width, | |
80 [@"" cr_sizeWithFont:font].width) << font_tag; | |
81 EXPECT_EQ([@"" sizeWithFont:font].height, | |
82 [@"" cr_sizeWithFont:font].height) << font_tag; | |
83 EXPECT_EQ([@"Test" sizeWithFont:font].width, | |
84 [@"Test" cr_sizeWithFont:font].width) << font_tag; | |
85 EXPECT_EQ([@"Test" sizeWithFont:font].height, | |
86 [@"Test" cr_sizeWithFont:font].height) << font_tag; | |
87 EXPECT_EQ([@"你好" sizeWithFont:font].width, | |
88 [@"你好" cr_sizeWithFont:font].width) << font_tag; | |
89 EXPECT_EQ([@"你好" sizeWithFont:font].height, | |
90 [@"你好" cr_sizeWithFont:font].height) << font_tag; | |
91 NSString* long_string = @"★ This is a test string that is very long."; | |
92 EXPECT_EQ([long_string sizeWithFont:font].width, | |
93 [long_string cr_sizeWithFont:font].width) << font_tag; | |
94 EXPECT_EQ([long_string sizeWithFont:font].height, | |
95 [long_string cr_sizeWithFont:font].height) << font_tag; | |
96 } | |
97 } | |
98 #pragma clang diagnostic pop // ignored "-Wdeprecated-declarations" | |
99 | |
100 TEST_F(NSStringCrStringDrawing, PixelAlignedSizeWithFont) { | |
101 NSArray* fonts = @[ | |
102 [UIFont systemFontOfSize:16], | |
103 [UIFont boldSystemFontOfSize:10], | |
104 [UIFont fontWithName:@"Helvetica" size:12.0], | |
105 ]; | |
106 NSArray* strings = @[ | |
107 @"", | |
108 @"Test", | |
109 @"你好", | |
110 @"★ This is a test string that is very long.", | |
111 ]; | |
112 for (UIFont* font in fonts) { | |
113 NSDictionary* attributes = @{ NSFontAttributeName : font }; | |
114 | |
115 for (NSString* string in strings) { | |
116 std::string test_tag = base::StringPrintf("for string '%s' with font %s", | |
117 base::SysNSStringToUTF8(string).c_str(), | |
118 base::SysNSStringToUTF8([font description]).c_str()); | |
119 | |
120 CGSize size_with_attributes = [string sizeWithAttributes:attributes]; | |
121 CGSize size_with_pixel_aligned = | |
122 [string cr_pixelAlignedSizeWithFont:font]; | |
123 | |
124 // Verify that the pixel_aligned size is always rounded up (i.e. the size | |
125 // returned from sizeWithAttributes: is less than or equal to the pixel- | |
126 // aligned size). | |
127 EXPECT_LE(size_with_attributes.width, | |
128 size_with_pixel_aligned.width) << test_tag; | |
129 EXPECT_LE(size_with_attributes.height, | |
130 size_with_pixel_aligned.height) << test_tag; | |
131 | |
132 // Verify that the pixel_aligned size is never more than a pixel different | |
133 // than the size returned from sizeWithAttributes:. | |
134 static CGFloat scale = [[UIScreen mainScreen] scale]; | |
135 EXPECT_NEAR(size_with_attributes.width * scale, | |
136 size_with_pixel_aligned.width * scale, | |
137 0.9999) << test_tag; | |
138 EXPECT_NEAR(size_with_attributes.height * scale, | |
139 size_with_pixel_aligned.height * scale, | |
140 0.9999) << test_tag; | |
141 | |
142 // Verify that the pixel-aligned value is pixel-aligned. | |
143 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale), | |
144 size_with_pixel_aligned.width * scale) << test_tag; | |
145 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale), | |
146 size_with_pixel_aligned.height * scale) << test_tag; | |
147 } | |
148 } | |
149 } | |
150 | |
151 } // namespace | |
OLD | NEW |