You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by sm...@apache.org on 2012/08/08 16:34:24 UTC

svn commit: r1370785 - in /pivot/branches/2.0.x/tests/src/org/apache/pivot/tests: JavascriptConsoleTest.java javascript_console_test.bxml

Author: smartini
Date: Wed Aug  8 14:34:22 2012
New Revision: 1370785

URL: http://svn.apache.org/viewvc?rev=1370785&view=rev
Log:
JavaScript Console Test: finish test 4 and put a TODO for a (future) test 5 inside it

Modified:
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml

Modified: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java?rev=1370785&r1=1370784&r2=1370785&view=diff
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java (original)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java Wed Aug  8 14:34:22 2012
@@ -17,6 +17,8 @@
 package org.apache.pivot.tests;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 
 import org.apache.pivot.beans.BXMLSerializer;
 import org.apache.pivot.collections.Map;
@@ -32,7 +34,7 @@ public class JavascriptConsoleTest exten
 
     @Override
     public void startup(Display displayArgument, Map<String, String> properties) throws Exception {
-        System.out.println("startup: start");
+        logObject("startup: start");
 
         this.display = displayArgument;
 
@@ -40,13 +42,13 @@ public class JavascriptConsoleTest exten
 
         // add a reference to the application itself in bxml namespace, to be used by JS inside bxml files
         bxmlSerializer.getNamespace().put("application", this);
-        System.out.println("put a reference to application in serializer namespace");
+        logObject("put a reference to application in serializer namespace");
 
         window = loadWindow("javascript_console_test.bxml", bxmlSerializer);
         initializeFields(bxmlSerializer);
         window.open(display);
 
-        System.out.println("startup: end");
+        logObject("startup: end");
     }
 
     @Override
@@ -59,21 +61,87 @@ public class JavascriptConsoleTest exten
     }
 
     private void initializeFields(BXMLSerializer serializer) {
-        System.out.println("initializeFields: start");
+        logObject("initializeFields: start");
 
-        System.out.println("got BXMLSerializer instance = " + serializer);
+        logObject("got BXMLSerializer instance = " + serializer);
 
-        System.out.println("initializeFields: end");
+        logObject("initializeFields: end");
     }
 
-    protected Window loadWindow(String fileName, BXMLSerializer bxmlSerializer)
+    /**
+     * Load (and returns) a Window, given its file name and serializer to use
+     *
+     * @param fileName the file name for the bxml file to load
+     * @param bxmlSerializer the serializer to use, or if null a new one will be created
+     * @return the Window instance
+     * @throws SerializationException in case of error
+     * @throws IOException in case of error
+     */
+    private Window loadWindow(String fileName, BXMLSerializer bxmlSerializer)
         throws SerializationException, IOException {
+        logObject("loadWindow from \"" + fileName + "\", with the serializer " + bxmlSerializer);
+
         if (bxmlSerializer == null) {
             bxmlSerializer = new BXMLSerializer();
         }
+
         return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName);
     }
 
+    /**
+     * Load (and returns) a Window, given its URL and serializer to use
+     * <p>
+     * Note that if public this method could be called even from JS in a bxml file
+     * (but a reference to the current application has to be put in serializer namespace).
+     * <p>
+     * Note that all Exceptions are catched inside this method, to not expose them to JS code.
+     *
+     * @param url the URL of the bxml file to load
+     * @param bxmlSerializer the serializer to use, or if null a new one will be created
+     * @return the Window instance
+     */
+    public Window loadWindowFromURL(String url, BXMLSerializer bxmlSerializer) {
+        logObject("loadWindow from \"" + url + "\", with the serializer " + bxmlSerializer);
+
+        if (bxmlSerializer == null) {
+            bxmlSerializer = new BXMLSerializer();
+        }
+
+        Window loadedWindow = null;
+        try {
+            loadedWindow = (Window)bxmlSerializer.readObject(new URL(url));
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (SerializationException e) {
+            e.printStackTrace();
+        }
+
+        return loadedWindow;
+    }
+
+
+    /**
+     * Sample utility method to log a formatted dump of the given object to System.out .
+     * <p>
+     * Note that it has been set public, static, and accepting Object (and not String as usual),
+     * even to make some tests on it from JS code.
+     *
+     * @param msg the object (or message) to log
+     */
+    public static final void logObject(Object obj) {
+        if (obj != null) {
+            System.out.println(new java.util.Date() + ", log: { class: \"" + obj.getClass().getName() + "\", msg:\"" + obj + "\" }");
+        }
+    }
+
+
+    /**
+     * Application entry point, when run as a Standard (Desktop) Java Application.
+     *
+     * @param args command line arguments
+     */
     public static void main(String[] args) {
         DesktopApplicationContext.main(JavascriptConsoleTest.class, args);
     }

Modified: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml?rev=1370785&r1=1370784&r2=1370785&view=diff
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml (original)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml Wed Aug  8 14:34:22 2012
@@ -26,7 +26,7 @@ limitations under the License.
     <bxml:script>
     <![CDATA[
     // note that this script could be moved in an external js file, like that already included in this file ...
-    log("inline script 1");
+    log("inline script 1 - start");
 
     // JS variables for (remote) resources
     // needed for test 2 and 4
@@ -34,6 +34,8 @@ limitations under the License.
     var frameURL = baseURL + "/frame.bxml";
     // note that bxml files must have the MIME Type "application/bxml" (as requested by BXMLSerializer),
     // or (BXMLSerializer) will try to get it from its file extension ...
+
+    log("inline script 1 - end");
     ]]>
     </bxml:script>
 
@@ -62,20 +64,37 @@ limitations under the License.
     <bxml:script>
     <![CDATA[
     // note that this script could be moved in an external js file, like that already included in this file ...
-    importPackage(java.net);
+    importPackage(org.apache.pivot.beans);  // for BXMLSerializer
     importPackage(org.apache.pivot.util);
     importPackage(org.apache.pivot.wtk);
-    importPackage(org.apache.pivot.wtk.media);
 
-    log("inline script 2");
+    log("inline script 2 - start");
 
-// TODO ...
-    // test 4, TODO
+    // test 4, ok
     // define the variable in JS, and load it by instancing Java classes from here
     // log("frameURL is \"" + frameURL + "\"");  // show that the required value is already defined
+    log("application is " + application);  // reference to the application itself, set in main class, in Java Code
 
     // JS variables for (remote) resources
-    var testRemoteFrame = "";
+    var bxmlSerializer = new BXMLSerializer();  // trick, I hope a good one ...
+    log("bxmlSerializer is " + bxmlSerializer);
+    // load the window/frame, but using an utility method defined in the application ...
+    log("load the window/frame now, by JS code (calling Java code from the application) ...");
+    var loadedWindow = application.loadWindowFromURL(frameURL, bxmlSerializer);
+    log("loadedWindow is " + loadedWindow + ", and its JavaScript typeof here is " + typeof loadedWindow);  // note that typeof loadedWindow is object
+
+    var testRemoteFrame = loadedWindow;
+    // log formatted info but using a static method defined in the application, just for test ...
+    // note that this call is done in the usual (non-static) way, and works the same even from here
+    application.logObject(testRemoteFrame);
+
+
+    // test 5, TODO
+    // isolate a JS snippet (but self-contained) derive from test 4, and
+    // find a way to execute it as the text written in the textArea in a JS interpreter,
+    // then put it in a fixed template, and generalize it inside this test/sample application ...
+
+    log("inline script 2 - end");
     ]]>
     </bxml:script>