[10109] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
| 3 |
|
---|
[17275] | 4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
| 5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
| 6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
| 7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
[10109] | 8 |
|
---|
[18870] | 9 | import java.util.Collections;
|
---|
[10109] | 10 |
|
---|
[14052] | 11 | import javax.swing.JOptionPane;
|
---|
| 12 |
|
---|
[17275] | 13 | import org.junit.jupiter.api.Test;
|
---|
[14081] | 14 | import org.openstreetmap.josm.TestUtils;
|
---|
[14052] | 15 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 16 | import org.openstreetmap.josm.gui.preferences.advanced.PreferencesTable.AllSettingsTableModel;
|
---|
[12884] | 17 | import org.openstreetmap.josm.spi.preferences.StringSetting;
|
---|
[18870] | 18 | import org.openstreetmap.josm.testutils.annotations.AssertionsInEDT;
|
---|
| 19 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
[14052] | 20 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
| 21 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
[10109] | 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Unit tests of {@link PreferencesTable} class.
|
---|
| 25 | */
|
---|
[18870] | 26 | @AssertionsInEDT
|
---|
| 27 | @BasicPreferences
|
---|
[17275] | 28 | class PreferencesTableTest {
|
---|
[10109] | 29 | private static PrefEntry newPrefEntry(String value) {
|
---|
| 30 | StringSetting val = new StringSetting(value);
|
---|
| 31 | StringSetting def = new StringSetting("defaultValue");
|
---|
| 32 | return new PrefEntry("key", val, def, false);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | private static PreferencesTable newTable() {
|
---|
[18870] | 36 | return new PreferencesTable(Collections.singletonList(newPrefEntry("value")));
|
---|
[10109] | 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Unit test of {@link PreferencesTable#PreferencesTable}.
|
---|
| 41 | */
|
---|
| 42 | @Test
|
---|
[17275] | 43 | void testPreferencesTable() {
|
---|
[14081] | 44 | TestUtils.assumeWorkingJMockit();
|
---|
[16160] | 45 | final JOptionPaneSimpleMocker mocker = new JOptionPaneSimpleMocker();
|
---|
| 46 | mocker.getMockResultMap().put("Please select the row to edit.", JOptionPane.OK_OPTION);
|
---|
| 47 | mocker.getMockResultMap().put("Please select the row to delete.", JOptionPane.OK_OPTION);
|
---|
[14052] | 48 | new ExtendedDialogMocker() {
|
---|
| 49 | @Override
|
---|
| 50 | protected int getMockResult(final ExtendedDialog instance) {
|
---|
| 51 | if (instance.getTitle().equals("Add setting")) {
|
---|
| 52 | return 1 + this.getButtonPositionFromLabel(instance, "Cancel");
|
---|
| 53 | } else {
|
---|
| 54 | return super.getMockResult(instance);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | };
|
---|
[10109] | 58 | PreferencesTable t = newTable();
|
---|
| 59 | t.fireDataChanged();
|
---|
| 60 | assertTrue(t.getSelectedItems().isEmpty());
|
---|
| 61 | assertFalse(t.editPreference(null));
|
---|
| 62 | assertNull(t.addPreference(null));
|
---|
| 63 | t.resetPreferences(null);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Unit test of {@link PreferencesTable.AllSettingsTableModel} class.
|
---|
| 68 | */
|
---|
| 69 | @Test
|
---|
[17275] | 70 | void testAllSettingsTableModel() {
|
---|
[10109] | 71 | AllSettingsTableModel model = (AllSettingsTableModel) newTable().getModel();
|
---|
| 72 | assertEquals(1, model.getRowCount());
|
---|
| 73 | assertFalse(model.isCellEditable(0, 0));
|
---|
| 74 | assertTrue(model.isCellEditable(0, 1));
|
---|
| 75 | assertEquals("key", model.getValueAt(0, 0));
|
---|
| 76 | assertEquals(newPrefEntry("value"), model.getValueAt(0, 1));
|
---|
| 77 | String foobar = "foobar";
|
---|
| 78 | model.setValueAt(foobar, 0, 1);
|
---|
| 79 | assertEquals(newPrefEntry(foobar), model.getValueAt(0, 1));
|
---|
| 80 | }
|
---|
| 81 | }
|
---|