3 #include "gfxASurface.h"
4 #include "gfxImageSurface.h"
9 GetASurfaceRefCount(gfxASurface
*s
) {
15 CheckInt (int value
, int expected
) {
16 if (value
!= expected
) {
17 fprintf (stderr
, "Expected %d got %d\n", expected
, value
);
25 CheckPointer (void *value
, void *expected
) {
26 if (value
!= expected
) {
27 fprintf (stderr
, "Expected %p got %p\n", expected
, value
);
34 static cairo_user_data_key_t destruction_key
;
36 SurfaceDestroyNotifier (void *data
) {
45 nsRefPtr
<gfxASurface
> s
= new gfxImageSurface (gfxIntSize(10, 10), gfxASurface::ImageFormatARGB32
);
46 cairo_surface_t
*cs
= s
->CairoSurface();
48 cairo_surface_set_user_data (cs
, &destruction_key
, &destroyed
, SurfaceDestroyNotifier
);
50 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 1);
51 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 1);
52 failures
+= CheckInt (destroyed
, 0);
54 cairo_surface_reference(cs
);
56 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 2);
57 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 2);
58 failures
+= CheckInt (destroyed
, 0);
60 gfxASurface
*savedWrapper
= s
.get();
64 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 1);
65 failures
+= CheckInt (destroyed
, 0);
67 s
= gfxASurface::Wrap(cs
);
69 failures
+= CheckPointer (s
.get(), savedWrapper
);
70 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 2);
71 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 2);
72 failures
+= CheckInt (destroyed
, 0);
74 cairo_surface_destroy(cs
);
76 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 1);
77 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 1);
78 failures
+= CheckInt (destroyed
, 0);
82 failures
+= CheckInt (destroyed
, 1);
88 TestExistingSurface () {
92 cairo_surface_t
*cs
= cairo_image_surface_create (CAIRO_FORMAT_ARGB32
, 10, 10);
94 cairo_surface_set_user_data (cs
, &destruction_key
, &destroyed
, SurfaceDestroyNotifier
);
96 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 1);
97 failures
+= CheckInt (destroyed
, 0);
99 nsRefPtr
<gfxASurface
> s
= gfxASurface::Wrap(cs
);
101 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 2);
103 cairo_surface_reference(cs
);
105 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 3);
106 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 3);
107 failures
+= CheckInt (destroyed
, 0);
109 gfxASurface
*savedWrapper
= s
.get();
113 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 2);
114 failures
+= CheckInt (destroyed
, 0);
116 s
= gfxASurface::Wrap(cs
);
118 failures
+= CheckPointer (s
.get(), savedWrapper
);
119 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 3);
120 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 3);
121 failures
+= CheckInt (destroyed
, 0);
123 cairo_surface_destroy(cs
);
125 failures
+= CheckInt (GetASurfaceRefCount(s
.get()), 2);
126 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 2);
127 failures
+= CheckInt (destroyed
, 0);
131 failures
+= CheckInt (cairo_surface_get_reference_count(cs
), 1);
132 failures
+= CheckInt (destroyed
, 0);
134 cairo_surface_destroy(cs
);
136 failures
+= CheckInt (destroyed
, 1);
142 main (int argc
, char **argv
) {
145 fail
= TestNewSurface();
146 fprintf (stderr
, "TestNewSurface: %d failures\n", fail
);
147 fail
= TestExistingSurface();
148 fprintf (stderr
, "TestExistingSurface: %d failures\n", fail
);