| 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "ppapi/c/pp_completion_callback.h" | 7 #include "ppapi/c/pp_completion_callback.h" |
| 8 #include "ppapi/c/pp_errors.h" | 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/shared_impl/callback_tracker.h" | 9 #include "ppapi/shared_impl/callback_tracker.h" |
| 10 #include "ppapi/shared_impl/resource.h" | 10 #include "ppapi/shared_impl/resource.h" |
| 11 #include "ppapi/shared_impl/resource_tracker.h" | 11 #include "ppapi/shared_impl/resource_tracker.h" |
| 12 #include "ppapi/shared_impl/test_globals.h" | 12 #include "ppapi/shared_impl/test_globals.h" |
| 13 #include "ppapi/shared_impl/tracked_callback.h" | 13 #include "ppapi/shared_impl/tracked_callback.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace ppapi { | 16 namespace ppapi { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class TrackedCallbackTest : public testing::Test { | 20 class TrackedCallbackTest : public testing::Test { |
| 21 public: | 21 public: |
| 22 TrackedCallbackTest() | 22 TrackedCallbackTest() |
| 23 : message_loop_(MessageLoop::TYPE_DEFAULT), | 23 : message_loop_(base::MessageLoop::TYPE_DEFAULT), pp_instance_(1234) {} |
| 24 pp_instance_(1234) {} | |
| 25 | 24 |
| 26 PP_Instance pp_instance() const { return pp_instance_; } | 25 PP_Instance pp_instance() const { return pp_instance_; } |
| 27 | 26 |
| 28 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
| 29 globals_.GetResourceTracker()->DidCreateInstance(pp_instance_); | 28 globals_.GetResourceTracker()->DidCreateInstance(pp_instance_); |
| 30 } | 29 } |
| 31 virtual void TearDown() OVERRIDE { | 30 virtual void TearDown() OVERRIDE { |
| 32 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); | 31 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); |
| 33 } | 32 } |
| 34 | 33 |
| 35 private: | 34 private: |
| 36 MessageLoop message_loop_; | 35 base::MessageLoop message_loop_; |
| 37 TestGlobals globals_; | 36 TestGlobals globals_; |
| 38 PP_Instance pp_instance_; | 37 PP_Instance pp_instance_; |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 struct CallbackRunInfo { | 40 struct CallbackRunInfo { |
| 42 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. | 41 // All valid results (PP_OK, PP_ERROR_...) are nonpositive. |
| 43 CallbackRunInfo() : run_count(0), result(1) {} | 42 CallbackRunInfo() : run_count(0), result(1) {} |
| 44 unsigned run_count; | 43 unsigned run_count; |
| 45 int32_t result; | 44 int32_t result; |
| 46 }; | 45 }; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 scoped_refptr<CallbackMockResource> resource_2( | 212 scoped_refptr<CallbackMockResource> resource_2( |
| 214 new CallbackMockResource(pp_instance())); | 213 new CallbackMockResource(pp_instance())); |
| 215 PP_Resource resource_2_id = resource_2->SetupForTest(); | 214 PP_Resource resource_2_id = resource_2->SetupForTest(); |
| 216 | 215 |
| 217 // Double-check that resource #1 is still okay. | 216 // Double-check that resource #1 is still okay. |
| 218 resource_1->CheckIntermediateState(); | 217 resource_1->CheckIntermediateState(); |
| 219 | 218 |
| 220 // Kill resource #1, spin the message loop to run posted calls, and check that | 219 // Kill resource #1, spin the message loop to run posted calls, and check that |
| 221 // things are in the expected states. | 220 // things are in the expected states. |
| 222 resource_tracker->ReleaseResource(resource_1_id); | 221 resource_tracker->ReleaseResource(resource_1_id); |
| 223 MessageLoop::current()->RunUntilIdle(); | 222 base::MessageLoop::current()->RunUntilIdle(); |
| 224 resource_1->CheckFinalState(); | 223 resource_1->CheckFinalState(); |
| 225 resource_2->CheckIntermediateState(); | 224 resource_2->CheckIntermediateState(); |
| 226 | 225 |
| 227 // Kill resource #2. | 226 // Kill resource #2. |
| 228 resource_tracker->ReleaseResource(resource_2_id); | 227 resource_tracker->ReleaseResource(resource_2_id); |
| 229 MessageLoop::current()->RunUntilIdle(); | 228 base::MessageLoop::current()->RunUntilIdle(); |
| 230 resource_1->CheckFinalState(); | 229 resource_1->CheckFinalState(); |
| 231 resource_2->CheckFinalState(); | 230 resource_2->CheckFinalState(); |
| 232 | 231 |
| 233 // This shouldn't be needed, but make sure there are no stranded tasks. | 232 // This shouldn't be needed, but make sure there are no stranded tasks. |
| 234 MessageLoop::current()->RunUntilIdle(); | 233 base::MessageLoop::current()->RunUntilIdle(); |
| 235 } | 234 } |
| 236 | 235 |
| 237 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) | 236 // Test that "resurrecting" a resource (getting a new ID for a |Resource|) |
| 238 // doesn't resurrect callbacks. | 237 // doesn't resurrect callbacks. |
| 239 TEST_F(CallbackResourceTest, Resurrection) { | 238 TEST_F(CallbackResourceTest, Resurrection) { |
| 240 ResourceTracker* resource_tracker = | 239 ResourceTracker* resource_tracker = |
| 241 PpapiGlobals::Get()->GetResourceTracker(); | 240 PpapiGlobals::Get()->GetResourceTracker(); |
| 242 | 241 |
| 243 scoped_refptr<CallbackMockResource> resource( | 242 scoped_refptr<CallbackMockResource> resource( |
| 244 new CallbackMockResource(pp_instance())); | 243 new CallbackMockResource(pp_instance())); |
| 245 PP_Resource resource_id = resource->SetupForTest(); | 244 PP_Resource resource_id = resource->SetupForTest(); |
| 246 | 245 |
| 247 // Unref it, spin the message loop to run posted calls, and check that things | 246 // Unref it, spin the message loop to run posted calls, and check that things |
| 248 // are in the expected states. | 247 // are in the expected states. |
| 249 resource_tracker->ReleaseResource(resource_id); | 248 resource_tracker->ReleaseResource(resource_id); |
| 250 MessageLoop::current()->RunUntilIdle(); | 249 base::MessageLoop::current()->RunUntilIdle(); |
| 251 resource->CheckFinalState(); | 250 resource->CheckFinalState(); |
| 252 | 251 |
| 253 // "Resurrect" it and check that the callbacks are still dead. | 252 // "Resurrect" it and check that the callbacks are still dead. |
| 254 PP_Resource new_resource_id = resource->GetReference(); | 253 PP_Resource new_resource_id = resource->GetReference(); |
| 255 MessageLoop::current()->RunUntilIdle(); | 254 base::MessageLoop::current()->RunUntilIdle(); |
| 256 resource->CheckFinalState(); | 255 resource->CheckFinalState(); |
| 257 | 256 |
| 258 // Unref it again and do the same. | 257 // Unref it again and do the same. |
| 259 resource_tracker->ReleaseResource(new_resource_id); | 258 resource_tracker->ReleaseResource(new_resource_id); |
| 260 MessageLoop::current()->RunUntilIdle(); | 259 base::MessageLoop::current()->RunUntilIdle(); |
| 261 resource->CheckFinalState(); | 260 resource->CheckFinalState(); |
| 262 | 261 |
| 263 // This shouldn't be needed, but make sure there are no stranded tasks. | 262 // This shouldn't be needed, but make sure there are no stranded tasks. |
| 264 MessageLoop::current()->RunUntilIdle(); | 263 base::MessageLoop::current()->RunUntilIdle(); |
| 265 } | 264 } |
| 266 | 265 |
| 267 } // namespace ppapi | 266 } // namespace ppapi |
| OLD | NEW |