OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 #import "ios/chrome/browser/ui/infobars/infobar_view.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "testing/gtest_mac.h" |
| 9 #include "testing/platform_test.h" |
| 10 |
| 11 @interface InfoBarView (Testing) |
| 12 - (CGFloat)buttonsHeight; |
| 13 - (CGFloat)buttonMargin; |
| 14 - (CGFloat)computeRequiredHeightAndLayoutSubviews:(BOOL)layout; |
| 15 - (CGFloat)heightThatFitsButtonsUnderOtherWidgets:(CGFloat)heightOfFirstLine |
| 16 layout:(BOOL)layout; |
| 17 - (CGFloat)minimumInfobarHeight; |
| 18 - (NSString*)stripMarkersFromString:(NSString*)string; |
| 19 - (const std::vector<std::pair<NSUInteger, NSRange>>&)linkRanges; |
| 20 @end |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int kShortStringLength = 4; |
| 25 const int kLongStringLength = 1000; |
| 26 |
| 27 class InfoBarViewTest : public PlatformTest { |
| 28 protected: |
| 29 void SetUp() override { |
| 30 PlatformTest::SetUp(); |
| 31 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; |
| 32 infobarView_.reset([[InfoBarView alloc] |
| 33 initWithFrame:CGRectMake(0, 0, screenWidth, 0) |
| 34 delegate:NULL]); |
| 35 [infobarView_ addCloseButtonWithTag:0 target:nil action:nil]; |
| 36 } |
| 37 |
| 38 NSString* RandomString(int numberOfCharacters) { |
| 39 NSMutableString* string = [NSMutableString string]; |
| 40 NSString* letters = @"abcde "; |
| 41 for (int i = 0; i < numberOfCharacters; i++) { |
| 42 [string |
| 43 appendFormat:@"%C", [letters characterAtIndex:arc4random_uniform( |
| 44 [letters length])]]; |
| 45 } |
| 46 return string; |
| 47 } |
| 48 |
| 49 NSString* ShortRandomString() { return RandomString(kShortStringLength); } |
| 50 |
| 51 NSString* LongRandomString() { return RandomString(kLongStringLength); } |
| 52 |
| 53 CGFloat InfobarHeight() { |
| 54 return [infobarView_ computeRequiredHeightAndLayoutSubviews:NO]; |
| 55 } |
| 56 |
| 57 CGFloat MinimumInfobarHeight() { return [infobarView_ minimumInfobarHeight]; } |
| 58 |
| 59 CGFloat ButtonsHeight() { return [infobarView_ buttonsHeight]; } |
| 60 |
| 61 CGFloat ButtonMargin() { return [infobarView_ buttonMargin]; } |
| 62 |
| 63 void TestLinkDetectionHelper( |
| 64 NSString* input, |
| 65 NSString* expectedOutput, |
| 66 const std::vector<std::pair<NSUInteger, NSRange>>& expectedRanges) { |
| 67 NSString* output = [infobarView_ stripMarkersFromString:input]; |
| 68 EXPECT_NSEQ(expectedOutput, output); |
| 69 const std::vector<std::pair<NSUInteger, NSRange>>& ranges = |
| 70 [infobarView_ linkRanges]; |
| 71 EXPECT_EQ(expectedRanges.size(), ranges.size()); |
| 72 for (unsigned int i = 0; i < expectedRanges.size(); ++i) { |
| 73 EXPECT_EQ(expectedRanges[i].first, ranges[i].first); |
| 74 EXPECT_TRUE(NSEqualRanges(expectedRanges[i].second, ranges[i].second)); |
| 75 } |
| 76 } |
| 77 |
| 78 base::scoped_nsobject<InfoBarView> infobarView_; |
| 79 }; |
| 80 |
| 81 TEST_F(InfoBarViewTest, TestLayoutWithNoLabel) { |
| 82 // Do not call -addLabel: to test the case when there is no label. |
| 83 EXPECT_EQ(MinimumInfobarHeight(), InfobarHeight()); |
| 84 } |
| 85 |
| 86 TEST_F(InfoBarViewTest, TestLayoutWithShortLabel) { |
| 87 [infobarView_ addLabel:ShortRandomString()]; |
| 88 EXPECT_EQ(MinimumInfobarHeight(), InfobarHeight()); |
| 89 } |
| 90 |
| 91 TEST_F(InfoBarViewTest, TestLayoutWithLongLabel) { |
| 92 [infobarView_ addLabel:LongRandomString()]; |
| 93 EXPECT_LT(MinimumInfobarHeight(), InfobarHeight()); |
| 94 EXPECT_EQ(0, |
| 95 [infobarView_ heightThatFitsButtonsUnderOtherWidgets:0 layout:NO]); |
| 96 } |
| 97 |
| 98 TEST_F(InfoBarViewTest, TestLayoutWithShortButtons) { |
| 99 [infobarView_ addLabel:ShortRandomString()]; |
| 100 [infobarView_ addButton1:ShortRandomString() |
| 101 tag1:0 |
| 102 button2:ShortRandomString() |
| 103 tag2:0 |
| 104 target:nil |
| 105 action:nil]; |
| 106 EXPECT_EQ(MinimumInfobarHeight(), InfobarHeight()); |
| 107 EXPECT_EQ(ButtonsHeight(), |
| 108 [infobarView_ heightThatFitsButtonsUnderOtherWidgets:0 layout:NO]); |
| 109 } |
| 110 |
| 111 TEST_F(InfoBarViewTest, TestLayoutWithOneLongButtonAndOneShortButton) { |
| 112 [infobarView_ addLabel:ShortRandomString()]; |
| 113 [infobarView_ addButton1:LongRandomString() |
| 114 tag1:0 |
| 115 button2:ShortRandomString() |
| 116 tag2:0 |
| 117 target:nil |
| 118 action:nil]; |
| 119 EXPECT_EQ(MinimumInfobarHeight() + ButtonsHeight() * 2 + ButtonMargin(), |
| 120 InfobarHeight()); |
| 121 EXPECT_EQ(ButtonsHeight() * 2, |
| 122 [infobarView_ heightThatFitsButtonsUnderOtherWidgets:0 layout:NO]); |
| 123 } |
| 124 |
| 125 TEST_F(InfoBarViewTest, TestLayoutWithShortLabelAndShortButton) { |
| 126 [infobarView_ addLabel:ShortRandomString()]; |
| 127 [infobarView_ addButton:ShortRandomString() tag:0 target:nil action:nil]; |
| 128 EXPECT_EQ(MinimumInfobarHeight(), InfobarHeight()); |
| 129 } |
| 130 |
| 131 TEST_F(InfoBarViewTest, TestLayoutWithShortLabelAndLongButton) { |
| 132 [infobarView_ addLabel:ShortRandomString()]; |
| 133 [infobarView_ addButton:LongRandomString() tag:0 target:nil action:nil]; |
| 134 EXPECT_EQ(MinimumInfobarHeight() + ButtonsHeight() + ButtonMargin(), |
| 135 InfobarHeight()); |
| 136 } |
| 137 |
| 138 TEST_F(InfoBarViewTest, TestLayoutWithLongLabelAndLongButtons) { |
| 139 [infobarView_ addLabel:LongRandomString()]; |
| 140 [infobarView_ addButton1:ShortRandomString() |
| 141 tag1:0 |
| 142 button2:LongRandomString() |
| 143 tag2:0 |
| 144 target:nil |
| 145 action:nil]; |
| 146 EXPECT_LT(MinimumInfobarHeight() + ButtonsHeight() * 2, InfobarHeight()); |
| 147 } |
| 148 |
| 149 TEST_F(InfoBarViewTest, TestLinkDetection) { |
| 150 [infobarView_ addLabel:ShortRandomString()]; |
| 151 NSString* linkFoo = [InfoBarView stringAsLink:@"foo" tag:1]; |
| 152 NSString* linkBar = [InfoBarView stringAsLink:@"bar" tag:2]; |
| 153 std::vector<std::pair<NSUInteger, NSRange>> ranges; |
| 154 // No link. |
| 155 TestLinkDetectionHelper(@"", @"", ranges); |
| 156 TestLinkDetectionHelper(@"foo", @"foo", ranges); |
| 157 // One link. |
| 158 ranges.push_back(std::make_pair(1, NSMakeRange(0, 3))); |
| 159 TestLinkDetectionHelper(linkFoo, @"foo", ranges); |
| 160 NSString* link1 = [NSString stringWithFormat:@"baz%@qux", linkFoo]; |
| 161 // Link in the middle. |
| 162 ranges.clear(); |
| 163 ranges.push_back(std::make_pair(1, NSMakeRange(3, 3))); |
| 164 TestLinkDetectionHelper(link1, @"bazfooqux", ranges); |
| 165 // Multiple links. |
| 166 NSString* link2 = [NSString stringWithFormat:@"%@%@", linkFoo, linkBar]; |
| 167 ranges.clear(); |
| 168 ranges.push_back(std::make_pair(1, NSMakeRange(0, 3))); |
| 169 ranges.push_back(std::make_pair(2, NSMakeRange(3, 3))); |
| 170 TestLinkDetectionHelper(link2, @"foobar", ranges); |
| 171 // Multiple links and text. |
| 172 NSString* link3 = |
| 173 [NSString stringWithFormat:@"baz%@qux%@tot", linkFoo, linkBar]; |
| 174 ranges.clear(); |
| 175 ranges.push_back(std::make_pair(1, NSMakeRange(3, 3))); |
| 176 ranges.push_back(std::make_pair(2, NSMakeRange(9, 3))); |
| 177 TestLinkDetectionHelper(link3, @"bazfooquxbartot", ranges); |
| 178 } |
| 179 |
| 180 } // namespace |
OLD | NEW |