NSString+CrStringDrawing_unittest.mm 6.8 KB

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