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/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
6 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
7 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
8 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
9 #include "ppapi/shared_impl/callback_tracker.h" | 10 #include "ppapi/shared_impl/callback_tracker.h" |
10 #include "ppapi/shared_impl/resource.h" | 11 #include "ppapi/shared_impl/resource.h" |
11 #include "ppapi/shared_impl/resource_tracker.h" | 12 #include "ppapi/shared_impl/resource_tracker.h" |
12 #include "ppapi/shared_impl/test_globals.h" | 13 #include "ppapi/shared_impl/test_globals.h" |
13 #include "ppapi/shared_impl/tracked_callback.h" | 14 #include "ppapi/shared_impl/tracked_callback.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 15 matching lines...) Expand all Loading... |
30 virtual void TearDown() OVERRIDE { | 31 virtual void TearDown() OVERRIDE { |
31 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); | 32 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); |
32 } | 33 } |
33 | 34 |
34 private: | 35 private: |
35 base::MessageLoop message_loop_; | 36 base::MessageLoop message_loop_; |
36 TestGlobals globals_; | 37 TestGlobals globals_; |
37 PP_Instance pp_instance_; | 38 PP_Instance pp_instance_; |
38 }; | 39 }; |
39 | 40 |
| 41 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. |
| 42 const int32_t kInitializedResultValue = 1; |
| 43 const int32_t kOverrideResultValue = 2; |
| 44 |
40 struct CallbackRunInfo { | 45 struct CallbackRunInfo { |
41 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. | 46 CallbackRunInfo() |
42 CallbackRunInfo() : run_count(0), result(1) {} | 47 : run_count(0), |
| 48 result(kInitializedResultValue), |
| 49 completion_task_run_count(0), |
| 50 completion_task_result(kInitializedResultValue) {} |
43 unsigned run_count; | 51 unsigned run_count; |
44 int32_t result; | 52 int32_t result; |
| 53 unsigned completion_task_run_count; |
| 54 int32_t completion_task_result; |
45 }; | 55 }; |
46 | 56 |
47 void TestCallback(void* user_data, int32_t result) { | 57 void TestCallback(void* user_data, int32_t result) { |
48 CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data); | 58 CallbackRunInfo* info = reinterpret_cast<CallbackRunInfo*>(user_data); |
49 info->run_count++; | 59 info->run_count++; |
50 if (info->run_count == 1) | 60 if (info->run_count == 1) |
51 info->result = result; | 61 info->result = result; |
52 } | 62 } |
53 | 63 |
54 } // namespace | 64 } // namespace |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 ~CallbackMockResource() {} | 147 ~CallbackMockResource() {} |
138 | 148 |
139 PP_Resource SetupForTest() { | 149 PP_Resource SetupForTest() { |
140 PP_Resource resource_id = GetReference(); | 150 PP_Resource resource_id = GetReference(); |
141 EXPECT_NE(0, resource_id); | 151 EXPECT_NE(0, resource_id); |
142 | 152 |
143 callback_did_run_ = new TrackedCallback( | 153 callback_did_run_ = new TrackedCallback( |
144 this, | 154 this, |
145 PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); | 155 PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); |
146 EXPECT_EQ(0U, info_did_run_.run_count); | 156 EXPECT_EQ(0U, info_did_run_.run_count); |
| 157 EXPECT_EQ(0U, info_did_run_.completion_task_run_count); |
| 158 |
| 159 // In order to test that the completion task can override the callback |
| 160 // result, we need to test callbacks with and without a completion task. |
| 161 callback_did_run_with_completion_task_ = new TrackedCallback( |
| 162 this, |
| 163 PP_MakeCompletionCallback(&TestCallback, |
| 164 &info_did_run_with_completion_task_)); |
| 165 callback_did_run_with_completion_task_->set_completion_task( |
| 166 Bind(&CallbackMockResource::CompletionTask, this, |
| 167 &info_did_run_with_completion_task_)); |
| 168 EXPECT_EQ(0U, info_did_run_with_completion_task_.run_count); |
| 169 EXPECT_EQ(0U, info_did_run_with_completion_task_.completion_task_run_count); |
147 | 170 |
148 callback_did_abort_ = new TrackedCallback( | 171 callback_did_abort_ = new TrackedCallback( |
149 this, | 172 this, |
150 PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); | 173 PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); |
| 174 callback_did_abort_->set_completion_task( |
| 175 Bind(&CallbackMockResource::CompletionTask, this, &info_did_abort_)); |
151 EXPECT_EQ(0U, info_did_abort_.run_count); | 176 EXPECT_EQ(0U, info_did_abort_.run_count); |
| 177 EXPECT_EQ(0U, info_did_abort_.completion_task_run_count); |
152 | 178 |
153 callback_didnt_run_ = new TrackedCallback( | 179 callback_didnt_run_ = new TrackedCallback( |
154 this, | 180 this, |
155 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); | 181 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); |
| 182 callback_didnt_run_->set_completion_task( |
| 183 Bind(&CallbackMockResource::CompletionTask, this, &info_didnt_run_)); |
156 EXPECT_EQ(0U, info_didnt_run_.run_count); | 184 EXPECT_EQ(0U, info_didnt_run_.run_count); |
| 185 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count); |
157 | 186 |
158 callback_did_run_->Run(PP_OK); | 187 callback_did_run_->Run(PP_OK); |
| 188 callback_did_run_with_completion_task_->Run(PP_OK); |
159 callback_did_abort_->Abort(); | 189 callback_did_abort_->Abort(); |
160 | 190 |
161 CheckIntermediateState(); | 191 CheckIntermediateState(); |
162 | 192 |
163 return resource_id; | 193 return resource_id; |
164 } | 194 } |
165 | 195 |
| 196 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { |
| 197 // We should run before the callback. |
| 198 EXPECT_EQ(0U, info->run_count); |
| 199 info->completion_task_run_count++; |
| 200 if (info->completion_task_run_count == 1) |
| 201 info->completion_task_result = result; |
| 202 return kOverrideResultValue; |
| 203 } |
| 204 |
166 void CheckIntermediateState() { | 205 void CheckIntermediateState() { |
167 EXPECT_EQ(1U, info_did_run_.run_count); | 206 EXPECT_EQ(1U, info_did_run_.run_count); |
168 EXPECT_EQ(PP_OK, info_did_run_.result); | 207 EXPECT_EQ(PP_OK, info_did_run_.result); |
| 208 EXPECT_EQ(0U, info_did_run_.completion_task_run_count); |
| 209 |
| 210 EXPECT_EQ(1U, info_did_run_with_completion_task_.run_count); |
| 211 // completion task should override the result. |
| 212 EXPECT_EQ(kOverrideResultValue, info_did_run_with_completion_task_.result); |
| 213 EXPECT_EQ(1U, info_did_run_with_completion_task_.completion_task_run_count); |
| 214 EXPECT_EQ(PP_OK, |
| 215 info_did_run_with_completion_task_.completion_task_result); |
169 | 216 |
170 EXPECT_EQ(1U, info_did_abort_.run_count); | 217 EXPECT_EQ(1U, info_did_abort_.run_count); |
| 218 // completion task shouldn't override an abort. |
171 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); | 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); |
172 | 222 |
| 223 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count); |
173 EXPECT_EQ(0U, info_didnt_run_.run_count); | 224 EXPECT_EQ(0U, info_didnt_run_.run_count); |
174 } | 225 } |
175 | 226 |
176 void CheckFinalState() { | 227 void CheckFinalState() { |
177 EXPECT_EQ(1U, info_did_run_.run_count); | 228 EXPECT_EQ(1U, info_did_run_.run_count); |
178 EXPECT_EQ(PP_OK, info_did_run_.result); | 229 EXPECT_EQ(PP_OK, info_did_run_.result); |
179 EXPECT_EQ(1U, info_did_abort_.run_count); | 230 EXPECT_EQ(1U, info_did_abort_.run_count); |
180 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); | 231 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); |
181 EXPECT_EQ(1U, info_didnt_run_.run_count); | 232 EXPECT_EQ(1U, info_didnt_run_.run_count); |
182 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result); | 233 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result); |
183 } | 234 } |
184 | 235 |
185 scoped_refptr<TrackedCallback> callback_did_run_; | 236 scoped_refptr<TrackedCallback> callback_did_run_; |
186 CallbackRunInfo info_did_run_; | 237 CallbackRunInfo info_did_run_; |
187 | 238 |
| 239 scoped_refptr<TrackedCallback> callback_did_run_with_completion_task_; |
| 240 CallbackRunInfo info_did_run_with_completion_task_; |
| 241 |
188 scoped_refptr<TrackedCallback> callback_did_abort_; | 242 scoped_refptr<TrackedCallback> callback_did_abort_; |
189 CallbackRunInfo info_did_abort_; | 243 CallbackRunInfo info_did_abort_; |
190 | 244 |
191 scoped_refptr<TrackedCallback> callback_didnt_run_; | 245 scoped_refptr<TrackedCallback> callback_didnt_run_; |
192 CallbackRunInfo info_didnt_run_; | 246 CallbackRunInfo info_didnt_run_; |
193 }; | 247 }; |
194 | 248 |
195 } // namespace | 249 } // namespace |
196 | 250 |
197 // Test that callbacks get aborted on the last resource unref. | 251 // Test that callbacks get aborted on the last resource unref. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // Unref it again and do the same. | 311 // Unref it again and do the same. |
258 resource_tracker->ReleaseResource(new_resource_id); | 312 resource_tracker->ReleaseResource(new_resource_id); |
259 base::MessageLoop::current()->RunUntilIdle(); | 313 base::MessageLoop::current()->RunUntilIdle(); |
260 resource->CheckFinalState(); | 314 resource->CheckFinalState(); |
261 | 315 |
262 // This shouldn't be needed, but make sure there are no stranded tasks. | 316 // This shouldn't be needed, but make sure there are no stranded tasks. |
263 base::MessageLoop::current()->RunUntilIdle(); | 317 base::MessageLoop::current()->RunUntilIdle(); |
264 } | 318 } |
265 | 319 |
266 } // namespace ppapi | 320 } // namespace ppapi |
OLD | NEW |