1 // Copyright (c) 2011 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 <AppKit/AppKit.h>
7 #include "base/memory/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #import "content/common/mac/attributed_string_coder.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gtest_mac.h"
14 using mac::AttributedStringCoder;
16 class AttributedStringCoderTest : public testing::Test {
18 NSMutableAttributedString* NewAttrString() {
19 NSString* str = @"The quick brown fox jumped over the lazy dog.";
20 return [[NSMutableAttributedString alloc] initWithString:str];
23 NSDictionary* FontAttribute(NSString* name, CGFloat size) {
24 NSFont* font = [NSFont fontWithName:name size:size];
25 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
28 NSAttributedString* EncodeAndDecode(NSAttributedString* str) {
29 scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str(
30 AttributedStringCoder::Encode(str));
31 return AttributedStringCoder::Decode(encoded_str.get());
35 TEST_F(AttributedStringCoderTest, SimpleString) {
36 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
37 [attr_str addAttributes:FontAttribute(@"Helvetica", 12.5)
38 range:NSMakeRange(0, [attr_str length])];
40 NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
41 EXPECT_NSEQ(attr_str.get(), decoded);
44 TEST_F(AttributedStringCoderTest, NoAttributes) {
45 scoped_nsobject<NSAttributedString> attr_str(NewAttrString());
46 NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
47 EXPECT_NSEQ(attr_str.get(), decoded);
50 TEST_F(AttributedStringCoderTest, StripColor) {
51 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
52 const NSUInteger kStringLength = [attr_str length];
53 [attr_str addAttribute:NSFontAttributeName
54 value:[NSFont systemFontOfSize:26]
55 range:NSMakeRange(0, kStringLength)];
56 [attr_str addAttribute:NSForegroundColorAttributeName
57 value:[NSColor redColor]
58 range:NSMakeRange(0, kStringLength)];
60 NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
63 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range];
64 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, kStringLength), range));
65 EXPECT_NSEQ([NSFont systemFontOfSize:26],
66 [attrs objectForKey:NSFontAttributeName]);
67 EXPECT_FALSE([attrs objectForKey:NSForegroundColorAttributeName]);
70 TEST_F(AttributedStringCoderTest, MultipleFonts) {
71 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
72 [attr_str setAttributes:FontAttribute(@"Courier", 12)
73 range:NSMakeRange(0, 10)];
74 [attr_str addAttributes:FontAttribute(@"Helvetica", 16)
75 range:NSMakeRange(12, 6)];
76 [attr_str addAttributes:FontAttribute(@"Helvetica", 14)
77 range:NSMakeRange(15, 5)];
79 NSAttributedString* decoded = EncodeAndDecode(attr_str);
81 EXPECT_NSEQ(attr_str.get(), decoded);
84 TEST_F(AttributedStringCoderTest, NoPertinentAttributes) {
85 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
86 [attr_str addAttribute:NSForegroundColorAttributeName
87 value:[NSColor blueColor]
88 range:NSMakeRange(0, 10)];
89 [attr_str addAttribute:NSBackgroundColorAttributeName
90 value:[NSColor blueColor]
91 range:NSMakeRange(15, 5)];
92 [attr_str addAttribute:NSKernAttributeName
93 value:[NSNumber numberWithFloat:2.6]
94 range:NSMakeRange(11, 3)];
96 NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
98 scoped_nsobject<NSAttributedString> expected(NewAttrString());
99 EXPECT_NSEQ(expected.get(), decoded);
102 TEST_F(AttributedStringCoderTest, NilString) {
103 NSAttributedString* decoded = EncodeAndDecode(nil);
104 EXPECT_TRUE(decoded);
105 EXPECT_EQ(0U, [decoded length]);
108 TEST_F(AttributedStringCoderTest, OutOfRange) {
109 AttributedStringCoder::EncodedString encoded(ASCIIToUTF16("Hello World"));
110 encoded.attributes()->push_back(
111 AttributedStringCoder::FontAttribute(
112 FontDescriptor([NSFont systemFontOfSize:12]),
114 encoded.attributes()->push_back(
115 AttributedStringCoder::FontAttribute(
116 FontDescriptor([NSFont systemFontOfSize:14]),
118 encoded.attributes()->push_back(
119 AttributedStringCoder::FontAttribute(
120 FontDescriptor([NSFont systemFontOfSize:16]),
123 NSAttributedString* decoded = AttributedStringCoder::Decode(&encoded);
124 EXPECT_TRUE(decoded);
127 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range];
128 EXPECT_NSEQ([NSFont systemFontOfSize:12],
129 [attrs objectForKey:NSFontAttributeName]);
130 EXPECT_TRUE(NSEqualRanges(range, NSMakeRange(0, 5)));
132 attrs = [decoded attributesAtIndex:5 effectiveRange:&range];
133 EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]);
134 EXPECT_EQ(0U, [attrs count]);