Skip to content

Commit

Permalink
test: add simple integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy committed Jul 23, 2024
1 parent 7d08a9a commit fb786ec
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.flowingcode.addons.simpletimer.integration;

public interface IntegrationCallables {

void setStartTime(Integer startTime);

void setEndTime(Integer endTime);

void start();

void pause();

void reset();

boolean isRunning();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.flowingcode.addons.simpletimer.integration;

import com.flowingcode.vaadin.addons.simpletimer.SimpleTimer;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route("/it")
public class IntegrationView extends Div implements IntegrationCallables {

private SimpleTimer timer = new SimpleTimer();

public IntegrationView() {
add(timer);
}

@Override
@ClientCallable
public void setStartTime(Integer startTime) {
timer.setStartTime(startTime);
}

@Override
@ClientCallable
public void setEndTime(Integer endTime) {
timer.setEndTime(endTime);
}

@Override
@ClientCallable
public void start() {
timer.start();
}

@Override
@ClientCallable
public void pause() {
timer.pause();
}

@Override
@ClientCallable
public void reset() {
timer.reset();
}

@Override
@ClientCallable
public boolean isRunning() {
return timer.isRunning();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.flowingcode.addons.simpletimer.integration;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport;
import org.junit.Test;

public class SimpleIT extends AbstractViewTest implements HasRpcSupport {

IntegrationCallables $server = createCallableProxy(IntegrationCallables.class);

public SimpleIT() {
super("it");
}

private Double currentTime() {
return $(SimpleTimerElement.class).first().currentTime();
}

@Test
public void countDown() {
assertThat(currentTime(), nullValue());
assertFalse($server.isRunning());

$server.setStartTime(1);
assertThat(currentTime(), equalTo(1.0));

$server.start();
assertTrue($server.isRunning());
double t0 = currentTime();
double t1 = currentTime();
assertThat(t0, lessThan(1.0));
assertThat(t1, lessThan(t0));
}

@Test
public void countUp() {
assertThat(currentTime(), nullValue());
assertFalse($server.isRunning());

$server.setEndTime(1);
assertThat(currentTime(), equalTo(0.0));

$server.start();
assertTrue($server.isRunning());
double t0 = currentTime();
double t1 = currentTime();
assertThat(t0, greaterThan(0.0));
assertThat(t1, greaterThan(t0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.flowingcode.addons.simpletimer.integration;

import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elementsbase.Element;
import java.util.Optional;

@Element("simple-timer")
public class SimpleTimerElement extends TestBenchElement {

Double currentTime() {
Number time = (Number) executeScript("return arguments[0].currentTime", this);
return Optional.ofNullable(time).map(Number::doubleValue).orElse(null);
}

}

0 comments on commit fb786ec

Please sign in to comment.