1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
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;
|
---|
8 |
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
16 | import org.openstreetmap.josm.gui.preferences.advanced.PreferencesTable.AllSettingsTableModel;
|
---|
17 | import org.openstreetmap.josm.spi.preferences.StringSetting;
|
---|
18 | import org.openstreetmap.josm.testutils.annotations.AssertionsInEDT;
|
---|
19 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
20 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
21 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link PreferencesTable} class.
|
---|
25 | */
|
---|
26 | @AssertionsInEDT
|
---|
27 | @BasicPreferences
|
---|
28 | class PreferencesTableTest {
|
---|
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() {
|
---|
36 | return new PreferencesTable(Collections.singletonList(newPrefEntry("value")));
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Unit test of {@link PreferencesTable#PreferencesTable}.
|
---|
41 | */
|
---|
42 | @Test
|
---|
43 | void testPreferencesTable() {
|
---|
44 | TestUtils.assumeWorkingJMockit();
|
---|
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);
|
---|
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 | };
|
---|
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
|
---|
70 | void testAllSettingsTableModel() {
|
---|
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 | }
|
---|