source: josm/trunk/test/unit/org/openstreetmap/josm/gui/dialogs/CommandStackDialogTest.java@ 17536

Last change on this file since 17536 was 17347, checked in by GerdP, 3 years ago

fix #17196: Undo/Redo may change data in inactive layer

  • use separate stacks for each edit layer
  • show empty undo/redo stack when active layer isn't an edit layer
  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.junit.jupiter.api.Assertions.assertFalse;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6
7import org.junit.jupiter.api.extension.RegisterExtension;
8import org.junit.jupiter.api.Test;
9import org.openstreetmap.josm.TestUtils;
10import org.openstreetmap.josm.command.Command;
11import org.openstreetmap.josm.data.UndoRedoHandler;
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.gui.MainApplication;
14import org.openstreetmap.josm.gui.MapFrame;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.testutils.JOSMTestRules;
17
18import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19
20/**
21 * Unit tests of {@link CommandStackDialog} class.
22 */
23class 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().preferences();
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}
Note: See TracBrowser for help on using the repository browser.