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 ~MyMockResource() override
{ mock_resource_alive_count
--; }
28 void LastPluginRefWasDeleted() override
{
29 last_plugin_ref_was_deleted_count
++;
31 void InstanceWasDeleted() override
{ instance_was_deleted_count
++; }
36 class ResourceTrackerTest
: public testing::Test
{
38 ResourceTrackerTest() {}
40 // Test implementation.
41 void SetUp() override
{
42 ASSERT_EQ(0, mock_resource_alive_count
);
43 last_plugin_ref_was_deleted_count
= 0;
44 instance_was_deleted_count
= 0;
46 void TearDown() override
{}
48 ResourceTracker
& resource_tracker() { return *globals_
.GetResourceTracker(); }
54 // Test that LastPluginRefWasDeleted is called when the last plugin ref was
55 // deleted but the object lives on.
56 TEST_F(ResourceTrackerTest
, LastPluginRef
) {
57 PP_Instance instance
= 0x1234567;
59 resource_tracker().DidCreateInstance(instance
);
61 scoped_refptr
<MyMockResource
> resource(new MyMockResource(instance
));
62 PP_Resource pp_resource
= resource
->GetReference();
63 EXPECT_TRUE(resource_tracker().GetResource(pp_resource
));
65 // Releasing it should keep the object (because we have a ref) but fire the
66 // "last plugin ref" message.
67 resource_tracker().ReleaseResource(pp_resource
);
68 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
69 EXPECT_EQ(1, mock_resource_alive_count
);
71 resource_tracker().DidDeleteInstance(instance
);
73 EXPECT_FALSE(resource_tracker().GetResource(pp_resource
));
76 // Tests when the plugin is holding a ref to a resource when the instance is
78 TEST_F(ResourceTrackerTest
, InstanceDeletedWithPluginRef
) {
79 // Make a resource with one ref held by the plugin, and delete the instance.
80 PP_Instance instance
= 0x2345678;
82 resource_tracker().DidCreateInstance(instance
);
83 MyMockResource
* resource
= new MyMockResource(instance
);
84 resource
->GetReference();
85 EXPECT_EQ(1, mock_resource_alive_count
);
86 resource_tracker().DidDeleteInstance(instance
);
88 // The resource should have been deleted, and before it was, it should have
89 // received a "last plugin ref was deleted" notification.
90 EXPECT_EQ(0, mock_resource_alive_count
);
91 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
92 EXPECT_EQ(0, instance_was_deleted_count
);
95 // Test when the plugin and the internal implementation (via scoped_refptr) is
96 // holding a ref to a resource when the instance is deleted.
97 TEST_F(ResourceTrackerTest
, InstanceDeletedWithBothRefed
) {
98 // Create a new instance.
99 PP_Instance instance
= 0x3456789;
102 // Make a resource with one ref held by the plugin and one ref held by us
103 // (outlives the plugin), and delete the instance.
104 resource_tracker().DidCreateInstance(instance
);
105 scoped_refptr
<MyMockResource
> resource
= new MyMockResource(instance
);
106 resource
->GetReference();
107 EXPECT_EQ(1, mock_resource_alive_count
);
108 resource_tracker().DidDeleteInstance(instance
);
110 // The resource should NOT have been deleted, and it should have received both
111 // a "last plugin ref was deleted" and a "instance was deleted" notification.
112 EXPECT_EQ(1, mock_resource_alive_count
);
113 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
114 EXPECT_EQ(1, instance_was_deleted_count
);
115 EXPECT_EQ(0, resource
->pp_instance());
118 EXPECT_EQ(0, mock_resource_alive_count
);