Example #1
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");
  }
Example #2
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");
  }