Skip to content

Commit

Permalink
8305709: [testbug] Tree/TableViewResizeColumnToFitContentTest fails w…
Browse files Browse the repository at this point in the history
…ith fractional screen scale

Reviewed-by: kpk, kcr
  • Loading branch information
Andy Goryachev committed Sep 21, 2023
1 parent 658c833 commit 9658fc7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package test.robot.javafx.scene.tableview;

import static org.junit.Assert.fail;
import java.util.concurrent.CountDownLatch;
import javafx.application.Application;
import javafx.application.Platform;
Expand Down Expand Up @@ -67,15 +66,13 @@ public static void main(String[] args) {

@Test
public void resizeColumnToFitContentTest() {
double colOneWidth = table.getColumns().get(0).getWidth();
double colTwoWidth = table.getColumns().get(1).getWidth();
double colThreeWidth = table.getColumns().get(2).getWidth();
double colsWidthBeforeResize = colOneWidth + colTwoWidth + colThreeWidth;
double wid0 = table.getColumns().get(0).getWidth();
double wid1 = table.getColumns().get(1).getWidth();
double wid2 = table.getColumns().get(2).getWidth();
double colsWidthBeforeResize = wid0 + wid1 + wid2;
double colHeaderHeight = 25;
double posX = scene.getWindow().getX() + table.getLayoutX() +
colOneWidth + colTwoWidth;
double posY = scene.getWindow().getY() + table.getLayoutY() +
colHeaderHeight / 2;
double posX = scene.getWindow().getX() + table.getLayoutX() + wid0 + wid1;
double posY = scene.getWindow().getY() + table.getLayoutY() + colHeaderHeight / 2;

CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
Expand All @@ -86,23 +83,24 @@ public void resizeColumnToFitContentTest() {
robot.mouseRelease(MouseButton.PRIMARY);
latch.countDown();
});

Util.waitForLatch(latch, 5, "Timeout while waiting for mouse double click");
try {
Thread.sleep(1000); // Delay for table resizing of table columns.
} catch (Exception e) {
fail("Thread was interrupted." + e);
}
Util.waitForIdle(scene);

Assert.assertTrue("resizeColumnToFitContent failed",
(colTwoWidth != table.getColumns().get(1).getWidth()));

// Skip this check on platforms with fractional scale until JDK-8299753 gets implemented
if (!Util.isFractionalScaleX(table)) {
colTwoWidth = table.getColumns().get(1).getWidth();
colThreeWidth = table.getColumns().get(2).getWidth();
double colsWidthAfterResize = colOneWidth + colTwoWidth + colThreeWidth;
Assert.assertEquals("TableView.CONSTRAINED_RESIZE_POLICY ignored.",
colsWidthBeforeResize, colsWidthAfterResize, EPSILON);
}
(wid1 != table.getColumns().get(1).getWidth()));

wid1 = table.getColumns().get(1).getWidth();
wid2 = table.getColumns().get(2).getWidth();
double colsWidthAfterResize = wid0 + wid1 + wid2;
double tolerance = Util.getTolerance(table);
String message = "TableView.CONSTRAINED_RESIZE_POLICY ignored" +
", before=" + colsWidthBeforeResize +
", after=" + colsWidthAfterResize +
", diff=" + Math.abs(colsWidthBeforeResize - colsWidthAfterResize) +
", tolerance=" + tolerance +
", tol+eps=" + (tolerance + EPSILON);
Assert.assertEquals(message, colsWidthBeforeResize, colsWidthAfterResize, tolerance + EPSILON);
}

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package test.robot.javafx.scene.treetableview;

