| 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/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "ppapi/c/pp_var.h" | 8 #include "ppapi/c/pp_var.h" |
| 9 #include "ppapi/c/ppp_instance.h" | 9 #include "ppapi/c/ppp_instance.h" |
| 10 #include "third_party/npapi/bindings/npruntime.h" | 10 #include "third_party/npapi/bindings/npruntime.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 PpapiUnittest::TearDown(); | 92 PpapiUnittest::TearDown(); |
| 93 ResourceTracker::ClearSingletonOverride(); | 93 ResourceTracker::ClearSingletonOverride(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 ResourceTracker& tracker() { return tracker_; } | 96 ResourceTracker& tracker() { return tracker_; } |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 ResourceTracker tracker_; | 99 ResourceTracker tracker_; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 TEST_F(ResourceTrackerTest, Ref) { | |
| 103 ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); | |
| 104 EXPECT_EQ(0u, | |
| 105 tracker().GetLiveObjectsForInstance(instance()->pp_instance())); | |
| 106 { | |
| 107 scoped_refptr<TrackedMockResource> new_resource( | |
| 108 new TrackedMockResource(instance())); | |
| 109 ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); | |
| 110 | |
| 111 // Since we haven't gotten a PP_Resource, it's not associated with the | |
| 112 // module. | |
| 113 EXPECT_EQ(0u, | |
| 114 tracker().GetLiveObjectsForInstance(instance()->pp_instance())); | |
| 115 } | |
| 116 ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); | |
| 117 | |
| 118 // Make a new resource and get it as a PP_Resource. | |
| 119 PP_Resource resource_id = 0; | |
| 120 { | |
| 121 scoped_refptr<TrackedMockResource> new_resource( | |
| 122 new TrackedMockResource(instance())); | |
| 123 ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); | |
| 124 resource_id = new_resource->GetReference(); | |
| 125 EXPECT_EQ(1u, | |
| 126 tracker().GetLiveObjectsForInstance(instance()->pp_instance())); | |
| 127 | |
| 128 // Resource IDs should be consistent. | |
| 129 PP_Resource resource_id_2 = new_resource->GetReference(); | |
| 130 ASSERT_EQ(resource_id, resource_id_2); | |
| 131 } | |
| 132 | |
| 133 // This time it should not have been deleted since the PP_Resource carries | |
| 134 // a ref. | |
| 135 ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); | |
| 136 | |
| 137 // Now we have two refs, derefing twice should delete the object. | |
| 138 tracker().UnrefResource(resource_id); | |
| 139 ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); | |
| 140 tracker().UnrefResource(resource_id); | |
| 141 ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); | |
| 142 } | |
| 143 | |
| 144 TEST_F(ResourceTrackerTest, DeleteResourceWithInstance) { | |
| 145 // Make a second instance (the test harness already creates & manages one). | |
| 146 scoped_refptr<PluginInstance> instance2( | |
| 147 PluginInstance::Create1_0(delegate(), module(), | |
| 148 GetMockInterface(PPP_INSTANCE_INTERFACE_1_0))); | |
| 149 PP_Instance pp_instance2 = instance2->pp_instance(); | |
| 150 | |
| 151 // Make two resources and take refs on behalf of the "plugin" for each. | |
| 152 scoped_refptr<TrackedMockResource> resource1( | |
| 153 new TrackedMockResource(instance2)); | |
| 154 resource1->GetReference(); | |
| 155 scoped_refptr<TrackedMockResource> resource2( | |
| 156 new TrackedMockResource(instance2)); | |
| 157 resource2->GetReference(); | |
| 158 | |
| 159 // Keep an "internal" ref to only the first (the PP_Resource also holds a | |
| 160 // ref to each resource on behalf of the plugin). | |
| 161 resource2 = NULL; | |
| 162 | |
| 163 ASSERT_EQ(2, TrackedMockResource::tracked_objects_alive); | |
| 164 EXPECT_EQ(2u, tracker().GetLiveObjectsForInstance(pp_instance2)); | |
| 165 | |
| 166 // Free the instance, this should release both plugin refs. | |
| 167 instance2 = NULL; | |
| 168 EXPECT_EQ(0u, tracker().GetLiveObjectsForInstance(pp_instance2)); | |
| 169 | |
| 170 // The resource we have a scoped_refptr to should still be alive, but it | |
| 171 // should have a NULL instance. | |
| 172 ASSERT_FALSE(resource1->instance()); | |
| 173 ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); | |
| 174 resource1 = NULL; | |
| 175 ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); | |
| 176 } | |
| 177 | |
| 178 TEST_F(ResourceTrackerTest, DeleteObjectVarWithInstance) { | 102 TEST_F(ResourceTrackerTest, DeleteObjectVarWithInstance) { |
| 179 // Make a second instance (the test harness already creates & manages one). | 103 // Make a second instance (the test harness already creates & manages one). |
| 180 scoped_refptr<PluginInstance> instance2( | 104 scoped_refptr<PluginInstance> instance2( |
| 181 PluginInstance::Create1_0(delegate(), module(), | 105 PluginInstance::Create1_0(delegate(), module(), |
| 182 GetMockInterface(PPP_INSTANCE_INTERFACE_1_0))); | 106 GetMockInterface(PPP_INSTANCE_INTERFACE_1_0))); |
| 183 PP_Instance pp_instance2 = instance2->pp_instance(); | 107 PP_Instance pp_instance2 = instance2->pp_instance(); |
| 184 | 108 |
| 185 // Make an object var. | 109 // Make an object var. |
| 186 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); | 110 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); |
| 187 NPObjectToPPVar(instance2.get(), npobject.get()); | 111 NPObjectToPPVar(instance2.get(), npobject.get()); |
| 188 | 112 |
| 189 EXPECT_EQ(1, g_npobjects_alive); | 113 EXPECT_EQ(1, g_npobjects_alive); |
| 190 EXPECT_EQ(1u, tracker().GetLiveObjectsForInstance(pp_instance2)); | 114 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); |
| 191 | 115 |
| 192 // Free the instance, this should release the ObjectVar. | 116 // Free the instance, this should release the ObjectVar. |
| 193 instance2 = NULL; | 117 instance2 = NULL; |
| 194 EXPECT_EQ(0u, tracker().GetLiveObjectsForInstance(pp_instance2)); | 118 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); |
| 195 } | 119 } |
| 196 | 120 |
| 197 // Make sure that using the same NPObject should give the same PP_Var | 121 // Make sure that using the same NPObject should give the same PP_Var |
| 198 // each time. | 122 // each time. |
| 199 TEST_F(ResourceTrackerTest, ReuseVar) { | 123 TEST_F(ResourceTrackerTest, ReuseVar) { |
| 200 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); | 124 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); |
| 201 | 125 |
| 202 PP_Var pp_object1 = NPObjectToPPVar(instance(), npobject.get()); | 126 PP_Var pp_object1 = NPObjectToPPVar(instance(), npobject.get()); |
| 203 PP_Var pp_object2 = NPObjectToPPVar(instance(), npobject.get()); | 127 PP_Var pp_object2 = NPObjectToPPVar(instance(), npobject.get()); |
| 204 | 128 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 222 | 146 |
| 223 // Releasing the resource should free the internal ref, and so making a new | 147 // Releasing the resource should free the internal ref, and so making a new |
| 224 // one now should generate a new ID. | 148 // one now should generate a new ID. |
| 225 PP_Var pp_object3 = NPObjectToPPVar(instance(), npobject.get()); | 149 PP_Var pp_object3 = NPObjectToPPVar(instance(), npobject.get()); |
| 226 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); | 150 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); |
| 227 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); | 151 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); |
| 228 } | 152 } |
| 229 | 153 |
| 230 } // namespace ppapi | 154 } // namespace ppapi |
| 231 } // namespace webkit | 155 } // namespace webkit |
| OLD | NEW |