2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #include "PluginTest.h"
28 #include "PluginObject.h"
32 // NPP_SetWindow should be called with a null window handle as destruction begins on non-Mac platforms.
34 class NPPSetWindowCalledDuringDestruction : public PluginTest {
36 NPPSetWindowCalledDuringDestruction(NPP, const string& identifier);
38 void setWillBeDestroyed() { m_willBeDestroyed = true; }
41 struct ScriptObject : Object<ScriptObject> {
42 bool hasMethod(NPIdentifier);
43 bool invoke(NPIdentifier, const NPVariant*, uint32_t, NPVariant*);
46 virtual NPError NPP_GetValue(NPPVariable, void*);
47 virtual NPError NPP_SetWindow(NPWindow*);
48 virtual NPError NPP_Destroy(NPSavedData**);
50 bool m_willBeDestroyed;
51 bool m_setWindowCalledBeforeDestruction;
52 bool m_setWindowCalledDuringDestruction;
55 static PluginTest::Register<NPPSetWindowCalledDuringDestruction> registrar("npp-set-window-called-during-destruction");
57 NPPSetWindowCalledDuringDestruction::NPPSetWindowCalledDuringDestruction(NPP npp, const string& identifier)
58 : PluginTest(npp, identifier)
59 , m_willBeDestroyed(false)
60 , m_setWindowCalledBeforeDestruction(false)
61 , m_setWindowCalledDuringDestruction(false)
65 NPError NPPSetWindowCalledDuringDestruction::NPP_GetValue(NPPVariable variable, void* value)
67 if (variable != NPPVpluginScriptableNPObject)
68 return NPERR_GENERIC_ERROR;
70 *static_cast<NPObject**>(value) = ScriptObject::create(this);
72 return NPERR_NO_ERROR;
75 NPError NPPSetWindowCalledDuringDestruction::NPP_SetWindow(NPWindow* window)
77 if (m_willBeDestroyed) {
78 m_setWindowCalledDuringDestruction = true;
79 if (!m_setWindowCalledBeforeDestruction) {
80 log("Fail: setWillBeDestroyed() was called before the initial NPP_SetWindow call");
81 return NPERR_NO_ERROR;
85 log("Fail: NPP_SetWindow passed a non-null window during plugin destruction");
87 return NPERR_NO_ERROR;
90 if (m_setWindowCalledBeforeDestruction) {
91 log("Fail: NPP_SetWindow called more than once before plugin destruction");
92 return NPERR_NO_ERROR;
95 m_setWindowCalledBeforeDestruction = true;
96 return NPERR_NO_ERROR;
99 NPError NPPSetWindowCalledDuringDestruction::NPP_Destroy(NPSavedData**)
102 bool shouldHaveBeenCalledDuringDestruction = false;
104 bool shouldHaveBeenCalledDuringDestruction = true;
107 if (m_setWindowCalledDuringDestruction == shouldHaveBeenCalledDuringDestruction)
108 log("Success: NPP_SetWindow %s called during plugin destruction", shouldHaveBeenCalledDuringDestruction ? "was" : "was not");
110 log("Fail: NPP_SetWindow %s called during plugin destruction", shouldHaveBeenCalledDuringDestruction ? "was not" : "was");
112 return NPERR_NO_ERROR;
115 bool NPPSetWindowCalledDuringDestruction::ScriptObject::hasMethod(NPIdentifier methodName)
117 return methodName == pluginTest()->NPN_GetStringIdentifier("setWillBeDestroyed");
120 bool NPPSetWindowCalledDuringDestruction::ScriptObject::invoke(NPIdentifier identifier, const NPVariant*, uint32_t, NPVariant*)
122 assert(identifier == pluginTest()->NPN_GetStringIdentifier("setWillBeDestroyed"));
123 static_cast<NPPSetWindowCalledDuringDestruction*>(pluginTest())->setWillBeDestroyed();