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 org.junit.Test;
  28 
  29 import java.io.IOException;
  30 import java.util.concurrent.atomic.AtomicBoolean;
  31 import javafx.fxml.FXMLLoader;
  32 import javafx.fxml.LoadListener;
  33 
  34 import static org.junit.Assert.*;
  35 
  36 public class FXMLLoader_ScriptTest {
  37     @Test
  38     @SuppressWarnings("deprecation")
  39     public void testStaticScriptLoad() throws IOException {
  40         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("static_script_load.fxml"));
  41         fxmlLoader.impl_setStaticLoad(true);
  42         AtomicBoolean scriptCalled = new AtomicBoolean();
  43         AtomicBoolean scriptEndCalled = new AtomicBoolean();
  44         fxmlLoader.setLoadListener(new LoadListener() {
  45 
  46             @Override
  47             public void readImportProcessingInstruction(String target) {
  48             }
  49 
  50             @Override
  51             public void readLanguageProcessingInstruction(String language) {
  52             }
  53 
  54             @Override
  55             public void readComment(String comment) {
  56             }
  57 
  58             @Override
  59             public void beginInstanceDeclarationElement(Class<?> type) {
  60             }
  61 
  62             @Override
  63             public void beginUnknownTypeElement(String name) {
  64             }
  65 
  66             @Override
  67             public void beginIncludeElement() {
  68             }
  69 
  70             @Override
  71             public void beginReferenceElement() {
  72             }
  73 
  74             @Override
  75             public void beginCopyElement() {
  76             }
  77 
  78             @Override
  79             public void beginRootElement() {
  80             }
  81 
  82             @Override
  83             public void beginPropertyElement(String name, Class<?> sourceType) {
  84             }
  85 
  86             @Override
  87             public void beginUnknownStaticPropertyElement(String name) {
  88             }
  89 
  90             @Override
  91             public void beginScriptElement() {
  92                 assertFalse(scriptCalled.getAndSet(true));
  93             }
  94 
  95             @Override
  96             public void beginDefineElement() {
  97             }
  98 
  99             @Override
 100             public void readInternalAttribute(String name, String value) {
 101             }
 102 
 103             @Override
 104             public void readPropertyAttribute(String name, Class<?> sourceType, String value) {
 105             }
 106 
 107             @Override
 108             public void readUnknownStaticPropertyAttribute(String name, String value) {
 109             }
 110 
 111             @Override
 112             public void readEventHandlerAttribute(String name, String value) {
 113             }
 114 
 115             @Override
 116             public void endElement(Object value) {
 117                 if (value instanceof String && ((String) value).contains("doSomething")) {
 118                     assertTrue(scriptCalled.get());
 119                     assertFalse(scriptEndCalled.getAndSet(true));
 120                 }
 121             }
 122         });
 123 
 124         fxmlLoader.load();
 125         assertTrue(scriptCalled.get());
 126         assertTrue(scriptEndCalled.get());
 127     }
 128 
 129     @Test
 130     public void testScriptHandler() throws IOException {
 131 
 132         FXMLLoader loader = new FXMLLoader(getClass().getResource("script_handler.fxml"));
 133         loader.load();
 134 
 135         Widget w = (Widget) loader.getNamespace().get("w");
 136         assertNotNull(w);
 137         loader.getNamespace().put("actionDone", new AtomicBoolean(false));
 138         w.fire();
 139         assertTrue(((AtomicBoolean) loader.getNamespace().get("actionDone")).get());
 140     }
 141 
 142     @Test
 143     public void testExternalScriptHandler() throws IOException {
 144 
 145         FXMLLoader loader = new FXMLLoader(getClass().getResource("script_handler_external.fxml"));
 146         loader.load();
 147 
 148         Widget w = (Widget) loader.getNamespace().get("w");
 149         assertNotNull(w);
 150         loader.getNamespace().put("actionDone", new AtomicBoolean(false));
 151         w.fire();
 152         assertTrue(((AtomicBoolean)loader.getNamespace().get("actionDone")).get());
 153     }
 154 }