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