| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/plugins/ppapi/ppapi_unittest.h" | 5 #include "webkit/plugins/ppapi/ppapi_unittest.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "ppapi/c/pp_completion_callback.h" | 9 #include "ppapi/c/pp_completion_callback.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "webkit/plugins/ppapi/callbacks.h" | 11 #include "webkit/plugins/ppapi/callbacks.h" |
| 12 #include "webkit/plugins/ppapi/mock_resource.h" | 12 #include "webkit/plugins/ppapi/mock_resource.h" |
| 13 #include "webkit/plugins/ppapi/plugin_module.h" | 13 #include "webkit/plugins/ppapi/plugin_module.h" |
| 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 15 #include "webkit/plugins/ppapi/resource_helper.h" |
| 15 #include "webkit/plugins/ppapi/resource_tracker.h" | 16 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 16 | 17 |
| 17 namespace webkit { | 18 namespace webkit { |
| 18 namespace ppapi { | 19 namespace ppapi { |
| 19 | 20 |
| 20 struct CallbackRunInfo { | 21 struct CallbackRunInfo { |
| 21 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. | 22 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. |
| 22 CallbackRunInfo() : run_count(0), result(1) {} | 23 CallbackRunInfo() : run_count(0), result(1) {} |
| 23 unsigned run_count; | 24 unsigned run_count; |
| 24 int32_t result; | 25 int32_t result; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 EXPECT_EQ(1U, info_didnt_run().run_count); | 104 EXPECT_EQ(1U, info_didnt_run().run_count); |
| 104 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result); | 105 EXPECT_EQ(PP_ERROR_ABORTED, info_didnt_run().result); |
| 105 } | 106 } |
| 106 | 107 |
| 107 // ----------------------------------------------------------------------------- | 108 // ----------------------------------------------------------------------------- |
| 108 | 109 |
| 109 namespace { | 110 namespace { |
| 110 | 111 |
| 111 class CallbackMockResource : public MockResource { | 112 class CallbackMockResource : public MockResource { |
| 112 public: | 113 public: |
| 113 CallbackMockResource(PluginInstance* instance) : MockResource(instance) {} | 114 CallbackMockResource(PP_Instance instance) : MockResource(instance) {} |
| 114 ~CallbackMockResource() {} | 115 ~CallbackMockResource() {} |
| 115 | 116 |
| 116 PP_Resource SetupForTest() { | 117 PP_Resource SetupForTest() { |
| 117 PP_Resource resource_id = GetReference(); | 118 PP_Resource resource_id = GetReference(); |
| 118 EXPECT_NE(0, resource_id); | 119 EXPECT_NE(0, resource_id); |
| 119 | 120 |
| 121 PluginModule* module = ResourceHelper::GetPluginModule(this); |
| 122 |
| 120 callback_did_run_ = new TrackedCompletionCallback( | 123 callback_did_run_ = new TrackedCompletionCallback( |
| 121 instance()->module()->GetCallbackTracker(), | 124 module->GetCallbackTracker(), |
| 122 resource_id, | 125 resource_id, |
| 123 PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); | 126 PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); |
| 124 EXPECT_EQ(0U, info_did_run_.run_count); | 127 EXPECT_EQ(0U, info_did_run_.run_count); |
| 125 | 128 |
| 126 callback_did_abort_ = new TrackedCompletionCallback( | 129 callback_did_abort_ = new TrackedCompletionCallback( |
| 127 instance()->module()->GetCallbackTracker(), | 130 module->GetCallbackTracker(), |
| 128 resource_id, | 131 resource_id, |
| 129 PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); | 132 PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); |
| 130 EXPECT_EQ(0U, info_did_abort_.run_count); | 133 EXPECT_EQ(0U, info_did_abort_.run_count); |
| 131 | 134 |
| 132 callback_didnt_run_ = new TrackedCompletionCallback( | 135 callback_didnt_run_ = new TrackedCompletionCallback( |
| 133 instance()->module()->GetCallbackTracker(), | 136 module->GetCallbackTracker(), |
| 134 resource_id, | 137 resource_id, |
| 135 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); | 138 PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); |
| 136 EXPECT_EQ(0U, info_didnt_run_.run_count); | 139 EXPECT_EQ(0U, info_didnt_run_.run_count); |
| 137 | 140 |
| 138 callback_did_run_->Run(PP_OK); | 141 callback_did_run_->Run(PP_OK); |
| 139 callback_did_abort_->Abort(); | 142 callback_did_abort_->Abort(); |
| 140 | 143 |
| 141 CheckIntermediateState(); | 144 CheckIntermediateState(); |
| 142 | 145 |
| 143 return resource_id; | 146 return resource_id; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 184 |
| 182 // Test that callbacks get aborted on the last resource unref. | 185 // Test that callbacks get aborted on the last resource unref. |
| 183 TEST_F(CallbackResourceTest, AbortOnNoRef) { | 186 TEST_F(CallbackResourceTest, AbortOnNoRef) { |
| 184 ResourceTracker* resource_tracker = ResourceTracker::Get(); | 187 ResourceTracker* resource_tracker = ResourceTracker::Get(); |
| 185 | 188 |
| 186 // Test several things: Unref-ing a resource (to zero refs) with callbacks | 189 // Test several things: Unref-ing a resource (to zero refs) with callbacks |
| 187 // which (1) have been run, (2) have been aborted, (3) haven't been completed. | 190 // which (1) have been run, (2) have been aborted, (3) haven't been completed. |
| 188 // Check that the uncompleted one gets aborted, and that the others don't get | 191 // Check that the uncompleted one gets aborted, and that the others don't get |
| 189 // called again. | 192 // called again. |
| 190 scoped_refptr<CallbackMockResource> resource_1( | 193 scoped_refptr<CallbackMockResource> resource_1( |
| 191 new CallbackMockResource(instance())); | 194 new CallbackMockResource(instance()->pp_instance())); |
| 192 PP_Resource resource_1_id = resource_1->SetupForTest(); | 195 PP_Resource resource_1_id = resource_1->SetupForTest(); |
| 193 | 196 |
| 194 // Also do the same for a second resource, and make sure that unref-ing the | 197 // Also do the same for a second resource, and make sure that unref-ing the |
| 195 // first resource doesn't much up the second resource. | 198 // first resource doesn't much up the second resource. |
| 196 scoped_refptr<CallbackMockResource> resource_2( | 199 scoped_refptr<CallbackMockResource> resource_2( |
| 197 new CallbackMockResource(instance())); | 200 new CallbackMockResource(instance()->pp_instance())); |
| 198 PP_Resource resource_2_id = resource_2->SetupForTest(); | 201 PP_Resource resource_2_id = resource_2->SetupForTest(); |
| 199 | 202 |
| 200 // Double-check that resource #1 is still okay. | 203 // Double-check that resource #1 is still okay. |
| 201 resource_1->CheckIntermediateState(); | 204 resource_1->CheckIntermediateState(); |
| 202 | 205 |
| 203 // Kill resource #1, spin the message loop to run posted calls, and check that | 206 // Kill resource #1, spin the message loop to run posted calls, and check that |
| 204 // things are in the expected states. | 207 // things are in the expected states. |
| 205 resource_tracker->ReleaseResource(resource_1_id); | 208 resource_tracker->ReleaseResource(resource_1_id); |
| 206 MessageLoop::current()->RunAllPending(); | 209 MessageLoop::current()->RunAllPending(); |
| 207 resource_1->CheckFinalState(); | 210 resource_1->CheckFinalState(); |
| 208 resource_2->CheckIntermediateState(); | 211 resource_2->CheckIntermediateState(); |
| 209 | 212 |
| 210 // Kill resource #2. | 213 // Kill resource #2. |
| 211 resource_tracker->ReleaseResource(resource_2_id); | 214 resource_tracker->ReleaseResource(resource_2_id); |
| 212 MessageLoop::current()->RunAllPending(); | 215 MessageLoop::current()->RunAllPending(); |
| 213 resource_1->CheckFinalState(); | 216 resource_1->CheckFinalState(); |
| 214 resource_2->CheckFinalState(); | 217 resource_2->CheckFinalState(); |
| 215 | 218 |
| 216 // This shouldn't be needed, but make sure there are no stranded tasks. | 219 // This shouldn't be needed, but make sure there are no stranded tasks. |
| 217 MessageLoop::current()->RunAllPending(); | 220 MessageLoop::current()->RunAllPending(); |
| 218 } | 221 } |
| 219 | 222 |
| 220 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) | 223 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) |
| 221 // doesn't resurrect callbacks. | 224 // doesn't resurrect callbacks. |
| 222 TEST_F(CallbackResourceTest, Resurrection) { | 225 TEST_F(CallbackResourceTest, Resurrection) { |
| 223 ResourceTracker* resource_tracker = ResourceTracker::Get(); | 226 ResourceTracker* resource_tracker = ResourceTracker::Get(); |
| 224 | 227 |
| 225 scoped_refptr<CallbackMockResource> resource( | 228 scoped_refptr<CallbackMockResource> resource( |
| 226 new CallbackMockResource(instance())); | 229 new CallbackMockResource(instance()->pp_instance())); |
| 227 PP_Resource resource_id = resource->SetupForTest(); | 230 PP_Resource resource_id = resource->SetupForTest(); |
| 228 | 231 |
| 229 // Unref it, spin the message loop to run posted calls, and check that things | 232 // Unref it, spin the message loop to run posted calls, and check that things |
| 230 // are in the expected states. | 233 // are in the expected states. |
| 231 resource_tracker->ReleaseResource(resource_id); | 234 resource_tracker->ReleaseResource(resource_id); |
| 232 MessageLoop::current()->RunAllPending(); | 235 MessageLoop::current()->RunAllPending(); |
| 233 resource->CheckFinalState(); | 236 resource->CheckFinalState(); |
| 234 | 237 |
| 235 // "Resurrect" it and check that the callbacks are still dead. | 238 // "Resurrect" it and check that the callbacks are still dead. |
| 236 PP_Resource new_resource_id = resource->GetReference(); | 239 PP_Resource new_resource_id = resource->GetReference(); |
| 237 MessageLoop::current()->RunAllPending(); | 240 MessageLoop::current()->RunAllPending(); |
| 238 resource->CheckFinalState(); | 241 resource->CheckFinalState(); |
| 239 | 242 |
| 240 // Unref it again and do the same. | 243 // Unref it again and do the same. |
| 241 resource_tracker->ReleaseResource(new_resource_id); | 244 resource_tracker->ReleaseResource(new_resource_id); |
| 242 MessageLoop::current()->RunAllPending(); | 245 MessageLoop::current()->RunAllPending(); |
| 243 resource->CheckFinalState(); | 246 resource->CheckFinalState(); |
| 244 | 247 |
| 245 // This shouldn't be needed, but make sure there are no stranded tasks. | 248 // This shouldn't be needed, but make sure there are no stranded tasks. |
| 246 MessageLoop::current()->RunAllPending(); | 249 MessageLoop::current()->RunAllPending(); |
| 247 } | 250 } |
| 248 | 251 |
| 249 } // namespace ppapi | 252 } // namespace ppapi |
| 250 } // namespace webkit | 253 } // namespace webkit |
| OLD | NEW |