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.

michael@0 1 #include <stdio.h>
michael@0 2
michael@0 3 #include "gtest/gtest.h"
michael@0 4
michael@0 5 #include "gfxASurface.h"
michael@0 6 #include "gfxImageSurface.h"
michael@0 7
michael@0 8 #include "cairo/cairo.h"
michael@0 9
michael@0 10 int
michael@0 11 GetASurfaceRefCount(gfxASurface *s) {
michael@0 12 NS_ADDREF(s);
michael@0 13 return s->Release();
michael@0 14 }
michael@0 15
michael@0 16 int
michael@0 17 CheckInt (int value, int expected) {
michael@0 18 if (value != expected) {
michael@0 19 fprintf (stderr, "Expected %d got %d\n", expected, value);
michael@0 20 return 1;
michael@0 21 }
michael@0 22
michael@0 23 return 0;
michael@0 24 }
michael@0 25
michael@0 26 int
michael@0 27 CheckPointer (void *value, void *expected) {
michael@0 28 if (value != expected) {
michael@0 29 fprintf (stderr, "Expected %p got %p\n", expected, value);
michael@0 30 return 1;
michael@0 31 }
michael@0 32
michael@0 33 return 0;
michael@0 34 }
michael@0 35
michael@0 36 static cairo_user_data_key_t destruction_key;
michael@0 37 void
michael@0 38 SurfaceDestroyNotifier (void *data) {
michael@0 39 *(int *)data = 1;
michael@0 40 }
michael@0 41
michael@0 42 int
michael@0 43 TestNewSurface () {
michael@0 44 int failures = 0;
michael@0 45 int destroyed = 0;
michael@0 46
michael@0 47 nsRefPtr<gfxASurface> s = new gfxImageSurface (gfxIntSize(10, 10), gfxImageFormat::ARGB32);
michael@0 48 cairo_surface_t *cs = s->CairoSurface();
michael@0 49
michael@0 50 cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
michael@0 51
michael@0 52 failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
michael@0 53 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
michael@0 54 failures += CheckInt (destroyed, 0);
michael@0 55
michael@0 56 cairo_surface_reference(cs);
michael@0 57
michael@0 58 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
michael@0 59 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
michael@0 60 failures += CheckInt (destroyed, 0);
michael@0 61
michael@0 62 gfxASurface *savedWrapper = s.get();
michael@0 63
michael@0 64 s = nullptr;
michael@0 65
michael@0 66 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
michael@0 67 failures += CheckInt (destroyed, 0);
michael@0 68
michael@0 69 s = gfxASurface::Wrap(cs);
michael@0 70
michael@0 71 failures += CheckPointer (s.get(), savedWrapper);
michael@0 72 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
michael@0 73 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
michael@0 74 failures += CheckInt (destroyed, 0);
michael@0 75
michael@0 76 cairo_surface_destroy(cs);
michael@0 77
michael@0 78 failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
michael@0 79 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
michael@0 80 failures += CheckInt (destroyed, 0);
michael@0 81
michael@0 82 s = nullptr;
michael@0 83
michael@0 84 failures += CheckInt (destroyed, 1);
michael@0 85
michael@0 86 return failures;
michael@0 87 }
michael@0 88
michael@0 89 int
michael@0 90 TestExistingSurface () {
michael@0 91 int failures = 0;
michael@0 92 int destroyed = 0;
michael@0 93
michael@0 94 cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
michael@0 95
michael@0 96 cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
michael@0 97
michael@0 98 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
michael@0 99 failures += CheckInt (destroyed, 0);
michael@0 100
michael@0 101 nsRefPtr<gfxASurface> s = gfxASurface::Wrap(cs);
michael@0 102
michael@0 103 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
michael@0 104
michael@0 105 cairo_surface_reference(cs);
michael@0 106
michael@0 107 failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
michael@0 108 failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
michael@0 109 failures += CheckInt (destroyed, 0);
michael@0 110
michael@0 111 gfxASurface *savedWrapper = s.get();
michael@0 112
michael@0 113 s = nullptr;
michael@0 114
michael@0 115 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
michael@0 116 failures += CheckInt (destroyed, 0);
michael@0 117
michael@0 118 s = gfxASurface::Wrap(cs);
michael@0 119
michael@0 120 failures += CheckPointer (s.get(), savedWrapper);
michael@0 121 failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
michael@0 122 failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
michael@0 123 failures += CheckInt (destroyed, 0);
michael@0 124
michael@0 125 cairo_surface_destroy(cs);
michael@0 126
michael@0 127 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
michael@0 128 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
michael@0 129 failures += CheckInt (destroyed, 0);
michael@0 130
michael@0 131 s = nullptr;
michael@0 132
michael@0 133 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
michael@0 134 failures += CheckInt (destroyed, 0);
michael@0 135
michael@0 136 cairo_surface_destroy(cs);
michael@0 137
michael@0 138 failures += CheckInt (destroyed, 1);
michael@0 139
michael@0 140 return failures;
michael@0 141 }
michael@0 142
michael@0 143 TEST(Gfx, SurfaceRefCount) {
michael@0 144 int fail;
michael@0 145
michael@0 146 fail = TestNewSurface();
michael@0 147 EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures";
michael@0 148 fail = TestExistingSurface();
michael@0 149 EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures";
michael@0 150 }
michael@0 151

mercurial