Revert of Refactor OS X sandbox processing and audit sandbox files (patchset #6 id...
[chromium-blink-merge.git] / ppapi / shared_impl / var_tracker_unittest.cc
blobf0408e79bb7946b21addfbb57fb8bbac687809ee
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 "base/message_loop/message_loop.h"
9 #include "ppapi/shared_impl/proxy_lock.h"
10 #include "ppapi/shared_impl/var.h"
11 #include "ppapi/shared_impl/var_tracker.h"
12 #include "ppapi/shared_impl/test_globals.h"
14 namespace ppapi {
16 namespace {
18 int mock_var_alive_count = 0;
20 class MockStringVar : public StringVar {
21 public:
22 MockStringVar(const std::string& str) : StringVar(str) {
23 mock_var_alive_count++;
25 ~MockStringVar() override { mock_var_alive_count--; }
26 bool HasValidVarID() { return GetExistingVarID() != 0; }
29 class MockObjectVar : public Var {
30 public:
31 MockObjectVar() : Var() { mock_var_alive_count++; }
32 ~MockObjectVar() override { mock_var_alive_count--; }
33 PP_VarType GetType() const override { return PP_VARTYPE_OBJECT; }
34 bool HasValidVarID() { return GetExistingVarID() != 0; }
37 } // namespace
39 class VarTrackerTest : public testing::Test {
40 public:
41 VarTrackerTest() {}
43 // Test implementation.
44 void SetUp() override {
45 ASSERT_EQ(0, mock_var_alive_count);
47 void TearDown() override {}
49 VarTracker& var_tracker() { return *globals_.GetVarTracker(); }
51 private:
52 base::MessageLoop message_loop_; // Required to receive callbacks.
53 TestGlobals globals_;
56 // Test that ResetVarID is called when the last PP_Var ref was deleted but the
57 // object lives on.
58 TEST_F(VarTrackerTest, LastResourceRef) {
59 ProxyAutoLock lock;
60 scoped_refptr<MockStringVar> var(new MockStringVar(std::string("xyz")));
61 PP_Var pp_var = var->GetPPVar();
62 EXPECT_TRUE(var->HasValidVarID());
63 EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
65 // Releasing it should keep the object (because we have a ref) but reset the
66 // var_id_.
67 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
68 EXPECT_FALSE(var->HasValidVarID());
69 EXPECT_EQ(1, mock_var_alive_count);
71 var = NULL;
72 EXPECT_EQ(0, mock_var_alive_count);
75 TEST_F(VarTrackerTest, GetPluginRefAgain) {
76 ProxyAutoLock lock;
77 scoped_refptr<MockStringVar> var(new MockStringVar(std::string("xyz")));
78 PP_Var pp_var = var->GetPPVar();
79 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
80 EXPECT_FALSE(var->HasValidVarID());
81 EXPECT_EQ(1, mock_var_alive_count);
83 // Obtaining PP_Var ref again, and add ref from VarTracker.
84 pp_var = var->GetPPVar();
85 EXPECT_TRUE(var->HasValidVarID());
86 EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
87 scoped_refptr<MockStringVar> another_var =
88 static_cast<MockStringVar*>(var_tracker().GetVar(pp_var));
89 EXPECT_EQ(1, mock_var_alive_count);
91 // Releasing it again.
92 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
93 EXPECT_FALSE(var->HasValidVarID());
94 EXPECT_EQ(1, mock_var_alive_count);
96 var = NULL;
97 EXPECT_FALSE(var_tracker().GetVar(pp_var));
98 EXPECT_EQ(1, mock_var_alive_count);
99 another_var = NULL;
100 EXPECT_FALSE(var_tracker().GetVar(pp_var));
101 EXPECT_EQ(0, mock_var_alive_count);
104 // Tests when the plugin is holding a ref to a PP_Var when the instance is
105 // owned only by VarTracker.
106 TEST_F(VarTrackerTest, PluginRefWithoutVarRef) {
107 ProxyAutoLock lock;
108 // Make a PP_Var with one ref held by the plugin, and release the reference.
109 scoped_refptr<MockStringVar> var(new MockStringVar(std::string("zzz")));
110 PP_Var pp_var = var->GetPPVar();
111 EXPECT_EQ(1, mock_var_alive_count);
112 var = NULL;
113 EXPECT_EQ(1, mock_var_alive_count);
115 // The var is owned only by VarTracker. PP_Var must be still valid.
116 EXPECT_TRUE(var_tracker().GetVar(pp_var));
118 var_tracker().ReleaseVar(pp_var);
119 EXPECT_EQ(0, mock_var_alive_count);
120 EXPECT_FALSE(var_tracker().GetVar(pp_var));
123 // Tests on Var having type of PP_VARTYPE_OBJECT.
124 TEST_F(VarTrackerTest, ObjectRef) {
125 ProxyAutoLock lock;
126 scoped_refptr<MockObjectVar> var(new MockObjectVar());
127 PP_Var pp_var = var->GetPPVar();
128 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
129 EXPECT_FALSE(var->HasValidVarID());
130 EXPECT_EQ(1, mock_var_alive_count);
132 // Obtaining PP_Var ref again, and add ref from VarTracker.
133 pp_var = var->GetPPVar();
134 EXPECT_TRUE(var->HasValidVarID());
135 EXPECT_TRUE(var_tracker().GetVar(var->GetExistingVarID()));
136 scoped_refptr<MockObjectVar> another_var =
137 static_cast<MockObjectVar*>(var_tracker().GetVar(pp_var));
138 EXPECT_EQ(1, mock_var_alive_count);
140 // Releasing all references, then only VarTracker own the instance.
141 var = NULL;
142 EXPECT_TRUE(var_tracker().GetVar(pp_var));
143 EXPECT_EQ(1, mock_var_alive_count);
144 another_var = NULL;
145 EXPECT_TRUE(var_tracker().GetVar(pp_var));
146 EXPECT_EQ(1, mock_var_alive_count);
148 // Releasing plugin reference.
149 EXPECT_TRUE(var_tracker().ReleaseVar(pp_var));
150 EXPECT_EQ(0, mock_var_alive_count);
153 } // namespace ppapi