1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "base/message_loop.h"
7 #include "ppapi/c/pp_completion_callback.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/shared_impl/callback_tracker.h"
10 #include "ppapi/shared_impl/resource.h"
11 #include "ppapi/shared_impl/resource_tracker.h"
12 #include "ppapi/shared_impl/test_globals.h"
13 #include "ppapi/shared_impl/tracked_callback.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 class TrackedCallbackTest
: public testing::Test
{
23 : message_loop_(base::MessageLoop::TYPE_DEFAULT
), pp_instance_(1234) {}
25 PP_Instance
pp_instance() const { return pp_instance_
; }
27 virtual void SetUp() OVERRIDE
{
28 globals_
.GetResourceTracker()->DidCreateInstance(pp_instance_
);
30 virtual void TearDown() OVERRIDE
{
31 globals_
.GetResourceTracker()->DidDeleteInstance(pp_instance_
);
35 base::MessageLoop message_loop_
;
37 PP_Instance pp_instance_
;
40 struct CallbackRunInfo
{
41 // All valid results (PP_OK, PP_ERROR_...) are nonpositive.
42 CallbackRunInfo() : run_count(0), result(1) {}
47 void TestCallback(void* user_data
, int32_t result
) {
48 CallbackRunInfo
* info
= reinterpret_cast<CallbackRunInfo
*>(user_data
);
50 if (info
->run_count
== 1)
51 info
->result
= result
;
56 // CallbackShutdownTest --------------------------------------------------------
60 class CallbackShutdownTest
: public TrackedCallbackTest
{
62 CallbackShutdownTest() {}
65 // (1) A callback which is run (so shouldn't be aborted on shutdown).
66 // (2) A callback which is aborted (so shouldn't be aborted on shutdown).
67 // (3) A callback which isn't run (so should be aborted on shutdown).
68 CallbackRunInfo
& info_did_run() { return info_did_run_
; } // (1)
69 CallbackRunInfo
& info_did_abort() { return info_did_abort_
; } // (2)
70 CallbackRunInfo
& info_didnt_run() { return info_didnt_run_
; } // (3)
73 CallbackRunInfo info_did_run_
;
74 CallbackRunInfo info_did_abort_
;
75 CallbackRunInfo info_didnt_run_
;
80 // Tests that callbacks are properly aborted on module shutdown.
81 TEST_F(CallbackShutdownTest
, AbortOnShutdown
) {
82 scoped_refptr
<Resource
> resource(new Resource(OBJECT_IS_IMPL
, pp_instance()));
84 // Set up case (1) (see above).
85 EXPECT_EQ(0U, info_did_run().run_count
);
86 scoped_refptr
<TrackedCallback
> callback_did_run
= new TrackedCallback(
88 PP_MakeCompletionCallback(&TestCallback
, &info_did_run()));
89 EXPECT_EQ(0U, info_did_run().run_count
);
90 callback_did_run
->Run(PP_OK
);
91 EXPECT_EQ(1U, info_did_run().run_count
);
92 EXPECT_EQ(PP_OK
, info_did_run().result
);
95 EXPECT_EQ(0U, info_did_abort().run_count
);
96 scoped_refptr
<TrackedCallback
> callback_did_abort
= new TrackedCallback(
98 PP_MakeCompletionCallback(&TestCallback
, &info_did_abort()));
99 EXPECT_EQ(0U, info_did_abort().run_count
);
100 callback_did_abort
->Abort();
101 EXPECT_EQ(1U, info_did_abort().run_count
);
102 EXPECT_EQ(PP_ERROR_ABORTED
, info_did_abort().result
);
105 EXPECT_EQ(0U, info_didnt_run().run_count
);
106 scoped_refptr
<TrackedCallback
> callback_didnt_run
= new TrackedCallback(
108 PP_MakeCompletionCallback(&TestCallback
, &info_didnt_run()));
109 EXPECT_EQ(0U, info_didnt_run().run_count
);
111 PpapiGlobals::Get()->GetCallbackTrackerForInstance(pp_instance())->AbortAll();
114 EXPECT_EQ(1U, info_did_run().run_count
);
117 EXPECT_EQ(1U, info_did_abort().run_count
);
120 EXPECT_EQ(1U, info_didnt_run().run_count
);
121 EXPECT_EQ(PP_ERROR_ABORTED
, info_didnt_run().result
);
124 // CallbackResourceTest --------------------------------------------------------
128 class CallbackResourceTest
: public TrackedCallbackTest
{
130 CallbackResourceTest() {}
133 class CallbackMockResource
: public Resource
{
135 CallbackMockResource(PP_Instance instance
)
136 : Resource(OBJECT_IS_IMPL
, instance
) {}
137 ~CallbackMockResource() {}
139 PP_Resource
SetupForTest() {
140 PP_Resource resource_id
= GetReference();
141 EXPECT_NE(0, resource_id
);
143 callback_did_run_
= new TrackedCallback(
145 PP_MakeCompletionCallback(&TestCallback
, &info_did_run_
));
146 EXPECT_EQ(0U, info_did_run_
.run_count
);
148 callback_did_abort_
= new TrackedCallback(
150 PP_MakeCompletionCallback(&TestCallback
, &info_did_abort_
));
151 EXPECT_EQ(0U, info_did_abort_
.run_count
);
153 callback_didnt_run_
= new TrackedCallback(
155 PP_MakeCompletionCallback(&TestCallback
, &info_didnt_run_
));
156 EXPECT_EQ(0U, info_didnt_run_
.run_count
);
158 callback_did_run_
->Run(PP_OK
);
159 callback_did_abort_
->Abort();
161 CheckIntermediateState();
166 void CheckIntermediateState() {
167 EXPECT_EQ(1U, info_did_run_
.run_count
);
168 EXPECT_EQ(PP_OK
, info_did_run_
.result
);
170 EXPECT_EQ(1U, info_did_abort_
.run_count
);
171 EXPECT_EQ(PP_ERROR_ABORTED
, info_did_abort_
.result
);
173 EXPECT_EQ(0U, info_didnt_run_
.run_count
);
176 void CheckFinalState() {
177 EXPECT_EQ(1U, info_did_run_
.run_count
);
178 EXPECT_EQ(PP_OK
, info_did_run_
.result
);
179 EXPECT_EQ(1U, info_did_abort_
.run_count
);
180 EXPECT_EQ(PP_ERROR_ABORTED
, info_did_abort_
.result
);
181 EXPECT_EQ(1U, info_didnt_run_
.run_count
);
182 EXPECT_EQ(PP_ERROR_ABORTED
, info_didnt_run_
.result
);
185 scoped_refptr
<TrackedCallback
> callback_did_run_
;
186 CallbackRunInfo info_did_run_
;
188 scoped_refptr
<TrackedCallback
> callback_did_abort_
;
189 CallbackRunInfo info_did_abort_
;
191 scoped_refptr
<TrackedCallback
> callback_didnt_run_
;
192 CallbackRunInfo info_didnt_run_
;
197 // Test that callbacks get aborted on the last resource unref.
198 TEST_F(CallbackResourceTest
, AbortOnNoRef
) {
199 ResourceTracker
* resource_tracker
=
200 PpapiGlobals::Get()->GetResourceTracker();
202 // Test several things: Unref-ing a resource (to zero refs) with callbacks
203 // which (1) have been run, (2) have been aborted, (3) haven't been completed.
204 // Check that the uncompleted one gets aborted, and that the others don't get
206 scoped_refptr
<CallbackMockResource
> resource_1(
207 new CallbackMockResource(pp_instance()));
208 PP_Resource resource_1_id
= resource_1
->SetupForTest();
210 // Also do the same for a second resource, and make sure that unref-ing the
211 // first resource doesn't much up the second resource.
212 scoped_refptr
<CallbackMockResource
> resource_2(
213 new CallbackMockResource(pp_instance()));
214 PP_Resource resource_2_id
= resource_2
->SetupForTest();
216 // Double-check that resource #1 is still okay.
217 resource_1
->CheckIntermediateState();
219 // Kill resource #1, spin the message loop to run posted calls, and check that
220 // things are in the expected states.
221 resource_tracker
->ReleaseResource(resource_1_id
);
222 base::MessageLoop::current()->RunUntilIdle();
223 resource_1
->CheckFinalState();
224 resource_2
->CheckIntermediateState();
227 resource_tracker
->ReleaseResource(resource_2_id
);
228 base::MessageLoop::current()->RunUntilIdle();
229 resource_1
->CheckFinalState();
230 resource_2
->CheckFinalState();
232 // This shouldn't be needed, but make sure there are no stranded tasks.
233 base::MessageLoop::current()->RunUntilIdle();
236 // Test that "resurrecting" a resource (getting a new ID for a |Resource|)
237 // doesn't resurrect callbacks.
238 TEST_F(CallbackResourceTest
, Resurrection
) {
239 ResourceTracker
* resource_tracker
=
240 PpapiGlobals::Get()->GetResourceTracker();
242 scoped_refptr
<CallbackMockResource
> resource(
243 new CallbackMockResource(pp_instance()));
244 PP_Resource resource_id
= resource
->SetupForTest();
246 // Unref it, spin the message loop to run posted calls, and check that things
247 // are in the expected states.
248 resource_tracker
->ReleaseResource(resource_id
);
249 base::MessageLoop::current()->RunUntilIdle();
250 resource
->CheckFinalState();
252 // "Resurrect" it and check that the callbacks are still dead.
253 PP_Resource new_resource_id
= resource
->GetReference();
254 base::MessageLoop::current()->RunUntilIdle();
255 resource
->CheckFinalState();
257 // Unref it again and do the same.
258 resource_tracker
->ReleaseResource(new_resource_id
);
259 base::MessageLoop::current()->RunUntilIdle();
260 resource
->CheckFinalState();
262 // This shouldn't be needed, but make sure there are no stranded tasks.
263 base::MessageLoop::current()->RunUntilIdle();