diff options
author | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-16 07:09:59 +0000 |
---|---|---|
committer | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-16 07:09:59 +0000 |
commit | e0c18c77100c783f4f9bcc8f2fd19c3fcfed91e4 (patch) | |
tree | 99358edc0d687445400c16ff6503942e594da81e | |
parent | 27816a59eecb8d185d4139bd55ee167f8cdc6ec6 (diff) | |
download | chromium_src-e0c18c77100c783f4f9bcc8f2fd19c3fcfed91e4.zip chromium_src-e0c18c77100c783f4f9bcc8f2fd19c3fcfed91e4.tar.gz chromium_src-e0c18c77100c783f4f9bcc8f2fd19c3fcfed91e4.tar.bz2 |
Pepper: unit tests for VarTracker
BUG=none
TEST=ppapi_unittests
Review URL: http://codereview.chromium.org/8953017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114778 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ppapi/ppapi_tests.gypi | 1 | ||||
-rw-r--r-- | ppapi/shared_impl/var_tracker.cc | 1 | ||||
-rw-r--r-- | ppapi/shared_impl/var_tracker_unittest.cc | 159 |
3 files changed, 161 insertions, 0 deletions
diff --git a/ppapi/ppapi_tests.gypi b/ppapi/ppapi_tests.gypi index 5680d1b..14e3d3c 100644 --- a/ppapi/ppapi_tests.gypi +++ b/ppapi/ppapi_tests.gypi @@ -152,6 +152,7 @@ 'shared_impl/resource_tracker_unittest.cc', 'shared_impl/test_globals.cc', 'shared_impl/test_globals.h', + 'shared_impl/var_tracker_unittest.cc', ], }, { diff --git a/ppapi/shared_impl/var_tracker.cc b/ppapi/shared_impl/var_tracker.cc index 852d586..287e7b1 100644 --- a/ppapi/shared_impl/var_tracker.cc +++ b/ppapi/shared_impl/var_tracker.cc @@ -162,6 +162,7 @@ bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { if (iter->second.ref_count != 0 || iter->second.track_with_no_reference_count != 0) return false; // Object still alive. + iter->second.var->ResetVarID(); live_vars_.erase(iter); return true; } diff --git a/ppapi/shared_impl/var_tracker_unittest.cc b/ppapi/shared_impl/var_tracker_unittest.cc new file mode 100644 index 0000000..23e2f59 --- /dev/null +++ b/ppapi/shared_impl/var_tracker_unittest.cc @@ -0,0 +1,159 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "base/compiler_specific.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; + } +}; + +} // namespace + +class VarTrackerTest : public testing::Test { + public: + VarTrackerTest() {} + + // Test implementation. + virtual void SetUp() OVERRIDE { + ASSERT_EQ(0, mock_var_alive_count); + } + virtual void TearDown() OVERRIDE { + } + + VarTracker& var_tracker() { return *globals_.GetVarTracker(); } + + private: + TestGlobals globals_; +}; + +// Test that ResetVarID is called when the last PP_Var ref was deleted but the +// object lives on. +TEST_F(VarTrackerTest, LastResourceRef) { + 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())); + + // Releasing it should keep the object (because we have a ref) but reset the + // var_id_. + 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) { + 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); + + // Obtaining PP_Var ref again, and add ref from VarTracker. + 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); + + // Releasing it again. + 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); +} + +// Tests when the plugin is holding a ref to a PP_Var when the instance is +// owned only by VarTracker. +TEST_F(VarTrackerTest, PluginRefWithoutVarRef) { + // Make a PP_Var with one ref held by the plugin, and release the reference. + 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); + + // The var is owned only by VarTracker. PP_Var must be still valid. + 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)); +} + +// Tests on Var having type of PP_VARTYPE_OBJECT. +TEST_F(VarTrackerTest, ObjectRef) { + 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); + + // Obtaining PP_Var ref again, and add ref from VarTracker. + 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); + + // Releasing all references, then only VarTracker own the instance. + 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); + + // Releasing plugin reference. + EXPECT_TRUE(var_tracker().ReleaseVar(pp_var)); + EXPECT_EQ(0, mock_var_alive_count); +} + +} // namespace ppapi |