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 ~MockStringVar() override
{ mock_var_alive_count
--; }
25 bool HasValidVarID() { return GetExistingVarID() != 0; }
28 class MockObjectVar
: public Var
{
30 MockObjectVar() : Var() { mock_var_alive_count
++; }
31 ~MockObjectVar() override
{ mock_var_alive_count
--; }
32 PP_VarType
GetType() const override
{ return PP_VARTYPE_OBJECT
; }
33 bool HasValidVarID() { return GetExistingVarID() != 0; }
38 class VarTrackerTest
: public testing::Test
{
42 // Test implementation.
43 void SetUp() override
{
44 ASSERT_EQ(0, mock_var_alive_count
);
46 void TearDown() override
{}
48 VarTracker
& var_tracker() { return *globals_
.GetVarTracker(); }
54 // Test that ResetVarID is called when the last PP_Var ref was deleted but the
56 TEST_F(VarTrackerTest
, LastResourceRef
) {
58 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("xyz")));
59 PP_Var pp_var
= var
->GetPPVar();
60 EXPECT_TRUE(var
->HasValidVarID());
61 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
63 // Releasing it should keep the object (because we have a ref) but reset the
65 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
66 EXPECT_FALSE(var
->HasValidVarID());
67 EXPECT_EQ(1, mock_var_alive_count
);
70 EXPECT_EQ(0, mock_var_alive_count
);
73 TEST_F(VarTrackerTest
, GetPluginRefAgain
) {
75 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("xyz")));
76 PP_Var pp_var
= var
->GetPPVar();
77 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
78 EXPECT_FALSE(var
->HasValidVarID());
79 EXPECT_EQ(1, mock_var_alive_count
);
81 // Obtaining PP_Var ref again, and add ref from VarTracker.
82 pp_var
= var
->GetPPVar();
83 EXPECT_TRUE(var
->HasValidVarID());
84 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
85 scoped_refptr
<MockStringVar
> another_var
=
86 static_cast<MockStringVar
*>(var_tracker().GetVar(pp_var
));
87 EXPECT_EQ(1, mock_var_alive_count
);
89 // Releasing it again.
90 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
91 EXPECT_FALSE(var
->HasValidVarID());
92 EXPECT_EQ(1, mock_var_alive_count
);
95 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
96 EXPECT_EQ(1, mock_var_alive_count
);
98 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
99 EXPECT_EQ(0, mock_var_alive_count
);
102 // Tests when the plugin is holding a ref to a PP_Var when the instance is
103 // owned only by VarTracker.
104 TEST_F(VarTrackerTest
, PluginRefWithoutVarRef
) {
106 // Make a PP_Var with one ref held by the plugin, and release the reference.
107 scoped_refptr
<MockStringVar
> var(new MockStringVar(std::string("zzz")));
108 PP_Var pp_var
= var
->GetPPVar();
109 EXPECT_EQ(1, mock_var_alive_count
);
111 EXPECT_EQ(1, mock_var_alive_count
);
113 // The var is owned only by VarTracker. PP_Var must be still valid.
114 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
116 var_tracker().ReleaseVar(pp_var
);
117 EXPECT_EQ(0, mock_var_alive_count
);
118 EXPECT_FALSE(var_tracker().GetVar(pp_var
));
121 // Tests on Var having type of PP_VARTYPE_OBJECT.
122 TEST_F(VarTrackerTest
, ObjectRef
) {
124 scoped_refptr
<MockObjectVar
> var(new MockObjectVar());
125 PP_Var pp_var
= var
->GetPPVar();
126 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
127 EXPECT_FALSE(var
->HasValidVarID());
128 EXPECT_EQ(1, mock_var_alive_count
);
130 // Obtaining PP_Var ref again, and add ref from VarTracker.
131 pp_var
= var
->GetPPVar();
132 EXPECT_TRUE(var
->HasValidVarID());
133 EXPECT_TRUE(var_tracker().GetVar(var
->GetExistingVarID()));
134 scoped_refptr
<MockObjectVar
> another_var
=
135 static_cast<MockObjectVar
*>(var_tracker().GetVar(pp_var
));
136 EXPECT_EQ(1, mock_var_alive_count
);
138 // Releasing all references, then only VarTracker own the instance.
140 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
141 EXPECT_EQ(1, mock_var_alive_count
);
143 EXPECT_TRUE(var_tracker().GetVar(pp_var
));
144 EXPECT_EQ(1, mock_var_alive_count
);
146 // Releasing plugin reference.
147 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var
));
148 EXPECT_EQ(0, mock_var_alive_count
);