KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > ExplorerPanelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.openide.explorer;
22
23 import java.awt.datatransfer.ClipboardOwner JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31
32 import javax.swing.Action JavaDoc;
33 import org.openide.DialogDisplayer;
34 import org.openide.NotifyDescriptor;
35
36 import org.openide.actions.CopyAction;
37 import org.openide.actions.CutAction;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.Repository;
40 import org.openide.nodes.Children;
41 import org.openide.nodes.Node;
42 import org.openide.nodes.AbstractNode;
43 import org.openide.util.actions.SystemAction;
44 import org.openide.util.ContextAwareAction;
45 import org.openide.util.Lookup;
46 import org.openide.util.datatransfer.PasteType;
47 import org.openide.util.io.NbMarshalledObject;
48
49
50 /**
51  * Tests for <code>ExplorerPanel</code>.
52  *
53  * @author Peter Zavadsky
54  */

55 public class ExplorerPanelTest extends NbTestCase {
56     /** context the action should work in */
57     private Lookup context;
58     /** explorer manager to work on */
59     private ExplorerManager manager;
60     
61     
62     
63     /** need it for stopActions here */
64     private ExplorerPanel ep;
65     
66     public ExplorerPanelTest(java.lang.String JavaDoc testName) {
67         super(testName);
68     }
69     
70     public static void main(java.lang.String JavaDoc[] args) {
71         junit.textui.TestRunner.run(suite());
72     }
73     
74     public static Test suite() {
75         TestSuite suite = new NbTestSuite(ExplorerPanelTest.class);
76         return suite;
77     }
78
79
80     protected boolean runInEQ() {
81         return true;
82     }
83     
84     /** Setups the tests.
85      */

86     protected final void setUp () {
87         // XXX consider replacing with MockServices
88
System.setProperty ("org.openide.util.Lookup", "org.openide.explorer.ExplorerPanelTest$Lkp");
89         
90         Object JavaDoc[] arr = createManagerAndContext (false);
91         manager = (ExplorerManager)arr[0];
92         context = (Lookup)arr[1];
93         
94     }
95     
96     /** Creates a manager to operate on.
97      */

98     protected Object JavaDoc[] createManagerAndContext (boolean confirm) {
99         ep = new ExplorerPanel (null, confirm);
100         return new Object JavaDoc[] { ep.getExplorerManager(), ep.getLookup() };
101     }
102     
103     /** Instructs the actions to stop/
104      */

105     protected void stopActions (ExplorerManager em) {
106         ep.componentDeactivated ();
107     }
108     /** Instructs the actions to start again.
109      */

110     protected void startActions(ExplorerManager em) {
111         ep.componentActivated();
112     }
113     
114     /** Tests whether the cut, copy (callback) actions are enabled/disabled
115      * in the right time, see # */

116     public void testCutCopyActionsEnabling() throws Exception JavaDoc {
117         assertTrue ("Can run only in AWT thread", java.awt.EventQueue.isDispatchThread());
118
119         TestNode enabledNode = new TestNode(true, true, true);
120         TestNode disabledNode = new TestNode(false, false, false);
121
122         manager.setRootContext(new TestRoot(
123             new Node[] {enabledNode, disabledNode}));
124
125         Action JavaDoc copy = ((ContextAwareAction)SystemAction.get(CopyAction.class)).createContextAwareInstance(context);
126         Action JavaDoc cut = ((ContextAwareAction)SystemAction.get(CutAction.class)).createContextAwareInstance(context);
127
128         assertTrue("Copy action has to be disabled", !copy.isEnabled());
129         assertTrue("Cut action has to be disabled", !cut.isEnabled());
130
131         manager.setSelectedNodes(new Node[] {enabledNode});
132
133         assertTrue("Copy action has to be enabled", copy.isEnabled());
134         assertTrue("Cut action has to be enabled", cut.isEnabled());
135         
136         copy.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
137         assertEquals ("clipboardCopy invoked", 1, enabledNode.countCopy);
138         
139         cut.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
140         assertEquals ("clipboardCut invoked", 1, enabledNode.countCut);
141         
142
143         manager.setSelectedNodes(new Node[] {disabledNode});
144
145         assertTrue("Copy action has to be disabled", !copy.isEnabled());
146         assertTrue("Cut action has to be disabled", !cut.isEnabled());
147     }
148     
149     public void testDeleteAction () throws Exception JavaDoc {
150         TestNode enabledNode = new TestNode(true, true, true);
151         TestNode enabledNode2 = new TestNode(true, true, true);
152         TestNode disabledNode = new TestNode(false, false, false);
153
154         manager.setRootContext(new TestRoot(
155             new Node[] {enabledNode, enabledNode2, disabledNode}));
156
157         Action JavaDoc delete = ((ContextAwareAction)SystemAction.get(org.openide.actions.DeleteAction.class)).createContextAwareInstance(context);
158         
159         assertTrue ("By default delete is disabled", !delete.isEnabled());
160         
161         manager.setSelectedNodes(new Node[] { enabledNode });
162         assertTrue ("Now it gets enabled", delete.isEnabled ());
163         
164         manager.setSelectedNodes (new Node[] { disabledNode });
165         assertTrue ("Is disabled", !delete.isEnabled ());
166
167         manager.setSelectedNodes(new Node[] { manager.getRootContext() });
168         assertTrue ("Delete is enabled on root", delete.isEnabled ());
169         
170         manager.setSelectedNodes(new Node[] { manager.getRootContext(), enabledNode });
171         assertTrue ("But is disabled on now, as one selected node is child of another", !delete.isEnabled ());
172         
173         manager.setSelectedNodes (new Node[] { enabledNode, enabledNode2 });
174         assertTrue ("It gets enabled", delete.isEnabled ());
175         
176         delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
177         
178         assertEquals ("Destoy was called", 1, enabledNode.countDelete);
179         assertEquals ("Destoy was called", 1, enabledNode2.countDelete);
180         
181         
182     }
183
184     public void testDeleteConfirmAction () throws Exception JavaDoc {
185         TestNode [] nodes = new TestNode [] {
186             new TestNode(true, true, true),
187             new TestNode(true, true, true, true),
188             new TestNode(true, true, true),
189             new TestNode(true, true, true, true),
190             new TestNode(false, false, false)
191         };
192
193         YesDialogDisplayer ydd = (YesDialogDisplayer)Lookup.getDefault().lookup(YesDialogDisplayer.class);
194         DialogDisplayer dd = (DialogDisplayer)Lookup.getDefault().lookup(DialogDisplayer.class);
195         assertNotNull("Custom DialogDisplayer is not set", ydd);
196         int notifyCount = ydd.getNotifyCount();
197         assertEquals("YesDialogDisplayer is current DialogDisplayer", ydd, dd);
198         
199         Object JavaDoc[] arr = createManagerAndContext (true);
200         
201         ExplorerPanel delep = new ExplorerPanel ((ExplorerManager)arr[0], true);
202         ExplorerManager delManager = delep.getExplorerManager();
203         delManager.setRootContext(new TestRoot(
204             nodes));
205
206         Action JavaDoc delete = ((ContextAwareAction)SystemAction.get(org.openide.actions.DeleteAction.class)).createContextAwareInstance((Lookup)arr[1]);
207
208         // delete should ask for confirmation
209
delManager.setSelectedNodes (new Node[] { nodes[0] });
210         assertTrue ("It gets enabled", delete.isEnabled ());
211         
212         delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
213         
214         assertEquals ("Destoy was called", 1, nodes[0].countDelete);
215         
216         assertEquals ("Confirm delete was called ", notifyCount+1, ydd.getNotifyCount());
217         
218         // but delete should not ask for confirmation if the node wants to perform handle delete
219
delManager.setSelectedNodes (new Node[] { nodes[1] });
220         assertTrue ("It gets enabled", delete.isEnabled ());
221         
222         delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
223         
224         assertEquals ("Destoy was called", 1, nodes[1].countDelete);
225         
226         assertEquals ("Confirm delete was called ", notifyCount+1, ydd.getNotifyCount()); // no next dialog
227

228         // anyway ask for confirmation if at least one node has default behaviour
229
delManager.setSelectedNodes (new Node[] { nodes[2], nodes[3] });
230         assertTrue ("It gets enabled", delete.isEnabled ());
231         
232         delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
233         
234         assertEquals ("Destoy was called", 1, nodes[2].countDelete);
235         assertEquals ("Destoy was called", 1, nodes[3].countDelete);
236         
237         assertEquals ("Confirm delete was called ", notifyCount+2, ydd.getNotifyCount()); // no next dialog
238

239     }
240
241     public void testPasteAction () throws Exception JavaDoc {
242         TestNode enabledNode = new TestNode(true, true, true);
243         TestNode disabledNode = new TestNode(false, false, false);
244
245         manager.setRootContext(new TestRoot(
246             new Node[] {enabledNode, disabledNode}));
247
248         Action JavaDoc paste = ((ContextAwareAction)SystemAction.get(org.openide.actions.PasteAction.class)).createContextAwareInstance(context);
249         assertTrue ("Disabled by default", !paste.isEnabled ());
250         
251         class PT extends PasteType {
252             public int count;
253             
254             public java.awt.datatransfer.Transferable JavaDoc paste () {
255                 count++;
256                 return null;
257             }
258         }
259         PT[] arr = { new PT () };
260         
261         enabledNode.types = arr;
262         
263         manager.setSelectedNodes (new Node[] { enabledNode });
264         
265         assertTrue ("Paste is enabled", paste.isEnabled ());
266         
267         paste.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
268         assertEquals ("Paste invoked", 1, arr[0].count);
269         
270         manager.setSelectedNodes (new Node[] { disabledNode });
271         assertTrue ("Disabled paste", !paste.isEnabled ());
272         
273         arr = new PT[] { new PT(), new PT() };
274         enabledNode.types = arr;
275         
276         manager.setSelectedNodes (new Node[] { enabledNode });
277         assertTrue ("Paste enabled again", paste.isEnabled ());
278         
279         org.openide.util.datatransfer.ExClipboard ec = (org.openide.util.datatransfer.ExClipboard)Lookup.getDefault().lookup (org.openide.util.datatransfer.ExClipboard.class);
280         assertNotNull ("Without ExClipboard this will not work much", ec);
281         
282         java.awt.datatransfer.StringSelection JavaDoc ss = new java.awt.datatransfer.StringSelection JavaDoc ("Hi");
283         ec.setContents(ss, ss);
284         
285         assertTranferables (ss, enabledNode.lastTransferable);
286         
287         stopActions (manager);
288
289         manager.setSelectedNodes(new Node[] { disabledNode });
290         assertTrue("Paste still enabled as we are not listening", paste.isEnabled());
291         
292         java.awt.datatransfer.StringSelection JavaDoc ns = new java.awt.datatransfer.StringSelection JavaDoc ("New Selection");
293         ec.setContents(ns, ns);
294
295         assertTranferables (ss, enabledNode.lastTransferable);
296         
297         startActions (manager);
298         
299         assertFalse ("The selected node is the disabled one, so now we are disabled", paste.isEnabled ());
300         assertTranferables (ns, disabledNode.lastTransferable);
301         
302         
303         ns = new java.awt.datatransfer.StringSelection JavaDoc("Another Selection");
304         ec.setContents(ns, ns);
305         assertTranferables (ns, disabledNode.lastTransferable);
306     }
307     
308     public void skipForNowtestSelectedNodesInDeserializedPanel () throws Exception JavaDoc {
309         ExplorerPanel panel = new ExplorerPanel ();
310
311         FileObject fo = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
312         
313         Node root = new SerializableNode ();
314         panel.getExplorerManager ().setRootContext (root);
315         panel.getExplorerManager ().setSelectedNodes (new Node[] {root});
316         
317         assertNotNull ("Array of selected nodes is not null.", panel.getExplorerManager ().getSelectedNodes ());
318         assertFalse ("Array of selected nodes is not empty.", panel.getExplorerManager ().getSelectedNodes ().length == 0);
319         assertEquals ("The selected node is Filesystems root.", panel.getExplorerManager ().getSelectedNodes ()[0], root);
320         
321         NbMarshalledObject mar = new NbMarshalledObject (panel);
322         Object JavaDoc obj = mar.get ();
323         ExplorerPanel deserializedPanel = (ExplorerPanel) obj;
324         
325         assertNotNull ("Deserialized panel is not null.", deserializedPanel);
326         
327         assertNotNull ("[Deserialized panel] Array of selected nodes is not null.", deserializedPanel.getExplorerManager ().getSelectedNodes ());
328         assertFalse ("[Deserialized panel] Array of selected nodes is not empty.", deserializedPanel.getExplorerManager ().getSelectedNodes ().length == 0);
329         assertEquals ("[Deserialized panel] The selected node is Filesystems root.", deserializedPanel.getExplorerManager ().getSelectedNodes ()[0], root);
330         
331     }
332     
333     /** Compares whether two transferables are the same.
334      */

335     private static void assertTranferables (java.awt.datatransfer.Transferable JavaDoc t1, java.awt.datatransfer.Transferable JavaDoc t2)
336     throws Exception JavaDoc {
337        // if (t1 == t2) return;
338

339         java.awt.datatransfer.DataFlavor JavaDoc[] arr1 = t1.getTransferDataFlavors ();
340         java.awt.datatransfer.DataFlavor JavaDoc[] arr2 = t2.getTransferDataFlavors ();
341         
342         assertEquals ("Flavors are the same",
343             Arrays.asList (arr1),
344             Arrays.asList (arr2)
345         );
346         
347         for (int i = 0; i < arr1.length; i++) {
348             Object JavaDoc f1 = convert (t1.getTransferData(arr1[i]));
349             Object JavaDoc f2 = convert (t2.getTransferData(arr1[i]));
350             
351             assertEquals (i + " flavor " + arr1[i], f1, f2);
352         }
353     }
354     
355     private static Object JavaDoc convert (Object JavaDoc obj) throws Exception JavaDoc {
356         if (obj instanceof java.io.StringReader JavaDoc) {
357             java.io.StringReader JavaDoc sr = (java.io.StringReader JavaDoc)obj;
358             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
359             for (;;) {
360                 int ch = sr.read ();
361                 if (ch == -1) return sb.toString ();
362                 sb.append ((char)ch);
363             }
364         }
365         
366         return obj;
367     }
368     
369     /** Test root node. */
370     private static class TestRoot extends AbstractNode {
371         public TestRoot(Node[] children) {
372             super(new Children.Array());
373             getChildren().add(children);
374         }
375         
376         public boolean canDestroy () {
377             return true;
378         }
379     }
380     
381     /** Node which enables both cut and copy actions. */
382     private static class TestNode extends AbstractNode {
383         public boolean canCopy;
384         public boolean canCut;
385         public boolean canDelete;
386         private boolean customDelete;
387         public PasteType[] types = new PasteType[0];
388         public java.awt.datatransfer.Transferable JavaDoc lastTransferable;
389         
390         public int countCopy;
391         public int countCut;
392         public int countDelete;
393         
394         public TestNode(boolean canCopy, boolean canCut, boolean canDelete, boolean customDelete) {
395             this (canCopy, canCut, canDelete);
396             this.customDelete = customDelete;
397         }
398         
399         public TestNode(boolean b, boolean c, boolean d) {
400             super(Children.LEAF);
401             canCopy = b;
402             canCut = c;
403             canDelete = d;
404         }
405         
406         public void destroy () {
407             countDelete++;
408         }
409         
410         public boolean canDestroy () {
411             return canDelete;
412         }
413         
414         public boolean canCopy() {
415             return canCopy;
416         }
417         public boolean canCut() {
418             return canCut;
419         }
420
421         public java.awt.datatransfer.Transferable JavaDoc clipboardCopy() throws java.io.IOException JavaDoc {
422             java.awt.datatransfer.Transferable JavaDoc retValue;
423             
424             retValue = super.clipboardCopy();
425             
426             countCopy++;
427             
428             return retValue;
429         }
430         
431         public java.awt.datatransfer.Transferable JavaDoc clipboardCut() throws java.io.IOException JavaDoc {
432             java.awt.datatransfer.Transferable JavaDoc retValue;
433             
434             retValue = super.clipboardCut();
435             
436             countCut++;
437             
438             return retValue;
439         }
440         
441         protected void createPasteTypes(java.awt.datatransfer.Transferable JavaDoc t, java.util.List JavaDoc s) {
442             this.lastTransferable = t;
443             s.addAll (Arrays.asList (types));
444         }
445
446         public Object JavaDoc getValue(String JavaDoc attributeName) {
447             if (customDelete && "customDelete".equals(attributeName)) {
448                 return Boolean.TRUE;
449             }
450             return super.getValue(attributeName);
451         }
452         
453     }
454
455     public static final class SerializableNode extends AbstractNode
456     implements Node.Handle, java.io.Externalizable JavaDoc {
457         
458         static final long serialVersionUID = 439503248509342L;
459         
460         public SerializableNode () {
461             super (Children.LEAF);
462         }
463         
464         public Handle getHandle () {
465             return this;
466         }
467         
468         public Node getNode () {
469             return this;
470         }
471         
472         public void writeExternal (java.io.ObjectOutput JavaDoc oo) {
473         }
474         public void readExternal (java.io.ObjectInput JavaDoc oi) {
475         }
476         
477         //
478
// All instances of SerializableNode are equal
479
//
480

481         public int hashCode() {
482             return getClass ().hashCode ();
483         }
484
485         public boolean equals(java.lang.Object JavaDoc obj) {
486             return obj != null && getClass ().equals (obj.getClass ());
487         }
488     } // end SerializableNode
489

490     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
491         public Lkp () {
492             this (new org.openide.util.lookup.InstanceContent ());
493         }
494         
495         private Lkp (org.openide.util.lookup.InstanceContent ic) {
496             super (ic);
497             ic.add (new Clb ("Testing clipboard"));
498             ic.add (new YesDialogDisplayer());
499         }
500     }
501     
502     private static final class Clb extends org.openide.util.datatransfer.ExClipboard {
503         public Clb (String JavaDoc s) {
504             super (s);
505         }
506
507         protected org.openide.util.datatransfer.ExClipboard.Convertor[] getConvertors() {
508             return new org.openide.util.datatransfer.ExClipboard.Convertor[0];
509         }
510         
511         public void setContents (Transferable JavaDoc t, ClipboardOwner JavaDoc o) {
512             super.setContents (t, o);
513             fireClipboardChange ();
514         }
515     }
516     
517     private static final class YesDialogDisplayer extends DialogDisplayer {
518         private int counter = 0;
519         
520         public YesDialogDisplayer() {
521             super();
522         }
523         
524         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
525             counter++;
526             return NotifyDescriptor.YES_OPTION;
527         }
528
529         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
530             return null;
531         }
532         
533         public int getNotifyCount() {
534             return counter;
535         }
536     }
537 }
538
Popular Tags