Bug 595132 - Don't trap and send context menu events destined for the browser to...
[mozilla-central.git] / gfx / tests / gfxSurfaceRefCountTest.cpp
blobf4b66142bd04bf708a8d6e5d3a458d5f7e9f251e
1 #include <stdio.h>
3 #include "gfxASurface.h"
4 #include "gfxImageSurface.h"
6 #include "cairo.h"
8 int
9 GetASurfaceRefCount(gfxASurface *s) {
10 NS_ADDREF(s);
11 return s->Release();
14 int
15 CheckInt (int value, int expected) {
16 if (value != expected) {
17 fprintf (stderr, "Expected %d got %d\n", expected, value);
18 return 1;
21 return 0;
24 int
25 CheckPointer (void *value, void *expected) {
26 if (value != expected) {
27 fprintf (stderr, "Expected %p got %p\n", expected, value);
28 return 1;
31 return 0;
34 static cairo_user_data_key_t destruction_key;
35 void
36 SurfaceDestroyNotifier (void *data) {
37 *(int *)data = 1;
40 int
41 TestNewSurface () {
42 int failures = 0;
43 int destroyed = 0;
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();
62 s = nsnull;
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);
80 s = nsnull;
82 failures += CheckInt (destroyed, 1);
84 return failures;
87 int
88 TestExistingSurface () {
89 int failures = 0;
90 int destroyed = 0;
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();
111 s = nsnull;
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);
129 s = nsnull;
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);
138 return failures;
142 main (int argc, char **argv) {
143 int fail;
145 fail = TestNewSurface();
146 fprintf (stderr, "TestNewSurface: %d failures\n", fail);
147 fail = TestExistingSurface();
148 fprintf (stderr, "TestExistingSurface: %d failures\n", fail);