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 #import <Cocoa/Cocoa.h>
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/shared_memory.h"
12 #include "content/common/mac/font_loader.h"
13 #include "content/common/sandbox_mac_unittest_helper.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 using sandboxtest::MacSandboxTest;
19 using sandbox::Sandbox;
21 class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase {
23 FontLoadingTestCase() : font_data_length_(-1) {}
24 virtual bool BeforeSandboxInit();
25 virtual bool SandboxedTest();
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 (!file_util::ReadFileToString(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);
48 if (!font_shmem_.get()) {
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(NULL, &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::mac::ScopedCFTypeRef<CGFontRef> cgfont(cg_font_ref);
86 CTFontRef ct_font_ref =
87 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL);
88 base::mac::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 FilePath temp_file_path;
108 FILE* temp_file = file_util::CreateAndOpenTemporaryFile(&temp_file_path);
109 ASSERT_TRUE(temp_file);
110 file_util::ScopedFILE temp_file_closer(temp_file);
112 base::SharedMemory font_data;
113 uint32 font_data_size;
115 NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0];
116 EXPECT_TRUE(FontLoader::LoadFontIntoBuffer(srcFont,
117 &font_data, &font_data_size, &font_id));
118 EXPECT_GT(font_data_size, 0U);
119 EXPECT_GT(font_id, 0U);
121 file_util::WriteFileDescriptor(fileno(temp_file),
122 static_cast<const char *>(font_data.memory()), font_data_size);
124 ASSERT_TRUE(RunTestInSandbox(content::SANDBOX_TYPE_RENDERER,
125 "FontLoadingTestCase", temp_file_path.value().c_str()));
126 temp_file_closer.reset();
127 ASSERT_TRUE(file_util::Delete(temp_file_path, false));