Skip to content

Commit

Permalink
#50 #51: Select new month when month changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Dec 5, 2020
1 parent 723db30 commit aad81e5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 44 deletions.
12 changes: 10 additions & 2 deletions jfxui/src/main/java/org/itsallcode/whiterabbit/jfxui/ui/AppUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
Expand Down Expand Up @@ -186,8 +187,7 @@ private BorderPane createMainPane()
{
final Insets insets = new Insets(GAP_PIXEL);
final Node daysTable = dayRecordTable.initTable();
state.currentDateProperty.property()
.addListener((observable, oldValue, newValue) -> dayRecordTable.selectRow(newValue));
state.currentDateProperty.property().addListener(this::dateChanged);
final Node activitiesTab = activitiesTable.initTable();
final Button addActivityButton = button("add-activity-button", "+", "Add activity", e -> app.addActivity());
final Button removeActivityButton = button("remove-activity-button", "-", "Remove activity",
Expand Down Expand Up @@ -291,6 +291,7 @@ private Node monthDropDownBox()
{
state.availableMonths.addAll(appService.getAvailableDataYearMonth());
final ComboBox<YearMonth> comboBox = new ComboBox<>(state.availableMonths);
comboBox.setId("selected-month-combobox");

state.currentMonth.addListener(
(observable, oldValue, newValue) -> comboBox.getSelectionModel().select(newValue.getYearMonth()));
Expand All @@ -316,5 +317,12 @@ private Button button(String id, String label, String tooltip, EventHandler<Acti
}
return button;
}

private void dateChanged(ObservableValue<? extends LocalDate> observable, LocalDate oldDate, LocalDate newDate) {
dayRecordTable.selectRow(newDate);
if(oldDate.getMonth()!=newDate.getMonth()) {
app.loadMonth(YearMonth.from(newDate));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package org.itsallcode.whiterabbit.jfxui;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Locale;

import javafx.scene.control.Labeled;
import javafx.stage.Stage;
import org.itsallcode.whiterabbit.jfxui.testutil.TestUtil;
import org.itsallcode.whiterabbit.logic.model.json.JsonDay;
import org.itsallcode.whiterabbit.logic.model.json.JsonMonth;
import org.junit.jupiter.api.Test;
Expand All @@ -18,8 +13,11 @@
import org.testfx.framework.junit5.Start;
import org.testfx.framework.junit5.Stop;

import javafx.scene.control.Labeled;
import javafx.stage.Stage;
import java.time.*;
import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

@ExtendWith(ApplicationExtension.class)
class JavaFxAppUiTest extends JavaFxAppUiTestBase
Expand Down Expand Up @@ -101,6 +99,37 @@ void jsonFileWrittenAfterMinuteTick()
() -> assertThat(month.getDays()).extracting(JsonDay::getEnd).containsExactly(end));
}

@Test
void newMonthSelectedWhenMonthChanges()
{
assertAll(
() -> assertThat(app().getSelectedMonth()).isEqualTo(YearMonth.of(2007, Month.DECEMBER)),
() -> app().dayTable().assertDate(0, LocalDate.of(2007, Month.DECEMBER, 1)));

time().tickDay(LocalDateTime.of(2008, Month.JANUARY, 2, 8, 15, 0));

assertAll(
() -> assertThat(app().getSelectedMonth()).isEqualTo(YearMonth.of(2008, Month.JANUARY)),
() -> app().dayTable().assertDate(0, LocalDate.of(2008, Month.JANUARY, 1)));
}

@Test
void newMonthSelectedUserChangesMonth()
{
time().tickDay(LocalDateTime.of(2008, Month.JANUARY, 2, 8, 15, 0));

assertAll(
() -> assertThat(app().getSelectedMonth()).isEqualTo(YearMonth.of(2008, Month.JANUARY)),
() -> app().dayTable().assertDate(0, LocalDate.of(2008, Month.JANUARY, 1)));

app().setSelectedMonth(YearMonth.of(2007, Month.DECEMBER));
TestUtil.sleepShort();

assertAll(
() -> assertThat(app().getSelectedMonth()).isEqualTo(YearMonth.of(2007, Month.DECEMBER)),
() -> app().dayTable().assertDate(0, LocalDate.of(2007, Month.DECEMBER, 1)));
}

@Override
@Start
void start(Stage stage)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
package org.itsallcode.whiterabbit.jfxui.testutil;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mockito.ArgumentCaptor;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mockito.ArgumentCaptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

public class TimeUtil
{
Expand Down Expand Up @@ -109,7 +98,13 @@ public void tickDay(LocalTime time)
{
final LocalDateTime now = LocalDateTime.now(clockMock);
final LocalDateTime tomorrow = LocalDateTime.of(now.toLocalDate().plusDays(1), time);
final Duration duration = Duration.between(now, tomorrow);
tickDay(tomorrow);
}

public void tickDay(LocalDateTime nextDay)
{
final LocalDateTime now = LocalDateTime.now(clockMock);
final Duration duration = Duration.between(now, nextDay);
addTime(duration);
LOG.info("Tick day by {} to {}", duration, clockMock.instant());
this.updateEverySecondRunnable.run();
Expand Down Expand Up @@ -145,10 +140,6 @@ public void captureScheduledRunnables()
this.updateEverySecondRunnable = arg.getAllValues().get(1);
this.updateEveryMinuteRunnable = arg.getAllValues().get(2);

LOG.trace("Found callback for seconds: {}", updateEverySecondRunnable);
LOG.trace("Found callback for days: {}", updateEveryDayRunnable);
LOG.trace("Found callback for minutes: {}", updateEveryMinuteRunnable);

assertAll(
() -> assertThat(updateEverySecondRunnable.toString())
.contains("trigger=PeriodicTrigger [roundToUnit=Seconds]"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.itsallcode.whiterabbit.jfxui.testutil.model;

import java.time.Duration;

import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.StackPane;
import javafx.stage.Window;
import org.itsallcode.whiterabbit.jfxui.JavaFxUtil;
import org.itsallcode.whiterabbit.jfxui.table.activities.ActivityPropertyAdapter;
import org.itsallcode.whiterabbit.jfxui.table.days.DayRecordPropertyAdapter;
import org.testfx.api.FxRobot;
import org.testfx.assertions.api.Assertions;

import javafx.scene.control.Button;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.StackPane;
import javafx.stage.Window;
import java.time.Duration;
import java.time.YearMonth;

public class ApplicationHelper
{
Expand Down Expand Up @@ -80,4 +82,19 @@ public AddInterruptionDialog addInterruption()
Assertions.assertThat(dialogWindow).isShowing();
return new AddInterruptionDialog(robot, dialogWindow);
}

public YearMonth getSelectedMonth()
{
return getSelectedMonthComboBox().getValue();
}

public void setSelectedMonth(YearMonth month)
{
JavaFxUtil.runOnFxApplicationThread(() -> getSelectedMonthComboBox().setValue(month));
}

private ComboBox<YearMonth> getSelectedMonthComboBox()
{
return robot.lookup("#selected-month-combobox").queryComboBox();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.testfx.api.FxRobot;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -46,6 +47,17 @@ public void assertBeginAndEnd(int row, LocalTime begin, LocalTime end)
() -> assertThat(getEnd(row)).as("end").isEqualTo(end));
}

public void assertDate(int row, LocalDate expectedDate)
{
assertThat(getDate(row)).isEqualTo(expectedDate);
}

public LocalDate getDate(int row)
{
final TableCell<?, ?> tableCell = table.getTableCell(row, "date");
return (LocalDate) tableCell.getItem();
}

public LocalTime getBegin(int row)
{
final TableCell<?, ?> tableCell = table.getTableCell(row, "begin");
Expand Down Expand Up @@ -94,7 +106,8 @@ public void typeComment(int row, String value)
robot.doubleClickOn(getCommentCell(row)).write(value).type(KeyCode.TAB);
}

public TableCell<?, ?> getCommentCell(int row) {
public TableCell<?, ?> getCommentCell(int row)
{
return table.getTableCell(row, "comment");
}

Expand Down

0 comments on commit aad81e5

Please sign in to comment.