Add ability for NetLogLogger to gather data from more than just NetLog
[chromium-blink-merge.git] / ppapi / shared_impl / tracked_callback_unittest.cc
blob12ad4235bdb66341e0c82a7b922e4864d38caf8b
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/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop.h"
8 #include "ppapi/c/pp_completion_callback.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/shared_impl/callback_tracker.h"
11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/resource.h"
13 #include "ppapi/shared_impl/resource_tracker.h"
14 #include "ppapi/shared_impl/test_globals.h"
15 #include "ppapi/shared_impl/tracked_callback.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace ppapi {
20 namespace {
22 class TrackedCallbackTest : public testing::Test {
23 public:
24 TrackedCallbackTest() : pp_instance_(1234) {}
26 PP_Instance pp_instance() const { return pp_instance_; }
28 virtual void SetUp() override {
29 ProxyAutoLock lock;
30 globals_.GetResourceTracker()->DidCreateInstance(pp_instance_);
32 virtual void TearDown() override {
33 ProxyAutoLock lock;
34 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_);
37 private:
38 base::MessageLoop message_loop_;
39 TestGlobals globals_;
40 PP_Instance pp_instance_;
43 // All valid results (PP_OK, PP_ERROR_...) are nonpositive.
44 const int32_t kInitializedResultValue = 1;
45 const int32_t kOverrideResultValue = 2;
47 struct CallbackRunInfo {
48 CallbackRunInfo()
49 : run_count(0),
50 result(kInitializedResultValue),
51 completion_task_run_count(0),
52 completion_task_result(kInitializedResultValue) {}
53 unsigned run_count;
54 int32_t result;
55 unsigned completion_task_run_count;
56 int32_t completion_task_result;
59 void TestCallback(void* user_data, int32_t result) {
60 CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data);
61 info->run_count++;
62 if (info->run_count == 1)
63 info->result = result;
66 } // namespace
68 // CallbackShutdownTest --------------------------------------------------------
70 namespace {
72 class CallbackShutdownTest : public TrackedCallbackTest {
73 public:
74 CallbackShutdownTest() {}
76 // Cases:
77 // (1) A callback which is run (so shouldn't be aborted on shutdown).
78 // (2) A callback which is aborted (so shouldn't be aborted on shutdown).
79 // (3) A callback which isn't run (so should be aborted on shutdown).
80 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1)
81 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2)
82 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3)
84 private:
85 CallbackRunInfo info_did_run_;
86 CallbackRunInfo info_did_abort_;
87 CallbackRunInfo info_didnt_run_;
90 } // namespace
92 // Tests that callbacks are properly aborted on module shutdown.
93 TEST_F(CallbackShutdownTest, AbortOnShutdown) {
94 ProxyAutoLock lock;
95 scoped_refptr<Resource> resource(new Resource(OBJECT_IS_IMPL, pp_instance()));
97 // Set up case (1) (see above).
98 EXPECT_EQ(0U, info_did_run().run_count);
99 scoped_refptr<TrackedCallback> callback_did_run = new TrackedCallback(
100 resource.get(),
101 PP_MakeCompletionCallback(&TestCallback, &info_did_run()));
102 EXPECT_EQ(0U, info_did_run().run_count);
103 callback_did_run->Run(PP_OK);
104 EXPECT_EQ(1U, info_did_run().run_count);
105 EXPECT_EQ(PP_OK, info_did_run().result);
107 // Set up case (2).
108 EXPECT_EQ(0U, info_did_abort().run_count);
109 scoped_refptr<TrackedCallback> callback_did_abort = new TrackedCallback(
110 resource.get(),
111 PP_MakeCompletionCallback(&TestCallback, &info_did_abort()));
112 EXPECT_EQ(0U, info_did_abort().run_count);
113 callback_did_abort->Abort();
114 EXPECT_EQ(1U, info_did_abort().run_count);
115 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort().result);
117 // Set up case (3).
118 EXPECT_EQ(0U, info_didnt_run().run_count);
119 scoped_refptr<TrackedCallback> callback_didnt_run = new TrackedCallback(
120 resource.get(),
121 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run()));
122 EXPECT_EQ(0U, info_didnt_run().run_count);
124 PpapiGlobals::Get()->GetCallbackTrackerForInstance(pp_instance())->AbortAll();
126 // Check case (1).
127 EXPECT_EQ(1U, info_did_run().run_count);
129 // Check case (2).
130 EXPECT_EQ(1U, info_did_abort().run_count);
132 // Check case (3).
133 EXPECT_EQ(1U, info_didnt_run().run_count);
134 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result);
137 // CallbackResourceTest --------------------------------------------------------
139 namespace {
141 class CallbackResourceTest : public TrackedCallbackTest {
142 public:
143 CallbackResourceTest() {}
146 class CallbackMockResource : public Resource {
147 public:
148 CallbackMockResource(PP_Instance instance)
149 : Resource(OBJECT_IS_IMPL, instance) {}
150 ~CallbackMockResource() {}
152 PP_Resource SetupForTest() {
153 PP_Resource resource_id = GetReference();
154 EXPECT_NE(0, resource_id);
156 callback_did_run_ = new TrackedCallback(
157 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_));
158 EXPECT_EQ(0U, info_did_run_.run_count);
159 EXPECT_EQ(0U, info_did_run_.completion_task_run_count);
161 // In order to test that the completion task can override the callback
162 // result, we need to test callbacks with and without a completion task.
163 callback_did_run_with_completion_task_ = new TrackedCallback(
164 this,
165 PP_MakeCompletionCallback(&TestCallback,
166 &info_did_run_with_completion_task_));
167 callback_did_run_with_completion_task_->set_completion_task(
168 Bind(&CallbackMockResource::CompletionTask,
169 this,
170 &info_did_run_with_completion_task_));
171 EXPECT_EQ(0U, info_did_run_with_completion_task_.run_count);
172 EXPECT_EQ(0U, info_did_run_with_completion_task_.completion_task_run_count);
174 callback_did_abort_ = new TrackedCallback(
175 this, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_));
176 callback_did_abort_->set_completion_task(
177 Bind(&CallbackMockResource::CompletionTask, this, &info_did_abort_));
178 EXPECT_EQ(0U, info_did_abort_.run_count);
179 EXPECT_EQ(0U, info_did_abort_.completion_task_run_count);
181 callback_didnt_run_ = new TrackedCallback(
182 this, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_));
183 callback_didnt_run_->set_completion_task(
184 Bind(&CallbackMockResource::CompletionTask, this, &info_didnt_run_));
185 EXPECT_EQ(0U, info_didnt_run_.run_count);
186 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count);
188 callback_did_run_->Run(PP_OK);
189 callback_did_run_with_completion_task_->Run(PP_OK);
190 callback_did_abort_->Abort();
192 CheckIntermediateState();
194 return resource_id;
197 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) {
198 // We should run before the callback.
199 EXPECT_EQ(0U, info->run_count);
200 info->completion_task_run_count++;
201 if (info->completion_task_run_count == 1)
202 info->completion_task_result = result;
203 return kOverrideResultValue;
206 void CheckIntermediateState() {
207 EXPECT_EQ(1U, info_did_run_.run_count);
208 EXPECT_EQ(PP_OK, info_did_run_.result);
209 EXPECT_EQ(0U, info_did_run_.completion_task_run_count);
211 EXPECT_EQ(1U, info_did_run_with_completion_task_.run_count);
212 // completion task should override the result.
213 EXPECT_EQ(kOverrideResultValue, info_did_run_with_completion_task_.result);
214 EXPECT_EQ(1U, info_did_run_with_completion_task_.completion_task_run_count);
215 EXPECT_EQ(PP_OK, info_did_run_with_completion_task_.completion_task_result);
217 EXPECT_EQ(1U, info_did_abort_.run_count);
218 // completion task shouldn't override an abort.
219 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
220 EXPECT_EQ(1U, info_did_abort_.completion_task_run_count);
221 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.completion_task_result);
223 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count);
224 EXPECT_EQ(0U, info_didnt_run_.run_count);
227 void CheckFinalState() {
228 EXPECT_EQ(1U, info_did_run_.run_count);
229 EXPECT_EQ(PP_OK, info_did_run_.result);
230 EXPECT_EQ(1U, info_did_abort_.run_count);
231 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
232 EXPECT_EQ(1U, info_didnt_run_.run_count);
233 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result);
236 scoped_refptr<TrackedCallback> callback_did_run_;
237 CallbackRunInfo info_did_run_;
239 scoped_refptr<TrackedCallback> callback_did_run_with_completion_task_;
240 CallbackRunInfo info_did_run_with_completion_task_;
242 scoped_refptr<TrackedCallback> callback_did_abort_;
243 CallbackRunInfo info_did_abort_;
245 scoped_refptr<TrackedCallback> callback_didnt_run_;
246 CallbackRunInfo info_didnt_run_;
249 } // namespace
251 // Test that callbacks get aborted on the last resource unref.
252 TEST_F(CallbackResourceTest, AbortOnNoRef) {
253 ProxyAutoLock lock;
254 ResourceTracker* resource_tracker = PpapiGlobals::Get()->GetResourceTracker();
256 // Test several things: Unref-ing a resource (to zero refs) with callbacks
257 // which (1) have been run, (2) have been aborted, (3) haven't been completed.
258 // Check that the uncompleted one gets aborted, and that the others don't get
259 // called again.
260 scoped_refptr<CallbackMockResource> resource_1(
261 new CallbackMockResource(pp_instance()));
262 PP_Resource resource_1_id = resource_1->SetupForTest();
264 // Also do the same for a second resource, and make sure that unref-ing the
265 // first resource doesn't much up the second resource.
266 scoped_refptr<CallbackMockResource> resource_2(
267 new CallbackMockResource(pp_instance()));
268 PP_Resource resource_2_id = resource_2->SetupForTest();
270 // Double-check that resource #1 is still okay.
271 resource_1->CheckIntermediateState();
273 // Kill resource #1, spin the message loop to run posted calls, and check that
274 // things are in the expected states.
275 resource_tracker->ReleaseResource(resource_1_id);
277 ProxyAutoUnlock unlock;
278 base::MessageLoop::current()->RunUntilIdle();
280 resource_1->CheckFinalState();
281 resource_2->CheckIntermediateState();
283 // Kill resource #2.
284 resource_tracker->ReleaseResource(resource_2_id);
286 ProxyAutoUnlock unlock;
287 base::MessageLoop::current()->RunUntilIdle();
289 resource_1->CheckFinalState();
290 resource_2->CheckFinalState();
292 // This shouldn't be needed, but make sure there are no stranded tasks.
294 ProxyAutoUnlock unlock;
295 base::MessageLoop::current()->RunUntilIdle();
299 // Test that "resurrecting" a resource (getting a new ID for a |Resource|)
300 // doesn't resurrect callbacks.
301 TEST_F(CallbackResourceTest, Resurrection) {
302 ProxyAutoLock lock;
303 ResourceTracker* resource_tracker = PpapiGlobals::Get()->GetResourceTracker();
305 scoped_refptr<CallbackMockResource> resource(
306 new CallbackMockResource(pp_instance()));
307 PP_Resource resource_id = resource->SetupForTest();
309 // Unref it, spin the message loop to run posted calls, and check that things
310 // are in the expected states.
311 resource_tracker->ReleaseResource(resource_id);
313 ProxyAutoUnlock unlock;
314 base::MessageLoop::current()->RunUntilIdle();
316 resource->CheckFinalState();
318 // "Resurrect" it and check that the callbacks are still dead.
319 PP_Resource new_resource_id = resource->GetReference();
321 ProxyAutoUnlock unlock;
322 base::MessageLoop::current()->RunUntilIdle();
324 resource->CheckFinalState();
326 // Unref it again and do the same.
327 resource_tracker->ReleaseResource(new_resource_id);
329 ProxyAutoUnlock unlock;
330 base::MessageLoop::current()->RunUntilIdle();
332 resource->CheckFinalState();
334 // This shouldn't be needed, but make sure there are no stranded tasks.
336 ProxyAutoUnlock unlock;
337 base::MessageLoop::current()->RunUntilIdle();
341 } // namespace ppapi