KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > MoveUpActionTest


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 package org.openide.actions;
21
22 import org.netbeans.junit.*;
23 import junit.textui.TestRunner;
24 import org.openide.actions.*;
25 import org.openide.util.actions.SystemAction;
26 import org.openide.nodes.Node;
27 import org.openide.nodes.AbstractNode;
28 import org.openide.nodes.Children;
29 import org.openide.nodes.Index;
30 import java.util.Arrays JavaDoc;
31
32 /** Test behavior of MoveUpAction (also MoveDownAction and ReorderAction).
33  * @author Jesse Glick
34  */

35 public class MoveUpActionTest extends NbTestCase {
36
37     static {
38         // Get Lookup right to begin with.
39
ActionsInfraHid.class.getName();
40     }
41     
42     public MoveUpActionTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public static void main(String JavaDoc[] args) {
47         TestRunner.run(new NbTestSuite(MoveUpActionTest.class));
48     }
49     
50     private Node n, n1, n2, n3;
51     
52     protected void setUp() throws Exception JavaDoc {
53         n1 = new AbstractNode(Children.LEAF);
54         n1.setName("n1");
55         n2 = new AbstractNode(Children.LEAF);
56         n2.setName("n2");
57         n3 = new AbstractNode(Children.LEAF);
58         n3.setName("n3");
59         final Index.ArrayChildren c = new Index.ArrayChildren() {
60             {
61                 add(new Node[] {n1, n2, n3});
62             }
63             public void reorder() {
64                 reorder(new int[] {1, 2, 0});
65             }
66         };
67         n = new AbstractNode(c) {
68             {
69                 getCookieSet().add(c);
70             }
71         };
72         n.setName("n");
73     }
74     
75     /**
76      * in order to run in awt event queue
77      * fix for #39789
78      */

79     protected boolean runInEQ()
80     {
81         return true;
82     }
83     
84     public void testBasicUsage() throws Exception JavaDoc {
85         SystemAction mua = SystemAction.get(MoveUpAction.class);
86         SystemAction mda = SystemAction.get(MoveDownAction.class);
87         SystemAction roa = SystemAction.get(ReorderAction.class);
88         ActionsInfraHid.WaitPCL l = null;
89         try {
90             assertNull(ActionsInfraHid.UT.getCurrentNodes());
91             assertFalse(mua.isEnabled());
92             assertFalse(mda.isEnabled());
93             assertFalse(roa.isEnabled());
94             l = new ActionsInfraHid.WaitPCL(SystemAction.PROP_ENABLED);
95             mua.addPropertyChangeListener(l);
96             assertFalse(mua.isEnabled());
97             assertFalse(mda.isEnabled());
98             assertFalse(roa.isEnabled());
99             ActionsInfraHid.UT.setCurrentNodes(new Node[] {n});
100             if (!l.changed()) {
101                 Thread.sleep(1000);
102             }
103             l.gotit = 0;
104             assertFalse(mua.isEnabled());
105             assertFalse(mda.isEnabled());
106             assertTrue(roa.isEnabled());
107             assertEquals(Arrays.asList(new Node[] {n1, n2, n3}), Arrays.asList(n.getChildren().getNodes()));
108             roa.actionPerformed(null);
109             assertEquals(Arrays.asList(new Node[] {n3, n1, n2}), Arrays.asList(n.getChildren().getNodes()));
110             assertTrue(roa.isEnabled());
111             ActionsInfraHid.UT.setCurrentNodes(new Node[] {n1, n2});
112             if (!l.changed()) {
113                 Thread.sleep(1000);
114             }
115             l.gotit = 0;
116             assertFalse(mua.isEnabled());
117             assertFalse(mda.isEnabled());
118             assertFalse(roa.isEnabled());
119             ActionsInfraHid.UT.setCurrentNodes(new Node[] {n1});
120             if (!l.changed()) {
121                 Thread.sleep(1000);
122             }
123             l.gotit = 0;
124             assertTrue("MoveUp is enabled on a node in the middle of its parents", mua.isEnabled());
125             assertTrue(mda.isEnabled());
126             assertFalse(roa.isEnabled());
127             mua.actionPerformed(null);
128             assertEquals(Arrays.asList(new Node[] {n1, n3, n2}), Arrays.asList(n.getChildren().getNodes()));
129             if (!l.changed()) {
130                 Thread.sleep(1000);
131             }
132             l.gotit = 0;
133             assertTrue("MoveUp is turned off after a node is moved to the very top", !mua.isEnabled());
134             assertTrue(mda.isEnabled());
135             assertFalse(roa.isEnabled());
136             ActionsInfraHid.UT.setCurrentNodes(new Node[] {n2});
137             if (!l.changed()) {
138                 Thread.sleep(1000);
139             }
140             l.gotit = 0;
141             assertTrue(mua.isEnabled());
142             assertFalse(mda.isEnabled());
143             assertFalse(roa.isEnabled());
144             ActionsInfraHid.UT.setCurrentNodes(new Node[] {n3});
145             if (!l.changed()) {
146                 Thread.sleep(1000);
147             }
148             l.gotit = 0;
149             assertTrue(mua.isEnabled());
150             assertTrue(mda.isEnabled());
151             assertFalse(roa.isEnabled());
152             mda.actionPerformed(null);
153             assertEquals(Arrays.asList(new Node[] {n1, n2, n3}), Arrays.asList(n.getChildren().getNodes()));
154             if (!l.changed()) {
155                 Thread.sleep(1000);
156             }
157             l.gotit = 0;
158             assertTrue(mua.isEnabled());
159             assertFalse(mda.isEnabled());
160             assertFalse(roa.isEnabled());
161         } finally {
162             if (l != null) {
163                 mua.removePropertyChangeListener(l);
164                 mda.removePropertyChangeListener(l);
165                 roa.removePropertyChangeListener(l);
166             }
167             ActionsInfraHid.UT.setCurrentNodes(new Node[0]);
168             ActionsInfraHid.UT.setCurrentNodes(null);
169         }
170     }
171     
172 }
173
Popular Tags