1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 |
|
---|
7 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
8 | import org.junit.jupiter.api.Test;
|
---|
9 | import org.openstreetmap.josm.TestUtils;
|
---|
10 | import org.openstreetmap.josm.command.Command;
|
---|
11 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
14 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
17 |
|
---|
18 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Unit tests of {@link CommandStackDialog} class.
|
---|
22 | */
|
---|
23 | class CommandStackDialogTest {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Setup tests
|
---|
27 | */
|
---|
28 | @RegisterExtension
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules().main().projection();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unit test of {@link CommandStackDialog} class - empty case.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | void testCommandStackDialogEmpty() {
|
---|
37 | CommandStackDialog dlg = new CommandStackDialog();
|
---|
38 | dlg.showDialog();
|
---|
39 | assertTrue(dlg.isVisible());
|
---|
40 | dlg.hideDialog();
|
---|
41 | assertFalse(dlg.isVisible());
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Unit test of {@link CommandStackDialog} class - not empty case.
|
---|
46 | */
|
---|
47 | @Test
|
---|
48 | void testCommandStackDialogNotEmpty() {
|
---|
49 | DataSet ds = new DataSet();
|
---|
50 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
51 | MainApplication.getLayerManager().addLayer(layer);
|
---|
52 | try {
|
---|
53 | Command cmd1 = TestUtils.newCommand(ds);
|
---|
54 | Command cmd2 = TestUtils.newCommand(ds);
|
---|
55 | UndoRedoHandler.getInstance().add(cmd1);
|
---|
56 | UndoRedoHandler.getInstance().add(cmd2);
|
---|
57 | UndoRedoHandler.getInstance().undo(1);
|
---|
58 |
|
---|
59 | assertTrue(UndoRedoHandler.getInstance().hasUndoCommands());
|
---|
60 | assertTrue(UndoRedoHandler.getInstance().hasRedoCommands());
|
---|
61 |
|
---|
62 | MapFrame map = MainApplication.getMap();
|
---|
63 | CommandStackDialog dlg = new CommandStackDialog();
|
---|
64 | map.addToggleDialog(dlg);
|
---|
65 | dlg.unfurlDialog();
|
---|
66 | assertTrue(dlg.isVisible());
|
---|
67 | map.removeToggleDialog(dlg);
|
---|
68 | dlg.hideDialog();
|
---|
69 | assertFalse(dlg.isVisible());
|
---|
70 | } finally {
|
---|
71 | UndoRedoHandler.getInstance().clean();
|
---|
72 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Unit test of {@link CommandStackDialog} class - undo followed by addCommand should empty redo tree.
|
---|
78 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/16911">Bug #16911</a>.
|
---|
79 | */
|
---|
80 | @Test
|
---|
81 | void testCommandStackDialogUndoAddCommand() {
|
---|
82 | DataSet ds = new DataSet();
|
---|
83 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
84 | MainApplication.getLayerManager().addLayer(layer);
|
---|
85 | try {
|
---|
86 | Command cmd1 = TestUtils.newCommand(ds);
|
---|
87 | Command cmd2 = TestUtils.newCommand(ds);
|
---|
88 | Command cmd3 = TestUtils.newCommand(ds);
|
---|
89 | MapFrame map = MainApplication.getMap();
|
---|
90 | CommandStackDialog dlg = new CommandStackDialog();
|
---|
91 | map.addToggleDialog(dlg);
|
---|
92 | dlg.unfurlDialog();
|
---|
93 | assertTrue(dlg.isVisible());
|
---|
94 | assertTrue(dlg.redoTreeIsEmpty());
|
---|
95 | UndoRedoHandler.getInstance().add(cmd1);
|
---|
96 | assertTrue(dlg.redoTreeIsEmpty());
|
---|
97 | UndoRedoHandler.getInstance().add(cmd2);
|
---|
98 | assertTrue(dlg.redoTreeIsEmpty());
|
---|
99 | UndoRedoHandler.getInstance().undo(1);
|
---|
100 | assertFalse(dlg.redoTreeIsEmpty());
|
---|
101 | UndoRedoHandler.getInstance().add(cmd3);
|
---|
102 | assertTrue(dlg.redoTreeIsEmpty());
|
---|
103 |
|
---|
104 | assertTrue(UndoRedoHandler.getInstance().hasUndoCommands());
|
---|
105 | assertFalse(UndoRedoHandler.getInstance().hasRedoCommands());
|
---|
106 |
|
---|
107 | map.removeToggleDialog(dlg);
|
---|
108 | dlg.hideDialog();
|
---|
109 | assertFalse(dlg.isVisible());
|
---|
110 | } finally {
|
---|
111 | UndoRedoHandler.getInstance().clean();
|
---|
112 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|