|
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "gtest/gtest.h" |
|
7 |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsTArray.h" |
|
10 #include "nsString.h" |
|
11 #include "nsDependentString.h" |
|
12 |
|
13 #include "mozilla/Preferences.h" |
|
14 |
|
15 #include "gfxContext.h" |
|
16 #include "gfxFont.h" |
|
17 #include "gfxPlatform.h" |
|
18 |
|
19 #include "gfxFontTest.h" |
|
20 |
|
21 using namespace mozilla; |
|
22 |
|
23 enum { |
|
24 S_UTF8 = 0, |
|
25 S_ASCII = 1 |
|
26 }; |
|
27 |
|
28 class FrameTextRunCache; |
|
29 |
|
30 struct LiteralArray { |
|
31 LiteralArray (unsigned long l1) { |
|
32 data.AppendElement(l1); |
|
33 } |
|
34 LiteralArray (unsigned long l1, unsigned long l2) { |
|
35 data.AppendElement(l1); |
|
36 data.AppendElement(l2); |
|
37 } |
|
38 LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3) { |
|
39 data.AppendElement(l1); |
|
40 data.AppendElement(l2); |
|
41 data.AppendElement(l3); |
|
42 } |
|
43 LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3, unsigned long l4) { |
|
44 data.AppendElement(l1); |
|
45 data.AppendElement(l2); |
|
46 data.AppendElement(l3); |
|
47 data.AppendElement(l4); |
|
48 } |
|
49 LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3, unsigned long l4, unsigned long l5) { |
|
50 data.AppendElement(l1); |
|
51 data.AppendElement(l2); |
|
52 data.AppendElement(l3); |
|
53 data.AppendElement(l4); |
|
54 data.AppendElement(l5); |
|
55 } |
|
56 |
|
57 LiteralArray (const LiteralArray& other) { |
|
58 data = other.data; |
|
59 } |
|
60 |
|
61 nsTArray<unsigned long> data; |
|
62 }; |
|
63 |
|
64 #define GLYPHS LiteralArray |
|
65 |
|
66 struct TestEntry { |
|
67 TestEntry (const char *aUTF8FamilyString, |
|
68 const gfxFontStyle& aFontStyle, |
|
69 const char *aString) |
|
70 : utf8FamilyString(aUTF8FamilyString), |
|
71 fontStyle(aFontStyle), |
|
72 stringType(S_ASCII), |
|
73 string(aString), |
|
74 isRTL(false) |
|
75 { |
|
76 } |
|
77 |
|
78 TestEntry (const char *aUTF8FamilyString, |
|
79 const gfxFontStyle& aFontStyle, |
|
80 int stringType, |
|
81 const char *aString) |
|
82 : utf8FamilyString(aUTF8FamilyString), |
|
83 fontStyle(aFontStyle), |
|
84 stringType(stringType), |
|
85 string(aString), |
|
86 isRTL(false) |
|
87 { |
|
88 } |
|
89 |
|
90 struct ExpectItem { |
|
91 ExpectItem(const nsCString& aFontName, |
|
92 const LiteralArray& aGlyphs) |
|
93 : fontName(aFontName), glyphs(aGlyphs) |
|
94 { } |
|
95 |
|
96 bool Compare(const nsCString& aFontName, |
|
97 cairo_glyph_t *aGlyphs, |
|
98 int num_glyphs) |
|
99 { |
|
100 // bit that allowed for empty fontname to match all is commented |
|
101 // out |
|
102 if (/*!fontName.IsEmpty() &&*/ !fontName.Equals(aFontName)) |
|
103 return false; |
|
104 |
|
105 if (num_glyphs != int(glyphs.data.Length())) |
|
106 return false; |
|
107 |
|
108 for (int j = 0; j < num_glyphs; j++) { |
|
109 if (glyphs.data[j] != aGlyphs[j].index) |
|
110 return false; |
|
111 } |
|
112 |
|
113 return true; |
|
114 } |
|
115 |
|
116 nsCString fontName; |
|
117 LiteralArray glyphs; |
|
118 }; |
|
119 |
|
120 void SetRTL() |
|
121 { |
|
122 isRTL = true; |
|
123 } |
|
124 |
|
125 // Empty/nullptr fontName means ignore font name |
|
126 void Expect (const char *platform, |
|
127 const char *fontName, |
|
128 const LiteralArray& glyphs) |
|
129 { |
|
130 if (fontName) |
|
131 Expect (platform, nsDependentCString(fontName), glyphs); |
|
132 else |
|
133 Expect (platform, nsCString(), glyphs); |
|
134 } |
|
135 |
|
136 void Expect (const char *platform, |
|
137 const nsCString& fontName, |
|
138 const LiteralArray& glyphs) |
|
139 { |
|
140 #if defined(XP_WIN) |
|
141 if (strcmp(platform, "win32")) |
|
142 return; |
|
143 #elif defined(XP_MACOSX) |
|
144 if (strcmp(platform, "macosx")) |
|
145 return; |
|
146 #elif defined(XP_UNIX) |
|
147 if (strcmp(platform, "gtk2-pango")) |
|
148 return; |
|
149 #else |
|
150 return; |
|
151 #endif |
|
152 |
|
153 expectItems.AppendElement(ExpectItem(fontName, glyphs)); |
|
154 } |
|
155 |
|
156 bool Check (gfxFontTestStore *store) { |
|
157 if (expectItems.Length() == 0 || |
|
158 store->items.Length() != expectItems.Length()) |
|
159 { |
|
160 return false; |
|
161 } |
|
162 |
|
163 for (uint32_t i = 0; i < expectItems.Length(); i++) { |
|
164 if (!expectItems[i].Compare(store->items[i].platformFont, |
|
165 store->items[i].glyphs, |
|
166 store->items[i].num_glyphs)) |
|
167 return false; |
|
168 } |
|
169 |
|
170 return true; |
|
171 } |
|
172 |
|
173 const char *utf8FamilyString; |
|
174 gfxFontStyle fontStyle; |
|
175 |
|
176 int stringType; |
|
177 const char *string; |
|
178 bool isRTL; |
|
179 |
|
180 nsTArray<ExpectItem> expectItems; |
|
181 }; |
|
182 |
|
183 static already_AddRefed<gfxContext> |
|
184 MakeContext () |
|
185 { |
|
186 const int size = 200; |
|
187 |
|
188 nsRefPtr<gfxASurface> surface; |
|
189 |
|
190 surface = gfxPlatform::GetPlatform()-> |
|
191 CreateOffscreenSurface(IntSize(size, size), |
|
192 gfxASurface::ContentFromFormat(gfxImageFormat::RGB24)); |
|
193 nsRefPtr<gfxContext> ctx = new gfxContext(surface); |
|
194 return ctx.forget(); |
|
195 } |
|
196 |
|
197 TestEntry* |
|
198 AddTest (nsTArray<TestEntry>& testList, |
|
199 const char *utf8FamilyString, |
|
200 const gfxFontStyle& fontStyle, |
|
201 int stringType, |
|
202 const char *string) |
|
203 { |
|
204 TestEntry te (utf8FamilyString, |
|
205 fontStyle, |
|
206 stringType, |
|
207 string); |
|
208 |
|
209 testList.AppendElement(te); |
|
210 |
|
211 return &(testList[testList.Length()-1]); |
|
212 } |
|
213 |
|
214 void |
|
215 DumpStore (gfxFontTestStore *store) { |
|
216 if (store->items.Length() == 0) { |
|
217 printf ("(empty)\n"); |
|
218 } |
|
219 |
|
220 for (uint32_t i = 0; |
|
221 i < store->items.Length(); |
|
222 i++) |
|
223 { |
|
224 printf ("Run[% 2d]: '%s' ", i, store->items[i].platformFont.BeginReading()); |
|
225 |
|
226 for (int j = 0; j < store->items[i].num_glyphs; j++) |
|
227 printf ("%d ", int(store->items[i].glyphs[j].index)); |
|
228 |
|
229 printf ("\n"); |
|
230 } |
|
231 } |
|
232 |
|
233 void |
|
234 DumpTestExpect (TestEntry *test) { |
|
235 for (uint32_t i = 0; i < test->expectItems.Length(); i++) { |
|
236 printf ("Run[% 2d]: '%s' ", i, test->expectItems[i].fontName.BeginReading()); |
|
237 for (uint32_t j = 0; j < test->expectItems[i].glyphs.data.Length(); j++) |
|
238 printf ("%d ", int(test->expectItems[i].glyphs.data[j])); |
|
239 |
|
240 printf ("\n"); |
|
241 } |
|
242 } |
|
243 |
|
244 void SetupTests(nsTArray<TestEntry>& testList); |
|
245 |
|
246 static bool |
|
247 RunTest (TestEntry *test, gfxContext *ctx) { |
|
248 nsRefPtr<gfxFontGroup> fontGroup; |
|
249 |
|
250 fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->utf8FamilyString), &test->fontStyle, nullptr); |
|
251 |
|
252 nsAutoPtr<gfxTextRun> textRun; |
|
253 gfxTextRunFactory::Parameters params = { |
|
254 ctx, nullptr, nullptr, nullptr, 0, 60 |
|
255 }; |
|
256 uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT; |
|
257 if (test->isRTL) { |
|
258 flags |= gfxTextRunFactory::TEXT_IS_RTL; |
|
259 } |
|
260 uint32_t length; |
|
261 if (test->stringType == S_ASCII) { |
|
262 flags |= gfxTextRunFactory::TEXT_IS_ASCII | gfxTextRunFactory::TEXT_IS_8BIT; |
|
263 length = strlen(test->string); |
|
264 textRun = fontGroup->MakeTextRun(reinterpret_cast<const uint8_t*>(test->string), length, ¶ms, flags); |
|
265 } else { |
|
266 NS_ConvertUTF8toUTF16 str(nsDependentCString(test->string)); |
|
267 length = str.Length(); |
|
268 textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags); |
|
269 } |
|
270 |
|
271 gfxFontTestStore::NewStore(); |
|
272 textRun->Draw(ctx, gfxPoint(0,0), DrawMode::GLYPH_FILL, 0, length, nullptr, nullptr, nullptr); |
|
273 gfxFontTestStore *s = gfxFontTestStore::CurrentStore(); |
|
274 |
|
275 if (!test->Check(s)) { |
|
276 DumpStore(s); |
|
277 printf (" expected:\n"); |
|
278 DumpTestExpect(test); |
|
279 gfxFontTestStore::DeleteStore(); |
|
280 return false; |
|
281 } |
|
282 |
|
283 gfxFontTestStore::DeleteStore(); |
|
284 |
|
285 return true; |
|
286 } |
|
287 |
|
288 TEST(Gfx, FontSelection) { |
|
289 int passed = 0; |
|
290 int failed = 0; |
|
291 |
|
292 // set up the tests |
|
293 nsTArray<TestEntry> testList; |
|
294 SetupTests(testList); |
|
295 |
|
296 nsRefPtr<gfxContext> context = MakeContext(); |
|
297 |
|
298 for (uint32_t test = 0; |
|
299 test < testList.Length(); |
|
300 test++) |
|
301 { |
|
302 bool result = RunTest (&testList[test], context); |
|
303 if (result) { |
|
304 passed++; |
|
305 } else { |
|
306 printf ("Test %d failed\n", test); |
|
307 failed++; |
|
308 } |
|
309 } |
|
310 } |
|
311 |
|
312 // The tests themselves |
|
313 |
|
314 #include "gfxFontSelectionTests.h" |