001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.navigator;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import junit.framework.TestCase;
016:
017: import org.eclipse.core.commands.operations.IUndoableOperation;
018: import org.eclipse.core.resources.IFolder;
019: import org.eclipse.core.resources.IProject;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.IAdaptable;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.swt.widgets.TreeItem;
026: import org.eclipse.ui.IViewPart;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.IWorkbenchWindow;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.internal.navigator.filters.UpdateActiveFiltersOperation;
031: import org.eclipse.ui.navigator.CommonViewer;
032: import org.eclipse.ui.navigator.INavigatorContentService;
033: import org.eclipse.ui.tests.navigator.util.TestWorkspace;
034:
035: public class PipelineTest extends TestCase {
036:
037: public static final String COMMON_NAVIGATOR_INSTANCE_ID = "org.eclipse.ui.tests.navigator.PipelineTestView"; //$NON-NLS-1$
038:
039: public static final String COMMON_NAVIGATOR_RESOURCE_EXT = "org.eclipse.ui.navigator.resourceContent"; //$NON-NLS-1$
040:
041: public static final String COMMON_NAVIGATOR_JAVA_EXT = "org.eclipse.jdt.java.ui.javaContent"; //$NON-NLS-1$
042:
043: private Set expectedChildren = new HashSet();
044:
045: private IProject project;
046:
047: private CommonViewer viewer;
048:
049: private INavigatorContentService contentService;
050:
051: protected void setUp() throws Exception {
052:
053: TestWorkspace.init();
054:
055: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
056: project = root.getProject("Test"); //$NON-NLS-1$
057:
058: expectedChildren.add(project.getFolder("src")); //$NON-NLS-1$
059: expectedChildren.add(project.getFolder("bin")); //$NON-NLS-1$
060: expectedChildren.add(project.getFile(".project")); //$NON-NLS-1$
061: expectedChildren.add(project.getFile(".classpath")); //$NON-NLS-1$
062: expectedChildren.add(project.getFile("model.properties")); //$NON-NLS-1$
063:
064: EditorTestHelper.showView(COMMON_NAVIGATOR_INSTANCE_ID, true);
065:
066: IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
067: .getActiveWorkbenchWindow();
068: IWorkbenchPage activePage = activeWindow.getActivePage();
069:
070: IViewPart commonNavigator = activePage
071: .findView(COMMON_NAVIGATOR_INSTANCE_ID);
072: commonNavigator.setFocus();
073: viewer = (CommonViewer) commonNavigator
074: .getAdapter(CommonViewer.class);
075:
076: contentService = viewer.getNavigatorContentService();
077:
078: IUndoableOperation updateFilters = new UpdateActiveFiltersOperation(
079: viewer, new String[0], true);
080: updateFilters.execute(null, null);
081:
082: }
083:
084: public void testNavigatorRootContents() throws Exception {
085:
086: assertEquals(
087: "There should be no visible extensions for the pipeline viewer.",
088: 0, contentService.getVisibleExtensionIds().length);
089:
090: contentService.bindExtensions(new String[] {
091: COMMON_NAVIGATOR_RESOURCE_EXT,
092: COMMON_NAVIGATOR_JAVA_EXT }, false);
093:
094: assertEquals(
095: "There should be two visible extension for the pipeline viewer.",
096: 2, contentService.getVisibleExtensionIds().length);
097:
098: contentService.getActivationService().activateExtensions(
099: new String[] { COMMON_NAVIGATOR_RESOURCE_EXT,
100: COMMON_NAVIGATOR_JAVA_EXT }, true);
101:
102: viewer.refresh();
103:
104: // we do this to force the rendering of the children of items[0]
105: viewer.setSelection(new StructuredSelection(project
106: .getFile(".project")), true); //$NON-NLS-1$
107:
108: TreeItem[] rootItems = viewer.getTree().getItems();
109:
110: assertEquals("There should be one item.", 1, rootItems.length); //$NON-NLS-1$
111:
112: assertTrue(
113: "The root object should be an IJavaProject, which is IAdaptable.", rootItems[0].getData() instanceof IAdaptable); //$NON-NLS-1$
114:
115: IProject adaptedProject = (IProject) ((IAdaptable) rootItems[0]
116: .getData()).getAdapter(IProject.class);
117: assertEquals(project, adaptedProject);
118:
119: IFolder sourceFolder = project.getFolder(new Path("src"));
120: viewer.add(project, sourceFolder);
121:
122: TreeItem[] projectChildren = rootItems[0].getItems();
123:
124: assertTrue(
125: "There should be some items.", projectChildren.length > 0); //$NON-NLS-1$
126:
127: for (int i = 0; i < projectChildren.length; i++) {
128: if (projectChildren[i].getData() == sourceFolder)
129: fail("The src folder should not be added as an IFolder.");
130: }
131:
132: // a new project without a Java nature should add without an issue.
133: IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
134: .getProject("New Project");
135: viewer.add(viewer.getInput(), newProject);
136:
137: rootItems = viewer.getTree().getItems();
138:
139: assertEquals("There should be two items.", 2, rootItems.length); //$NON-NLS-1$
140:
141: boolean found = false;
142: for (int i = 0; i < rootItems.length && !found; i++) {
143: if (rootItems[i].getData() instanceof IProject) {
144: IProject newProjectFromTree = (IProject) rootItems[i]
145: .getData();
146: if (newProject.equals(newProjectFromTree))
147: found = true;
148: }
149: }
150: assertTrue(found);
151:
152: }
153:
154: }
|