/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.jopr.jsfunit;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.lang.ObjectUtils;
import org.jboss.jopr.jsfunit.util.ActiveConditionChecker;
import org.jboss.jopr.jsfunit.util.DescribedCondition;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit;
import org.mozilla.javascript.NativeFunction;
/**
* JavaScript tests. Primarily written to try how Rhino JavaScript impl works.
*
* @author Ondrej Zizka
*/
public class JavaScriptTest extends EmbjoprTestCase
{
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite( JavaScriptTest.class );
}
public void testTreeItemLoaded() throws IOException, Exception
{
JavaScriptEngine jse = client.getContentPage().getEnclosingWindow().getWebClient().getJavaScriptEngine();
log.info( "Script is "+ (jse.isScriptRunning()?"":"not") +" running." );
Object jsTreeItem = jse.execute((HtmlPage) client.getContentPage(), "Tree.Item", "testTreeItemLoaded", 0);
log.info( jsTreeItem.getClass().getName() );
log.info( ObjectUtils.toString(jsTreeItem) );
//org.mozilla.javascript.InterpretedFunction
// extends NativeFunction implements Script
// extends BaseFunction
NativeFunction func = (NativeFunction) jsTreeItem;
log.info( "Source: " + func.getEncodedSource() );
// JS null object?
jsTreeItem = jse.execute((HtmlPage) client.getContentPage(), "null", "testTreeItemLoaded", 0);
if( null == jsTreeItem ){
// True.
}
else {
log.info( jsTreeItem.getClass().getName() );
log.info( ObjectUtils.toString(jsTreeItem) );
}
// Own active condition waiting
new ActiveConditionChecker( new DescribedCondition("Tree.Item JS object is initialized.") {
final EmbJoprTestToolkit selfEjtt = ejtt;
public boolean isTrue() throws Exception {
Object obj = selfEjtt.getJavaScriptObject("Tree.Item");
if( null == obj ) return false;
//if ( org.mozilla.javascript.NOT_FOUND ) return false;
if( ! (obj instanceof NativeFunction ) ) return false;
return true;
}
}).throwOnTimeout().waitWithTimeout(100, 20);
// Using EJTT method
ejtt.navTree.waitUntilReady( 100, 20 );
}
}