OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/scoped_cftyperef.h" |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "base/shared_memory.h" |
| 12 #include "chrome/common/font_loader_mac.h" |
| 13 #include "chrome/common/sandbox_mac_unittest_helper.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 using sandboxtest::MacSandboxTest; |
| 19 |
| 20 class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase { |
| 21 public: |
| 22 FontLoadingTestCase() : font_data_length_(-1) {} |
| 23 virtual bool BeforeSandboxInit(); |
| 24 virtual bool SandboxedTest(); |
| 25 private: |
| 26 scoped_ptr<base::SharedMemory> font_shmem_; |
| 27 size_t font_data_length_; |
| 28 }; |
| 29 REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase); |
| 30 |
| 31 |
| 32 // Load raw font data into shared memory object. |
| 33 bool FontLoadingTestCase::BeforeSandboxInit() { |
| 34 std::string font_data; |
| 35 if (!file_util::ReadFileToString(FilePath(test_data_.c_str()), &font_data)) { |
| 36 LOG(ERROR) << "Failed to read font data from file (" << test_data_ << ")"; |
| 37 return false; |
| 38 } |
| 39 |
| 40 font_data_length_ = font_data.length(); |
| 41 if (font_data_length_ <= 0) { |
| 42 LOG(ERROR) << "No font data: " << font_data_length_; |
| 43 return false; |
| 44 } |
| 45 |
| 46 font_shmem_.reset(new base::SharedMemory); |
| 47 if (!font_shmem_.get()) { |
| 48 LOG(ERROR) << "Failed to create shared memory object."; |
| 49 return false; |
| 50 } |
| 51 |
| 52 if (!font_shmem_->Create(L"", false, false, font_data_length_)) { |
| 53 LOG(ERROR) << "SharedMemory::Create failed"; |
| 54 return false; |
| 55 } |
| 56 |
| 57 if (!font_shmem_->Map(font_data_length_)) { |
| 58 LOG(ERROR) << "SharedMemory::Map failed"; |
| 59 return false; |
| 60 } |
| 61 |
| 62 memcpy(font_shmem_->memory(), font_data.c_str(), font_data_length_); |
| 63 if (!font_shmem_->Unmap()) { |
| 64 LOG(ERROR) << "SharedMemory::Unmap failed"; |
| 65 return false; |
| 66 } |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool FontLoadingTestCase::SandboxedTest() { |
| 71 base::SharedMemoryHandle shmem_handle; |
| 72 if (!font_shmem_->ShareToProcess(NULL, &shmem_handle)) { |
| 73 LOG(ERROR) << "SharedMemory::ShareToProcess failed"; |
| 74 return false; |
| 75 } |
| 76 |
| 77 CGFontRef font_ref; |
| 78 if (!FontLoader::CreateCGFontFromBuffer(shmem_handle, font_data_length_, |
| 79 &font_ref)) { |
| 80 LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; |
| 81 return false; |
| 82 } |
| 83 |
| 84 scoped_cftyperef<CGFontRef> cgfont; |
| 85 |
| 86 if (!font_ref) { |
| 87 LOG(ERROR) << "Got NULL CGFontRef"; |
| 88 return false; |
| 89 } |
| 90 cgfont.reset(font_ref); |
| 91 |
| 92 const NSFont* nsfont = reinterpret_cast<const NSFont*>( |
| 93 CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, |
| 94 NULL, NULL)); |
| 95 if (!nsfont) { |
| 96 LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed"; |
| 97 return false; |
| 98 } |
| 99 |
| 100 // Do something with the font to make sure it's loaded. |
| 101 CGFloat cap_height = [nsfont capHeight]; |
| 102 |
| 103 if (cap_height <= 0.0) { |
| 104 LOG(ERROR) << "Got bad value for [NSFont capHeight] " << cap_height; |
| 105 return false; |
| 106 } |
| 107 |
| 108 return true; |
| 109 } |
| 110 |
| 111 TEST_F(MacSandboxTest, FontLoadingTest) { |
| 112 FilePath temp_file_path; |
| 113 FILE* temp_file = file_util::CreateAndOpenTemporaryFile(&temp_file_path); |
| 114 ASSERT_TRUE(temp_file); |
| 115 file_util::ScopedFILE temp_file_closer(temp_file); |
| 116 |
| 117 base::SharedMemory font_data; |
| 118 uint32 font_data_size; |
| 119 EXPECT_TRUE(FontLoader::LoadFontIntoBuffer(ASCIIToUTF16("Geeza Pro"), 16.0, |
| 120 &font_data, &font_data_size)); |
| 121 EXPECT_GT(font_data_size, 0U); |
| 122 |
| 123 file_util::WriteFileDescriptor(fileno(temp_file), |
| 124 static_cast<const char *>(font_data.memory()), font_data_size); |
| 125 |
| 126 ASSERT_TRUE(RunTestInSandbox(sandbox::SANDBOX_TYPE_RENDERER, |
| 127 "FontLoadingTestCase", temp_file_path.value().c_str())); |
| 128 temp_file_closer.reset(); |
| 129 ASSERT_TRUE(file_util::Delete(temp_file_path, false)); |
| 130 } |
| 131 |
| 132 } // namespace |
OLD | NEW |