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