diff options
author | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-20 23:20:36 +0000 |
---|---|---|
committer | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-20 23:20:36 +0000 |
commit | 240b5a625f612df3c18495946dde2e66682ffdd1 (patch) | |
tree | 7a0c831bdd5519b3eaac477f07e0a4c6c04e3543 /o3d | |
parent | 1d769c1b75d700e219d01a6c6419e52b3f8d22cb (diff) | |
download | chromium_src-240b5a625f612df3c18495946dde2e66682ffdd1.zip chromium_src-240b5a625f612df3c18495946dde2e66682ffdd1.tar.gz chromium_src-240b5a625f612df3c18495946dde2e66682ffdd1.tar.bz2 |
Added NPObjectBase, a base class for NPObject that forwards NPObject calls through to virtual functions in the subclass.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/174180
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23912 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d')
-rw-r--r-- | o3d/gpu_plugin/base_np_object.cc | 136 | ||||
-rw-r--r-- | o3d/gpu_plugin/base_np_object.h | 133 | ||||
-rw-r--r-- | o3d/gpu_plugin/base_np_object_mock.cc | 13 | ||||
-rw-r--r-- | o3d/gpu_plugin/base_np_object_mock.h | 47 | ||||
-rw-r--r-- | o3d/gpu_plugin/base_np_object_unittest.cc | 157 | ||||
-rw-r--r-- | o3d/gpu_plugin/gpu_plugin.h | 6 |
6 files changed, 489 insertions, 3 deletions
diff --git a/o3d/gpu_plugin/base_np_object.cc b/o3d/gpu_plugin/base_np_object.cc new file mode 100644 index 0000000..68b827e --- /dev/null +++ b/o3d/gpu_plugin/base_np_object.cc @@ -0,0 +1,136 @@ +// Copyright (c) 2006-2008 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 "o3d/gpu_plugin/base_np_object.h" + +namespace o3d { +namespace gpu_plugin { + +BaseNPObject::BaseNPObject(NPP npp) : npp_(npp) { +} + +BaseNPObject::~BaseNPObject() { +} + +// The default implementations of the virtual functions return failure and clear +// the result variant to void if appropriate. + +void BaseNPObject::Invalidate() { +} + +bool BaseNPObject::HasMethod(NPIdentifier name) { + return false; +} + +bool BaseNPObject::Invoke(NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + VOID_TO_NPVARIANT(*result); + return false; +} + +bool BaseNPObject::InvokeDefault(const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + VOID_TO_NPVARIANT(*result); + return false; +} + +bool BaseNPObject::HasProperty(NPIdentifier name) { + return false; +} + +bool BaseNPObject::GetProperty(NPIdentifier name, NPVariant* result) { + VOID_TO_NPVARIANT(*result); + return false; +} + +bool BaseNPObject::SetProperty(NPIdentifier name, const NPVariant* value) { + return false; +} + +bool BaseNPObject::RemoveProperty(NPIdentifier name) { + return false; +} + +bool BaseNPObject::Enumerate(NPIdentifier** names, uint32_t* count) { + return false; +} + +bool BaseNPObject::Construct(const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + VOID_TO_NPVARIANT(*result); + return false; +} + +// These implementations of the NPClass functions forward to the virtual +// functions in BaseNPObject. + +void BaseNPObject::DeallocateImpl(NPObject* object) { + delete static_cast<BaseNPObject*>(object); +} + +void BaseNPObject::InvalidateImpl(NPObject* object) { + return static_cast<BaseNPObject*>(object)->Invalidate(); +} + +bool BaseNPObject::HasMethodImpl(NPObject* object, NPIdentifier name) { + return static_cast<BaseNPObject*>(object)->HasMethod(name); +} + +bool BaseNPObject::InvokeImpl(NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + return static_cast<BaseNPObject*>(object)->Invoke( + name, args, num_args, result); +} + +bool BaseNPObject::InvokeDefaultImpl(NPObject* object, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + return static_cast<BaseNPObject*>(object)->InvokeDefault( + args, num_args, result); +} + +bool BaseNPObject::HasPropertyImpl(NPObject* object, NPIdentifier name) { + return static_cast<BaseNPObject*>(object)->HasProperty(name); +} + +bool BaseNPObject::GetPropertyImpl(NPObject* object, + NPIdentifier name, + NPVariant* result) { + return static_cast<BaseNPObject*>(object)->GetProperty(name, result); +} + +bool BaseNPObject::SetPropertyImpl(NPObject* object, + NPIdentifier name, + const NPVariant* value) { + return static_cast<BaseNPObject*>(object)->SetProperty(name, value); +} + +bool BaseNPObject::RemovePropertyImpl(NPObject* object, NPIdentifier name) { + return static_cast<BaseNPObject*>(object)->RemoveProperty(name); +} + +bool BaseNPObject::EnumerateImpl(NPObject* object, + NPIdentifier** names, + uint32_t* count) { + return static_cast<BaseNPObject*>(object)->Enumerate(names, count); +} + +bool BaseNPObject::ConstructImpl(NPObject* object, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + return static_cast<BaseNPObject*>(object)->Construct( + args, num_args, result); +} + +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/base_np_object.h b/o3d/gpu_plugin/base_np_object.h new file mode 100644 index 0000000..898732e --- /dev/null +++ b/o3d/gpu_plugin/base_np_object.h @@ -0,0 +1,133 @@ +// Copyright (c) 2006-2008 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. + +#ifndef O3D_GPU_PLUGIN_NP_OBJECT_BASE_H_ +#define O3D_GPU_PLUGIN_NP_OBJECT_BASE_H_ + +#include "third_party/npapi/bindings/npapi.h" +#include "third_party/npapi/bindings/npruntime.h" + +namespace o3d { +namespace gpu_plugin { + +// This class is a base class for NPObjects implemented in C++. It forwards +// The NPObject calls through to virtual functions implemented by the subclass. +class BaseNPObject : public NPObject { + public: + // Returns the NPClass for the concrete BaseNPObject subclass T. + template <typename T> + static const NPClass* GetNPClass(); + + explicit BaseNPObject(NPP npp); + virtual ~BaseNPObject(); + + NPP npp() const { + return npp_; + } + + // Override these in subclass. + virtual void Invalidate(); + + virtual bool HasMethod(NPIdentifier name); + + virtual bool Invoke(NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + virtual bool InvokeDefault(const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + virtual bool HasProperty(NPIdentifier name); + + virtual bool GetProperty(NPIdentifier name, NPVariant* result); + + virtual bool SetProperty(NPIdentifier name, const NPVariant* value); + + virtual bool RemoveProperty(NPIdentifier name); + + virtual bool Enumerate(NPIdentifier** names, + uint32_t* count); + + virtual bool Construct(const NPVariant* args, + uint32_t num_args, + NPVariant* result); + private: + // This template version of the NPClass allocate function creates a subclass + // of BaseNPObject. + template <typename T> + static NPObject* AllocateImpl(NPP npp, NPClass*) { + return new T(npp); + } + + // These implementations of the NPClass functions forward to the virtual + // functions in BaseNPObject. + static void DeallocateImpl(NPObject* object); + + static void InvalidateImpl(NPObject* object); + + static bool HasMethodImpl(NPObject* object, NPIdentifier name); + + static bool InvokeImpl(NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + static bool InvokeDefaultImpl(NPObject* object, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + static bool HasPropertyImpl(NPObject* object, NPIdentifier name); + + static bool GetPropertyImpl(NPObject* object, + NPIdentifier name, + NPVariant* result); + + static bool SetPropertyImpl(NPObject* object, + NPIdentifier name, + const NPVariant* value); + + static bool RemovePropertyImpl(NPObject* object, NPIdentifier name); + + static bool EnumerateImpl(NPObject* object, + NPIdentifier** names, + uint32_t* count); + + static bool ConstructImpl(NPObject* object, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + NPP npp_; + + DISALLOW_COPY_AND_ASSIGN(BaseNPObject); +}; + +template <typename T> +const NPClass* BaseNPObject::GetNPClass() { + static NPClass np_class = { + NP_CLASS_STRUCT_VERSION, + AllocateImpl<T>, + DeallocateImpl, + InvalidateImpl, + HasMethodImpl, + InvokeImpl, + InvokeDefaultImpl, + HasPropertyImpl, + GetPropertyImpl, + SetPropertyImpl, + RemovePropertyImpl, + EnumerateImpl, + ConstructImpl, + }; + return &np_class; +}; + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_OBJECT_BASE_H_ diff --git a/o3d/gpu_plugin/base_np_object_mock.cc b/o3d/gpu_plugin/base_np_object_mock.cc new file mode 100644 index 0000000..3955b4f --- /dev/null +++ b/o3d/gpu_plugin/base_np_object_mock.cc @@ -0,0 +1,13 @@ +// Copyright (c) 2006-2008 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 "o3d/gpu_plugin/base_np_object_mock.h" + +namespace o3d { +namespace gpu_plugin { + +int MockBaseNPObject::count_ = 0; + +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/base_np_object_mock.h b/o3d/gpu_plugin/base_np_object_mock.h new file mode 100644 index 0000000..9047eed --- /dev/null +++ b/o3d/gpu_plugin/base_np_object_mock.h @@ -0,0 +1,47 @@ +// Copyright (c) 2006-2008 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. + +#ifndef O3D_GPU_PLUGIN_NP_OBJECT_BASE_MOCK_H_ +#define O3D_GPU_PLUGIN_NP_OBJECT_BASE_MOCK_H_ + +#include "o3d/gpu_plugin/base_np_object.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace o3d { +namespace gpu_plugin { + +class MockBaseNPObject : public BaseNPObject { + public: + explicit MockBaseNPObject(NPP npp) : BaseNPObject(npp) { + ++count_; + } + + ~MockBaseNPObject() { + --count_; + } + + static int count() { + return count_; + } + + MOCK_METHOD0(Invalidate, void()); + MOCK_METHOD1(HasMethod, bool(NPIdentifier)); + MOCK_METHOD4(Invoke, + bool(NPIdentifier, const NPVariant*, uint32_t, NPVariant*)); + MOCK_METHOD3(InvokeDefault, bool(const NPVariant*, uint32_t, NPVariant*)); + MOCK_METHOD1(HasProperty, bool(NPIdentifier)); + MOCK_METHOD2(GetProperty, bool(NPIdentifier, NPVariant*)); + MOCK_METHOD2(SetProperty, bool(NPIdentifier, const NPVariant*)); + MOCK_METHOD1(RemoveProperty, bool(NPIdentifier)); + MOCK_METHOD2(Enumerate, bool(NPIdentifier**, uint32_t*)); + MOCK_METHOD3(Construct, bool(const NPVariant*, uint32_t, NPVariant*)); + + private: + static int count_; +}; + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_OBJECT_BASE_MOCK_H_ diff --git a/o3d/gpu_plugin/base_np_object_unittest.cc b/o3d/gpu_plugin/base_np_object_unittest.cc new file mode 100644 index 0000000..9de2d94 --- /dev/null +++ b/o3d/gpu_plugin/base_np_object_unittest.cc @@ -0,0 +1,157 @@ +// Copyright (c) 2006-2008 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 "o3d/gpu_plugin/base_np_object_mock.h" +#include "o3d/gpu_plugin/base_np_object.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using testing::StrictMock; + +namespace o3d { +namespace gpu_plugin { + +class BaseNPObjectTest : public testing::Test { + protected: + virtual void SetUp() { + np_class = const_cast<NPClass*>( + BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >()); + + // Dummy identifier is never used with real NPAPI so it can point to + // anything. + identifier = this; + + // Make sure no MockBaseNPObject objects exist before test. + ASSERT_EQ(0, MockBaseNPObject::count()); + } + + virtual void TearDown() { + // Make sure no MockBaseNPObject leaked an object. + ASSERT_EQ(0, MockBaseNPObject::count()); + } + + NPP_t npp_; + NPClass* np_class; + NPIdentifier identifier; + NPVariant args[3]; + NPVariant result; +}; + + +TEST_F(BaseNPObjectTest, AllocateAndDeallocateObject) { + EXPECT_EQ(0, MockBaseNPObject::count()); + + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + EXPECT_TRUE(NULL != object); + + EXPECT_EQ(1, MockBaseNPObject::count()); + + np_class->deallocate(object); + EXPECT_EQ(0, MockBaseNPObject::count()); +} + +TEST_F(BaseNPObjectTest, InvalidateForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, Invalidate()); + np_class->invalidate(object); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, HasMethodForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, HasMethod(identifier)); + np_class->hasMethod(object, identifier); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, InvokeForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, Invoke(identifier, args, 3, &result)); + np_class->invoke(object, identifier, args, 3, &result); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, InvokeDefaultForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, InvokeDefault(args, 3, &result)); + np_class->invokeDefault(object, args, 3, &result); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, HasPropertyForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, HasProperty(identifier)); + np_class->hasProperty(object, identifier); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, GetPropertyForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, GetProperty(identifier, &result)); + np_class->getProperty(object, identifier, &result); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, SetPropertyForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, SetProperty(identifier, &result)); + np_class->setProperty(object, identifier, &result); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, RemovePropertyForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, RemoveProperty(identifier)); + np_class->removeProperty(object, identifier); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, EnumerateForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + NPIdentifier* identifier = NULL; + uint32_t count; + EXPECT_CALL(*object, Enumerate(&identifier, &count)); + np_class->enumerate(object, &identifier, &count); + + np_class->deallocate(object); +} + +TEST_F(BaseNPObjectTest, ConstructForwards) { + MockBaseNPObject* object = static_cast<MockBaseNPObject*>( + np_class->allocate(&npp_, np_class)); + + EXPECT_CALL(*object, Construct(args, 3, &result)); + np_class->construct(object, args, 3, &result); + + np_class->deallocate(object); +} +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/gpu_plugin.h b/o3d/gpu_plugin/gpu_plugin.h index 1fa2279..f3d95d2 100644 --- a/o3d/gpu_plugin/gpu_plugin.h +++ b/o3d/gpu_plugin/gpu_plugin.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef O3D_GPU_PLUGIN_GPU_PLUGIN_H__ -#define O3D_GPU_PLUGIN_GPU_PLUGIN_H__ +#ifndef O3D_GPU_PLUGIN_GPU_PLUGIN_H_ +#define O3D_GPU_PLUGIN_GPU_PLUGIN_H_ #include "third_party/npapi/bindings/npapi.h" @@ -29,4 +29,4 @@ NPError API_CALL NP_Shutdown(); } // namespace gpu_plugin } // namespace o3d -#endif // O3D_GPU_PLUGIN_GPU_PLUGIN_H__ +#endif // O3D_GPU_PLUGIN_GPU_PLUGIN_H_ |