1 package test.javafx.fxml;
   2 /*
   3  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 import com.sun.javafx.fxml.FXMLLoaderHelper;
  28 import org.junit.Test;
  29 
  30 import java.io.IOException;
  31 import java.util.concurrent.atomic.AtomicBoolean;
  32 import javafx.fxml.FXMLLoader;
  33 import javafx.fxml.LoadListener;
  34 
  35 import static org.junit.Assert.*;
  36 
  37 public class FXMLLoader_ScriptTest {
  38     @Test
  39     @SuppressWarnings("deprecation")
  40     public void testStaticScriptLoad() throws IOException {
  41         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("static_script_load.fxml"));
  42         FXMLLoaderHelper.setStaticLoad(fxmlLoader, true);
  43         AtomicBoolean scriptCalled = new AtomicBoolean();
  44         AtomicBoolean scriptEndCalled = new AtomicBoolean();
  45         fxmlLoader.setLoadListener(new LoadListener() {
  46 
  47             @Override
  48             public void readImportProcessingInstruction(String target) {
  49             }
  50 
  51             @Override
  52             public void readLanguageProcessingInstruction(String language) {
  53             }
  54 
  55             @Override
  56             public void readComment(String comment) {
  57             }
  58 
  59             @Override
  60             public void beginInstanceDeclarationElement(Class<?> type) {
  61             }
  62 
  63             @Override
  64             public void beginUnknownTypeElement(String name) {
  65             }
  66 
  67             @Override
  68             public void beginIncludeElement() {
  69             }
  70 
  71             @Override
  72             public void beginReferenceElement() {
  73             }
  74 
  75             @Override
  76             public void beginCopyElement() {
  77             }
  78 
  79             @Override
  80             public void beginRootElement() {
  81             }
  82 
  83             @Override
  84             public void beginPropertyElement(String name, Class<?> sourceType) {
  85             }
  86 
  87             @Override
  88             public void beginUnknownStaticPropertyElement(String name) {
  89             }
  90 
  91             @Override
  92             public void beginScriptElement() {
  93                 assertFalse(scriptCalled.getAndSet(true));
  94             }
  95 
  96             @Override
  97             public void beginDefineElement() {
  98             }
  99 
 100             @Override
 101             public void readInternalAttribute(String name, String value) {
 102             }
 103 
 104             @Override
 105             public void readPropertyAttribute(String name, Class<?> sourceType, String value) {
 106             }
 107 
 108             @Override
 109             public void readUnknownStaticPropertyAttribute(String name, String value) {
 110             }
 111 
 112             @Override
 113             public void readEventHandlerAttribute(String name, String value) {
 114             }
 115 
 116             @Override
 117             public void endElement(Object value) {
 118                 if (value instanceof String && ((String) value).contains("doSomething")) {
 119                     assertTrue(scriptCalled.get());
 120                     assertFalse(scriptEndCalled.getAndSet(true));
 121                 }
 122             }
 123         });
 124 
 125         fxmlLoader.load();
 126         assertTrue(scriptCalled.get());
 127         assertTrue(scriptEndCalled.get());
 128     }
 129 
 130     @Test
 131     public void testScriptHandler() throws IOException {
 132 
 133         FXMLLoader loader = new FXMLLoader(getClass().getResource("script_handler.fxml"));
 134         loader.load();
 135 
 136         Widget w = (Widget) loader.getNamespace().get("w");
 137         assertNotNull(w);
 138         loader.getNamespace().put("actionDone", new AtomicBoolean(false));
 139         w.fire();
 140         assertTrue(((AtomicBoolean) loader.getNamespace().get("actionDone")).get());
 141     }
 142 
 143     @Test
 144     public void testExternalScriptHandler() throws IOException {
 145 
 146         FXMLLoader loader = new FXMLLoader(getClass().getResource("script_handler_external.fxml"));
 147         loader.load();
 148 
 149         Widget w = (Widget) loader.getNamespace().get("w");
 150         assertNotNull(w);
 151         loader.getNamespace().put("actionDone", new AtomicBoolean(false));
 152         w.fire();
 153         assertTrue(((AtomicBoolean)loader.getNamespace().get("actionDone")).get());
 154     }
 155 }