Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSimcoe committed Oct 29, 2024
1 parent 453e722 commit ee36ad8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkiverse.fx.deployment;

import static org.awaitility.Awaitility.await;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;

import io.quarkiverse.fx.FxPostStartupEvent;
import io.quarkiverse.fx.FxStartupLatch;
import io.quarkiverse.fx.QuarkusFxApplication;
import io.quarkus.runtime.Quarkus;

public class FxTestBase {

@Inject
FxStartupLatch startupLatch;

private static final AtomicBoolean eventObserved = new AtomicBoolean(false);

protected void startAndWait() {
// Non-blocking launch
CompletableFuture.runAsync(() -> Quarkus.run(QuarkusFxApplication.class));

// Wait for readiness
try {
this.startupLatch.await();
} catch (InterruptedException e) {
// Should not be interrupted
Assertions.fail(e);
}

await()
.atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.until(eventObserved::get);

Assertions.assertTrue(eventObserved.get());
}

void observeEvent(@Observes final FxPostStartupEvent event) {
eventObserved.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,33 @@

import static org.awaitility.Awaitility.await;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import jakarta.enterprise.event.Observes;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.fx.FxPostStartupEvent;
import io.quarkiverse.fx.QuarkusFxApplication;
import io.quarkiverse.fx.RunOnFxThread;
import io.quarkus.runtime.Quarkus;
import io.quarkus.test.QuarkusUnitTest;
import javafx.application.Platform;

class RunOnFxThreadTest {
class RunOnFxThreadTest extends FxTestBase {

private static final int ASYNC_RUN_TIMEOUT_MS = 500;

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

private static boolean primaryStageObserved = false;
private static Boolean regularThread;
private static Boolean fxThread;

@Test
void test() {
// Non-blocking JavaFX launch
CompletableFuture.runAsync(() -> Quarkus.run(QuarkusFxApplication.class));

await()
.atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.until(() -> primaryStageObserved);
this.startAndWait();

// Synchronous regular thread run
this.runOnRegularThread();
Expand All @@ -55,11 +44,6 @@ void test() {
Assertions.assertTrue(fxThread);
}

void observePrimaryStage(@Observes final FxPostStartupEvent event) {
Assertions.assertNotNull(event.getPrimaryStage());
primaryStageObserved = true;
}

void runOnRegularThread() {
regularThread = !Platform.isFxApplicationThread();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,106 +1,42 @@
package io.quarkiverse.fx.deployment.fxviews;

import io.quarkiverse.fx.FxPostStartupEvent;
import io.quarkiverse.fx.FxStartupLatch;
import io.quarkiverse.fx.QuarkusFxApplication;
import io.quarkiverse.fx.deployment.FxTestConstants;
import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.fx.deployment.FxTestBase;
import io.quarkiverse.fx.deployment.fxviews.controllers.ComponentWithStyleController;
import io.quarkiverse.fx.views.FxViewRepository;
import io.quarkiverse.fx.views.StylesheetReloadStrategy;
import io.quarkus.runtime.Quarkus;
import io.quarkus.test.QuarkusUnitTest;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.stage.Stage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.awaitility.Awaitility.await;

class FxStyleReloadTest {
class FxStyleReloadTest extends FxTestBase {

@RegisterExtension
private static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.withApplicationRoot((jar) -> {
jar.addClass(ComponentWithStyleController.class);
jar.addAsResource("views");
})
.overrideConfigKey("quarkus.fx.views-root", "views")
.overrideConfigKey("quarkus.fx.stylesheet-reload-strategy", StylesheetReloadStrategy.ALWAYS.name())
.overrideConfigKey("quarkus.fx.target-classes", "app-root/views/")
.overrideConfigKey("quarkus.fx.source-resources", "target/");

private static final AtomicBoolean eventObserved = new AtomicBoolean(false);
.overrideConfigKey("quarkus.fx.target-resources", "app-root/")
.overrideConfigKey("quarkus.fx.source-resources", "src/test/resources/");

@Inject
FxViewRepository viewRepository;

@Inject
FxStartupLatch startupLatch;

private Path copy;

@BeforeEach
void before() {

InputStream input = this.getClass().getClassLoader().getResourceAsStream("/views/ComponentWithStyle.css");
Assertions.assertNotNull(input);

// Copy the resource to a temp file
try {
this.copy = Path.of("ComponentWithStyle.css");
Files.copy(input, this.copy, StandardCopyOption.REPLACE_EXISTING);

// Read file content
String content = String.join("\n", Files.readAllLines(this.copy));

// Replace text as needed
String modifiedContent = content.replace("blue", "red");

// Write the modified content back to the temp file or a different location
Files.writeString(this.copy, modifiedContent, StandardOpenOption.TRUNCATE_EXISTING);

} catch (IOException e) {
Assertions.fail(e);
}
}

@AfterEach
void after() {
try {
Files.delete(this.copy);
} catch (IOException e) {
Assertions.fail(e);
}
}

// Can't really test that modifications are effective,
// but we can test that stylesheet has been replaced by the one from sources directory
@Test
void testLiveReload() throws InterruptedException {

// Non-blocking launch
CompletableFuture.runAsync(() -> Quarkus.run(QuarkusFxApplication.class));
void testLiveReload() {

// Wait for readiness
this.startupLatch.await();

await()
.atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.until(eventObserved::get);
this.startAndWait();

Stage primaryStage = this.viewRepository.getPrimaryStage();
Assertions.assertNotNull(primaryStage);
Expand All @@ -109,10 +45,6 @@ void testLiveReload() throws InterruptedException {
Parent component = this.viewRepository.getViewData("ComponentWithStyle").getRootNode();
ObservableList<String> stylesheets = component.getStylesheets();
Assertions.assertEquals(1, stylesheets.size());
Assertions.assertTrue(stylesheets.get(0).endsWith("target/ComponentWithStyle.css"));
}

void observeEvent(@Observes final FxPostStartupEvent event) {
eventObserved.set(true);
Assertions.assertTrue(stylesheets.get(0).endsWith("src/test/resources/views/ComponentWithStyle.css"));
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package io.quarkiverse.fx.deployment.fxviews;

import static org.awaitility.Awaitility.await;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.fx.FxPostStartupEvent;
import io.quarkiverse.fx.QuarkusFxApplication;
import io.quarkiverse.fx.deployment.FxTestConstants;
import io.quarkiverse.fx.deployment.FxTestBase;
import io.quarkiverse.fx.deployment.fxviews.controllers.RootResourceController;
import io.quarkiverse.fx.views.FxViewData;
import io.quarkiverse.fx.views.FxViewRepository;
import io.quarkus.runtime.Quarkus;
import io.quarkus.test.QuarkusUnitTest;
import javafx.scene.layout.VBox;

class FxViewRootResourceTest {
class FxViewRootResourceTest extends FxTestBase {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
Expand All @@ -32,22 +22,15 @@ class FxViewRootResourceTest {
jar.addAsResource("RootResource.fxml");
});

private static final AtomicBoolean eventObserved = new AtomicBoolean(false);

@Inject
FxViewRepository fxViewRepository;
FxViewRepository viewRepository;

@Test
void test() {

// Non-blocking launch
CompletableFuture.runAsync(() -> Quarkus.run(QuarkusFxApplication.class));
this.startAndWait();

await()
.atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.until(eventObserved::get);

FxViewData data = this.fxViewRepository.getViewData("RootResource");
FxViewData data = this.viewRepository.getViewData("RootResource");
Assertions.assertNotNull(data);

VBox root = data.getRootNode();
Expand All @@ -57,8 +40,4 @@ void test() {
Assertions.assertNotNull(data);
Assertions.assertNotNull(controller.label);
}

void observeEvent(@Observes final FxPostStartupEvent event) {
eventObserved.set(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package io.quarkiverse.fx.deployment.fxviews;

import static org.awaitility.Awaitility.await;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.fx.FxPostStartupEvent;
import io.quarkiverse.fx.FxStartupLatch;
import io.quarkiverse.fx.QuarkusFxApplication;
import io.quarkiverse.fx.deployment.FxTestConstants;
import io.quarkiverse.fx.deployment.FxTestBase;
import io.quarkiverse.fx.deployment.fxviews.controllers.ComponentWithStyleController;
import io.quarkiverse.fx.deployment.fxviews.controllers.SampleDialogController;
import io.quarkiverse.fx.deployment.fxviews.controllers.SampleSceneController;
Expand All @@ -25,13 +15,12 @@
import io.quarkiverse.fx.deployment.fxviews.controllers.SubSampleTestController;
import io.quarkiverse.fx.views.FxViewData;
import io.quarkiverse.fx.views.FxViewRepository;
import io.quarkus.runtime.Quarkus;
import io.quarkus.test.QuarkusUnitTest;
import javafx.collections.ObservableList;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

class FxViewTest {
class FxViewTest extends FxTestBase {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
Expand All @@ -50,9 +39,6 @@ class FxViewTest {
@Inject
FxViewRepository viewRepository;

@Inject
FxStartupLatch startupLatch;

@Inject
SubSampleTestController subSampleTestController;

Expand All @@ -65,20 +51,10 @@ class FxViewTest {
@Inject
SampleSceneController sampleSceneController;

private static final AtomicBoolean eventObserved = new AtomicBoolean(false);

@Test
void testFxView() throws InterruptedException {
void testFxView() {

// Non-blocking launch
CompletableFuture.runAsync(() -> Quarkus.run(QuarkusFxApplication.class));

// Wait for readiness
this.startupLatch.await();

await()
.atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.until(eventObserved::get);
this.startAndWait();

Stage primaryStage = this.viewRepository.getPrimaryStage();
Assertions.assertNotNull(primaryStage);
Expand Down Expand Up @@ -106,8 +82,4 @@ void testFxView() throws InterruptedException {
ObservableList<String> componentStylesheets = pane.getStylesheets();
Assertions.assertEquals(1, componentStylesheets.size());
}

void observeEvent(@Observes final FxPostStartupEvent event) {
eventObserved.set(true);
}
}
Loading

0 comments on commit ee36ad8

Please sign in to comment.