1 // Copyright (c) 2012 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 #import <Cocoa/Cocoa.h>
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_file.h"
9 #include "base/logging.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/shared_memory.h"
13 #include "content/common/mac/font_descriptor.h"
14 #include "content/common/mac/font_loader.h"
15 #include "content/common/sandbox_mac_unittest_helper.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class FontLoadingTestCase : public MacSandboxTestCase {
22 FontLoadingTestCase() : font_data_length_(-1) {}
23 bool BeforeSandboxInit() override;
24 bool SandboxedTest() override;
27 scoped_ptr<base::SharedMemory> font_shmem_;
28 size_t font_data_length_;
30 REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase);
33 // Load raw font data into shared memory object.
34 bool FontLoadingTestCase::BeforeSandboxInit() {
35 std::string font_data;
36 if (!base::ReadFileToString(base::FilePath(test_data_.c_str()), &font_data)) {
37 LOG(ERROR) << "Failed to read font data from file (" << test_data_ << ")";
41 font_data_length_ = font_data.length();
42 if (font_data_length_ <= 0) {
43 LOG(ERROR) << "No font data: " << font_data_length_;
47 font_shmem_.reset(new base::SharedMemory);
49 LOG(ERROR) << "Failed to create shared memory object.";
53 if (!font_shmem_->CreateAndMapAnonymous(font_data_length_)) {
54 LOG(ERROR) << "SharedMemory::Create failed";
58 memcpy(font_shmem_->memory(), font_data.c_str(), font_data_length_);
59 if (!font_shmem_->Unmap()) {
60 LOG(ERROR) << "SharedMemory::Unmap failed";
66 bool FontLoadingTestCase::SandboxedTest() {
67 base::SharedMemoryHandle shmem_handle;
68 if (!font_shmem_->ShareToProcess(base::kNullProcessHandle, &shmem_handle)) {
69 LOG(ERROR) << "SharedMemory::ShareToProcess failed";
73 CGFontRef cg_font_ref;
74 if (!FontLoader::CGFontRefFromBuffer(shmem_handle, font_data_length_,
76 LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed";
81 LOG(ERROR) << "Got NULL CGFontRef";
84 base::ScopedCFTypeRef<CGFontRef> cgfont(cg_font_ref);
86 CTFontRef ct_font_ref =
87 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL);
88 base::ScopedCFTypeRef<CTFontRef> ctfont(ct_font_ref);
91 LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed";
95 // Do something with the font to make sure it's loaded.
96 CGFloat cap_height = CTFontGetCapHeight(ct_font_ref);
98 if (cap_height <= 0.0) {
99 LOG(ERROR) << "Got bad value for CTFontGetCapHeight " << cap_height;
106 TEST_F(MacSandboxTest, FontLoadingTest) {
107 base::FilePath temp_file_path;
108 FILE* temp_file = base::CreateAndOpenTemporaryFile(&temp_file_path);
109 ASSERT_TRUE(temp_file);
110 base::ScopedFILE temp_file_closer(temp_file);
112 NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0];
113 FontDescriptor descriptor(srcFont);
114 FontLoader::Result result;
115 FontLoader::LoadFont(descriptor, &result);
116 EXPECT_GT(result.font_data_size, 0U);
117 EXPECT_GT(result.font_id, 0U);
119 base::WriteFileDescriptor(fileno(temp_file),
120 static_cast<const char *>(result.font_data.memory()),
121 result.font_data_size);
123 ASSERT_TRUE(RunTestInSandbox(SANDBOX_TYPE_RENDERER,
124 "FontLoadingTestCase", temp_file_path.value().c_str()));
125 temp_file_closer.reset();
126 ASSERT_TRUE(base::DeleteFile(temp_file_path, false));
129 } // namespace content