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/resource.h"
9 #include "ppapi/shared_impl/resource_tracker.h"
10 #include "ppapi/shared_impl/test_globals.h"
16 int mock_resource_alive_count
= 0;
17 int last_plugin_ref_was_deleted_count
= 0;
18 int instance_was_deleted_count
= 0;
20 class MyMockResource
: public Resource
{
22 MyMockResource(PP_Instance instance
) : Resource(OBJECT_IS_IMPL
, instance
) {
23 mock_resource_alive_count
++;
25 virtual ~MyMockResource() {
26 mock_resource_alive_count
--;
29 virtual void LastPluginRefWasDeleted() OVERRIDE
{
30 last_plugin_ref_was_deleted_count
++;
32 virtual void InstanceWasDeleted() OVERRIDE
{
33 instance_was_deleted_count
++;
39 class ResourceTrackerTest
: public testing::Test
{
41 ResourceTrackerTest() {}
43 // Test implementation.
44 virtual void SetUp() OVERRIDE
{
45 ASSERT_EQ(0, mock_resource_alive_count
);
46 last_plugin_ref_was_deleted_count
= 0;
47 instance_was_deleted_count
= 0;
49 virtual void TearDown() OVERRIDE
{
52 ResourceTracker
& resource_tracker() { return *globals_
.GetResourceTracker(); }
58 // Test that LastPluginRefWasDeleted is called when the last plugin ref was
59 // deleted but the object lives on.
60 TEST_F(ResourceTrackerTest
, LastPluginRef
) {
61 PP_Instance instance
= 0x1234567;
62 resource_tracker().DidCreateInstance(instance
);
64 scoped_refptr
<MyMockResource
> resource(new MyMockResource(instance
));
65 PP_Resource pp_resource
= resource
->GetReference();
66 EXPECT_TRUE(resource_tracker().GetResource(pp_resource
));
68 // Releasing it should keep the object (because we have a ref) but fire the
69 // "last plugin ref" message.
70 resource_tracker().ReleaseResource(pp_resource
);
71 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
72 EXPECT_EQ(1, mock_resource_alive_count
);
74 resource_tracker().DidDeleteInstance(instance
);
76 EXPECT_FALSE(resource_tracker().GetResource(pp_resource
));
79 // Tests when the plugin is holding a ref to a resource when the instance is
81 TEST_F(ResourceTrackerTest
, InstanceDeletedWithPluginRef
) {
82 // Make a resource with one ref held by the plugin, and delete the instance.
83 PP_Instance instance
= 0x2345678;
84 resource_tracker().DidCreateInstance(instance
);
85 MyMockResource
* resource
= new MyMockResource(instance
);
86 resource
->GetReference();
87 EXPECT_EQ(1, mock_resource_alive_count
);
88 resource_tracker().DidDeleteInstance(instance
);
90 // The resource should have been deleted, and before it was, it should have
91 // received a "last plugin ref was deleted" notification.
92 EXPECT_EQ(0, mock_resource_alive_count
);
93 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
94 EXPECT_EQ(0, instance_was_deleted_count
);
97 // Test when the plugin and the internal implementation (via scoped_refptr) is
98 // holding a ref to a resource when the instance is deleted.
99 TEST_F(ResourceTrackerTest
, InstanceDeletedWithBothRefed
) {
100 // Create a new instance.
101 PP_Instance instance
= 0x3456789;
103 // Make a resource with one ref held by the plugin and one ref held by us
104 // (outlives the plugin), and delete the instance.
105 resource_tracker().DidCreateInstance(instance
);
106 scoped_refptr
<MyMockResource
> resource
= new MyMockResource(instance
);
107 resource
->GetReference();
108 EXPECT_EQ(1, mock_resource_alive_count
);
109 resource_tracker().DidDeleteInstance(instance
);
111 // The resource should NOT have been deleted, and it should have received both
112 // a "last plugin ref was deleted" and a "instance was deleted" notification.
113 EXPECT_EQ(1, mock_resource_alive_count
);
114 EXPECT_EQ(1, last_plugin_ref_was_deleted_count
);
115 EXPECT_EQ(1, instance_was_deleted_count
);
116 EXPECT_EQ(0, resource
->pp_instance());
119 EXPECT_EQ(0, mock_resource_alive_count
);