- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / mac / font_descriptor_unittest.mm
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 "content/common/mac/font_descriptor.h"
6
7 #include <Cocoa/Cocoa.h>
8
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest_mac.h"
13 #include "testing/platform_test.h"
14
15 namespace {
16
17 class FontSerializationTest : public PlatformTest {};
18
19 const std::string kCourierFontName("Courier");
20
21 // Compare 2 fonts, make sure they point at the same font definition and have
22 // the same style.  Only Bold & Italic style attributes are tested since those
23 // are the only ones we care about at the moment.
24 bool CompareFonts(NSFont* font1, NSFont* font2) {
25   ATSFontRef id1 = CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font1), 0);
26   ATSFontRef id2 = CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font2), 0);
27
28   if (id1 != id2) {
29     DLOG(ERROR) << "ATSFontRefs for "
30         << [[font1 fontName] UTF8String]
31         << " and "
32         << [[font2 fontName] UTF8String]
33         << " are different";
34     return false;
35   }
36
37   CGFloat size1 = [font1 pointSize];
38   CGFloat size2 = [font2 pointSize];
39   if (size1 != size2) {
40     DLOG(ERROR) << "font sizes for "
41         << [[font1 fontName] UTF8String] << " (" << size1 << ")"
42         << "and"
43         << [[font2 fontName] UTF8String]  << " (" << size2 << ")"
44         << " are different";
45     return false;
46   }
47
48   NSFontTraitMask traits1 = [[NSFontManager sharedFontManager]
49                                 traitsOfFont:font1];
50   NSFontTraitMask traits2 = [[NSFontManager sharedFontManager]
51                                 traitsOfFont:font2];
52
53   bool is_bold1 = traits1 & NSBoldFontMask;
54   bool is_bold2 = traits2 & NSBoldFontMask;
55   bool is_italic1 = traits1 & NSItalicFontMask;
56   bool is_italic2 = traits2 & NSItalicFontMask;
57
58   if (is_bold1 != is_bold2 || is_italic1 != is_italic2) {
59     DLOG(ERROR) << "Style information for "
60         << [[font1 fontName] UTF8String]
61         << " and "
62         << [[font2 fontName] UTF8String]
63         << " are different";
64         return false;
65   }
66
67   return true;
68 }
69
70 // Create an NSFont via a FontDescriptor object.
71 NSFont* MakeNSFont(const std::string& font_name, float font_point_size) {
72   FontDescriptor desc;
73   desc.font_name = UTF8ToUTF16(font_name);
74   desc.font_point_size = font_point_size;
75   return desc.ToNSFont();
76 }
77
78 // Verify that serialization and deserialization of fonts with various styles
79 // is performed correctly by FontDescriptor.
80 TEST_F(FontSerializationTest, StyledFonts) {
81   NSFont* plain_font = [NSFont systemFontOfSize:12.0];
82   ASSERT_TRUE(plain_font != nil);
83   FontDescriptor desc_plain(plain_font);
84   EXPECT_TRUE(CompareFonts(plain_font, desc_plain.ToNSFont()));
85
86   NSFont* bold_font = [NSFont boldSystemFontOfSize:30.0];
87   ASSERT_TRUE(bold_font != nil);
88   FontDescriptor desc_bold(bold_font);
89   EXPECT_TRUE(CompareFonts(bold_font, desc_bold.ToNSFont()));
90
91   NSFont* italic_bold_font =
92       [[NSFontManager sharedFontManager]
93           fontWithFamily:base::SysUTF8ToNSString(kCourierFontName)
94                   traits:(NSBoldFontMask | NSItalicFontMask)
95                   weight:5
96                     size:18.0];
97   ASSERT_TRUE(italic_bold_font != nil);
98   FontDescriptor desc_italic_bold(italic_bold_font);
99   EXPECT_TRUE(CompareFonts(italic_bold_font, desc_italic_bold.ToNSFont()));
100 }
101
102 // Test that FontDescriptor doesn't crash when used with bad parameters.
103 // This is important since FontDescriptors are constructed with parameters
104 // sent over IPC and bad values must not trigger unexpected behavior.
105 TEST_F(FontSerializationTest, BadParameters) {
106   EXPECT_NSNE(MakeNSFont(kCourierFontName, 12), nil);
107   EXPECT_NSNE(MakeNSFont(kCourierFontName, std::numeric_limits<float>::min()),
108               nil);
109   EXPECT_NSNE(MakeNSFont(kCourierFontName, 0), nil);
110   EXPECT_NSNE(MakeNSFont(kCourierFontName, std::numeric_limits<float>::max()),
111               nil);
112 }
113
114 }  // namespace