| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <AppKit/AppKit.h> | |
| 6 | |
| 7 #include "base/memory/scoped_nsobject.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #import "chrome/common/attributed_string_coder_mac.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "testing/gtest_mac.h" | |
| 13 | |
| 14 using mac::AttributedStringCoder; | |
| 15 | |
| 16 class AttributedStringCoderTest : public testing::Test { | |
| 17 public: | |
| 18 NSMutableAttributedString* NewAttrString() { | |
| 19 NSString* str = @"The quick brown fox jumped over the lazy dog."; | |
| 20 return [[NSMutableAttributedString alloc] initWithString:str]; | |
| 21 } | |
| 22 | |
| 23 NSDictionary* FontAttribute(NSString* name, CGFloat size) { | |
| 24 NSFont* font = [NSFont fontWithName:name size:size]; | |
| 25 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; | |
| 26 } | |
| 27 | |
| 28 NSAttributedString* EncodeAndDecode(NSAttributedString* str) { | |
| 29 scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str( | |
| 30 AttributedStringCoder::Encode(str)); | |
| 31 return AttributedStringCoder::Decode(encoded_str.get()); | |
| 32 } | |
| 33 }; | |
| 34 | |
| 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])]; | |
| 39 | |
| 40 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | |
| 41 EXPECT_NSEQ(attr_str.get(), decoded); | |
| 42 } | |
| 43 | |
| 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); | |
| 48 } | |
| 49 | |
| 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)]; | |
| 59 | |
| 60 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | |
| 61 | |
| 62 NSRange range; | |
| 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]); | |
| 68 } | |
| 69 | |
| 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)]; | |
| 78 | |
| 79 NSAttributedString* decoded = EncodeAndDecode(attr_str); | |
| 80 | |
| 81 EXPECT_NSEQ(attr_str.get(), decoded); | |
| 82 } | |
| 83 | |
| 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)]; | |
| 95 | |
| 96 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | |
| 97 | |
| 98 scoped_nsobject<NSAttributedString> expected(NewAttrString()); | |
| 99 EXPECT_NSEQ(expected.get(), decoded); | |
| 100 } | |
| 101 | |
| 102 TEST_F(AttributedStringCoderTest, NilString) { | |
| 103 NSAttributedString* decoded = EncodeAndDecode(nil); | |
| 104 EXPECT_TRUE(decoded); | |
| 105 EXPECT_EQ(0U, [decoded length]); | |
| 106 } | |
| 107 | |
| 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]), | |
| 113 ui::Range(0, 5))); | |
| 114 encoded.attributes()->push_back( | |
| 115 AttributedStringCoder::FontAttribute( | |
| 116 FontDescriptor([NSFont systemFontOfSize:14]), | |
| 117 ui::Range(5, 100))); | |
| 118 encoded.attributes()->push_back( | |
| 119 AttributedStringCoder::FontAttribute( | |
| 120 FontDescriptor([NSFont systemFontOfSize:16]), | |
| 121 ui::Range(100, 5))); | |
| 122 | |
| 123 NSAttributedString* decoded = AttributedStringCoder::Decode(&encoded); | |
| 124 EXPECT_TRUE(decoded); | |
| 125 | |
| 126 NSRange range; | |
| 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))); | |
| 131 | |
| 132 attrs = [decoded attributesAtIndex:5 effectiveRange:&range]; | |
| 133 EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]); | |
| 134 EXPECT_EQ(0U, [attrs count]); | |
| 135 } | |
| OLD | NEW |