001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 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.Iterator;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import junit.framework.TestCase;
018:
019: import org.eclipse.core.commands.operations.IUndoableOperation;
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.IWorkspaceRoot;
022: import org.eclipse.core.resources.ResourcesPlugin;
023: import org.eclipse.jface.viewers.StructuredSelection;
024: import org.eclipse.swt.widgets.TreeItem;
025: import org.eclipse.ui.IViewPart;
026: import org.eclipse.ui.IWorkbenchPage;
027: import org.eclipse.ui.IWorkbenchWindow;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.internal.navigator.filters.UpdateActiveFiltersOperation;
030: import org.eclipse.ui.navigator.CommonViewer;
031: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
032: import org.eclipse.ui.navigator.INavigatorContentService;
033: import org.eclipse.ui.navigator.INavigatorSorterService;
034: import org.eclipse.ui.tests.navigator.extension.TestDataSorter;
035: import org.eclipse.ui.tests.navigator.util.TestWorkspace;
036:
037: public class ProgrammaticOpenTest extends TestCase {
038:
039: public static final String COMMON_NAVIGATOR_INSTANCE_ID = "org.eclipse.ui.tests.navigator.ProgrammaticTestView"; //$NON-NLS-1$
040:
041: public static final String COMMON_NAVIGATOR_RESOURCE_EXT = "org.eclipse.ui.navigator.resourceContent"; //$NON-NLS-1$
042:
043: public static final String COMMON_NAVIGATOR_TEST_EXT = "org.eclipse.ui.tests.navigator.testContent"; //$NON-NLS-1$
044:
045: private Set expectedChildren = new HashSet();
046:
047: private IProject project;
048:
049: private CommonViewer viewer;
050:
051: private INavigatorContentService contentService;
052:
053: protected void setUp() throws Exception {
054:
055: TestWorkspace.init();
056:
057: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
058: project = root.getProject("Test"); //$NON-NLS-1$
059:
060: expectedChildren.add(project.getFolder("src")); //$NON-NLS-1$
061: expectedChildren.add(project.getFolder("bin")); //$NON-NLS-1$
062: expectedChildren.add(project.getFile(".project")); //$NON-NLS-1$
063: expectedChildren.add(project.getFile(".classpath")); //$NON-NLS-1$
064: expectedChildren.add(project.getFile("model.properties")); //$NON-NLS-1$
065:
066: EditorTestHelper.showView(COMMON_NAVIGATOR_INSTANCE_ID, true);
067:
068: IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
069: .getActiveWorkbenchWindow();
070: IWorkbenchPage activePage = activeWindow.getActivePage();
071:
072: IViewPart commonNavigator = activePage
073: .findView(COMMON_NAVIGATOR_INSTANCE_ID);
074: commonNavigator.setFocus();
075: viewer = (CommonViewer) commonNavigator
076: .getAdapter(CommonViewer.class);
077:
078: contentService = viewer.getNavigatorContentService();
079:
080: IUndoableOperation updateFilters = new UpdateActiveFiltersOperation(
081: viewer, new String[0], true);
082: updateFilters.execute(null, null);
083:
084: }
085:
086: public void testNavigatorRootContents() throws Exception {
087:
088: assertEquals(
089: "There should be no visible extensions for the programmatic viewer.",
090: 0, contentService.getVisibleExtensionIds().length);
091:
092: contentService.bindExtensions(
093: new String[] { COMMON_NAVIGATOR_RESOURCE_EXT }, false);
094:
095: assertEquals(
096: "There should be one visible extension for the programmatic viewer.",
097: 1, contentService.getVisibleExtensionIds().length);
098:
099: contentService.getActivationService().activateExtensions(
100: new String[] { COMMON_NAVIGATOR_RESOURCE_EXT }, true);
101:
102: viewer.refresh();
103: viewer.expandAll();
104:
105: TreeItem[] items = viewer.getTree().getItems();
106:
107: assertTrue("There should be some items.", items.length > 0); //$NON-NLS-1$
108:
109: assertEquals(project, items[0].getData());
110:
111: // we do this to force the rendering of the children of items[0]
112: viewer.setSelection(new StructuredSelection(project
113: .getFile(".project"))); //$NON-NLS-1$
114:
115: TreeItem[] children = items[0].getItems();
116:
117: assertEquals(expectedChildren.size(), children.length);
118: for (int i = 0; i < children.length; i++) {
119: assertTrue(expectedChildren.contains(children[i].getData()));
120: }
121:
122: }
123:
124: public void testNavigatorExtensionEnablement() throws Exception {
125:
126: contentService.getActivationService().activateExtensions(
127: new String[] {}, true);
128:
129: viewer.refresh();
130: viewer.expandAll();
131:
132: TreeItem[] items = viewer.getTree().getItems();
133:
134: assertTrue("There should be NO items.", items.length == 0); //$NON-NLS-1$
135:
136: contentService.getActivationService().deactivateExtensions(
137: new String[] {}, true);
138:
139: viewer.expandToLevel(2);
140:
141: items = viewer.getTree().getItems();
142:
143: assertTrue("There should be some items.", items.length > 0); //$NON-NLS-1$
144:
145: }
146:
147: public void testNavigatorSorterAccess() throws Exception {
148:
149: INavigatorSorterService sorterService = contentService
150: .getSorterService();
151:
152: INavigatorContentDescriptor testDescriptor = contentService
153: .getContentDescriptorById(COMMON_NAVIGATOR_TEST_EXT);
154:
155: Map sorters = sorterService
156: .findAvailableSorters(testDescriptor);
157:
158: assertEquals(3, sorters.size());
159:
160: for (Iterator iter = sorters.entrySet().iterator(); iter
161: .hasNext();) {
162: Map.Entry entry = (Map.Entry) iter.next();
163: assertTrue(entry.getValue() instanceof TestDataSorter);
164: }
165:
166: }
167:
168: }
|