private String getTextFor(OpenDefinitionsDocument doc) { DefinitionsPane pane = _frame.getDefPaneGivenODD(doc); String endl = "\n"; // was StringOps.EOL; but Swing uses '\n' for newLine int loc = pane.getCaretPosition(); int start = loc; int end = loc; String text; text = doc.getText(); /* get the starting point of 2 lines up... */ for (int i = 0; i < 4; i++) { if (start > 0) start = text.lastIndexOf(endl, start - endl.length()); } if (start == -1) start = 0; // skip the end line, if we're at one // if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == // endl) // start += endl.length(); if (doc.getLength() >= endl.length() && text.substring(start, start + endl.length()).equals(endl)) start += endl.length(); /* get the ending point 2 lines down */ int index; for (int i = 0; i < 4; i++) { if (end < doc.getLength()) { index = text.indexOf(endl, end + endl.length()); if (index != -1) end = index; } } if (end < start) end = start; text = text.substring(start, end); return text; }
/** * Tests that the clipboard is modified after a "cut line" action. NOTE: Commented out for commit * because of failures, despite proper behavior in GUI. This may not work unless ActionEvents are * dropped in the event queue */ public void testCutLine() throws BadLocationException { // First, copy some data out of the main document. final DefinitionsPane pane = _frame.getCurrentDefPane(); final OpenDefinitionsDocument doc = pane.getOpenDefDocument(); // _frame.setVisible(true); Utilities.invokeAndWait( new Runnable() { public void run() { try { doc.insertString(0, "abcdefg", null); pane.setCaretPosition(5); _frame.validate(); // _frame.paint(_frame.getGraphics()); // ActionMap actionMap = _pane.getActionMap(); // String selString = DefaultEditorKit.selectionEndLineAction; // actionMap.get(selString).actionPerformed(new ActionEvent(pane, 0, // "SelectionEndLine")); pane.setSelectionStart(2); pane.setSelectionEnd(7); _frame.validate(); pane.cut(); // _frame.cutAction.actionPerformed(new ActionEvent(pane, 0, "Cut")); // Get a copy of the current clipboard. // Trigger the Cut Line action from a new position. // _pane.setCaretPosition(2); _frame.validate(); // _frame.paint(_frame.getGraphics()); // _frame._cutLineAction.actionPerformed(new ActionEvent(this, 0, "Cut // Line")); // _frame.dispatchEvent(new ActionEvent(this, 0, "Cut Line")); // Verify that the clipboard contents are what we expect. Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable contents = clip.getContents(null); _data = (String) contents.getTransferData(DataFlavor.stringFlavor); } catch (Throwable t) { listenerFail(t.getMessage()); } } }); Utilities.clearEventQueue(); assertEquals("Clipboard contents should be changed after Cut Line.", "cdefg", _data); // Verify that the document text is what we expect. assertEquals("Current line of text should be truncated by Cut Line.", "ab", doc.getText()); _log.log("testCutLine completed"); }
/** * Tests that the current location of a document is equal to the caret Position after switching to * another document and back. */ public void testDocLocationAfterSwitch() throws BadLocationException { final DefinitionsPane pane = _frame.getCurrentDefPane(); final OpenDefinitionsDocument doc = pane.getOpenDefDocument(); setDocText(doc.getDocument(), "abcd"); Utilities.invokeAndWait( new Runnable() { public void run() { doc.setCurrentLocation(3); pane.setCaretPosition(3); // The caret is not affected by setCurrentLocation } }); assertEquals("Location of old doc before switch", 3, doc.getCurrentLocation()); assertEquals("Location of cursor in old document", 3, pane.getCaretPosition()); // Create a new file SingleDisplayModel model = _frame.getModel(); final OpenDefinitionsDocument oldDoc = doc; final DefinitionsPane oldPane = pane; final OpenDefinitionsDocument newDoc = model.newFile(); // Current pane should be new doc, pos 0 DefinitionsPane curPane; OpenDefinitionsDocument curDoc; curPane = _frame.getCurrentDefPane(); curDoc = curPane.getOpenDefDocument(); // .getDocument(); assertEquals("New curr DefPane's document", newDoc, curDoc); assertEquals("Location in new document", 0, newDoc.getCurrentLocation()); // Switch back to old document model.setActiveNextDocument(); Utilities.clearEventQueue(); assertEquals("Next active doc", oldDoc, model.getActiveDocument()); // Current pane should be old doc, pos 3 curPane = _frame.getCurrentDefPane(); curDoc = curPane.getOpenDefDocument(); // .getDocument(); assertEquals("Next active pane", oldPane, curPane); assertEquals("Current document is old document", oldDoc, curDoc); assertEquals("Location of caret in old document", 3, curPane.getCaretPosition()); _log.log("testDocLocationAfterSwitch completed"); }
/** * Ensure that a document's editable status is set appropriately throughout the compile process. * Since the behavior is interesting only when the model changes its active document, that's what * this test looks most like. */ public void testGlassPaneEditableState() { SingleDisplayModel model = _frame.getModel(); final OpenDefinitionsDocument doc1 = model.newFile(); final OpenDefinitionsDocument doc2 = model.newFile(); // doc2 is now active Utilities.invokeAndWait( new Runnable() { public void run() { _pane1 = _frame._createDefScrollPane(doc1); _pane2 = _frame._createDefScrollPane(doc2); _defPane1 = (DefinitionsPane) _pane1.getViewport().getView(); _defPane2 = (DefinitionsPane) _pane2.getViewport().getView(); _frame._switchDefScrollPane(); } }); Utilities.clearEventQueue(); // Execute all pending asynchronous tasks; assertTrue("Start: defPane1", _defPane1.isEditable()); assertTrue("Start: defPane2", _defPane2.isEditable()); Utilities.invokeAndWait( new Runnable() { public void run() { _frame.hourglassOn(); } }); Utilities.clearEventQueue(); assertTrue("Glass on: defPane1", _defPane1.isEditable()); assertTrue("Glass on: defPane2", (!_defPane2.isEditable())); model.setActiveDocument(doc1); Utilities.invokeAndWait( new Runnable() { public void run() { _frame._switchDefScrollPane(); } }); Utilities.clearEventQueue(); assertTrue("Doc Switch: defPane1", (!_defPane1.isEditable())); assertTrue("Doc Switch: defPane2", _defPane2.isEditable()); Utilities.invokeAndWait( new Runnable() { public void run() { _frame.hourglassOff(); } }); Utilities.clearEventQueue(); assertTrue("End: defPane1", _defPane1.isEditable()); assertTrue("End: defPane2", _defPane2.isEditable()); _log.log("testGlassPaneEditableState completed"); }
/** Tests that undoing/redoing a multi-line indent will restore the caret position. */ public void testMultilineIndentAfterScroll() throws BadLocationException, InterruptedException { final String text = "public class stuff {\n" + "private int _int;\n" + "private Bar _bar;\n" + "public void foo() {\n" + "_bar.baz(_int);\n" + "}\n" + "}\n"; final String indented = "public class stuff {\n" + " private int _int;\n" + " private Bar _bar;\n" + " public void foo() {\n" + " _bar.baz(_int);\n" + " }\n" + "}\n"; final int newPos = 20; final DefinitionsPane pane = _frame.getCurrentDefPane(); final OpenDefinitionsDocument doc = pane.getOpenDefDocument(); setConfigSetting(OptionConstants.INDENT_INC, Integer.valueOf(2)); Utilities.invokeAndWait( new Runnable() { public void run() { doc.append(text, null); pane.setCaretPosition(0); pane.endCompoundEdit(); } }); Utilities.clearEventQueue(); assertEquals("Should have inserted correctly.", text, doc.getText()); Utilities.invokeAndWait( new Runnable() { public void run() { doc.indentLines(0, doc.getLength()); } }); assertEquals("Should have indented.", indented, doc.getText()); Utilities.invokeAndWait( new Runnable() { public void run() { doc.getUndoManager().undo(); // System.err.println("New position is: " + pane.getCaretPosition()); } }); assertEquals("Should have undone.", text, doc.getText()); // int rePos = doc.getCurrentLocation(); // System.err.println("Restored position is: " + rePos); // cursor will be located at beginning of first line that is changed // assertEquals("Undo should have restored cursor position.", oldPos, rePos); Utilities.invokeAndWait( new Runnable() { public void run() { pane.setCaretPosition(newPos); doc.getUndoManager().redo(); } }); Utilities.clearEventQueue(); assertEquals("redo", indented, doc.getText()); // assertEquals("redo restores caret position", oldPos, pane.getCaretPosition()); _log.log("testMultilineIndentAfterScroll completed"); }