| 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" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 11 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" | 12 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" |
| 12 #include "webkit/plugins/ppapi/mock_resource.h" | 13 #include "webkit/plugins/ppapi/mock_resource.h" |
| 13 #include "webkit/plugins/ppapi/npapi_glue.h" | 14 #include "webkit/plugins/ppapi/npapi_glue.h" |
| 14 #include "webkit/plugins/ppapi/npobject_var.h" | 15 #include "webkit/plugins/ppapi/npobject_var.h" |
| 15 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 16 #include "webkit/plugins/ppapi/resource_tracker.h" | 17 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 17 | 18 |
| 18 using ppapi::NPObjectVar; | 19 using ppapi::NPObjectVar; |
| 19 | 20 |
| 20 namespace webkit { | 21 namespace webkit { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 int TrackedMockResource::tracked_objects_alive = 0; | 40 int TrackedMockResource::tracked_objects_alive = 0; |
| 40 | 41 |
| 41 // Tracked NPObjects ----------------------------------------------------------- | 42 // Tracked NPObjects ----------------------------------------------------------- |
| 42 | 43 |
| 43 int g_npobjects_alive = 0; | 44 int g_npobjects_alive = 0; |
| 44 | 45 |
| 45 void TrackedClassDeallocate(NPObject* npobject) { | 46 void TrackedClassDeallocate(NPObject* npobject) { |
| 46 g_npobjects_alive--; | 47 g_npobjects_alive--; |
| 48 delete npobject; |
| 47 } | 49 } |
| 48 | 50 |
| 49 NPClass g_tracked_npclass = { | 51 NPClass g_tracked_npclass = { |
| 50 NP_CLASS_STRUCT_VERSION, | 52 NP_CLASS_STRUCT_VERSION, |
| 51 NULL, | 53 NULL, |
| 52 &TrackedClassDeallocate, | 54 &TrackedClassDeallocate, |
| 53 NULL, | 55 NULL, |
| 54 NULL, | 56 NULL, |
| 55 NULL, | 57 NULL, |
| 56 NULL, | 58 NULL, |
| 57 NULL, | 59 NULL, |
| 58 NULL, | 60 NULL, |
| 59 NULL, | 61 NULL, |
| 60 NULL, | 62 NULL, |
| 61 NULL, | 63 NULL, |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 // Returns a new tracked NPObject with a refcount of 1. | 66 // Returns a new tracked NPObject with a refcount of 1. You'll want to put this |
| 67 // in a NPObjectReleaser to free this ref when the test completes. |
| 65 NPObject* NewTrackedNPObject() { | 68 NPObject* NewTrackedNPObject() { |
| 66 NPObject* object = new NPObject; | 69 NPObject* object = new NPObject; |
| 67 object->_class = &g_tracked_npclass; | 70 object->_class = &g_tracked_npclass; |
| 68 object->referenceCount = 1; | 71 object->referenceCount = 1; |
| 69 | 72 |
| 70 g_npobjects_alive++; | 73 g_npobjects_alive++; |
| 71 return object; | 74 return object; |
| 72 } | 75 } |
| 73 | 76 |
| 77 class ReleaseNPObject { |
| 78 public: |
| 79 void operator()(NPObject* o) const { |
| 80 WebKit::WebBindings::releaseObject(o); |
| 81 } |
| 82 }; |
| 83 |
| 84 // Handles automatically releasing a reference to the NPObject on destruction. |
| 85 // It's assumed the input has a ref already taken. |
| 86 typedef scoped_ptr_malloc<NPObject, ReleaseNPObject> NPObjectReleaser; |
| 87 |
| 74 } // namespace | 88 } // namespace |
| 75 | 89 |
| 76 // ResourceTrackerTest --------------------------------------------------------- | 90 // ResourceTrackerTest --------------------------------------------------------- |
| 77 | 91 |
| 78 class ResourceTrackerTest : public PpapiUnittest { | 92 class ResourceTrackerTest : public PpapiUnittest { |
| 79 public: | 93 public: |
| 80 ResourceTrackerTest() { | 94 ResourceTrackerTest() { |
| 81 } | 95 } |
| 82 | 96 |
| 83 virtual void SetUp() { | 97 virtual void SetUp() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 190 } |
| 177 | 191 |
| 178 TEST_F(ResourceTrackerTest, DeleteObjectVarWithInstance) { | 192 TEST_F(ResourceTrackerTest, DeleteObjectVarWithInstance) { |
| 179 // Make a second instance (the test harness already creates & manages one). | 193 // Make a second instance (the test harness already creates & manages one). |
| 180 scoped_refptr<PluginInstance> instance2( | 194 scoped_refptr<PluginInstance> instance2( |
| 181 PluginInstance::Create1_0(delegate(), module(), | 195 PluginInstance::Create1_0(delegate(), module(), |
| 182 GetMockInterface(PPP_INSTANCE_INTERFACE_1_0))); | 196 GetMockInterface(PPP_INSTANCE_INTERFACE_1_0))); |
| 183 PP_Instance pp_instance2 = instance2->pp_instance(); | 197 PP_Instance pp_instance2 = instance2->pp_instance(); |
| 184 | 198 |
| 185 // Make an object var. | 199 // Make an object var. |
| 186 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); | 200 NPObjectReleaser npobject(NewTrackedNPObject()); |
| 187 NPObjectToPPVar(instance2.get(), npobject.get()); | 201 NPObjectToPPVar(instance2.get(), npobject.get()); |
| 188 | 202 |
| 189 EXPECT_EQ(1, g_npobjects_alive); | 203 EXPECT_EQ(1, g_npobjects_alive); |
| 190 EXPECT_EQ(1u, tracker().GetLiveObjectsForInstance(pp_instance2)); | 204 EXPECT_EQ(1u, tracker().GetLiveObjectsForInstance(pp_instance2)); |
| 191 | 205 |
| 192 // Free the instance, this should release the ObjectVar. | 206 // Free the instance, this should release the ObjectVar. |
| 193 instance2 = NULL; | 207 instance2 = NULL; |
| 194 EXPECT_EQ(0u, tracker().GetLiveObjectsForInstance(pp_instance2)); | 208 EXPECT_EQ(0u, tracker().GetLiveObjectsForInstance(pp_instance2)); |
| 195 } | 209 } |
| 196 | 210 |
| 197 // Make sure that using the same NPObject should give the same PP_Var | 211 // Make sure that using the same NPObject should give the same PP_Var |
| 198 // each time. | 212 // each time. |
| 199 TEST_F(ResourceTrackerTest, ReuseVar) { | 213 TEST_F(ResourceTrackerTest, ReuseVar) { |
| 200 scoped_ptr<NPObject> npobject(NewTrackedNPObject()); | 214 NPObjectReleaser npobject(NewTrackedNPObject()); |
| 201 | 215 |
| 202 PP_Var pp_object1 = NPObjectToPPVar(instance(), npobject.get()); | 216 PP_Var pp_object1 = NPObjectToPPVar(instance(), npobject.get()); |
| 203 PP_Var pp_object2 = NPObjectToPPVar(instance(), npobject.get()); | 217 PP_Var pp_object2 = NPObjectToPPVar(instance(), npobject.get()); |
| 204 | 218 |
| 205 // The two results should be the same. | 219 // The two results should be the same. |
| 206 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); | 220 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); |
| 207 | 221 |
| 208 // The objects should be able to get us back to the associated NPObject. | 222 // The objects should be able to get us back to the associated NPObject. |
| 209 // This ObjectVar must be released before we do NPObjectToPPVar again | 223 // This ObjectVar must be released before we do NPObjectToPPVar again |
| 210 // below so it gets freed and we get a new identifier. | 224 // below so it gets freed and we get a new identifier. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 222 | 236 |
| 223 // Releasing the resource should free the internal ref, and so making a new | 237 // Releasing the resource should free the internal ref, and so making a new |
| 224 // one now should generate a new ID. | 238 // one now should generate a new ID. |
| 225 PP_Var pp_object3 = NPObjectToPPVar(instance(), npobject.get()); | 239 PP_Var pp_object3 = NPObjectToPPVar(instance(), npobject.get()); |
| 226 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); | 240 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)); | 241 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); |
| 228 } | 242 } |
| 229 | 243 |
| 230 } // namespace ppapi | 244 } // namespace ppapi |
| 231 } // namespace webkit | 245 } // namespace webkit |
| OLD | NEW |