1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package myapp5;
  27 
  28 import javafx.application.Application;
  29 import javafx.concurrent.Worker;
  30 import javafx.scene.web.WebView;
  31 import javafx.stage.Stage;
  32 import netscape.javascript.JSObject;
  33 
  34 import myapp5.pkg1.MyCallback;
  35 
  36 import static myapp5.Constants.*;
  37 
  38 /**
  39  * Modular test application for testing Javascript callback.
  40  * This is launched by ModuleLauncherTest.
  41  */
  42 public class AppJSCallbackUnexported extends Application {
  43 
  44     private final MyCallback callback = new MyCallback();
  45 
  46     /**
  47      * @param args the command line arguments
  48      */
  49     public static void main(String[] args) {
  50         try {
  51             Application.launch(args);
  52         } catch (Throwable t) {
  53             System.err.println("ERROR: caught unexpected exception: " + t);
  54             t.printStackTrace(System.err);
  55             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  56         }
  57     }
  58 
  59     @Override
  60     public void start(Stage stage) throws Exception {
  61         try {
  62             final WebView webView = new WebView();
  63             webView.getEngine().getLoadWorker().stateProperty().addListener((ov, o, n) -> {
  64                 if (n == Worker.State.SUCCEEDED) {
  65                     try {
  66                         final JSObject window = (JSObject) webView.getEngine().executeScript("window");
  67                         Util.assertNotNull(window);
  68                         window.setMember("javaCallback", callback);
  69                         webView.getEngine().executeScript("document.getElementById(\"mybtn1\").click()");
  70                         Util.assertEquals(0, callback.getCount());
  71                         System.exit(ERROR_NONE);
  72                     } catch (Throwable t) {
  73                         t.printStackTrace(System.err);
  74                         System.exit(ERROR_ASSERTION_FAILURE);
  75                     }
  76                 }
  77             });
  78             webView.getEngine().loadContent(Util.content);
  79         } catch (Error | Exception ex) {
  80             System.err.println("ERROR: caught unexpected exception: " + ex);
  81             ex.printStackTrace(System.err);
  82             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  83         }
  84     }
  85 
  86 }