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/label_observer.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/time/time.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/gtest_mac.h" |
| 11 #include "testing/platform_test.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class LabelObserverTest : public PlatformTest { |
| 17 protected: |
| 18 void SetUp() override { |
| 19 label_.reset([[UILabel alloc] initWithFrame:CGRectZero]); |
| 20 } |
| 21 |
| 22 UILabel* label() { return label_.get(); } |
| 23 LabelObserver* observer() { return [LabelObserver observerForLabel:label()]; } |
| 24 |
| 25 base::scoped_nsobject<UILabel> label_; |
| 26 }; |
| 27 |
| 28 // Tests that all types of LabelObserverActions are successfully called. |
| 29 TEST_F(LabelObserverTest, SimpleTest) { |
| 30 __block BOOL text_action_called = NO; |
| 31 [observer() addTextChangedAction:^(UILabel* label) { |
| 32 text_action_called = YES; |
| 33 }]; |
| 34 label().text = @"text"; |
| 35 EXPECT_TRUE(text_action_called); |
| 36 __block BOOL layout_action_called = NO; |
| 37 [observer() addLayoutChangedAction:^(UILabel* label) { |
| 38 layout_action_called = YES; |
| 39 }]; |
| 40 [label() sizeToFit]; |
| 41 EXPECT_TRUE(layout_action_called); |
| 42 __block BOOL style_action_called = NO; |
| 43 [observer() addStyleChangedAction:^(UILabel* label) { |
| 44 style_action_called = YES; |
| 45 }]; |
| 46 label().textColor = [UIColor blueColor]; |
| 47 EXPECT_TRUE(style_action_called); |
| 48 } |
| 49 |
| 50 // Tests that a LabelObserverAction that causes another KVO callback does not |
| 51 // get called twice |
| 52 TEST_F(LabelObserverTest, CallCountTest) { |
| 53 __block NSUInteger text_action_call_count = 0; |
| 54 [observer() addTextChangedAction:^(UILabel* label) { |
| 55 ++text_action_call_count; |
| 56 label.text = [label.text stringByAppendingString:@"x"]; |
| 57 }]; |
| 58 label().text = @"x"; |
| 59 EXPECT_EQ(1U, text_action_call_count); |
| 60 EXPECT_NSEQ(label().text, @"xx"); |
| 61 } |
| 62 |
| 63 // Tests that LabelObserverActions are called in the same order in which they |
| 64 // are added. |
| 65 TEST_F(LabelObserverTest, ObserverActionOrderTest) { |
| 66 __block BOOL first_action_called = NO; |
| 67 __block BOOL second_action_called = NO; |
| 68 [observer() addTextChangedAction:^(UILabel* label) { |
| 69 EXPECT_FALSE(first_action_called); |
| 70 EXPECT_FALSE(second_action_called); |
| 71 first_action_called = YES; |
| 72 }]; |
| 73 [observer() addTextChangedAction:^(UILabel* label) { |
| 74 EXPECT_TRUE(first_action_called); |
| 75 EXPECT_FALSE(second_action_called); |
| 76 second_action_called = YES; |
| 77 }]; |
| 78 label().text = @"test"; |
| 79 EXPECT_TRUE(first_action_called); |
| 80 EXPECT_TRUE(second_action_called); |
| 81 } |
| 82 |
| 83 } // namespace |
OLD | NEW |