Chromium Code Reviews| 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) { | |
|
dmichael (off chromium)
2013/08/09 21:14:01
I think it would make sense to do EXPECT_EQ(0, inf
bbudge
2013/08/09 21:36:29
Nice, done!
| |
| 197 info->completion_task_run_count++; | |
| 198 if (info->completion_task_run_count == 1) | |
| 199 info->completion_task_result = result; | |
| 200 return kOverrideResultValue; | |
| 201 } | |
| 202 | |
| 166 void CheckIntermediateState() { | 203 void CheckIntermediateState() { |
| 167 EXPECT_EQ(1U, info_did_run_.run_count); | 204 EXPECT_EQ(1U, info_did_run_.run_count); |
| 168 EXPECT_EQ(PP_OK, info_did_run_.result); | 205 EXPECT_EQ(PP_OK, info_did_run_.result); |
| 206 EXPECT_EQ(0U, info_did_run_.completion_task_run_count); | |
| 207 | |
| 208 EXPECT_EQ(1U, info_did_run_with_completion_task_.run_count); | |
| 209 // completion task should override the result. | |
| 210 EXPECT_EQ(kOverrideResultValue, info_did_run_with_completion_task_.result); | |
| 211 EXPECT_EQ(1U, info_did_run_with_completion_task_.completion_task_run_count); | |
| 212 EXPECT_EQ(PP_OK, | |
| 213 info_did_run_with_completion_task_.completion_task_result); | |
| 169 | 214 |
| 170 EXPECT_EQ(1U, info_did_abort_.run_count); | 215 EXPECT_EQ(1U, info_did_abort_.run_count); |
| 216 // completion task shouldn't override an abort. | |
| 171 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); | 217 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); |
| 218 EXPECT_EQ(1U, info_did_abort_.completion_task_run_count); | |
| 219 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.completion_task_result); | |
| 172 | 220 |
| 221 EXPECT_EQ(0U, info_didnt_run_.completion_task_run_count); | |
| 173 EXPECT_EQ(0U, info_didnt_run_.run_count); | 222 EXPECT_EQ(0U, info_didnt_run_.run_count); |
| 174 } | 223 } |
| 175 | 224 |
| 176 void CheckFinalState() { | 225 void CheckFinalState() { |
| 177 EXPECT_EQ(1U, info_did_run_.run_count); | 226 EXPECT_EQ(1U, info_did_run_.run_count); |
| 178 EXPECT_EQ(PP_OK, info_did_run_.result); | 227 EXPECT_EQ(PP_OK, info_did_run_.result); |
| 179 EXPECT_EQ(1U, info_did_abort_.run_count); | 228 EXPECT_EQ(1U, info_did_abort_.run_count); |
| 180 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); | 229 EXPECT_EQ(PP_ERROR_ABORTED, info_did_abort_.result); |
| 181 EXPECT_EQ(1U, info_didnt_run_.run_count); | 230 EXPECT_EQ(1U, info_didnt_run_.run_count); |
| 182 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result); | 231 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run_.result); |
| 183 } | 232 } |
| 184 | 233 |
| 185 scoped_refptr<TrackedCallback> callback_did_run_; | 234 scoped_refptr<TrackedCallback> callback_did_run_; |
| 186 CallbackRunInfo info_did_run_; | 235 CallbackRunInfo info_did_run_; |
| 187 | 236 |
| 237 scoped_refptr<TrackedCallback> callback_did_run_with_completion_task_; | |
| 238 CallbackRunInfo info_did_run_with_completion_task_; | |
| 239 | |
| 188 scoped_refptr<TrackedCallback> callback_did_abort_; | 240 scoped_refptr<TrackedCallback> callback_did_abort_; |
| 189 CallbackRunInfo info_did_abort_; | 241 CallbackRunInfo info_did_abort_; |
| 190 | 242 |
| 191 scoped_refptr<TrackedCallback> callback_didnt_run_; | 243 scoped_refptr<TrackedCallback> callback_didnt_run_; |
| 192 CallbackRunInfo info_didnt_run_; | 244 CallbackRunInfo info_didnt_run_; |
| 193 }; | 245 }; |
| 194 | 246 |
| 195 } // namespace | 247 } // namespace |
| 196 | 248 |
| 197 // Test that callbacks get aborted on the last resource unref. | 249 // 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. | 309 // Unref it again and do the same. |
| 258 resource_tracker->ReleaseResource(new_resource_id); | 310 resource_tracker->ReleaseResource(new_resource_id); |
| 259 base::MessageLoop::current()->RunUntilIdle(); | 311 base::MessageLoop::current()->RunUntilIdle(); |
| 260 resource->CheckFinalState(); | 312 resource->CheckFinalState(); |
| 261 | 313 |
| 262 // This shouldn't be needed, but make sure there are no stranded tasks. | 314 // This shouldn't be needed, but make sure there are no stranded tasks. |
| 263 base::MessageLoop::current()->RunUntilIdle(); | 315 base::MessageLoop::current()->RunUntilIdle(); |
| 264 } | 316 } |
| 265 | 317 |
| 266 } // namespace ppapi | 318 } // namespace ppapi |
| OLD | NEW |