blob: afab716d614fe426948e9683c179938e1f501f2b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.project.ui;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.junit.MockServices;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.RandomlyFails;
import org.netbeans.modules.project.ui.actions.TestSupport;
import org.netbeans.spi.project.ui.ProjectOpenedHook;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.nodes.Node;
import org.openide.nodes.NodeEvent;
import org.openide.nodes.NodeListener;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.Lookups;
/**
*
* @author Jaroslav Tulach <jtulach@netbeans.org>
*/
public class OpenProjectListSetMain2Test extends NbTestCase {
CountDownLatch down;
private ProjectsRootNode logicalView;
public OpenProjectListSetMain2Test(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
clearWorkDir();
MockServices.setServices(TestSupport.TestProjectFactory.class);
FileObject workDir = FileUtil.toFileObject(getWorkDir());
assertNotNull(workDir);
down = new CountDownLatch(1);
List<URL> list = new ArrayList<URL>();
List<ExtIcon> icons = new ArrayList<ExtIcon>();
List<String> names = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
FileObject prj = TestSupport.createTestProject(workDir, "prj" + i);
URL url = URLMapper.findURL(prj, URLMapper.EXTERNAL);
list.add(url);
names.add(url.toExternalForm());
icons.add(new ExtIcon());
TestSupport.TestProject tmp = (TestSupport.TestProject)ProjectManager.getDefault ().findProject (prj);
assertNotNull("Project found", tmp);
tmp.setLookup(Lookups.singleton(new TestProjectOpenedHookImpl(down)));
}
OpenProjectListSettings.getInstance().setOpenProjectsURLs(list);
OpenProjectListSettings.getInstance().setOpenProjectsDisplayNames(names);
OpenProjectListSettings.getInstance().setOpenProjectsIcons(icons);
//compute project root node children in sync mode
System.setProperty("test.projectnode.sync", "true");
}
@RandomlyFails // NB-Core-Build #1058
public void testBehaviourOfProjectsLogicNode() throws Throwable {
logicalView = new ProjectsRootNode(ProjectsRootNode.LOGICAL_VIEW);
L listener = new L();
logicalView.addNodeListener(listener);
assertEquals("30 children", 30, logicalView.getChildren().getNodesCount());
listener.assertEvents("None", 0);
assertEquals("No project opened yet", 0, opened);
for (Node n : logicalView.getChildren().getNodes()) {
TestSupport.TestProject p = n.getLookup().lookup(TestSupport.TestProject.class);
assertNull("No project of this type, yet", p);
Project lazy = n.getLookup().lookup(Project.class);
assertNotNull("Lazy project is there", lazy);
}
{
Project mainP = OpenProjects.getDefault().getMainProject();
assertNull("No main project yet", mainP);
}
// let project open code run
down.countDown();
toOpen.await();
if (error != null) {
throw error;
}
assertEquals("All projects opened", 30, opened);
OpenProjectList.waitProjectsFullyOpen();
for (Node n : logicalView.getChildren().getNodes()) {
TestSupport.TestProject p = n.getLookup().lookup(TestSupport.TestProject.class);
assertNotNull("Nodes have correct project of this type", p);
}
listener.assertEvents("Goal is to receive no events at all", 0);
assertTrue("Finished", OpenProjects.getDefault().openProjects().isDone());
assertFalse("Not cancelled, Finished", OpenProjects.getDefault().openProjects().isCancelled());
Project[] arr = OpenProjects.getDefault().openProjects().get();
assertEquals("30", 30, arr.length);
{
Project mainP = OpenProjects.getDefault().getMainProject();
assertNotNull("Main project is associated", mainP);
assertEquals("It is real project", TestSupport.TestProject.class, mainP.getClass());
}
if (error != null) {
throw error;
}
}
private static class L implements NodeListener {
public List<EventObject> events = new ArrayList<EventObject>();
public void childrenAdded(NodeMemberEvent ev) {
assertFalse("No event in AWT thread", EventQueue.isDispatchThread());
events.add(ev);
}
public void childrenRemoved(NodeMemberEvent ev) {
assertFalse("No event in AWT thread", EventQueue.isDispatchThread());
events.add(ev);
}
public void childrenReordered(NodeReorderEvent ev) {
assertFalse("No event in AWT thread", EventQueue.isDispatchThread());
events.add(ev);
}
public void nodeDestroyed(NodeEvent ev) {
assertFalse("No event in AWT thread", EventQueue.isDispatchThread());
events.add(ev);
}
public void propertyChange(PropertyChangeEvent evt) {
assertFalse("No event in AWT thread", EventQueue.isDispatchThread());
events.add(evt);
}
final void assertEvents(String string, int i) {
assertEquals(string + events, i, events.size());
events.clear();
}
}
public static CountDownLatch toOpen = new CountDownLatch(30);
public static int opened = 0;
public static int closed = 0;
public static Throwable error;
private class TestProjectOpenedHookImpl extends ProjectOpenedHook {
private CountDownLatch toWaitOn;
public TestProjectOpenedHookImpl(CountDownLatch toWaitOn) {
this.toWaitOn = toWaitOn;
}
protected void projectClosed() {
closed++;
}
protected void projectOpened() {
try {
if (toWaitOn != null) {
try {
toWaitOn.await();
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}
int o = 0;
for (Node n : logicalView.getChildren().getNodes()) {
TestSupport.TestProject p = n.getLookup().lookup(TestSupport.TestProject.class);
if (p != null) {
o++;
OpenProjects.getDefault().setMainProject(p);
}
}
assertEquals("There is really enough opened projects", opened, o);
opened++;
toOpen.countDown();
} catch (Throwable t) {
error = t;
}
}
}
}