This source file includes following definitions.
- TrackedClassDeallocate
- NewTrackedNPObject
- tracker
- TEST_F
- TEST_F
#include "base/memory/scoped_ptr.h"
#include "content/renderer/pepper/host_globals.h"
#include "content/renderer/pepper/host_var_tracker.h"
#include "content/renderer/pepper/mock_resource.h"
#include "content/renderer/pepper/npapi_glue.h"
#include "content/renderer/pepper/npobject_var.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/test/ppapi_unittest.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/ppp_instance.h"
#include "third_party/npapi/bindings/npruntime.h"
#include "third_party/WebKit/public/web/WebBindings.h"
using ppapi::NPObjectVar;
namespace content {
namespace {
int g_npobjects_alive = 0;
void TrackedClassDeallocate(NPObject* npobject) {
g_npobjects_alive--;
delete npobject;
}
NPClass g_tracked_npclass = {
NP_CLASS_STRUCT_VERSION,
NULL,
&TrackedClassDeallocate,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
NPObject* NewTrackedNPObject() {
NPObject* object = new NPObject;
object->_class = &g_tracked_npclass;
object->referenceCount = 1;
g_npobjects_alive++;
return object;
}
struct ReleaseNPObject {
void operator()(NPObject* o) const {
blink::WebBindings::releaseObject(o);
}
};
typedef scoped_ptr<NPObject, ReleaseNPObject> NPObjectReleaser;
}
class HostVarTrackerTest : public PpapiUnittest {
public:
HostVarTrackerTest() {
}
HostVarTracker& tracker() {
return *HostGlobals::Get()->host_var_tracker();
}
};
TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) {
scoped_refptr<PepperPluginInstanceImpl> instance2(
PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL()));
PP_Instance pp_instance2 = instance2->pp_instance();
NPObjectReleaser npobject(NewTrackedNPObject());
NPObjectToPPVarForTest(instance2.get(), npobject.get());
EXPECT_EQ(1, g_npobjects_alive);
EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
instance2 = NULL;
EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
}
TEST_F(HostVarTrackerTest, ReuseVar) {
NPObjectReleaser npobject(NewTrackedNPObject());
PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get());
PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get());
EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id);
{
scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1));
ASSERT_TRUE(check_object.get());
EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance());
EXPECT_EQ(npobject.get(), check_object->np_object());
}
ppapi::VarTracker* var_tracker =
ppapi::PpapiGlobals::Get()->GetVarTracker();
var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id));
var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id));
PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get());
EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id);
var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id));
}
}