Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
* master:
  Merged #37
  Update README.md
  Added support for following labelFor references.
  • Loading branch information
svenruppert committed Jan 10, 2014
2 parents 605f6b6 + 7f44af3 commit d35069e
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DesktopTest extends GuiTest
### Documentation

* [Getting started][11]
* [Examples][17]
* Reference
* [Mouse control][12]
* [Keyboard control][13]
Expand All @@ -50,7 +51,6 @@ class DesktopTest extends GuiTest
* [ListViews](https://github.com/SmartBear/TestFX/blob/master/src/test/java/org/loadui/testfx/ListViewsTest.java)
* [TextInputControls](https://github.com/SmartBear/TestFX/blob/master/src/test/java/org/loadui/testfx/TextInputControlsTest.java)
* [Misc.][16]
* [Examples][17]

You might also be interested in these conference sessions featuring TestFX: [JavaZone](http://jz13.java.no/presentation.html?id=89b56833) and [JavaOne][8].

Expand Down
41 changes: 24 additions & 17 deletions src/main/java/org/loadui/testfx/GuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
import com.google.common.base.Predicates;
import com.google.common.collect.*;
import com.google.common.util.concurrent.SettableFuture;

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Labeled;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.loadui.testfx.exceptions.NoNodesFoundException;
Expand All @@ -45,6 +45,7 @@
import org.loadui.testfx.utils.TestUtils;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -60,13 +61,11 @@
import static org.loadui.testfx.utils.FXTestUtils.flattenSets;
import static org.loadui.testfx.utils.FXTestUtils.intersection;
import static org.loadui.testfx.utils.FXTestUtils.isVisible;
import static org.loadui.testfx.controls.Commons.hasLabel;

public abstract class GuiTest
{
private static final SettableFuture<Stage> stageFuture = SettableFuture.create();
protected static Stage stage;
private static String stylesheet = null;

public static class TestFxApp extends Application
{
Expand Down Expand Up @@ -104,8 +103,7 @@ private void showNodeInStage()

private void showNodeInStage( final String stylesheet )
{
GuiTest.stylesheet = stylesheet;

// TODO: Do something with the stylesheet?
if( stage == null )
{
FXTestUtils.launchApp(TestFxApp.class);
Expand Down Expand Up @@ -150,10 +148,6 @@ public void run()

public static <T extends Window> T targetWindow( T window )
{
if( window instanceof Stage )
{
Stage stage = ( Stage )window;
}
lastSeenWindow = window;
return window;
}
Expand Down Expand Up @@ -314,6 +308,16 @@ public static <T extends Node> T find( final String query )
throw new NoNodesFoundException( "No nodes found with label '" + query + "'! Screenshot saved as " + captureScreenshot().getAbsolutePath() );
}

if( foundNode instanceof Label )
{
Label label = ( Label )foundNode;
Node labelFor = label.getLabelFor();
if( labelFor != null )
{
return (T) labelFor;
}
}

return foundNode;
}

Expand Down Expand Up @@ -432,7 +436,8 @@ public Boolean call() throws Exception
}, timeoutInSeconds );
}

private static <T extends Node> T findByCssSelector( final String selector )
@SuppressWarnings("unchecked")
private static <T extends Node> T findByCssSelector( final String selector )
{
Set<Node> locallyFound = findAll( selector );
Iterable<Node> globallyFound = concat( transform( getWindows(),
Expand All @@ -452,7 +457,8 @@ public Iterable<Node> apply( Window input )
return ( T )getFirst( visibleNodes, null );
}

public static <T extends Node> T find( final Matcher<Object> matcher )
@SuppressWarnings("unchecked")
public static <T extends Node> T find( final Matcher<Object> matcher )
{
Iterable<Set<Node>> found = transform( getWindows(),
new Function<Window, Set<Node>>()
Expand Down Expand Up @@ -504,7 +510,7 @@ public static Set<Node> findAll( Matcher<Object> matcher, Node parent )

private static Set<Node> findAllRecursively( Matcher<Object> matcher, Node parent)
{
Set<Node> found = new HashSet();
Set<Node> found = new HashSet<Node>();
if( matcher.matches( parent ) )
{
found.add(parent);
Expand All @@ -528,8 +534,9 @@ public static <T extends Node> Set<T> findAll( Predicate<T> predicate, Node pare

private static <T extends Node> Set<T> findAllRecursively( Predicate<T> predicate, Node parent)
{
Set<T> found = new HashSet();
Set<T> found = new HashSet<T>();
try {
@SuppressWarnings("unchecked")
T node = (T) parent;
if( predicate.apply( node ) )
{
Expand Down Expand Up @@ -1183,11 +1190,11 @@ else if( target instanceof Window )
}
else if( target instanceof Matcher )
{
return pointFor( find( ( Matcher )target ) );
return pointFor( find( ( Matcher<Object> )target ) );
}
else if( target instanceof Predicate )
{
return pointFor( find( (Predicate) target ) );
return pointFor( find( (Predicate<Node>) target ) );
}
else if( target instanceof Iterable<?> )
{
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/loadui/testfx/controls/ListViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.scene.Node;
import javafx.scene.control.ListView;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
Expand Down Expand Up @@ -33,8 +34,9 @@ public static int numberOfRowsIn(String listQuery)
return table.getItems().size();
}

@SuppressWarnings("unchecked")
@Factory
public static org.hamcrest.Matcher containsRow(Object rowValue)
public static <S> org.hamcrest.Matcher<S> containsRow(Object rowValue)
{
return new ListContainsMatcher(rowValue);
}
Expand All @@ -58,6 +60,7 @@ static ListView<?> getListView(String listSelector) {
return (ListView<?>) node;
}

@SuppressWarnings("rawtypes")
private static class ListContainsMatcher extends BaseMatcher {
private Object valueToMatch;

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/loadui/testfx/controls/TableViews.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.loadui.testfx.controls;

import com.google.common.base.Predicate;

import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
Expand Down Expand Up @@ -50,15 +52,16 @@ public static int numberOfRowsIn(String tableQuery)
return table.getItems().size();
}

@SuppressWarnings("unchecked")
@Factory
public static Matcher containsCell(Object cellValue)
public static <S> Matcher<S> containsCell(Object cellValue)
{
return new TableContainsMatcher(cellValue);
}

static boolean containsCell(TableView<?> table, Predicate<String> cellPredicate)
{
for( TableColumn column : table.getColumns() )
for( TableColumn<?, ?> column : table.getColumns() )
{
for(int i=0; i<table.getItems().size(); i++ )
{
Expand All @@ -72,7 +75,7 @@ static boolean containsCell(TableView<?> table, Predicate<String> cellPredicate)

static boolean containsCell(TableView<?> table, Object cellValue)
{
for( TableColumn column : table.getColumns() )
for( TableColumn<?, ?> column : table.getColumns() )
{
for(int i=0; i<table.getItems().size(); i++ )
{
Expand Down Expand Up @@ -154,6 +157,7 @@ protected static TableRow<?> row(String tableSelector, int row) {
}
}

@SuppressWarnings("rawtypes")
private static class TableContainsMatcher extends BaseMatcher
{
private Object valueToMatch;
Expand All @@ -163,6 +167,7 @@ public TableContainsMatcher(Object valueToMatch)
this.valueToMatch = valueToMatch;
}

@SuppressWarnings("unchecked")
@Override
public boolean matches(Object o) {
if( o instanceof String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.internal.matchers.TypeSafeMatcher;

import static org.loadui.testfx.GuiTest.find;
import static com.google.common.base.Preconditions.checkArgument;

public class HasLabelMatcher extends TypeSafeMatcher<Object>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public class NoNodesFoundException extends NodeQueryException
{
public NoNodesFoundException(String message)
private static final long serialVersionUID = 1L;

public NoNodesFoundException(String message)
{
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.loadui.testfx.exceptions;

public class NoNodesVisibleException extends NodeQueryException {
private static final long serialVersionUID = 1L;

public NoNodesVisibleException(String message)
{
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public abstract class NodeQueryException extends RuntimeException
{
private static final long serialVersionUID = 1L;

public NodeQueryException(String message)
{
super(message);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/loadui/testfx/utils/FXTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.BoundingBox;
Expand All @@ -37,6 +37,7 @@
import javafx.stage.Stage;

import com.google.common.util.concurrent.SettableFuture;

import org.loadui.testfx.GuiTest;

public class FXTestUtils
Expand Down Expand Up @@ -233,6 +234,7 @@ public boolean apply(javafx.scene.Node node) {
}
};

@SuppressWarnings("deprecation")
public static boolean isNodeVisible(Node node)
{
if(!node.isVisible() || !node.impl_isTreeVisible())
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/loadui/testfx/DragDropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ protected Parent getRootNode() {
return HBoxBuilder.create().children(list1, list2).build();
}

@SuppressWarnings("unchecked")
@Test(timeout=10000)
public void shouldMoveElements() throws Exception {

Expand Down
55 changes: 55 additions & 0 deletions src/test/java/org/loadui/testfx/LabelForTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2013 SmartBear Software
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the Licence for the specific language governing permissions and limitations
* under the Licence.
*/
package org.loadui.testfx;

import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.loadui.testfx.categories.TestFX;

import static org.loadui.testfx.Assertions.verifyThat;
import static org.loadui.testfx.controls.Commons.hasText;

@Category( TestFX.class )
public class LabelForTest extends GuiTest
{
@Override
protected Parent getRootNode()
{
TextField uText = TextFieldBuilder.create().id( "uname" ).build();
TextField pText = PasswordFieldBuilder.create().id( "pword" ).build();
Label u = LabelBuilder.create().text( "User name" ).build();
Label p = LabelBuilder.create().text( "Password" ).labelFor( pText ).build();

GridPane grid = new GridPane();
grid.add( u, 0, 0 );
grid.add( uText, 1, 0 );
grid.add( p, 0, 1 );
grid.add( pText, 1, 1 );
return grid;
}

@Test
public void shouldClickButton()
{
click( "User name" ).type( "Steve" );
click( "Password" ).type( "duke4ever" );

verifyThat( "#pword", hasText( "duke4ever" ) );
}
}
8 changes: 0 additions & 8 deletions src/test/java/org/loadui/testfx/ListViewsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
*/
package org.loadui.testfx;

import com.google.common.base.Predicate;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBoxBuilder;
import javafx.util.Callback;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.loadui.testfx.GuiTest;
Expand Down

0 comments on commit d35069e

Please sign in to comment.