Make castv2 performance test work.
[chromium-blink-merge.git] / ppapi / shared_impl / var_tracker_unittest.cc
blobfb8fda38b96c62acbf7f56ab632c42ef4efc7117
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"
13 namespace ppapi {
15 namespace {
17 int mock_var_alive_count = 0;
19 class MockStringVar : public StringVar {
20 public:
21 MockStringVar(const std::string& str) : StringVar(str) {
22 mock_var_alive_count++;
24 virtual ~MockStringVar() { mock_var_alive_count--; }
25 bool HasValidVarID() { return GetExistingVarID() != 0; }
28 class MockObjectVar : public Var {
29 public:
30 MockObjectVar() : Var() { mock_var_alive_count++; }
31 virtual ~MockObjectVar() { mock_var_alive_count--; }
32 virtual PP_VarType GetType() const override { return PP_VARTYPE_OBJECT; }
33 bool HasValidVarID() { return GetExistingVarID() != 0; }
36 } // namespace
38 class VarTrackerTest : public testing::Test {
39 public:
40 VarTrackerTest() {}
42 // Test implementation.
43 virtual void SetUp() override {
44 ASSERT_EQ(0, mock_var_alive_count);
46 virtual void TearDown() override {}
48 VarTracker& var_tracker() { return *globals_.GetVarTracker(); }
50 private:
51 TestGlobals globals_;
54 // Test that ResetVarID is called when the last PP_Var ref was deleted but the
55 // object lives on.
56 TEST_F(VarTrackerTest, LastResourceRef) {
57 ProxyAutoLock lock;
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
64 // var_id_.
65 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
66 EXPECT_FALSE(var->HasValidVarID());
67 EXPECT_EQ(1, mock_var_alive_count);
69 var = NULL;
70 EXPECT_EQ(0, mock_var_alive_count);
73 TEST_F(VarTrackerTest, GetPluginRefAgain) {
74 ProxyAutoLock lock;
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);
94 var = NULL;
95 EXPECT_FALSE(var_tracker().GetVar(pp_var));
96 EXPECT_EQ(1, mock_var_alive_count);
97 another_var = NULL;
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) {
105 ProxyAutoLock lock;
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);
110 var = NULL;
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) {
123 ProxyAutoLock lock;
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.
139 var = NULL;
140 EXPECT_TRUE(var_tracker().GetVar(pp_var));
141 EXPECT_EQ(1, mock_var_alive_count);
142 another_var = NULL;
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);
151 } // namespace ppapi