001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.project.ui;
041:
042: import java.awt.EventQueue;
043: import java.beans.PropertyChangeEvent;
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.EventObject;
049: import java.util.List;
050: import java.util.concurrent.CountDownLatch;
051: import javax.swing.Action;
052: import javax.swing.SwingUtilities;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectManager;
055: import org.netbeans.junit.MockServices;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.project.ui.actions.TestSupport;
058: import org.netbeans.spi.project.ui.ProjectOpenedHook;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.filesystems.URLMapper;
062: import org.openide.nodes.Node;
063: import org.openide.nodes.NodeEvent;
064: import org.openide.nodes.NodeListener;
065: import org.openide.nodes.NodeMemberEvent;
066: import org.openide.nodes.NodeReorderEvent;
067: import org.openide.util.ContextAwareAction;
068: import org.openide.util.lookup.Lookups;
069:
070: /**
071: *
072: * @author Jaroslav Tulach <jtulach@netbeans.org>
073: */
074: public class ProjectsRootNodePreferredFromPopupTest extends NbTestCase {
075: CountDownLatch first;
076: CountDownLatch middle;
077: CountDownLatch rest;
078:
079: public ProjectsRootNodePreferredFromPopupTest(String testName) {
080: super (testName);
081: }
082:
083: @Override
084: protected void setUp() throws Exception {
085: clearWorkDir();
086:
087: MockServices.setServices(TestSupport.TestProjectFactory.class);
088:
089: FileObject workDir = FileUtil.toFileObject(getWorkDir());
090: assertNotNull(workDir);
091:
092: first = new CountDownLatch(1);
093: middle = new CountDownLatch(1);
094: rest = new CountDownLatch(2);
095:
096: List<URL> list = new ArrayList<URL>();
097: List<ExtIcon> icons = new ArrayList<ExtIcon>();
098: List<String> names = new ArrayList<String>();
099: for (int i = 0; i < 10; i++) {
100: FileObject prj = TestSupport.createTestProject(workDir,
101: "prj" + i);
102: URL url = URLMapper.findURL(prj, URLMapper.EXTERNAL);
103: list.add(url);
104: names.add(url.toExternalForm());
105: icons.add(new ExtIcon());
106: TestSupport.TestProject tmp = (TestSupport.TestProject) ProjectManager
107: .getDefault().findProject(prj);
108: assertNotNull("Project found", tmp);
109: CountDownLatch down = i == 0 ? first : (i == 5 ? middle
110: : rest);
111: tmp.setLookup(Lookups
112: .singleton(new TestProjectOpenedHookImpl(down)));
113: }
114:
115: OpenProjectListSettings.getInstance().setOpenProjectsURLs(list);
116: OpenProjectListSettings.getInstance()
117: .setOpenProjectsDisplayNames(names);
118: OpenProjectListSettings.getInstance().setOpenProjectsIcons(
119: icons);
120: }
121:
122: @Override
123: protected void tearDown() throws Exception {
124: super .tearDown();
125: }
126:
127: public void testPreferencesInOpenCanBeChanged()
128: throws InterruptedException, IOException, Exception {
129: Node logicalView = new ProjectsRootNode(
130: ProjectsRootNode.LOGICAL_VIEW);
131: L listener = new L();
132: logicalView.addNodeListener(listener);
133:
134: assertEquals("10 children", 10, logicalView.getChildren()
135: .getNodesCount());
136: listener.assertEvents("None", 0);
137: assertEquals("No project opened yet", 0,
138: TestProjectOpenedHookImpl.opened);
139:
140: for (Node n : logicalView.getChildren().getNodes()) {
141: TestSupport.TestProject p = n.getLookup().lookup(
142: TestSupport.TestProject.class);
143: assertNull("No project of this type, yet", p);
144: }
145:
146: Node midNode = logicalView.getChildren().getNodes()[5];
147: {
148: TestSupport.TestProject p = midNode.getLookup().lookup(
149: TestSupport.TestProject.class);
150: assertNull("No project of this type, yet", p);
151: }
152: Project lazyP = midNode.getLookup().lookup(Project.class);
153: assertNotNull("Some project is found", lazyP);
154: assertEquals("It is lazy project", LazyProject.class, lazyP
155: .getClass());
156:
157: middle.countDown();
158: // not necessary, but to ensure middle really does not run
159: Thread.sleep(300);
160: assertEquals("Still no processing", 0,
161: TestProjectOpenedHookImpl.opened);
162:
163: // make a file of some project selected, that
164: // shall trigger OpenProjectList.preferredProject(lazyP);
165: Action[] arr = midNode.getActions(true);
166: assertEquals("Three: " + Arrays.asList(arr), 3, arr.length);
167: assertAction("Initializ", arr[0], false, midNode);
168: assertAction("Close", arr[1], true, midNode);
169: assertAction("Custom", arr[2], false, midNode);
170:
171: first.countDown();
172:
173: TestProjectOpenedHookImpl.toOpen.await();
174:
175: {
176: TestSupport.TestProject p = null;
177: for (int i = 0; i < 10; i++) {
178: Node midNode2 = logicalView.getChildren().getNodes()[5];
179: p = midNode.getLookup().lookup(
180: TestSupport.TestProject.class);
181: if (p != null) {
182: break;
183: }
184: Thread.sleep(100);
185: }
186: assertNotNull("The right project opened", p);
187: }
188:
189: rest.countDown();
190: rest.countDown();
191: OpenProjectList.waitProjectsFullyOpen();
192:
193: assertEquals("All projects opened", 10,
194: TestProjectOpenedHookImpl.opened);
195:
196: for (Node n : logicalView.getChildren().getNodes()) {
197: TestSupport.TestProject p = n.getLookup().lookup(
198: TestSupport.TestProject.class);
199: assertNotNull("Nodes have correct project of this type", p);
200: }
201: }
202:
203: private static class L implements NodeListener {
204: public List<EventObject> events = new ArrayList<EventObject>();
205:
206: public void childrenAdded(NodeMemberEvent ev) {
207: assertFalse("No event in AWT thread", EventQueue
208: .isDispatchThread());
209: events.add(ev);
210: }
211:
212: public void childrenRemoved(NodeMemberEvent ev) {
213: assertFalse("No event in AWT thread", EventQueue
214: .isDispatchThread());
215: events.add(ev);
216: }
217:
218: public void childrenReordered(NodeReorderEvent ev) {
219: assertFalse("No event in AWT thread", EventQueue
220: .isDispatchThread());
221: events.add(ev);
222: }
223:
224: public void nodeDestroyed(NodeEvent ev) {
225: assertFalse("No event in AWT thread", EventQueue
226: .isDispatchThread());
227: events.add(ev);
228: }
229:
230: public void propertyChange(PropertyChangeEvent evt) {
231: assertFalse("No event in AWT thread", EventQueue
232: .isDispatchThread());
233: events.add(evt);
234: }
235:
236: final void assertEvents(String string, int i) {
237: assertEquals(string + events, i, events.size());
238: events.clear();
239: }
240:
241: }
242:
243: private static class TestProjectOpenedHookImpl extends
244: ProjectOpenedHook {
245:
246: public static CountDownLatch toOpen = new CountDownLatch(2);
247: public static int opened = 0;
248: public static int closed = 0;
249:
250: private CountDownLatch toWaitOn;
251:
252: public TestProjectOpenedHookImpl(CountDownLatch toWaitOn) {
253: this .toWaitOn = toWaitOn;
254: }
255:
256: protected void projectClosed() {
257: closed++;
258: }
259:
260: protected void projectOpened() {
261: if (toWaitOn != null) {
262: try {
263: toWaitOn.await();
264: } catch (InterruptedException ex) {
265: throw new IllegalStateException(ex);
266: }
267: }
268: opened++;
269: toOpen.countDown();
270: }
271:
272: }
273:
274: private void assertAction(String text, Action action, boolean b,
275: Node n) throws Exception {
276: final Action clone = action instanceof ContextAwareAction ? ((ContextAwareAction) action)
277: .createContextAwareInstance(n.getLookup())
278: : action;
279:
280: assertTrue("Expecting " + text + " but was " + action, action
281: .getClass().getName().contains(text));
282:
283: class Is implements Runnable {
284: boolean is;
285:
286: public void run() {
287: is = clone.isEnabled();
288: }
289: }
290: Is enabled = new Is();
291: SwingUtilities.invokeAndWait(enabled);
292:
293: assertEquals("Enabled? " + text + " and: " + b, b, enabled.is);
294: }
295:
296: }
|