This source file includes following definitions.
- LastPluginRefWasDeleted
- InstanceWasDeleted
- SetUp
- TearDown
- resource_tracker
- TEST_F
- TEST_F
- TEST_F
#include "testing/gtest/include/gtest/gtest.h"
#include "base/compiler_specific.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/test_globals.h"
namespace ppapi {
namespace {
int mock_resource_alive_count = 0;
int last_plugin_ref_was_deleted_count = 0;
int instance_was_deleted_count = 0;
class MyMockResource : public Resource {
public:
MyMockResource(PP_Instance instance) : Resource(OBJECT_IS_IMPL, instance) {
mock_resource_alive_count++;
}
virtual ~MyMockResource() { mock_resource_alive_count--; }
virtual void LastPluginRefWasDeleted() OVERRIDE {
last_plugin_ref_was_deleted_count++;
}
virtual void InstanceWasDeleted() OVERRIDE { instance_was_deleted_count++; }
};
}
class ResourceTrackerTest : public testing::Test {
public:
ResourceTrackerTest() {}
virtual void SetUp() OVERRIDE {
ASSERT_EQ(0, mock_resource_alive_count);
last_plugin_ref_was_deleted_count = 0;
instance_was_deleted_count = 0;
}
virtual void TearDown() OVERRIDE {}
ResourceTracker& resource_tracker() { return *globals_.GetResourceTracker(); }
private:
TestGlobals globals_;
};
TEST_F(ResourceTrackerTest, LastPluginRef) {
PP_Instance instance = 0x1234567;
ProxyAutoLock lock;
resource_tracker().DidCreateInstance(instance);
scoped_refptr<MyMockResource> resource(new MyMockResource(instance));
PP_Resource pp_resource = resource->GetReference();
EXPECT_TRUE(resource_tracker().GetResource(pp_resource));
resource_tracker().ReleaseResource(pp_resource);
EXPECT_EQ(1, last_plugin_ref_was_deleted_count);
EXPECT_EQ(1, mock_resource_alive_count);
resource_tracker().DidDeleteInstance(instance);
resource = NULL;
EXPECT_FALSE(resource_tracker().GetResource(pp_resource));
}
TEST_F(ResourceTrackerTest, InstanceDeletedWithPluginRef) {
PP_Instance instance = 0x2345678;
ProxyAutoLock lock;
resource_tracker().DidCreateInstance(instance);
MyMockResource* resource = new MyMockResource(instance);
resource->GetReference();
EXPECT_EQ(1, mock_resource_alive_count);
resource_tracker().DidDeleteInstance(instance);
EXPECT_EQ(0, mock_resource_alive_count);
EXPECT_EQ(1, last_plugin_ref_was_deleted_count);
EXPECT_EQ(0, instance_was_deleted_count);
}
TEST_F(ResourceTrackerTest, InstanceDeletedWithBothRefed) {
PP_Instance instance = 0x3456789;
ProxyAutoLock lock;
resource_tracker().DidCreateInstance(instance);
scoped_refptr<MyMockResource> resource = new MyMockResource(instance);
resource->GetReference();
EXPECT_EQ(1, mock_resource_alive_count);
resource_tracker().DidDeleteInstance(instance);
EXPECT_EQ(1, mock_resource_alive_count);
EXPECT_EQ(1, last_plugin_ref_was_deleted_count);
EXPECT_EQ(1, instance_was_deleted_count);
EXPECT_EQ(0, resource->pp_instance());
resource = NULL;
EXPECT_EQ(0, mock_resource_alive_count);
}
}