Ticket #21558: 21558.2.patch
File 21558.2.patch, 7.1 KB (added by , 3 years ago) |
---|
-
src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java
diff --git a/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java b/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java index 3dedcdfd1b..cf5ddb9f3c 100644
a b public class NotesDialog extends ToggleDialog implements LayerChangeListener, No 407 407 if (note != null) { 408 408 int selectedIndex = displayList.getSelectedIndex(); 409 409 noteData.closeNote(note, dialog.getInputText()); 410 noteData.setSelectedNote(model.getElementAt(selectedIndex)); 410 // This is required since filtering may cause the model to not have any visible elements 411 if (model.getSize() > 0) { 412 noteData.setSelectedNote(model.getElementAt(selectedIndex)); 413 } else { 414 noteData.setSelectedNote(null); 415 } 411 416 } 412 417 } 413 418 } -
test/unit/org/openstreetmap/josm/gui/dialogs/NotesDialogTest.java
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/NotesDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/NotesDialogTest.java index a8ec189fc8..c2fe4f2a64 100644
a b 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.dialogs; 3 3 4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 5 import static org.junit.jupiter.api.Assertions.assertEquals; 5 6 import static org.junit.jupiter.api.Assertions.assertFalse; 6 7 import static org.junit.jupiter.api.Assertions.assertTrue; 8 import static org.openstreetmap.josm.tools.I18n.tr; 7 9 8 10 import java.time.Instant; 11 import java.util.Collections; 9 12 10 13 import javax.swing.JLabel; 11 14 import javax.swing.JList; 12 15 16 import org.junit.jupiter.api.Test; 17 import org.junit.jupiter.api.extension.RegisterExtension; 18 import org.junit.platform.commons.util.ReflectionUtils; 19 import org.openstreetmap.josm.TestUtils; 13 20 import org.openstreetmap.josm.data.coor.LatLon; 14 21 import org.openstreetmap.josm.data.notes.Note; 15 22 import org.openstreetmap.josm.data.notes.NoteComment; 16 23 import org.openstreetmap.josm.data.osm.User; 24 import org.openstreetmap.josm.gui.ExtendedDialog; 25 import org.openstreetmap.josm.gui.MainApplication; 17 26 import org.openstreetmap.josm.gui.dialogs.NotesDialog.NoteRenderer; 27 import org.openstreetmap.josm.gui.layer.NoteLayer; 28 import org.openstreetmap.josm.gui.widgets.JosmTextField; 29 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 30 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 19 20 import org.junit.jupiter.api.Test; 31 import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker; 21 32 22 33 /** 23 34 * Unit tests of {@link NotesDialog} 24 35 */ 25 36 @BasicPreferences 26 37 class NotesDialogTest { 38 /** Only needed for {@link #testTicket21558} */ 39 @RegisterExtension 40 JOSMTestRules rules = new JOSMTestRules().main().projection(); 27 41 private Note createMultiLineNote() { 28 42 Note note = new Note(LatLon.ZERO); 29 43 note.setCreatedAt(Instant.now()); … … class NotesDialogTest { 55 69 assertFalse(NotesDialog.matchesNote("new", note)); 56 70 assertFalse(NotesDialog.matchesNote("reopened", note)); 57 71 } 72 73 /** 74 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/21558>#21558</a> 75 */ 76 @Test 77 void testTicket21558() throws Exception { 78 TestUtils.assumeWorkingJMockit(); 79 new ExtendedDialogMocker(Collections.singletonMap(tr("Close note"), tr("Close note"))) { 80 @Override 81 protected String getString(ExtendedDialog instance) { 82 return instance.getTitle(); 83 } 84 }; 85 final NotesDialog notesDialog = new NotesDialog(); 86 final NotesDialog.CloseAction closeAction = (NotesDialog.CloseAction) ReflectionUtils 87 .tryToReadFieldValue(NotesDialog.class, "closeAction", notesDialog).get(); 88 final JosmTextField filter = (JosmTextField) ReflectionUtils 89 .tryToReadFieldValue(NotesDialog.class, "filter", notesDialog).get(); 90 final NoteLayer noteLayer = new NoteLayer(); 91 MainApplication.getLayerManager().addLayer(noteLayer); 92 final Note note = createMultiLineNote(); 93 note.setState(Note.State.OPEN); 94 noteLayer.getNoteData().addNotes(Collections.singleton(note)); 95 noteLayer.getNoteData().setSelectedNote(note); 96 filter.setText("open"); 97 assertDoesNotThrow(() -> closeAction.actionPerformed(null)); 98 } 58 99 } -
test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
diff --git a/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java b/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java index f71e581dfc..2e77130c43 100644
a b public class ExtendedDialogMocker extends BaseDialogMockUp<ExtendedDialog> { 142 142 } 143 143 } 144 144 145 /** 146 * Get the result field for an extended dialog instance 147 * @param instance The instance to get the result field for 148 * @return The result field. May be private. 149 * @throws NoSuchFieldException If the field cannot be found. Should never be thrown. 150 */ 151 protected Field getResultField(ExtendedDialog instance) throws NoSuchFieldException { 152 // Note that subclasses of ExtendedDialogMocker will not have "result" as a declared field. 153 // Iterate up the chain until we get to a field that has "result" as a declared field. 154 // Only reason for this is just in case someone overrides the logic in ExtendedDialog. 155 Class<?> clazz = instance.getClass(); 156 Field resultField = null; 157 // Store the exception, if any 158 NoSuchFieldException noSuchFieldException = null; 159 while (!Object.class.equals(clazz) && resultField == null) { 160 try { 161 resultField = clazz.getDeclaredField("result"); 162 } catch (NoSuchFieldException e) { 163 clazz = instance.getClass().getSuperclass(); 164 // Only save the first exception 165 if (noSuchFieldException == null) { 166 noSuchFieldException = e; 167 } 168 } 169 } 170 if (resultField == null) { 171 throw noSuchFieldException; 172 } 173 return resultField; 174 } 175 145 176 @Mock 146 177 private void setupDialog(final Invocation invocation) { 147 178 if (!GraphicsEnvironment.isHeadless()) { … … public class ExtendedDialogMocker extends BaseDialogMockUp<ExtendedDialog> { 159 190 this.act(instance); 160 191 final int mockResult = this.getMockResult(instance); 161 192 // TODO check validity of mockResult? 162 Field resultField = instance.getClass().getDeclaredField("result");193 final Field resultField = this.getResultField(instance); 163 194 resultField.setAccessible(true); 164 195 resultField.set(instance, mockResult); 165 196 Logging.info(