|
| 1 | +package com.codename1.samples; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.ui.CheckBox; |
| 6 | +import com.codename1.ui.Display; |
| 7 | +import com.codename1.ui.DisplayTest; |
| 8 | +import com.codename1.ui.Form; |
| 9 | +import com.codename1.ui.layouts.BoxLayout; |
| 10 | +import com.codename1.ui.spinner.Picker; |
| 11 | + |
| 12 | +import java.util.Calendar; |
| 13 | +import java.util.Date; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | + |
| 19 | +class StringPickerSampleTest extends UITestBase { |
| 20 | + |
| 21 | + @FormTest |
| 22 | + public void testStringPickerToggleAndRange() { |
| 23 | + Form form = new Form("Hi World", BoxLayout.y()); |
| 24 | + |
| 25 | + final Picker languagePicker = new Picker(); |
| 26 | + languagePicker.setType(Display.PICKER_TYPE_STRINGS); |
| 27 | + languagePicker.setStrings("Italian", "English", "German"); |
| 28 | + languagePicker.setSelectedString("English"); |
| 29 | + final AtomicInteger actionCount = new AtomicInteger(); |
| 30 | + languagePicker.addActionListener(e -> actionCount.incrementAndGet()); |
| 31 | + |
| 32 | + final Picker datePicker = new Picker(); |
| 33 | + datePicker.setType(Display.PICKER_TYPE_DATE); |
| 34 | + final Picker timePicker = new Picker(); |
| 35 | + timePicker.setType(Display.PICKER_TYPE_TIME); |
| 36 | + final Picker dateTimePicker = new Picker(); |
| 37 | + dateTimePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME); |
| 38 | + |
| 39 | + final Picker rangePicker = new Picker(); |
| 40 | + rangePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME); |
| 41 | + rangePicker.setUseLightweightPopup(true); |
| 42 | + Date startDate = newDate(119, 6, 7); |
| 43 | + Date endDate = newDate(119, 6, 12); |
| 44 | + rangePicker.setStartDate(startDate); |
| 45 | + rangePicker.setEndDate(endDate); |
| 46 | + rangePicker.setHourRange(8, 11); |
| 47 | + rangePicker.setDate(newDate(119, 6, 8, 9)); |
| 48 | + |
| 49 | + final CheckBox lightweight = new CheckBox("LightWeight"); |
| 50 | + lightweight.setSelected(languagePicker.isUseLightweightPopup()); |
| 51 | + lightweight.addActionListener(e -> { |
| 52 | + boolean selected = lightweight.isSelected(); |
| 53 | + languagePicker.setUseLightweightPopup(selected); |
| 54 | + datePicker.setUseLightweightPopup(selected); |
| 55 | + timePicker.setUseLightweightPopup(selected); |
| 56 | + dateTimePicker.setUseLightweightPopup(selected); |
| 57 | + rangePicker.setUseLightweightPopup(selected); |
| 58 | + }); |
| 59 | + |
| 60 | + form.add(languagePicker); |
| 61 | + form.add(datePicker); |
| 62 | + form.add(timePicker); |
| 63 | + form.add(lightweight); |
| 64 | + form.add(dateTimePicker); |
| 65 | + form.add(rangePicker); |
| 66 | + form.show(); |
| 67 | + DisplayTest.flushEdt(); |
| 68 | + flushSerialCalls(); |
| 69 | + ensureSized(form, lightweight); |
| 70 | + ensureSized(form, languagePicker); |
| 71 | + ensureSized(form, rangePicker); |
| 72 | + } |
| 73 | + |
| 74 | + private void toggleWithPointer(CheckBox checkBox) { |
| 75 | + tap(checkBox); |
| 76 | + } |
| 77 | + |
| 78 | + private void tap(com.codename1.ui.Component component) { |
| 79 | + int x = component.getAbsoluteX() + component.getWidth() / 2; |
| 80 | + int y = component.getAbsoluteY() + component.getHeight() / 2; |
| 81 | + implementation.dispatchPointerPressAndRelease(x, y); |
| 82 | + } |
| 83 | + |
| 84 | + private void ensureSized(Form form, com.codename1.ui.Component component) { |
| 85 | + for (int i = 0; i < 5 && (component.getWidth() <= 0 || component.getHeight() <= 0); i++) { |
| 86 | + form.revalidate(); |
| 87 | + DisplayTest.flushEdt(); |
| 88 | + flushSerialCalls(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private Date newDate(int year, int month, int day) { |
| 93 | + Calendar cal = Calendar.getInstance(); |
| 94 | + cal.set(Calendar.YEAR, year + 1900); |
| 95 | + cal.set(Calendar.MONTH, month); |
| 96 | + cal.set(Calendar.DAY_OF_MONTH, day); |
| 97 | + cal.set(Calendar.HOUR_OF_DAY, 0); |
| 98 | + cal.set(Calendar.MINUTE, 0); |
| 99 | + cal.set(Calendar.SECOND, 0); |
| 100 | + return cal.getTime(); |
| 101 | + } |
| 102 | + |
| 103 | + private Date newDate(int year, int month, int day, int hour) { |
| 104 | + Calendar cal = Calendar.getInstance(); |
| 105 | + cal.set(Calendar.YEAR, year + 1900); |
| 106 | + cal.set(Calendar.MONTH, month); |
| 107 | + cal.set(Calendar.DAY_OF_MONTH, day); |
| 108 | + cal.set(Calendar.HOUR_OF_DAY, hour); |
| 109 | + cal.set(Calendar.MINUTE, 0); |
| 110 | + cal.set(Calendar.SECOND, 0); |
| 111 | + return cal.getTime(); |
| 112 | + } |
| 113 | +} |
0 commit comments