001: /*
002: * EditorTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui;
013:
014: import java.awt.Toolkit;
015: import java.awt.datatransfer.Clipboard;
016: import java.awt.datatransfer.DataFlavor;
017: import java.awt.event.KeyEvent;
018: import javax.swing.JMenuItem;
019: import junit.framework.TestCase;
020: import org.netbeans.jemmy.QueueTool;
021: import org.netbeans.jemmy.operators.JButtonOperator;
022: import org.netbeans.jemmy.operators.JCheckBoxOperator;
023: import org.netbeans.jemmy.operators.JComponentOperator;
024: import org.netbeans.jemmy.operators.JDialogOperator;
025: import org.netbeans.jemmy.operators.JFrameOperator;
026: import org.netbeans.jemmy.operators.JListOperator;
027: import org.netbeans.jemmy.operators.JMenuBarOperator;
028: import org.netbeans.jemmy.operators.JMenuItemOperator;
029: import org.netbeans.jemmy.operators.JMenuOperator;
030: import org.netbeans.jemmy.operators.JTextFieldOperator;
031: import workbench.gui.sql.EditorPanel;
032: import workbench.resource.Settings;
033:
034: /**
035: * @author support@sql-workbench.net
036: */
037: public class EditorTest extends TestCase {
038: private GuiTestUtil testUtil;
039:
040: public EditorTest(String testName) {
041: super (testName);
042: this .testUtil = new GuiTestUtil("EditorTest");
043: }
044:
045: private void startApplication() {
046: try {
047: testUtil.startApplication();
048: } catch (Exception e) {
049: e.printStackTrace();
050: fail(e.getMessage());
051: }
052: }
053:
054: private void commentText() {
055: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
056: NamedComponentChooser chooser = new NamedComponentChooser();
057: chooser.setName("sqleditor1");
058: JComponentOperator editorComp = new JComponentOperator(
059: mainWindow, chooser);
060: EditorPanel editor = (EditorPanel) editorComp.getSource();
061:
062: editor.setText("select nr, firstname, lastname\nfrom person;");
063: editor.setCaretPosition(0);
064: // editor.selectNone();
065:
066: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
067: JMenuOperator editMenu = new JMenuOperator(mainMenu.getMenu(1));
068: JMenuItem commentItem = (JMenuItem) editMenu
069: .getMenuComponent(17);
070: JMenuItem uncommentItem = (JMenuItem) editMenu
071: .getMenuComponent(18);
072:
073: JMenuItemOperator comment = new JMenuItemOperator(commentItem);
074: JMenuItemOperator unComment = new JMenuItemOperator(
075: uncommentItem);
076:
077: QueueTool tool = new QueueTool();
078:
079: //editor.select(0, 15);
080: comment.push();
081: tool.waitEmpty();
082:
083: String text = editor.getText();
084: assertTrue(text.startsWith("--"));
085:
086: //editor.select(0, 15);
087:
088: // test toggle
089: comment.push();
090: tool.waitEmpty();
091:
092: text = editor.getText();
093: assertFalse(text.startsWith("--"));
094:
095: editor.setText("-- first line\n-- second line\n");
096: editor.selectAll();
097: tool.waitEmpty();
098: unComment.push();
099: tool.waitEmpty();
100: text = editor.getText();
101:
102: assertEquals(" first line\n second line\n", text);
103: }
104:
105: private void copySnippet() {
106: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
107: NamedComponentChooser chooser = new NamedComponentChooser();
108: chooser.setName("sqleditor1");
109: JComponentOperator editorComp = new JComponentOperator(
110: mainWindow, chooser);
111: final EditorPanel editor = (EditorPanel) editorComp.getSource();
112:
113: editor.setText("select nr, firstname, lastname\nfrom person;");
114: editor.setCaretPosition(0);
115:
116: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
117: JMenuOperator sqlMenu = new JMenuOperator(mainMenu.getMenu(4));
118:
119: QueueTool tool = new QueueTool();
120: editor.selectAll();
121: tool.waitEmpty();
122:
123: Clipboard clp = Toolkit.getDefaultToolkit()
124: .getSystemClipboard();
125:
126: try {
127: mainMenu.pushMenu("SQL|Copy Code Snippet", "|");
128: tool.waitEmpty();
129:
130: final String text = (String) clp
131: .getData(DataFlavor.stringFlavor);
132: // System.out.println("clipboard: " + text);
133: assertTrue(text.startsWith("String sql = \"select nr,"));
134:
135: editor.setText(text);
136: editor.selectAll();
137: mainMenu.pushMenu("SQL|Clean Java Code", "|");
138: tool.waitEmpty();
139: String newText = editor.getText();
140: // System.out.println("new text: " + newText);
141: assertTrue(newText.startsWith("select nr,"));
142:
143: } catch (Exception e) {
144: fail(e.getMessage());
145: }
146: }
147:
148: private void findText() {
149: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
150: NamedComponentChooser chooser = new NamedComponentChooser();
151: chooser.setName("sqleditor1");
152: JComponentOperator editorComp = new JComponentOperator(
153: mainWindow, chooser);
154: EditorPanel editor = (EditorPanel) editorComp.getSource();
155:
156: editor.setText("select * from person;");
157: editor.setCaretPosition(0);
158:
159: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
160: mainMenu.pushMenuNoBlock("Edit|Find");
161: JDialogOperator dialog = new JDialogOperator(mainWindow, "Find");
162:
163: setCheckbox(dialog, "regex", false);
164: setCheckbox(dialog, "ignorecase", false);
165: setCheckbox(dialog, "wholeword", false);
166:
167: setTextField(dialog, "searchtext", "person");
168:
169: JButtonOperator ok = new JButtonOperator(dialog, "OK");
170: ok.push();
171:
172: assertEquals("person", editor.getSelectedText());
173: }
174:
175: private void replaceText() {
176: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
177: NamedComponentChooser chooser = new NamedComponentChooser();
178: chooser.setName("sqleditor1");
179: JComponentOperator editorComp = new JComponentOperator(
180: mainWindow, chooser);
181: EditorPanel editor = (EditorPanel) editorComp.getSource();
182:
183: editor.setText("select * from person;");
184: JMenuBarOperator mainMenu = new JMenuBarOperator(mainWindow);
185: mainMenu.pushMenuNoBlock("Edit|Replace");
186: JDialogOperator dialog = new JDialogOperator(mainWindow,
187: "Replace");
188:
189: setCheckbox(dialog, "regex", false);
190: setCheckbox(dialog, "ignorecase", true);
191: setCheckbox(dialog, "wordsonly", false);
192: setCheckbox(dialog, "selectedtext", false);
193:
194: setTextField(dialog, "searchtext", "*");
195: setTextField(dialog, "replacetext", "nr, firstname, lastname");
196:
197: chooser.setName("replaceallbutton");
198: JButtonOperator replaceAll = new JButtonOperator(dialog,
199: chooser);
200: replaceAll.push();
201:
202: new JButtonOperator(dialog, "Close").push();
203:
204: assertEquals("select nr, firstname, lastname from person;",
205: editor.getText());
206: }
207:
208: private void changeWordSep(String newSep) {
209: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
210: new JMenuBarOperator(mainWindow).pushMenuNoBlock(
211: "Tools|Options", "|");
212: JDialogOperator dialog = new JDialogOperator(mainWindow,
213: "Settings");
214: final JListOperator pages = new JListOperator(dialog);
215:
216: pages.selectItem(1); // select the editor page
217: setTextField(dialog, "nowordsep", newSep);
218: new JButtonOperator(dialog, "OK").push();
219: QueueTool tool = new QueueTool();
220: tool.waitEmpty();
221: }
222:
223: private void checkWordSep() {
224: JFrameOperator mainWindow = new JFrameOperator("SQL Workbench");
225: NamedComponentChooser chooser = new NamedComponentChooser();
226: chooser.setName("sqleditor1");
227: JComponentOperator editorComp = new JComponentOperator(
228: mainWindow, chooser);
229: EditorPanel editor = (EditorPanel) editorComp.getSource();
230:
231: QueueTool tool = new QueueTool();
232:
233: changeWordSep("");
234: assertEquals("Wrong word separator", "", Settings.getInstance()
235: .getEditorNoWordSep());
236:
237: editor.setText("my_person;");
238: editor.setCaretPosition(0);
239:
240: tool.waitEmpty();
241:
242: editorComp.pushKey(KeyEvent.VK_RIGHT, KeyEvent.CTRL_MASK);
243: tool.waitEmpty();
244:
245: int pos = editor.getCaretPosition();
246: assertEquals("Wrong word jump", 2, pos);
247:
248: changeWordSep("_");
249: assertEquals("Wrong word separator", "_", Settings
250: .getInstance().getEditorNoWordSep());
251:
252: editor.setCaretPosition(0);
253: editorComp.pushKey(KeyEvent.VK_RIGHT, KeyEvent.CTRL_MASK);
254: tool.waitEmpty();
255:
256: pos = editor.getCaretPosition();
257: assertEquals("Wrong word jump", 9, pos);
258: }
259:
260: private void setTextField(JDialogOperator dialog, String name,
261: String newText) {
262: NamedComponentChooser chooser = new NamedComponentChooser();
263: chooser.setName(name);
264: JTextFieldOperator searchtext = new JTextFieldOperator(dialog,
265: chooser);
266: searchtext.setText(newText);
267: }
268:
269: private void setCheckbox(JDialogOperator dialog, String cbxName,
270: boolean selected) {
271: NamedComponentChooser chooser = new NamedComponentChooser();
272: chooser.setName(cbxName);
273: JCheckBoxOperator cbx = new JCheckBoxOperator(dialog, chooser);
274: cbx.setSelected(selected);
275: }
276:
277: public void testEditor() {
278: try {
279: startApplication();
280: replaceText();
281: findText();
282: commentText();
283: copySnippet();
284: checkWordSep();
285: testUtil.stopApplication();
286: } catch (Exception e) {
287: e.printStackTrace();
288: fail(e.getMessage());
289: }
290: }
291: }
|