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