source: trunk/icedtea-web/tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc@ 443

Last change on this file since 443 was 427, checked in by dmik, 11 years ago

icedtea-web: Import version 1.5.1 from vendor.

File size: 5.3 KB
Line 
1/* Copyright (C) 2012 Red Hat
2
3 This file is part of IcedTea.
4
5 IcedTea is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 IcedTea is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version. */
36
37#include <UnitTest++.h>
38
39#include <npapi.h>
40
41#include "browser_mock.h"
42#include "MemoryLeakDetector.h"
43
44#include "IcedTeaScriptablePluginObject.h"
45#include "IcedTeaPluginUtils.h"
46
47static NPP_t dummy_npp = {0,0};
48
49SUITE(IcedTeaScriptablePluginObject) {
50 TEST(destructor) {
51 MemoryLeakDetector leak_detector;
52 IcedTeaScriptablePluginObject* obj = new IcedTeaScriptablePluginObject(&dummy_npp);
53 delete obj;
54 CHECK(leak_detector.memory_leaks() == 0);
55 }
56}
57
58SUITE(IcedTeaScriptableJavaObject) {
59 TEST(deallocate) {
60 MemoryLeakDetector leak_detector;
61 IcedTeaScriptableJavaObject* obj = new IcedTeaScriptableJavaObject(&dummy_npp);
62 IcedTeaScriptableJavaObject::deAllocate(obj);
63 CHECK(leak_detector.memory_leaks() == 0);
64 }
65
66 TEST(get_scriptable_java_object) {
67 MemoryLeakDetector leak_detector;
68
69 NPObject* first_obj = IcedTeaScriptableJavaObject::get_scriptable_java_object(&dummy_npp, "DummyClass", "DummyInstance", false);
70 browser_functions.releaseobject(first_obj);
71
72 /* After the first call, the object should be cached in the object map */
73 NPObject* second_obj = IcedTeaScriptableJavaObject::get_scriptable_java_object(&dummy_npp, "DummyClass", "DummyInstance", false);
74
75 /* Objects should be the same, because of caching */
76 CHECK(first_obj == second_obj);
77
78 browser_functions.releaseobject(second_obj);
79
80 CHECK(leak_detector.memory_leaks() == 0);
81 }
82}
83
84SUITE(IcedTeaScriptableJavaPackageObject) {
85 TEST(deallocate) {
86 MemoryLeakDetector leak_detector;
87 IcedTeaScriptableJavaPackageObject* obj = new IcedTeaScriptableJavaPackageObject(&dummy_npp);
88 IcedTeaScriptableJavaPackageObject::deAllocate(obj);
89 CHECK(leak_detector.memory_leaks() == 0);
90 }
91
92 TEST(get_scriptable_java_object) {
93 MemoryLeakDetector leak_detector;
94 NPObject* obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_package_object(&dummy_npp, "DummyPackage");
95 browser_functions.releaseobject(obj);
96 CHECK(leak_detector.memory_leaks() == 0);
97 }
98
99 static NPVariant np_checked_get(NPObject* obj, const char* identifier) {
100 NPVariant result;
101 CHECK(browser_functions.getproperty(&dummy_npp, obj, browser_functions.getstringidentifier(identifier), &result));
102 return result;
103 }
104
105 /* NOTICE: Requires icedtea-web Java-side to be running!
106 * Loads java.lang.Integer.MAX_VALUE */
107 TEST(getProperty) {
108 MemoryLeakDetector leak_detector;
109 /* Ensure destruction */{
110 /* Get the 'root' package */
111 NPObject* obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_package_object(&dummy_npp, "");
112 // Look up java.lang.Integer.MAX_VALUE
113 NPVariant java_result = np_checked_get(obj, "java");
114 NPVariant lang_result = np_checked_get(NPVARIANT_TO_OBJECT(java_result), "lang");
115 NPVariant integer_result = np_checked_get(NPVARIANT_TO_OBJECT(lang_result), "Integer");
116 NPVariant max_value_result = np_checked_get(NPVARIANT_TO_OBJECT(integer_result), "MAX_VALUE");
117
118 // Check that it is indeed equal to 2147483647
119 CHECK(NPVARIANT_IS_INT32(max_value_result));
120 CHECK_EQUAL(2147483647, NPVARIANT_TO_INT32(max_value_result));
121
122 browser_functions.releasevariantvalue(&java_result);
123 browser_functions.releasevariantvalue(&lang_result);
124 browser_functions.releasevariantvalue(&integer_result);
125 browser_functions.releasevariantvalue(&max_value_result);
126
127 browser_functions.releaseobject(obj);
128 }
129 CHECK(leak_detector.memory_leaks() == 0);
130 }
131}
Note: See TracBrowser for help on using the repository browser.