changeset 7162:223c57a7a73d

RT-30622: jfxrt.jar has a duplicate JSObject class without getWindow(Applet) Reviewed-by: ddehaven, ddhill
author kcr
date Thu, 22 May 2014 08:44:48 -0700
parents dd892a7f8463
children 1e023b779912
files modules/graphics/src/main/java/netscape/javascript/JSObject.java modules/graphics/src/test/java/netscape/javascript/JSObjectTest.java
diffstat 2 files changed, 164 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/netscape/javascript/JSObject.java	Thu May 29 14:51:49 2014 -0700
+++ b/modules/graphics/src/main/java/netscape/javascript/JSObject.java	Thu May 22 08:44:48 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,9 @@
 
 package netscape.javascript;
 
+import java.applet.Applet;
 
-// FIXME: need URL on java.sun.com for new LiveConnect spec
+// FIXME: need URL on oracle.com for new LiveConnect spec
 
 /**
  * <P> Allows Java code to manipulate JavaScript objects. </P>
@@ -41,10 +42,11 @@
  * <P> Any data returned from the JavaScript engine to Java is
  * converted to Java data types. Certain data passed to the JavaScript
  * engine is converted to JavaScript data types. See the section on <A
- * HREF="http://jdk6.java.net/plugin2/liveconnect/index.html#JAVA_JS_CONVERSIONS">
- * Data Type Conversions</A> in the <A
- * HREF="http://jdk6.java.net/plugin2/liveconnect">LiveConnect Specification</A>
- * for details on how values are converted. </P>
+ * HREF="https://jdk6.dev.java.net/plugin2/liveconnect/#JAVA_JS_CONVERSIONS">Data
+ * Type Conversions</A> in the <A
+ * HREF="https://jdk6.dev.java.net/plugin2/liveconnect/">new
+ * LiveConnect Specification</A> for details on how values are
+ * converted. </P>
  *
  */
 public abstract class JSObject {
@@ -62,7 +64,7 @@
      * </p>
      *
      * @param methodName The name of the JavaScript method to be invoked.
-     * @param args An array of Java object to be passed as arguments to the method.
+     * @param args the Java objects passed as arguments to the method.
      * @return Result of the method.
      */
     public abstract Object call(String methodName, Object... args) throws JSException;
@@ -126,15 +128,18 @@
      */
     public abstract void setSlot(int index, Object value) throws JSException;
 
-    /* *
+    /**
      * <p> Returns a JSObject for the window containing the given applet.
      * </p>
      *
      * @param applet The applet.
      * @return JSObject for the window containing the given applet.
-     * /
+     */
     public static JSObject getWindow(Applet applet) throws JSException {
 
+        throw new JSException("Unexpected error: This method should not be used unless loaded from plugin.jar");
+
+/*
         try
         {
             if (applet != null)
@@ -170,12 +175,16 @@
                 // new code for CustomProgress to get the JSObject w/o applet
                 AppContext ac = ToolkitStore.get().getAppContext();
                 if (ac != null) {
-                    Object context = ac.get(sun.plugin2.applet.Plugin2Manager.APPCONTEXT_PLUGIN2HOST_KEY);
-                    if (context != null && (context instanceof JSContext)) {
-                        JSContext jsc = (JSContext) context;
-                        JSObject ret = jsc.getOneWayJSObject();
-                        if (ret != null) {
-                           return ret;
+                    Plugin2Context context = (Plugin2Context)
+                            ac.get(sun.plugin2.applet.Plugin2Manager.APPCONTEXT_PLUGIN2CTX_KEY);
+                    if (context != null) {
+                        Applet2Host host = context.getHost();
+                        if (host != null && host instanceof JSContext) {
+                            JSContext jsc = (JSContext) host;
+                            JSObject ret = jsc.getOneWayJSObject();
+                            if (ret != null) {
+                               return ret;
+                            }
                         }
                     }
                 }
@@ -187,6 +196,6 @@
         }
 
         throw new JSException();
+*/
     }
-    */
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/graphics/src/test/java/netscape/javascript/JSObjectTest.java	Thu May 22 08:44:48 2014 -0700
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package netscape.javascript;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Test for JSObject class.
+ */
+public class JSObjectTest {
+
+    @Test
+    public void testGetWindow() {
+        try {
+            JSObject.getWindow(null);
+            fail("Did not get the expected JSException");
+        } catch (JSException ex) {
+            String msg = ex.getMessage();
+            assertNotNull(msg);
+            assertTrue(msg.startsWith("Unexpected error:"));
+            assertTrue(msg.endsWith("plugin.jar"));
+        }
+    }
+
+    @Test
+    public void testCall() {
+        JSObject jso = new JSObject() {
+            @Override
+            public Object call(String methodName, Object... args) throws JSException {
+                List<String> list = new ArrayList<>();
+                if (args != null) {
+                    for (Object arg : args) {
+                        list.add((String)arg);
+                    }
+                }
+                return list;
+            }
+
+            @Override
+            public Object eval(String s) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+
+            @Override
+            public Object getMember(String name) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+
+            @Override
+            public void setMember(String name, Object value) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+
+            @Override
+            public void removeMember(String name) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+
+            @Override
+            public Object getSlot(int index) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+
+            @Override
+            public void setSlot(int index, Object value) throws JSException {
+                throw new UnsupportedOperationException("Not supported yet.");
+            }
+        };
+
+        List<String> list;
+
+        list = (List<String>)jso.call("");
+        assertTrue(list.isEmpty());
+
+        list = (List<String>)jso.call("", new Object[0]);
+        assertTrue(list.isEmpty());
+
+        list = (List<String>)jso.call("", (Object[])null);
+        assertTrue(list.isEmpty());
+
+        list = (List<String>)jso.call("", null);
+        assertTrue(list.isEmpty());
+
+        list = (List<String>)jso.call("", (Object)null);
+        assertFalse(list.isEmpty());
+        assertEquals(1, list.size());
+        assertNull(list.get(0));
+
+        list = (List<String>)jso.call("", "str1");
+        assertFalse(list.isEmpty());
+        assertEquals(1, list.size());
+        assertEquals("str1", list.get(0));
+
+        list = (List<String>)jso.call("", new Object[] { "str1" });
+        assertFalse(list.isEmpty());
+        assertEquals(1, list.size());
+        assertEquals("str1", list.get(0));
+
+        list = (List<String>)jso.call("", "str1", "str2");
+        assertFalse(list.isEmpty());
+        assertEquals(2, list.size());
+        assertEquals("str1", list.get(0));
+        assertEquals("str2", list.get(1));
+
+        list = (List<String>)jso.call("", new Object[] { "str1", "str2" });
+        assertFalse(list.isEmpty());
+        assertEquals(2, list.size());
+        assertEquals("str1", list.get(0));
+        assertEquals("str2", list.get(1));
+    }
+
+}