1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/callback_tracker.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/test_globals.h"
#include "ppapi/shared_impl/tracked_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ppapi {
namespace {
class TrackedCallbackTest : public testing::Test {
public:
TrackedCallbackTest()
: message_loop_(MessageLoop::TYPE_DEFAULT),
pp_instance_(1234) {}
PP_Instance pp_instance() const { return pp_instance_; }
virtual void SetUp() OVERRIDE {
globals_.GetResourceTracker()->DidCreateInstance(pp_instance_);
}
virtual void TearDown() OVERRIDE {
globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_);
}
private:
MessageLoop message_loop_;
TestGlobals globals_;
PP_Instance pp_instance_;
};
struct CallbackRunInfo {
// All valid results (PP_OK, PP_ERROR_...) are nonpositive.
CallbackRunInfo() : run_count(0), result(1) {}
unsigned run_count;
int32_t result;
};
void TestCallback(void* user_data, int32_t result) {
CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data);
info->run_count++;
if (info->run_count == 1)
info->result = result;
}
} // namespace
// CallbackShutdownTest --------------------------------------------------------
namespace {
class CallbackShutdownTest : public TrackedCallbackTest {
public:
CallbackShutdownTest() {}
// Cases:
// (1) A callback which is run (so shouldn't be aborted on shutdown).
// (2) A callback which is aborted (so shouldn't be aborted on shutdown).
// (3) A callback which isn't run (so should be aborted on shutdown).
CallbackRunInfo& info_did_run() { return info_did_run_; } // (1)
CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2)
CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3)
private:
CallbackRunInfo info_did_run_;
CallbackRunInfo info_did_abort_;
CallbackRunInfo info_didnt_run_;
};
} // namespace
// Tests that callbacks are properly aborted on module shutdown.
TEST_F(CallbackShutdownTest, AbortOnShutdown) {
scoped_refptr<Resource> resource(new Resource(OBJECT_IS_IMPL, pp_instance()));
// Set up case (1) (see above).
EXPECT_EQ(0U, info_did_run().run_count);
scoped_refptr<TrackedCallback> callback_did_run = new TrackedCallback(
resource.get(),
PP_MakeCompletionCallback(&TestCallback, &info_did_run()));
EXPECT_EQ(0U, info_did_run().run_count);
callback_did_run->Run(PP_OK);
EXPECT_EQ(1U, info_did_run().run_count);
EXPECT_EQ(PP_OK, info_did_run().result);
// Set up case (2).
EXPECT_EQ(0U, info_did_abort().run_count);
scoped_refptr<TrackedCallback> callback_did_abort = new TrackedCallback(
resource.get(),
PP_MakeCompletionCallback(&TestCallback, &info_did_abort()));
EXPECT_EQ(0U, info_did_abort().run_count);
callback_did_abort->Abort();
EXPECT_EQ(1U, info_did_abort().run_count);
EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort().result);
// Set up case (3).
EXPECT_EQ(0U, info_didnt_run().run_count);
scoped_refptr<TrackedCallback> callback_didnt_run = new TrackedCallback(
resource.get(),
PP_MakeCompletionCallback(&TestCallback, &info_didnt_run()));
EXPECT_EQ(0U, info_didnt_run().run_count);
PpapiGlobals::Get()->GetCallbackTrackerForInstance(pp_instance())->AbortAll();
// Check case (1).
EXPECT_EQ(1U, info_did_run().run_count);
// Check case (2).
EXPECT_EQ(1U, info_did_abort().run_count);
// Check case (3).
EXPECT_EQ(1U, info_didnt_run().run_count);
EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result);
}
// CallbackResourceTest --------------------------------------------------------
namespace {
class CallbackResourceTest : public TrackedCallbackTest {
public:
CallbackResourceTest() {}
};
class CallbackMockResource : public Resource {
public:
CallbackMockResource(PP_Instance instance)
: Resource(OBJECT_IS_IMPL, instance) {}
~CallbackMockResource() {}
PP_Resource SetupForTest() {
PP_Resource resource_id = GetReference();
EXPECT_NE(0, resource_id);
callback_did_run_ = new TrackedCallback(
this,
PP_MakeCompletionCallback(&TestCallback, &info_did_run_));
EXPECT_EQ(0U, info_did_run_.run_count);
callback_did_abort_ = new TrackedCallback(
this,
PP_MakeCompletionCallback(&TestCallback, &info_did_abort_));
EXPECT_EQ(0U, info_did_abort_.run_count);
callback_didnt_run_ = new TrackedCallback(
this,
PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_));
EXPECT_EQ(0U, info_didnt_run_.run_count);
callback_did_run_->Run(PP_OK);
callback_did_abort_->Abort();
CheckIntermediateState();
return resource_id;
}
void CheckIntermediateState() {
EXPECT_EQ(1U, info_did_run_.run_count);
EXPECT_EQ(PP_OK, info_did_run_.result);
EXPECT_EQ(1U, info_did_abort_.run_count);
EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
EXPECT_EQ(0U, info_didnt_run_.run_count);
}
void CheckFinalState() {
EXPECT_EQ(1U, info_did_run_.run_count);
EXPECT_EQ(PP_OK, info_did_run_.result);
EXPECT_EQ(1U, info_did_abort_.run_count);
EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
EXPECT_EQ(1U, info_didnt_run_.run_count);
EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result);
}
scoped_refptr<TrackedCallback> callback_did_run_;
CallbackRunInfo info_did_run_;
scoped_refptr<TrackedCallback> callback_did_abort_;
CallbackRunInfo info_did_abort_;
scoped_refptr<TrackedCallback> callback_didnt_run_;
CallbackRunInfo info_didnt_run_;
};
} // namespace
// Test that callbacks get aborted on the last resource unref.
TEST_F(CallbackResourceTest, AbortOnNoRef) {
ResourceTracker* resource_tracker =
PpapiGlobals::Get()->GetResourceTracker();
// Test several things: Unref-ing a resource (to zero refs) with callbacks
// which (1) have been run, (2) have been aborted, (3) haven't been completed.
// Check that the uncompleted one gets aborted, and that the others don't get
// called again.
scoped_refptr<CallbackMockResource> resource_1(
new CallbackMockResource(pp_instance()));
PP_Resource resource_1_id = resource_1->SetupForTest();
// Also do the same for a second resource, and make sure that unref-ing the
// first resource doesn't much up the second resource.
scoped_refptr<CallbackMockResource> resource_2(
new CallbackMockResource(pp_instance()));
PP_Resource resource_2_id = resource_2->SetupForTest();
// Double-check that resource #1 is still okay.
resource_1->CheckIntermediateState();
// Kill resource #1, spin the message loop to run posted calls, and check that
// things are in the expected states.
resource_tracker->ReleaseResource(resource_1_id);
MessageLoop::current()->RunUntilIdle();
resource_1->CheckFinalState();
resource_2->CheckIntermediateState();
// Kill resource #2.
resource_tracker->ReleaseResource(resource_2_id);
MessageLoop::current()->RunUntilIdle();
resource_1->CheckFinalState();
resource_2->CheckFinalState();
// This shouldn't be needed, but make sure there are no stranded tasks.
MessageLoop::current()->RunUntilIdle();
}
// Test that "resurrecting" a resource (getting a new ID for a |Resource|)
// doesn't resurrect callbacks.
TEST_F(CallbackResourceTest, Resurrection) {
ResourceTracker* resource_tracker =
PpapiGlobals::Get()->GetResourceTracker();
scoped_refptr<CallbackMockResource> resource(
new CallbackMockResource(pp_instance()));
PP_Resource resource_id = resource->SetupForTest();
// Unref it, spin the message loop to run posted calls, and check that things
// are in the expected states.
resource_tracker->ReleaseResource(resource_id);
MessageLoop::current()->RunUntilIdle();
resource->CheckFinalState();
// "Resurrect" it and check that the callbacks are still dead.
PP_Resource new_resource_id = resource->GetReference();
MessageLoop::current()->RunUntilIdle();
resource->CheckFinalState();
// Unref it again and do the same.
resource_tracker->ReleaseResource(new_resource_id);
MessageLoop::current()->RunUntilIdle();
resource->CheckFinalState();
// This shouldn't be needed, but make sure there are no stranded tasks.
MessageLoop::current()->RunUntilIdle();
}
} // namespace ppapi
|