--- old/tests/system/src/test/java/test/javafx/scene/web/HTMLEditorTest.java 2018-10-31 23:44:49.439482775 +0530 +++ new/tests/system/src/test/java/test/javafx/scene/web/HTMLEditorTest.java 2018-10-31 23:44:49.184945457 +0530 @@ -25,7 +25,7 @@ package test.javafx.scene.web; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import javafx.application.Application; @@ -38,6 +38,7 @@ import javafx.scene.web.WebView; import javafx.stage.Stage; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -48,7 +49,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static test.util.Util.TIMEOUT; public class HTMLEditorTest { private static final CountDownLatch launchLatch = new CountDownLatch(1); @@ -85,14 +85,7 @@ new Thread(() -> Application.launch(HTMLEditorTestApp.class, (String[]) null)).start(); - try { - if (!launchLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { - fail("Timeout waiting for FX runtime to start"); - } - } catch (InterruptedException exception) { - fail("Unexpected exception: " + exception); - } - + assertTrue("Timeout waiting for FX runtime to start", Util.await(launchLatch)); } @AfterClass @@ -100,6 +93,22 @@ Platform.exit(); } + @Before + public void setupTestObjects() { + Platform.runLater(() -> { + htmlEditor = new HTMLEditor(); + Scene scene = new Scene(htmlEditor); + htmlEditorTestApp.primaryStage.setScene(scene); + htmlEditorTestApp.primaryStage.show(); + + webView = (WebView) htmlEditor.lookup(".web-view"); + assertNotNull(webView); + // Cancel the existing load to make our stateProperty listener + // usable + webView.getEngine().getLoadWorker().cancel(); + }); + } + /** * @test * @bug 8090011 @@ -110,14 +119,21 @@ // re-enabling this test case. @Test @Ignore("JDK-8202542") public void checkFocusChange() throws Exception { - final CountDownLatch editorStateLatch = new CountDownLatch(2); - final AtomicBoolean result = new AtomicBoolean(false); + final CountDownLatch editorStateLatch = new CountDownLatch(1); + final AtomicReference result = new AtomicReference<>(); Platform.runLater(() -> { - HTMLEditor htmlEditor = new HTMLEditor(); - Scene scene = new Scene(htmlEditor); - htmlEditorTestApp.primaryStage.setScene(scene); - WebView webView = (WebView)htmlEditor.lookup(".web-view"); - assertNotNull(webView); + webView.getEngine().getLoadWorker().stateProperty(). + addListener((observable, oldValue, newValue) -> { + if (newValue == SUCCEEDED) { + webView.getEngine().executeScript( + "document.body.style.backgroundColor='red';" + + "document.body.onfocusout = function() {" + + "document.body.style.backgroundColor = 'yellow';" + + "}"); + htmlEditor.requestFocus(); + } + }); + htmlEditor.setHtmlText(htmlEditor.getHtmlText()); KeyEvent tabKeyEvent = new KeyEvent(null, webView, KeyEvent.KEY_PRESSED, "", "", @@ -132,36 +148,18 @@ for (int i = 0; i < 10; ++i) { Event.fireEvent(webView, tabKeyEvent); } - result.set("red".equals(webView.getEngine(). + result.set(webView.getEngine(). executeScript("document.body.style.backgroundColor"). - toString())); + toString()); htmlEditorTestApp.primaryStage.hide(); editorStateLatch.countDown(); } }); - webView.getEngine().getLoadWorker().stateProperty(). - addListener((observable, oldValue, newValue) -> { - if (newValue == SUCCEEDED) { - webView.getEngine().executeScript( - "document.body.style.backgroundColor='red';" + - "document.body.onfocusout = function() {" + - "document.body.style.backgroundColor = 'yellow';" + - "}"); - htmlEditor.requestFocus(); - editorStateLatch.countDown(); - } - }); - htmlEditorTestApp.primaryStage.show(); }); - try { - editorStateLatch.await(TIMEOUT, TimeUnit.MILLISECONDS); - } catch (InterruptedException ex) { - throw new AssertionError(ex); - } finally { - assertTrue("Focus Change with design mode enabled ", result.get()); - } + assertTrue("Timeout when waiting for focus change ", Util.await(editorStateLatch)); + assertEquals("Focus Change with design mode enabled ", "red", result.get()); } /** @@ -171,7 +169,7 @@ */ @Test public void checkStyleWithCSS() throws Exception { - final CountDownLatch editorStateLatch = new CountDownLatch(2); + final CountDownLatch editorStateLatch = new CountDownLatch(1); final String editorCommand1 = "document.execCommand('bold', false, 'true');" + "document.execCommand('italic', false, 'true');" + @@ -192,10 +190,13 @@ "font-style: italic;\">Hello World"; Util.runAndWait(() -> { - htmlEditor = new HTMLEditor(); - Scene scene = new Scene(htmlEditor); - htmlEditorTestApp.primaryStage.setScene(scene); - webView = (WebView)htmlEditor.lookup(".web-view"); + webView.getEngine().getLoadWorker().stateProperty(). + addListener((observable, oldValue, newValue) -> { + if (newValue == SUCCEEDED) { + htmlEditor.requestFocus(); + } + }); + htmlEditor.setHtmlText(htmlEditor.getHtmlText()); assertNotNull(webView); webView.focusedProperty(). @@ -205,23 +206,9 @@ } }); - webView.getEngine().getLoadWorker().stateProperty(). - addListener((observable, oldValue, newValue) -> { - if (newValue == SUCCEEDED) { - htmlEditor.requestFocus(); - editorStateLatch.countDown(); - } - }); - htmlEditorTestApp.primaryStage.show(); }); - try { - if (!editorStateLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { - throw new AssertionError("Timeout waiting for callbacks"); - } - } catch (InterruptedException ex) { - throw new AssertionError(ex); - } + assertTrue("Timeout when waiting for focus change ", Util.await(editorStateLatch)); Util.runAndWait(() -> { webView.getEngine().executeScript("document.body.focus();"); @@ -274,23 +261,17 @@ */ @Test public void checkStyleProperty() throws Exception { - final CountDownLatch editorStateLatch = new CountDownLatch(2); - final AtomicBoolean result = new AtomicBoolean(false); - Platform.runLater(() -> { - HTMLEditor htmlEditor = new HTMLEditor(); - Scene scene = new Scene(htmlEditor); - htmlEditorTestApp.primaryStage.setScene(scene); - htmlEditor.setHtmlText("" + - "" + - "" + - "" + - "" + - "

Test

" + - "" + - ""); + final CountDownLatch editorStateLatch = new CountDownLatch(1); + final AtomicReference result = new AtomicReference<>(); + Util.runAndWait(() -> { + webView.getEngine().getLoadWorker().stateProperty(). + addListener((observable, oldValue, newValue) -> { + if (newValue == SUCCEEDED) { + htmlEditor.requestFocus(); + } + }); - WebView webView = (WebView)htmlEditor.lookup(".web-view"); - assertNotNull(webView); + htmlEditor.setHtmlText("

Test

"); webView.focusedProperty(). addListener((observable, oldValue, newValue) -> { @@ -301,30 +282,17 @@ executeScript("document.execCommand('selectAll', false, 'true');"); webView.getEngine(). executeScript("document.execCommand('removeFormat', false, null);"); - result.set("bold".equals(webView.getEngine(). + result.set(webView.getEngine(). executeScript("document.body.style.fontWeight"). - toString())); - htmlEditorTestApp.primaryStage.hide(); + toString()); editorStateLatch.countDown(); } }); - webView.getEngine().getLoadWorker().stateProperty(). - addListener((observable, oldValue, newValue) -> { - if (newValue == SUCCEEDED) { - htmlEditor.requestFocus(); - editorStateLatch.countDown(); - } - }); - htmlEditorTestApp.primaryStage.show(); }); - try { - editorStateLatch.await(TIMEOUT, TimeUnit.MILLISECONDS); - } catch (InterruptedException ex) { - throw new AssertionError(ex); - } finally { - assertTrue("check Style Property with removeFormat ", result.get()); - } + assertTrue("Timeout when waiting for focus change ", Util.await(editorStateLatch)); + assertNotNull("result must have a valid reference ", result.get()); + assertEquals("document.body.style.fontWeight must be bold ", "bold", result.get()); } }