File: NSString%2BCrStringDrawing_unittest.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (153 lines) | stat: -rw-r--r-- 6,128 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ui/gfx/ios/NSString+CrStringDrawing.h"

#import <UIKit/UIKit.h>

#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"

namespace {

typedef PlatformTest NSStringCrStringDrawing;

// These tests verify that the category methods return the same values as the
// deprecated methods, so ignore warnings about using deprecated methods.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

// Verifies that |cr_boundingSizeWithSize| returns the same size as the
// deprecated |sizeWithFont:constrainedToSize| for most values.
// Note that the methods return different values in a few cases (so they are not
// included in the test cases):
//  - the constrained size.width is less than a character.
//  - the constrained size.height is less than the font height.
//  - the string is empty.
TEST_F(NSStringCrStringDrawing, BoundingSizeWithSize) {
  NSArray* fonts = @[
    [UIFont systemFontOfSize:16],
    [UIFont boldSystemFontOfSize:10],
    [UIFont fontWithName:@"Helvetica" size:12.0],
  ];
  NSArray* strings = @[
    @"Test",
    @"multi word test",
    @"你好",
    @"★ This is a test string that is very long.",
  ];
  NSArray* sizes = @[
    [NSValue valueWithCGSize:CGSizeMake(20, 100)],
    [NSValue valueWithCGSize:CGSizeMake(100, 100)],
    [NSValue valueWithCGSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)],
  ];
  for (UIFont* font in fonts) {
    for (NSString* string in strings) {
      for (NSValue* sizeValue in sizes) {
        CGSize test_size = [sizeValue CGSizeValue];
        std::string test_tag = base::StringPrintf(
            "for string '%s' with font %s and size %s",
            base::SysNSStringToUTF8(string).c_str(),
            base::SysNSStringToUTF8([font description]).c_str(),
            base::SysNSStringToUTF8(NSStringFromCGSize(test_size)).c_str());

        CGSize size_with_font =
            [string sizeWithFont:font constrainedToSize:test_size];
        CGSize bounding_size =
            [string cr_boundingSizeWithSize:test_size font:font];
        EXPECT_EQ(size_with_font.width, bounding_size.width) << test_tag;
        EXPECT_EQ(size_with_font.height, bounding_size.height) << test_tag;
      }
    }
  }
}

TEST_F(NSStringCrStringDrawing, SizeWithFont) {
  NSArray* fonts = @[
    [NSNull null],
    [UIFont systemFontOfSize:16],
    [UIFont boldSystemFontOfSize:10],
    [UIFont fontWithName:@"Helvetica" size:12.0],
  ];
  for (UIFont* __strong font in fonts) {
    if ([font isEqual:[NSNull null]])
      font = nil;
    std::string font_tag = "with font ";
    font_tag.append(
        base::SysNSStringToUTF8(font ? [font description] : @"nil"));
    EXPECT_EQ([@"" sizeWithFont:font].width,
              [@"" cr_sizeWithFont:font].width) << font_tag;
    EXPECT_EQ([@"" sizeWithFont:font].height,
              [@"" cr_sizeWithFont:font].height) << font_tag;
    EXPECT_EQ([@"Test" sizeWithFont:font].width,
              [@"Test" cr_sizeWithFont:font].width) << font_tag;
    EXPECT_EQ([@"Test" sizeWithFont:font].height,
              [@"Test" cr_sizeWithFont:font].height) << font_tag;
    EXPECT_EQ([@"你好" sizeWithFont:font].width,
              [@"你好" cr_sizeWithFont:font].width) << font_tag;
    EXPECT_EQ([@"你好" sizeWithFont:font].height,
              [@"你好" cr_sizeWithFont:font].height) << font_tag;
    NSString* long_string = @"★ This is a test string that is very long.";
    EXPECT_EQ([long_string sizeWithFont:font].width,
              [long_string cr_sizeWithFont:font].width) << font_tag;
    EXPECT_EQ([long_string sizeWithFont:font].height,
              [long_string cr_sizeWithFont:font].height) << font_tag;
  }
}
#pragma clang diagnostic pop  // ignored "-Wdeprecated-declarations"

TEST_F(NSStringCrStringDrawing, PixelAlignedSizeWithFont) {
  NSArray* fonts = @[
    [UIFont systemFontOfSize:16],
    [UIFont boldSystemFontOfSize:10],
    [UIFont fontWithName:@"Helvetica" size:12.0],
  ];
  NSArray* strings = @[
    @"",
    @"Test",
    @"你好",
    @"★ This is a test string that is very long.",
  ];
  for (UIFont* font in fonts) {
    NSDictionary* attributes = @{ NSFontAttributeName : font };

    for (NSString* string in strings) {
      std::string test_tag = base::StringPrintf("for string '%s' with font %s",
          base::SysNSStringToUTF8(string).c_str(),
          base::SysNSStringToUTF8([font description]).c_str());

      CGSize size_with_attributes = [string sizeWithAttributes:attributes];
      CGSize size_with_pixel_aligned =
          [string cr_pixelAlignedSizeWithFont:font];

      // Verify that the pixel_aligned size is always rounded up (i.e. the size
      // returned from sizeWithAttributes: is less than or equal to the pixel-
      // aligned size).
      EXPECT_LE(size_with_attributes.width,
                size_with_pixel_aligned.width) << test_tag;
      EXPECT_LE(size_with_attributes.height,
                size_with_pixel_aligned.height) << test_tag;

      // Verify that the pixel_aligned size is never more than a pixel different
      // than the size returned from sizeWithAttributes:.
      static CGFloat scale = [[UIScreen mainScreen] scale];
      EXPECT_NEAR(size_with_attributes.width * scale,
                  size_with_pixel_aligned.width * scale,
                  0.9999) << test_tag;
      EXPECT_NEAR(size_with_attributes.height * scale,
                  size_with_pixel_aligned.height * scale,
                  0.9999) << test_tag;

      // Verify that the pixel-aligned value is pixel-aligned.
      EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale),
                      size_with_pixel_aligned.width * scale) << test_tag;
      EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale),
                      size_with_pixel_aligned.height * scale) << test_tag;
    }
  }
}

}  // namespace