summaryrefslogtreecommitdiffstats
path: root/o3d
diff options
context:
space:
mode:
authorapatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-25 23:13:46 +0000
committerapatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-25 23:13:46 +0000
commit1b951f98b5d86ed9ba82555566627017211ad9d4 (patch)
tree3a051afc6116fa678dadf76f72b5ce587d84f61d /o3d
parent130805e7d8ae9e6fe73b2f6180a04b989d7961e5 (diff)
downloadchromium_src-1b951f98b5d86ed9ba82555566627017211ad9d4.zip
chromium_src-1b951f98b5d86ed9ba82555566627017211ad9d4.tar.gz
chromium_src-1b951f98b5d86ed9ba82555566627017211ad9d4.tar.bz2
Added GPU plugin object, an NPObject to which a plugin instance's NPP calls are forwarded.
TEST=none BUG=none Review URL: http://codereview.chromium.org/173386 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24364 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d')
-rw-r--r--o3d/gpu_plugin/gpu_plugin.cc71
-rw-r--r--o3d/gpu_plugin/gpu_plugin.gyp12
-rw-r--r--o3d/gpu_plugin/gpu_plugin.h6
-rw-r--r--o3d/gpu_plugin/gpu_plugin_object.cc58
-rw-r--r--o3d/gpu_plugin/gpu_plugin_object.h57
-rw-r--r--o3d/gpu_plugin/gpu_plugin_object_unittest.cc98
-rw-r--r--o3d/gpu_plugin/gpu_plugin_object_win.cc56
-rw-r--r--o3d/gpu_plugin/gpu_plugin_unittest.cc222
-rw-r--r--o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc5
-rw-r--r--o3d/gpu_plugin/np_utils/np_object_pointer.h13
-rw-r--r--o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc11
-rw-r--r--o3d/gpu_plugin/np_utils/np_variant_utils.cc2
-rw-r--r--o3d/gpu_plugin/np_utils/np_variant_utils.h23
-rw-r--r--o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc603
-rw-r--r--o3d/gpu_plugin/np_utils/npn_test_stub.cc83
-rw-r--r--o3d/gpu_plugin/np_utils/npn_test_stub.h16
-rw-r--r--o3d/gpu_plugin/plugin_object.h45
-rw-r--r--o3d/gpu_plugin/plugin_object_factory.cc30
-rw-r--r--o3d/gpu_plugin/plugin_object_factory.h32
-rw-r--r--o3d/gpu_plugin/plugin_object_factory_mock.h25
-rw-r--r--o3d/gpu_plugin/plugin_object_factory_unittest.cc42
-rw-r--r--o3d/gpu_plugin/plugin_object_mock.h27
22 files changed, 1183 insertions, 354 deletions
diff --git a/o3d/gpu_plugin/gpu_plugin.cc b/o3d/gpu_plugin/gpu_plugin.cc
index a8654ce..ff21b8b 100644
--- a/o3d/gpu_plugin/gpu_plugin.cc
+++ b/o3d/gpu_plugin/gpu_plugin.cc
@@ -3,6 +3,9 @@
// found in the LICENSE file.
#include "o3d/gpu_plugin/gpu_plugin.h"
+#include "o3d/gpu_plugin/plugin_object.h"
+#include "o3d/gpu_plugin/plugin_object_factory.h"
+#include "o3d/gpu_plugin/np_utils/npn_funcs.h"
#include "webkit/glue/plugins/nphostapi.h"
namespace o3d {
@@ -11,28 +14,62 @@ namespace gpu_plugin {
// Definitions of NPAPI plugin entry points.
namespace {
-NPNetscapeFuncs* g_browser_funcs;
+bool g_initialized;
+PluginObjectFactory* g_plugin_object_factory = new PluginObjectFactory;
-NPError NPP_New(NPMIMEType pluginType, NPP instance,
+NPError NPP_New(NPMIMEType plugin_type, NPP instance,
uint16 mode, int16 argc, char* argn[],
char* argv[], NPSavedData* saved) {
- return NPERR_NO_ERROR;
+ if (!instance)
+ return NPERR_INVALID_INSTANCE_ERROR;
+
+ PluginObject* plugin_object = g_plugin_object_factory->CreatePluginObject(
+ instance, plugin_type);
+ if (!plugin_object)
+ return NPERR_GENERIC_ERROR;
+
+ instance->pdata = plugin_object;
+
+ return plugin_object->New(plugin_type, argc, argn, argv, saved);
}
-NPError NPP_Destroy(NPP instance, NPSavedData** save) {
- return NPERR_NO_ERROR;
+NPError NPP_Destroy(NPP instance, NPSavedData** saved) {
+ if (!instance)
+ return NPERR_INVALID_INSTANCE_ERROR;
+
+ PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata);
+ return plugin_object->Destroy(saved);
}
NPError NPP_SetWindow(NPP instance, NPWindow* window) {
- return NPERR_NO_ERROR;
+ if (!instance)
+ return NPERR_INVALID_INSTANCE_ERROR;
+
+ PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata);
+ return plugin_object->SetWindow(window);
}
int16 NPP_HandleEvent(NPP instance, void* event) {
- return 0;
+ if (!instance)
+ return 0;
+
+ PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata);
+ return plugin_object->HandleEvent(static_cast<NPEvent*>(event));
}
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
- return NPERR_NO_ERROR;
+ if (!instance)
+ return NPERR_INVALID_INSTANCE_ERROR;
+
+ PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata);
+ switch (variable) {
+ case NPPVpluginScriptableInstance:
+ *reinterpret_cast<NPObject**>(value) =
+ plugin_object->GetScriptableInstance();
+ return NPERR_NO_ERROR;
+ default:
+ return NPERR_GENERIC_ERROR;
+ }
}
NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) {
@@ -40,6 +77,12 @@ NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) {
}
}
+PluginObjectFactory* SetPluginObjectFactory(PluginObjectFactory* factory) {
+ PluginObjectFactory* previous_factory = g_plugin_object_factory;
+ g_plugin_object_factory = factory;
+ return previous_factory;
+}
+
NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* funcs) {
funcs->newp = NPP_New;
funcs->destroy = NPP_Destroy;
@@ -59,22 +102,26 @@ NPError API_CALL NP_Initialize(NPNetscapeFuncs *browser_funcs) {
if (!browser_funcs)
return NPERR_INVALID_FUNCTABLE_ERROR;
- if (g_browser_funcs)
+ if (g_initialized)
return NPERR_GENERIC_ERROR;
#if defined(OS_LINUX)
NP_GetEntryPoints(plugin_funcs);
#endif
- g_browser_funcs = browser_funcs;
+ SetBrowserFuncs(browser_funcs);
+ g_initialized = true;
+
return NPERR_NO_ERROR;
}
NPError API_CALL NP_Shutdown() {
- if (!g_browser_funcs)
+ if (!g_initialized)
return NPERR_GENERIC_ERROR;
- g_browser_funcs = NULL;
+ SetBrowserFuncs(NULL);
+ g_initialized = false;
+
return NPERR_NO_ERROR;
}
diff --git a/o3d/gpu_plugin/gpu_plugin.gyp b/o3d/gpu_plugin/gpu_plugin.gyp
index 43df8d2..6ee016e 100644
--- a/o3d/gpu_plugin/gpu_plugin.gyp
+++ b/o3d/gpu_plugin/gpu_plugin.gyp
@@ -20,6 +20,12 @@
'sources': [
'gpu_plugin.cc',
'gpu_plugin.h',
+ 'gpu_plugin_object.cc',
+ 'gpu_plugin_object.h',
+ 'gpu_plugin_object_win.cc',
+ 'plugin_object_factory.cc',
+ 'plugin_object.h',
+ 'plugin_object_factory.h',
'np_utils/base_np_object.cc',
'np_utils/base_np_object.h',
'np_utils/dispatched_np_object.cc',
@@ -29,6 +35,8 @@
'np_utils/np_object_pointer.h',
'np_utils/np_variant_utils.cc',
'np_utils/np_variant_utils.h',
+ 'np_utils/npn_funcs.cc',
+ 'np_utils/npn_funcs.h',
],
},
@@ -49,6 +57,10 @@
],
'sources': [
'gpu_plugin_unittest.cc',
+ 'gpu_plugin_object_unittest.cc',
+ 'plugin_object_factory_mock.h',
+ 'plugin_object_factory_unittest.cc',
+ 'plugin_object_mock.h',
'np_utils/base_np_object_mock.cc',
'np_utils/base_np_object_mock.h',
'np_utils/base_np_object_unittest.cc',
diff --git a/o3d/gpu_plugin/gpu_plugin.h b/o3d/gpu_plugin/gpu_plugin.h
index f3d95d2..269e053 100644
--- a/o3d/gpu_plugin/gpu_plugin.h
+++ b/o3d/gpu_plugin/gpu_plugin.h
@@ -6,6 +6,7 @@
#define O3D_GPU_PLUGIN_GPU_PLUGIN_H_
#include "third_party/npapi/bindings/npapi.h"
+#include "third_party/npapi/bindings/npruntime.h"
typedef struct _NPPluginFuncs NPPluginFuncs;
typedef struct _NPNetscapeFuncs NPNetscapeFuncs;
@@ -13,6 +14,11 @@ typedef struct _NPNetscapeFuncs NPNetscapeFuncs;
namespace o3d {
namespace gpu_plugin {
+class PluginObjectFactory;
+
+// Replaces the plugin object factory and returns the previous one.
+PluginObjectFactory* SetPluginObjectFactory(PluginObjectFactory* factory);
+
// Declarations of NPAPI plugin entry points.
NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* funcs);
diff --git a/o3d/gpu_plugin/gpu_plugin_object.cc b/o3d/gpu_plugin/gpu_plugin_object.cc
new file mode 100644
index 0000000..e6b7647
--- /dev/null
+++ b/o3d/gpu_plugin/gpu_plugin_object.cc
@@ -0,0 +1,58 @@
+// 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 <stdlib.h>
+
+#include "o3d/gpu_plugin/gpu_plugin_object.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+const NPUTF8 GPUPluginObject::kPluginType[] =
+ "application/vnd.google.chrome.gpu-plugin";
+
+NPError GPUPluginObject::New(NPMIMEType plugin_type,
+ int16 argc,
+ char* argn[],
+ char* argv[],
+ NPSavedData* saved) {
+ if (status_ != CREATED)
+ return NPERR_GENERIC_ERROR;
+
+ status_ = INITIALIZED;
+ return NPERR_NO_ERROR;
+}
+
+NPError GPUPluginObject::SetWindow(NPWindow* new_window) {
+ if (status_ != INITIALIZED)
+ return NPERR_GENERIC_ERROR;
+
+ NPError error = PlatformSpecificSetWindow(new_window);
+ if (error == NPERR_NO_ERROR) {
+ window_ = *new_window;
+ } else {
+ memset(&window_, 0, sizeof(window_));
+ }
+
+ return error;
+}
+
+int16 GPUPluginObject::HandleEvent(NPEvent* event) {
+ return 0;
+}
+
+NPError GPUPluginObject::Destroy(NPSavedData** saved) {
+ if (status_ != INITIALIZED)
+ return NPERR_GENERIC_ERROR;
+
+ status_ = DESTROYED;
+ return NPERR_NO_ERROR;
+}
+
+NPObject* GPUPluginObject::GetScriptableInstance() {
+ return NULL;
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/gpu_plugin_object.h b/o3d/gpu_plugin/gpu_plugin_object.h
new file mode 100644
index 0000000..d3c6576
--- /dev/null
+++ b/o3d/gpu_plugin/gpu_plugin_object.h
@@ -0,0 +1,57 @@
+// 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_GPU_PLUGIN_OBJECT_H_
+#define O3D_GPU_PLUGIN_GPU_PLUGIN_OBJECT_H_
+
+#include "o3d/gpu_plugin/np_utils/dispatched_np_object.h"
+#include "o3d/gpu_plugin/plugin_object.h"
+#include "third_party/npapi/bindings/npapi.h"
+#include "third_party/npapi/bindings/npruntime.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+// The scriptable object for the GPU plugin.
+class GPUPluginObject : public DispatchedNPObject, public PluginObject {
+ public:
+ static const NPUTF8 kPluginType[];
+
+ explicit GPUPluginObject(NPP npp)
+ : DispatchedNPObject(npp),
+ status_(CREATED) {
+ }
+
+ virtual NPError New(NPMIMEType plugin_type,
+ int16 argc,
+ char* argn[],
+ char* argv[],
+ NPSavedData* saved);
+
+ virtual NPError SetWindow(NPWindow* new_window);
+ const NPWindow& GetWindow() { return window_; }
+
+ virtual int16 HandleEvent(NPEvent* event);
+
+ virtual NPError Destroy(NPSavedData** saved);
+
+ virtual NPObject* GetScriptableInstance();
+
+ private:
+ NPError PlatformSpecificSetWindow(NPWindow* new_window);
+
+ enum Status {
+ CREATED,
+ INITIALIZED,
+ DESTROYED,
+ };
+
+ Status status_;
+ NPWindow window_;
+};
+
+} // namespace gpu_plugin
+} // namespace o3d
+
+#endif // O3D_GPU_PLUGIN_GPU_PLUGIN_OBJECT_H_
diff --git a/o3d/gpu_plugin/gpu_plugin_object_unittest.cc b/o3d/gpu_plugin/gpu_plugin_object_unittest.cc
new file mode 100644
index 0000000..2c4024f
--- /dev/null
+++ b/o3d/gpu_plugin/gpu_plugin_object_unittest.cc
@@ -0,0 +1,98 @@
+// 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/gpu_plugin_object.h"
+#include "o3d/gpu_plugin/np_utils/np_object_pointer.h"
+#include "o3d/gpu_plugin/np_utils/npn_test_stub.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webkit/glue/plugins/nphostapi.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+class GPUPluginObjectTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ InitializeNPNTestStub();
+
+ np_class_ = const_cast<NPClass*>(
+ BaseNPObject::GetNPClass<GPUPluginObject>());
+
+ plugin_object_ = static_cast<GPUPluginObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+ }
+
+ virtual void TearDown() {
+ gpu_plugin::NPN_ReleaseObject(plugin_object_);
+ ShutdownNPNTestStub();
+ }
+
+ NPClass* np_class_;
+ GPUPluginObject* plugin_object_;
+};
+
+TEST_F(GPUPluginObjectTest, CanInitializeAndDestroyPluginObject) {
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->Destroy(NULL));
+}
+
+TEST_F(GPUPluginObjectTest, DestroyFailsIfNotInitialized) {
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_object_->Destroy(NULL));
+}
+
+TEST_F(GPUPluginObjectTest, NewFailsIfAlreadyInitialized) {
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+}
+
+TEST_F(GPUPluginObjectTest, NewFailsIfObjectHasPreviouslyBeenDestroyed) {
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->Destroy(NULL));
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+}
+
+TEST_F(GPUPluginObjectTest, CanSetWindow) {
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->New("application/foo",
+ 0,
+ NULL,
+ NULL,
+ NULL));
+
+ NPWindow window = {0};
+ window.window = &window;
+ window.x = 7;
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->SetWindow(&window));
+ EXPECT_EQ(0, memcmp(&window, &plugin_object_->GetWindow(), sizeof(window)));
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->Destroy(NULL));
+}
+
+TEST_F(GPUPluginObjectTest, SetWindowFailsIfNotInitialized) {
+ NPWindow window = {0};
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_object_->SetWindow(&window));
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/gpu_plugin_object_win.cc b/o3d/gpu_plugin/gpu_plugin_object_win.cc
new file mode 100644
index 0000000..ddcdf2c
--- /dev/null
+++ b/o3d/gpu_plugin/gpu_plugin_object_win.cc
@@ -0,0 +1,56 @@
+// 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 <windows.h>
+
+#include "o3d/gpu_plugin/gpu_plugin_object.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+namespace {
+const LPCTSTR kPluginObjectProperty = TEXT("GPUPluginObject");
+const LPCTSTR kOriginalWindowProc = TEXT("GPUPluginObjectOriginalWindowProc");
+
+LRESULT CALLBACK WindowProc(HWND handle,
+ UINT message,
+ WPARAM w_param,
+ LPARAM l_param) {
+ return ::DefWindowProc(handle, message, w_param, l_param);
+}
+} // namespace anonymous
+
+NPError GPUPluginObject::PlatformSpecificSetWindow(NPWindow* new_window) {
+ // Detach properties from old window and restore the original window proc.
+ if (window_.window) {
+ HWND handle = reinterpret_cast<HWND>(window_.window);
+ ::RemoveProp(handle, kPluginObjectProperty);
+
+ LONG original_window_proc = reinterpret_cast<LONG>(
+ ::GetProp(handle, kOriginalWindowProc));
+ ::SetWindowLong(handle, GWL_WNDPROC,
+ original_window_proc);
+ ::RemoveProp(handle, kOriginalWindowProc);
+ }
+
+ // Attach properties to new window and set a new window proc.
+ if (new_window->window) {
+ HWND handle = reinterpret_cast<HWND>(new_window->window);
+ ::SetProp(handle,
+ kPluginObjectProperty,
+ reinterpret_cast<HANDLE>(this));
+
+ LONG original_window_proc = ::GetWindowLong(handle, GWL_WNDPROC);
+ ::SetProp(handle,
+ kOriginalWindowProc,
+ reinterpret_cast<HANDLE>(original_window_proc));
+ ::SetWindowLong(handle, GWL_WNDPROC,
+ reinterpret_cast<LONG>(WindowProc));
+ }
+
+ return NPERR_NO_ERROR;
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/gpu_plugin_unittest.cc b/o3d/gpu_plugin/gpu_plugin_unittest.cc
index b7a74c6..ab462c4 100644
--- a/o3d/gpu_plugin/gpu_plugin_unittest.cc
+++ b/o3d/gpu_plugin/gpu_plugin_unittest.cc
@@ -3,6 +3,9 @@
// found in the LICENSE file.
#include "o3d/gpu_plugin/gpu_plugin.h"
+#include "o3d/gpu_plugin/gpu_plugin_object.h"
+#include "o3d/gpu_plugin/plugin_object_factory_mock.h"
+#include "o3d/gpu_plugin/plugin_object_mock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/plugins/nphostapi.h"
@@ -12,18 +15,37 @@
#define INITIALIZE_PLUGIN_FUNCS
#endif
+using testing::_;
+using testing::DoAll;
+using testing::NiceMock;
+using testing::Return;
+using testing::SetArgumentPointee;
+using testing::StrictMock;
+
namespace o3d {
namespace gpu_plugin {
class GPUPluginTest : public testing::Test {
protected:
virtual void SetUp() {
+ memset(&npp_, 0, sizeof(npp_));
memset(&browser_funcs_, 0, sizeof(browser_funcs_));
memset(&plugin_funcs_, 0, sizeof(plugin_funcs_));
+ plugin_object_factory_ = new StrictMock<MockPluginObjectFactory>;
+ previous_plugin_object_factory_ = SetPluginObjectFactory(
+ plugin_object_factory_);
+ }
+
+ virtual void TearDown() {
+ SetPluginObjectFactory(previous_plugin_object_factory_);
+ delete plugin_object_factory_;
}
+ NPP_t npp_;
NPNetscapeFuncs browser_funcs_;
NPPluginFuncs plugin_funcs_;
+ MockPluginObjectFactory* plugin_object_factory_;
+ PluginObjectFactory* previous_plugin_object_factory_;
};
TEST_F(GPUPluginTest, GetEntryPointsSetsNeededFunctionPointers) {
@@ -66,5 +88,205 @@ TEST_F(GPUPluginTest, ShutdownFailsIfNotInitialized) {
EXPECT_EQ(NPERR_GENERIC_ERROR, NP_Shutdown());
}
+TEST_F(GPUPluginTest, NewReturnsErrorForInvalidInstance) {
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_INVALID_INSTANCE_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ NULL, 0, 0, NULL, NULL, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, GetValueReturnsErrorForInvalidInstance) {
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ int* result = NULL;
+ EXPECT_EQ(NPERR_INVALID_INSTANCE_ERROR, plugin_funcs_.getvalue(
+ NULL, NPPVjavaClass, &result));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, DestroyReturnsErrorForInvalidInstance) {
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_INVALID_INSTANCE_ERROR, plugin_funcs_.destroy(NULL, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, SetWindowReturnsErrorForInvalidInstance) {
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_INVALID_INSTANCE_ERROR, plugin_funcs_.setwindow(NULL, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, HandleEventReturnsFalseForInvalidInstance) {
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(0, plugin_funcs_.event(NULL, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, NewCreatesAPluginObjectAndInitializesIt) {
+ StrictMock<MockPluginObject> plugin_object;
+
+ EXPECT_CALL(*plugin_object_factory_, CreatePluginObject(
+ &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType)))
+ .WillOnce(Return(&plugin_object));
+
+ NPObject scriptable_instance;
+
+ EXPECT_CALL(plugin_object, New(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ 0, NULL, NULL, NULL))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ EXPECT_CALL(plugin_object, GetScriptableInstance())
+ .WillOnce(Return(&scriptable_instance));
+
+ EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL)))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ &npp_, 0, 0, NULL, NULL, NULL));
+
+ NPObject* result = NULL;
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.getvalue(
+ &npp_, NPPVpluginScriptableInstance, &result));
+ EXPECT_EQ(&scriptable_instance, result);
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.destroy(&npp_, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, NewFailsIfPluginObjectFactoryFails) {
+ EXPECT_CALL(*plugin_object_factory_, CreatePluginObject(
+ &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType)))
+ .WillOnce(Return(static_cast<PluginObject*>(NULL)));
+
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ &npp_, 0, 0, NULL, NULL, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, SetWindowForwardsToPluginObject) {
+ StrictMock<MockPluginObject> plugin_object;
+
+ EXPECT_CALL(*plugin_object_factory_, CreatePluginObject(
+ &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType)))
+ .WillOnce(Return(&plugin_object));
+
+ EXPECT_CALL(plugin_object, New(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ 0, NULL, NULL, NULL))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NPWindow window = {0};
+
+ EXPECT_CALL(plugin_object, SetWindow(&window))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL)))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ &npp_, 0, 0, NULL, NULL, NULL));
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.setwindow(&npp_, &window));
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.destroy(&npp_, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, HandleEventForwardsToPluginObject) {
+ StrictMock<MockPluginObject> plugin_object;
+
+ EXPECT_CALL(*plugin_object_factory_, CreatePluginObject(
+ &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType)))
+ .WillOnce(Return(&plugin_object));
+
+ EXPECT_CALL(plugin_object, New(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ 0, NULL, NULL, NULL))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NPEvent event = {0};
+
+ EXPECT_CALL(plugin_object, HandleEvent(&event))
+ .WillOnce(Return(7));
+
+ EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL)))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ &npp_, 0, 0, NULL, NULL, NULL));
+
+ EXPECT_EQ(7, plugin_funcs_.event(&npp_, &event));
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.destroy(&npp_, NULL));
+
+ NP_Shutdown();
+}
+
+TEST_F(GPUPluginTest, GetValueReturnsErrorForUnknownVariable) {
+ StrictMock<MockPluginObject> plugin_object;
+
+ EXPECT_CALL(*plugin_object_factory_, CreatePluginObject(
+ &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType)))
+ .WillOnce(Return(&plugin_object));
+
+ EXPECT_CALL(plugin_object, New(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ 0, NULL, NULL, NULL))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL)))
+ .WillOnce(Return(NPERR_NO_ERROR));
+
+ NP_GetEntryPoints(&plugin_funcs_);
+ NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS);
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.newp(
+ const_cast<NPMIMEType>(GPUPluginObject::kPluginType),
+ &npp_, 0, 0, NULL, NULL, NULL));
+
+ int* result = NULL;
+ EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_funcs_.getvalue(
+ &npp_, NPPVjavaClass, &result));
+
+ EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.destroy(&npp_, NULL));
+
+ NP_Shutdown();
+}
+
} // namespace gpu_plugin
} // namespace o3d
diff --git a/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc b/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc
index 791491e..b9edd67 100644
--- a/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc
+++ b/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc
@@ -68,6 +68,8 @@ class MockDispatchedNPObject : public DispatchedNPObject {
class NPObjectDispatcherTest : public testing::Test {
protected:
virtual void SetUp() {
+ InitializeNPNTestStub();
+
object_ = new StrictMock<MockDispatchedNPObject>;
for (int i = 0; i != arraysize(args_); ++i) {
@@ -78,6 +80,7 @@ class NPObjectDispatcherTest : public testing::Test {
virtual void TearDown() {
delete object_;
+ ShutdownNPNTestStub();
}
NPVariant args_[3];
@@ -325,7 +328,7 @@ TEST_F(NPObjectDispatcherTest, CanInvokeStringReturn) {
std::string(str.UTF8Characters, str.UTF8Length));
// Callee is responsible for releasing string.
- NPN_ReleaseVariantValue(&result_);
+ gpu_plugin::NPN_ReleaseVariantValue(&result_);
}
TEST_F(NPObjectDispatcherTest, CanInvokeObjectReturnWithObject) {
diff --git a/o3d/gpu_plugin/np_utils/np_object_pointer.h b/o3d/gpu_plugin/np_utils/np_object_pointer.h
index 6d8d2e0..045bc57 100644
--- a/o3d/gpu_plugin/np_utils/np_object_pointer.h
+++ b/o3d/gpu_plugin/np_utils/np_object_pointer.h
@@ -5,6 +5,7 @@
#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_OBJECT_POINTER_H_
#define O3D_GPU_PLUGIN_NP_UTILS_NP_OBJECT_POINTER_H_
+#include "o3d/gpu_plugin/np_utils/npn_funcs.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
@@ -19,29 +20,29 @@ class NPObjectPointer {
NPObjectPointer(const NPObjectPointer& rhs) : object_(rhs.object_) {
if (object_) {
- NPN_RetainObject(object_);
+ gpu_plugin::NPN_RetainObject(object_);
}
}
explicit NPObjectPointer(NPObjectType* p) : object_(p) {
if (object_) {
- NPN_RetainObject(object_);
+ gpu_plugin::NPN_RetainObject(object_);
}
}
~NPObjectPointer() {
if (object_) {
- NPN_ReleaseObject(object_);
+ gpu_plugin::NPN_ReleaseObject(object_);
}
}
NPObjectPointer& operator=(const NPObjectPointer& rhs) {
if (object_) {
- NPN_ReleaseObject(object_);
+ gpu_plugin::NPN_ReleaseObject(object_);
}
object_ = rhs.object_;
if (object_) {
- NPN_RetainObject(object_);
+ gpu_plugin::NPN_RetainObject(object_);
}
return *this;
}
@@ -51,7 +52,7 @@ class NPObjectPointer {
static NPObjectPointer FromReturned(NPObjectType* p) {
NPObjectPointer pointer(p);
if (p) {
- NPN_ReleaseObject(p);
+ gpu_plugin::NPN_ReleaseObject(p);
}
return pointer;
}
diff --git a/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc b/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc
index 3e96bd3..7daf218 100644
--- a/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc
+++ b/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc
@@ -4,6 +4,7 @@
#include "o3d/gpu_plugin/np_utils/base_np_object_mock.h"
#include "o3d/gpu_plugin/np_utils/np_object_pointer.h"
+#include "o3d/gpu_plugin/np_utils/npn_test_stub.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -16,6 +17,8 @@ namespace gpu_plugin {
class NPObjectPointerTest : public testing::Test {
protected:
virtual void SetUp() {
+ InitializeNPNTestStub();
+
np_class_ = const_cast<NPClass*>(
BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >());
@@ -23,14 +26,16 @@ class NPObjectPointerTest : public testing::Test {
ASSERT_EQ(0, MockBaseNPObject::count());
raw_pointer_ = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
}
virtual void TearDown() {
- NPN_ReleaseObject(raw_pointer_);
+ gpu_plugin::NPN_ReleaseObject(raw_pointer_);
// Make sure no MockBaseNPObject leaked an object.
ASSERT_EQ(0, MockBaseNPObject::count());
+
+ ShutdownNPNTestStub();
}
NPClass* np_class_;
@@ -105,7 +110,7 @@ TEST_F(NPObjectPointerTest, ArrowOperatorCanBeUsedToAccessNPObjectMembers) {
}
TEST_F(NPObjectPointerTest, PointerCanBeConstructedFromReturnedNPObject) {
- NPN_RetainObject(raw_pointer_);
+ gpu_plugin::NPN_RetainObject(raw_pointer_);
EXPECT_EQ(2, raw_pointer_->referenceCount);
{
NPObjectPointer<MockBaseNPObject> p(
diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils.cc b/o3d/gpu_plugin/np_utils/np_variant_utils.cc
index c8ec811..3a7332e 100644
--- a/o3d/gpu_plugin/np_utils/np_variant_utils.cc
+++ b/o3d/gpu_plugin/np_utils/np_variant_utils.cc
@@ -97,7 +97,7 @@ void ValueToNPVariant(const std::string& value, NPVariant* variant) {
void ValueToNPVariant(NPObject* value, NPVariant* variant) {
if (value) {
- NPN_RetainObject(value);
+ gpu_plugin::NPN_RetainObject(value);
OBJECT_TO_NPVARIANT(value, *variant);
} else {
NULL_TO_NPVARIANT(*variant);
diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils.h b/o3d/gpu_plugin/np_utils/np_variant_utils.h
index 4272ffb..f0f1c6a 100644
--- a/o3d/gpu_plugin/np_utils/np_variant_utils.h
+++ b/o3d/gpu_plugin/np_utils/np_variant_utils.h
@@ -7,6 +7,7 @@
#include <string>
+#include "o3d/gpu_plugin/np_utils/npn_funcs.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
@@ -51,7 +52,7 @@ class SmartNPVariant {
}
void Release() {
- NPN_ReleaseVariantValue(&variant_);
+ gpu_plugin::NPN_ReleaseVariantValue(&variant_);
VOID_TO_NPVARIANT(variant_);
}
@@ -73,8 +74,8 @@ class SmartNPVariant {
inline bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name) {
SmartNPVariant result;
- return NPN_Invoke(npp, object, name, NULL, 0,
- &result.GetVariant());
+ return gpu_plugin::NPN_Invoke(npp, object, name, NULL, 0,
+ &result.GetVariant());
}
template<typename R>
@@ -94,8 +95,8 @@ bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name,
SmartNPVariant args[1];
args[0].SetValue(p0);
SmartNPVariant result;
- return NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1,
- &result.GetVariant());
+ return gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1,
+ &result.GetVariant());
}
template<typename R, typename P0>
@@ -104,8 +105,8 @@ bool NPInvoke(NPP npp, NPObject* object, NPIdentifier name,
SmartNPVariant args[1];
args[0].SetValue(p0);
SmartNPVariant result;
- if (NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1,
- &result.GetVariant())) {
+ if (gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1,
+ &result.GetVariant())) {
return result.GetValue(r);
}
return false;
@@ -118,8 +119,8 @@ bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name,
args[0].SetValue(p0);
args[1].SetValue(p1);
SmartNPVariant result;
- return NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2,
- &result.GetVariant());
+ return gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2,
+ &result.GetVariant());
}
template<typename R, typename P0, typename P1>
@@ -129,8 +130,8 @@ bool NPInvoke(NPP npp, NPObject* object, NPIdentifier name,
args[0].SetValue(p0);
args[1].SetValue(p1);
SmartNPVariant result;
- if (NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2,
- &result.GetVariant())) {
+ if (gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2,
+ &result.GetVariant())) {
return result.GetValue(r);
}
return false;
diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc b/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc
index fdf40ce..4e47d22 100644
--- a/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc
+++ b/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc
@@ -1,299 +1,304 @@
-// 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/np_utils/base_np_object_mock.h"
-#include "o3d/gpu_plugin/np_utils/np_variant_utils.h"
-#include "testing/gmock/include/gmock/gmock.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-using testing::_;
-using testing::DoAll;
-using testing::MakeMatcher;
-using testing::Matcher;
-using testing::Pointee;
-using testing::Return;
-using testing::SetArgumentPointee;
-using testing::StrictMock;
-
-namespace o3d {
-namespace gpu_plugin {
-
-class NPVariantUtilsTest : public testing::Test {
- protected:
- virtual void SetUp() {
- np_class_ = const_cast<NPClass*>(
- BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >());
-
- // 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_;
- NPVariant variant_;
-};
-
-TEST_F(NPVariantUtilsTest, TestBoolNPVariantToValue) {
- bool v;
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_FALSE(v);
-
- BOOLEAN_TO_NPVARIANT(true, variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_TRUE(v);
-
- INT32_TO_NPVARIANT(7, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestIntNPVariantToValue) {
- INT32_TO_NPVARIANT(7, variant_);
-
- int v1;
- EXPECT_TRUE(NPVariantToValue(&v1, variant_));
- EXPECT_EQ(7, v1);
-
- float v2;
- EXPECT_TRUE(NPVariantToValue(&v2, variant_));
- EXPECT_EQ(7.0f, v2);
-
- double v3;
- EXPECT_TRUE(NPVariantToValue(&v3, variant_));
- EXPECT_EQ(7.0, v3);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v1, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestFloatNPVariantToValue) {
- float v;
-
- DOUBLE_TO_NPVARIANT(7.0, variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_EQ(7.0f, v);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestDoubleNPVariantToValue) {
- double v;
-
- DOUBLE_TO_NPVARIANT(7.0, variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_EQ(7.0, v);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestStringNPVariantToValue) {
- std::string v;
-
- STRINGZ_TO_NPVARIANT("hello", variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_EQ(std::string("hello"), v);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestObjectNPVariantToValue) {
- NPObject* object = NPN_CreateObject(NULL, np_class_);
- NPObject* v;
-
- OBJECT_TO_NPVARIANT(object, variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_EQ(object, v);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, TestNullNPVariantToValue) {
- NPObject* v;
-
- NULL_TO_NPVARIANT(variant_);
- EXPECT_TRUE(NPVariantToValue(&v, variant_));
- EXPECT_TRUE(NULL == v);
-
- BOOLEAN_TO_NPVARIANT(false, variant_);
- EXPECT_FALSE(NPVariantToValue(&v, variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestBoolValueToNPVariant) {
- ValueToNPVariant(true, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_));
- EXPECT_TRUE(NPVARIANT_TO_BOOLEAN(variant_));
-
- ValueToNPVariant(false, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_));
- EXPECT_FALSE(NPVARIANT_TO_BOOLEAN(variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestIntValueToNPVariant) {
- ValueToNPVariant(7, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_INT32(variant_));
- EXPECT_EQ(7, NPVARIANT_TO_INT32(variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestFloatValueToNPVariant) {
- ValueToNPVariant(7.0f, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_));
- EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestDoubleValueToNPVariant) {
- ValueToNPVariant(7.0, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_));
- EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_));
-}
-
-TEST_F(NPVariantUtilsTest, TestStringValueToNPVariant) {
- ValueToNPVariant(std::string("hello"), &variant_);
- EXPECT_TRUE(NPVARIANT_IS_STRING(variant_));
- EXPECT_EQ(std::string("hello"),
- std::string(variant_.value.stringValue.UTF8Characters,
- variant_.value.stringValue.UTF8Length));
-}
-
-TEST_F(NPVariantUtilsTest, TestObjectValueToNPVariant) {
- NPObject* object = NPN_CreateObject(NULL, np_class_);
-
- ValueToNPVariant(object, &variant_);
- EXPECT_TRUE(NPVARIANT_IS_OBJECT(variant_));
- EXPECT_EQ(object, NPVARIANT_TO_OBJECT(variant_));
-
- NPN_ReleaseVariantValue(&variant_);
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, TestNullValueToNPVariant) {
- ValueToNPVariant(static_cast<NPObject*>(NULL), &variant_);
- EXPECT_TRUE(NPVARIANT_IS_NULL(variant_));
-}
-
-template <typename T>
-class VariantMatcher : public testing::MatcherInterface<const NPVariant&> {
- public:
- explicit VariantMatcher(const T& value) : value_(value) {
- }
-
- virtual bool Matches(const NPVariant& variant) const {
- T other_value;
- return NPVariantToValue(&other_value, variant) && other_value == value_;
- }
-
- virtual void DescribeTo(::std::ostream* os) const {
- *os << "equals " << value_;
- }
-
- virtual void DescribeNegationTo(::std::ostream* os) const {
- *os << "does not equal " << value_;
- }
-
- private:
- T value_;
-};
-
-template <typename T>
-Matcher<const NPVariant&> VariantMatches(const T& value) {
- return MakeMatcher(new VariantMatcher<T>(value));
-}
-
-TEST_F(NPVariantUtilsTest, CanInvokeVoidMethodWithNativeTypes) {
- NPIdentifier name = NPN_GetStringIdentifier("foo");
- MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
-
- VOID_TO_NPVARIANT(variant_);
-
- EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
- .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
- Return(true)));
-
- EXPECT_TRUE(NPInvokeVoid(NULL, object, name, 7));
-
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, InvokeVoidMethodCanFail) {
- NPIdentifier name = NPN_GetStringIdentifier("foo");
- MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
-
- VOID_TO_NPVARIANT(variant_);
-
- EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
- .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
- Return(false)));
-
- EXPECT_FALSE(NPInvokeVoid(NULL, object, name, 7));
-
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, CanInvokeNonVoidMethodWithNativeTypes) {
- NPIdentifier name = NPN_GetStringIdentifier("foo");
- MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
-
- DOUBLE_TO_NPVARIANT(1.5, variant_);
-
- EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
- .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
- Return(true)));
-
- double r;
- EXPECT_TRUE(NPInvoke(NULL, object, name, 7, &r));
- EXPECT_EQ(1.5, r);
-
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodCanFail) {
- NPIdentifier name = NPN_GetStringIdentifier("foo");
- MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
-
- DOUBLE_TO_NPVARIANT(1.5, variant_);
-
- EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
- .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
- Return(false)));
-
- double r;
- EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r));
-
- NPN_ReleaseObject(object);
-}
-
-TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodFailsIfResultIsIncompatible) {
- NPIdentifier name = NPN_GetStringIdentifier("foo");
- MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
- NPN_CreateObject(NULL, np_class_));
-
- DOUBLE_TO_NPVARIANT(1.5, variant_);
-
- EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
- .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
- Return(true)));
-
- int r;
- EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r));
-
- NPN_ReleaseObject(object);
-}
-
-} // namespace gpu_plugin
-} // namespace o3d
+// 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/np_utils/base_np_object_mock.h"
+#include "o3d/gpu_plugin/np_utils/np_variant_utils.h"
+#include "o3d/gpu_plugin/np_utils/npn_test_stub.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+using testing::DoAll;
+using testing::MakeMatcher;
+using testing::Matcher;
+using testing::Pointee;
+using testing::Return;
+using testing::SetArgumentPointee;
+using testing::StrictMock;
+
+namespace o3d {
+namespace gpu_plugin {
+
+class NPVariantUtilsTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ InitializeNPNTestStub();
+
+ np_class_ = const_cast<NPClass*>(
+ BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >());
+
+ // 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());
+
+ ShutdownNPNTestStub();
+ }
+
+ NPP_t npp_;
+ NPClass* np_class_;
+ NPVariant variant_;
+};
+
+TEST_F(NPVariantUtilsTest, TestBoolNPVariantToValue) {
+ bool v;
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_FALSE(v);
+
+ BOOLEAN_TO_NPVARIANT(true, variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_TRUE(v);
+
+ INT32_TO_NPVARIANT(7, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestIntNPVariantToValue) {
+ INT32_TO_NPVARIANT(7, variant_);
+
+ int v1;
+ EXPECT_TRUE(NPVariantToValue(&v1, variant_));
+ EXPECT_EQ(7, v1);
+
+ float v2;
+ EXPECT_TRUE(NPVariantToValue(&v2, variant_));
+ EXPECT_EQ(7.0f, v2);
+
+ double v3;
+ EXPECT_TRUE(NPVariantToValue(&v3, variant_));
+ EXPECT_EQ(7.0, v3);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v1, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestFloatNPVariantToValue) {
+ float v;
+
+ DOUBLE_TO_NPVARIANT(7.0, variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_EQ(7.0f, v);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestDoubleNPVariantToValue) {
+ double v;
+
+ DOUBLE_TO_NPVARIANT(7.0, variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_EQ(7.0, v);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestStringNPVariantToValue) {
+ std::string v;
+
+ STRINGZ_TO_NPVARIANT("hello", variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_EQ(std::string("hello"), v);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestObjectNPVariantToValue) {
+ NPObject* object = gpu_plugin::NPN_CreateObject(NULL, np_class_);
+ NPObject* v;
+
+ OBJECT_TO_NPVARIANT(object, variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_EQ(object, v);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, TestNullNPVariantToValue) {
+ NPObject* v;
+
+ NULL_TO_NPVARIANT(variant_);
+ EXPECT_TRUE(NPVariantToValue(&v, variant_));
+ EXPECT_TRUE(NULL == v);
+
+ BOOLEAN_TO_NPVARIANT(false, variant_);
+ EXPECT_FALSE(NPVariantToValue(&v, variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestBoolValueToNPVariant) {
+ ValueToNPVariant(true, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_));
+ EXPECT_TRUE(NPVARIANT_TO_BOOLEAN(variant_));
+
+ ValueToNPVariant(false, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_));
+ EXPECT_FALSE(NPVARIANT_TO_BOOLEAN(variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestIntValueToNPVariant) {
+ ValueToNPVariant(7, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_INT32(variant_));
+ EXPECT_EQ(7, NPVARIANT_TO_INT32(variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestFloatValueToNPVariant) {
+ ValueToNPVariant(7.0f, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_));
+ EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestDoubleValueToNPVariant) {
+ ValueToNPVariant(7.0, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_));
+ EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_));
+}
+
+TEST_F(NPVariantUtilsTest, TestStringValueToNPVariant) {
+ ValueToNPVariant(std::string("hello"), &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_STRING(variant_));
+ EXPECT_EQ(std::string("hello"),
+ std::string(variant_.value.stringValue.UTF8Characters,
+ variant_.value.stringValue.UTF8Length));
+}
+
+TEST_F(NPVariantUtilsTest, TestObjectValueToNPVariant) {
+ NPObject* object = gpu_plugin::NPN_CreateObject(NULL, np_class_);
+
+ ValueToNPVariant(object, &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_OBJECT(variant_));
+ EXPECT_EQ(object, NPVARIANT_TO_OBJECT(variant_));
+
+ gpu_plugin::NPN_ReleaseVariantValue(&variant_);
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, TestNullValueToNPVariant) {
+ ValueToNPVariant(static_cast<NPObject*>(NULL), &variant_);
+ EXPECT_TRUE(NPVARIANT_IS_NULL(variant_));
+}
+
+template <typename T>
+class VariantMatcher : public testing::MatcherInterface<const NPVariant&> {
+ public:
+ explicit VariantMatcher(const T& value) : value_(value) {
+ }
+
+ virtual bool Matches(const NPVariant& variant) const {
+ T other_value;
+ return NPVariantToValue(&other_value, variant) && other_value == value_;
+ }
+
+ virtual void DescribeTo(::std::ostream* os) const {
+ *os << "equals " << value_;
+ }
+
+ virtual void DescribeNegationTo(::std::ostream* os) const {
+ *os << "does not equal " << value_;
+ }
+
+ private:
+ T value_;
+};
+
+template <typename T>
+Matcher<const NPVariant&> VariantMatches(const T& value) {
+ return MakeMatcher(new VariantMatcher<T>(value));
+}
+
+TEST_F(NPVariantUtilsTest, CanInvokeVoidMethodWithNativeTypes) {
+ NPIdentifier name = NPN_GetStringIdentifier("foo");
+ MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+
+ VOID_TO_NPVARIANT(variant_);
+
+ EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
+ .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
+ Return(true)));
+
+ EXPECT_TRUE(NPInvokeVoid(NULL, object, name, 7));
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, InvokeVoidMethodCanFail) {
+ NPIdentifier name = NPN_GetStringIdentifier("foo");
+ MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+
+ VOID_TO_NPVARIANT(variant_);
+
+ EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
+ .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
+ Return(false)));
+
+ EXPECT_FALSE(NPInvokeVoid(NULL, object, name, 7));
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, CanInvokeNonVoidMethodWithNativeTypes) {
+ NPIdentifier name = NPN_GetStringIdentifier("foo");
+ MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+
+ DOUBLE_TO_NPVARIANT(1.5, variant_);
+
+ EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
+ .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
+ Return(true)));
+
+ double r;
+ EXPECT_TRUE(NPInvoke(NULL, object, name, 7, &r));
+ EXPECT_EQ(1.5, r);
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodCanFail) {
+ NPIdentifier name = NPN_GetStringIdentifier("foo");
+ MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+
+ DOUBLE_TO_NPVARIANT(1.5, variant_);
+
+ EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
+ .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
+ Return(false)));
+
+ double r;
+ EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r));
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodFailsIfResultIsIncompatible) {
+ NPIdentifier name = NPN_GetStringIdentifier("foo");
+ MockBaseNPObject* object = static_cast<MockBaseNPObject*>(
+ gpu_plugin::NPN_CreateObject(NULL, np_class_));
+
+ DOUBLE_TO_NPVARIANT(1.5, variant_);
+
+ EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _))
+ .WillOnce(DoAll(SetArgumentPointee<3>(variant_),
+ Return(true)));
+
+ int r;
+ EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r));
+
+ gpu_plugin::NPN_ReleaseObject(object);
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/np_utils/npn_test_stub.cc b/o3d/gpu_plugin/np_utils/npn_test_stub.cc
index 4e8b472..f217711 100644
--- a/o3d/gpu_plugin/np_utils/npn_test_stub.cc
+++ b/o3d/gpu_plugin/np_utils/npn_test_stub.cc
@@ -7,15 +7,19 @@
#include <set>
#include <string>
+#include "o3d/gpu_plugin/np_utils/npn_funcs.h"
#include "o3d/gpu_plugin/np_utils/npn_test_stub.h"
+#include "webkit/glue/plugins/nphostapi.h"
// Simple implementation of subset of the NPN functions for testing.
+namespace o3d {
+namespace gpu_plugin {
+
namespace {
- std::set<std::string> names;
-}
+std::set<std::string> names;
-NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name) {
+NPIdentifier GetStringIdentifier(const NPUTF8* name) {
std::set<std::string>::iterator it = names.find(name);
if (it == names.end()) {
it = names.insert(name).first;
@@ -23,42 +27,97 @@ NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name) {
return const_cast<NPUTF8*>((*it).c_str());
}
-void* NPN_MemAlloc(size_t size) {
+void* MemAlloc(size_t size) {
return malloc(size);
}
-void NPN_MemFree(void* p) {
+void MemFree(void* p) {
free(p);
}
-NPObject* NPN_CreateObject(NPP npp, NPClass* cl) {
+NPObject* CreateObject(NPP npp, NPClass* cl) {
NPObject* object = cl->allocate(npp, cl);
object->referenceCount = 1;
object->_class = cl;
return object;
}
-NPObject* NPN_RetainObject(NPObject* object) {
+NPObject* RetainObject(NPObject* object) {
++object->referenceCount;
return object;
}
-void NPN_ReleaseObject(NPObject* object) {
+void ReleaseObject(NPObject* object) {
--object->referenceCount;
if (object->referenceCount == 0) {
object->_class->deallocate(object);
}
}
-void NPN_ReleaseVariantValue(NPVariant* variant) {
+void ReleaseVariantValue(NPVariant* variant) {
if (NPVARIANT_IS_STRING(*variant)) {
NPN_MemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters));
} else if (NPVARIANT_IS_OBJECT(*variant)) {
- NPN_ReleaseObject(NPVARIANT_TO_OBJECT(*variant));
+ gpu_plugin::NPN_ReleaseObject(NPVARIANT_TO_OBJECT(*variant));
}
}
-bool NPN_Invoke(NPP npp, NPObject* object, NPIdentifier name,
- const NPVariant* args, uint32_t num_args, NPVariant* result) {
+bool Invoke(NPP npp, NPObject* object, NPIdentifier name,
+ const NPVariant* args, uint32_t num_args, NPVariant* result) {
return object->_class->invoke(object, name, args, num_args, result);
}
+} // namespace anonymous
+
+void InitializeNPNTestStub() {
+ static NPNetscapeFuncs funcs = {
+ sizeof(NPNetscapeFuncs),
+ NP_VERSION_MINOR,
+ NULL, // geturl
+ NULL, // posturl
+ NULL, // requestread
+ NULL, // newstream
+ NULL, // write
+ NULL, // destroystream
+ NULL, // status
+ NULL, // uagent
+ MemAlloc,
+ MemFree,
+ NULL, // memflush
+ NULL, // reloadplugins
+ NULL, // getJavaEnv
+ NULL, // getJavaPeer
+ NULL, // geturlnotify
+ NULL, // posturlnotify
+ NULL, // getvalue
+ NULL, // setvalue
+ NULL, // invalidaterect
+ NULL, // invalidateregion
+ NULL, // forceredraw
+ GetStringIdentifier,
+ NULL, // getstringidentifiers
+ NULL, // getintidentifier
+ NULL, // identifierisstring
+ NULL, // utf8fromidentifier
+ NULL, // intfromidentifier
+ CreateObject,
+ RetainObject,
+ ReleaseObject,
+ Invoke,
+ NULL, // invokeDefault
+ NULL, // evaluate
+ NULL, // getproperty
+ NULL, // setproperty
+ NULL, // removeproperty
+ NULL, // hasproperty
+ NULL, // hasmethod
+ ReleaseVariantValue,
+ };
+ SetBrowserFuncs(&funcs);
+}
+
+void ShutdownNPNTestStub() {
+ SetBrowserFuncs(NULL);
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/np_utils/npn_test_stub.h b/o3d/gpu_plugin/np_utils/npn_test_stub.h
index 9567c17..6b07416 100644
--- a/o3d/gpu_plugin/np_utils/npn_test_stub.h
+++ b/o3d/gpu_plugin/np_utils/npn_test_stub.h
@@ -8,16 +8,14 @@
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
+namespace o3d {
+namespace gpu_plugin {
+
// Simple implementation of subset of the NPN functions for testing.
+void InitializeNPNTestStub();
+void ShutdownNPNTestStub();
-NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name);
-void* NPN_MemAlloc(size_t size);
-void NPN_MemFree(void* p);
-NPObject* NPN_CreateObject(NPP npp, NPClass* cl);
-NPObject* NPN_RetainObject(NPObject* object);
-void NPN_ReleaseObject(NPObject* object);
-void NPN_ReleaseVariantValue(NPVariant* variant);
-bool NPN_Invoke(NPP npp, NPObject* object, NPIdentifier name,
- const NPVariant* args, uint32_t num_args, NPVariant* result);
+} // namespace gpu_plugin
+} // namespace o3d
#endif // O3D_GPU_PLUGIN_NP_UTILS_NPN_TEST_STUB_H_
diff --git a/o3d/gpu_plugin/plugin_object.h b/o3d/gpu_plugin/plugin_object.h
new file mode 100644
index 0000000..d04dd07
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object.h
@@ -0,0 +1,45 @@
+// 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_PLUGIN_OBJECT_H_
+#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_H_
+
+#include "third_party/npapi/bindings/npapi.h"
+#include "third_party/npapi/bindings/npruntime.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+// Interface for a plugin instance. The NPP plugin calls forwards to an instance
+// of this interface.
+class PluginObject {
+ public:
+ PluginObject() {
+ }
+
+ virtual ~PluginObject() {
+ }
+
+ virtual NPError New(NPMIMEType plugin_type,
+ int16 argc,
+ char* argn[],
+ char* argv[],
+ NPSavedData* saved) = 0;
+
+ virtual NPError SetWindow(NPWindow* new_window) = 0;
+
+ virtual int16 HandleEvent(NPEvent* event) = 0;
+
+ virtual NPError Destroy(NPSavedData** saved) = 0;
+
+ virtual NPObject* GetScriptableInstance() = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PluginObject);
+};
+
+} // namespace gpu_plugin
+} // namespace o3d
+
+#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_H_
diff --git a/o3d/gpu_plugin/plugin_object_factory.cc b/o3d/gpu_plugin/plugin_object_factory.cc
new file mode 100644
index 0000000..5165f0d
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object_factory.cc
@@ -0,0 +1,30 @@
+// 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/gpu_plugin_object.h"
+#include "o3d/gpu_plugin/plugin_object_factory.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+PluginObjectFactory::PluginObjectFactory() {
+}
+
+PluginObjectFactory::~PluginObjectFactory() {
+}
+
+PluginObject* PluginObjectFactory::CreatePluginObject(NPP npp,
+ NPMIMEType plugin_type) {
+ if (strcmp(plugin_type, GPUPluginObject::kPluginType) == 0) {
+ NPClass* np_class = const_cast<NPClass*>(
+ BaseNPObject::GetNPClass<GPUPluginObject>());
+ return static_cast<GPUPluginObject*>(
+ gpu_plugin::NPN_CreateObject(npp, np_class));
+ }
+
+ return NULL;
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/plugin_object_factory.h b/o3d/gpu_plugin/plugin_object_factory.h
new file mode 100644
index 0000000..3d10866
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object_factory.h
@@ -0,0 +1,32 @@
+// 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_PLUGIN_OBJECT_FACTORY_H_
+#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_H_
+
+#include "third_party/npapi/bindings/npapi.h"
+#include "third_party/npapi/bindings/npruntime.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+class PluginObject;
+
+// Mockable factory used to create instances of PluginObject based on plugin
+// mime type.
+class PluginObjectFactory {
+ public:
+ PluginObjectFactory();
+ virtual ~PluginObjectFactory();
+
+ virtual PluginObject* CreatePluginObject(NPP npp, NPMIMEType plugin_type);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PluginObjectFactory);
+};
+
+} // namespace gpu_plugin
+} // namespace o3d
+
+#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_H_
diff --git a/o3d/gpu_plugin/plugin_object_factory_mock.h b/o3d/gpu_plugin/plugin_object_factory_mock.h
new file mode 100644
index 0000000..8640c7d
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object_factory_mock.h
@@ -0,0 +1,25 @@
+// 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_PLUGIN_OBJECT_FACTORY_MOCK_H_
+#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_MOCK_H_
+
+#include "o3d/gpu_plugin/plugin_object_factory.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+// Mockable factory used to create instances of PluginObject based on plugin
+// mime type.
+class MockPluginObjectFactory : public PluginObjectFactory {
+ public:
+ MOCK_METHOD2(CreatePluginObject, PluginObject*(NPP, NPMIMEType));
+};
+
+} // namespace gpu_plugin
+} // namespace o3d
+
+#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_MOCK_H_
diff --git a/o3d/gpu_plugin/plugin_object_factory_unittest.cc b/o3d/gpu_plugin/plugin_object_factory_unittest.cc
new file mode 100644
index 0000000..a5fc3d2
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object_factory_unittest.cc
@@ -0,0 +1,42 @@
+// 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/gpu_plugin_object.h"
+#include "o3d/gpu_plugin/plugin_object_factory.h"
+#include "o3d/gpu_plugin/np_utils/npn_test_stub.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+class PluginObjectFactoryTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ InitializeNPNTestStub();
+ factory_ = new PluginObjectFactory;
+ }
+
+ virtual void TearDown() {
+ delete factory_;
+ ShutdownNPNTestStub();
+ }
+
+ PluginObjectFactory* factory_;
+};
+
+TEST_F(PluginObjectFactoryTest, ReturnsNullForUnknownMimeType) {
+ PluginObject* plugin_object = factory_->CreatePluginObject(
+ NULL, "application/unknown");
+ EXPECT_TRUE(NULL == plugin_object);
+}
+
+TEST_F(PluginObjectFactoryTest, CreatesGPUPlugin) {
+ PluginObject* plugin_object = factory_->CreatePluginObject(
+ NULL, const_cast<NPMIMEType>(GPUPluginObject::kPluginType));
+ EXPECT_TRUE(NULL != plugin_object);
+}
+
+} // namespace gpu_plugin
+} // namespace o3d
diff --git a/o3d/gpu_plugin/plugin_object_mock.h b/o3d/gpu_plugin/plugin_object_mock.h
new file mode 100644
index 0000000..904e757
--- /dev/null
+++ b/o3d/gpu_plugin/plugin_object_mock.h
@@ -0,0 +1,27 @@
+// 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_PLUGIN_OBJECT_MOCK_H_
+#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_MOCK_H_
+
+#include "o3d/gpu_plugin/plugin_object.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace o3d {
+namespace gpu_plugin {
+
+class MockPluginObject : public PluginObject {
+ public:
+ MOCK_METHOD5(New, NPError(NPMIMEType, int16, char*[], char*[], NPSavedData*));
+ MOCK_METHOD1(SetWindow, NPError(NPWindow*));
+ MOCK_METHOD1(HandleEvent, int16(NPEvent*));
+ MOCK_METHOD1(Destroy, NPError(NPSavedData**));
+ MOCK_METHOD0(GetScriptableInstance, NPObject*());
+};
+
+} // namespace gpu_plugin
+} // namespace o3d
+
+#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_MOCK_H_