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 "testing/gtest/include/gtest/gtest.h"
7 #include "base/compiler_specific.h"
8 #include "ppapi/shared_impl/proxy_lock.h"
9 #include "ppapi/shared_impl/resource.h"
10 #include "ppapi/shared_impl/resource_tracker.h"
11 #include "ppapi/shared_impl/test_globals.h"
17 int mock_resource_alive_count
= 0;
18 int last_plugin_ref_was_deleted_count
= 0;
19 int instance_was_deleted_count
= 0;
21 class MyMockResource
: public Resource
{
23 MyMockResource(PP_Instance instance
) : Resource(OBJECT_IS_IMPL
, instance
) {
24 mock_resource_alive_count
++;
26 virtual ~MyMockResource() {
27 mock_resource_alive_count
--;
30 virtual void LastPluginRefWasDeleted() OVERRIDE
{
31 last_plugin_ref_was_deleted_count
++;
33 virtual void InstanceWasDeleted() OVERRIDE
{
34 instance_was_deleted_count
++;
40 class ResourceTrackerTest
: public testing::Test
{
42 ResourceTrackerTest() {}
44 // Test implementation.
45 virtual void SetUp() OVERRIDE
{
46 ASSERT_EQ(0, mock_resource_alive_count
);
47 last_plugin_ref_was_deleted_count
= 0;
48 instance_was_deleted_count
= 0;
50 virtual void TearDown() OVERRIDE
{
53 ResourceTracker
& resource_tracker() { return *globals_
.GetResourceTracker(); }
59 // Test that LastPluginRefWasDeleted is called when the last plugin ref was
60 // deleted but the object lives on.
61 TEST_F(ResourceTrackerTest
, LastPluginRef
) {
62 PP_Instance instance
= 0x1234567;
64 resource_tracker().DidCreateInstance(instance
);
66 scoped_refptr
<MyMockResource
> resource(new MyMockResource(instance
));
67 PP_Resource pp_resource
= resource
->GetReference();
68 EXPECT_TRUE(resource_tracker().GetResource(pp_resource
));
70 // Releasing it should keep the object (because we have a ref) but fire the
71 // "last plugin ref" message.
72 resource_tracker().ReleaseResource(pp_resource
);
73 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
74 EXPECT_EQ(1, mock_resource_alive_count
);
76 resource_tracker().DidDeleteInstance(instance
);
78 EXPECT_FALSE(resource_tracker().GetResource(pp_resource
));
81 // Tests when the plugin is holding a ref to a resource when the instance is
83 TEST_F(ResourceTrackerTest
, InstanceDeletedWithPluginRef
) {
84 // Make a resource with one ref held by the plugin, and delete the instance.
85 PP_Instance instance
= 0x2345678;
87 resource_tracker().DidCreateInstance(instance
);
88 MyMockResource
* resource
= new MyMockResource(instance
);
89 resource
->GetReference();
90 EXPECT_EQ(1, mock_resource_alive_count
);
91 resource_tracker().DidDeleteInstance(instance
);
93 // The resource should have been deleted, and before it was, it should have
94 // received a "last plugin ref was deleted" notification.
95 EXPECT_EQ(0, mock_resource_alive_count
);
96 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
97 EXPECT_EQ(0, instance_was_deleted_count
);
100 // Test when the plugin and the internal implementation (via scoped_refptr) is
101 // holding a ref to a resource when the instance is deleted.
102 TEST_F(ResourceTrackerTest
, InstanceDeletedWithBothRefed
) {
103 // Create a new instance.
104 PP_Instance instance
= 0x3456789;
107 // Make a resource with one ref held by the plugin and one ref held by us
108 // (outlives the plugin), and delete the instance.
109 resource_tracker().DidCreateInstance(instance
);
110 scoped_refptr
<MyMockResource
> resource
= new MyMockResource(instance
);
111 resource
->GetReference();
112 EXPECT_EQ(1, mock_resource_alive_count
);
113 resource_tracker().DidDeleteInstance(instance
);
115 // The resource should NOT have been deleted, and it should have received both
116 // a "last plugin ref was deleted" and a "instance was deleted" notification.
117 EXPECT_EQ(1, mock_resource_alive_count
);
118 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
119 EXPECT_EQ(1, instance_was_deleted_count
);
120 EXPECT_EQ(0, resource
->pp_instance());
123 EXPECT_EQ(0, mock_resource_alive_count
);