示例#1
0
  /**
   * 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");
  }
示例#2
0
  /**
   * 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");
  }
示例#3
0
  /** 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");
  }