Skip to content
Snippets Groups Projects
Commit 838097e6 authored by Nicolas anquetil's avatar Nicolas anquetil
Browse files

Finally with some running tests

parent 27058cbb
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,6 @@ public abstract class AbstractVue extends Stage implements Observer, ITemperatur ...@@ -42,7 +42,6 @@ public abstract class AbstractVue extends Stage implements Observer, ITemperatur
@Override @Override
public void update(Subject subj, Object data) { public void update(Subject subj, Object data) {
System.out.println("AbstractVue.update("+modele.getTemperature());
setDisplayedValue(modele.getTemperature()); setDisplayedValue(modele.getTemperature());
} }
......
...@@ -58,7 +58,7 @@ public class TextView extends AbstractVue { ...@@ -58,7 +58,7 @@ public class TextView extends AbstractVue {
public void setDisplayedValue(double val) { public void setDisplayedValue(double val) {
if (val > 25) { if (val > 25) {
saisie.setStyle("-fx-background-color:red;"); saisie.setStyle("-fx-control-inner-background:red;");
} }
else if (val < -5) { else if (val < -5) {
saisie.setStyle("-fx-control-inner-background:blue;"); saisie.setStyle("-fx-control-inner-background:blue;");
......
package fr.univlille.iutinfo.m3105.q3; package fr.univlille.iutinfo.m3105.q3;
import fr.univlille.iutinfo.m3105.MainQ3;
import fr.univlille.iutinfo.m3105.modelQ2.Echelle; import fr.univlille.iutinfo.m3105.modelQ2.Echelle;
import fr.univlille.iutinfo.m3105.modelQ2.Temperature; import fr.univlille.iutinfo.m3105.modelQ2.Temperature;
import fr.univlille.iutinfo.m3105.viewQ3.TextView; import fr.univlille.iutinfo.m3105.viewQ3.TextView;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.Window;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -15,36 +19,123 @@ import org.testfx.api.FxRobot; ...@@ -15,36 +19,123 @@ import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension; import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.ApplicationTest; import org.testfx.framework.junit5.ApplicationTest;
import org.testfx.matcher.control.TextInputControlMatchers; import org.testfx.matcher.control.TextInputControlMatchers;
import org.testfx.service.finder.WindowFinder;
import org.testfx.service.query.NodeQuery;
import org.testfx.util.WaitForAsyncUtils;
/**
* Thanks to the <code>@ExtendWith(ApplicationExtension.class)</code>, test methods have access to RobotFx
* The robot allows to manipulate and query the GUI
*/
@ExtendWith(ApplicationExtension.class) @ExtendWith(ApplicationExtension.class)
public class TestQ3_3 /*extends ApplicationTest */{ public class TestQ3_3 {
/**
* Launches the given main class before running each test
*/
@BeforeEach @BeforeEach
public void setup() throws Exception { public void setup() throws Exception {
ApplicationTest.launch(fr.univlille.iutinfo.m3105.MainQ3.class); ApplicationTest.launch(fr.univlille.iutinfo.m3105.MainQ3.class);
} }
//@ Start @Test
public void start(Stage stage) throws Exception { public void test_initial_value_of_Celsius_textField(FxRobot robot) {
System.err.println("in START !!!"); FxAssert.verifyThat(
Temperature temp = new Temperature(Echelle.CELSIUS); queryTextFieldInWindow(robot, celsiusWindow(robot)),
temp.setTemperature(18.0); TextInputControlMatchers.hasText("18.0"));
}
new TextView(temp).show(); @Test
public void test_initial_value_of_Fahrenheit_textField(FxRobot robot) {
FxAssert.verifyThat(
queryTextFieldInWindow(robot, fahrenheitWindow(robot)),
TextInputControlMatchers.hasText("64.4"));
} }
@Test @Test
public void test1(FxRobot robot) { public void test_plus_button_increments_Celsius_value(FxRobot robot) {
//robot.clickOn(".button"); Window window = celsiusWindow(robot);
/*
for (Window w : robot.listWindows()) { // .query() executes the NodeQuery and returns the first result
System.out.println("window type:" + w.getClass().getName()); Button plusButton = queryLabeledNodeInWindow(robot, window, "+").query();
robot.moveTo(plusButton).clickOn(MouseButton.PRIMARY);
WaitForAsyncUtils.waitForFxEvents();
// check new value of TextField in CELSIUS Window
FxAssert.verifyThat(
queryTextFieldInWindow(robot, window),
TextInputControlMatchers.hasText("19.0"));
}
@Test
public void test_plus_button_increments_Fahrenheit_value(FxRobot robot) {
Window window = fahrenheitWindow(robot);
// .query() executes the NodeQuery and returns the first result
Button plusButton = queryLabeledNodeInWindow(robot, window, "+").query();
robot.moveTo(plusButton).clickOn(MouseButton.PRIMARY);
// check new value of TextField in FAHRENHEIT Window
FxAssert.verifyThat(
queryTextFieldInWindow(robot, window),
TextInputControlMatchers.hasText("65.4"));
}
/**********************************************************
* U T I L I T I E S
**********************************************************/
/**
* Uses the FxRobot to find the window for Celsius
*/
protected Window celsiusWindow(FxRobot robot) {
return windowWithTitle(robot, "Celsius");
}
/**
* Uses the FxRobot to find the window for Fahrenheit
*/
protected Window fahrenheitWindow(FxRobot robot) {
return windowWithTitle(robot, "Fahrenheit");
}
/**
* Uses the FxRobot to find the window for Newton
*/
protected Window newtonWindow(FxRobot robot) {
return windowWithTitle(robot, "Newton");
}
/**
* uses the WindowFinder of the FxRobot to find the (first) window having the <code>expectedTitle</code>
* Will throw a <code>NoSuchElementException</code> if the window is not found
*/
protected Window windowWithTitle(FxRobot robot, String expectedTitle) {
WindowFinder finder = robot.robotContext().getWindowFinder();
finder.targetWindow(expectedTitle);
return finder.targetWindow();
} }
Window wndw = wndw = robot.listWindows().get(2);
System.out.println("window 2 type:" + wndw.getClass().getName()); /**
* Returns a <code>NodeQuery</code> that will filter all <code>TextField</code> Nodes inside the Scene of the <code>window</code>
* <code>robot.from</code> is used to create an initial NodeQuery
* <code>NodeQuery.lookup</code> creates another query that searches all nodes matching the lambda inside the children of the first query
*/
protected NodeQuery queryTextFieldInWindow(FxRobot robot, Window window) {
return robot.from(((Stage)window).getScene().getRoot()).lookup( (Node n) -> n instanceof TextField);
}
/**
* Returns a <code>NodeQuery</code> that will filter all Nodes inside the Scene of the <code>window</code> with the <code>expectedLabel</code>
* <code>robot.from</code> is used to create an initial NodeQuery
* <code>NodeQuery.lookup</code> creates another query that searches all nodes matching the lambda inside the children of the first query
*/ */
FxAssert.verifyThat( robot.lookup(node -> node instanceof TextField) , TextInputControlMatchers.hasText("18.0")); protected NodeQuery queryLabeledNodeInWindow(FxRobot robot, Window window, String expectedLabel) {
return robot.from(((Stage)window).getScene().getRoot()).lookup( expectedLabel);
} }
} }
package fr.univlille.iutinfo.m3105.q3;
import fr.univlille.iutinfo.m3105.modelQ2.Echelle;
import fr.univlille.iutinfo.m3105.modelQ2.Temperature;
import fr.univlille.iutinfo.m3105.viewQ3.TextView;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.testfx.api.FxAssert;
import org.testfx.framework.junit5.ApplicationTest;
import org.testfx.framework.junit5.Start;
import org.testfx.framework.junit5.Stop;
import org.testfx.matcher.base.GeneralMatchers;
import org.testfx.matcher.base.StyleableMatchers;
import org.testfx.matcher.control.TextInputControlMatchers;
import org.testfx.service.query.NodeQuery;
public class TestTextView extends ApplicationTest {
protected TextView vue = null;
protected Temperature temp;
@Start
public void start(Stage stage) throws Exception {
// we ignore the main Stage created by ApplicationTest and create our own stage (TextView)
temp = new Temperature(Echelle.CELSIUS);
temp.setTemperature(18.0);
vue = new TextView(temp);
}
/**
* 1st argument of verifyThat(...) is a <code>NodeQuery</code> that will find a TextField in the tested window (there is only one)
*
* 2nd argument is a <code>Matcher</code> that checks whether one element (of the previous NodeQuery) has for text "18.0"
* The query (1st argument) is such that there should be only one Node matching it
* The Matcher assumes the elements returned by the query are TextInputControl (such as a TExtField) which it is, by construction of the query
*/
@Test
public void test_initial_value_of_textField() {
FxAssert.verifyThat(
queryTextFieldInWindow(),
TextInputControlMatchers.hasText("18.0"));
}
@Test
public void test_plus_button_increments_value() {
// from(...) creates an initial NodeQuery from the rootNode in the tested window
// lookup("+") searches for children Nodes of the rootNode that are Labeled (such as Buttons) and have this label ("+")
// .query() executes the NodeQuery and returns the first result
Button plusButton = from(getWindowRootNode()).lookup("+").query();
moveTo(plusButton).clickOn(MouseButton.PRIMARY);
// this may be necessary as JavaFX executes in another thread and JUnit thread could be faster than JavaFX thread
//WaitForAsyncUtils.waitForFxEvents();
// check new value of TextField
FxAssert.verifyThat(
queryTextFieldInWindow(),
TextInputControlMatchers.hasText("19.0"));
}
@Test
public void test_minus_button_decrements_value() {
// from(...) creates an initial NodeQuery from the rootNode in the tested window
// lookup("+") searches for children Nodes of the rootNode that are Labeled (such as Buttons) and have this label ("+")
// .query() executes the NodeQuery and returns the first result
Button plusButton = from(getWindowRootNode()).lookup("-").query();
moveTo(plusButton).clickOn(MouseButton.PRIMARY);
// this may be necessary as JavaFX executes in another thread and JUnit thread could be faster than JavaFX thread
//WaitForAsyncUtils.waitForFxEvents();
// check new value of TextField
FxAssert.verifyThat(
queryTextFieldInWindow(),
TextInputControlMatchers.hasText("17.0"));
}
/**********************************************************
* U T I L I T I E S
**********************************************************/
private Parent getWindowRootNode() {
return vue.getScene().getRoot();
}
/**
* Returns a <code>NodeQuery</code> that will filter all <code>TextField</code> Nodes inside the Scene of the tested window
* <code>from(...)</code> is used to create an initial NodeQuery
* <code>lookup(...)</code> creates another query that searches, inside the children of the first query, all nodes matching the lambda
*/
private NodeQuery queryTextFieldInWindow() {
return from(getWindowRootNode()).lookup( (Node n) -> n instanceof TextField);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment