From 135f9f7972e551fb8001f078e5379e386764e947 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 26 Jun 2016 22:04:02 +0200 Subject: [PATCH] Fix formatting of timestamps in the future and a few seconds ago --- .../sone/web/ajax/GetTimesAjaxPage.java | 4 +-- .../sone/web/ajax/GetTimesAjaxPageTest.java | 39 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.java diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java index b52198b..02fd0c1 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java @@ -150,10 +150,10 @@ public class GetTimesAjaxPage extends JsonPage { String text; long refresh; if (age < 0) { - text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture"); + text = webInterface.getL10n().getString("View.Time.InTheFuture"); refresh = TimeUnit.MINUTES.toMillis(5); } else if (age < TimeUnit.SECONDS.toMillis(20)) { - text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo"); + text = webInterface.getL10n().getString("View.Time.AFewSecondsAgo"); refresh = TimeUnit.SECONDS.toMillis(10); } else if (age < TimeUnit.SECONDS.toMillis(45)) { text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo"); diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.java new file mode 100644 index 0000000..e1ab857 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.java @@ -0,0 +1,39 @@ +package net.pterodactylus.sone.web.ajax; + +import static java.lang.System.currentTimeMillis; +import static net.pterodactylus.sone.web.ajax.GetTimesAjaxPage.getTime; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage.Time; + +import org.junit.Test; +import org.mockito.Mockito; + +/** + * Unit test for {@link GetTimesAjaxPage}. + * + * @author David ‘Bombe’ Roden + */ +public class GetTimesAjaxPageTest { + + private final WebInterface webInterface = Mockito.mock(WebInterface.class, RETURNS_DEEP_STUBS); + + @Test + public void timestampInTheFutureIsTranslatedCorrectly() { + when(webInterface.getL10n().getString("View.Time.InTheFuture")).thenReturn("in the future"); + Time time = getTime(webInterface, currentTimeMillis() + 100); + assertThat(time.getText(), is("in the future")); + } + + @Test + public void timestampAFewSecondsAgoIsTranslatedCorrectly() { + when(webInterface.getL10n().getString("View.Time.AFewSecondsAgo")).thenReturn("a few seconds ago"); + Time time = getTime(webInterface, currentTimeMillis() - 1000); + assertThat(time.getText(), is("a few seconds ago")); + } + +} -- 2.7.4