Skip to content
Snippets Groups Projects
Commit c0ef8575 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Cleaned up test code

parent 4d8863d8
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,9 @@ package todolist.ui; ...@@ -2,8 +2,9 @@ package todolist.ui;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate; import java.util.function.Predicate;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node; import javafx.scene.Node;
...@@ -18,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; ...@@ -18,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.testfx.framework.junit5.ApplicationTest; import org.testfx.framework.junit5.ApplicationTest;
import org.testfx.util.WaitForAsyncUtils;
import todolist.core.TodoItem; import todolist.core.TodoItem;
import todolist.core.TodoList; import todolist.core.TodoList;
import todolist.core.TodoModel; import todolist.core.TodoModel;
...@@ -53,11 +55,6 @@ public class TodoListControllerTest extends ApplicationTest { ...@@ -53,11 +55,6 @@ public class TodoListControllerTest extends ApplicationTest {
assertTrue(todoModel.iterator().hasNext()); assertTrue(todoModel.iterator().hasNext());
this.todoList = todoModel.iterator().next(); this.todoList = todoModel.iterator().next();
this.controller.setTodoList(this.todoList); this.controller.setTodoList(this.todoList);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
// same as in test-todomodel.json // same as in test-todomodel.json
Iterator<TodoItem> todoItems = todoList.iterator(); Iterator<TodoItem> todoItems = todoList.iterator();
item1 = new TodoItem().as(todoItems.next()); item1 = new TodoItem().as(todoItems.next());
...@@ -97,9 +94,7 @@ public class TodoListControllerTest extends ApplicationTest { ...@@ -97,9 +94,7 @@ public class TodoListControllerTest extends ApplicationTest {
public void testDeleteTodoItem() { public void testDeleteTodoItem() {
// final ListView<TodoItem> todoListView = lookup("#todoItemsView").query(); // final ListView<TodoItem> todoListView = lookup("#todoItemsView").query();
// todoListView.getSelectionModel().select(1); // todoListView.getSelectionModel().select(1);
TodoItemListCell todoItemListCell = findTodoItemListCell(1); clickOn(findTodoItemListCellNode(cell -> true, ".label", 1));
clickOn(todoItemListCell.lookup(".label"));
clickOn("#deleteTodoItemButton"); clickOn("#deleteTodoItemButton");
// item2 is removed, only item1 is left // item2 is removed, only item1 is left
checkTodoListItems(item1); checkTodoListItems(item1);
...@@ -111,8 +106,7 @@ public class TodoListControllerTest extends ApplicationTest { ...@@ -111,8 +106,7 @@ public class TodoListControllerTest extends ApplicationTest {
@Test @Test
public void testCheckTodoItemListCell() { public void testCheckTodoItemListCell() {
TodoItemListCell todoItemListCell = findTodoItemListCell(cell -> ! cell.getItem().isChecked()); clickOn(findTodoItemListCellNode(cell -> !cell.getItem().isChecked(), ".check-box", 0));
clickOn(todoItemListCell.lookup(".check-box"));
TodoItem newItem2 = item2.withChecked(true); TodoItem newItem2 = item2.withChecked(true);
// item is changed // item is changed
checkTodoListItems(item1, newItem2); checkTodoListItems(item1, newItem2);
...@@ -137,22 +131,42 @@ public class TodoListControllerTest extends ApplicationTest { ...@@ -137,22 +131,42 @@ public class TodoListControllerTest extends ApplicationTest {
return findTodoItemListCell(cell -> true, num); return findTodoItemListCell(cell -> true, num);
} }
private TodoItemListCell findTodoItemListCell(Predicate<TodoItemListCell> test) { private Node findNode(Predicate<Node> nodeTest, int num) {
return findTodoItemListCell(test, 0); int count = 0;
for (Node node : lookup(nodeTest).queryAll()) {
if (nodeTest.test(node) && count++ == num) {
return node;
}
}
return null;
} }
private TodoItemListCell findTodoItemListCell(Predicate<TodoItemListCell> test, int num) { private Node waitForNode(Predicate<Node> nodeTest, int num) {
Collection<Node> nodes = lookup(node -> node instanceof TodoItemListCell).queryAll(); // ".list-cell").queryAll(); Node[] nodes = new Node[1];
for (Node node : nodes) { try {
if (node instanceof TodoItemListCell) { WaitForAsyncUtils.waitFor(500, TimeUnit.MILLISECONDS,
TodoItemListCell todoItemListCell = (TodoItemListCell) node; () -> {
if (test.test(todoItemListCell) && num-- == 0) { while (true) {
return todoItemListCell; if ((nodes[0] = findNode(nodeTest, num)) != null) {
return true;
} }
Thread.sleep(10);
} }
} }
fail("Didn't find TodoItemListCell #" + num + " in " + nodes); );
return null; } catch (TimeoutException e) {
fail("No appropriate node available");
}
return nodes[0];
}
private TodoItemListCell findTodoItemListCell(Predicate<TodoItemListCell> test, int num) {
return (TodoItemListCell) waitForNode(node -> node instanceof TodoItemListCell && test.test((TodoItemListCell) node), num);
}
private Node findTodoItemListCellNode(Predicate<TodoItemListCell> test, String selector, int num) {
Node listCell = waitForNode(node -> node instanceof TodoItemListCell && (selector == null || node.lookup(selector) != null) && test.test((TodoItemListCell) node), num);
return listCell.lookup(selector);
} }
private void checkTodoItem(TodoItem item, Boolean checked, String text) { private void checkTodoItem(TodoItem item, Boolean checked, String text) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment