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.
5 #include "content/common/mac/font_descriptor.h"
7 #include <Cocoa/Cocoa.h>
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"
17 class FontSerializationTest : public PlatformTest {};
19 const std::string kCourierFontName("Courier");
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);
29 DLOG(ERROR) << "ATSFontRefs for "
30 << [[font1 fontName] UTF8String]
32 << [[font2 fontName] UTF8String]
37 CGFloat size1 = [font1 pointSize];
38 CGFloat size2 = [font2 pointSize];
40 DLOG(ERROR) << "font sizes for "
41 << [[font1 fontName] UTF8String] << " (" << size1 << ")"
43 << [[font2 fontName] UTF8String] << " (" << size2 << ")"
48 NSFontTraitMask traits1 = [[NSFontManager sharedFontManager]
50 NSFontTraitMask traits2 = [[NSFontManager sharedFontManager]
53 bool is_bold1 = traits1 & NSBoldFontMask;
54 bool is_bold2 = traits2 & NSBoldFontMask;
55 bool is_italic1 = traits1 & NSItalicFontMask;
56 bool is_italic2 = traits2 & NSItalicFontMask;
58 if (is_bold1 != is_bold2 || is_italic1 != is_italic2) {
59 DLOG(ERROR) << "Style information for "
60 << [[font1 fontName] UTF8String]
62 << [[font2 fontName] UTF8String]
70 // Create an NSFont via a FontDescriptor object.
71 NSFont* MakeNSFont(const std::string& font_name, float font_point_size) {
73 desc.font_name = base::UTF8ToUTF16(font_name);
74 desc.font_point_size = font_point_size;
75 return desc.ToNSFont();
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()));
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()));
91 NSFont* italic_bold_font =
92 [[NSFontManager sharedFontManager]
93 fontWithFamily:base::SysUTF8ToNSString(kCourierFontName)
94 traits:(NSBoldFontMask | NSItalicFontMask)
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()));
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()),
109 EXPECT_NSNE(MakeNSFont(kCourierFontName, 0), nil);
110 EXPECT_NSNE(MakeNSFont(kCourierFontName, std::numeric_limits<float>::max()),