gfx/tests/gtest/gfxSurfaceRefCountTest.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:8283d714a324
1 #include <stdio.h>
2
3 #include "gtest/gtest.h"
4
5 #include "gfxASurface.h"
6 #include "gfxImageSurface.h"
7
8 #include "cairo/cairo.h"
9
10 int
11 GetASurfaceRefCount(gfxASurface *s) {
12 NS_ADDREF(s);
13 return s->Release();
14 }
15
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 }
22
23 return 0;
24 }
25
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 }
32
33 return 0;
34 }
35
36 static cairo_user_data_key_t destruction_key;
37 void
38 SurfaceDestroyNotifier (void *data) {
39 *(int *)data = 1;
40 }
41
42 int
43 TestNewSurface () {
44 int failures = 0;
45 int destroyed = 0;
46
47 nsRefPtr<gfxASurface> s = new gfxImageSurface (gfxIntSize(10, 10), gfxImageFormat::ARGB32);
48 cairo_surface_t *cs = s->CairoSurface();
49
50 cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
51
52 failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
53 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
54 failures += CheckInt (destroyed, 0);
55
56 cairo_surface_reference(cs);
57
58 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
59 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
60 failures += CheckInt (destroyed, 0);
61
62 gfxASurface *savedWrapper = s.get();
63
64 s = nullptr;
65
66 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
67 failures += CheckInt (destroyed, 0);
68
69 s = gfxASurface::Wrap(cs);
70
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);
75
76 cairo_surface_destroy(cs);
77
78 failures += CheckInt (GetASurfaceRefCount(s.get()), 1);
79 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
80 failures += CheckInt (destroyed, 0);
81
82 s = nullptr;
83
84 failures += CheckInt (destroyed, 1);
85
86 return failures;
87 }
88
89 int
90 TestExistingSurface () {
91 int failures = 0;
92 int destroyed = 0;
93
94 cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
95
96 cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier);
97
98 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
99 failures += CheckInt (destroyed, 0);
100
101 nsRefPtr<gfxASurface> s = gfxASurface::Wrap(cs);
102
103 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
104
105 cairo_surface_reference(cs);
106
107 failures += CheckInt (GetASurfaceRefCount(s.get()), 3);
108 failures += CheckInt (cairo_surface_get_reference_count(cs), 3);
109 failures += CheckInt (destroyed, 0);
110
111 gfxASurface *savedWrapper = s.get();
112
113 s = nullptr;
114
115 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
116 failures += CheckInt (destroyed, 0);
117
118 s = gfxASurface::Wrap(cs);
119
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);
124
125 cairo_surface_destroy(cs);
126
127 failures += CheckInt (GetASurfaceRefCount(s.get()), 2);
128 failures += CheckInt (cairo_surface_get_reference_count(cs), 2);
129 failures += CheckInt (destroyed, 0);
130
131 s = nullptr;
132
133 failures += CheckInt (cairo_surface_get_reference_count(cs), 1);
134 failures += CheckInt (destroyed, 0);
135
136 cairo_surface_destroy(cs);
137
138 failures += CheckInt (destroyed, 1);
139
140 return failures;
141 }
142
143 TEST(Gfx, SurfaceRefCount) {
144 int fail;
145
146 fail = TestNewSurface();
147 EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures";
148 fail = TestExistingSurface();
149 EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures";
150 }
151

mercurial