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.HashMap;
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.resources.IProject;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.jface.viewers.ILabelProvider;
023: import org.eclipse.jface.viewers.ITreeContentProvider;
024: import org.eclipse.ui.IViewPart;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.internal.navigator.NavigatorContentService;
029: import org.eclipse.ui.navigator.CommonViewer;
030: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
031: import org.eclipse.ui.navigator.INavigatorContentExtension;
032: import org.eclipse.ui.navigator.INavigatorContentService;
033: import org.eclipse.ui.navigator.NavigatorContentServiceFactory;
034: import org.eclipse.ui.tests.navigator.extension.TestContentProvider;
035: import org.eclipse.ui.tests.navigator.util.TestWorkspace;
036:
037: public class INavigatorContentServiceTests extends TestCase {
038:
039: public static final String COMMON_NAVIGATOR_INSTANCE_ID = "org.eclipse.ui.tests.navigator.TestView";
040:
041: public static final String TEST_EXTENSION_ID = "org.eclipse.ui.tests.navigator.testContent";
042:
043: public static final String ENFORCE_HASCHILDREN_EXTENSION_ID = "org.eclipse.ui.tests.navigator.testHasChildren";
044:
045: public static final String TEST_EXTENSION_2_ID = "org.eclipse.ui.tests.navigator.testContent2";
046:
047: public static final String RESOURCE_EXTENSION_ID = "org.eclipse.ui.navigator.resourceContent";
048:
049: private final Map expectedChildren = new HashMap();
050:
051: private IProject project;
052:
053: private INavigatorContentService contentService;
054:
055: private CommonViewer viewer;
056:
057: protected void setUp() throws Exception {
058:
059: TestWorkspace.init();
060:
061: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
062: project = root.getProject("Test");
063: Map projectMap = new HashMap();
064:
065: expectedChildren.put(project, (projectMap = new HashMap()));
066:
067: projectMap.put(project.getFolder("src"), new HashMap());
068: projectMap.put(project.getFolder("bin"), new HashMap());
069: projectMap.put(project.getFile(".project"), null);
070: projectMap.put(project.getFile(".classpath"), null);
071: projectMap.put(project.getFile("model.properties"), null);
072:
073: EditorTestHelper.showView(COMMON_NAVIGATOR_INSTANCE_ID, true);
074:
075: IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
076: .getActiveWorkbenchWindow();
077: IWorkbenchPage activePage = activeWindow.getActivePage();
078:
079: IViewPart commonNavigator = activePage
080: .findView(COMMON_NAVIGATOR_INSTANCE_ID);
081:
082: viewer = (CommonViewer) commonNavigator
083: .getAdapter(CommonViewer.class);
084: viewer.expandAll();
085:
086: contentService = viewer.getNavigatorContentService();
087: }
088:
089: protected void tearDown() throws Exception {
090: super .tearDown();
091: }
092:
093: public void testFindValidExtensions() {
094:
095: contentService.getActivationService()
096: .activateExtensions(
097: new String[] { TEST_EXTENSION_ID,
098: RESOURCE_EXTENSION_ID }, true);
099:
100: ITreeContentProvider contentServiceContentProvider = contentService
101: .createCommonContentProvider();
102:
103: ILabelProvider contentServiceLabelProvider = contentService
104: .createCommonLabelProvider();
105:
106: ITreeContentProvider[] rootContentProviders = ((NavigatorContentService) contentService)
107: .findRootContentProviders(ResourcesPlugin
108: .getWorkspace().getRoot());
109:
110: assertEquals("Ensure there is only one root content provider.",
111: 1, rootContentProviders.length);
112:
113: Set projectContentExtensions = contentService
114: .findContentExtensionsByTriggerPoint(project);
115:
116: assertEquals(
117: "Ensure there are two content providers for an IProject.",
118: 2, projectContentExtensions.size());
119:
120: boolean found = false;
121: INavigatorContentExtension ext;
122: for (Iterator i = projectContentExtensions.iterator(); i
123: .hasNext();) {
124: ext = (INavigatorContentExtension) i.next();
125: if (ext.getContentProvider() instanceof TestContentProvider) {
126:
127: TestContentProvider testContentProvider = (TestContentProvider) ext
128: .getContentProvider();
129: Object[] projectChildren = testContentProvider
130: .getChildren(project);
131: assertEquals(
132: "There should be one test-type child of the project.",
133: 1, projectChildren.length);
134: assertEquals("Parent", contentServiceLabelProvider
135: .getText(projectChildren[0]));
136: Object[] testRootChildren = contentServiceContentProvider
137: .getChildren(projectChildren[0]);
138: assertEquals(
139: "There should be one test-type child of the root test-type item.",
140: 3, testRootChildren.length);
141: found = true;
142: }
143: }
144:
145: assertTrue("The test content provider was not found.", found);
146:
147: }
148:
149: public void testDeactivateTestExtension() {
150:
151: contentService.getActivationService().activateExtensions(
152: new String[] { RESOURCE_EXTENSION_ID }, true);
153:
154: contentService.createCommonContentProvider();
155:
156: contentService.createCommonLabelProvider();
157:
158: Set rootContentProviders = contentService
159: .findRootContentExtensions(ResourcesPlugin
160: .getWorkspace().getRoot());
161:
162: assertEquals("Ensure there is only one root content provider.",
163: 1, rootContentProviders.size());
164:
165: Set projectContentExtensions = contentService
166: .findContentExtensionsByTriggerPoint(project);
167:
168: assertEquals(
169: "Ensure there is one content provider for an IProject.",
170: 1, projectContentExtensions.size());
171:
172: }
173:
174: public void testBindTestExtension() {
175:
176: INavigatorContentService contentServiceWithProgrammaticBindings = NavigatorContentServiceFactory.INSTANCE
177: .createContentService(COMMON_NAVIGATOR_INSTANCE_ID);
178: INavigatorContentDescriptor[] boundDescriptors = contentServiceWithProgrammaticBindings
179: .bindExtensions(new String[] { TEST_EXTENSION_2_ID },
180: false);
181: contentServiceWithProgrammaticBindings
182: .getActivationService()
183: .activateExtensions(
184: new String[] { RESOURCE_EXTENSION_ID,
185: TEST_EXTENSION_ID, TEST_EXTENSION_2_ID },
186: false);
187:
188: assertEquals("One descriptor should have been returned.", 1,
189: boundDescriptors.length);
190:
191: assertEquals(
192: "The declarative content service should have one fewer visible extension ids than the one created programmatically.",
193: contentService.getVisibleExtensionIds().length + 1,
194: contentServiceWithProgrammaticBindings
195: .getVisibleExtensionIds().length);
196:
197: INavigatorContentDescriptor[] visibleDescriptors = contentServiceWithProgrammaticBindings
198: .getVisibleExtensions();
199: boolean found = false;
200: for (int i = 0; i < visibleDescriptors.length; i++)
201: if (TEST_EXTENSION_2_ID.equals(visibleDescriptors[i]
202: .getId()))
203: found = true;
204: assertTrue(
205: "The programmatically bound extension should be bound.",
206: found);
207:
208: Set enabledDescriptors = contentServiceWithProgrammaticBindings
209: .findContentExtensionsByTriggerPoint(project);
210:
211: assertEquals("There should be a three extensions.", 3,
212: enabledDescriptors.size());
213:
214: }
215:
216: public void testTestExtensionVisibility() {
217: assertTrue("The test extension should be visible.",
218: contentService.getViewerDescriptor()
219: .isVisibleContentExtension(TEST_EXTENSION_ID));
220: }
221:
222: public void testResourceExtensionVisibility() {
223: assertTrue("The test extension should be visible.",
224: contentService.getViewerDescriptor()
225: .isVisibleContentExtension(
226: RESOURCE_EXTENSION_ID));
227: }
228:
229: public void testVisibleExtensionIds() {
230: String[] visibleIds = contentService.getVisibleExtensionIds();
231:
232: assertEquals("There should be three visible extensions.", 3,
233: visibleIds.length);
234:
235: for (int i = 0; i < visibleIds.length; i++) {
236: if (!TEST_EXTENSION_ID.equals(visibleIds[i])
237: && !RESOURCE_EXTENSION_ID.equals(visibleIds[i])
238: && !ENFORCE_HASCHILDREN_EXTENSION_ID
239: .equals(visibleIds[i])) {
240: assertTrue("The extension id is invalid:"
241: + visibleIds[i], false);
242: }
243: }
244:
245: INavigatorContentDescriptor[] visibleDescriptors = contentService
246: .getVisibleExtensions();
247:
248: for (int i = 0; i < visibleIds.length; i++) {
249: if (!TEST_EXTENSION_ID
250: .equals(visibleDescriptors[i].getId())
251: && !RESOURCE_EXTENSION_ID
252: .equals(visibleDescriptors[i].getId())
253: && !ENFORCE_HASCHILDREN_EXTENSION_ID
254: .equals(visibleDescriptors[i].getId())) {
255: assertTrue("The extension id is invalid:"
256: + visibleDescriptors[i].getId(), false);
257: }
258: }
259:
260: }
261: }
|