From: <gda...@us...> - 2012-04-26 16:11:38
|
Revision: 2828 http://abbot.svn.sourceforge.net/abbot/?rev=2828&view=rev Author: gdavison Date: 2012-04-26 16:11:29 +0000 (Thu, 26 Apr 2012) Log Message: ----------- JTeeLocation was not EDT safe as we were seeing this as test failures Modified Paths: -------------- abbot/trunk/ChangeLog abbot/trunk/src/abbot/tester/JTreeLocation.java Modified: abbot/trunk/ChangeLog =================================================================== --- abbot/trunk/ChangeLog 2012-04-04 12:19:17 UTC (rev 2827) +++ abbot/trunk/ChangeLog 2012-04-26 16:11:29 UTC (rev 2828) @@ -1,3 +1,6 @@ +2012-04-26 Gerard Davison + * Make the code in JTreeLocation EDT safe + 2012-04-03 Gerard Davison * Show the user an error dialog if the script file contains erros * Rather than consume every key event make the CallStep editor wait for a little while Modified: abbot/trunk/src/abbot/tester/JTreeLocation.java =================================================================== --- abbot/trunk/src/abbot/tester/JTreeLocation.java 2012-04-04 12:19:17 UTC (rev 2827) +++ abbot/trunk/src/abbot/tester/JTreeLocation.java 2012-04-26 16:11:29 UTC (rev 2828) @@ -12,6 +12,8 @@ import abbot.script.parsers.TreePathParser; import abbot.util.ExtendedComparator; +import java.util.concurrent.Callable; + /** Provides encapsulation of a visible location on a {@link JTree}. * A row index or a {@link String}ified {@link TreePath} (i.e. each * {@link TreePath} component is a {@link String}) or a {@link TreePath} @@ -84,8 +86,14 @@ /** Convert the given row to an x, y coordinate. @throws LocationUnavailableException if the row is not visible. */ - protected Point rowToPoint(JTree tree, int row) { - TreePath path = tree.getPathForRow(row); + protected Point rowToPoint(final JTree tree, final int row) { + TreePath path = Robot.callAndWait(tree, new Callable<TreePath>() { + @Override + public TreePath call() { + return tree.getPathForRow(row); + } + }); + if (path == null) { String msg = Strings.get("tester.JTree.row_not_visible", new Object[] { new Integer(row) }); @@ -98,12 +106,21 @@ @throws LocationUnavailableException if any part of the path is hidden. */ - protected Point pathToPoint(JTree tree, TreePath path) { - path = findMatchingPath(tree, path); - Rectangle rect = tree.getPathBounds(path); + protected Point pathToPoint(final JTree tree, final TreePath path) { + final TreePath matchingPath = findMatchingPath(tree, path); + + Rectangle rect = Robot.callAndWait(tree, new Callable<Rectangle>() { + @Override + public Rectangle call() { + return tree.getPathBounds(matchingPath); + } + }); + + + if (rect == null) { String msg = Strings.get("tester.JTree.path_not_visible", - new Object[] { path }); + new Object[] { matchingPath }); throw new LocationUnavailableException(msg); } if (inExpansion) @@ -119,7 +136,7 @@ /** Return the path represented by this JTree location. @return null if the path can not be found. */ - TreePath getPath(JTree tree) { + TreePath getPath(final JTree tree) { if (treePath != null) { try { return findMatchingPath(tree, treePath); @@ -129,22 +146,44 @@ } } if (row != -1) { - return tree.getPathForRow(row); + return Robot.callAndWait(tree, new Callable<TreePath>() { + @Override + public TreePath call() { + return tree.getPathForRow(row); + } + }); } - Point where = super.getPoint(tree); - return tree.getPathForLocation(where.x, where.y); + final Point where = super.getPoint(tree); + return Robot.callAndWait(tree, new Callable<TreePath>() { + @Override + public TreePath call() { + return tree.getPathForLocation(where.x, where.y); + } + }); } /** Return the row represented by this JTree location. @return -1 if the row is not found. */ - int getRow(JTree tree) { - if (treePath != null) - return tree.getRowForPath(getPath(tree)); - else if (row != -1) + int getRow(final JTree tree) { + if (treePath != null) { + return Robot.callAndWait(tree, new Callable<Integer>() { + @Override + public Integer call() { + return tree.getRowForPath(getPath(tree)); + } + }); + } + else if (row != -1) { return row; - Point where = super.getPoint(tree); - return tree.getRowForLocation(where.x, where.y); + } + final Point where = super.getPoint(tree); + return Robot.callAndWait(tree, new Callable<Integer>() { + @Override + public Integer call() { + return tree.getRowForLocation(where.x, where.y); + } + }); } /** Return a concrete point for the abstract location. */ @@ -161,13 +200,20 @@ } public Rectangle getBounds(Component c) { - JTree tree = (JTree)c; - int row = getRow(tree); + final JTree tree = (JTree)c; + final int row = getRow(tree); if (row == -1) { Point where = getPoint(c); return new Rectangle(where.x, where.y, 1, 1); } - Rectangle rect = tree.getRowBounds(row); + Rectangle rect = Robot.callAndWait(tree, new Callable<Rectangle>() { + @Override + public Rectangle call() { + return tree.getRowBounds(row); + } + }); + + if (rect == null) { String msg = Strings.get("tester.JTree.row_not_visible", new Object[] { new Integer(row) }); @@ -318,7 +364,7 @@ // Return null if any of them are not found. Log.debug("Comparing " + realPath + " with " + input[0] + " from " + java.util.Arrays.asList(input)); - TreeModel model = tree.getModel(); + final TreeModel model = tree.getModel(); if (!matchesLastComponent(tree, input[0], realPath)) { Log.debug("no root match"); } @@ -329,12 +375,26 @@ Object[] subs = new Object[input.length-1]; System.arraycopy(input, 1, subs, 0, subs.length); - Object obj = realPath.getLastPathComponent(); - int count = model.getChildCount(obj); - Log.debug("Obj " + obj + " (" + obj.getClass() + ") has " + count); + final Object obj = realPath.getLastPathComponent(); + + // Talk to the model on the event queue + // + Object[] children = Robot.callAndWait(tree, new Callable<Object[]>() { + @Override + public Object[] call() { + int count = model.getChildCount(obj); + Object ret[] = new Object[count]; + for (int i = 0; i < count; i++) { + ret[i] = model.getChild(obj, i); + } + return ret; + } + }); + + Log.debug("Obj " + obj + " (" + obj.getClass() + ") has " + children.length); // Find the right child to match - for (int i=0;i < count;i++) { - Object child = model.getChild(obj, i); + for (int i=0;i < children.length;i++) { + Object child = children[i]; Log.debug("checking child " + i + " (" + child + ")"); TreePath newPath = findMatchingPath(tree, realPath.pathByAddingChild(child), This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |