• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12namespace {
13
14typedef PlatformTest NSStringCrStringDrawing;
15
16// These test verifies 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
21TEST_F(NSStringCrStringDrawing, SizeWithFont) {
22  NSArray* fonts = @[
23    [NSNull null],
24    [UIFont systemFontOfSize:16],
25    [UIFont boldSystemFontOfSize:10],
26    [UIFont fontWithName:@"Helvetica" size:12.0],
27  ];
28  for (UIFont* font in fonts) {
29    if ([font isEqual:[NSNull null]])
30      font = nil;
31    std::string font_tag = "with font ";
32    font_tag.append(
33        base::SysNSStringToUTF8(font ? [font description] : @"nil"));
34    EXPECT_EQ([@"" sizeWithFont:font].width,
35              [@"" cr_sizeWithFont:font].width) << font_tag;
36    EXPECT_EQ([@"" sizeWithFont:font].height,
37              [@"" cr_sizeWithFont:font].height) << font_tag;
38    EXPECT_EQ([@"Test" sizeWithFont:font].width,
39              [@"Test" cr_sizeWithFont:font].width) << font_tag;
40    EXPECT_EQ([@"Test" sizeWithFont:font].height,
41              [@"Test" cr_sizeWithFont:font].height) << font_tag;
42    EXPECT_EQ([@"你好" sizeWithFont:font].width,
43              [@"你好" cr_sizeWithFont:font].width) << font_tag;
44    EXPECT_EQ([@"你好" sizeWithFont:font].height,
45              [@"你好" cr_sizeWithFont:font].height) << font_tag;
46    NSString* long_string = @"★ This is a test string that is very long.";
47    EXPECT_EQ([long_string sizeWithFont:font].width,
48              [long_string cr_sizeWithFont:font].width) << font_tag;
49    EXPECT_EQ([long_string sizeWithFont:font].height,
50              [long_string cr_sizeWithFont:font].height) << font_tag;
51  }
52}
53#pragma clang diagnostic pop  // ignored "-Wdeprecated-declarations"
54
55TEST_F(NSStringCrStringDrawing, PixelAlignedSizeWithFont) {
56  NSArray* fonts = @[
57    [UIFont systemFontOfSize:16],
58    [UIFont boldSystemFontOfSize:10],
59    [UIFont fontWithName:@"Helvetica" size:12.0],
60  ];
61  NSArray* strings = @[
62    @"",
63    @"Test",
64    @"你好",
65    @"★ This is a test string that is very long.",
66  ];
67  for (UIFont* font in fonts) {
68    NSDictionary* attributes = @{ NSFontAttributeName : font };
69
70    for (NSString* string in strings) {
71      std::string test_tag = base::StringPrintf("for string '%s' with font %s",
72          base::SysNSStringToUTF8(string).c_str(),
73          base::SysNSStringToUTF8([font description]).c_str());
74
75      CGSize size_with_attributes = [string sizeWithAttributes:attributes];
76      CGSize size_with_pixel_aligned =
77          [string cr_pixelAlignedSizeWithFont:font];
78
79      // Verify that the pixel_aligned size is always rounded up (i.e. the size
80      // returned from sizeWithAttributes: is less than or equal to the pixel-
81      // aligned size).
82      EXPECT_LE(size_with_attributes.width,
83                size_with_pixel_aligned.width) << test_tag;
84      EXPECT_LE(size_with_attributes.height,
85                size_with_pixel_aligned.height) << test_tag;
86
87      // Verify that the pixel_aligned size is never more than a pixel different
88      // than the size returned from sizeWithAttributes:.
89      static CGFloat scale = [[UIScreen mainScreen] scale];
90      EXPECT_NEAR(size_with_attributes.width * scale,
91                  size_with_pixel_aligned.width * scale,
92                  0.9999) << test_tag;
93      EXPECT_NEAR(size_with_attributes.height * scale,
94                  size_with_pixel_aligned.height * scale,
95                  0.9999) << test_tag;
96
97      // Verify that the pixel-aligned value is pixel-aligned.
98      EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.width * scale),
99                      size_with_pixel_aligned.width * scale) << test_tag;
100      EXPECT_FLOAT_EQ(roundf(size_with_pixel_aligned.height * scale),
101                      size_with_pixel_aligned.height * scale) << test_tag;
102    }
103  }
104}
105
106
107}  // namespace
108