1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
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.assertTrue;
|
---|
7 |
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.GraphicsEnvironment;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.FileInputStream;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.InputStream;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.JComponent;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 |
|
---|
22 | import org.junit.jupiter.api.Test;
|
---|
23 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
24 | import org.junit.jupiter.params.provider.EnumSource;
|
---|
25 | import org.openstreetmap.josm.TestUtils;
|
---|
26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
27 | import org.openstreetmap.josm.data.osm.UploadPolicy;
|
---|
28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
29 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
30 | import org.openstreetmap.josm.io.OsmReader;
|
---|
31 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
32 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
33 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
34 |
|
---|
35 | import mockit.Invocation;
|
---|
36 | import mockit.Mock;
|
---|
37 | import mockit.MockUp;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Unit tests of {@link SaveLayersDialog} class.
|
---|
41 | */
|
---|
42 | @BasicPreferences
|
---|
43 | class SaveLayersDialogTest {
|
---|
44 | /**
|
---|
45 | * Test of {@link SaveLayersDialog#confirmSaveLayerInfosOK}.
|
---|
46 | */
|
---|
47 | @Test
|
---|
48 | void testConfirmSaveLayerInfosOK() {
|
---|
49 | final List<SaveLayerInfo> list = Collections.singletonList(new SaveLayerInfo(new OsmDataLayer(new DataSet(), null, null)));
|
---|
50 |
|
---|
51 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
|
---|
52 | @Override
|
---|
53 | protected void act(final Object message) {
|
---|
54 | // use this opportunity to assert that our SaveLayerInfo is the single option in the JList
|
---|
55 | @SuppressWarnings("unchecked")
|
---|
56 | final JList<SaveLayerInfo> jList = (JList<SaveLayerInfo>) ((JComponent) message).getComponent(1);
|
---|
57 | assertEquals(1, jList.getModel().getSize());
|
---|
58 | assertEquals(list.get(0), jList.getModel().getElementAt(0));
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | protected String getStringFromMessage(final Object message) {
|
---|
63 | return ((JLabel) ((JComponent) message).getComponent(0)).getText();
|
---|
64 | }
|
---|
65 | };
|
---|
66 |
|
---|
67 | jopsMocker.getMockResultMap().put(
|
---|
68 | "<html>1 layer has unresolved conflicts.<br>Either resolve them first or discard the "
|
---|
69 | + "modifications.<br>Layer with conflicts:</html>", JOptionPane.OK_OPTION
|
---|
70 | );
|
---|
71 |
|
---|
72 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
|
---|
73 | @Override
|
---|
74 | public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
|
---|
75 | return list;
|
---|
76 | }
|
---|
77 | }));
|
---|
78 |
|
---|
79 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
80 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
81 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
|
---|
82 | assertEquals("Unsaved data and conflicts", invocationLogEntry[2]);
|
---|
83 |
|
---|
84 | jopsMocker.resetInvocationLog();
|
---|
85 | jopsMocker.getMockResultMap().clear();
|
---|
86 | jopsMocker.getMockResultMap().put(
|
---|
87 | "<html>1 layer needs saving but has no associated file.<br>Either select a file for this "
|
---|
88 | + "layer or discard the changes.<br>Layer without a file:</html>", JOptionPane.OK_OPTION
|
---|
89 | );
|
---|
90 |
|
---|
91 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
|
---|
92 | @Override
|
---|
93 | public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
|
---|
94 | return list;
|
---|
95 | }
|
---|
96 | }));
|
---|
97 |
|
---|
98 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
99 | invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
100 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
|
---|
101 | assertEquals("Unsaved data and missing associated file", invocationLogEntry[2]);
|
---|
102 |
|
---|
103 | jopsMocker.resetInvocationLog();
|
---|
104 | jopsMocker.getMockResultMap().clear();
|
---|
105 | jopsMocker.getMockResultMap().put(
|
---|
106 | "<html>1 layer needs saving but has an associated file<br>which cannot be written.<br>Either "
|
---|
107 | + "select another file for this layer or discard the changes.<br>Layer with a non-writable "
|
---|
108 | + "file:</html>", JOptionPane.OK_OPTION
|
---|
109 | );
|
---|
110 |
|
---|
111 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
|
---|
112 | @Override
|
---|
113 | public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
|
---|
114 | return list;
|
---|
115 | }
|
---|
116 | }));
|
---|
117 |
|
---|
118 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
119 | invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
120 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
|
---|
121 | assertEquals("Unsaved data non-writable files", invocationLogEntry[2]);
|
---|
122 |
|
---|
123 | jopsMocker.resetInvocationLog();
|
---|
124 | jopsMocker.getMockResultMap().clear();
|
---|
125 |
|
---|
126 | assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Non-regression test for #22817: No warning when deleting a layer with changes and discourages upload
|
---|
131 | * @param policy The upload policy to test
|
---|
132 | * @throws IOException if an error occurs
|
---|
133 | * @throws IllegalDataException if an error occurs
|
---|
134 | */
|
---|
135 | @ParameterizedTest
|
---|
136 | @EnumSource(value = UploadPolicy.class)
|
---|
137 | void testNonRegression22817(UploadPolicy policy) throws IOException, IllegalDataException {
|
---|
138 | File file = new File(TestUtils.getRegressionDataFile(22817, "data.osm"));
|
---|
139 | InputStream is = new FileInputStream(file);
|
---|
140 | final OsmDataLayer osmDataLayer = new OsmDataLayer(OsmReader.parseDataSet(is, null), null, null);
|
---|
141 | osmDataLayer.onPostLoadFromFile();
|
---|
142 | osmDataLayer.getDataSet().setUploadPolicy(policy);
|
---|
143 | osmDataLayer.setAssociatedFile(file);
|
---|
144 | assertTrue(osmDataLayer.getDataSet().isModified());
|
---|
145 | assertFalse(osmDataLayer.requiresSaveToFile());
|
---|
146 | assertTrue(osmDataLayer.getDataSet().requiresUploadToServer());
|
---|
147 | assertEquals(policy != UploadPolicy.BLOCKED, osmDataLayer.requiresUploadToServer());
|
---|
148 | assertEquals(policy != UploadPolicy.BLOCKED, osmDataLayer.isUploadable());
|
---|
149 | new WindowMocker();
|
---|
150 | // Needed since the *first call* is to check whether we are in a headless environment
|
---|
151 | new GraphicsEnvironmentMock();
|
---|
152 | // Needed since we need to mock out the UI
|
---|
153 | SaveLayersDialogMock saveLayersDialogMock = new SaveLayersDialogMock();
|
---|
154 | assertTrue(SaveLayersDialog.saveUnsavedModifications(Collections.singleton(osmDataLayer), SaveLayersDialog.Reason.DELETE));
|
---|
155 | int res = saveLayersDialogMock.getUserActionCalled;
|
---|
156 | if (policy == UploadPolicy.NORMAL) {
|
---|
157 | assertEquals(1, res, "The user should have been asked for an action on the layer");
|
---|
158 | } else {
|
---|
159 | assertEquals(0, res, "The user should not have been asked for an action on the layer");
|
---|
160 |
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | private static final class GraphicsEnvironmentMock extends MockUp<GraphicsEnvironment> {
|
---|
165 | @Mock
|
---|
166 | public static boolean isHeadless(Invocation invocation) {
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static final class SaveLayersDialogMock extends MockUp<SaveLayersDialog> {
|
---|
172 | private final SaveLayersModel model = new SaveLayersModel();
|
---|
173 | private int getUserActionCalled = 0;
|
---|
174 | @Mock
|
---|
175 | public void $init(Component parent) {
|
---|
176 | // Do nothing
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Mock
|
---|
180 | public void prepareForSavingAndUpdatingLayers(final SaveLayersDialog.Reason reason) {
|
---|
181 | // Do nothing
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Mock
|
---|
185 | public SaveLayersModel getModel() {
|
---|
186 | return this.model;
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Mock
|
---|
190 | public void setVisible(boolean b) {
|
---|
191 | // Do nothing
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Mock
|
---|
195 | public SaveLayersDialog.UserAction getUserAction() {
|
---|
196 | this.getUserActionCalled++;
|
---|
197 | return SaveLayersDialog.UserAction.PROCEED;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Mock
|
---|
201 | public void closeDialog() {
|
---|
202 | // Do nothing
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|