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

Side by Side Diff: ui/ios/NSString+CrStringDrawing_unittest.mm

Issue 704283002: [ui/ios] Add helper methods for getting the bounding size of some text. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix comment Created 6 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 #import "ui/ios/NSString+CrStringDrawing.h" 4 #import "ui/ios/NSString+CrStringDrawing.h"
5 5
6 #include "base/mac/scoped_nsobject.h" 6 #include "base/mac/scoped_nsobject.h"
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h" 10 #include "testing/platform_test.h"
11 11
12 namespace { 12 namespace {
13 13
14 typedef PlatformTest NSStringCrStringDrawing; 14 typedef PlatformTest NSStringCrStringDrawing;
15 15
16 // These test verifies that the category methods return the same values as the 16 // These tests verify that the category methods return the same values as the
17 // deprecated methods, so ignore warnings about using deprecated methods. 17 // deprecated methods, so ignore warnings about using deprecated methods.
18 #pragma clang diagnostic push 18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 19 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
20 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
21 TEST_F(NSStringCrStringDrawing, SizeWithFont) { 66 TEST_F(NSStringCrStringDrawing, SizeWithFont) {
22 NSArray* fonts = @[ 67 NSArray* fonts = @[
23 [NSNull null], 68 [NSNull null],
24 [UIFont systemFontOfSize:16], 69 [UIFont systemFontOfSize:16],
25 [UIFont boldSystemFontOfSize:10], 70 [UIFont boldSystemFontOfSize:10],
26 [UIFont fontWithName:@"Helvetica" size:12.0], 71 [UIFont fontWithName:@"Helvetica" size:12.0],
27 ]; 72 ];
28 for (UIFont* font in fonts) { 73 for (UIFont* font in fonts) {
29 if ([font isEqual:[NSNull null]]) 74 if ([font isEqual:[NSNull null]])
30 font = nil; 75 font = nil;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 141
97 // Verify that the pixel-aligned value is pixel-aligned. 142 // Verify that the pixel-aligned value is pixel-aligned.
98 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale), 143 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale),
99 size_with_pixel_aligned.width * scale) << test_tag; 144 size_with_pixel_aligned.width * scale) << test_tag;
100 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale), 145 EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale),
101 size_with_pixel_aligned.height * scale) << test_tag; 146 size_with_pixel_aligned.height * scale) << test_tag;
102 } 147 }
103 } 148 }
104 } 149 }
105 150
106
107 } // namespace 151 } // namespace
OLDNEW
« ui/ios/NSString+CrStringDrawing.h ('K') | « ui/ios/NSString+CrStringDrawing.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698