| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h" | 5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h" |
| 6 | 6 |
| 7 #import "base/mac/scoped_nsobject.h" | 7 #import "base/mac/scoped_nsobject.h" |
| 8 #import "ios/chrome/browser/ui/util/label_observer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/gtest_mac.h" | 10 #include "testing/gtest_mac.h" |
| 10 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 class UILabelAttributeUtilsTest : public PlatformTest { | 14 class UILabelAttributeUtilsTest : public PlatformTest { |
| 14 protected: | 15 protected: |
| 15 void SetUp() override { | 16 void SetUp() override { |
| 16 PlatformTest::SetUp(); | 17 PlatformTest::SetUp(); |
| 17 _scopedLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); | 18 _scopedLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); |
| 19 _observer.reset( |
| 20 [[LabelObserver observerForLabel:_scopedLabel.get()] retain]); |
| 21 [_observer startObserving]; |
| 18 } | 22 } |
| 19 | 23 |
| 24 ~UILabelAttributeUtilsTest() override { [_observer stopObserving]; } |
| 25 |
| 20 void CheckLabelLineHeight(CGFloat expected_height) { | 26 void CheckLabelLineHeight(CGFloat expected_height) { |
| 21 UILabel* label = _scopedLabel.get(); | 27 UILabel* label = _scopedLabel.get(); |
| 22 EXPECT_NE(nil, label.attributedText); | 28 EXPECT_NE(nil, label.attributedText); |
| 23 NSParagraphStyle* style = | 29 NSParagraphStyle* style = |
| 24 [label.attributedText attribute:NSParagraphStyleAttributeName | 30 [label.attributedText attribute:NSParagraphStyleAttributeName |
| 25 atIndex:0 | 31 atIndex:0 |
| 26 effectiveRange:nullptr]; | 32 effectiveRange:nullptr]; |
| 27 EXPECT_NE(nil, style); | 33 EXPECT_NE(nil, style); |
| 28 EXPECT_EQ(expected_height, style.maximumLineHeight); | 34 EXPECT_EQ(expected_height, style.maximumLineHeight); |
| 29 EXPECT_EQ(expected_height, label.cr_lineHeight); | 35 EXPECT_EQ(expected_height, label.cr_lineHeight); |
| 30 } | 36 } |
| 31 base::scoped_nsobject<UILabel> _scopedLabel; | 37 base::scoped_nsobject<UILabel> _scopedLabel; |
| 38 base::scoped_nsobject<LabelObserver> _observer; |
| 32 }; | 39 }; |
| 33 } | 40 } |
| 34 | 41 |
| 35 TEST_F(UILabelAttributeUtilsTest, CleanupTest) { | 42 TEST_F(UILabelAttributeUtilsTest, TwoObservers) { |
| 36 UILabel* label = _scopedLabel.get(); | 43 UILabel* label = _scopedLabel.get(); |
| 37 // Setting a lineheight will create an observer object. | 44 label.text = @"sample text"; |
| 45 |
| 46 // Add a second observer. |
| 47 base::scoped_nsobject<LabelObserver> secondObserver( |
| 48 [[LabelObserver observerForLabel:label] retain]); |
| 49 [secondObserver startObserving]; |
| 50 |
| 51 // Modify the line height with two observers. |
| 38 label.cr_lineHeight = 18.0; | 52 label.cr_lineHeight = 18.0; |
| 39 // The observer should stop observing cleanly when the label is deallocated. | 53 CheckLabelLineHeight(18.0); |
| 40 EXPECT_NO_FATAL_FAILURE(_scopedLabel.reset()); | 54 [secondObserver stopObserving]; |
| 55 secondObserver.reset(); |
| 56 |
| 57 // Even when one observer is stopped, the second is still observing. |
| 58 label.cr_lineHeight = 21.0; |
| 59 CheckLabelLineHeight(21.0); |
| 60 |
| 61 // Once both are stopped, the height isn't persisted when the text changes. |
| 62 [_observer stopObserving]; |
| 63 label.cr_lineHeight = 25.0; |
| 64 label.text = @"longer sample text"; |
| 65 CheckLabelLineHeight(0); |
| 41 } | 66 } |
| 42 | 67 |
| 43 TEST_F(UILabelAttributeUtilsTest, SettingTests) { | 68 TEST_F(UILabelAttributeUtilsTest, SettingTests) { |
| 44 base::scoped_nsobject<UILabel> scopedLabel( | 69 base::scoped_nsobject<UILabel> scopedLabel( |
| 45 [[UILabel alloc] initWithFrame:CGRectZero]); | 70 [[UILabel alloc] initWithFrame:CGRectZero]); |
| 46 UILabel* label = _scopedLabel.get(); | 71 UILabel* label = _scopedLabel.get(); |
| 47 | 72 |
| 48 // A label should have a line height of 0 if nothing has been specified yet. | 73 // A label should have a line height of 0 if nothing has been specified yet. |
| 49 EXPECT_EQ(0.0, label.cr_lineHeight); | 74 EXPECT_EQ(0.0, label.cr_lineHeight); |
| 50 | 75 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 style.get().maximumLineHeight = 15.0; | 117 style.get().maximumLineHeight = 15.0; |
| 93 [string addAttribute:NSParagraphStyleAttributeName | 118 [string addAttribute:NSParagraphStyleAttributeName |
| 94 value:style | 119 value:style |
| 95 range:NSMakeRange(0, [string length])]; | 120 range:NSMakeRange(0, [string length])]; |
| 96 | 121 |
| 97 label.cr_lineHeight = 19.0; | 122 label.cr_lineHeight = 19.0; |
| 98 EXPECT_EQ(19.0, label.cr_lineHeight); | 123 EXPECT_EQ(19.0, label.cr_lineHeight); |
| 99 label.attributedText = string; | 124 label.attributedText = string; |
| 100 CheckLabelLineHeight(19.0); | 125 CheckLabelLineHeight(19.0); |
| 101 } | 126 } |
| OLD | NEW |