This source file includes following definitions.
- HasValidVarID
- GetType
- HasValidVarID
- SetUp
- TearDown
- var_tracker
- TEST_F
- 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/var.h"
#include "ppapi/shared_impl/var_tracker.h"
#include "ppapi/shared_impl/test_globals.h"
namespace ppapi {
namespace {
int mock_var_alive_count = 0;
class MockStringVar : public StringVar {
public:
MockStringVar(const std::string& str) : StringVar(str) {
mock_var_alive_count++;
}
virtual ~MockStringVar() { mock_var_alive_count--; }
bool HasValidVarID() { return GetExistingVarID() != 0; }
};
class MockObjectVar : public Var {
public:
MockObjectVar() : Var() { mock_var_alive_count++; }
virtual ~MockObjectVar() { mock_var_alive_count--; }
virtual PP_VarType GetType() const OVERRIDE { return PP_VARTYPE_OBJECT; }
bool HasValidVarID() { return GetExistingVarID() != 0; }
};
}
class VarTrackerTest : public testing::Test {
public:
VarTrackerTest() {}
virtual void SetUp() OVERRIDE {
ASSERT_EQ(0, mock_var_alive_count);
ProxyLock::EnableLockingOnThreadForTest();
}
virtual void TearDown() OVERRIDE {}
VarTracker& var_tracker() { return *globals_.GetVarTracker(); }
private:
TestGlobals globals_;
};
TEST_F(VarTrackerTest, LastResourceRef) {
ProxyAutoLock lock;
scoped_refptr<MockStringVar> var(new MockStringVar(std::string("xyz")));
PP_Var pp_var = var->GetPPVar();
EXPECT_TRUE(var->HasValidVarID());
EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
EXPECT_FALSE(var->HasValidVarID());
EXPECT_EQ(1, mock_var_alive_count);
var = NULL;
EXPECT_EQ(0, mock_var_alive_count);
}
TEST_F(VarTrackerTest, GetPluginRefAgain) {
ProxyAutoLock lock;
scoped_refptr<MockStringVar> var(new MockStringVar(std::string("xyz")));
PP_Var pp_var = var->GetPPVar();
EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
EXPECT_FALSE(var->HasValidVarID());
EXPECT_EQ(1, mock_var_alive_count);
pp_var = var->GetPPVar();
EXPECT_TRUE(var->HasValidVarID());
EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
scoped_refptr<MockStringVar> another_var =
static_cast<MockStringVar*>(var_tracker().GetVar(pp_var));
EXPECT_EQ(1, mock_var_alive_count);
EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
EXPECT_FALSE(var->HasValidVarID());
EXPECT_EQ(1, mock_var_alive_count);
var = NULL;
EXPECT_FALSE(var_tracker().GetVar(pp_var));
EXPECT_EQ(1, mock_var_alive_count);
another_var = NULL;
EXPECT_FALSE(var_tracker().GetVar(pp_var));
EXPECT_EQ(0, mock_var_alive_count);
}
TEST_F(VarTrackerTest, PluginRefWithoutVarRef) {
ProxyAutoLock lock;
scoped_refptr<MockStringVar> var(new MockStringVar(std::string("zzz")));
PP_Var pp_var = var->GetPPVar();
EXPECT_EQ(1, mock_var_alive_count);
var = NULL;
EXPECT_EQ(1, mock_var_alive_count);
EXPECT_TRUE(var_tracker().GetVar(pp_var));
var_tracker().ReleaseVar(pp_var);
EXPECT_EQ(0, mock_var_alive_count);
EXPECT_FALSE(var_tracker().GetVar(pp_var));
}
TEST_F(VarTrackerTest, ObjectRef) {
ProxyAutoLock lock;
scoped_refptr<MockObjectVar> var(new MockObjectVar());
PP_Var pp_var = var->GetPPVar();
EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
EXPECT_FALSE(var->HasValidVarID());
EXPECT_EQ(1, mock_var_alive_count);
pp_var = var->GetPPVar();
EXPECT_TRUE(var->HasValidVarID());
EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
scoped_refptr<MockObjectVar> another_var =
static_cast<MockObjectVar*>(var_tracker().GetVar(pp_var));
EXPECT_EQ(1, mock_var_alive_count);
var = NULL;
EXPECT_TRUE(var_tracker().GetVar(pp_var));
EXPECT_EQ(1, mock_var_alive_count);
another_var = NULL;
EXPECT_TRUE(var_tracker().GetVar(pp_var));
EXPECT_EQ(1, mock_var_alive_count);
EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
EXPECT_EQ(0, mock_var_alive_count);
}
}