Home | History | Annotate | Download | only in mac
      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/mac/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"
     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   base::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   base::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   base::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   base::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   base::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   base::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(
    110       base::ASCIIToUTF16("Hello World"));
    111   encoded.attributes()->push_back(
    112       AttributedStringCoder::FontAttribute(
    113           FontDescriptor([NSFont systemFontOfSize:12]),
    114           gfx::Range(0, 5)));
    115   encoded.attributes()->push_back(
    116       AttributedStringCoder::FontAttribute(
    117           FontDescriptor([NSFont systemFontOfSize:14]),
    118           gfx::Range(5, 100)));
    119   encoded.attributes()->push_back(
    120       AttributedStringCoder::FontAttribute(
    121           FontDescriptor([NSFont systemFontOfSize:16]),
    122           gfx::Range(100, 5)));
    123 
    124   NSAttributedString* decoded = AttributedStringCoder::Decode(&encoded);
    125   EXPECT_TRUE(decoded);
    126 
    127   NSRange range;
    128   NSDictionary* attrs = [decoded attributesAtIndex:0 effectiveRange:&range];
    129   EXPECT_NSEQ([NSFont systemFontOfSize:12],
    130               [attrs objectForKey:NSFontAttributeName]);
    131   EXPECT_TRUE(NSEqualRanges(range, NSMakeRange(0, 5)));
    132 
    133   attrs = [decoded attributesAtIndex:5 effectiveRange:&range];
    134   EXPECT_FALSE([attrs objectForKey:NSFontAttributeName]);
    135   EXPECT_EQ(0U, [attrs count]);
    136 }
    137