Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/common/font_descriptor_mac.h" | |
| 6 | |
| 7 #include <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "testing/platform_test.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 class FontSerializationTest : public PlatformTest {}; | |
| 16 | |
| 17 | |
| 18 // Compare 2 fonts, make sure they point at the same font definition and have | |
| 19 // the same style. Only Bold & Italic style attributes are tested since those | |
| 20 // are the only ones we care about at the moment. | |
| 21 bool CompareFonts(NSFont* font1, NSFont* font2) { | |
| 22 ATSFontRef id1 = CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font1), 0); | |
| 23 ATSFontRef id2 = CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font2), 0); | |
| 24 | |
| 25 if (id1 != id2) { | |
| 26 LOG(ERROR) << "ATSUFontIDs for " | |
|
Avi (use Gerrit)
2010/06/15 13:57:48
Change the error to refer to ATSFontRefs too.
| |
| 27 << [[font1 fontName] UTF8String] | |
| 28 << " and " | |
| 29 << [[font2 fontName] UTF8String] | |
| 30 << " are different"; | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 CGFloat size1 = [font1 pointSize]; | |
| 35 CGFloat size2 = [font2 pointSize]; | |
| 36 if (size1 != size2) { | |
| 37 LOG(ERROR) << "font sizes for " | |
| 38 << [[font1 fontName] UTF8String] << " (" << size1 << ")" | |
| 39 << "and" | |
| 40 << [[font2 fontName] UTF8String] << " (" << size2 << ")" | |
| 41 << " are different"; | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 NSFontTraitMask traits1 = [[NSFontManager sharedFontManager] | |
| 46 traitsOfFont:font1]; | |
| 47 NSFontTraitMask traits2 = [[NSFontManager sharedFontManager] | |
| 48 traitsOfFont:font2]; | |
| 49 | |
| 50 bool is_bold1 = traits1 & NSBoldFontMask; | |
| 51 bool is_bold2 = traits2 & NSBoldFontMask; | |
| 52 bool is_italic1 = traits1 & NSItalicFontMask; | |
| 53 bool is_italic2 = traits2 & NSItalicFontMask; | |
| 54 | |
| 55 if (is_bold1 != is_bold2 || is_italic1 != is_italic2) { | |
| 56 LOG(ERROR) << "Style information for " | |
| 57 << [[font1 fontName] UTF8String] | |
| 58 << " and " | |
| 59 << [[font2 fontName] UTF8String] | |
| 60 << " are different"; | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 // Verify that serialization and deserialization of fonts with various styles | |
| 68 // is performed correctly by FontDescriptor. | |
| 69 TEST_F(FontSerializationTest, StyledFonts) { | |
| 70 NSFont* plain_font = [NSFont systemFontOfSize:12.0]; | |
| 71 ASSERT_TRUE(plain_font != nil); | |
| 72 FontDescriptor desc_plain(plain_font); | |
| 73 EXPECT_TRUE(CompareFonts(plain_font, desc_plain.nsFont())); | |
| 74 | |
| 75 NSFont* bold_font = [NSFont boldSystemFontOfSize:30.0]; | |
| 76 ASSERT_TRUE(bold_font != nil); | |
| 77 FontDescriptor desc_bold(bold_font); | |
| 78 EXPECT_TRUE(CompareFonts(bold_font, desc_bold.nsFont())); | |
| 79 | |
| 80 NSFont* italic_bold_font = | |
| 81 [[NSFontManager sharedFontManager] | |
| 82 fontWithFamily:@"Courier" | |
| 83 traits:(NSBoldFontMask | NSItalicFontMask) | |
| 84 weight:5 | |
| 85 size:18.0]; | |
| 86 ASSERT_TRUE(italic_bold_font != nil); | |
| 87 FontDescriptor desc_italic_bold(italic_bold_font); | |
| 88 EXPECT_TRUE(CompareFonts(italic_bold_font, desc_italic_bold.nsFont())); | |
| 89 } | |
| 90 | |
| 91 } // namsepace | |
| OLD | NEW |