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