Skip to content

Commit 5f9e2e7

Browse files
authored
test: add translation tests (#457)
1 parent 0c23cd1 commit 5f9e2e7

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.cdi.itest.i18n;
17+
18+
import java.util.Locale;
19+
import java.util.Optional;
20+
import java.util.concurrent.CompletableFuture;
21+
22+
import com.vaadin.flow.component.AttachEvent;
23+
import com.vaadin.flow.component.UI;
24+
import com.vaadin.flow.component.html.Div;
25+
import com.vaadin.flow.component.html.Span;
26+
import com.vaadin.flow.i18n.I18NProvider;
27+
import com.vaadin.flow.internal.LocaleUtil;
28+
import com.vaadin.flow.router.Route;
29+
30+
@Route(value = "translations")
31+
public class TranslationView extends Div {
32+
33+
public static final String TEST_VIEW_ID = "TranslationView";
34+
public static final String LOCALES_ID = "available-locales";
35+
36+
private Span dynamic;
37+
38+
public TranslationView() {
39+
setId(TEST_VIEW_ID);
40+
41+
Span defaultLang = new Span(getTranslation(Locale.ENGLISH, "label"));
42+
defaultLang.setId("english");
43+
Span german = new Span(getTranslation(Locale.GERMAN, "label"));
44+
german.setId("german");
45+
Span germany = new Span(getTranslation(Locale.GERMANY, "label"));
46+
germany.setId("germany");
47+
Span finnish = new Span(
48+
getTranslation(new Locale("fi", "FI"), "label"));
49+
finnish.setId("finnish");
50+
Span french = new Span(getTranslation(Locale.FRANCE, "label"));
51+
french.setId("french");
52+
Span japanese = new Span(getTranslation(Locale.JAPAN, "label"));
53+
japanese.setId("japanese");
54+
55+
Optional<I18NProvider> i18NProvider = LocaleUtil.getI18NProvider();
56+
if (i18NProvider.isPresent()) {
57+
add(new Span("Available translation locales:"));
58+
StringBuilder locales = new StringBuilder();
59+
for (Locale locale : i18NProvider.get().getProvidedLocales()) {
60+
if (locales.length() > 0) {
61+
locales.append(", ");
62+
}
63+
locales.append(locale.toString());
64+
}
65+
Span localeSpan = new Span(locales.toString());
66+
localeSpan.setId(LOCALES_ID);
67+
add(localeSpan, new Div());
68+
}
69+
dynamic = new Span("waiting");
70+
dynamic.setId("dynamic");
71+
72+
add(defaultLang, new Div(), german, new Div(), germany, new Div(),
73+
finnish, new Div(), french, new Div(), japanese, new Div(),
74+
dynamic);
75+
}
76+
77+
@Override
78+
protected void onAttach(AttachEvent event) {
79+
UI ui = event.getUI();
80+
ui.setPollInterval(100);
81+
CompletableFuture.runAsync(() -> {
82+
try {
83+
Thread.sleep(50);
84+
} catch (Exception e) {
85+
e.printStackTrace();
86+
} finally {
87+
ui.access(() -> {
88+
dynamic.setText(getTranslation(Locale.FRANCE, "label"));
89+
ui.setPollInterval(-1);
90+
});
91+
92+
}
93+
});
94+
95+
}
96+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label=Default
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label=Deutsch
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label=Suomi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label=français
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label=日本語
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.cdi.itest;
18+
19+
import java.io.File;
20+
21+
import com.vaadin.cdi.itest.i18n.TranslationView;
22+
import org.jboss.arquillian.container.test.api.Deployment;
23+
import org.jboss.shrinkwrap.api.spec.WebArchive;
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
27+
28+
public class TranslationTest extends AbstractCdiTest {
29+
30+
@Deployment(testable = false)
31+
public static WebArchive createCdiServletEnabledDeployment() {
32+
return ArchiveProvider.createWebArchive("translations",
33+
TranslationView.class)
34+
.addAsResource(new File("src/main/resources/vaadin-i18n"));
35+
36+
}
37+
38+
@Override
39+
protected String getTestPath() {
40+
return "/translations";
41+
}
42+
43+
44+
@Test
45+
public void translationFilesExist_defaultI18NInstantiated_languagesWork() {
46+
open();
47+
48+
String locales = $("span").id(TranslationView.LOCALES_ID)
49+
.getText();
50+
Assert.assertTrue("Couldn't verify German locale", locales.contains("de"));
51+
Assert.assertTrue("Couldn't verify Finnish locale", locales.contains("fi_FI"));
52+
Assert.assertTrue("Couldn't verify French locale", locales.contains("fr_FR"));
53+
Assert.assertTrue("Couldn't verify Japanese locale", locales.contains("ja_JP"));
54+
55+
Assert.assertEquals("Default",
56+
$("span").id("english").getText());
57+
Assert.assertEquals("Deutsch",
58+
$("span").id("german").getText());
59+
Assert.assertEquals("Deutsch",
60+
$("span").id("germany").getText());
61+
Assert.assertEquals("Suomi",
62+
$("span").id("finnish").getText());
63+
Assert.assertEquals("français",
64+
$("span").id("french").getText());
65+
Assert.assertEquals("日本語",
66+
$("span").id("japanese").getText());
67+
}
68+
69+
@Test
70+
public void translationFilesExist_defaultI18NInstantiated_updateFromExternalThreadWorks() {
71+
open();
72+
73+
waitUntilNot(driver -> $("span").id("dynamic").getText()
74+
.equals("waiting"));
75+
76+
Assert.assertEquals("Dynamic update from thread should have used correct bundle.",
77+
"français",
78+
$("span").id("dynamic").getText());
79+
}
80+
}

0 commit comments

Comments
 (0)