Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8602e89baf5b8ef92169b5819e5737d8cc9627ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2004, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.tests.dynamicplugins;

import java.util.Random;

import junit.framework.TestSuite;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.ObjectActionContributorManager;
import org.eclipse.ui.internal.PartSite;
import org.eclipse.ui.internal.PopupMenuExtender;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;

/**
 * @since 3.1
 */
public class ObjectContributionTests extends DynamicTestCase {

	private static final String GROUP_ID = "#OC";
	private static final String OBJECT_ACTION_ID = "org.eclipse.newOC1";
	private static final String VIEWER_ACTION_ID = "org.eclipse.newOC2";
	
	public static TestSuite suite() {
		return new TestSuite(ObjectContributionTests.class);
	}
	
	/**
	 * @param testName
	 */
	public ObjectContributionTests(String testName) {
		super(testName);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.tests.dynamicplugins.DynamicTestCase#getExtensionId()
	 */
	protected String getExtensionId() {
		return "newOC1.testDynamicOCAddition";
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.tests.dynamicplugins.DynamicTestCase#getExtensionPoint()
	 */
	protected String getExtensionPoint() {
		return IWorkbenchRegistryConstants.PL_POPUP_MENU;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.tests.dynamicplugins.DynamicTestCase#getInstallLocation()
	 */
	protected String getInstallLocation() {		
		return "data/org.eclipse.newOC1";
	}
	
	public void testViewerContributions() {
		IWorkbenchWindow window = openTestWindow(IDE.RESOURCE_PERSPECTIVE_ID);
		IWorkbenchPart part = window.getActivePage().getActivePart();
		MenuManager menu = new MenuManager();
		resetViewerMenu(menu);
		ISelectionProvider provider = new ISelectionProvider() {

			public void addSelectionChangedListener(ISelectionChangedListener listener) {
				
			}

			public ISelection getSelection() {
				return new StructuredSelection(new Random());
			}

			public void removeSelectionChangedListener(ISelectionChangedListener listener) {
			}

			public void setSelection(ISelection selection) {
			}
			
		};
		
		PopupMenuExtender extender = new PopupMenuExtender(GROUP_ID, menu, provider, part, ((PartSite)part.getSite()).getContext());
		extender.menuAboutToShow(menu);
					
		assertNull(menu.find(VIEWER_ACTION_ID));
		resetViewerMenu(menu);
		getBundle();
		
		extender.menuAboutToShow(menu);
		assertNotNull(menu.find(VIEWER_ACTION_ID));
		resetViewerMenu(menu);
		removeBundle();
		
		extender.menuAboutToShow(menu);		
		assertNull(menu.find(VIEWER_ACTION_ID));	
		
		extender.dispose();
	}
	
	/**
	 * @param menu
	 */
	private void resetViewerMenu(MenuManager menu) {
		menu.removeAll();
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(new GroupMarker(GROUP_ID));
	}

	public void testObjectContribtions() {
		IWorkbenchWindow window = openTestWindow(IDE.RESOURCE_PERSPECTIVE_ID);
		IWorkbenchPart part = window.getActivePage().getActivePart();
		ObjectActionContributorManager manager = ObjectActionContributorManager.getManager();
		IMenuManager menu = new MenuManager();
		ISelectionProvider provider = new ISelectionProvider() {

			public void addSelectionChangedListener(ISelectionChangedListener listener) {
				
			}

			public ISelection getSelection() {
				return new StructuredSelection(new Random());
			}

			public void removeSelectionChangedListener(ISelectionChangedListener listener) {
			}

			public void setSelection(ISelection selection) {
			}
			
		};

		manager.contributeObjectActions(part, menu, provider);		
		assertNull(menu.find(OBJECT_ACTION_ID));
		menu.removeAll();
		getBundle();
		
		manager.contributeObjectActions(part, menu, provider);
		assertNotNull(menu.find(OBJECT_ACTION_ID));
		menu.removeAll();
		removeBundle();
		
		manager.contributeObjectActions(part, menu, provider);		
		assertNull(menu.find(OBJECT_ACTION_ID));
		menu.removeAll();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.tests.dynamicplugins.DynamicTestCase#getMarkerClass()
	 */
	protected String getMarkerClass() {
		return "org.eclipse.ui.dynamic.MockObjectActionDelegate";
	}
}

Back to the top