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 "content/test/plugin/plugin_npobject_identity_test.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
12 class NPThingy : public NPObject {
14 NPThingy() : NPObject() {}
16 static NPObject* Allocate(NPP npp, NPClass* npclass) {
17 return new NPThingy();
20 static void Deallocate(NPObject* npobject) {
21 delete static_cast<NPThingy*>(npobject);
25 NPClass* GetNPThingyClass() {
26 static NPClass plugin_class = {
27 NP_CLASS_STRUCT_VERSION,
33 NULL, // InvokeDefault
37 NULL, // RemoveProperty
45 namespace NPAPIClient {
47 NPObjectIdentityTest::NPObjectIdentityTest(NPP id,
48 NPNetscapeFuncs *host_functions)
49 : PluginTest(id, host_functions) {
52 NPError NPObjectIdentityTest::SetWindow(NPWindow* pNPWindow) {
53 #if !defined(OS_MACOSX)
54 if (pNPWindow->window == NULL)
55 return NPERR_NO_ERROR;
58 NPIdentifier are_these_the_same_id = HostFunctions()->getstringidentifier(
61 // Get a function from window.areTheseTheSame.
63 HostFunctions()->getvalue(id(), NPNVWindowNPObject, &window);
65 HostFunctions()->getproperty(id(), window, are_these_the_same_id, &func_var);
66 NPObject* func = NPVARIANT_TO_OBJECT(func_var);
68 // Create a custom NPObject and pass it in both arguments to areTheseTheSame.
69 NPObject* thingy = HostFunctions()->createobject(id(), GetNPThingyClass());
70 NPVariant func_args[2];
71 OBJECT_TO_NPVARIANT(thingy, func_args[0]);
72 OBJECT_TO_NPVARIANT(thingy, func_args[1]);
73 NPVariant were_the_same_var;
74 HostFunctions()->invokeDefault(id(), func, (const NPVariant*)&func_args, 2,
77 // Confirm that JavaScript could see that the objects were the same.
78 bool were_the_same = NPVARIANT_TO_BOOLEAN(were_the_same_var);
80 SetError("Identity was lost in passing from NPAPI into JavaScript.");
82 HostFunctions()->releaseobject(thingy);
84 // If this test failed, then we'd have crashed by now.
85 SignalTestCompleted();
87 return NPERR_NO_ERROR;
90 } // namespace NPAPIClient