Skip to content

Commit ed18f1c

Browse files
authored
Add DateFormat sample UI unit test (#4196)
1 parent 5c93c21 commit ed18f1c

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.l10n.L10NManager;
6+
import com.codename1.ui.Button;
7+
import com.codename1.ui.DisplayTest;
8+
import com.codename1.ui.Form;
9+
import com.codename1.ui.Label;
10+
import com.codename1.ui.layouts.BoxLayout;
11+
import java.util.Date;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
class DateFormatTest2772PortTest extends UITestBase {
16+
17+
@FormTest
18+
void tappingShowDateFormatsUsingLocalizationManager() {
19+
Date fixedDate = new Date(1700000000000L);
20+
RecordingL10NManager localizationManager = new RecordingL10NManager();
21+
implementation.setLocalizationManager(localizationManager);
22+
23+
Form form = new Form("Test Date Format", BoxLayout.y());
24+
Label result = new Label();
25+
Button showDate = new Button("Show Date");
26+
showDate.addActionListener(evt -> {
27+
result.setText(L10NManager.getInstance().formatDateLongStyle(fixedDate));
28+
form.revalidateWithAnimationSafety();
29+
});
30+
form.add(result);
31+
form.add(showDate);
32+
form.show();
33+
ensureSized(showDate, form);
34+
35+
implementation.tapComponent(showDate);
36+
flushSerialCalls();
37+
DisplayTest.flushEdt();
38+
flushSerialCalls();
39+
40+
assertEquals("Formatted: 1700000000000", result.getText());
41+
assertNotNull(localizationManager.getLastFormattedDate());
42+
assertEquals(fixedDate.getTime(), localizationManager.getLastFormattedDate().getTime());
43+
}
44+
45+
private void ensureSized(Button button, Form form) {
46+
for (int i = 0; i < 5 && (button.getWidth() <= 0 || button.getHeight() <= 0); i++) {
47+
form.revalidate();
48+
flushSerialCalls();
49+
DisplayTest.flushEdt();
50+
flushSerialCalls();
51+
}
52+
}
53+
54+
private static class RecordingL10NManager extends L10NManager {
55+
private Date lastFormattedDate;
56+
57+
RecordingL10NManager() {
58+
super("en", "US");
59+
}
60+
61+
Date getLastFormattedDate() {
62+
return lastFormattedDate;
63+
}
64+
65+
@Override
66+
public String formatDateLongStyle(Date date) {
67+
lastFormattedDate = date;
68+
if (date == null) {
69+
return "";
70+
}
71+
return "Formatted: " + date.getTime();
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)