|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.ui.Button; |
| 6 | +import com.codename1.ui.Component; |
| 7 | +import com.codename1.ui.Container; |
| 8 | +import com.codename1.ui.layouts.BorderLayout; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Calendar; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Date; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +class CalendarTest extends UITestBase { |
| 19 | + |
| 20 | + @FormTest |
| 21 | + void selectingDayUpdatesSelectedDate() { |
| 22 | + implementation.setBuiltinSoundsEnabled(false); |
| 23 | + Form form = Display.getInstance().getCurrent(); |
| 24 | + form.setLayout(new BorderLayout()); |
| 25 | + |
| 26 | + com.codename1.ui.Calendar widget = new com.codename1.ui.Calendar(); |
| 27 | + form.add(BorderLayout.CENTER, widget); |
| 28 | + form.revalidate(); |
| 29 | + |
| 30 | + Calendar cal = Calendar.getInstance(); |
| 31 | + cal.set(2022, Calendar.APRIL, 15, 0, 0, 0); |
| 32 | + cal.set(Calendar.MILLISECOND, 0); |
| 33 | + widget.setDate(cal.getTime()); |
| 34 | + form.revalidate(); |
| 35 | + |
| 36 | + Button target = findDayButton(widget, 20); |
| 37 | + assertNotNull(target, "Expected to find button for day 20"); |
| 38 | + |
| 39 | + int x = target.getAbsoluteX() + target.getWidth() / 2; |
| 40 | + int y = target.getAbsoluteY() + target.getHeight() / 2; |
| 41 | + target.pointerPressed(x, y); |
| 42 | + target.pointerReleased(x, y); |
| 43 | + |
| 44 | + Date selected = widget.getDate(); |
| 45 | + Calendar selectedCal = Calendar.getInstance(); |
| 46 | + selectedCal.setTime(selected); |
| 47 | + assertEquals(20, selectedCal.get(Calendar.DAY_OF_MONTH), "Selecting day button should update selected date"); |
| 48 | + } |
| 49 | + |
| 50 | + @FormTest |
| 51 | + void disablingSelectionPreventsDateChanges() { |
| 52 | + implementation.setBuiltinSoundsEnabled(false); |
| 53 | + Form form = Display.getInstance().getCurrent(); |
| 54 | + form.setLayout(new BorderLayout()); |
| 55 | + |
| 56 | + com.codename1.ui.Calendar widget = new com.codename1.ui.Calendar(); |
| 57 | + widget.setChangesSelectedDateEnabled(false); |
| 58 | + form.add(BorderLayout.CENTER, widget); |
| 59 | + form.revalidate(); |
| 60 | + |
| 61 | + Calendar cal = Calendar.getInstance(); |
| 62 | + cal.set(2023, Calendar.JANUARY, 5, 0, 0, 0); |
| 63 | + cal.set(Calendar.MILLISECOND, 0); |
| 64 | + widget.setDate(cal.getTime()); |
| 65 | + form.revalidate(); |
| 66 | + |
| 67 | + Date original = widget.getDate(); |
| 68 | + |
| 69 | + Button target = findDayButton(widget, 10); |
| 70 | + assertNotNull(target, "Expected to find day button"); |
| 71 | + |
| 72 | + int x = target.getAbsoluteX() + target.getWidth() / 2; |
| 73 | + int y = target.getAbsoluteY() + target.getHeight() / 2; |
| 74 | + target.pointerPressed(x, y); |
| 75 | + target.pointerReleased(x, y); |
| 76 | + |
| 77 | + assertEquals(original, widget.getDate(), "Date should remain unchanged when selection disabled"); |
| 78 | + } |
| 79 | + |
| 80 | + @FormTest |
| 81 | + void multipleSelectionTracksAllSelectedDays() { |
| 82 | + implementation.setBuiltinSoundsEnabled(false); |
| 83 | + Form form = Display.getInstance().getCurrent(); |
| 84 | + form.setLayout(new BorderLayout()); |
| 85 | + |
| 86 | + com.codename1.ui.Calendar widget = new com.codename1.ui.Calendar(); |
| 87 | + widget.setMultipleSelectionEnabled(true); |
| 88 | + form.add(BorderLayout.CENTER, widget); |
| 89 | + form.revalidate(); |
| 90 | + |
| 91 | + Calendar cal = Calendar.getInstance(); |
| 92 | + cal.set(2021, Calendar.AUGUST, 1, 0, 0, 0); |
| 93 | + cal.set(Calendar.MILLISECOND, 0); |
| 94 | + widget.setDate(cal.getTime()); |
| 95 | + form.revalidate(); |
| 96 | + |
| 97 | + Button day5 = findDayButton(widget, 5); |
| 98 | + Button day7 = findDayButton(widget, 7); |
| 99 | + assertNotNull(day5, "Expected day 5 button"); |
| 100 | + assertNotNull(day7, "Expected day 7 button"); |
| 101 | + |
| 102 | + pressButton(day5); |
| 103 | + pressButton(day7); |
| 104 | + |
| 105 | + Collection<Date> selectedDays = widget.getSelectedDays(); |
| 106 | + assertNotNull(selectedDays, "Selected days collection should not be null"); |
| 107 | + assertEquals(2, selectedDays.size(), "Two days should be selected"); |
| 108 | + |
| 109 | + List<Integer> dayValues = new ArrayList<>(); |
| 110 | + for (Date d : selectedDays) { |
| 111 | + Calendar c = Calendar.getInstance(); |
| 112 | + c.setTime(d); |
| 113 | + dayValues.add(c.get(Calendar.DAY_OF_MONTH)); |
| 114 | + } |
| 115 | + assertTrue(dayValues.contains(5), "Selected days should include 5"); |
| 116 | + assertTrue(dayValues.contains(7), "Selected days should include 7"); |
| 117 | + } |
| 118 | + |
| 119 | + private void pressButton(Button button) { |
| 120 | + int x = button.getAbsoluteX() + button.getWidth() / 2; |
| 121 | + int y = button.getAbsoluteY() + button.getHeight() / 2; |
| 122 | + button.pointerPressed(x, y); |
| 123 | + button.pointerReleased(x, y); |
| 124 | + } |
| 125 | + |
| 126 | + private Button findDayButton(com.codename1.ui.Calendar calendar, int day) { |
| 127 | + Container monthView = (Container) calendar.getComponentAt(calendar.getComponentCount() - 1); |
| 128 | + Container days = (Container) monthView.getComponentAt(1); |
| 129 | + for (int i = 0; i < days.getComponentCount(); i++) { |
| 130 | + Component cmp = days.getComponentAt(i); |
| 131 | + if (cmp instanceof Button) { |
| 132 | + Button button = (Button) cmp; |
| 133 | + if (button.isEnabled()) { |
| 134 | + String text = button.getText(); |
| 135 | + if (text != null && text.trim().equals(String.valueOf(day))) { |
| 136 | + return button; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | +} |
0 commit comments