This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 39678
Collapse All | Expand All

(-)openide/test/unit/src/org/openide/text/CopyPasteInEditorTest.java (+256 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.text;
15
16
import java.io.ByteArrayOutputStream;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.util.Arrays;
20
import javax.swing.Action;
21
import junit.framework.*;
22
23
import org.netbeans.junit.*;
24
25
import org.openide.util.Lookup;
26
import org.openide.util.actions.SystemAction;
27
import org.openide.util.lookup.*;
28
29
30
/** Testing different features of CloneableEditorSupport
31
 *
32
 * @author Jaroslav Tulach
33
 */
34
public class CopyPasteInEditorTest extends NbTestCase implements CloneableEditorSupport.Env {
35
    /** the support to work with */
36
    private CloneableEditorSupport support;
37
    /** the content of lookup of support */
38
    private InstanceContent ic;
39
40
    
41
    // Env variables
42
    private String content = "";
43
    private boolean valid = true;
44
    private boolean modified = false;
45
    /** if not null contains message why this document cannot be modified */
46
    private String cannotBeModified;
47
    private java.util.Date date = new java.util.Date ();
48
    private java.util.List/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList ();
49
    private java.beans.VetoableChangeListener vetoL;
50
51
    
52
    public CopyPasteInEditorTest (java.lang.String testName) {
53
        super(testName);
54
    }
55
    
56
    public static void main(java.lang.String[] args) {
57
        junit.textui.TestRunner.run(suite());
58
        System.exit (0);
59
    }
60
    
61
    public static Test suite() {
62
        TestSuite suite = new NbTestSuite(CopyPasteInEditorTest.class);
63
        
64
        return suite;
65
    }
66
    
67
68
    protected void setUp () {
69
        ic = new InstanceContent ();
70
        support = new CES (this, new AbstractLookup (ic));
71
    }
72
73
    protected boolean runInEQ () {
74
        return true;
75
    }
76
    
77
    public void testCopyPasteInteraction39678 () throws Exception {
78
        java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard ().setContents (EmptyTransferable.EMPTY, EmptyTransferable.EMPTY);
79
        
80
        content = "Ahoj\nMyDoc";
81
        support.open ();
82
        
83
        javax.swing.JEditorPane[] panes = support.getOpenedPanes ();
84
        assertNotNull ("One is opened, btw. this does not run good with current core/windows, it is better to use DummyWindowManager", panes);
85
        assertEquals ("One is opened", 1, panes.length);
86
        
87
        java.awt.Component c = panes[0];
88
        while (c != null) {
89
            if (c instanceof org.openide.windows.TopComponent) {
90
                break;
91
            }
92
            c = c.getParent ();
93
        }
94
        assertNotNull ("It has to be top component", c);
95
        
96
        assertEquals ("And it is selected", c, org.openide.windows.TopComponent.getRegistry ().getActivated ());
97
        
98
        Action paste = SystemAction.get (org.openide.actions.PasteAction.class);
99
        Action copy = SystemAction.get (org.openide.actions.CopyAction.class);
100
        Action cut = SystemAction.get (org.openide.actions.CutAction.class);
101
        
102
        javax.swing.ActionMap map = ((javax.swing.JComponent)c).getActionMap ();
103
        
104
        
105
        assertFalse ("Local paste disabled", findLocalAction (map, paste).isEnabled ());
106
        assertFalse ("Disabled", paste.isEnabled ());
107
        
108
        assertFalse ("Local copy disabled", findLocalAction (map, copy).isEnabled ());
109
        assertFalse ("Disabled copy", copy.isEnabled ());
110
        
111
        
112
        assertFalse ("Local cut disabled", findLocalAction (map, cut).isEnabled ());
113
        assertFalse ("Disabled cut", cut.isEnabled ());
114
        
115
    }
116
    
117
    private static Action findLocalAction (javax.swing.ActionMap map, Action global) {
118
        assertTrue (global instanceof org.openide.util.actions.CallbackSystemAction);
119
        Object key = ((org.openide.util.actions.CallbackSystemAction)global).getActionMapKey ();
120
        assertNotNull ("Key is there", key);
121
        Action a = map.get (key);
122
        assertNotNull ("Action is there", a);
123
        System.out.println("key   : " + key);
124
        System.out.println("action: " + a);
125
        System.out.println("state : " + a.isEnabled ());
126
        return a;
127
    }
128
    
129
    
130
    private void compareStreamWithString(InputStream is, String s) throws Exception{
131
        int i;
132
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
133
        while ((i = is.read()) != -1) {
134
            baos.write(i);
135
        }
136
        byte b1[] = baos.toByteArray();
137
        byte b2[] = s.getBytes();
138
        assertTrue("Same bytes as would result from the string: " + s, Arrays.equals(b1, b2));
139
    }
140
    
141
    //
142
    // Implementation of the CloneableEditorSupport.Env
143
    //
144
    
145
    public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
146
        propL.add (l);
147
    }    
148
    public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
149
        propL.remove (l);
150
    }
