OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | |
9 #include "base/threading/simple_thread.h" | |
8 #include "ppapi/c/pp_completion_callback.h" | 10 #include "ppapi/c/pp_completion_callback.h" |
9 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
12 #include "ppapi/proxy/ppapi_proxy_test.h" | |
13 #include "ppapi/proxy/ppb_message_loop_proxy.h" | |
10 #include "ppapi/shared_impl/callback_tracker.h" | 14 #include "ppapi/shared_impl/callback_tracker.h" |
11 #include "ppapi/shared_impl/proxy_lock.h" | 15 #include "ppapi/shared_impl/proxy_lock.h" |
12 #include "ppapi/shared_impl/resource.h" | 16 #include "ppapi/shared_impl/resource.h" |
13 #include "ppapi/shared_impl/resource_tracker.h" | 17 #include "ppapi/shared_impl/resource_tracker.h" |
18 #include "ppapi/shared_impl/scoped_pp_resource.h" | |
14 #include "ppapi/shared_impl/test_globals.h" | 19 #include "ppapi/shared_impl/test_globals.h" |
15 #include "ppapi/shared_impl/tracked_callback.h" | 20 #include "ppapi/shared_impl/tracked_callback.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
17 | 22 |
23 // Note, this file tests TrackedCallback which lives in ppapi/shared_impl. | |
24 // Unfortunately, we need the test to live in ppapi/proxy so that it can use | |
25 // the thread support there. | |
18 namespace ppapi { | 26 namespace ppapi { |
27 namespace proxy { | |
19 | 28 |
20 namespace { | 29 namespace { |
21 | 30 |
22 class TrackedCallbackTest : public testing::Test { | 31 class CallbackThread : public base::SimpleThread { |
23 public: | 32 public: |
24 TrackedCallbackTest() : pp_instance_(1234) {} | 33 CallbackThread(PP_Instance instance) |
bbudge
2015/03/05 01:29:35
nit: explicit
dmichael (off chromium)
2015/03/18 15:51:48
Done.
| |
34 : SimpleThread("CallbackThread"), instance_(instance) {} | |
35 ~CallbackThread() override { | |
36 ProxyAutoLock acquire; | |
37 loop_ = 0; | |
bbudge
2015/03/05 01:29:35
Could we use nullptr here?
dmichael (off chromium)
2015/03/18 15:51:48
That's better, not sure what I was thinking.
| |
38 }; | |
25 | 39 |
26 PP_Instance pp_instance() const { return pp_instance_; } | 40 // base::SimpleThread overrides. |
27 | 41 void Start() override { |
28 virtual void SetUp() override { | 42 { |
29 ProxyAutoLock lock; | 43 ProxyAutoLock acquire; |
30 globals_.GetResourceTracker()->DidCreateInstance(pp_instance_); | 44 // Create the loop here, after PpapiGlobals has been created. |
45 loop_ = new MessageLoopResource(instance_); | |
46 } | |
47 base::SimpleThread::Start(); | |
31 } | 48 } |
32 virtual void TearDown() override { | 49 void Run() override { |
33 ProxyAutoLock lock; | 50 ProxyAutoLock acquire; |
34 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); | 51 loop_->AttachToCurrentThread(); |
52 // Note, run releases the lock to run events. | |
53 loop_->Run(); | |
35 } | 54 } |
36 | 55 |
56 MessageLoopResource* loop() { return loop_.get(); } | |
bbudge
2015/03/05 01:29:35
nit: s/loop/message_loop
It confused me first time
dmichael (off chromium)
2015/03/18 15:51:48
Done.
| |
57 | |
37 private: | 58 private: |
38 base::MessageLoop message_loop_; | 59 PP_Instance instance_; |
39 TestGlobals globals_; | 60 scoped_refptr<MessageLoopResource> loop_; |
40 PP_Instance pp_instance_; | 61 }; |
62 | |
63 class TrackedCallbackTest : public PluginProxyTest { | |
64 public: | |
65 TrackedCallbackTest() : thread_(pp_instance()) {} | |
66 CallbackThread& thread() { return thread_; } | |
67 | |
68 private: | |
69 // PluginProxyTest overrides. | |
70 void SetUp() override { | |
71 PluginProxyTest::SetUp(); | |
72 thread_.Start(); | |
73 } | |
74 void TearDown() override { | |
75 { | |
76 ProxyAutoLock acquire; | |
77 thread_.loop()->PostQuit(PP_TRUE); | |
78 } | |
79 thread_.Join(); | |
bbudge
2015/03/05 01:29:35
Would it be cleaner to pull the above code into a
dmichael (off chromium)
2015/03/18 15:51:48
How about I override Join()? That seems like a nic
| |
80 PluginProxyTest::TearDown(); | |
81 } | |
82 CallbackThread thread_; | |
41 }; | 83 }; |
42 | 84 |
43 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. | 85 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. |
44 const int32_t kInitializedResultValue = 1; | 86 const int32_t kInitializedResultValue = 1; |
45 const int32_t kOverrideResultValue = 2; | 87 const int32_t kOverrideResultValue = 2; |
46 | 88 |
47 struct CallbackRunInfo { | 89 struct CallbackRunInfo { |
48 CallbackRunInfo() | 90 explicit CallbackRunInfo(base::ThreadChecker* thread_checker) |
bbudge
2015/03/05 01:29:35
I don't see any calls of this. It looks like you h
dmichael (off chromium)
2015/03/18 15:51:48
See the initializer lists for CallbackShutdownTest
bbudge
2015/03/19 00:21:10
Missed those, you're right.
| |
49 : run_count(0), | 91 : run_count_(0), |
50 result(kInitializedResultValue), | 92 result_(kInitializedResultValue), |
51 completion_task_run_count(0), | 93 completion_task_run_count_(0), |
52 completion_task_result(kInitializedResultValue) {} | 94 completion_task_result_(kInitializedResultValue), |
53 unsigned run_count; | 95 thread_checker_(thread_checker), |
54 int32_t result; | 96 callback_did_run_event_(true, false) {} |
55 unsigned completion_task_run_count; | 97 void CallbackDidRun(int32_t result) { |
56 int32_t completion_task_result; | 98 CHECK(thread_checker_->CalledOnValidThread()); |
99 if (!run_count_) | |
100 result_ = result; | |
101 ++run_count_; | |
102 callback_did_run_event_.Signal(); | |
103 } | |
104 void CompletionTaskDidRun(int32_t result) { | |
105 CHECK(thread_checker_->CalledOnValidThread()); | |
106 if (!completion_task_run_count_) | |
107 completion_task_result_ = result; | |
108 ++completion_task_run_count_; | |
109 } | |
110 void WaitUntilCompleted() { callback_did_run_event_.Wait(); } | |
111 const unsigned run_count() { return run_count_; } | |
112 int32_t result() { return result_; } | |
113 unsigned completion_task_run_count() { return completion_task_run_count_; } | |
114 int32_t completion_task_result() { return completion_task_result_; } | |
115 private: | |
116 unsigned run_count_; | |
117 int32_t result_; | |
118 unsigned completion_task_run_count_; | |
119 int32_t completion_task_result_; | |
120 // Weak; owned by the creator of CallbackRunInfo. | |
121 base::ThreadChecker* thread_checker_; | |
122 | |
123 base::WaitableEvent callback_did_run_event_; | |
57 }; | 124 }; |
58 | 125 |
59 void TestCallback(void* user_data, int32_t result) { | 126 void TestCallback(void* user_data, int32_t result) { |
60 CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data); | 127 CallbackRunInfo* info = static_cast<CallbackRunInfo*>(user_data); |
61 info->run_count++; | 128 info->CallbackDidRun(result); |
62 if (info->run_count == 1) | |
63 info->result = result; | |
64 } | 129 } |
65 | 130 |
66 } // namespace | |
67 | |
68 // CallbackShutdownTest -------------------------------------------------------- | 131 // CallbackShutdownTest -------------------------------------------------------- |
69 | 132 |
70 namespace { | |
71 | |
72 class CallbackShutdownTest : public TrackedCallbackTest { | 133 class CallbackShutdownTest : public TrackedCallbackTest { |
73 public: | 134 public: |
74 CallbackShutdownTest() {} | 135 CallbackShutdownTest() : info_did_run_(&thread_checker_), |
136 info_did_abort_(&thread_checker_), | |
137 info_didnt_run_(&thread_checker_) {} | |
75 | 138 |
76 // Cases: | 139 // Cases: |
77 // (1) A callback which is run (so shouldn't be aborted on shutdown). | 140 // (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). | 141 // (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). | 142 // (3) A callback which isn't run (so should be aborted on shutdown). |
80 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) | 143 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) |
81 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) | 144 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) |
82 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) | 145 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) |
83 | 146 |
84 private: | 147 private: |
148 base::ThreadChecker thread_checker_; | |
85 CallbackRunInfo info_did_run_; | 149 CallbackRunInfo info_did_run_; |
86 CallbackRunInfo info_did_abort_; | 150 CallbackRunInfo info_did_abort_; |
87 CallbackRunInfo info_didnt_run_; | 151 CallbackRunInfo info_didnt_run_; |
88 }; | 152 }; |
89 | 153 |
90 } // namespace | 154 } // namespace |
91 | 155 |
92 // Tests that callbacks are properly aborted on module shutdown. | 156 // Tests that callbacks are properly aborted on module shutdown. |
93 TEST_F(CallbackShutdownTest, AbortOnShutdown) { | 157 TEST_F(CallbackShutdownTest, AbortOnShutdown) { |
94 ProxyAutoLock lock; | 158 ProxyAutoLock lock; |
95 scoped_refptr<Resource> resource(new Resource(OBJECT_IS_IMPL, pp_instance())); | 159 scoped_refptr<Resource> resource( |
160 new Resource(OBJECT_IS_PROXY, pp_instance())); | |
96 | 161 |
97 // Set up case (1) (see above). | 162 // Set up case (1) (see above). |
98 EXPECT_EQ(0U, info_did_run().run_count); | 163 EXPECT_EQ(0U, info_did_run().run_count()); |
164 // TODO(dmichael): Test this on a background thread? | |
99 scoped_refptr<TrackedCallback> callback_did_run = new TrackedCallback( | 165 scoped_refptr<TrackedCallback> callback_did_run = new TrackedCallback( |
100 resource.get(), | 166 resource.get(), |
101 PP_MakeCompletionCallback(&TestCallback, &info_did_run())); | 167 PP_MakeCompletionCallback(&TestCallback, &info_did_run())); |
102 EXPECT_EQ(0U, info_did_run().run_count); | 168 EXPECT_EQ(0U, info_did_run().run_count()); |
103 callback_did_run->Run(PP_OK); | 169 callback_did_run->Run(PP_OK); |
104 EXPECT_EQ(1U, info_did_run().run_count); | 170 EXPECT_EQ(1U, info_did_run().run_count()); |
105 EXPECT_EQ(PP_OK, info_did_run().result); | 171 EXPECT_EQ(PP_OK, info_did_run().result()); |
106 | 172 |
107 // Set up case (2). | 173 // Set up case (2). |
108 EXPECT_EQ(0U, info_did_abort().run_count); | 174 EXPECT_EQ(0U, info_did_abort().run_count()); |
109 scoped_refptr<TrackedCallback> callback_did_abort = new TrackedCallback( | 175 scoped_refptr<TrackedCallback> callback_did_abort = new TrackedCallback( |
110 resource.get(), | 176 resource.get(), |
111 PP_MakeCompletionCallback(&TestCallback, &info_did_abort())); | 177 PP_MakeCompletionCallback(&TestCallback, &info_did_abort())); |
112 EXPECT_EQ(0U, info_did_abort().run_count); | 178 EXPECT_EQ(0U, info_did_abort().run_count()); |
113 callback_did_abort->Abort(); | 179 callback_did_abort->Abort(); |
114 EXPECT_EQ(1U, info_did_abort().run_count); | 180 EXPECT_EQ(1U, info_did_abort().run_count()); |
115 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort().result); | 181 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort().result()); |
116 | 182 |
117 // Set up case (3). | 183 // Set up case (3). |
118 EXPECT_EQ(0U, info_didnt_run().run_count); | 184 EXPECT_EQ(0U, info_didnt_run().run_count()); |
119 scoped_refptr<TrackedCallback> callback_didnt_run = new TrackedCallback( | 185 scoped_refptr<TrackedCallback> callback_didnt_run = new TrackedCallback( |
120 resource.get(), | 186 resource.get(), |
121 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run())); | 187 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run())); |
122 EXPECT_EQ(0U, info_didnt_run().run_count); | 188 EXPECT_EQ(0U, info_didnt_run().run_count()); |
123 | 189 |
124 PpapiGlobals::Get()->GetCallbackTrackerForInstance(pp_instance())->AbortAll(); | 190 GetGlobals()->GetCallbackTrackerForInstance(pp_instance())->AbortAll(); |
125 | 191 |
126 // Check case (1). | 192 // Check case (1). |
127 EXPECT_EQ(1U, info_did_run().run_count); | 193 EXPECT_EQ(1U, info_did_run().run_count()); |
128 | 194 |
129 // Check case (2). | 195 // Check case (2). |
130 EXPECT_EQ(1U, info_did_abort().run_count); | 196 EXPECT_EQ(1U, info_did_abort().run_count()); |
131 | 197 |
132 // Check case (3). | 198 // Check case (3). |
133 EXPECT_EQ(1U, info_didnt_run().run_count); | 199 EXPECT_EQ(1U, info_didnt_run().run_count()); |
134 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result); | 200 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result()); |
135 } | 201 } |
136 | 202 |
137 // CallbackResourceTest -------------------------------------------------------- | 203 // CallbackResourceTest -------------------------------------------------------- |
138 | 204 |
139 namespace { | 205 namespace { |
140 | 206 |
141 class CallbackResourceTest : public TrackedCallbackTest { | 207 class CallbackResourceTest : public TrackedCallbackTest { |
142 public: | 208 public: |
143 CallbackResourceTest() {} | 209 CallbackResourceTest() {} |
144 }; | 210 }; |
145 | 211 |
146 class CallbackMockResource : public Resource { | 212 class CallbackMockResource : public Resource { |
147 public: | 213 public: |
148 CallbackMockResource(PP_Instance instance) | 214 static scoped_refptr<CallbackMockResource> Create(PP_Instance instance) { |
149 : Resource(OBJECT_IS_IMPL, instance) {} | 215 ProxyAutoLock acquire; |
216 return scoped_refptr<CallbackMockResource>( | |
217 new CallbackMockResource(instance)); | |
218 } | |
150 ~CallbackMockResource() {} | 219 ~CallbackMockResource() {} |
151 | 220 |
152 PP_Resource SetupForTest() { | 221 // Take a reference to this resource, which will add it to the tracker. |
153 PP_Resource resource_id = GetReference(); | 222 void TakeRef() { |
154 EXPECT_NE(0, resource_id); | 223 ProxyAutoLock acquire; |
224 ScopedPPResource temp_resource(ScopedPPResource::PassRef(), GetReference()); | |
225 EXPECT_NE(0, temp_resource.get()); | |
226 reference_holder_ = temp_resource; | |
227 } | |
228 // Release it, removing it from the tracker. | |
229 void ReleaseRef() { | |
230 ProxyAutoLock acquire; | |
231 reference_holder_ = 0; | |
232 } | |
233 | |
234 // Create the test callbacks on a background thread, so that we can verify | |
235 // they are run on the same thread where they were created. | |
236 void CreateCallbacksOnLoop(MessageLoopResource* loop_resource) { | |
237 ProxyAutoLock acquire; | |
238 // |thread_checker_| will bind to the background thread. | |
239 thread_checker_.DetachFromThread(); | |
240 loop_resource->message_loop_proxy()->PostTask(FROM_HERE, | |
241 RunWhileLocked( | |
242 base::Bind(&CallbackMockResource::CreateCallbacks, this))); | |
243 } | |
244 | |
245 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { | |
246 // The completion task must run on the thread where the callback was | |
247 // created, and must hold the proxy lock. | |
248 CHECK(thread_checker_.CalledOnValidThread()); | |
249 ProxyLock::AssertAcquired(); | |
250 | |
251 // We should run before the callback. | |
252 CHECK_EQ(0U, info->run_count()); | |
253 info->CompletionTaskDidRun(result); | |
254 return kOverrideResultValue; | |
255 } | |
256 | |
257 void CheckInitialState() { | |
258 callbacks_created_event_.Wait(); | |
259 EXPECT_EQ(0U, info_did_run_.run_count()); | |
260 EXPECT_EQ(0U, info_did_run_.completion_task_run_count()); | |
261 | |
262 EXPECT_EQ(0U, info_did_run_with_completion_task_.run_count()); | |
263 EXPECT_EQ(0U, | |
264 info_did_run_with_completion_task_.completion_task_run_count()); | |
265 | |
266 EXPECT_EQ(0U, info_did_abort_.run_count()); | |
267 EXPECT_EQ(0U, info_did_abort_.completion_task_run_count()); | |
268 | |
269 EXPECT_EQ(0U, info_didnt_run_.run_count()); | |
270 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count()); | |
271 } | |
272 | |
273 void RunCallbacks() { | |
274 callback_did_run_->Run(PP_OK); | |
275 callback_did_run_with_completion_task_->Run(PP_OK); | |
276 callback_did_abort_->Abort(); | |
277 info_did_run_.WaitUntilCompleted(); | |
278 info_did_run_with_completion_task_.WaitUntilCompleted(); | |
279 info_did_abort_.WaitUntilCompleted(); | |
280 } | |
281 | |
282 void CheckIntermediateState() { | |
283 EXPECT_EQ(1U, info_did_run_.run_count()); | |
284 EXPECT_EQ(PP_OK, info_did_run_.result()); | |
285 EXPECT_EQ(0U, info_did_run_.completion_task_run_count()); | |
286 | |
287 EXPECT_EQ(1U, info_did_run_with_completion_task_.run_count()); | |
288 // completion task should override the result. | |
289 EXPECT_EQ(kOverrideResultValue, | |
290 info_did_run_with_completion_task_.result()); | |
291 EXPECT_EQ(1U, | |
292 info_did_run_with_completion_task_.completion_task_run_count()); | |
293 EXPECT_EQ(PP_OK, | |
294 info_did_run_with_completion_task_.completion_task_result()); | |
295 | |
296 EXPECT_EQ(1U, info_did_abort_.run_count()); | |
297 // completion task shouldn't override an abort. | |
298 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result()); | |
299 EXPECT_EQ(1U, info_did_abort_.completion_task_run_count()); | |
300 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.completion_task_result()); | |
301 | |
302 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count()); | |
303 EXPECT_EQ(0U, info_didnt_run_.run_count()); | |
304 } | |
305 | |
306 void CheckFinalState() { | |
307 info_didnt_run_.WaitUntilCompleted(); | |
308 EXPECT_EQ(1U, info_did_run_.run_count()); | |
309 EXPECT_EQ(PP_OK, info_did_run_.result()); | |
310 EXPECT_EQ(1U, info_did_abort_.run_count()); | |
311 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result()); | |
312 EXPECT_EQ(1U, info_didnt_run_.run_count()); | |
313 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result()); | |
314 } | |
315 | |
316 private: | |
317 explicit CallbackMockResource(PP_Instance instance) | |
318 : Resource(OBJECT_IS_IMPL, instance), | |
319 info_did_run_(&thread_checker_), | |
320 info_did_run_with_completion_task_(&thread_checker_), | |
321 info_did_abort_(&thread_checker_), | |
322 info_didnt_run_(&thread_checker_), | |
323 callbacks_created_event_(true, false) {} | |
324 void CreateCallbacks() { | |
325 // Bind thread_checker_ to the thread where we create the callbacks. | |
326 // Later, when the callback runs, it will check that it was invoked on this | |
327 // same thread. | |
328 CHECK(thread_checker_.CalledOnValidThread()); | |
155 | 329 |
156 callback_did_run_ = new TrackedCallback( | 330 callback_did_run_ = new TrackedCallback( |
157 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); | 331 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); | |
160 | 332 |
161 // In order to test that the completion task can override the callback | 333 // 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. | 334 // result, we need to test callbacks with and without a completion task. |
163 callback_did_run_with_completion_task_ = new TrackedCallback( | 335 callback_did_run_with_completion_task_ = new TrackedCallback( |
164 this, | 336 this, |
165 PP_MakeCompletionCallback(&TestCallback, | 337 PP_MakeCompletionCallback(&TestCallback, |
166 &info_did_run_with_completion_task_)); | 338 &info_did_run_with_completion_task_)); |
167 callback_did_run_with_completion_task_->set_completion_task( | 339 callback_did_run_with_completion_task_->set_completion_task( |
168 Bind(&CallbackMockResource::CompletionTask, | 340 Bind(&CallbackMockResource::CompletionTask, |
169 this, | 341 this, |
170 &info_did_run_with_completion_task_)); | 342 &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); | |
173 | 343 |
174 callback_did_abort_ = new TrackedCallback( | 344 callback_did_abort_ = new TrackedCallback( |
175 this, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); | 345 this, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); |
176 callback_did_abort_->set_completion_task( | 346 callback_did_abort_->set_completion_task( |
177 Bind(&CallbackMockResource::CompletionTask, this, &info_did_abort_)); | 347 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); | |
180 | 348 |
181 callback_didnt_run_ = new TrackedCallback( | 349 callback_didnt_run_ = new TrackedCallback( |
182 this, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); | 350 this, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); |
183 callback_didnt_run_->set_completion_task( | 351 callback_didnt_run_->set_completion_task( |
184 Bind(&CallbackMockResource::CompletionTask, this, &info_didnt_run_)); | 352 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); | |
187 | 353 |
188 callback_did_run_->Run(PP_OK); | 354 callbacks_created_event_.Signal(); |
189 callback_did_run_with_completion_task_->Run(PP_OK); | |
190 callback_did_abort_->Abort(); | |
191 | |
192 CheckIntermediateState(); | |
193 | |
194 return resource_id; | |
195 } | 355 } |
196 | 356 |
197 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { | 357 // Used to verify that the callback runs on the same thread where it is |
198 // We should run before the callback. | 358 // created. |
199 EXPECT_EQ(0U, info->run_count); | 359 base::ThreadChecker thread_checker_; |
200 info->completion_task_run_count++; | |
201 if (info->completion_task_run_count == 1) | |
202 info->completion_task_result = result; | |
203 return kOverrideResultValue; | |
204 } | |
205 | |
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); | |
210 | |
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); | |
216 | |
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); | |
222 | |
223 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count); | |
224 EXPECT_EQ(0U, info_didnt_run_.run_count); | |
225 } | |
226 | |
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); | |
234 } | |
235 | 360 |
236 scoped_refptr<TrackedCallback> callback_did_run_; | 361 scoped_refptr<TrackedCallback> callback_did_run_; |
237 CallbackRunInfo info_did_run_; | 362 CallbackRunInfo info_did_run_; |
238 | 363 |
239 scoped_refptr<TrackedCallback> callback_did_run_with_completion_task_; | 364 scoped_refptr<TrackedCallback> callback_did_run_with_completion_task_; |
240 CallbackRunInfo info_did_run_with_completion_task_; | 365 CallbackRunInfo info_did_run_with_completion_task_; |
241 | 366 |
242 scoped_refptr<TrackedCallback> callback_did_abort_; | 367 scoped_refptr<TrackedCallback> callback_did_abort_; |
243 CallbackRunInfo info_did_abort_; | 368 CallbackRunInfo info_did_abort_; |
244 | 369 |
245 scoped_refptr<TrackedCallback> callback_didnt_run_; | 370 scoped_refptr<TrackedCallback> callback_didnt_run_; |
246 CallbackRunInfo info_didnt_run_; | 371 CallbackRunInfo info_didnt_run_; |
372 | |
373 base::WaitableEvent callbacks_created_event_; | |
374 | |
375 ScopedPPResource reference_holder_; | |
247 }; | 376 }; |
248 | 377 |
249 } // namespace | 378 } // namespace |
250 | 379 |
251 // Test that callbacks get aborted on the last resource unref. | 380 // Test that callbacks get aborted on the last resource unref. |
252 TEST_F(CallbackResourceTest, AbortOnNoRef) { | 381 TEST_F(CallbackResourceTest, AbortOnNoRef) { |
253 ProxyAutoLock lock; | |
254 ResourceTracker* resource_tracker = PpapiGlobals::Get()->GetResourceTracker(); | |
255 | |
256 // Test several things: Unref-ing a resource (to zero refs) with callbacks | 382 // 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. | 383 // 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 | 384 // Check that the uncompleted one gets aborted, and that the others don't get |
259 // called again. | 385 // called again. |
260 scoped_refptr<CallbackMockResource> resource_1( | 386 scoped_refptr<CallbackMockResource> resource_1( |
261 new CallbackMockResource(pp_instance())); | 387 CallbackMockResource::Create(pp_instance())); |
262 PP_Resource resource_1_id = resource_1->SetupForTest(); | 388 resource_1->CreateCallbacksOnLoop(thread().loop()); |
389 resource_1->CheckInitialState(); | |
390 resource_1->RunCallbacks(); | |
391 resource_1->TakeRef(); | |
392 resource_1->CheckIntermediateState(); | |
263 | 393 |
264 // Also do the same for a second resource, and make sure that unref-ing the | 394 // 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. | 395 // first resource doesn't much up the second resource. |
266 scoped_refptr<CallbackMockResource> resource_2( | 396 scoped_refptr<CallbackMockResource> resource_2( |
267 new CallbackMockResource(pp_instance())); | 397 CallbackMockResource::Create(pp_instance())); |
268 PP_Resource resource_2_id = resource_2->SetupForTest(); | 398 resource_2->CreateCallbacksOnLoop(thread().loop()); |
399 resource_2->CheckInitialState(); | |
400 resource_2->RunCallbacks(); | |
401 resource_2->TakeRef(); | |
402 resource_2->CheckIntermediateState(); | |
269 | 403 |
270 // Double-check that resource #1 is still okay. | 404 // Double-check that resource #1 is still okay. |
271 resource_1->CheckIntermediateState(); | 405 resource_1->CheckIntermediateState(); |
272 | 406 |
273 // Kill resource #1, spin the message loop to run posted calls, and check that | 407 // Kill resource #1, spin the message loop to run posted calls, and check that |
274 // things are in the expected states. | 408 // things are in the expected states. |
275 resource_tracker->ReleaseResource(resource_1_id); | 409 resource_1->ReleaseRef(); |
276 { | 410 |
277 ProxyAutoUnlock unlock; | |
278 base::MessageLoop::current()->RunUntilIdle(); | |
279 } | |
280 resource_1->CheckFinalState(); | 411 resource_1->CheckFinalState(); |
281 resource_2->CheckIntermediateState(); | 412 resource_2->CheckIntermediateState(); |
282 | 413 |
283 // Kill resource #2. | 414 // Kill resource #2. |
284 resource_tracker->ReleaseResource(resource_2_id); | 415 resource_2->ReleaseRef(); |
285 { | 416 |
286 ProxyAutoUnlock unlock; | |
287 base::MessageLoop::current()->RunUntilIdle(); | |
288 } | |
289 resource_1->CheckFinalState(); | 417 resource_1->CheckFinalState(); |
290 resource_2->CheckFinalState(); | 418 resource_2->CheckFinalState(); |
291 | |
292 // This shouldn't be needed, but make sure there are no stranded tasks. | |
293 { | |
294 ProxyAutoUnlock unlock; | |
295 base::MessageLoop::current()->RunUntilIdle(); | |
296 } | |
297 } | 419 } |
298 | 420 |
299 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) | 421 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) |
300 // doesn't resurrect callbacks. | 422 // doesn't resurrect callbacks. |
301 TEST_F(CallbackResourceTest, Resurrection) { | 423 TEST_F(CallbackResourceTest, Resurrection) { |
302 ProxyAutoLock lock; | 424 scoped_refptr<CallbackMockResource> resource( |
303 ResourceTracker* resource_tracker = PpapiGlobals::Get()->GetResourceTracker(); | 425 CallbackMockResource::Create(pp_instance())); |
426 resource->CreateCallbacksOnLoop(thread().loop()); | |
427 resource->CheckInitialState(); | |
428 resource->RunCallbacks(); | |
429 resource->TakeRef(); | |
430 resource->CheckIntermediateState(); | |
304 | 431 |
305 scoped_refptr<CallbackMockResource> resource( | 432 // Unref it and check that things are in the expected states. |
306 new CallbackMockResource(pp_instance())); | 433 resource->ReleaseRef(); |
307 PP_Resource resource_id = resource->SetupForTest(); | |
308 | |
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); | |
312 { | |
313 ProxyAutoUnlock unlock; | |
314 base::MessageLoop::current()->RunUntilIdle(); | |
315 } | |
316 resource->CheckFinalState(); | 434 resource->CheckFinalState(); |
317 | 435 |
318 // "Resurrect" it and check that the callbacks are still dead. | 436 // "Resurrect" it and check that the callbacks are still dead. |
319 PP_Resource new_resource_id = resource->GetReference(); | 437 resource->TakeRef(); |
320 { | |
321 ProxyAutoUnlock unlock; | |
322 base::MessageLoop::current()->RunUntilIdle(); | |
323 } | |
324 resource->CheckFinalState(); | 438 resource->CheckFinalState(); |
325 | 439 |
326 // Unref it again and do the same. | 440 // Unref it again and do the same. |
327 resource_tracker->ReleaseResource(new_resource_id); | 441 resource->ReleaseRef(); |
328 { | |
329 ProxyAutoUnlock unlock; | |
330 base::MessageLoop::current()->RunUntilIdle(); | |
331 } | |
332 resource->CheckFinalState(); | 442 resource->CheckFinalState(); |
333 | |
334 // This shouldn't be needed, but make sure there are no stranded tasks. | |
335 { | |
336 ProxyAutoUnlock unlock; | |
337 base::MessageLoop::current()->RunUntilIdle(); | |
338 } | |
339 } | 443 } |
340 | 444 |
445 } // namespace proxy | |
341 } // namespace ppapi | 446 } // namespace ppapi |
OLD | NEW |