Skip to content

Commit e399b1e

Browse files
author
Federico Fissore
committedFeb 20, 2013
first functional test made with FEST: simulation of user interaction with the IDE
found and solved a bug with caret positioning with a cleared editor (see #1288)
1 parent 271a2c5 commit e399b1e

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
 

‎app/src/processing/app/Editor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public void windowDeactivated(WindowEvent e) {
232232
upper.add(header);
233233

234234
textarea = new JEditTextArea(new PdeTextAreaDefaults());
235+
textarea.setName("editor");
235236
textarea.setRightClickPopup(new TextAreaPopup());
236237
textarea.setHorizontalOffset(6);
237238

@@ -1135,9 +1136,11 @@ public void actionPerformed(ActionEvent e) {
11351136

11361137
protected JMenu buildEditMenu() {
11371138
JMenu menu = new JMenu(_("Edit"));
1139+
menu.setName("menuEdit");
11381140
JMenuItem item;
11391141

11401142
undoItem = newJMenuItem(_("Undo"), 'Z');
1143+
undoItem.setName("menuEditUndo");
11411144
undoItem.addActionListener(undoAction = new UndoAction());
11421145
menu.add(undoItem);
11431146

@@ -1146,6 +1149,7 @@ protected JMenu buildEditMenu() {
11461149
} else {
11471150
redoItem = newJMenuItemShift(_("Redo"), 'Z');
11481151
}
1152+
redoItem.setName("menuEditRedo");
11491153
redoItem.addActionListener(redoAction = new RedoAction());
11501154
menu.add(redoItem);
11511155

@@ -1345,7 +1349,10 @@ public void actionPerformed(ActionEvent e) {
13451349
}
13461350
if (undo.getLastUndoableEdit() != null && undo.getLastUndoableEdit() instanceof CaretAwareUndoableEdit) {
13471351
CaretAwareUndoableEdit undoableEdit = (CaretAwareUndoableEdit) undo.getLastUndoableEdit();
1348-
textarea.setCaretPosition(undoableEdit.getCaretPosition() - 1);
1352+
int nextCaretPosition = undoableEdit.getCaretPosition() - 1;
1353+
if (nextCaretPosition >= 0 && textarea.getDocumentLength() > nextCaretPosition) {
1354+
textarea.setCaretPosition(nextCaretPosition);
1355+
}
13491356
}
13501357
updateUndoState();
13511358
redoAction.updateRedoState();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package processing.app;
2+
3+
import org.fest.swing.core.ComponentMatcher;
4+
import org.fest.swing.fixture.FrameFixture;
5+
import org.fest.swing.fixture.JMenuItemFixture;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import processing.app.syntax.JEditTextArea;
9+
10+
import javax.swing.*;
11+
import java.awt.*;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class ReplacingTextGeneratesTwoUndoActionsTest {
16+
17+
private FrameFixture window;
18+
private Base base;
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
Base.initPlatform();
23+
Preferences.init(null);
24+
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
25+
Theme.init();
26+
Base.platform.setLookAndFeel();
27+
Base.untitledFolder = Base.createTempFolder("untitled");
28+
Base.untitledFolder.deleteOnExit();
29+
30+
base = new Base(new String[0]);
31+
window = new FrameFixture(base.editors.get(0));
32+
}
33+
34+
@Test
35+
public void testName() throws Exception {
36+
JMenuItemFixture menuEditUndo = window.menuItem("menuEditUndo");
37+
menuEditUndo.requireDisabled();
38+
JMenuItemFixture menuEditRedo = window.menuItem("menuEditRedo");
39+
menuEditRedo.requireDisabled();
40+
41+
JEditTextArea jEditTextArea = (JEditTextArea) window.robot.finder().find(new ComponentMatcher() {
42+
@Override
43+
public boolean matches(Component component) {
44+
return component instanceof JEditTextArea && "editor".equals(component.getName());
45+
}
46+
});
47+
48+
jEditTextArea.setText("fake text");
49+
50+
menuEditUndo.requireEnabled();
51+
menuEditUndo.click();
52+
53+
assertEquals("", jEditTextArea.getText());
54+
55+
menuEditRedo.requireEnabled();
56+
menuEditRedo.click();
57+
58+
assertEquals("fake text", jEditTextArea.getText());
59+
60+
menuEditUndo.requireEnabled();
61+
menuEditUndo.click();
62+
menuEditUndo.click();
63+
menuEditUndo.requireDisabled();
64+
menuEditRedo.requireEnabled();
65+
}
66+
}

0 commit comments

Comments
 (0)
Please sign in to comment.