1 // Copyright (c) 2011 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/var.h"
10 #include "ppapi/shared_impl/var_tracker.h"
11 #include "ppapi/shared_impl/test_globals.h"
17 int mock_var_alive_count
= 0;
19 class MockStringVar
: public StringVar
{
21 MockStringVar(const std::string
& str
) : StringVar(str
) {
22 mock_var_alive_count
++;
24 virtual ~MockStringVar() {
25 mock_var_alive_count
--;
27 bool HasValidVarID() {
28 return GetExistingVarID() != 0;
32 class MockObjectVar
: public Var
{
34 MockObjectVar() : Var() {
35 mock_var_alive_count
++;
37 virtual ~MockObjectVar() {
38 mock_var_alive_count
--;
40 virtual PP_VarType
GetType() const OVERRIDE
{
41 return PP_VARTYPE_OBJECT
;
43 bool HasValidVarID() {
44 return GetExistingVarID() != 0;
50 class VarTrackerTest
: public testing::Test
{
54 // Test implementation.
55 virtual void SetUp() OVERRIDE
{
56 ASSERT_EQ(0, mock_var_alive_count
);
57 ProxyLock::EnableLockingOnThreadForTest();
59 virtual void TearDown() OVERRIDE
{
62 VarTracker
& var_tracker() { return *globals_
.GetVarTracker(); }
68 // Test that ResetVarID is called when the last PP_Var ref was deleted but the
70 TEST_F(VarTrackerTest
, LastResourceRef
) {
72 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("xyz")));
73 PP_Var pp_var
= var
->GetPPVar();
74 EXPECT_TRUE(var
->HasValidVarID());
75 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
77 // Releasing it should keep the object (because we have a ref) but reset the
79 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
80 EXPECT_FALSE(var
->HasValidVarID());
81 EXPECT_EQ(1, mock_var_alive_count
);
84 EXPECT_EQ(0, mock_var_alive_count
);
87 TEST_F(VarTrackerTest
, GetPluginRefAgain
) {
89 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("xyz")));
90 PP_Var pp_var
= var
->GetPPVar();
91 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
92 EXPECT_FALSE(var
->HasValidVarID());
93 EXPECT_EQ(1, mock_var_alive_count
);
95 // Obtaining PP_Var ref again, and add ref from VarTracker.
96 pp_var
= var
->GetPPVar();
97 EXPECT_TRUE(var
->HasValidVarID());
98 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
99 scoped_refptr
<MockStringVar
> another_var
=
100 static_cast<MockStringVar
*>(var_tracker().GetVar(pp_var
));
101 EXPECT_EQ(1, mock_var_alive_count
);
103 // Releasing it again.
104 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
105 EXPECT_FALSE(var
->HasValidVarID());
106 EXPECT_EQ(1, mock_var_alive_count
);
109 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
110 EXPECT_EQ(1, mock_var_alive_count
);
112 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
113 EXPECT_EQ(0, mock_var_alive_count
);
116 // Tests when the plugin is holding a ref to a PP_Var when the instance is
117 // owned only by VarTracker.
118 TEST_F(VarTrackerTest
, PluginRefWithoutVarRef
) {
120 // Make a PP_Var with one ref held by the plugin, and release the reference.
121 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("zzz")));
122 PP_Var pp_var
= var
->GetPPVar();
123 EXPECT_EQ(1, mock_var_alive_count
);
125 EXPECT_EQ(1, mock_var_alive_count
);
127 // The var is owned only by VarTracker. PP_Var must be still valid.
128 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
130 var_tracker().ReleaseVar(pp_var
);
131 EXPECT_EQ(0, mock_var_alive_count
);
132 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
135 // Tests on Var having type of PP_VARTYPE_OBJECT.
136 TEST_F(VarTrackerTest
, ObjectRef
) {
138 scoped_refptr
<MockObjectVar
> var(new MockObjectVar());
139 PP_Var pp_var
= var
->GetPPVar();
140 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
141 EXPECT_FALSE(var
->HasValidVarID());
142 EXPECT_EQ(1, mock_var_alive_count
);
144 // Obtaining PP_Var ref again, and add ref from VarTracker.
145 pp_var
= var
->GetPPVar();
146 EXPECT_TRUE(var
->HasValidVarID());
147 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
148 scoped_refptr
<MockObjectVar
> another_var
=
149 static_cast<MockObjectVar
*>(var_tracker().GetVar(pp_var
));
150 EXPECT_EQ(1, mock_var_alive_count
);
152 // Releasing all references, then only VarTracker own the instance.
154 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
155 EXPECT_EQ(1, mock_var_alive_count
);
157 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
158 EXPECT_EQ(1, mock_var_alive_count
);
160 // Releasing plugin reference.
161 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
162 EXPECT_EQ(0, mock_var_alive_count
);