/src/mozilla-central/gfx/tests/gtest/gfxSurfaceRefCountTest.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include <stdio.h> |
8 | | |
9 | | #include "gtest/gtest.h" |
10 | | |
11 | | #include "gfxASurface.h" |
12 | | #include "gfxImageSurface.h" |
13 | | |
14 | | #include "cairo.h" |
15 | | |
16 | | int |
17 | 0 | GetASurfaceRefCount(gfxASurface *s) { |
18 | 0 | NS_ADDREF(s); |
19 | 0 | return s->Release(); |
20 | 0 | } |
21 | | |
22 | | int |
23 | 0 | CheckInt (int value, int expected) { |
24 | 0 | if (value != expected) { |
25 | 0 | fprintf (stderr, "Expected %d got %d\n", expected, value); |
26 | 0 | return 1; |
27 | 0 | } |
28 | 0 | |
29 | 0 | return 0; |
30 | 0 | } |
31 | | |
32 | | int |
33 | 0 | CheckPointer (void *value, void *expected) { |
34 | 0 | if (value != expected) { |
35 | 0 | fprintf (stderr, "Expected %p got %p\n", expected, value); |
36 | 0 | return 1; |
37 | 0 | } |
38 | 0 | |
39 | 0 | return 0; |
40 | 0 | } |
41 | | |
42 | | static cairo_user_data_key_t destruction_key; |
43 | | void |
44 | 0 | SurfaceDestroyNotifier (void *data) { |
45 | 0 | *(int *)data = 1; |
46 | 0 | } |
47 | | |
48 | | int |
49 | 0 | TestNewSurface () { |
50 | 0 | int failures = 0; |
51 | 0 | int destroyed = 0; |
52 | 0 |
|
53 | 0 | RefPtr<gfxASurface> s = new gfxImageSurface (mozilla::gfx::IntSize(10, 10), SurfaceFormat::A8R8G8B8_UINT32); |
54 | 0 | cairo_surface_t *cs = s->CairoSurface(); |
55 | 0 |
|
56 | 0 | cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier); |
57 | 0 |
|
58 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 1); |
59 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 1); |
60 | 0 | failures += CheckInt (destroyed, 0); |
61 | 0 |
|
62 | 0 | cairo_surface_reference(cs); |
63 | 0 |
|
64 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 2); |
65 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 2); |
66 | 0 | failures += CheckInt (destroyed, 0); |
67 | 0 |
|
68 | 0 | gfxASurface *savedWrapper = s.get(); |
69 | 0 |
|
70 | 0 | s = nullptr; |
71 | 0 |
|
72 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 1); |
73 | 0 | failures += CheckInt (destroyed, 0); |
74 | 0 |
|
75 | 0 | s = gfxASurface::Wrap(cs); |
76 | 0 |
|
77 | 0 | failures += CheckPointer (s.get(), savedWrapper); |
78 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 2); |
79 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 2); |
80 | 0 | failures += CheckInt (destroyed, 0); |
81 | 0 |
|
82 | 0 | cairo_surface_destroy(cs); |
83 | 0 |
|
84 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 1); |
85 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 1); |
86 | 0 | failures += CheckInt (destroyed, 0); |
87 | 0 |
|
88 | 0 | s = nullptr; |
89 | 0 |
|
90 | 0 | failures += CheckInt (destroyed, 1); |
91 | 0 |
|
92 | 0 | return failures; |
93 | 0 | } |
94 | | |
95 | | int |
96 | 0 | TestExistingSurface () { |
97 | 0 | int failures = 0; |
98 | 0 | int destroyed = 0; |
99 | 0 |
|
100 | 0 | cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10); |
101 | 0 |
|
102 | 0 | cairo_surface_set_user_data (cs, &destruction_key, &destroyed, SurfaceDestroyNotifier); |
103 | 0 |
|
104 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 1); |
105 | 0 | failures += CheckInt (destroyed, 0); |
106 | 0 |
|
107 | 0 | RefPtr<gfxASurface> s = gfxASurface::Wrap(cs); |
108 | 0 |
|
109 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 2); |
110 | 0 |
|
111 | 0 | cairo_surface_reference(cs); |
112 | 0 |
|
113 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 3); |
114 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 3); |
115 | 0 | failures += CheckInt (destroyed, 0); |
116 | 0 |
|
117 | 0 | gfxASurface *savedWrapper = s.get(); |
118 | 0 |
|
119 | 0 | s = nullptr; |
120 | 0 |
|
121 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 2); |
122 | 0 | failures += CheckInt (destroyed, 0); |
123 | 0 |
|
124 | 0 | s = gfxASurface::Wrap(cs); |
125 | 0 |
|
126 | 0 | failures += CheckPointer (s.get(), savedWrapper); |
127 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 3); |
128 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 3); |
129 | 0 | failures += CheckInt (destroyed, 0); |
130 | 0 |
|
131 | 0 | cairo_surface_destroy(cs); |
132 | 0 |
|
133 | 0 | failures += CheckInt (GetASurfaceRefCount(s.get()), 2); |
134 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 2); |
135 | 0 | failures += CheckInt (destroyed, 0); |
136 | 0 |
|
137 | 0 | s = nullptr; |
138 | 0 |
|
139 | 0 | failures += CheckInt (cairo_surface_get_reference_count(cs), 1); |
140 | 0 | failures += CheckInt (destroyed, 0); |
141 | 0 |
|
142 | 0 | cairo_surface_destroy(cs); |
143 | 0 |
|
144 | 0 | failures += CheckInt (destroyed, 1); |
145 | 0 |
|
146 | 0 | return failures; |
147 | 0 | } |
148 | | |
149 | 0 | TEST(Gfx, SurfaceRefCount) { |
150 | 0 | int fail; |
151 | 0 |
|
152 | 0 | fail = TestNewSurface(); |
153 | 0 | EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures"; |
154 | 0 | fail = TestExistingSurface(); |
155 | 0 | EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures"; |
156 | 0 | } |
157 | | |