|
| 1 | +package com.codename1.share; |
| 2 | + |
| 3 | +import com.codename1.test.UITestBase; |
| 4 | +import com.codename1.ui.Button; |
| 5 | +import com.codename1.ui.Command; |
| 6 | +import com.codename1.ui.Component; |
| 7 | +import com.codename1.ui.Container; |
| 8 | +import com.codename1.ui.Form; |
| 9 | +import com.codename1.ui.Label; |
| 10 | +import com.codename1.ui.TextArea; |
| 11 | +import com.codename1.ui.TextField; |
| 12 | +import com.codename1.ui.layouts.BorderLayout; |
| 13 | +import com.codename1.ui.events.ActionEvent; |
| 14 | +import com.codename1.ui.events.ActionListener; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +class ShareFormTest extends UITestBase { |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + void setUpImageIO() { |
| 30 | + when(implementation.getImageIO()).thenReturn(null); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testConstructorWithoutRecipientAddsMessageOnly() throws Exception { |
| 35 | + AtomicInteger postClicks = new AtomicInteger(); |
| 36 | + Form contacts = new Form(); |
| 37 | + ShareForm form = new ShareForm(contacts, "Share", null, "Hello", new RecordingListener(postClicks)); |
| 38 | + |
| 39 | + assertEquals("Share", form.getTitle()); |
| 40 | + assertEquals("Hello", form.getMessage()); |
| 41 | + assertEquals("", form.getTo()); |
| 42 | + |
| 43 | + TextField toField = getField(form, "to", TextField.class); |
| 44 | + assertFalse(form.getContentPane().contains(toField)); |
| 45 | + |
| 46 | + TextArea messageField = getField(form, "message", TextArea.class); |
| 47 | + assertSame(form.getContentPane(), messageField.getParent()); |
| 48 | + |
| 49 | + Button postButton = getField(form, "post", Button.class); |
| 50 | + triggerButton(postButton); |
| 51 | + assertEquals(1, postClicks.get()); |
| 52 | + |
| 53 | + assertEquals(BorderLayout.class, form.getContentPane().getLayout().getClass()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testRecipientFieldIsAddedWhenProvided() throws Exception { |
| 58 | + Form contacts = new Form(); |
| 59 | + ShareForm form = new ShareForm( contacts, "Share", "[email protected]", "Message", new RecordingListener( new AtomicInteger())); |
| 60 | + |
| 61 | + assertEquals( "[email protected]", form. getTo()); |
| 62 | + TextField toField = getField(form, "to", TextField.class); |
| 63 | + assertSame(form.getContentPane(), toField.getParent()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testImageFallbackDisplaysPathTextWhenScalingUnavailable() throws Exception { |
| 68 | + Form contacts = new Form(); |
| 69 | + String imagePath = "missing-image.jpg"; |
| 70 | + ShareForm form = new ShareForm( contacts, "Share", "[email protected]", "Caption", imagePath, new RecordingListener( new AtomicInteger())); |
| 71 | + |
| 72 | + TextArea messageField = getField(form, "message", TextArea.class); |
| 73 | + Container body = (Container) messageField.getParent(); |
| 74 | + assertNotNull(body); |
| 75 | + assertEquals(BorderLayout.class, body.getLayout().getClass()); |
| 76 | + |
| 77 | + Label imageLabel = findFirstLabel(body); |
| 78 | + assertNotNull(imageLabel); |
| 79 | + assertEquals(imagePath, imageLabel.getText()); |
| 80 | + assertSame(body, imageLabel.getParent()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testBackCommandInvokesContactsShowBack() { |
| 85 | + final AtomicInteger backCalls = new AtomicInteger(); |
| 86 | + Form contacts = new Form() { |
| 87 | + @Override |
| 88 | + public void showBack() { |
| 89 | + backCalls.incrementAndGet(); |
| 90 | + } |
| 91 | + }; |
| 92 | + ShareForm form = new ShareForm(contacts, "Share", null, "Body", new RecordingListener(new AtomicInteger())); |
| 93 | + |
| 94 | + Command back = form.getBackCommand(); |
| 95 | + assertNotNull(back); |
| 96 | + back.actionPerformed(null); |
| 97 | + assertEquals(1, backCalls.get()); |
| 98 | + } |
| 99 | + |
| 100 | + private void triggerButton(Button button) throws Exception { |
| 101 | + Method fire = Button.class.getDeclaredMethod("fireActionEvent", int.class, int.class); |
| 102 | + fire.setAccessible(true); |
| 103 | + fire.invoke(button, 0, 0); |
| 104 | + } |
| 105 | + |
| 106 | + private Label findFirstLabel(Container root) { |
| 107 | + List<Component> children = root.getChildrenAsList(true); |
| 108 | + for (Component child : children) { |
| 109 | + if (child instanceof Label) { |
| 110 | + return (Label) child; |
| 111 | + } |
| 112 | + if (child instanceof Container) { |
| 113 | + Label nested = findFirstLabel((Container) child); |
| 114 | + if (nested != null) { |
| 115 | + return nested; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + private <T> T getField(Object target, String fieldName, Class<T> type) throws Exception { |
| 123 | + Field field = target.getClass().getDeclaredField(fieldName); |
| 124 | + field.setAccessible(true); |
| 125 | + return type.cast(field.get(target)); |
| 126 | + } |
| 127 | + |
| 128 | + private static class RecordingListener implements ActionListener { |
| 129 | + private final AtomicInteger counter; |
| 130 | + |
| 131 | + RecordingListener(AtomicInteger counter) { |
| 132 | + this.counter = counter; |
| 133 | + } |
| 134 | + |
| 135 | + public void actionPerformed(ActionEvent evt) { |
| 136 | + counter.incrementAndGet(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments