OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/scoped_cftyperef.h" | 9 #include "base/scoped_cftyperef.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
11 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
12 #include "chrome/common/font_loader_mac.h" | 12 #include "chrome/common/font_loader_mac.h" |
13 #include "chrome/common/sandbox_mac_unittest_helper.h" | 13 #include "chrome/common/sandbox_mac_unittest_helper.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 using sandboxtest::MacSandboxTest; | 18 using sandboxtest::MacSandboxTest; |
19 | 19 |
| 20 bool CGFontFromFontContainer(ATSFontContainerRef container, CGFontRef* out) { |
| 21 // Count the number of fonts that were loaded. |
| 22 ItemCount fontCount = 0; |
| 23 OSStatus err = ATSFontFindFromContainer(container, kATSOptionFlagsDefault, 0, |
| 24 NULL, &fontCount); |
| 25 |
| 26 if (err != noErr || fontCount < 1) { |
| 27 return false; |
| 28 } |
| 29 |
| 30 // Load font from container. |
| 31 ATSFontRef font_ref_ats = 0; |
| 32 ATSFontFindFromContainer(container, kATSOptionFlagsDefault, 1, |
| 33 &font_ref_ats, NULL); |
| 34 |
| 35 if (!font_ref_ats) { |
| 36 return false; |
| 37 } |
| 38 |
| 39 // Convert to cgFont. |
| 40 CGFontRef font_ref_cg = CGFontCreateWithPlatformFont(&font_ref_ats); |
| 41 |
| 42 if (!font_ref_cg) { |
| 43 return false; |
| 44 } |
| 45 |
| 46 *out = font_ref_cg; |
| 47 return true; |
| 48 } |
| 49 |
| 50 class ScopedFontContainer { |
| 51 public: |
| 52 explicit ScopedFontContainer(ATSFontContainerRef ref) |
| 53 : container_ref(ref) {} |
| 54 |
| 55 ~ScopedFontContainer() { |
| 56 ATSFontDeactivate(container_ref, NULL, kATSOptionFlagsDefault); |
| 57 } |
| 58 |
| 59 ATSFontContainerRef container_ref; |
| 60 }; |
| 61 |
20 class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase { | 62 class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase { |
21 public: | 63 public: |
22 FontLoadingTestCase() : font_data_length_(-1) {} | 64 FontLoadingTestCase() : font_data_length_(-1) {} |
23 virtual bool BeforeSandboxInit(); | 65 virtual bool BeforeSandboxInit(); |
24 virtual bool SandboxedTest(); | 66 virtual bool SandboxedTest(); |
25 private: | 67 private: |
26 scoped_ptr<base::SharedMemory> font_shmem_; | 68 scoped_ptr<base::SharedMemory> font_shmem_; |
27 size_t font_data_length_; | 69 size_t font_data_length_; |
28 }; | 70 }; |
29 REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase); | 71 REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 return true; | 109 return true; |
68 } | 110 } |
69 | 111 |
70 bool FontLoadingTestCase::SandboxedTest() { | 112 bool FontLoadingTestCase::SandboxedTest() { |
71 base::SharedMemoryHandle shmem_handle; | 113 base::SharedMemoryHandle shmem_handle; |
72 if (!font_shmem_->ShareToProcess(NULL, &shmem_handle)) { | 114 if (!font_shmem_->ShareToProcess(NULL, &shmem_handle)) { |
73 LOG(ERROR) << "SharedMemory::ShareToProcess failed"; | 115 LOG(ERROR) << "SharedMemory::ShareToProcess failed"; |
74 return false; | 116 return false; |
75 } | 117 } |
76 | 118 |
77 CGFontRef font_ref; | 119 ATSFontContainerRef font_container; |
78 if (!FontLoader::CreateCGFontFromBuffer(shmem_handle, font_data_length_, | 120 if (!FontLoader::ATSFontContainerFromBuffer(shmem_handle, font_data_length_, |
79 &font_ref)) { | 121 &font_container)) { |
80 LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; | 122 LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; |
81 return false; | 123 return false; |
82 } | 124 } |
83 | 125 |
84 scoped_cftyperef<CGFontRef> cgfont; | 126 // Unload the font container when done. |
| 127 ScopedFontContainer scoped_unloader(font_container); |
| 128 |
| 129 CGFontRef font_ref; |
| 130 if (!CGFontFromFontContainer(font_container, &font_ref)) { |
| 131 LOG(ERROR) << "CGFontFromFontContainer failed"; |
| 132 return false; |
| 133 } |
85 | 134 |
86 if (!font_ref) { | 135 if (!font_ref) { |
87 LOG(ERROR) << "Got NULL CGFontRef"; | 136 LOG(ERROR) << "Got NULL CGFontRef"; |
88 return false; | 137 return false; |
89 } | 138 } |
| 139 scoped_cftyperef<CGFontRef> cgfont; |
90 cgfont.reset(font_ref); | 140 cgfont.reset(font_ref); |
91 | 141 |
92 const NSFont* nsfont = reinterpret_cast<const NSFont*>( | 142 const NSFont* nsfont = reinterpret_cast<const NSFont*>( |
93 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, | 143 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, |
94 NULL, NULL)); | 144 NULL, NULL)); |
95 if (!nsfont) { | 145 if (!nsfont) { |
96 LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed"; | 146 LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed"; |
97 return false; | 147 return false; |
98 } | 148 } |
99 | 149 |
100 // Do something with the font to make sure it's loaded. | 150 // Do something with the font to make sure it's loaded. |
101 CGFloat cap_height = [nsfont capHeight]; | 151 CGFloat cap_height = [nsfont capHeight]; |
102 | 152 |
103 if (cap_height <= 0.0) { | 153 if (cap_height <= 0.0) { |
104 LOG(ERROR) << "Got bad value for [NSFont capHeight] " << cap_height; | 154 LOG(ERROR) << "Got bad value for [NSFont capHeight] " << cap_height; |
105 return false; | 155 return false; |
106 } | 156 } |
107 | 157 |
108 return true; | 158 return true; |
109 } | 159 } |
110 | 160 |
111 TEST_F(MacSandboxTest, FontLoadingTest) { | 161 TEST_F(MacSandboxTest, FontLoadingTest) { |
112 FilePath temp_file_path; | 162 FilePath temp_file_path; |
113 FILE* temp_file = file_util::CreateAndOpenTemporaryFile(&temp_file_path); | 163 FILE* temp_file = file_util::CreateAndOpenTemporaryFile(&temp_file_path); |
114 ASSERT_TRUE(temp_file); | 164 ASSERT_TRUE(temp_file); |
115 file_util::ScopedFILE temp_file_closer(temp_file); | 165 file_util::ScopedFILE temp_file_closer(temp_file); |
116 | 166 |
117 base::SharedMemory font_data; | 167 base::SharedMemory font_data; |
118 uint32 font_data_size; | 168 uint32 font_data_size; |
119 EXPECT_TRUE(FontLoader::LoadFontIntoBuffer(ASCIIToUTF16("Geeza Pro"), 16.0, | 169 NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0]; |
| 170 EXPECT_TRUE(FontLoader::LoadFontIntoBuffer(srcFont, |
120 &font_data, &font_data_size)); | 171 &font_data, &font_data_size)); |
121 EXPECT_GT(font_data_size, 0U); | 172 EXPECT_GT(font_data_size, 0U); |
122 | 173 |
123 file_util::WriteFileDescriptor(fileno(temp_file), | 174 file_util::WriteFileDescriptor(fileno(temp_file), |
124 static_cast<const char *>(font_data.memory()), font_data_size); | 175 static_cast<const char *>(font_data.memory()), font_data_size); |
125 | 176 |
126 ASSERT_TRUE(RunTestInSandbox(sandbox::SANDBOX_TYPE_RENDERER, | 177 ASSERT_TRUE(RunTestInSandbox(sandbox::SANDBOX_TYPE_RENDERER, |
127 "FontLoadingTestCase", temp_file_path.value().c_str())); | 178 "FontLoadingTestCase", temp_file_path.value().c_str())); |
128 temp_file_closer.reset(); | 179 temp_file_closer.reset(); |
129 ASSERT_TRUE(file_util::Delete(temp_file_path, false)); | 180 ASSERT_TRUE(file_util::Delete(temp_file_path, false)); |
130 } | 181 } |
131 | 182 |
132 } // namespace | 183 } // namespace |
OLD | NEW |