[email protected] | b54ec1f5 | 2012-04-09 02:41:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 2 | // 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] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 9 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 9cc992b | 2013-07-17 06:30:36 | [diff] [blame] | 11 | #include "base/memory/shared_memory.h" |
[email protected] | b54ec1f5 | 2012-04-09 02:41:13 | [diff] [blame] | 12 | #include "content/common/mac/font_descriptor.h" |
[email protected] | 81fc9f0 | 2011-09-09 23:05:34 | [diff] [blame] | 13 | #include "content/common/mac/font_loader.h" |
[email protected] | 415c2cd | 2011-03-11 21:56:11 | [diff] [blame] | 14 | #include "content/common/sandbox_mac_unittest_helper.h" |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 17 | namespace content { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 18 | |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 19 | class FontLoadingTestCase : public MacSandboxTestCase { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 20 | public: |
| 21 | FontLoadingTestCase() : font_data_length_(-1) {} |
[email protected] | 6b7fa22d | 2013-02-20 11:16:21 | [diff] [blame] | 22 | virtual bool BeforeSandboxInit() OVERRIDE; |
| 23 | virtual bool SandboxedTest() OVERRIDE; |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 24 | private: |
| 25 | scoped_ptr<base::SharedMemory> font_shmem_; |
| 26 | size_t font_data_length_; |
| 27 | }; |
| 28 | REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase); |
| 29 | |
| 30 | |
| 31 | // Load raw font data into shared memory object. |
| 32 | bool FontLoadingTestCase::BeforeSandboxInit() { |
| 33 | std::string font_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 34 | if (!base::ReadFileToString(base::FilePath(test_data_.c_str()), &font_data)) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 35 | 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] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 46 | if (!font_shmem_) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 47 | LOG(ERROR) << "Failed to create shared memory object."; |
| 48 | return false; |
| 49 | } |
| 50 | |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 51 | if (!font_shmem_->CreateAndMapAnonymous(font_data_length_)) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 52 | LOG(ERROR) << "SharedMemory::Create failed"; |
| 53 | return false; |
| 54 | } |
| 55 | |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 56 | 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 | |
| 64 | bool FontLoadingTestCase::SandboxedTest() { |
| 65 | base::SharedMemoryHandle shmem_handle; |
[email protected] | f1bd3699 | 2013-03-24 03:55:46 | [diff] [blame] | 66 | if (!font_shmem_->ShareToProcess(base::kNullProcessHandle, &shmem_handle)) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 67 | LOG(ERROR) << "SharedMemory::ShareToProcess failed"; |
| 68 | return false; |
| 69 | } |
| 70 | |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 71 | CGFontRef cg_font_ref; |
[email protected] | 94851d9 | 2011-09-07 09:23:33 | [diff] [blame] | 72 | if (!FontLoader::CGFontRefFromBuffer(shmem_handle, font_data_length_, |
| 73 | &cg_font_ref)) { |
| 74 | LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 75 | return false; |
| 76 | } |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 77 | |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 78 | if (!cg_font_ref) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 79 | LOG(ERROR) << "Got NULL CGFontRef"; |
| 80 | return false; |
| 81 | } |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 82 | base::ScopedCFTypeRef<CGFontRef> cgfont(cg_font_ref); |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 83 | |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 84 | CTFontRef ct_font_ref = |
| 85 | CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL); |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 86 | base::ScopedCFTypeRef<CTFontRef> ctfont(ct_font_ref); |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 87 | |
| 88 | if (!ct_font_ref) { |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 89 | LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed"; |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // Do something with the font to make sure it's loaded. |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 94 | CGFloat cap_height = CTFontGetCapHeight(ct_font_ref); |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 95 | |
| 96 | if (cap_height <= 0.0) { |
[email protected] | cf8d5e85 | 2010-11-07 18:10:30 | [diff] [blame] | 97 | LOG(ERROR) << "Got bad value for CTFontGetCapHeight " << cap_height; |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | TEST_F(MacSandboxTest, FontLoadingTest) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 105 | base::FilePath temp_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 106 | FILE* temp_file = base::CreateAndOpenTemporaryFile(&temp_file_path); |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 107 | ASSERT_TRUE(temp_file); |
| 108 | file_util::ScopedFILE temp_file_closer(temp_file); |
| 109 | |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 110 | NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0]; |
[email protected] | b54ec1f5 | 2012-04-09 02:41:13 | [diff] [blame] | 111 | 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] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 116 | |
| 117 | file_util::WriteFileDescriptor(fileno(temp_file), |
[email protected] | b54ec1f5 | 2012-04-09 02:41:13 | [diff] [blame] | 118 | static_cast<const char *>(result.font_data.memory()), |
| 119 | result.font_data_size); |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 120 | |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 121 | ASSERT_TRUE(RunTestInSandbox(SANDBOX_TYPE_RENDERER, |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 122 | "FontLoadingTestCase", temp_file_path.value().c_str())); |
| 123 | temp_file_closer.reset(); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 124 | ASSERT_TRUE(base::DeleteFile(temp_file_path, false)); |
[email protected] | 865f3792 | 2010-05-23 16:43:44 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 127 | } // namespace content |