import static org.junit.Assert.fail;
import java.util.concurrent.CountDownLatch;
import javafx.application.Application;
import javafx.application.Platform;
Expand Down Expand Up @@ -68,15 +67,14 @@ public static void main(String[] args) {

@Test
public void resizeColumnToFitContentTest() {
double colOneWidth = treeTableView.getColumns().get(0).getWidth();
double colTwoWidth = treeTableView.getColumns().get(1).getWidth();
double colThreeWidth = treeTableView.getColumns().get(2).getWidth();
double colsWidthBeforeResize = colOneWidth + colTwoWidth + colThreeWidth;
double wid0 = treeTableView.getColumns().get(0).getWidth();
double wid1 = treeTableView.getColumns().get(1).getWidth();
double wid2 = treeTableView.getColumns().get(2).getWidth();
double colsWidthBeforeResize = wid0 + wid1 + wid2;
double colHeaderHeight = 25;
double posX = scene.getWindow().getX() + treeTableView.getLayoutX() +
colOneWidth + colTwoWidth;
double posY = scene.getWindow().getY() + treeTableView.getLayoutY() +
colHeaderHeight / 2;
double posX = scene.getWindow().getX() + treeTableView.getLayoutX() + wid0 + wid1;
double posY = scene.getWindow().getY() + treeTableView.getLayoutY() + colHeaderHeight / 2;

CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
robot.mouseMove((int) posX, (int) posY);
Expand All @@ -86,23 +84,24 @@ public void resizeColumnToFitContentTest() {
robot.mouseRelease(MouseButton.PRIMARY);
latch.countDown();
});

Util.waitForLatch(latch, 5, "Timeout while waiting for mouse double click");
try {
Thread.sleep(1000); // Delay for table resizing of table columns.
} catch (Exception e) {
fail("Thread was interrupted." + e);
}
Util.waitForIdle(scene);

Assert.assertTrue("resizeColumnToFitContent failed",
(colTwoWidth != treeTableView.getColumns().get(1).getWidth()));

// Skip this check on platforms with fractional scale until JDK-8299753 gets implemented
if (!Util.isFractionalScaleX(treeTableView)) {
colTwoWidth = treeTableView.getColumns().get(1).getWidth();
colThreeWidth = treeTableView.getColumns().get(2).getWidth();
double colsWidthAfterResize = colOneWidth + colTwoWidth + colThreeWidth;
Assert.assertEquals("TreeTableView.CONSTRAINED_RESIZE_POLICY ignored.",
colsWidthBeforeResize, colsWidthAfterResize, EPSILON);
}
(wid1 != treeTableView.getColumns().get(1).getWidth()));

wid1 = treeTableView.getColumns().get(1).getWidth();
wid2 = treeTableView.getColumns().get(2).getWidth();
double colsWidthAfterResize = wid0 + wid1 + wid2;
double tolerance = Util.getTolerance(treeTableView);
String message = "TreeTableView.CONSTRAINED_RESIZE_POLICY ignored" +
", before=" + colsWidthBeforeResize +
", after=" + colsWidthAfterResize +
", diff=" + Math.abs(colsWidthBeforeResize - colsWidthAfterResize) +
", tolerance=" + tolerance +
", tol+eps=" + (tolerance + EPSILON);
Assert.assertEquals(message, colsWidthBeforeResize, colsWidthAfterResize, tolerance + EPSILON);
}

@BeforeClass
Expand Down
31 changes: 30 additions & 1 deletion tests/system/src/test/java/test/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
import javafx.scene.Node;
import javafx.scene.robot.Robot;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.stage.Screen;
import javafx.stage.Stage;

import javafx.stage.Window;
import org.junit.Assert;

import junit.framework.AssertionFailedError;
Expand Down Expand Up @@ -466,4 +467,32 @@ public static boolean isFractionalScaleX(Node n) {
private static boolean isFractional(double x) {
return x != Math.rint(x);
}

/**
* Returns the tolerance which should be used when comparing values,
* when {@link Region#isSnapToPixel()} returns true and the scale can
* be determined from the region's parent {@code Window}.
* When scale cannot be determined it is assumed to be 1.0.
* Otherwise, returns 0.0.
*
* @param r the region in question
* @return the tolerance value
*/
public static double getTolerance(Region r) {
if (r.isSnapToPixel()) {
Scene scene = r.getScene();
if (scene != null) {
Window win = scene.getWindow();
if (win != null) {
// x and y usually have the same scale, so we'll use x
double scale = win.getRenderScaleX();
// distance between pixels in the local (unscaled) coordinates is (1 / scale)
return 1.0 / scale;
}
}
// default to 1 when the scale cannot be determited
return 1.0;
}
return 0.0;
}
}

1 comment on commit 9658fc7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.