| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/plugins/ppapi/ppapi_unittest.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "ppapi/c/pp_var.h" | |
| 9 #include "ppapi/c/ppp_instance.h" | |
| 10 #include "third_party/npapi/bindings/npruntime.h" | |
| 11 #include "third_party/WebKit/public/web/WebBindings.h" | |
| 12 #include "webkit/plugins/ppapi/host_globals.h" | |
| 13 #include "webkit/plugins/ppapi/host_var_tracker.h" | |
| 14 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" | |
| 15 #include "webkit/plugins/ppapi/mock_resource.h" | |
| 16 #include "webkit/plugins/ppapi/npapi_glue.h" | |
| 17 #include "webkit/plugins/ppapi/npobject_var.h" | |
| 18 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h" | |
| 19 | |
| 20 using ppapi::NPObjectVar; | |
| 21 | |
| 22 namespace webkit { | |
| 23 namespace ppapi { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Tracked NPObjects ----------------------------------------------------------- | |
| 28 | |
| 29 int g_npobjects_alive = 0; | |
| 30 | |
| 31 void TrackedClassDeallocate(NPObject* npobject) { | |
| 32 g_npobjects_alive--; | |
| 33 delete npobject; | |
| 34 } | |
| 35 | |
| 36 NPClass g_tracked_npclass = { | |
| 37 NP_CLASS_STRUCT_VERSION, | |
| 38 NULL, | |
| 39 &TrackedClassDeallocate, | |
| 40 NULL, | |
| 41 NULL, | |
| 42 NULL, | |
| 43 NULL, | |
| 44 NULL, | |
| 45 NULL, | |
| 46 NULL, | |
| 47 NULL, | |
| 48 NULL, | |
| 49 }; | |
| 50 | |
| 51 // Returns a new tracked NPObject with a refcount of 1. You'll want to put this | |
| 52 // in a NPObjectReleaser to free this ref when the test completes. | |
| 53 NPObject* NewTrackedNPObject() { | |
| 54 NPObject* object = new NPObject; | |
| 55 object->_class = &g_tracked_npclass; | |
| 56 object->referenceCount = 1; | |
| 57 | |
| 58 g_npobjects_alive++; | |
| 59 return object; | |
| 60 } | |
| 61 | |
| 62 class ReleaseNPObject { | |
| 63 public: | |
| 64 void operator()(NPObject* o) const { | |
| 65 WebKit::WebBindings::releaseObject(o); | |
| 66 } | |
| 67 }; | |
| 68 | |
| 69 // Handles automatically releasing a reference to the NPObject on destruction. | |
| 70 // It's assumed the input has a ref already taken. | |
| 71 typedef scoped_ptr_malloc<NPObject, ReleaseNPObject> NPObjectReleaser; | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 class HostVarTrackerTest : public PpapiUnittest { | |
| 76 public: | |
| 77 HostVarTrackerTest() { | |
| 78 } | |
| 79 | |
| 80 HostVarTracker& tracker() { | |
| 81 return *HostGlobals::Get()->host_var_tracker(); | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) { | |
| 86 // Make a second instance (the test harness already creates & manages one). | |
| 87 scoped_refptr<PluginInstanceImpl> instance2( | |
| 88 PluginInstanceImpl::Create(delegate(), NULL, module(), NULL, GURL())); | |
| 89 PP_Instance pp_instance2 = instance2->pp_instance(); | |
| 90 | |
| 91 // Make an object var. | |
| 92 NPObjectReleaser npobject(NewTrackedNPObject()); | |
| 93 NPObjectToPPVarForTest(instance2.get(), npobject.get()); | |
| 94 | |
| 95 EXPECT_EQ(1, g_npobjects_alive); | |
| 96 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); | |
| 97 | |
| 98 // Free the instance, this should release the ObjectVar. | |
| 99 instance2 = NULL; | |
| 100 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); | |
| 101 } | |
| 102 | |
| 103 // Make sure that using the same NPObject should give the same PP_Var | |
| 104 // each time. | |
| 105 TEST_F(HostVarTrackerTest, ReuseVar) { | |
| 106 NPObjectReleaser npobject(NewTrackedNPObject()); | |
| 107 | |
| 108 PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get()); | |
| 109 PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get()); | |
| 110 | |
| 111 // The two results should be the same. | |
| 112 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); | |
| 113 | |
| 114 // The objects should be able to get us back to the associated NPObject. | |
| 115 // This ObjectVar must be released before we do NPObjectToPPVarForTest again | |
| 116 // below so it gets freed and we get a new identifier. | |
| 117 { | |
| 118 scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1)); | |
| 119 ASSERT_TRUE(check_object.get()); | |
| 120 EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance()); | |
| 121 EXPECT_EQ(npobject.get(), check_object->np_object()); | |
| 122 } | |
| 123 | |
| 124 // Remove both of the refs we made above. | |
| 125 ::ppapi::VarTracker* var_tracker = | |
| 126 ::ppapi::PpapiGlobals::Get()->GetVarTracker(); | |
| 127 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id)); | |
| 128 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id)); | |
| 129 | |
| 130 // Releasing the resource should free the internal ref, and so making a new | |
| 131 // one now should generate a new ID. | |
| 132 PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get()); | |
| 133 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); | |
| 134 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); | |
| 135 } | |
| 136 | |
| 137 } // namespace ppapi | |
| 138 } // namespace webkit | |
| OLD | NEW |