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.
5 #include "ios/chrome/common/string_util.h"
7 #import <UIKit/UIKit.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
16 TEST(StringUtilTest, ParseStringWithLink) {
17 NSArray* const all_test_data = @[
19 @"input" : @"Text without link.",
20 @"expected" : @"Text without link.",
21 @"link range" : [NSValue valueWithRange:NSMakeRange(NSNotFound, 0)]
24 @"input" : @"Text with empty link BEGIN_LINK END_LINK.",
25 @"expected" : @"Text with empty link .",
26 @"link range" : [NSValue valueWithRange:NSMakeRange(21, 0)]
29 @"input" : @"Text with BEGIN_LINK and no end link.",
30 @"expected" : @"Text with BEGIN_LINK and no end link.",
31 @"link range" : [NSValue valueWithRange:NSMakeRange(NSNotFound, 0)]
34 @"input" : @"Text with valid BEGIN_LINK link END_LINK and spaces.",
35 @"expected" : @"Text with valid link and spaces.",
36 @"link range" : [NSValue valueWithRange:NSMakeRange(16, 4)]
39 @"input" : @"Text with valid BEGIN_LINKlinkEND_LINK and no spaces.",
40 @"expected" : @"Text with valid link and no spaces.",
41 @"link range" : [NSValue valueWithRange:NSMakeRange(16, 4)]
44 for (NSDictionary* test_data : all_test_data) {
45 NSString* input_text = test_data[@"input"];
46 NSString* expected_text = test_data[@"expected"];
47 NSRange expected_range = [test_data[@"link range"] rangeValue];
49 EXPECT_NSEQ(expected_text, ParseStringWithLink(input_text, nullptr));
51 // Initialize |range| with some values that are not equal to the expected
53 NSRange range = NSMakeRange(1000, 2000);
54 EXPECT_NSEQ(expected_text, ParseStringWithLink(input_text, &range));
55 EXPECT_EQ(expected_range.location, range.location);
56 EXPECT_EQ(expected_range.length, range.length);
60 TEST(StringUtilTest, CleanNSStringForDisplay) {
61 NSArray* const all_test_data = @[
63 @"input" : @"Clean String",
64 @"remove_graphic_chars" : @NO,
65 @"expected" : @"Clean String"
68 @"input" : @" \t String with leading and trailing whitespaces ",
69 @"remove_graphic_chars" : @NO,
70 @"expected" : @"String with leading and trailing whitespaces"
73 @"input" : @" \n\n\r String with \n\n\r\n\r newline characters \n\n\r",
74 @"remove_graphic_chars" : @NO,
75 @"expected" : @"String with newline characters"
78 @"input" : @"String with an arrow ⟰ that remains intact",
79 @"remove_graphic_chars" : @NO,
80 @"expected" : @"String with an arrow ⟰ that remains intact"
83 @"input" : @"String with an arrow ⟰ that gets cleaned up",
84 @"remove_graphic_chars" : @YES,
85 @"expected" : @"String with an arrow that gets cleaned up"
89 for (NSDictionary* test_data : all_test_data) {
90 NSString* input_text = test_data[@"input"];
91 NSString* expected_text = test_data[@"expected"];
92 BOOL remove_graphic_chars = [test_data[@"remove_graphic_chars"] boolValue];
94 EXPECT_NSEQ(expected_text,
95 CleanNSStringForDisplay(input_text, remove_graphic_chars));
99 TEST(StringUnitTest, SubstringOfWidth) {
100 // returns nil for zero-length strings
101 EXPECT_NSEQ(SubstringOfWidth(@"", @{}, 100, NO), nil);
102 EXPECT_NSEQ(SubstringOfWidth(nil, @{}, 100, NO), nil);
104 // This font should always exist
105 UIFont* sys_font = [UIFont systemFontOfSize:18.0f];
106 NSDictionary* attributes = @{NSFontAttributeName : sys_font};
108 EXPECT_NSEQ(SubstringOfWidth(@"asdf", attributes, 100, NO), @"asdf");
110 // construct some string of known lenghts
111 NSString* leading = @"some text";
112 NSString* trailing = @"some other text";
113 NSString* mid = @"some text for the method to do some actual work";
114 NSString* long_string =
115 [[leading stringByAppendingString:mid] stringByAppendingString:trailing];
117 CGFloat leading_width = [leading sizeWithAttributes:attributes].width;
118 CGFloat trailing_width = [trailing sizeWithAttributes:attributes].width;
120 NSString* leading_calculated =
121 SubstringOfWidth(long_string, attributes, leading_width, NO);
122 EXPECT_NSEQ(leading, leading_calculated);
124 NSString* trailing_calculated =
125 SubstringOfWidth(long_string, attributes, trailing_width, YES);
126 EXPECT_NSEQ(trailing, trailing_calculated);