Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: webkit/plugins/ppapi/callbacks_unittest.cc

Issue 9015009: Use the new callback tracker and delete the old one (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add IsPending Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/plugins/ppapi/callbacks.cc ('k') | webkit/plugins/ppapi/file_callbacks.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "webkit/plugins/ppapi/ppapi_unittest.h"
6
7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop.h"
9 #include "ppapi/c/pp_completion_callback.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/shared_impl/resource_tracker.h"
12 #include "webkit/plugins/ppapi/callbacks.h"
13 #include "webkit/plugins/ppapi/host_globals.h"
14 #include "webkit/plugins/ppapi/mock_resource.h"
15 #include "webkit/plugins/ppapi/plugin_module.h"
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
17 #include "webkit/plugins/ppapi/resource_helper.h"
18
19 namespace webkit {
20 namespace ppapi {
21
22 struct CallbackRunInfo {
23 // All valid results (PP_OK, PP_ERROR_...) are nonpositive.
24 CallbackRunInfo() : run_count(0), result(1) {}
25 unsigned run_count;
26 int32_t result;
27 };
28
29 namespace {
30
31 void TestCallback(void* user_data, int32_t result) {
32 CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data);
33 info->run_count++;
34 if (info->run_count == 1)
35 info->result = result;
36 }
37
38 } // namespace
39
40 // CallbackShutdownTest --------------------------------------------------------
41
42 // Tests that callbacks are properly aborted on module shutdown.
43 class CallbackShutdownTest : public PpapiUnittest {
44 public:
45 CallbackShutdownTest() {}
46
47 // Cases:
48 // (1) A callback which is run (so shouldn't be aborted on shutdown).
49 // (2) A callback which is aborted (so shouldn't be aborted on shutdown).
50 // (3) A callback which isn't run (so should be aborted on shutdown).
51 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1)
52 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2)
53 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3)
54
55 private:
56 CallbackRunInfo info_did_run_;
57 CallbackRunInfo info_did_abort_;
58 CallbackRunInfo info_didnt_run_;
59 };
60
61 TEST_F(CallbackShutdownTest, AbortOnShutdown) {
62 // Set up case (1) (see above).
63 EXPECT_EQ(0U, info_did_run().run_count);
64 scoped_refptr<TrackedCompletionCallback> callback_did_run =
65 new TrackedCompletionCallback(
66 instance()->module()->GetCallbackTracker(),
67 0,
68 PP_MakeCompletionCallback(&TestCallback, &info_did_run()));
69 EXPECT_EQ(0U, info_did_run().run_count);
70 callback_did_run->Run(PP_OK);
71 EXPECT_EQ(1U, info_did_run().run_count);
72 EXPECT_EQ(PP_OK, info_did_run().result);
73
74 // Set up case (2).
75 EXPECT_EQ(0U, info_did_abort().run_count);
76 scoped_refptr<TrackedCompletionCallback> callback_did_abort =
77 new TrackedCompletionCallback(
78 instance()->module()->GetCallbackTracker(),
79 0,
80 PP_MakeCompletionCallback(&TestCallback, &info_did_abort()));
81 EXPECT_EQ(0U, info_did_abort().run_count);
82 callback_did_abort->Abort();
83 EXPECT_EQ(1U, info_did_abort().run_count);
84 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort().result);
85
86
87 // Set up case (3).
88 EXPECT_EQ(0U, info_didnt_run().run_count);
89 scoped_refptr<TrackedCompletionCallback> callback_didnt_run =
90 new TrackedCompletionCallback(
91 instance()->module()->GetCallbackTracker(),
92 0,
93 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run()));
94 EXPECT_EQ(0U, info_didnt_run().run_count);
95
96 ShutdownModule();
97
98 // Check case (1).
99 EXPECT_EQ(1U, info_did_run().run_count);
100
101 // Check case (2).
102 EXPECT_EQ(1U, info_did_abort().run_count);
103
104 // Check case (3).
105 EXPECT_EQ(1U, info_didnt_run().run_count);
106 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result);
107 }
108
109 // -----------------------------------------------------------------------------
110
111 namespace {
112
113 class CallbackMockResource : public MockResource {
114 public:
115 CallbackMockResource(PP_Instance instance) : MockResource(instance) {}
116 ~CallbackMockResource() {}
117
118 PP_Resource SetupForTest() {
119 PP_Resource resource_id = GetReference();
120 EXPECT_NE(0, resource_id);
121
122 PluginModule* module = ResourceHelper::GetPluginModule(this);
123
124 callback_did_run_ = new TrackedCompletionCallback(
125 module->GetCallbackTracker(),
126 resource_id,
127 PP_MakeCompletionCallback(&TestCallback, &info_did_run_));
128 EXPECT_EQ(0U, info_did_run_.run_count);
129
130 callback_did_abort_ = new TrackedCompletionCallback(
131 module->GetCallbackTracker(),
132 resource_id,
133 PP_MakeCompletionCallback(&TestCallback, &info_did_abort_));
134 EXPECT_EQ(0U, info_did_abort_.run_count);
135
136 callback_didnt_run_ = new TrackedCompletionCallback(
137 module->GetCallbackTracker(),
138 resource_id,
139 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_));
140 EXPECT_EQ(0U, info_didnt_run_.run_count);
141
142 callback_did_run_->Run(PP_OK);
143 callback_did_abort_->Abort();
144
145 CheckIntermediateState();
146
147 return resource_id;
148 }
149
150 void CheckIntermediateState() {
151 EXPECT_EQ(1U, info_did_run_.run_count);
152 EXPECT_EQ(PP_OK, info_did_run_.result);
153
154 EXPECT_EQ(1U, info_did_abort_.run_count);
155 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
156
157 EXPECT_EQ(0U, info_didnt_run_.run_count);
158 }
159
160 void CheckFinalState() {
161 EXPECT_EQ(1U, info_did_run_.run_count);
162 EXPECT_EQ(PP_OK, info_did_run_.result);
163 EXPECT_EQ(1U, info_did_abort_.run_count);
164 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result);
165 EXPECT_EQ(1U, info_didnt_run_.run_count);
166 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result);
167 }
168
169 scoped_refptr<TrackedCompletionCallback> callback_did_run_;
170 CallbackRunInfo info_did_run_;
171
172 scoped_refptr<TrackedCompletionCallback> callback_did_abort_;
173 CallbackRunInfo info_did_abort_;
174
175 scoped_refptr<TrackedCompletionCallback> callback_didnt_run_;
176 CallbackRunInfo info_didnt_run_;
177 };
178
179 } // namespace
180
181 class CallbackResourceTest : public PpapiUnittest {
182 public:
183 CallbackResourceTest() {}
184 };
185
186 // Test that callbacks get aborted on the last resource unref.
187 TEST_F(CallbackResourceTest, AbortOnNoRef) {
188 HostResourceTracker* resource_tracker =
189 HostGlobals::Get()->host_resource_tracker();
190
191 // Test several things: Unref-ing a resource (to zero refs) with callbacks
192 // which (1) have been run, (2) have been aborted, (3) haven't been completed.
193 // Check that the uncompleted one gets aborted, and that the others don't get
194 // called again.
195 scoped_refptr<CallbackMockResource> resource_1(
196 new CallbackMockResource(instance()->pp_instance()));
197 PP_Resource resource_1_id = resource_1->SetupForTest();
198
199 // Also do the same for a second resource, and make sure that unref-ing the
200 // first resource doesn't much up the second resource.
201 scoped_refptr<CallbackMockResource> resource_2(
202 new CallbackMockResource(instance()->pp_instance()));
203 PP_Resource resource_2_id = resource_2->SetupForTest();
204
205 // Double-check that resource #1 is still okay.
206 resource_1->CheckIntermediateState();
207
208 // Kill resource #1, spin the message loop to run posted calls, and check that
209 // things are in the expected states.
210 resource_tracker->ReleaseResource(resource_1_id);
211 MessageLoop::current()->RunAllPending();
212 resource_1->CheckFinalState();
213 resource_2->CheckIntermediateState();
214
215 // Kill resource #2.
216 resource_tracker->ReleaseResource(resource_2_id);
217 MessageLoop::current()->RunAllPending();
218 resource_1->CheckFinalState();
219 resource_2->CheckFinalState();
220
221 // This shouldn't be needed, but make sure there are no stranded tasks.
222 MessageLoop::current()->RunAllPending();
223 }
224
225 // Test that "resurrecting" a resource (getting a new ID for a |Resource|)
226 // doesn't resurrect callbacks.
227 TEST_F(CallbackResourceTest, Resurrection) {
228 HostResourceTracker* resource_tracker =
229 HostGlobals::Get()->host_resource_tracker();
230
231 scoped_refptr<CallbackMockResource> resource(
232 new CallbackMockResource(instance()->pp_instance()));
233 PP_Resource resource_id = resource->SetupForTest();
234
235 // Unref it, spin the message loop to run posted calls, and check that things
236 // are in the expected states.
237 resource_tracker->ReleaseResource(resource_id);
238 MessageLoop::current()->RunAllPending();
239 resource->CheckFinalState();
240
241 // "Resurrect" it and check that the callbacks are still dead.
242 PP_Resource new_resource_id = resource->GetReference();
243 MessageLoop::current()->RunAllPending();
244 resource->CheckFinalState();
245
246 // Unref it again and do the same.
247 resource_tracker->ReleaseResource(new_resource_id);
248 MessageLoop::current()->RunAllPending();
249 resource->CheckFinalState();
250
251 // This shouldn't be needed, but make sure there are no stranded tasks.
252 MessageLoop::current()->RunAllPending();
253 }
254
255 } // namespace ppapi
256 } // namespace webkit
OLDNEW
« no previous file with comments | « webkit/plugins/ppapi/callbacks.cc ('k') | webkit/plugins/ppapi/file_callbacks.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698