151
    
152
    public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
153
        assertNull ("This is the first veto listener", vetoL);
154
        vetoL = l;
155
    }
156
    public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
157
        assertEquals ("Removing the right veto one", vetoL, l);
158
        vetoL = null;
159
    }
160
    
161
    public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
162
        return support;
163
    }
164
    
165
    public String getMimeType() {
166
        return "text/plain";
167
    }
168
    
169
    public java.util.Date getTime() {
170
        return date;
171
    }
172
    
173
    public java.io.InputStream inputStream() throws java.io.IOException {
174
        return new java.io.ByteArrayInputStream (content.getBytes ());
175
    }
176
    public java.io.OutputStream outputStream() throws java.io.IOException {
177
        class ContentStream extends java.io.ByteArrayOutputStream {
178
            public void close () throws java.io.IOException {
179
                super.close ();
180
                content = new String (toByteArray ());
181
            }
182
        }
183
        
184
        return new ContentStream ();
185
    }
186
    
187
    public boolean isValid() {
188
        return valid;
189
    }
190
    
191
    public boolean isModified() {
192
        return modified;
193
    }
194
195
    public void markModified() throws java.io.IOException {
196
        if (cannotBeModified != null) {
197
            IOException e = new IOException ();
198
            org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified);
199
            throw e;
200
        }
201
        
202
        modified = true;
203
    }
204
    
205
    public void unmarkModified() {
206
        modified = false;
207
    }
208
209
    /** Implementation of the CES */
210
    private static final class CES extends CloneableEditorSupport {
211
        public CES (Env env, Lookup l) {
212
            super (env, l);
213
        }
214
        
215
        protected String messageName() {
216
            return "Name";
217
        }
218
        
219
        protected String messageOpened() {
220
            return "Opened";
221
        }
222
        
223
        protected String messageOpening() {
224
            return "Opening";
225
        }
226
        
227
        protected String messageSave() {
228
            return "Save";
229
        }
230
        
231
        protected String messageToolTip() {
232
            return "ToolTip";
233
        }
234
        
235
    }
236
    private static class EmptyTransferable implements 
237
    java.awt.datatransfer.Transferable, java.awt.datatransfer.ClipboardOwner {
238
        public static final EmptyTransferable EMPTY = new EmptyTransferable ();
239
        
240
        public Object getTransferData (java.awt.datatransfer.DataFlavor flavor) throws java.awt.datatransfer.UnsupportedFlavorException, IOException {
241
            throw new java.awt.datatransfer.UnsupportedFlavorException (flavor);
242
        }
243
        
244
        public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors () {
245
            return new java.awt.datatransfer.DataFlavor[0];
246
        }
247
        
248
        public boolean isDataFlavorSupported (java.awt.datatransfer.DataFlavor flavor) {
249
            return false;
250
        }
251
        
252
        public void lostOwnership (java.awt.datatransfer.Clipboard clipboard, java.awt.datatransfer.Transferable contents) {
253
        }
254
        
255
    } // EmptyTransferable
256
}

Return to bug 39678