1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "PluginTest.h"
27
28using namespace std;
29
30
31class NPRuntimeRemoveProperty : public PluginTest {
32public:
33    NPRuntimeRemoveProperty(NPP npp, const string& identifier)
34        : PluginTest(npp, identifier)
35    {
36    }
37
38private:
39    struct TestObject : Object<TestObject> {
40    public:
41        bool hasMethod(NPIdentifier methodName)
42        {
43            return methodName == pluginTest()->NPN_GetStringIdentifier("testRemoveProperty");
44        }
45
46        bool invoke(NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
47        {
48            assert(methodName == pluginTest()->NPN_GetStringIdentifier("testRemoveProperty"));
49
50            if (argumentCount != 2)
51                return false;
52
53            if (!NPVARIANT_IS_OBJECT(arguments[0]))
54                return false;
55
56            if (!NPVARIANT_IS_STRING(arguments[1]) && !NPVARIANT_IS_DOUBLE(arguments[1]))
57                return false;
58
59            NPIdentifier propertyName;
60            if (NPVARIANT_IS_STRING(arguments[1])) {
61                string propertyNameString(arguments[1].value.stringValue.UTF8Characters,
62                                          arguments[1].value.stringValue.UTF8Length);
63
64                propertyName = pluginTest()->NPN_GetStringIdentifier(propertyNameString.c_str());
65            } else {
66                int32_t number = arguments[1].value.doubleValue;
67                propertyName = pluginTest()->NPN_GetIntIdentifier(number);
68            }
69
70            pluginTest()->NPN_RemoveProperty(NPVARIANT_TO_OBJECT(arguments[0]), propertyName);
71
72            VOID_TO_NPVARIANT(*result);
73            return true;
74        }
75    };
76
77    virtual NPError NPP_GetValue(NPPVariable variable, void *value)
78    {
79        if (variable != NPPVpluginScriptableNPObject)
80            return NPERR_GENERIC_ERROR;
81
82        *(NPObject**)value = TestObject::create(this);
83
84        return NPERR_NO_ERROR;
85    }
86
87};
88
89static PluginTest::Register<NPRuntimeRemoveProperty> npRuntimeRemoveProperty("npruntime-remove-property");
90