Index: openide/test/unit/src/org/openide/text/CopyPasteInEditorTest.java =================================================================== RCS file: openide/test/unit/src/org/openide/text/CopyPasteInEditorTest.java diff -N openide/test/unit/src/org/openide/text/CopyPasteInEditorTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/test/unit/src/org/openide/text/CopyPasteInEditorTest.java 10 Feb 2004 16:51:55 -0000 @@ -0,0 +1,256 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.text; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import javax.swing.Action; +import junit.framework.*; + +import org.netbeans.junit.*; + +import org.openide.util.Lookup; +import org.openide.util.actions.SystemAction; +import org.openide.util.lookup.*; + + +/** Testing different features of CloneableEditorSupport + * + * @author Jaroslav Tulach + */ +public class CopyPasteInEditorTest extends NbTestCase implements CloneableEditorSupport.Env { + /** the support to work with */ + private CloneableEditorSupport support; + /** the content of lookup of support */ + private InstanceContent ic; + + + // Env variables + private String content = ""; + private boolean valid = true; + private boolean modified = false; + /** if not null contains message why this document cannot be modified */ + private String cannotBeModified; + private java.util.Date date = new java.util.Date (); + private java.util.List/**/ propL = new java.util.ArrayList (); + private java.beans.VetoableChangeListener vetoL; + + + public CopyPasteInEditorTest (java.lang.String testName) { + super(testName); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(suite()); + System.exit (0); + } + + public static Test suite() { + TestSuite suite = new NbTestSuite(CopyPasteInEditorTest.class); + + return suite; + } + + + protected void setUp () { + ic = new InstanceContent (); + support = new CES (this, new AbstractLookup (ic)); + } + + protected boolean runInEQ () { + return true; + } + + public void testCopyPasteInteraction39678 () throws Exception { + java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard ().setContents (EmptyTransferable.EMPTY, EmptyTransferable.EMPTY); + + content = "Ahoj\nMyDoc"; + support.open (); + + javax.swing.JEditorPane[] panes = support.getOpenedPanes (); + assertNotNull ("One is opened, btw. this does not run good with current core/windows, it is better to use DummyWindowManager", panes); + assertEquals ("One is opened", 1, panes.length); + + java.awt.Component c = panes[0]; + while (c != null) { + if (c instanceof org.openide.windows.TopComponent) { + break; + } + c = c.getParent (); + } + assertNotNull ("It has to be top component", c); + + assertEquals ("And it is selected", c, org.openide.windows.TopComponent.getRegistry ().getActivated ()); + + Action paste = SystemAction.get (org.openide.actions.PasteAction.class); + Action copy = SystemAction.get (org.openide.actions.CopyAction.class); + Action cut = SystemAction.get (org.openide.actions.CutAction.class); + + javax.swing.ActionMap map = ((javax.swing.JComponent)c).getActionMap (); + + + assertFalse ("Local paste disabled", findLocalAction (map, paste).isEnabled ()); + assertFalse ("Disabled", paste.isEnabled ()); + + assertFalse ("Local copy disabled", findLocalAction (map, copy).isEnabled ()); + assertFalse ("Disabled copy", copy.isEnabled ()); + + + assertFalse ("Local cut disabled", findLocalAction (map, cut).isEnabled ()); + assertFalse ("Disabled cut", cut.isEnabled ()); + + } + + private static Action findLocalAction (javax.swing.ActionMap map, Action global) { + assertTrue (global instanceof org.openide.util.actions.CallbackSystemAction); + Object key = ((org.openide.util.actions.CallbackSystemAction)global).getActionMapKey (); + assertNotNull ("Key is there", key); + Action a = map.get (key); + assertNotNull ("Action is there", a); + System.out.println("key : " + key); + System.out.println("action: " + a); + System.out.println("state : " + a.isEnabled ()); + return a; + } + + + private void compareStreamWithString(InputStream is, String s) throws Exception{ + int i; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while ((i = is.read()) != -1) { + baos.write(i); + } + byte b1[] = baos.toByteArray(); + byte b2[] = s.getBytes(); + assertTrue("Same bytes as would result from the string: " + s, Arrays.equals(b1, b2)); + } + + // + // Implementation of the CloneableEditorSupport.Env + // + + public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { + propL.add (l); + } + public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { + propL.remove (l); + } + + public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertNull ("This is the first veto listener", vetoL); + vetoL = l; + } + public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertEquals ("Removing the right veto one", vetoL, l); + vetoL = null; + } + + public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { + return support; + } + + public String getMimeType() { + return "text/plain"; + } + + public java.util.Date getTime() { + return date; + } + + public java.io.InputStream inputStream() throws java.io.IOException { + return new java.io.ByteArrayInputStream (content.getBytes ()); + } + public java.io.OutputStream outputStream() throws java.io.IOException { + class ContentStream extends java.io.ByteArrayOutputStream { + public void close () throws java.io.IOException { + super.close (); + content = new String (toByteArray ()); + } + } + + return new ContentStream (); + } + + public boolean isValid() { + return valid; + } + + public boolean isModified() { + return modified; + } + + public void markModified() throws java.io.IOException { + if (cannotBeModified != null) { + IOException e = new IOException (); + org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified); + throw e; + } + + modified = true; + } + + public void unmarkModified() { + modified = false; + } + + /** Implementation of the CES */ + private static final class CES extends CloneableEditorSupport { + public CES (Env env, Lookup l) { + super (env, l); + } + + protected String messageName() { + return "Name"; + } + + protected String messageOpened() { + return "Opened"; + } + + protected String messageOpening() { + return "Opening"; + } + + protected String messageSave() { + return "Save"; + } + + protected String messageToolTip() { + return "ToolTip"; + } + + } + private static class EmptyTransferable implements + java.awt.datatransfer.Transferable, java.awt.datatransfer.ClipboardOwner { + public static final EmptyTransferable EMPTY = new EmptyTransferable (); + + public Object getTransferData (java.awt.datatransfer.DataFlavor flavor) throws java.awt.datatransfer.UnsupportedFlavorException, IOException { + throw new java.awt.datatransfer.UnsupportedFlavorException (flavor); + } + + public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors () { + return new java.awt.datatransfer.DataFlavor[0]; + } + + public boolean isDataFlavorSupported (java.awt.datatransfer.DataFlavor flavor) { + return false; + } + + public void lostOwnership (java.awt.datatransfer.Clipboard clipboard, java.awt.datatransfer.Transferable contents) { + } + + } // EmptyTransferable +}