Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / gfx / ios / NSString+CrStringDrawing_unittest.mm
blob874cdb1421e106e6b6384af179b1d15cb3be2ab2
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/gfx/ios/NSString+CrStringDrawing.h"
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_mac.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
13 namespace {
15 typedef PlatformTest NSStringCrStringDrawing;
17 // These tests verify that the category methods return the same values as the
18 // deprecated methods, so ignore warnings about using deprecated methods.
19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
22 // Verifies that |cr_boundingSizeWithSize| returns the same size as the
23 // deprecated |sizeWithFont:constrainedToSize| for most values.
24 // Note that the methods return different values in a few cases (so they are not
25 // included in the test cases):
26 //  - the constrained size.width is less than a character.
27 //  - the constrained size.height is less than the font height.
28 //  - the string is empty.
29 TEST_F(NSStringCrStringDrawing, BoundingSizeWithSize) {
30   NSArray* fonts = @[
31     [UIFont systemFontOfSize:16],
32     [UIFont boldSystemFontOfSize:10],
33     [UIFont fontWithName:@"Helvetica" size:12.0],
34   ];
35   NSArray* strings = @[
36     @"Test",
37     @"multi word test",
38     @"你好",
39     @"★ This is a test string that is very long.",
40   ];
41   NSArray* sizes = @[
42     [NSValue valueWithCGSize:CGSizeMake(20, 100)],
43     [NSValue valueWithCGSize:CGSizeMake(100, 100)],
44     [NSValue valueWithCGSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)],
45   ];
46   for (UIFont* font in fonts) {
47     for (NSString* string in strings) {
48       for (NSValue* sizeValue in sizes) {
49         CGSize test_size = [sizeValue CGSizeValue];
50         std::string test_tag = base::StringPrintf(
51             "for string '%s' with font %s and size %s",
52             base::SysNSStringToUTF8(string).c_str(),
53             base::SysNSStringToUTF8([font description]).c_str(),
54             base::SysNSStringToUTF8(NSStringFromCGSize(test_size)).c_str());
56         CGSize size_with_font =
57             [string sizeWithFont:font constrainedToSize:test_size];
58         CGSize bounding_size =
59             [string cr_boundingSizeWithSize:test_size font:font];
60         EXPECT_EQ(size_with_font.width, bounding_size.width) << test_tag;
61         EXPECT_EQ(size_with_font.height, bounding_size.height) << test_tag;
62       }
63     }
64   }
67 TEST_F(NSStringCrStringDrawing, SizeWithFont) {
68   NSArray* fonts = @[
69     [NSNull null],
70     [UIFont systemFontOfSize:16],
71     [UIFont boldSystemFontOfSize:10],
72     [UIFont fontWithName:@"Helvetica" size:12.0],
73   ];
74   for (UIFont* font in fonts) {
75     if ([font isEqual:[NSNull null]])
76       font = nil;
77     std::string font_tag = "with font ";
78     font_tag.append(
79         base::SysNSStringToUTF8(font ? [font description] : @"nil"));
80     EXPECT_EQ([@"" sizeWithFont:font].width,
81               [@"" cr_sizeWithFont:font].width) << font_tag;
82     EXPECT_EQ([@"" sizeWithFont:font].height,
83               [@"" cr_sizeWithFont:font].height) << font_tag;
84     EXPECT_EQ([@"Test" sizeWithFont:font].width,
85               [@"Test" cr_sizeWithFont:font].width) << font_tag;
86     EXPECT_EQ([@"Test" sizeWithFont:font].height,
87               [@"Test" cr_sizeWithFont:font].height) << font_tag;
88     EXPECT_EQ([@"你好" sizeWithFont:font].width,
89               [@"你好" cr_sizeWithFont:font].width) << font_tag;
90     EXPECT_EQ([@"你好" sizeWithFont:font].height,
91               [@"你好" cr_sizeWithFont:font].height) << font_tag;
92     NSString* long_string = @"★ This is a test string that is very long.";
93     EXPECT_EQ([long_string sizeWithFont:font].width,
94               [long_string cr_sizeWithFont:font].width) << font_tag;
95     EXPECT_EQ([long_string sizeWithFont:font].height,
96               [long_string cr_sizeWithFont:font].height) << font_tag;
97   }
99 #pragma clang diagnostic pop  // ignored "-Wdeprecated-declarations"
101 TEST_F(NSStringCrStringDrawing, PixelAlignedSizeWithFont) {
102   NSArray* fonts = @[
103     [UIFont systemFontOfSize:16],
104     [UIFont boldSystemFontOfSize:10],
105     [UIFont fontWithName:@"Helvetica" size:12.0],
106   ];
107   NSArray* strings = @[
108     @"",
109     @"Test",
110     @"你好",
111     @"★ This is a test string that is very long.",
112   ];
113   for (UIFont* font in fonts) {
114     NSDictionary* attributes = @{ NSFontAttributeName : font };
116     for (NSString* string in strings) {
117       std::string test_tag = base::StringPrintf("for string '%s' with font %s",
118           base::SysNSStringToUTF8(string).c_str(),
119           base::SysNSStringToUTF8([font description]).c_str());
121       CGSize size_with_attributes = [string sizeWithAttributes:attributes];
122       CGSize size_with_pixel_aligned =
123           [string cr_pixelAlignedSizeWithFont:font];
125       // Verify that the pixel_aligned size is always rounded up (i.e. the size
126       // returned from sizeWithAttributes: is less than or equal to the pixel-
127       // aligned size).
128       EXPECT_LE(size_with_attributes.width,
129                 size_with_pixel_aligned.width) << test_tag;
130       EXPECT_LE(size_with_attributes.height,
131                 size_with_pixel_aligned.height) << test_tag;
133       // Verify that the pixel_aligned size is never more than a pixel different
134       // than the size returned from sizeWithAttributes:.
135       static CGFloat scale = [[UIScreen mainScreen] scale];
136       EXPECT_NEAR(size_with_attributes.width * scale,
137                   size_with_pixel_aligned.width * scale,
138                   0.9999) << test_tag;
139       EXPECT_NEAR(size_with_attributes.height * scale,
140                   size_with_pixel_aligned.height * scale,
141                   0.9999) << test_tag;
143       // Verify that the pixel-aligned value is pixel-aligned.
144       EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale),
145                       size_with_pixel_aligned.width * scale) << test_tag;
146       EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale),
147                       size_with_pixel_aligned.height * scale) << test_tag;
148     }
149   }
152 TEST_F(NSStringCrStringDrawing, CutString) {
153   EXPECT_NSEQ(@"foo", [@"foo" cr_stringByCuttingToIndex:4]);
154   EXPECT_NSEQ(@"bar", [@"bar" cr_stringByCuttingToIndex:3]);
155   EXPECT_NSEQ(@"f…", [@"foo" cr_stringByCuttingToIndex:2]);
156   EXPECT_NSEQ(@"…", [@"bar" cr_stringByCuttingToIndex:1]);
157   EXPECT_NSEQ(@"", [@"foo" cr_stringByCuttingToIndex:0]);
160 TEST_F(NSStringCrStringDrawing, ElideStringToFitInRect) {
161   NSString* result =
162       [@"lorem ipsum dolores" cr_stringByElidingToFitSize:CGSizeZero];
163   EXPECT_NSEQ(@"", result);
164   result = [@"lorem ipsum dolores"
165       cr_stringByElidingToFitSize:CGSizeMake(1000, 1000)];
166   EXPECT_NSEQ(@"lorem ipsum dolores", result);
167   result =
168       [@"lorem ipsum dolores" cr_stringByElidingToFitSize:CGSizeMake(30, 50)];
169   EXPECT_TRUE([@"lorem ipsum dolores" length] > [result length]);
172 }  // namespace