gfx/tests/gtest/gfxSurfaceRefCountTest.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 #include <stdio.h>
     3 #include "gtest/gtest.h"
     5 #include "gfxASurface.h"
     6 #include "gfxImageSurface.h"
     8 #include "cairo/cairo.h"
    10 int
    11 GetASurfaceRefCount(gfxASurface *s) {
    12     NS_ADDREF(s);
    13     return s->Release();
    14 }
    16 int
    17 CheckInt (int value, int expected) {
    18     if (value != expected) {
    19         fprintf (stderr, "Expected %d got %d\n", expected, value);
    20         return 1;
    21     }
    23     return 0;
    24 }
    26 int
    27 CheckPointer (void *value, void *expected) {
    28     if (value != expected) {
    29         fprintf (stderr, "Expected %p got %p\n", expected, value);
    30         return 1;
    31     }
    33     return 0;
    34 }
    36 static cairo_user_data_key_t destruction_key;
    37 void
    38 SurfaceDestroyNotifier (void *data) {
    39     *(int *)data = 1;
    40 }
    42 int
    43 TestNewSurface () {
    44     int failures = 0;
    45     int destroyed = 0;
    47     nsRefPtr<gfxASurface> s = new gfxImageSurface (gfxIntSize(10, 10), gfxImageFormat::ARGB32);
    48     cairo_surface_t *cs = s->CairoSurface();
    50     cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
    52     failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
    53     failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    54     failures += CheckInt (destroyed, 0);
    56     cairo_surface_reference(cs);
    58     failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
    59     failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
    60     failures += CheckInt (destroyed, 0);
    62     gfxASurface *savedWrapper = s.get();
    64     s = nullptr;
    66     failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    67     failures += CheckInt (destroyed, 0);
    69     s = gfxASurface::Wrap(cs);
    71     failures += CheckPointer (s.get(), savedWrapper);
    72     failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
    73     failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
    74     failures += CheckInt (destroyed, 0);
    76     cairo_surface_destroy(cs);
    78     failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
    79     failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    80     failures += CheckInt (destroyed, 0);
    82     s = nullptr;
    84     failures += CheckInt (destroyed, 1);
    86     return failures;
    87 }
    89 int
    90 TestExistingSurface () {
    91     int failures = 0;
    92     int destroyed = 0;
    94     cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
    96     cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
    98     failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    99     failures += CheckInt (destroyed, 0);
   101     nsRefPtr<gfxASurface> s = gfxASurface::Wrap(cs);
   103     failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
   105     cairo_surface_reference(cs);
   107     failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
   108     failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
   109     failures += CheckInt (destroyed, 0);
   111     gfxASurface *savedWrapper = s.get();
   113     s = nullptr;
   115     failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
   116     failures += CheckInt (destroyed, 0);
   118     s = gfxASurface::Wrap(cs);
   120     failures += CheckPointer (s.get(), savedWrapper);
   121     failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
   122     failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
   123     failures += CheckInt (destroyed, 0);
   125     cairo_surface_destroy(cs);
   127     failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
   128     failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
   129     failures += CheckInt (destroyed, 0);
   131     s = nullptr;
   133     failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
   134     failures += CheckInt (destroyed, 0);
   136     cairo_surface_destroy(cs);
   138     failures += CheckInt (destroyed, 1);
   140     return failures;
   141 }
   143 TEST(Gfx, SurfaceRefCount) {
   144     int fail;
   146     fail = TestNewSurface();
   147     EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures";
   148     fail = TestExistingSurface();
   149     EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures";
   150 }

mercurial