OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 |
| 5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/gtest_mac.h" |
| 10 #include "testing/platform_test.h" |
| 11 |
| 12 namespace { |
| 13 class UILabelAttributeUtilsTest : public PlatformTest { |
| 14 protected: |
| 15 void SetUp() override { |
| 16 PlatformTest::SetUp(); |
| 17 _scopedLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); |
| 18 } |
| 19 |
| 20 void CheckLabelLineHeight(CGFloat expected_height) { |
| 21 UILabel* label = _scopedLabel.get(); |
| 22 EXPECT_NE(nil, label.attributedText); |
| 23 NSParagraphStyle* style = |
| 24 [label.attributedText attribute:NSParagraphStyleAttributeName |
| 25 atIndex:0 |
| 26 effectiveRange:nullptr]; |
| 27 EXPECT_NE(nil, style); |
| 28 EXPECT_EQ(expected_height, style.maximumLineHeight); |
| 29 EXPECT_EQ(expected_height, label.cr_lineHeight); |
| 30 } |
| 31 base::scoped_nsobject<UILabel> _scopedLabel; |
| 32 }; |
| 33 } |
| 34 |
| 35 TEST_F(UILabelAttributeUtilsTest, CleanupTest) { |
| 36 UILabel* label = _scopedLabel.get(); |
| 37 // Setting a lineheight will create an observer object. |
| 38 label.cr_lineHeight = 18.0; |
| 39 // The observer should stop observing cleanly when the label is deallocated. |
| 40 EXPECT_NO_FATAL_FAILURE(_scopedLabel.reset()); |
| 41 } |
| 42 |
| 43 TEST_F(UILabelAttributeUtilsTest, SettingTests) { |
| 44 base::scoped_nsobject<UILabel> scopedLabel( |
| 45 [[UILabel alloc] initWithFrame:CGRectZero]); |
| 46 UILabel* label = _scopedLabel.get(); |
| 47 |
| 48 // A label should have a line height of 0 if nothing has been specified yet. |
| 49 EXPECT_EQ(0.0, label.cr_lineHeight); |
| 50 |
| 51 label.text = @"sample text"; |
| 52 label.cr_lineHeight = 20.0; |
| 53 CheckLabelLineHeight(20.0); |
| 54 label.text = @"longer sample text"; |
| 55 CheckLabelLineHeight(20.0); |
| 56 |
| 57 base::scoped_nsobject<NSMutableAttributedString> string( |
| 58 [[NSMutableAttributedString alloc] |
| 59 initWithString:@"attributed sample text"]); |
| 60 label.attributedText = string; |
| 61 CheckLabelLineHeight(20.0); |
| 62 [string addAttribute:NSForegroundColorAttributeName |
| 63 value:[UIColor brownColor] |
| 64 range:NSMakeRange(0, [string length])]; |
| 65 label.attributedText = string; |
| 66 CheckLabelLineHeight(20.0); |
| 67 base::scoped_nsobject<NSMutableParagraphStyle> style( |
| 68 [[NSMutableParagraphStyle alloc] init]); |
| 69 style.get().maximumLineHeight = 15.0; |
| 70 [string addAttribute:NSParagraphStyleAttributeName |
| 71 value:style |
| 72 range:NSMakeRange(0, [string length])]; |
| 73 CheckLabelLineHeight(20.0); |
| 74 } |
| 75 |
| 76 TEST_F(UILabelAttributeUtilsTest, NullTextTest) { |
| 77 UILabel* label = _scopedLabel.get(); |
| 78 |
| 79 label.cr_lineHeight = 19.0; |
| 80 EXPECT_EQ(19.0, label.cr_lineHeight); |
| 81 label.text = @"sample text"; |
| 82 CheckLabelLineHeight(19.0); |
| 83 } |
| 84 |
| 85 TEST_F(UILabelAttributeUtilsTest, NullAttrTextTest) { |
| 86 UILabel* label = _scopedLabel.get(); |
| 87 base::scoped_nsobject<NSMutableAttributedString> string( |
| 88 [[NSMutableAttributedString alloc] |
| 89 initWithString:@"attributed sample text"]); |
| 90 base::scoped_nsobject<NSMutableParagraphStyle> style( |
| 91 [[NSMutableParagraphStyle alloc] init]); |
| 92 style.get().maximumLineHeight = 15.0; |
| 93 [string addAttribute:NSParagraphStyleAttributeName |
| 94 value:style |
| 95 range:NSMakeRange(0, [string length])]; |
| 96 |
| 97 label.cr_lineHeight = 19.0; |
| 98 EXPECT_EQ(19.0, label.cr_lineHeight); |
| 99 label.attributedText = string; |
| 100 CheckLabelLineHeight(19.0); |
| 101 } |
OLD | NEW |