Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / plugins / ppapi / host_var_tracker_unittest.cc
blobf2be72c4332dd058e8d55f0785571d7deb38795a
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.
5 #include "webkit/plugins/ppapi/ppapi_unittest.h"
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/Source/WebKit/chromium/public/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.h"
20 using ppapi::NPObjectVar;
22 namespace webkit {
23 namespace ppapi {
25 namespace {
27 // Tracked NPObjects -----------------------------------------------------------
29 int g_npobjects_alive = 0;
31 void TrackedClassDeallocate(NPObject* npobject) {
32 g_npobjects_alive--;
33 delete npobject;
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,
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;
58 g_npobjects_alive++;
59 return object;
62 class ReleaseNPObject {
63 public:
64 void operator()(NPObject* o) const {
65 WebKit::WebBindings::releaseObject(o);
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;
73 } // namespace
75 class HostVarTrackerTest : public PpapiUnittest {
76 public:
77 HostVarTrackerTest() {
80 HostVarTracker& tracker() {
81 return *HostGlobals::Get()->host_var_tracker();
85 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) {
86 // Make a second instance (the test harness already creates & manages one).
87 scoped_refptr<PluginInstance> instance2(
88 PluginInstance::Create(delegate(), module(), NULL, GURL()));
89 PP_Instance pp_instance2 = instance2->pp_instance();
91 // Make an object var.
92 NPObjectReleaser npobject(NewTrackedNPObject());
93 NPObjectToPPVar(instance2.get(), npobject.get());
95 EXPECT_EQ(1, g_npobjects_alive);
96 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
98 // Free the instance, this should release the ObjectVar.
99 instance2 = NULL;
100 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
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());
108 PP_Var pp_object1 = NPObjectToPPVar(instance(), npobject.get());
109 PP_Var pp_object2 = NPObjectToPPVar(instance(), npobject.get());
111 // The two results should be the same.
112 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id);
114 // The objects should be able to get us back to the associated NPObject.
115 // This ObjectVar must be released before we do NPObjectToPPVar again
116 // below so it gets freed and we get a new identifier.
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());
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));
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 = NPObjectToPPVar(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));
137 } // namespace ppapi
138 } // namespace webkit