license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/gfx/uniscribe.h" |
| 6 | #include "base/win_util.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
| 9 | // This must be in the gfx namespace for the friend statements in uniscribe.h |
| 10 | // to work. |
| 11 | namespace gfx { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | class UniscribeTest : public testing::Test { |
| 16 | public: |
| 17 | UniscribeTest() { |
| 18 | } |
| 19 | |
| 20 | // Returns an HFONT with the given name. The caller does not have to free |
| 21 | // this, it will be automatically freed at the end of the test. Returns NULL |
| 22 | // on failure. On success, the |
| 23 | HFONT MakeFont(const wchar_t* font_name, SCRIPT_CACHE** cache) { |
| 24 | LOGFONT lf; |
| 25 | memset(&lf, 0, sizeof(LOGFONT)); |
| 26 | lf.lfHeight = 20; |
| 27 | wcscpy_s(lf.lfFaceName, font_name); |
| 28 | |
| 29 | HFONT hfont = CreateFontIndirect(&lf); |
| 30 | if (!hfont) |
| 31 | return NULL; |
| 32 | |
| 33 | *cache = new SCRIPT_CACHE; |
| 34 | **cache = NULL; |
| 35 | created_fonts_.push_back(std::make_pair(hfont, *cache)); |
| 36 | return hfont; |
| 37 | } |
| 38 | |
| 39 | protected: |
| 40 | // Default font properties structure for tests to use. |
| 41 | SCRIPT_FONTPROPERTIES properties_; |
| 42 | |
| 43 | private: |
| 44 | virtual void SetUp() { |
| 45 | memset(&properties_, 0, sizeof(SCRIPT_FONTPROPERTIES)); |
| 46 | properties_.cBytes = sizeof(SCRIPT_FONTPROPERTIES); |
| 47 | properties_.wgBlank = ' '; |
| 48 | properties_.wgDefault = '?'; // Used when the character is not in the font. |
| 49 | properties_.wgInvalid = '#'; // Used for invalid characters. |
| 50 | } |
| 51 | |
| 52 | virtual void TearDown() { |
| 53 | // Free any allocated fonts. |
| 54 | for (size_t i = 0; i < created_fonts_.size(); i++) { |
| 55 | DeleteObject(created_fonts_[i].first); |
| 56 | ScriptFreeCache(created_fonts_[i].second); |
| 57 | delete created_fonts_[i].second; |
| 58 | } |
| 59 | created_fonts_.clear(); |
| 60 | } |
| 61 | |
| 62 | // Tracks allocated fonts so we can delete them at the end of the test. |
| 63 | // The script cache pointer is heap allocated and must be freed. |
| 64 | std::vector< std::pair<HFONT, SCRIPT_CACHE*> > created_fonts_; |
| 65 | |
| 66 | DISALLOW_EVIL_CONSTRUCTORS(UniscribeTest); |
| 67 | }; |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | // This test tests giving Uniscribe a very large buffer, which will cause a |
| 72 | // failure. |
| 73 | TEST_F(UniscribeTest, TooBig) { |
| 74 | // This test will only run on Windows XP. It seems Uniscribe does not have the |
| 75 | // internal limit on Windows 2000 that we rely on to cause this failure. |
| 76 | if (win_util::GetWinVersion() <= win_util::WINVERSION_2000) |
| 77 | return; |
| 78 | |
| 79 | // Make a large string with an e with a zillion combining accents. |
| 80 | std::wstring input(L"e"); |
| 81 | for (int i = 0; i < 100000; i++) |
| 82 | input.push_back(0x301); // Combining acute accent. |
| 83 | |
| 84 | SCRIPT_CACHE* script_cache; |
| 85 | HFONT hfont = MakeFont(L"Times New Roman", &script_cache); |
| 86 | ASSERT_TRUE(hfont); |
| 87 | |
| 88 | // Test a long string without the normal length protection we have. This will |
| 89 | // cause shaping to fail. |
| 90 | { |
| 91 | gfx::UniscribeState uniscribe(input.data(), static_cast<int>(input.size()), |
| 92 | false, hfont, script_cache, &properties_); |
| 93 | uniscribe.InitWithOptionalLengthProtection(false); |
| 94 | |
| 95 | // There should be one shaping entry, with nothing in it. |
| 96 | ASSERT_EQ(1, uniscribe.shapes_->size()); |
| 97 | EXPECT_EQ(0, uniscribe.shapes_[0].glyphs->size()); |
| 98 | EXPECT_EQ(0, uniscribe.shapes_[0].logs->size()); |
| 99 | EXPECT_EQ(0, uniscribe.shapes_[0].visattr->size()); |
| 100 | EXPECT_EQ(0, uniscribe.shapes_[0].advance->size()); |
| 101 | EXPECT_EQ(0, uniscribe.shapes_[0].offsets->size()); |
| 102 | EXPECT_EQ(0, uniscribe.shapes_[0].justify->size()); |
| 103 | EXPECT_EQ(0, uniscribe.shapes_[0].abc.abcA); |
| 104 | EXPECT_EQ(0, uniscribe.shapes_[0].abc.abcB); |
| 105 | EXPECT_EQ(0, uniscribe.shapes_[0].abc.abcC); |
| 106 | |
| 107 | // The sizes of the other stuff should match the shaping entry. |
| 108 | EXPECT_EQ(1, uniscribe.runs_->size()); |
| 109 | EXPECT_EQ(1, uniscribe.screen_order_->size()); |
| 110 | |
| 111 | // Check that the various querying functions handle the empty case properly. |
| 112 | EXPECT_EQ(0, uniscribe.Width()); |
| 113 | EXPECT_EQ(0, uniscribe.FirstGlyphForCharacter(0)); |
| 114 | EXPECT_EQ(0, uniscribe.FirstGlyphForCharacter(1000)); |
| 115 | EXPECT_EQ(0, uniscribe.XToCharacter(0)); |
| 116 | EXPECT_EQ(0, uniscribe.XToCharacter(1000)); |
| 117 | } |
| 118 | |
| 119 | // Now test the very large string and make sure it is handled properly by the |
| 120 | // length protection. |
| 121 | { |
| 122 | gfx::UniscribeState uniscribe(input.data(), static_cast<int>(input.size()), |
| 123 | false, hfont, script_cache, &properties_); |
| 124 | uniscribe.InitWithOptionalLengthProtection(true); |
| 125 | |
| 126 | // There should be 0 runs and shapes. |
| 127 | EXPECT_EQ(0, uniscribe.runs_->size()); |
| 128 | EXPECT_EQ(0, uniscribe.shapes_->size()); |
| 129 | EXPECT_EQ(0, uniscribe.screen_order_->size()); |
| 130 | |
| 131 | EXPECT_EQ(0, uniscribe.Width()); |
| 132 | EXPECT_EQ(0, uniscribe.FirstGlyphForCharacter(0)); |
| 133 | EXPECT_EQ(0, uniscribe.FirstGlyphForCharacter(1000)); |
| 134 | EXPECT_EQ(0, uniscribe.XToCharacter(0)); |
| 135 | EXPECT_EQ(0, uniscribe.XToCharacter(1000)); |
| 136 | } |
| 137 | } |
| 138 | |
deanm@google.com | 9713786 | 2008-08-14 20:44:17 +0900 | [diff] [blame] | 139 | } // namespace gfx |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 140 | |