gfx/tests/gtest/gfxSurfaceRefCountTest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/tests/gtest/gfxSurfaceRefCountTest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +#include <stdio.h>
     1.5 +
     1.6 +#include "gtest/gtest.h"
     1.7 +
     1.8 +#include "gfxASurface.h"
     1.9 +#include "gfxImageSurface.h"
    1.10 +
    1.11 +#include "cairo/cairo.h"
    1.12 +
    1.13 +int
    1.14 +GetASurfaceRefCount(gfxASurface *s) {
    1.15 +    NS_ADDREF(s);
    1.16 +    return s->Release();
    1.17 +}
    1.18 +
    1.19 +int
    1.20 +CheckInt (int value, int expected) {
    1.21 +    if (value != expected) {
    1.22 +        fprintf (stderr, "Expected %d got %d\n", expected, value);
    1.23 +        return 1;
    1.24 +    }
    1.25 +
    1.26 +    return 0;
    1.27 +}
    1.28 +
    1.29 +int
    1.30 +CheckPointer (void *value, void *expected) {
    1.31 +    if (value != expected) {
    1.32 +        fprintf (stderr, "Expected %p got %p\n", expected, value);
    1.33 +        return 1;
    1.34 +    }
    1.35 +
    1.36 +    return 0;
    1.37 +}
    1.38 +
    1.39 +static cairo_user_data_key_t destruction_key;
    1.40 +void
    1.41 +SurfaceDestroyNotifier (void *data) {
    1.42 +    *(int *)data = 1;
    1.43 +}
    1.44 +
    1.45 +int
    1.46 +TestNewSurface () {
    1.47 +    int failures = 0;
    1.48 +    int destroyed = 0;
    1.49 +
    1.50 +    nsRefPtr<gfxASurface> s = new gfxImageSurface (gfxIntSize(10, 10), gfxImageFormat::ARGB32);
    1.51 +    cairo_surface_t *cs = s->CairoSurface();
    1.52 +
    1.53 +    cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
    1.54 +
    1.55 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
    1.56 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    1.57 +    failures += CheckInt (destroyed, 0);
    1.58 +
    1.59 +    cairo_surface_reference(cs);
    1.60 +
    1.61 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
    1.62 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
    1.63 +    failures += CheckInt (destroyed, 0);
    1.64 +
    1.65 +    gfxASurface *savedWrapper = s.get();
    1.66 +
    1.67 +    s = nullptr;
    1.68 +
    1.69 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    1.70 +    failures += CheckInt (destroyed, 0);
    1.71 +
    1.72 +    s = gfxASurface::Wrap(cs);
    1.73 +
    1.74 +    failures += CheckPointer (s.get(), savedWrapper);
    1.75 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
    1.76 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
    1.77 +    failures += CheckInt (destroyed, 0);
    1.78 +
    1.79 +    cairo_surface_destroy(cs);
    1.80 +
    1.81 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
    1.82 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
    1.83 +    failures += CheckInt (destroyed, 0);
    1.84 +
    1.85 +    s = nullptr;
    1.86 +
    1.87 +    failures += CheckInt (destroyed, 1);
    1.88 +
    1.89 +    return failures;
    1.90 +}
    1.91 +
    1.92 +int
    1.93 +TestExistingSurface () {
    1.94 +    int failures = 0;
    1.95 +    int destroyed = 0;
    1.96 +
    1.97 +    cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
    1.98 +
    1.99 +    cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
   1.100 +
   1.101 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
   1.102 +    failures += CheckInt (destroyed, 0);
   1.103 +
   1.104 +    nsRefPtr<gfxASurface> s = gfxASurface::Wrap(cs);
   1.105 +
   1.106 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
   1.107 +
   1.108 +    cairo_surface_reference(cs);
   1.109 +
   1.110 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
   1.111 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
   1.112 +    failures += CheckInt (destroyed, 0);
   1.113 +
   1.114 +    gfxASurface *savedWrapper = s.get();
   1.115 +
   1.116 +    s = nullptr;
   1.117 +
   1.118 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
   1.119 +    failures += CheckInt (destroyed, 0);
   1.120 +
   1.121 +    s = gfxASurface::Wrap(cs);
   1.122 +
   1.123 +    failures += CheckPointer (s.get(), savedWrapper);
   1.124 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
   1.125 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
   1.126 +    failures += CheckInt (destroyed, 0);
   1.127 +
   1.128 +    cairo_surface_destroy(cs);
   1.129 +
   1.130 +    failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
   1.131 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
   1.132 +    failures += CheckInt (destroyed, 0);
   1.133 +
   1.134 +    s = nullptr;
   1.135 +
   1.136 +    failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
   1.137 +    failures += CheckInt (destroyed, 0);
   1.138 +
   1.139 +    cairo_surface_destroy(cs);
   1.140 +
   1.141 +    failures += CheckInt (destroyed, 1);
   1.142 +
   1.143 +    return failures;
   1.144 +}
   1.145 +
   1.146 +TEST(Gfx, SurfaceRefCount) {
   1.147 +    int fail;
   1.148 +
   1.149 +    fail = TestNewSurface();
   1.150 +    EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures";
   1.151 +    fail = TestExistingSurface();
   1.152 +    EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures";
   1.153 +}
   1.154 +

mercurial