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