Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "testing/gtest/include/gtest/gtest.h" | |
| 6 | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "ppapi/shared_impl/resource.h" | |
| 9 #include "ppapi/shared_impl/resource_tracker.h" | |
| 10 #include "ppapi/shared_impl/tracker_base.h" | |
| 11 | |
| 12 namespace ppapi { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 int mock_resource_alive_count = 0; | |
| 17 int last_plugin_ref_was_deleted_count = 0; | |
| 18 int instance_was_deleted_count = 0; | |
| 19 | |
| 20 class MyMockResource : public Resource { | |
| 21 public: | |
| 22 MyMockResource(PP_Instance instance) : Resource(instance) { | |
| 23 mock_resource_alive_count++; | |
| 24 } | |
| 25 virtual ~MyMockResource() { | |
| 26 mock_resource_alive_count--; | |
| 27 } | |
| 28 | |
| 29 virtual void LastPluginRefWasDeleted() OVERRIDE { | |
| 30 Resource::LastPluginRefWasDeleted(); | |
| 31 last_plugin_ref_was_deleted_count++; | |
| 32 } | |
| 33 virtual void InstanceWasDeleted() OVERRIDE { | |
| 34 Resource::InstanceWasDeleted(); | |
| 35 instance_was_deleted_count++; | |
| 36 } | |
| 37 }; | |
| 38 | |
| 39 // Global singleton used by the TrackerBase. | |
| 40 TrackerBase* my_tracker_base = NULL; | |
| 41 TrackerBase* GetMyTrackerBase() { | |
| 42 return my_tracker_base; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 class ResourceTrackerTest : public testing::Test, public TrackerBase { | |
| 48 public: | |
| 49 ResourceTrackerTest() {} | |
| 50 | |
| 51 // Test implementation. | |
| 52 virtual void SetUp() OVERRIDE { | |
| 53 my_tracker_base = this; | |
| 54 TrackerBase::Init(&GetMyTrackerBase); | |
| 55 | |
| 56 ASSERT_EQ(0, mock_resource_alive_count); | |
| 57 last_plugin_ref_was_deleted_count = 0; | |
| 58 instance_was_deleted_count = 0; | |
| 59 } | |
| 60 virtual void TearDown() OVERRIDE { | |
| 61 my_tracker_base = NULL; | |
| 62 TrackerBase::Init(NULL); | |
| 63 } | |
| 64 | |
| 65 // TrackerBase implementation. | |
| 66 virtual FunctionGroupBase* GetFunctionAPI( | |
| 67 PP_Instance inst, | |
| 68 pp::proxy::InterfaceID id) OVERRIDE { | |
| 69 return NULL; | |
| 70 } | |
| 71 virtual VarTracker* GetVarTracker() OVERRIDE { | |
| 72 return NULL; | |
| 73 } | |
| 74 virtual ResourceTracker* GetResourceTracker() OVERRIDE { | |
| 75 return &resource_tracker_; | |
| 76 } | |
| 77 | |
| 78 ResourceTracker& resource_tracker() { return resource_tracker_; } | |
| 79 | |
| 80 private: | |
| 81 ResourceTracker resource_tracker_; | |
| 82 }; | |
| 83 | |
| 84 // Test that LastPluginRefWasDeleted is called when the last plugin ref was | |
| 85 // deleted but the object lives on. | |
| 86 TEST_F(ResourceTrackerTest, LastPluginRef) { | |
| 87 PP_Instance instance = 0x1234567; | |
| 88 resource_tracker().DidCreateInstance(instance); | |
| 89 | |
| 90 scoped_refptr<MyMockResource> resource(new MyMockResource(instance)); | |
| 91 PP_Resource pp_resource = resource->GetReference(); | |
| 92 EXPECT_TRUE(resource_tracker().GetResource(pp_resource)); | |
| 93 | |
| 94 // Releasing it should keep the object (because we have a ref) but fire the | |
| 95 // "last plugin ref" message. | |
| 96 resource_tracker().ReleaseResource(pp_resource); | |
| 97 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | |
| 98 EXPECT_EQ(1, mock_resource_alive_count); | |
| 99 | |
| 100 resource_tracker().DidDeleteInstance(instance); | |
| 101 resource = NULL; | |
| 102 EXPECT_FALSE(resource_tracker().GetResource(pp_resource)); | |
| 103 } | |
| 104 | |
| 105 // Tests when the plugin is holding a ref to a resource when the instance is | |
| 106 // deleted. | |
| 107 TEST_F(ResourceTrackerTest, InstanceDeletedWithPluginRef) { | |
| 108 // Make a resource with one ref held by the plugin, and delete the instance. | |
| 109 PP_Instance instance = 0x2345678; | |
| 110 resource_tracker().DidCreateInstance(instance); | |
| 111 MyMockResource* resource = new MyMockResource(instance); | |
| 112 resource->GetReference(); | |
| 113 EXPECT_EQ(1, mock_resource_alive_count); | |
| 114 resource_tracker().DidDeleteInstance(instance); | |
| 115 | |
| 116 // The resource should have been deleted, and before it was, it should have | |
| 117 // received a "last plugin ref was deleted" notification. | |
| 118 EXPECT_EQ(0, mock_resource_alive_count); | |
| 119 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | |
| 120 EXPECT_EQ(0, instance_was_deleted_count); | |
| 121 } | |
| 122 | |
| 123 // Test when the plugin and the internal implementation (via scoped_refptr) is | |
| 124 // holding a ref to a resource when the instance is deleted. | |
| 125 TEST_F(ResourceTrackerTest, InstanceDeletedWithBothRefed) { | |
| 126 // Create a new instance. | |
| 127 PP_Instance instance = 0x3456789; | |
| 128 | |
| 129 // Make a resource with one ref held by the plugin and one ref held by us | |
| 130 // (outlives the plugin), and delete the instance. | |
| 131 resource_tracker().DidCreateInstance(instance); | |
| 132 scoped_refptr<MyMockResource> resource = new MyMockResource(instance); | |
| 133 resource->GetReference(); | |
| 134 EXPECT_EQ(1, mock_resource_alive_count); | |
| 135 resource_tracker().DidDeleteInstance(instance); | |
| 136 | |
| 137 // The resource should NOT have been deleted, and it should have received both | |
| 138 // a "last plugin ref was deleted" and a "instance was deleted" notification. | |
| 139 EXPECT_EQ(1, mock_resource_alive_count); | |
| 140 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | |
| 141 EXPECT_EQ(1, instance_was_deleted_count); | |
| 142 | |
| 143 resource = NULL; | |
| 144 EXPECT_EQ(0, mock_resource_alive_count); | |
|
dmichael (off chromium)
2011/08/17 19:02:54
optional: For this and the previous test, you coul
| |
| 145 } | |
| 146 | |
| 147 } // namespace ppapi | |
| OLD | NEW |