1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JDialog;
|
---|
12 |
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
20 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.CancelAction;
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ChangesetDetailViewSynchronizer;
|
---|
22 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.CloseSelectedChangesetsAction;
|
---|
23 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.DownloadMyChangesets;
|
---|
24 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.DownloadSelectedChangesetContentAction;
|
---|
25 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.DownloadSelectedChangesetsAction;
|
---|
26 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.QueryAction;
|
---|
27 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.RemoveFromCacheAction;
|
---|
28 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ShowDetailAction;
|
---|
29 | import org.openstreetmap.josm.gui.dialogs.changeset.query.ChangesetQueryDialog;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 | import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
|
---|
32 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
33 |
|
---|
34 | import mockit.Invocation;
|
---|
35 | import mockit.Mock;
|
---|
36 | import mockit.MockUp;
|
---|
37 |
|
---|
38 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Unit tests of {@link ChangesetCacheManager} class.
|
---|
42 | */
|
---|
43 | class ChangesetCacheManagerTest {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Setup tests
|
---|
47 | */
|
---|
48 | @RegisterExtension
|
---|
49 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
50 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Unit test of {@link ChangesetCacheManager#destroyInstance}.
|
---|
54 | */
|
---|
55 | @Test
|
---|
56 | void testDestroyInstance() {
|
---|
57 | ChangesetCacheManager.destroyInstance();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Unit test of {@link ChangesetCacheManager#buildButtonPanel},
|
---|
62 | * {@link ChangesetCacheManager#buildToolbarPanel}.
|
---|
63 | * {@link ChangesetCacheManager#buildModel}.
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | void testBuild() {
|
---|
67 | assertNotNull(ChangesetCacheManager.buildButtonPanel());
|
---|
68 | assertNotNull(ChangesetCacheManager.buildToolbarPanel());
|
---|
69 | assertNotNull(ChangesetCacheManager.buildModel());
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Unit test of {@link ChangesetCacheManager.ChangesetDetailViewSynchronizer} class.
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | void testChangesetDetailViewSynchronizer() {
|
---|
77 | new ChangesetDetailViewSynchronizer(new ChangesetCacheManagerModel(null) {
|
---|
78 | @Override
|
---|
79 | public List<Changeset> getSelectedChangesets() {
|
---|
80 | return Collections.emptyList();
|
---|
81 | }
|
---|
82 | }).valueChanged(null);
|
---|
83 |
|
---|
84 | new ChangesetDetailViewSynchronizer(new ChangesetCacheManagerModel(null) {
|
---|
85 | @Override
|
---|
86 | public List<Changeset> getSelectedChangesets() {
|
---|
87 | return Collections.singletonList(new Changeset());
|
---|
88 | }
|
---|
89 | }).valueChanged(null);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Unit test of {@link ChangesetCacheManager.CancelAction} class.
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | void testCancelAction() {
|
---|
97 | new CancelAction().actionPerformed(null);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Unit test of {@link ChangesetCacheManager.CloseSelectedChangesetsAction} class.
|
---|
102 | */
|
---|
103 | @Test
|
---|
104 | void testCloseSelectedChangesetsAction() {
|
---|
105 | CloseSelectedChangesetsAction action = new CloseSelectedChangesetsAction(new ChangesetCacheManagerModel(null) {
|
---|
106 | @Override
|
---|
107 | public List<Changeset> getSelectedChangesets() {
|
---|
108 | return Collections.singletonList(new Changeset());
|
---|
109 | }
|
---|
110 | });
|
---|
111 | action.valueChanged(null);
|
---|
112 | action.actionPerformed(null);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Unit test of {@link ChangesetCacheManager.DownloadMyChangesets} class.
|
---|
117 | */
|
---|
118 | @Test
|
---|
119 | void testDownloadMyChangesets() {
|
---|
120 | TestUtils.assumeWorkingJMockit();
|
---|
121 | final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
|
---|
122 | Collections.singletonMap(
|
---|
123 | "<html>JOSM is currently running with an anonymous user. It cannot download<br>"
|
---|
124 | + "your changesets from the OSM server unless you enter your OSM user name<br>"
|
---|
125 | + "in the JOSM preferences.</html>",
|
---|
126 | "OK"
|
---|
127 | )
|
---|
128 | );
|
---|
129 |
|
---|
130 | new DownloadMyChangesets().actionPerformed(null);
|
---|
131 |
|
---|
132 | assertEquals(1, haMocker.getInvocationLog().size());
|
---|
133 | Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
|
---|
134 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
135 | assertEquals("Warning", invocationLogEntry[2]);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Unit test of {@link ChangesetCacheManager.DownloadSelectedChangesetContentAction} class.
|
---|
140 | */
|
---|
141 | @Test
|
---|
142 | void testDownloadSelectedChangesetContentAction() {
|
---|
143 | if (GraphicsEnvironment.isHeadless()) {
|
---|
144 | TestUtils.assumeWorkingJMockit();
|
---|
145 | // to allow us to construct a JDialog
|
---|
146 | new WindowMocker();
|
---|
147 | }
|
---|
148 |
|
---|
149 | DownloadSelectedChangesetContentAction action = new DownloadSelectedChangesetContentAction(ChangesetCacheManager.buildModel());
|
---|
150 | action.valueChanged(null);
|
---|
151 | action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo"));
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Unit test of {@link ChangesetCacheManager.DownloadSelectedChangesetsAction} class.
|
---|
156 | */
|
---|
157 | @Test
|
---|
158 | void testDownloadSelectedChangesetsAction() {
|
---|
159 | if (GraphicsEnvironment.isHeadless()) {
|
---|
160 | TestUtils.assumeWorkingJMockit();
|
---|
161 | // to allow us to construct a JDialog
|
---|
162 | new WindowMocker();
|
---|
163 | }
|
---|
164 |
|
---|
165 | DownloadSelectedChangesetsAction action = new DownloadSelectedChangesetsAction(ChangesetCacheManager.buildModel());
|
---|
166 | action.valueChanged(null);
|
---|
167 | action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo"));
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Unit test of {@link ChangesetCacheManager.QueryAction} class.
|
---|
172 | */
|
---|
173 | @Test
|
---|
174 | void testQueryAction() {
|
---|
175 | TestUtils.assumeWorkingJMockit();
|
---|
176 |
|
---|
177 | // set up mockers to simulate the dialog being cancelled
|
---|
178 | final boolean[] dialogShown = new boolean[] {false};
|
---|
179 | if (GraphicsEnvironment.isHeadless()) {
|
---|
180 | new WindowMocker();
|
---|
181 | }
|
---|
182 | new MockUp<JDialog>() {
|
---|
183 | @Mock
|
---|
184 | void setVisible(final Invocation invocation, final boolean visible) throws Exception {
|
---|
185 | if (visible) {
|
---|
186 | ((JButton) TestUtils.getComponentByName((JDialog) invocation.getInvokedInstance(), "cancelButton")).doClick();
|
---|
187 | dialogShown[0] = true;
|
---|
188 | }
|
---|
189 | // critically, don't proceed into implementation
|
---|
190 | }
|
---|
191 | };
|
---|
192 | new MockUp<ChangesetQueryDialog>() {
|
---|
193 | @Mock
|
---|
194 | void setVisible(final Invocation invocation, final boolean visible) throws Exception {
|
---|
195 | if (GraphicsEnvironment.isHeadless()) {
|
---|
196 | // we have to mock the behaviour quite coarsely as much of ChangesetQueryDialog will
|
---|
197 | // raise a HeadlessException
|
---|
198 | if (visible) {
|
---|
199 | TestUtils.setPrivateField(ChangesetQueryDialog.class, invocation.getInvokedInstance(), "canceled", true);
|
---|
200 | dialogShown[0] = true;
|
---|
201 | }
|
---|
202 | } else {
|
---|
203 | // proceeding into the implementation allows a bit more of the target code to be
|
---|
204 | // covered, actual mocking is performed on JDialog's setVisible()
|
---|
205 | invocation.proceed(visible);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | };
|
---|
209 |
|
---|
210 | new QueryAction().actionPerformed(null);
|
---|
211 |
|
---|
212 | assertTrue(dialogShown[0]);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Unit test of {@link ChangesetCacheManager.RemoveFromCacheAction} class.
|
---|
217 | */
|
---|
218 | @Test
|
---|
219 | void testRemoveFromCacheAction() {
|
---|
220 | RemoveFromCacheAction action = new RemoveFromCacheAction(ChangesetCacheManager.buildModel());
|
---|
221 | action.valueChanged(null);
|
---|
222 | action.actionPerformed(null);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Unit test of {@link ChangesetCacheManager.ShowDetailAction} class.
|
---|
227 | */
|
---|
228 | @Test
|
---|
229 | void testShowDetailAction() {
|
---|
230 | new ShowDetailAction(ChangesetCacheManager.buildModel()).actionPerformed(null);
|
---|
231 | }
|
---|
232 | }
|
---|