blob: 12853599a40db88e6e567131d500d2fb52e8bf53 [file] [log] [blame]
[email protected]b54ec1f52012-04-09 02:41:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]865f37922010-05-23 16:43:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <Cocoa/Cocoa.h>
6
7#include "base/file_util.h"
8#include "base/logging.h"
[email protected]df0ca6c82010-10-17 04:09:069#include "base/mac/scoped_cftyperef.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
[email protected]9cc992b2013-07-17 06:30:3611#include "base/memory/shared_memory.h"
[email protected]b54ec1f52012-04-09 02:41:1312#include "content/common/mac/font_descriptor.h"
[email protected]81fc9f02011-09-09 23:05:3413#include "content/common/mac/font_loader.h"
[email protected]415c2cd2011-03-11 21:56:1114#include "content/common/sandbox_mac_unittest_helper.h"
[email protected]865f37922010-05-23 16:43:4415#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]e25b04c92012-10-23 20:05:0617namespace content {
[email protected]865f37922010-05-23 16:43:4418
[email protected]e25b04c92012-10-23 20:05:0619class FontLoadingTestCase : public MacSandboxTestCase {
[email protected]865f37922010-05-23 16:43:4420 public:
21 FontLoadingTestCase() : font_data_length_(-1) {}
[email protected]6b7fa22d2013-02-20 11:16:2122 virtual bool BeforeSandboxInit() OVERRIDE;
23 virtual bool SandboxedTest() OVERRIDE;
[email protected]865f37922010-05-23 16:43:4424 private:
25 scoped_ptr<base::SharedMemory> font_shmem_;
26 size_t font_data_length_;
27};
28REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase);
29
30
31// Load raw font data into shared memory object.
32bool FontLoadingTestCase::BeforeSandboxInit() {
33 std::string font_data;
[email protected]82f84b92013-08-30 18:23:5034 if (!base::ReadFileToString(base::FilePath(test_data_.c_str()), &font_data)) {
[email protected]865f37922010-05-23 16:43:4435 LOG(ERROR) << "Failed to read font data from file (" << test_data_ << ")";
36 return false;
37 }
38
39 font_data_length_ = font_data.length();
40 if (font_data_length_ <= 0) {
41 LOG(ERROR) << "No font data: " << font_data_length_;
42 return false;
43 }
44
45 font_shmem_.reset(new base::SharedMemory);
[email protected]59383c782013-04-17 16:43:2746 if (!font_shmem_) {
[email protected]865f37922010-05-23 16:43:4447 LOG(ERROR) << "Failed to create shared memory object.";
48 return false;
49 }
50
[email protected]54e3dfa22010-10-27 18:16:0651 if (!font_shmem_->CreateAndMapAnonymous(font_data_length_)) {
[email protected]865f37922010-05-23 16:43:4452 LOG(ERROR) << "SharedMemory::Create failed";
53 return false;
54 }
55
[email protected]865f37922010-05-23 16:43:4456 memcpy(font_shmem_->memory(), font_data.c_str(), font_data_length_);
57 if (!font_shmem_->Unmap()) {
58 LOG(ERROR) << "SharedMemory::Unmap failed";
59 return false;
60 }
61 return true;
62}
63
64bool FontLoadingTestCase::SandboxedTest() {
65 base::SharedMemoryHandle shmem_handle;
[email protected]f1bd36992013-03-24 03:55:4666 if (!font_shmem_->ShareToProcess(base::kNullProcessHandle, &shmem_handle)) {
[email protected]865f37922010-05-23 16:43:4467 LOG(ERROR) << "SharedMemory::ShareToProcess failed";
68 return false;
69 }
70
[email protected]cf8d5e852010-11-07 18:10:3071 CGFontRef cg_font_ref;
[email protected]94851d92011-09-07 09:23:3372 if (!FontLoader::CGFontRefFromBuffer(shmem_handle, font_data_length_,
73 &cg_font_ref)) {
74 LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed";
[email protected]20199662010-06-17 03:29:2675 return false;
76 }
[email protected]865f37922010-05-23 16:43:4477
[email protected]cf8d5e852010-11-07 18:10:3078 if (!cg_font_ref) {
[email protected]865f37922010-05-23 16:43:4479 LOG(ERROR) << "Got NULL CGFontRef";
80 return false;
81 }
[email protected]3df79f42013-06-24 18:49:0582 base::ScopedCFTypeRef<CGFontRef> cgfont(cg_font_ref);
[email protected]865f37922010-05-23 16:43:4483
[email protected]cf8d5e852010-11-07 18:10:3084 CTFontRef ct_font_ref =
85 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL);
[email protected]3df79f42013-06-24 18:49:0586 base::ScopedCFTypeRef<CTFontRef> ctfont(ct_font_ref);
[email protected]cf8d5e852010-11-07 18:10:3087
88 if (!ct_font_ref) {
[email protected]865f37922010-05-23 16:43:4489 LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed";
90 return false;
91 }
92
93 // Do something with the font to make sure it's loaded.
[email protected]cf8d5e852010-11-07 18:10:3094 CGFloat cap_height = CTFontGetCapHeight(ct_font_ref);
[email protected]865f37922010-05-23 16:43:4495
96 if (cap_height <= 0.0) {
[email protected]cf8d5e852010-11-07 18:10:3097 LOG(ERROR) << "Got bad value for CTFontGetCapHeight " << cap_height;
[email protected]865f37922010-05-23 16:43:4498 return false;
99 }
100
101 return true;
102}
103
104TEST_F(MacSandboxTest, FontLoadingTest) {
[email protected]a7329162013-02-07 19:21:48105 base::FilePath temp_file_path;
[email protected]03d9afc02013-12-03 17:55:52106 FILE* temp_file = base::CreateAndOpenTemporaryFile(&temp_file_path);
[email protected]865f37922010-05-23 16:43:44107 ASSERT_TRUE(temp_file);
108 file_util::ScopedFILE temp_file_closer(temp_file);
109
[email protected]20199662010-06-17 03:29:26110 NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0];
[email protected]b54ec1f52012-04-09 02:41:13111 FontDescriptor descriptor(srcFont);
112 FontLoader::Result result;
113 FontLoader::LoadFont(descriptor, &result);
114 EXPECT_GT(result.font_data_size, 0U);
115 EXPECT_GT(result.font_id, 0U);
[email protected]865f37922010-05-23 16:43:44116
117 file_util::WriteFileDescriptor(fileno(temp_file),
[email protected]b54ec1f52012-04-09 02:41:13118 static_cast<const char *>(result.font_data.memory()),
119 result.font_data_size);
[email protected]865f37922010-05-23 16:43:44120
[email protected]e25b04c92012-10-23 20:05:06121 ASSERT_TRUE(RunTestInSandbox(SANDBOX_TYPE_RENDERER,
[email protected]865f37922010-05-23 16:43:44122 "FontLoadingTestCase", temp_file_path.value().c_str()));
123 temp_file_closer.reset();
[email protected]dd3aa792013-07-16 19:10:23124 ASSERT_TRUE(base::DeleteFile(temp_file_path, false));
[email protected]865f37922010-05-23 16:43:44125}
126
[email protected]e25b04c92012-10-23 20:05:06127} // namespace content