OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <AppKit/AppKit.h> | 5 #include <AppKit/AppKit.h> |
6 | 6 |
7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #import "content/common/mac/attributed_string_coder.h" | 10 #import "content/common/mac/attributed_string_coder.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
13 | 13 |
14 using mac::AttributedStringCoder; | 14 using mac::AttributedStringCoder; |
15 | 15 |
16 class AttributedStringCoderTest : public testing::Test { | 16 class AttributedStringCoderTest : public testing::Test { |
17 public: | 17 public: |
18 NSMutableAttributedString* NewAttrString() { | 18 NSMutableAttributedString* NewAttrString() { |
19 NSString* str = @"The quick brown fox jumped over the lazy dog."; | 19 NSString* str = @"The quick brown fox jumped over the lazy dog."; |
20 return [[NSMutableAttributedString alloc] initWithString:str]; | 20 return [[NSMutableAttributedString alloc] initWithString:str]; |
21 } | 21 } |
22 | 22 |
23 NSDictionary* FontAttribute(NSString* name, CGFloat size) { | 23 NSDictionary* FontAttribute(NSString* name, CGFloat size) { |
24 NSFont* font = [NSFont fontWithName:name size:size]; | 24 NSFont* font = [NSFont fontWithName:name size:size]; |
25 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; | 25 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; |
26 } | 26 } |
27 | 27 |
28 NSAttributedString* EncodeAndDecode(NSAttributedString* str) { | 28 NSAttributedString* EncodeAndDecode(NSAttributedString* str) { |
29 scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str( | 29 scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str( |
30 AttributedStringCoder::Encode(str)); | 30 AttributedStringCoder::Encode(str)); |
31 return AttributedStringCoder::Decode(encoded_str.get()); | 31 return AttributedStringCoder::Decode(encoded_str.get()); |
32 } | 32 } |
33 }; | 33 }; |
34 | 34 |
35 TEST_F(AttributedStringCoderTest, SimpleString) { | 35 TEST_F(AttributedStringCoderTest, SimpleString) { |
36 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); | 36 base::scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
37 [attr_str addAttributes:FontAttribute(@"Helvetica", 12.5) | 37 [attr_str addAttributes:FontAttribute(@"Helvetica", 12.5) |
38 range:NSMakeRange(0, [attr_str length])]; | 38 range:NSMakeRange(0, [attr_str length])]; |
39 | 39 |
40 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | 40 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
41 EXPECT_NSEQ(attr_str.get(), decoded); | 41 EXPECT_NSEQ(attr_str.get(), decoded); |
42 } | 42 } |
43 | 43 |
44 TEST_F(AttributedStringCoderTest, NoAttributes) { | 44 TEST_F(AttributedStringCoderTest, NoAttributes) { |
45 scoped_nsobject<NSAttributedString> attr_str(NewAttrString()); | 45 base::scoped_nsobject<NSAttributedString> attr_str(NewAttrString()); |
46 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | 46 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
47 EXPECT_NSEQ(attr_str.get(), decoded); | 47 EXPECT_NSEQ(attr_str.get(), decoded); |
48 } | 48 } |
49 | 49 |
50 TEST_F(AttributedStringCoderTest, StripColor) { | 50 TEST_F(AttributedStringCoderTest, StripColor) { |
51 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); | 51 base::scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
52 const NSUInteger kStringLength = [attr_str length]; | 52 const NSUInteger kStringLength = [attr_str length]; |
53 [attr_str addAttribute:NSFontAttributeName | 53 [attr_str addAttribute:NSFontAttributeName |
54 value:[NSFont systemFontOfSize:26] | 54 value:[NSFont systemFontOfSize:26] |
55 range:NSMakeRange(0, kStringLength)]; | 55 range:NSMakeRange(0, kStringLength)]; |
56 [attr_str addAttribute:NSForegroundColorAttributeName | 56 [attr_str addAttribute:NSForegroundColorAttributeName |
57 value:[NSColor redColor] | 57 value:[NSColor redColor] |
58 range:NSMakeRange(0, kStringLength)]; | 58 range:NSMakeRange(0, kStringLength)]; |
59 | 59 |
60 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | 60 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
61 | 61 |
62 NSRange range; | 62 NSRange range; |
63 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range]; | 63 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range]; |
64 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, kStringLength), range)); | 64 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, kStringLength), range)); |
65 EXPECT_NSEQ([NSFont systemFontOfSize:26], | 65 EXPECT_NSEQ([NSFont systemFontOfSize:26], |
66 [attrs objectForKey:NSFontAttributeName]); | 66 [attrs objectForKey:NSFontAttributeName]); |
67 EXPECT_FALSE([attrs objectForKey:NSForegroundColorAttributeName]); | 67 EXPECT_FALSE([attrs objectForKey:NSForegroundColorAttributeName]); |
68 } | 68 } |
69 | 69 |
70 TEST_F(AttributedStringCoderTest, MultipleFonts) { | 70 TEST_F(AttributedStringCoderTest, MultipleFonts) { |
71 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); | 71 base::scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
72 [attr_str setAttributes:FontAttribute(@"Courier", 12) | 72 [attr_str setAttributes:FontAttribute(@"Courier", 12) |
73 range:NSMakeRange(0, 10)]; | 73 range:NSMakeRange(0, 10)]; |
74 [attr_str addAttributes:FontAttribute(@"Helvetica", 16) | 74 [attr_str addAttributes:FontAttribute(@"Helvetica", 16) |
75 range:NSMakeRange(12, 6)]; | 75 range:NSMakeRange(12, 6)]; |
76 [attr_str addAttributes:FontAttribute(@"Helvetica", 14) | 76 [attr_str addAttributes:FontAttribute(@"Helvetica", 14) |
77 range:NSMakeRange(15, 5)]; | 77 range:NSMakeRange(15, 5)]; |
78 | 78 |
79 NSAttributedString* decoded = EncodeAndDecode(attr_str); | 79 NSAttributedString* decoded = EncodeAndDecode(attr_str); |
80 | 80 |
81 EXPECT_NSEQ(attr_str.get(), decoded); | 81 EXPECT_NSEQ(attr_str.get(), decoded); |
82 } | 82 } |
83 | 83 |
84 TEST_F(AttributedStringCoderTest, NoPertinentAttributes) { | 84 TEST_F(AttributedStringCoderTest, NoPertinentAttributes) { |
85 scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); | 85 base::scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString()); |
86 [attr_str addAttribute:NSForegroundColorAttributeName | 86 [attr_str addAttribute:NSForegroundColorAttributeName |
87 value:[NSColor blueColor] | 87 value:[NSColor blueColor] |
88 range:NSMakeRange(0, 10)]; | 88 range:NSMakeRange(0, 10)]; |
89 [attr_str addAttribute:NSBackgroundColorAttributeName | 89 [attr_str addAttribute:NSBackgroundColorAttributeName |
90 value:[NSColor blueColor] | 90 value:[NSColor blueColor] |
91 range:NSMakeRange(15, 5)]; | 91 range:NSMakeRange(15, 5)]; |
92 [attr_str addAttribute:NSKernAttributeName | 92 [attr_str addAttribute:NSKernAttributeName |
93 value:[NSNumber numberWithFloat:2.6] | 93 value:[NSNumber numberWithFloat:2.6] |
94 range:NSMakeRange(11, 3)]; | 94 range:NSMakeRange(11, 3)]; |
95 | 95 |
96 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); | 96 NSAttributedString* decoded = EncodeAndDecode(attr_str.get()); |
97 | 97 |
98 scoped_nsobject<NSAttributedString> expected(NewAttrString()); | 98 base::scoped_nsobject<NSAttributedString> expected(NewAttrString()); |
99 EXPECT_NSEQ(expected.get(), decoded); | 99 EXPECT_NSEQ(expected.get(), decoded); |
100 } | 100 } |
101 | 101 |
102 TEST_F(AttributedStringCoderTest, NilString) { | 102 TEST_F(AttributedStringCoderTest, NilString) { |
103 NSAttributedString* decoded = EncodeAndDecode(nil); | 103 NSAttributedString* decoded = EncodeAndDecode(nil); |
104 EXPECT_TRUE(decoded); | 104 EXPECT_TRUE(decoded); |
105 EXPECT_EQ(0U, [decoded length]); | 105 EXPECT_EQ(0U, [decoded length]); |
106 } | 106 } |
107 | 107 |
108 TEST_F(AttributedStringCoderTest, OutOfRange) { | 108 TEST_F(AttributedStringCoderTest, OutOfRange) { |
(...skipping 17 matching lines...) Expand all Loading... |
126 NSRange range; | 126 NSRange range; |
127 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range]; | 127 NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range]; |
128 EXPECT_NSEQ([NSFont systemFontOfSize:12], | 128 EXPECT_NSEQ([NSFont systemFontOfSize:12], |
129 [attrs objectForKey:NSFontAttributeName]); | 129 [attrs objectForKey:NSFontAttributeName]); |
130 EXPECT_TRUE(NSEqualRanges(range, NSMakeRange(0, 5))); | 130 EXPECT_TRUE(NSEqualRanges(range, NSMakeRange(0, 5))); |
131 | 131 |
132 attrs = [decoded attributesAtIndex:5 effectiveRange:&range]; | 132 attrs = [decoded attributesAtIndex:5 effectiveRange:&range]; |
133 EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]); | 133 EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]); |
134 EXPECT_EQ(0U, [attrs count]); | 134 EXPECT_EQ(0U, [attrs count]); |
135 } | 135 } |
OLD | NEW |