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.

     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/. */
     6 #include "gtest/gtest.h"
     8 #include "nsCOMPtr.h"
     9 #include "nsTArray.h"
    10 #include "nsString.h"
    11 #include "nsDependentString.h"
    13 #include "mozilla/Preferences.h"
    15 #include "gfxContext.h"
    16 #include "gfxFont.h"
    17 #include "gfxPlatform.h"
    19 #include "gfxFontTest.h"
    21 using namespace mozilla;
    23 enum {
    24     S_UTF8 = 0,
    25     S_ASCII = 1
    26 };
    28 class FrameTextRunCache;
    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     }
    57     LiteralArray (const LiteralArray& other) {
    58         data = other.data;
    59     }
    61     nsTArray<unsigned long> data;
    62 };
    64 #define GLYPHS LiteralArray
    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     }
    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     }
    90     struct ExpectItem {
    91         ExpectItem(const nsCString& aFontName,
    92                    const LiteralArray& aGlyphs)
    93             : fontName(aFontName), glyphs(aGlyphs)
    94         { }
    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;
   105             if (num_glyphs != int(glyphs.data.Length()))
   106                 return false;
   108             for (int j = 0; j < num_glyphs; j++) {
   109                 if (glyphs.data[j] != aGlyphs[j].index)
   110                 return false;
   111             }
   113             return true;
   114         }
   116         nsCString fontName;
   117         LiteralArray glyphs;
   118     };
   120     void SetRTL()
   121     {
   122         isRTL = true;
   123     }
   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     }
   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
   153         expectItems.AppendElement(ExpectItem(fontName, glyphs));
   154     }
   156     bool Check (gfxFontTestStore *store) {
   157         if (expectItems.Length() == 0 ||
   158             store->items.Length() != expectItems.Length())
   159         {
   160             return false;
   161         }
   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         }
   170         return true;
   171     }
   173     const char *utf8FamilyString;
   174     gfxFontStyle fontStyle;
   176     int stringType;
   177     const char *string;
   178     bool isRTL;
   180     nsTArray<ExpectItem> expectItems;
   181 };
   183 static already_AddRefed<gfxContext>
   184 MakeContext ()
   185 {
   186     const int size = 200;
   188     nsRefPtr<gfxASurface> surface;
   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 }
   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);
   209     testList.AppendElement(te);
   211     return &(testList[testList.Length()-1]);
   212 }
   214 void
   215 DumpStore (gfxFontTestStore *store) {
   216     if (store->items.Length() == 0) {
   217         printf ("(empty)\n");
   218     }
   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());
   226         for (int j = 0; j < store->items[i].num_glyphs; j++)
   227             printf ("%d ", int(store->items[i].glyphs[j].index));
   229         printf ("\n");
   230     }
   231 }
   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]));
   240         printf ("\n");
   241     }
   242 }
   244 void SetupTests(nsTArray<TestEntry>& testList);
   246 static bool
   247 RunTest (TestEntry *test, gfxContext *ctx) {
   248     nsRefPtr<gfxFontGroup> fontGroup;
   250     fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->utf8FamilyString), &test->fontStyle, nullptr);
   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, &params, flags);
   265     } else {
   266         NS_ConvertUTF8toUTF16 str(nsDependentCString(test->string));
   267         length = str.Length();
   268         textRun = fontGroup->MakeTextRun(str.get(), length, &params, flags);
   269     }
   271     gfxFontTestStore::NewStore();
   272     textRun->Draw(ctx, gfxPoint(0,0), DrawMode::GLYPH_FILL, 0, length, nullptr, nullptr, nullptr);
   273     gfxFontTestStore *s = gfxFontTestStore::CurrentStore();
   275     if (!test->Check(s)) {
   276         DumpStore(s);
   277         printf ("  expected:\n");
   278         DumpTestExpect(test);
   279         gfxFontTestStore::DeleteStore();
   280         return false;
   281     }
   283     gfxFontTestStore::DeleteStore();
   285     return true;
   286 }
   288 TEST(Gfx, FontSelection) {
   289     int passed = 0;
   290     int failed = 0;
   292     // set up the tests
   293     nsTArray<TestEntry> testList;
   294     SetupTests(testList);
   296     nsRefPtr<gfxContext> context = MakeContext();
   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 }
   312 // The tests themselves
   314 #include "gfxFontSelectionTests.h"

mercurial