gfx/tests/gtest/gfxFontSelectionTest.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial