summaryrefslogtreecommitdiffstats
path: root/chrome/common/attributed_string_coder_mac_unittest.mm
blob: bf7ebff5673ebd08f48f303fcd7f011e6daf6b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <AppKit/AppKit.h>

#include "base/memory/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#import "chrome/common/attributed_string_coder_mac.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"

using mac::AttributedStringCoder;

class AttributedStringCoderTest : public testing::Test {
 public:
  NSMutableAttributedString* NewAttrString() {
    NSString* str = @"The quick brown fox jumped over the lazy dog.";
    return [[NSMutableAttributedString alloc] initWithString:str];
  }

  NSDictionary* FontAttribute(NSString* name, CGFloat size) {
    NSFont* font = [NSFont fontWithName:name size:size];
    return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
  }

  NSAttributedString* EncodeAndDecode(NSAttributedString* str) {
    scoped_ptr<const AttributedStringCoder::EncodedString> encoded_str(
        AttributedStringCoder::Encode(str));
    return AttributedStringCoder::Decode(encoded_str.get());
  }
};

TEST_F(AttributedStringCoderTest, SimpleString) {
  scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
  [attr_str addAttributes:FontAttribute(@"Helvetica", 12.5)
                    range:NSMakeRange(0, [attr_str length])];

  NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
  EXPECT_NSEQ(attr_str.get(), decoded);
}

TEST_F(AttributedStringCoderTest, NoAttributes) {
  scoped_nsobject<NSAttributedString> attr_str(NewAttrString());
  NSAttributedString* decoded = EncodeAndDecode(attr_str.get());
  EXPECT_NSEQ(attr_str.get(), decoded);
}

TEST_F(AttributedStringCoderTest, StripColor) {
  scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
  const NSUInteger kStringLength = [attr_str length];
  [attr_str addAttribute:NSFontAttributeName
                   value:[NSFont systemFontOfSize:26]
                   range:NSMakeRange(0, kStringLength)];
  [attr_str addAttribute:NSForegroundColorAttributeName
                   value:[NSColor redColor]
                   range:NSMakeRange(0, kStringLength)];

  NSAttributedString* decoded = EncodeAndDecode(attr_str.get());

  NSRange range;
  NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range];
  EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, kStringLength), range));
  EXPECT_NSEQ([NSFont systemFontOfSize:26],
              [attrs objectForKey:NSFontAttributeName]);
  EXPECT_FALSE([attrs objectForKey:NSForegroundColorAttributeName]);
}

TEST_F(AttributedStringCoderTest, MultipleFonts) {
  scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
  [attr_str setAttributes:FontAttribute(@"Courier", 12)
                    range:NSMakeRange(0, 10)];
  [attr_str addAttributes:FontAttribute(@"Helvetica", 16)
                    range:NSMakeRange(12, 6)];
  [attr_str addAttributes:FontAttribute(@"Helvetica", 14)
                    range:NSMakeRange(15, 5)];

  NSAttributedString* decoded = EncodeAndDecode(attr_str);

  EXPECT_NSEQ(attr_str.get(), decoded);
}

TEST_F(AttributedStringCoderTest, NoPertinentAttributes) {
  scoped_nsobject<NSMutableAttributedString> attr_str(NewAttrString());
  [attr_str addAttribute:NSForegroundColorAttributeName
                   value:[NSColor blueColor]
                   range:NSMakeRange(0, 10)];
  [attr_str addAttribute:NSBackgroundColorAttributeName
                   value:[NSColor blueColor]
                   range:NSMakeRange(15, 5)];
  [attr_str addAttribute:NSKernAttributeName
                   value:[NSNumber numberWithFloat:2.6]
                   range:NSMakeRange(11, 3)];

  NSAttributedString* decoded = EncodeAndDecode(attr_str.get());

  scoped_nsobject<NSAttributedString> expected(NewAttrString());
  EXPECT_NSEQ(expected.get(), decoded);
}

TEST_F(AttributedStringCoderTest, NilString) {
  NSAttributedString* decoded = EncodeAndDecode(nil);
  EXPECT_TRUE(decoded);
  EXPECT_EQ(0U, [decoded length]);
}

TEST_F(AttributedStringCoderTest, OutOfRange) {
  AttributedStringCoder::EncodedString encoded(ASCIIToUTF16("Hello World"));
  encoded.attributes()->push_back(
      AttributedStringCoder::FontAttribute(
          FontDescriptor([NSFont systemFontOfSize:12]),
          ui::Range(0, 5)));
  encoded.attributes()->push_back(
      AttributedStringCoder::FontAttribute(
          FontDescriptor([NSFont systemFontOfSize:14]),
          ui::Range(5, 100)));
  encoded.attributes()->push_back(
      AttributedStringCoder::FontAttribute(
          FontDescriptor([NSFont systemFontOfSize:16]),
          ui::Range(100, 5)));

  NSAttributedString* decoded = AttributedStringCoder::Decode(&encoded);
  EXPECT_TRUE(decoded);

  NSRange range;
  NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range];
  EXPECT_NSEQ([NSFont systemFontOfSize:12],
              [attrs objectForKey:NSFontAttributeName]);
  EXPECT_TRUE(NSEqualRanges(range, NSMakeRange(0, 5)));

  attrs = [decoded attributesAtIndex:5 effectiveRange:&range];
  EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]);
  EXPECT_EQ(0U, [attrs count]);
}