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 #import "chrome/common/attributed_string_coder_mac.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 |
| 13 using mac::AttributedStringCoder; |
| 14 |
| 15 class AttributedStringCoderTest : public testing::Test { |
| 16 public: |
| 17 NSMutableAttributedString* NewAttrString() { |
| 18 NSString* str = @"The quick brown fox jumped over the lazy dog."; |
| 19 return [[NSMutableAttributedString alloc] initWithString:str]; |
| 20 } |
| 21 |
| 22 NSDictionary* FontAttribute(NSString* name, CGFloat size) { |
| 23 NSFont* font = [NSFont fontWithName:name size:size]; |
| 24 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; |
| 25 } |
| 26 |
| 27 NSAttributedString* EncodeAndDecode(NSAttributedString* str) { |
| 28 scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str( |
| 29 AttributedStringCoder::Encode(str)); |
| 30 return AttributedStringCoder::Decode(encoded_str.get()); |
| 31 } |
| 32 }; |
| 33 |
| 34 TEST_F(AttributedStringCoderTest, SimpleString) { |
| 35 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
| 36 [attr_str addAttributes:FontAttribute(@"Helvetica", 12.5) |
| 37 range:NSMakeRange(0, [attr_str length])]; |
| 38 |
| 39 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
| 40 EXPECT_NSEQ(attr_str.get(), decoded); |
| 41 } |
| 42 |
| 43 TEST_F(AttributedStringCoderTest, NoAttributes) { |
| 44 scoped_nsobject<NSAttributedString> attr_str(NewAttrString()); |
| 45 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
| 46 EXPECT_NSEQ(attr_str.get(), decoded); |
| 47 } |
| 48 |
| 49 TEST_F(AttributedStringCoderTest, StripColor) { |
| 50 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
| 51 const NSUInteger kStringLength = [attr_str length]; |
| 52 [attr_str addAttribute:NSFontAttributeName |
| 53 value:[NSFont systemFontOfSize:26] |
| 54 range:NSMakeRange(0, kStringLength)]; |
| 55 [attr_str addAttribute:NSForegroundColorAttributeName |
| 56 value:[NSColor redColor] |
| 57 range:NSMakeRange(0, kStringLength)]; |
| 58 |
| 59 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
| 60 |
| 61 NSRange range; |
| 62 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range]; |
| 63 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, kStringLength), range)); |
| 64 EXPECT_NSEQ([NSFont systemFontOfSize:26], |
| 65 [attrs objectForKey:NSFontAttributeName]); |
| 66 EXPECT_FALSE([attrs objectForKey:NSForegroundColorAttributeName]); |
| 67 } |
| 68 |
| 69 TEST_F(AttributedStringCoderTest, MultipleFonts) { |
| 70 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
| 71 [attr_str setAttributes:FontAttribute(@"Courier", 12) |
| 72 range:NSMakeRange(0, 10)]; |
| 73 [attr_str addAttributes:FontAttribute(@"Helvetica", 16) |
| 74 range:NSMakeRange(12, 6)]; |
| 75 [attr_str addAttributes:FontAttribute(@"Helvetica", 14) |
| 76 range:NSMakeRange(15, 5)]; |
| 77 |
| 78 NSAttributedString* decoded = EncodeAndDecode(attr_str); |
| 79 |
| 80 EXPECT_NSEQ(attr_str.get(), decoded); |
| 81 } |
| 82 |
| 83 TEST_F(AttributedStringCoderTest, NoPertinentAttributes) { |
| 84 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
| 85 [attr_str addAttribute:NSForegroundColorAttributeName |
| 86 value:[NSColor blueColor] |
| 87 range:NSMakeRange(0, 10)]; |
| 88 [attr_str addAttribute:NSBackgroundColorAttributeName |
| 89 value:[NSColor blueColor] |
| 90 range:NSMakeRange(15, 5)]; |
| 91 [attr_str addAttribute:NSKernAttributeName |
| 92 value:[NSNumber numberWithFloat:2.6] |
| 93 range:NSMakeRange(11, 3)]; |
| 94 |
| 95 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
| 96 |
| 97 scoped_nsobject<NSAttributedString> expected(NewAttrString()); |
| 98 EXPECT_NSEQ(expected.get(), decoded); |
| 99 } |
| 100 |
| 101 TEST_F(AttributedStringCoderTest, NilString) { |
| 102 NSAttributedString* decoded = EncodeAndDecode(nil); |
| 103 EXPECT_TRUE(decoded); |
| 104 EXPECT_EQ(0U, [decoded length]); |
| 105 } |
OLD | NEW |